Skip to content
This repository has been archived by the owner on Mar 8, 2021. It is now read-only.

Commit

Permalink
PEP8 hedge.discretization.local
Browse files Browse the repository at this point in the history
  • Loading branch information
inducer committed Feb 16, 2014
1 parent 97c053e commit c2552bc
Showing 1 changed file with 36 additions and 48 deletions.
84 changes: 36 additions & 48 deletions hedge/discretization/local.py
Expand Up @@ -24,9 +24,6 @@
THE SOFTWARE.
"""




import numpy
import numpy.linalg as la
from hedge.tools import AffineMap
Expand All @@ -36,9 +33,8 @@
import hedge.mesh.element




# {{{ warping helpers ---------------------------------------------------------

class WarpFactorCalculator:
"""Calculator for Warburton's warp factor.
Expand All @@ -64,17 +60,14 @@ def __call__(self, x):
return self.int_f(x) / (1 - x ** 2)




class FaceVertexMismatch(Exception):
def __init__(self, value):
self.value = value

def __str__(self):
return repr(self.value)




class TriangleWarper:
def __init__(self, alpha, order):
self.alpha = alpha
Expand Down Expand Up @@ -115,16 +108,15 @@ def __call__(self, bp):

return reduce(add, shifts)




# }}}


TriangleBasisFunction = hedge._internal.TriangleBasisFunction
GradTriangleBasisFunction = hedge._internal.GradTriangleBasisFunction
TetrahedronBasisFunction = hedge._internal.TetrahedronBasisFunction
GradTetrahedronBasisFunction = hedge._internal.GradTetrahedronBasisFunction


# {{{ base classes ------------------------------------------------------------
# {{{ generic base classes ----------------------------------------------------
class LocalDiscretization(object):
Expand Down Expand Up @@ -171,7 +163,7 @@ def _assemble_multi_face_mass_matrix(self, face_mass_matrix):

for i_face, f_indices in enumerate(self.face_indices()):
f_indices = numpy.array(f_indices, dtype=numpy.uintp)
result[f_indices, i_face*fmm_width : (i_face + 1)*fmm_width] = \
result[f_indices, i_face*fmm_width: (i_face + 1)*fmm_width] = \
face_mass_matrix

return result
Expand Down Expand Up @@ -212,7 +204,6 @@ def find_diff_mat_permutation(self, target_idx):
"""

node_tups = self.node_tuples()
d = self.dimensions

from pytools import get_read_from_map_from_permutation

Expand All @@ -233,8 +224,6 @@ def transpose(tup, i, j):
# }}}




class OrthonormalLocalDiscretization(LocalDiscretization):
@memoize_method
def inverse_mass_matrix(self):
Expand Down Expand Up @@ -270,7 +259,9 @@ def differentiation_matrices(self):

# }}}


# {{{ simplex base class ------------------------------------------------------

class PkSimplexDiscretization(OrthonormalLocalDiscretization):
# queries -----------------------------------------------------------------
@property
Expand All @@ -295,8 +286,6 @@ def vertex_indices(self):
"""Return the list of the vertices' node indices."""
from pytools import wandering_element

result = []

node_tup_to_idx = dict(
(ituple, idx)
for idx, ituple in enumerate(self.node_tuples()))
Expand Down Expand Up @@ -367,7 +356,7 @@ def face_mass_matrix(self):

@memoize_method
def face_affine_maps(self):
"""Return an affine map for each face that maps the (n-1)-dimensional
"""Return an affine map for each face that maps the (n-1)-dimensional
face unit coordinates to their volume coordintes.
"""
face_vertex_node_index_lists = \
Expand All @@ -380,13 +369,13 @@ def find_missing_node(face_vertex_node_indices):
vertex_node_indices - set(face_vertex_node_indices))]

unit_nodes = self.unit_nodes()
sets_of_to_points = [[unit_nodes[fvni]
sets_of_to_points = [[unit_nodes[fvni]
for fvni in face_vertex_node_indices]
+ [find_missing_node(face_vertex_node_indices)]
for face_vertex_node_indices in face_vertex_node_index_lists]
from_points = sets_of_to_points[0]

# Construct an affine map that promotes face nodes into volume
# Construct an affine map that promotes face nodes into volume
# by appending -1, this should end up on the first face
dim = self.dimensions
from hedge.tools.affine import AffineMap
Expand Down Expand Up @@ -461,7 +450,7 @@ def check_and_chop(pt):

unodes = self.unit_nodes()
face_unit_nodes = [
check_and_chop(unodes[i])
check_and_chop(unodes[i])
for i in self.face_indices()[0]]

return self.get_face_index_shuffle_lookup_map_for_nodes(face_unit_nodes)
Expand Down Expand Up @@ -604,7 +593,7 @@ def face_vandermonde(self):
def volume_up_interpolation_matrix(self):
from hedge.tools.linalg import leftsolve
return leftsolve(
self.ldis.vandermonde(),
self.ldis.vandermonde(),
self.vandermonde())

@memoize_method
Expand All @@ -617,7 +606,7 @@ def diff_vandermonde_matrices(self):
@memoize_method
def volume_to_face_up_interpolation_matrix(self):
"""Generate a matrix that maps volume nodal values to
a vector of face nodal values on the quadrature grid, with
a vector of face nodal values on the quadrature grid, with
faces immediately concatenated, i.e::
[face 1 nodal data][face 2 nodal data]...
Expand All @@ -643,7 +632,7 @@ def volume_to_face_up_interpolation_matrix(self):
def face_up_interpolation_matrix(self):
from hedge.tools.linalg import leftsolve
return leftsolve(
self.ldis.face_vandermonde(),
self.ldis.face_vandermonde(),
self.face_vandermonde())

@memoize_method
Expand All @@ -667,7 +656,6 @@ def stiffness_t_matrices(self):
order="C")
for diff_vdm in self.diff_vandermonde_matrices()]


@memoize_method
def face_mass_matrix(self):
return numpy.asarray(
Expand All @@ -680,7 +668,7 @@ def face_mass_matrix(self):

@memoize_method
def multi_face_mass_matrix(self):
z= self.ldis._assemble_multi_face_mass_matrix(
z = self.ldis._assemble_multi_face_mass_matrix(
self.face_mass_matrix())
return z
# }}}
Expand All @@ -697,7 +685,6 @@ def get_face_index_shuffle_to_match(self, face_1_vertices, face_2_vertices):
self.get_face_index_shuffle_lookup_map())
# }}}


@memoize_method
def get_quadrature_info(self, exact_to_degree):
return self.QuadratureInfo(self, exact_to_degree)
Expand All @@ -707,6 +694,7 @@ def get_quadrature_info(self, exact_to_degree):
# }}}
# }}}


# {{{ interval local discretization -------------------------------------------
class IntervalDiscretization(PkSimplexDiscretization):
"""An arbitrary-order polynomial finite interval element.
Expand Down Expand Up @@ -803,7 +791,7 @@ def basis_functions(self):
r**i for i <= N
"""
from hedge.polynomial import VectorLegendreFunction
return [VectorLegendreFunction(idx[0])
return [VectorLegendreFunction(idx[0])
for idx in self.generate_mode_identifiers()]

def grad_basis_functions(self):
Expand Down Expand Up @@ -839,10 +827,9 @@ def dt_non_geometric_factor(self):
def dt_geometric_factor(self, vertices, el):
return abs(el.map.jacobian())



# }}}


# {{{ triangle local discretization -------------------------------------------
class TriangleDiscretization(PkSimplexDiscretization):
"""An arbitrary-order triangular finite element.
Expand Down Expand Up @@ -929,6 +916,7 @@ def node_tuples(self):
[]).append(node_tup)

result = []

def add_face_nodes(faces):
result.extend(faces_to_nodes.get(frozenset(faces), []))

Expand Down Expand Up @@ -1032,7 +1020,7 @@ def get_submesh_indices(self):
# {{{ basis functions -----------------------------------------------------
@memoize_method
def basis_functions(self):
"""Get a sequence of functions that form a basis of the
"""Get a sequence of functions that form a basis of the
approximation space.
The approximation space is spanned by the polynomials:::
Expand All @@ -1043,7 +1031,7 @@ def basis_functions(self):
self.generate_mode_identifiers()]

def grad_basis_functions(self):
"""Get the gradient functions of the basis_functions(),
"""Get the gradient functions of the basis_functions(),
in the same order.
"""

Expand All @@ -1063,10 +1051,9 @@ def dt_geometric_factor(self, vertices, el):
for vi1, vi2 in [(0, 1), (1, 2), (2, 0)])/2
return area / semiperimeter



# }}}


# {{{ tetrahedron local discretization ----------------------------------------
class TetrahedronDiscretization(PkSimplexDiscretization):
"""An arbitrary-order tetrahedral finite element.
Expand Down Expand Up @@ -1114,8 +1101,8 @@ class TetrahedronDiscretization(PkSimplexDiscretization):
"""

# In case you were wondering: the double backslashes in the docstring
# above are required because single backslashes serve to escape
# their subsequent newlines, and thus end up not yielding a
# above are required because single backslashes serve to escape
# their subsequent newlines, and thus end up not yielding a
# correct docstring.

dimensions = 3
Expand Down Expand Up @@ -1151,6 +1138,7 @@ def node_tuples(self):
[]).append(node_tup)

result = []

def add_face_nodes(faces):
result.extend(faces_to_nodes.get(frozenset(faces), []))

Expand Down Expand Up @@ -1194,8 +1182,8 @@ def cmp_node_tuples(nt1, nt2):
result = node_tups
#result.sort(cmp_node_tuples)

for i, nt in enumerate(result):
fnt = self.faces_for_node_tuple(nt)
#for i, nt in enumerate(result):
#fnt = self.faces_for_node_tuple(nt)
#print i, nt, fnt

return result
Expand Down Expand Up @@ -1226,9 +1214,9 @@ def barycentric_to_equilateral((lambda1, lambda2, lambda3, lambda4)):

# reflects vertices in equilateral coordinates
return numpy.array([
(-lambda1 +lambda2),
(-lambda1 -lambda2 +2*lambda3 )/sqrt(3.0),
(-lambda1 -lambda2 - lambda3 +3*lambda4)/sqrt(6.0),
(-lambda1 + lambda2),
(-lambda1 - lambda2 + 2*lambda3)/sqrt(3.0),
(-lambda1 - lambda2 - lambda3 + 3 * lambda4)/sqrt(6.0),
])

# see doc/hedge-notes.tm
Expand Down Expand Up @@ -1289,7 +1277,7 @@ def equilateral_nodes(self):

# find the vertex opposite to the current face
opp_vertex_index = (
set(all_vertex_indices)
set(all_vertex_indices)
- set(fvi)).__iter__().next()

shifted = []
Expand All @@ -1303,7 +1291,7 @@ def equilateral_nodes(self):
if abs(denom) > 1e-12:
blend /= denom
else:
blend = 0.5 # each edge gets shifted twice
blend = 0.5 # each edge gets shifted twice
break

shifted.append(ep + blend*reduce(add,
Expand Down Expand Up @@ -1333,7 +1321,7 @@ def try_add_tet(d1, d2, d3, d4):
node_dict[add_tuples(current, d3)],
node_dict[add_tuples(current, d4)],
))
except KeyError, e:
except KeyError:
pass

result = []
Expand Down Expand Up @@ -1372,6 +1360,7 @@ def grad_basis_functions(self):
"""
return [GradTetrahedronBasisFunction(*idx) for idx in
self.generate_mode_identifiers()]

@memoize_method
def face_basis(self):
from pytools import generate_nonnegative_integer_tuples_summing_to_at_most
Expand All @@ -1386,16 +1375,15 @@ def dt_geometric_factor(self, vertices, el):
result = abs(el.map.jacobian())/max(abs(fj) for fj in el.face_jacobians)
if self.order in [1, 2]:
from warnings import warn
warn("cowardly halving timestep for order 1 and 2 tets to avoid CFL issues")
warn("cowardly halving timestep for order 1 and 2 tets "
"to avoid CFL issues")
result /= 2

return result

# }}}




GEOMETRY_TO_LDIS = {
hedge.mesh.element.Interval: IntervalDiscretization,
hedge.mesh.element.Triangle: TriangleDiscretization,
Expand Down

0 comments on commit c2552bc

Please sign in to comment.