Package meshpy :: Module tet
[hide private]
[frames] | no frames]

Source Code for Module meshpy.tet

  1  from meshpy.common import MeshInfoBase, dump_array 
  2  import meshpy._tetgen as internals 
  3   
  4   
  5   
  6   
7 -class MeshInfo(internals.MeshInfo, MeshInfoBase):
8 - def set_facets(self, facets, markers=None):
9 """Set a list of simple, single-polygon factes. Unlike :meth:`set_facets_ex`, 10 :meth:`set_facets` does not allow hole and only lets you use a single 11 polygon per facet. 12 13 :param facets: a list of facets, where each facet is a single 14 polygons, represented by a list of point indices. 15 :param markers: Either None or a list of integers of the same 16 length as *facets*. Each integer is the facet marker assigned 17 to its corresponding facet. 18 19 :note: When the above says "list", any repeatable iterable 20 also accepted instead. 21 """ 22 23 if markers: 24 assert len(markers) == len(facets) 25 26 self.facets.resize(len(facets)) 27 28 for i, vlist in enumerate(facets): 29 facet = self.facets[i] 30 polys = facet.polygons 31 polys.resize(1) 32 poly = facet.polygons[0] 33 poly.vertices.resize(len(vlist)) 34 for j, pt_idx in enumerate(vlist): 35 poly.vertices[j] = pt_idx 36 37 if markers: 38 self.facet_markers.setup() 39 for i, mark in enumerate(markers): 40 self.facet_markers[i] = mark
41
42 - def set_facets_ex(self, facets, facet_holestarts=None, markers=None):
43 """Set a list of complicated factes. Unlike :meth:`set_facets`, 44 :meth:`set_facets_ex` allows holes and multiple polygons per 45 facet. 46 47 :param facets: a list of facets, where each facet is a list 48 of polygons, and each polygon is represented by a list 49 of point indices. 50 :param facet_holestarts: Either None or a list of hole starting points 51 for each facet. Each facet may have several hole starting points. 52 The mesh generator starts "eating" a hole into the facet at each 53 starting point and continues until it hits a polygon specified 54 in this facet's record in *facets*. 55 :param markers: Either None or a list of integers of the same 56 length as *facets*. Each integer is the facet marker assigned 57 to its corresponding facet. 58 59 :note: When the above says "list", any repeatable iterable 60 also accepted instead. 61 """ 62 63 if markers: 64 assert len(markers) == len(facets) 65 if facet_holestarts is not None: 66 assert len(facet_holestarts) == len(facets) 67 68 self.facets.resize(len(facets)) 69 for i_facet, poly_list in enumerate(facets): 70 facet = self.facets[i_facet] 71 polys = facet.polygons 72 73 polys.resize(len(poly_list)) 74 for i_poly, vertex_list in enumerate(poly_list): 75 poly = facet.polygons[i_poly] 76 77 poly.vertices.resize(len(vertex_list)) 78 for i_point, point in enumerate(vertex_list): 79 poly.vertices[i_point] = point 80 81 if facet_holestarts is not None: 82 hole_list = facet_holestarts[i_facet] 83 facet_holes = facet.holes 84 facet_holes.resize(len(hole_list)) 85 for i_hole, hole_start in enumerate(hole_list): 86 for i_coordinate, co_value in enumerate(hole_start): 87 facet_holes[i_hole, i_coordinate] = co_value 88 89 if markers: 90 self.facet_markers.setup() 91 for i, mark in enumerate(markers): 92 self.facet_markers[i] = mark
93
94 - def dump(self):
95 for name in ["points"]: 96 dump_array(name, getattr(self, name)) 97 for ifacet, facet in enumerate(self.faces): 98 print "facet %d:" % ifacet 99 for ipolygon, polygon in enumerate(facet.polygons): 100 print " polygon %d: vertices [%s]" % \ 101 (ipolygon, ",".join(str(vi) for vi in polygon.vertices))
102
103 - def write_vtk(self, filename):
104 import pyvtk 105 vtkelements = pyvtk.VtkData( 106 pyvtk.UnstructuredGrid( 107 self.points, 108 tetra=self.elements), 109 "Mesh") 110 vtkelements.tofile(filename)
111
112 - def set_elements(self, elements):
113 self.elements.resize(len(elements)) 114 115 for i, element in enumerate(elements): 116 self.elements[i] = element
117
118 - def set_element_constraints(self, element_constraints):
119 self.element_volumes.setup() 120 121 for i in xrange(len(self.element_volumes)): 122 if i in element_constraints: 123 self.element_volumes[i] = element_constraints[i] 124 else: 125 self.element_volumes[i] = -1
126 127 128 129
130 -class Options(internals.Options):
131 - def __init__(self, switches, **kwargs):
132 internals.Options.__init__(self) 133 if len(switches) == 0: 134 from warnings import warn 135 warn("Recommend non-empty 'switches' for crash-free meshing") 136 self.parse_switches(switches) 137 self.quiet = 1 138 139 for k, v in kwargs.iteritems(): 140 try: 141 getattr(self, k) 142 except AttributeError: 143 raise ValueError, "invalid option: %s" % k 144 else: 145 setattr(self, k, v)
146 147 148 149 150
151 -def _PBCGroup_get_transmat(self):
152 import numpy 153 return numpy.array( 154 [[self.get_transmat_entry(i,j) 155 for j in xrange(4)] 156 for i in xrange(4)])
157 158 159 160
161 -def _PBCGroup_set_transmat(self, matrix):
162 for i in xrange(4): 163 for j in xrange(4): 164 self.set_transmat_entry(i, j, matrix[i,j])
165 166 167 168
169 -def _PBCGroup_set_transform(self, matrix=None, translation=None):
170 for i in xrange(4): 171 for j in xrange(4): 172 self.set_transmat_entry(i, j, 0) 173 174 self.set_transmat_entry(3, 3, 1) 175 176 if matrix is not None: 177 for i in xrange(3): 178 for j in xrange(3): 179 self.set_transmat_entry(i, j, matrix[i][j]) 180 else: 181 for i in xrange(3): 182 self.set_transmat_entry(i, i, 1) 183 184 if translation is not None: 185 for i in xrange(3): 186 self.set_transmat_entry(i, 3, translation[i])
187 188 189 190 191 internals.PBCGroup.matrix = property( 192 _PBCGroup_get_transmat, 193 _PBCGroup_set_transmat) 194 internals.PBCGroup.set_transform = _PBCGroup_set_transform 195 196 197 198
199 -def tetrahedralize(mesh_info, options):
200 mesh = MeshInfo() 201 202 # restore "C" locale--otherwise tetgen might mis-parse stuff like "a0.01" 203 try: 204 import locale 205 except ImportErorr: 206 have_locale = False 207 else: 208 have_locale = True 209 prev_num_locale = locale.getlocale(locale.LC_NUMERIC) 210 locale.setlocale(locale.LC_NUMERIC, "C") 211 212 try: 213 internals.tetrahedralize(options, mesh_info, mesh) 214 finally: 215 # restore previous locale if we've changed it 216 if have_locale: 217 locale.setlocale(locale.LC_NUMERIC, prev_num_locale) 218 219 return mesh
220 221 222
223 -def build(mesh_info, options=Options("pq"), verbose=False, 224 attributes=False, volume_constraints=False, max_volume=None, 225 diagnose=False, insert_points=None):
226 if not verbose: 227 options.quiet = 1 228 229 if insert_points is not None: 230 options.insertaddpoints = 1 231 232 if attributes: 233 options.regionattrib = 1 234 if volume_constraints: 235 options.varvolume = 1 236 if max_volume: 237 options.fixedvolume = 1 238 options.maxvolume = max_volume 239 if diagnose: 240 options.diagnose = 1 241 242 return tetrahedralize(mesh_info, options)
243