def __init__(self, name='mesh', filename=None, prefix_dir=None, **kwargs): """Create a Mesh. Parameters ---------- name : str Object name. filename : str Loads a mesh from the specified file, if not None. prefix_dir : str If not None, the filename is relative to that directory. """ Struct.__init__(self, name=name, **kwargs) if filename is None: self.io = None self.setup_done = 0 else: io = MeshIO.any_from_filename(filename, prefix_dir=prefix_dir) output('reading mesh (%s)...' % (io.filename)) tt = time.clock() io.read(self) output('...done in %.2f s' % (time.clock() - tt)) self._set_shape_info()
def __init__(self, name='mesh', filename=None, prefix_dir=None, **kwargs): """Create a Mesh. Parameters ---------- name : str Object name. filename : str Loads a mesh from the specified file, if not None. prefix_dir : str If not None, the filename is relative to that directory. """ Struct.__init__(self, name=name, **kwargs) if filename is None: self.io = None self.setup_done = 0 else: io = MeshIO.any_from_filename(filename, prefix_dir=prefix_dir) output( 'reading mesh (%s)...' % (io.filename) ) tt = time.clock() io.read(self) output( '...done in %.2f s' % (time.clock() - tt) ) self._set_shape_info()
def skip_read_line( fd ): while 1: try: line = fd.readline().strip() except EOFError: break except: output( "reading " + fd.name + " failed!" ) raise if (len( line ) == 0) or (line[0] == '#'): continue return line
def read_list(fd, n_item, dtype): vals = [] ii = 0 while ii < n_item: line = [dtype(ic) for ic in fd.readline().split()] vals.append(line) ii += len(line) if ii > n_item: output('corrupted row?', line, ii, n_item) raise ValueError return vals
def read_tuple( file, n_item, n_tuple ): out = (); for it in range( 0, n_tuple ): token = (); for ii in range( 0, n_item ): token = token + (read_token( file ),); # print token[ii]; if (len( token[ii] ) == 0): output( "Corrupted file (token %d)!" % ii ) raise "ERR_CorruptedFile" out = out + (token,); return out;
def validate( self, required = None, other = None ): required_left_over, required_missing \ = self._validate_helper( required, other ) other_left_over, other_missing \ = self._validate_helper( other, required ) assert_( required_left_over == other_left_over ) if other_left_over and self.verbose: output( 'left over:', other_left_over ) if required_missing: raise ValueError('required missing: %s' % required_missing) return other_missing
def read_array( file, n_row, n_col, dtype ): """n_col is basically ignored, n_col == -1 -> intentionally ignored.""" val = [] for ir in range( 0, n_row ): try: while 1: line = file.readline().split() if (len( line ) == 0) or (line[0] == "#"): continue else: break except: output( "Array (%d, %d) reading failed!" % (n_row, n_col) ) raise row = [float( ii ) for ii in line] val.append( row ) val = array( val, dtype ); return val
def validate( self, required = None, other = None ): required_left_over, required_missing \ = self._validate_helper( required, other ) other_left_over, other_missing \ = self._validate_helper( other, required ) assert_( required_left_over == other_left_over ) err = False if required_missing: err = True output( 'error: required missing:', required_missing ) if other_left_over: output( 'left over:', other_left_over ) if err: raise ValueError return other_missing
def from_file(filename=None, io='auto', prefix_dir=None, omit_facets=False): """ Read a mesh from a file. Parameters ---------- filename : string or function or MeshIO instance or Mesh instance The name of file to read the mesh from. For convenience, a mesh creation function or a MeshIO instance or directly a Mesh instance can be passed in place of the file name. io : *MeshIO instance Passing *MeshIO instance has precedence over filename. prefix_dir : str If not None, the filename is relative to that directory. omit_facets : bool If True, do not read cells of lower dimension than the space dimension (faces and/or edges). Only some MeshIO subclasses support this! """ if isinstance(filename, Mesh): return filename if io == 'auto': if filename is None: output('filename or io must be specified!') raise ValueError else: io = MeshIO.any_from_filename(filename, prefix_dir=prefix_dir) output('reading mesh (%s)...' % (io.filename)) tt = time.clock() trunk = io.get_filename_trunk() mesh = Mesh(trunk) mesh = io.read(mesh, omit_facets=omit_facets) output('...done in %.2f s' % (time.clock() - tt)) mesh._set_shape_info() return mesh
def from_file(filename=None, io='auto', prefix_dir=None, omit_facets=False): """ Read a mesh from a file. Parameters ---------- filename : string or function or MeshIO instance or Mesh instance The name of file to read the mesh from. For convenience, a mesh creation function or a MeshIO instance or directly a Mesh instance can be passed in place of the file name. io : *MeshIO instance Passing *MeshIO instance has precedence over filename. prefix_dir : str If not None, the filename is relative to that directory. omit_facets : bool If True, do not read cells of lower dimension than the space dimension (faces and/or edges). Only some MeshIO subclasses support this! """ if isinstance(filename, Mesh): return filename if io == 'auto': if filename is None: output( 'filename or io must be specified!' ) raise ValueError else: io = MeshIO.any_from_filename(filename, prefix_dir=prefix_dir) output('reading mesh (%s)...' % (io.filename)) tt = time.clock() trunk = io.get_filename_trunk() mesh = Mesh(trunk) mesh = io.read(mesh, omit_facets=omit_facets) output('...done in %.2f s' % (time.clock() - tt)) mesh._set_shape_info() return mesh
def fix_double_nodes(coor, ngroups, conns, eps): """ Detect and attempt fixing double nodes in a mesh. The double nodes are nodes having the same coordinates w.r.t. precision given by `eps`. """ n_nod, dim = coor.shape cmap = find_map(coor, nm.zeros((0, dim)), eps=eps, allow_double=True) if cmap.size: output('double nodes in input mesh!') output('trying to fix...') while cmap.size: print cmap.size # Just like in Variable.equation_mapping()... ii = nm.argsort(cmap[:, 1]) scmap = cmap[ii] eq = nm.arange(n_nod) eq[scmap[:, 1]] = -1 eqi = eq[eq >= 0] eq[eqi] = nm.arange(eqi.shape[0]) remap = eq.copy() remap[scmap[:, 1]] = eq[scmap[:, 0]] print coor.shape coor = coor[eqi] ngroups = ngroups[eqi] print coor.shape ccs = [] for conn in conns: ccs.append(remap[conn]) conns = ccs cmap = find_map(coor, nm.zeros((0, dim)), eps=eps, allow_double=True) output('...done') return coor, ngroups, conns
def fix_double_nodes(coor, ngroups, conns, eps): """ Detect and attempt fixing double nodes in a mesh. The double nodes are nodes having the same coordinates w.r.t. precision given by `eps`. """ n_nod, dim = coor.shape cmap = find_map( coor, nm.zeros( (0,dim) ), eps = eps, allow_double = True ) if cmap.size: output('double nodes in input mesh!') output('trying to fix...') while cmap.size: print cmap.size # Just like in Variable.equation_mapping()... ii = nm.argsort( cmap[:,1] ) scmap = cmap[ii] eq = nm.arange( n_nod ) eq[scmap[:,1]] = -1 eqi = eq[eq >= 0] eq[eqi] = nm.arange( eqi.shape[0] ) remap = eq.copy() remap[scmap[:,1]] = eq[scmap[:,0]] print coor.shape coor = coor[eqi] ngroups = ngroups[eqi] print coor.shape ccs = [] for conn in conns: ccs.append( remap[conn] ) conns = ccs cmap = find_map( coor, nm.zeros( (0,dim) ), eps = eps, allow_double = True ) output('...done') return coor, ngroups, conns