def __init__(self): f = FoamFile('faces') lines = f.read_foam_file() self.d = convert_to_dict(self, lines)
def __init__(self): foam_points = FoamFile('points') lines = foam_points.read_foam_file() self.d = convert_to_dict(self, lines)
def read_openfoam(self, block_mesh_name='blockMeshDict'): """reads the BlockMesh file""" self.log.info('block_mesh_name = %r' % block_mesh_name) foam_file = FoamFile(block_mesh_name) foam_lines = foam_file.read_foam_file() foam_file_dict = convert_to_dict(self, foam_lines, debug=True) #print(write_dict(foam_file_dict)) #unused_keys = foam_file_dict.keys() vertices = foam_file_dict['vertices'] blocks = foam_file_dict['blocks'] boundaries = foam_file_dict['boundary'] #self.log.info(boundaries) nodes = [] for unused_ivertex, vertex in vertices.items(): x, y, z = vertex.strip('() ').split() nodes.append([x, y, z]) hexas = [] npoints = [] grading = [] for key, block in blocks.items(): hexa, npointsi, gradingi, blank = block.split(')') assert blank == '', '%r' % blank hexa = hexa.replace('hex (', '').split() npointsi = npointsi.strip('( ').split() gradingi = gradingi.split('(')[1].split() #self.log.info('%s %s' % (npointsi, gradingi)) #self.log.info(hexa) hexa = [int(i) for i in hexa] #print('hexa', key, hexa) hexas.append(hexa) npoints.append(npointsi) grading.append(gradingi) bc_map = { 'wall': 0, 'patch': 1, 'symmetry': 2, } iname_to_quads = defaultdict(list) inames = [] bcs = [] iname = 1 iname_to_name = {} iname_to_type = {} quads = [] for key, boundary in boundaries.items(): #print key, boundary.keys() Type = boundary['type'] bc = bc_map[Type] #bcs.append(bc) faces = boundary['faces'] iname_to_name[iname] = key iname_to_type[iname] = Type for unused_iface, face in faces.items(): quad = face.strip('() ').split() quad = [int(i) for i in quad] quads.append(quad) iname_to_quads[iname].append(quad) inames.append(iname) bcs.append(bc) self.log.info('iname=%s -> %s; bc=%s -> %s' % (iname, key, bc, Type)) iname += 1 nodes = np.array(nodes, dtype='float32') self.nodes = nodes hexas = np.array(hexas, dtype='int32') npoints = np.array(npoints, dtype='int32') grading = np.array(grading, dtype='float32') self.hexas = hexas self.npoints = npoints self.grading = grading quads = np.array(quads, dtype='int32') inames = np.array(inames, dtype='int32') bcs = np.array(bcs, dtype='int32') self.quads = quads self.iname_to_quads = iname_to_quads self.inames = inames self.bcs = bcs self.iname_to_name = iname_to_name self.iname_to_type = iname_to_type #bdf_filename = 'blockMesh.bdf' #self.write_bdf(bdf_filename, nodes, hexas) return nodes, hexas, quads, inames, bcs
def read_openfoam(self, point_filename, face_filename, boundary_filename): assert os.path.exists(face_filename), print_bad_path(face_filename) assert os.path.exists(point_filename), print_bad_path(point_filename) assert os.path.exists(boundary_filename), print_bad_path( boundary_filename) print('face_filename = %r' % face_filename) print('point_filename = %r' % point_filename) print('boundary_filename = %r' % boundary_filename) assert 'faces' in face_filename, face_filename assert 'points' in point_filename, point_filename assert 'boundary' in boundary_filename, boundary_filename print('starting Boundary') p = PointFile(log=None, debug=True) #from PyFoam.RunDictionary.ParsedBlockMeshDict import ParsedBlockMeshDict #print(dir(f)) f = FaceFile(log=None, debug=True) b = BoundaryFile(log=None, debug=False) boundaries = b.read_boundary_file(boundary_filename) if 0: b = FoamFile(boundary_filename, log=p.log) print('getting lines') blines = b.read_foam_file() print('converting') bd = convert_to_dict(b, blines, debug=True) del blines print('getting npoints') #print write_dict(d) #------------------------------------------- # count number of faces by looking at the boundary info # so we can allocate faces2 nfaces2 = 0 ifaces_to_read = [] #f_boundary_faces = open('boundary_faces.py', 'wb') for name, boundary in iteritems(boundaries): # type patch; # 0 # nFaces nFaces; # 1 # startFace 777700; # 2 print('boundary[%s] = %s' % (name, boundary)) nfacesi = boundary[1] startface = int(boundary[2]) nfaces2 += nfacesi new_faces = list(arange(nfacesi, dtype='int32') + startface) #f_boundary_faces.write('boundary_faces[%s, %s] = %s\n' % (name, len(new_faces), new_faces)) ifaces_to_read += new_faces print('nfaces2 = ', nfaces2) ifaces_to_read = ravel(ifaces_to_read) assert len( ifaces_to_read) == nfaces2, 'len(ifaces_to_read)=%s nfaces2=%s' % ( ifaces_to_read.shape, nfaces2) print(ifaces_to_read) faces = f.read_face_file(face_filename, ifaces_to_read=ifaces_to_read) #faces = f.read_face_file(face_filename, ifaces_to_read=None) del ifaces_to_read if 0: # doesn't work for some reason... # we want to only plot a subset of faces to reduce the data set # that works, but we also need to decrease the number of nodes (they take wayyy too long) # so we take our faces, get the unique nodes # sort them so they're consistent with the order in the file using the same block of code # that works in the face reader, but it still fails for some reason... # after this step, we renumber the faces with the adjusted node ids ipoints_to_read = unique(faces.ravel()) print('nnodes = %s' % len(ipoints_to_read)) ipoints_to_read.sort() print('ipoints_to_read = %s' % ipoints_to_read) else: ipoints_to_read = None nodes = p.read_point_file(point_filename, ipoints_to_read=ipoints_to_read) if ipoints_to_read is not None: nid_to_ipoint = {} for i, nid in enumerate(ipoints_to_read): nid_to_ipoint[nid] = i print(faces, faces.max()) for i, face in enumerate(faces): #print('face = %s' % face) faces[i, 0] = nid_to_ipoint[faces[i, 0]] faces[i, 1] = nid_to_ipoint[faces[i, 1]] faces[i, 2] = nid_to_ipoint[faces[i, 2]] #print('faces[%i] = %s' % (i, faces[i, :])) print(faces, faces.max()) print('done...') del ipoints_to_read del nid_to_ipoint #------------------------------------------- # keep only the required faces iface = 0 #faces2 = zeros((nfaces2, 4), dtype='int32') names = zeros(nfaces2, dtype='int32') iname = 1 snames = [None] * (len(boundaries) + 1) self.log.info('') for name, boundary in iteritems(boundaries): self.log.info('iname=%s name=%s boundary=%s' % (iname, name, boundary)) # type patch; # nFaces nFaces; # startFace 777700; try: Type = boundary[0] nfacesi = int(boundary[1]) startface = int(boundary[2]) except: print(boundary.keys()) raise #faces2[iface:iface+nfacesi] = faces[startface:startface + nfacesi] names[iface:iface + nfacesi] = iname snames[iname] = name iface += nfacesi iname += 1 #del faces quads = faces if 0: f_boundary_faces.write('\n\n---Faces----\n') for iface, face in enumerate(faces): pid = names[iface] name = snames[pid] f_boundary_faces.write( '%i (%i %i %i %i) pid=%s name=%s\n' % (iface, face[0], face[1], face[2], face[3], pid, name)) f_boundary_faces.write('\n\n---First Faces----\n') pid_save = set([]) for iface, face in enumerate(faces): pid = names[iface] if pid not in pid_save: name = snames[pid] f_boundary_faces.write( '%i (%i %i %i %i) pid=%s name=%s\n' % (iface, face[0], face[1], face[2], face[3], pid, name)) pid_save.add(pid) # only save the unique nodes # ... #unodes = unique(quads.ravel()) #unodes.sort() #nodes = nodes[unodes, :] # renumber the nodes on the faces # ... self.log.debug('names=%s; max=%s min=%s' % (names, names.max(), names.min())) print('done with Boundary') #self.nodes = nodes return nodes, quads, names
def read_openfoam(self, blockMesh_name='blockMeshDict'): print('blockMesh_name = %r' % blockMesh_name) f = FoamFile(blockMesh_name) lines = f.read_foam_file() d = convert_to_dict(self, lines, debug=True) #print write_dict(d) keys = d.keys() vertices = d['vertices'] blocks = d['blocks'] boundaries = d['boundary'] #print(boundaries) nodes = [] for ivertex, vertex in iteritems(vertices): x, y, z = vertex.strip('() ').split() nodes.append([x, y, z]) hexas = [] npoints = [] grading = [] for key, block in iteritems(blocks): hexa, npointsi, gradingi, blank = block.split(')') assert blank == '', '%r' % blank hexa = hexa.replace('hex (', '').split() npointsi = npointsi.strip('( ').split() gradingi = gradingi.split('(')[1].split() #print npointsi, gradingi #print(hexa) hexa = [int(i) for i in hexa] #print('hexa', key, hexa) hexas.append(hexa) npoints.append(npointsi) grading.append(gradingi) bc_map = { 'wall' : 0, 'patch' : 1, 'symmetry' : 2, } iname_to_quads = defaultdict(list) inames = [] bcs = [] iname = 1 iname_to_name = {} iname_to_type = {} quads = [] for key, boundary in iteritems(boundaries): #print key, boundary.keys() Type = boundary['type'] bc = bc_map[Type] #bcs.append(bc) faces = boundary['faces'] iname_to_name[iname] = key iname_to_type[iname] = Type for iface, face in iteritems(faces): quad = face.strip('() ').split() quad = [int(i) for i in quad] quads.append(quad) iname_to_quads[iname].append(quad) inames.append(iname) bcs.append(bc) self.log.info('iname=%s -> %s; bc=%s -> %s' % (iname, key, bc, Type)) iname += 1 nodes = array(nodes, dtype='float32') self.nodes = nodes hexas = array(hexas, dtype='int32') npoints = array(npoints, dtype='int32') grading = array(grading, dtype='float32') self.hexas = hexas self.npoints = npoints self.grading = grading quads = array(quads, dtype='int32') inames = array(inames, dtype='int32') bcs = array(bcs, dtype='int32') self.iname_to_quads = iname_to_quads self.inames = inames self.bcs = bcs self.iname_to_name = iname_to_name self.iname_to_type = iname_to_type bdf_filename = 'blockMesh.bdf' #self.write_bdf(bdf_filename, nodes, hexas) return nodes, hexas, quads, inames, bcs