def free_write(self, freename=None): cubit.cmd('set info off') cubit.cmd('set echo off') cubit.cmd('set journal off') from sets import Set normal = (0, 0, 1) if not freename: freename = self.freename freehex = open(freename, 'w') print 'Writing ' + freename + '.....' # # for block, flag in zip(self.block_bc, self.block_bc_flag): if block == self.topography: name = cubit.get_exodus_entity_name('block', block) print 'free surface (topography) block name:', name, 'id:', block quads_all = cubit.get_block_faces(block) print ' number of faces = ', len(quads_all) dic_quads_all = dict(zip(quads_all, quads_all)) freehex.write('%10i\n' % len(quads_all)) list_hex = cubit.parse_cubit_list('hex', 'all') for h in list_hex: faces = cubit.get_sub_elements('hex', h, 2) for f in faces: if dic_quads_all.has_key(f): #print f txt = self.create_facenode_string(h, f, normal, cknormal=True) freehex.write(txt) freehex.close() elif block == self.free: name = cubit.get_exodus_entity_name('block', block) print 'free surface block name:', name, 'id:', block quads_all = cubit.get_block_faces(block) print ' number of faces = ', len(quads_all) dic_quads_all = dict(zip(quads_all, quads_all)) freehex.write('%10i\n' % len(quads_all)) list_hex = cubit.parse_cubit_list('hex', 'all') for h in list_hex: faces = cubit.get_sub_elements('hex', h, 2) for f in faces: if dic_quads_all.has_key(f): txt = self.create_facenode_string(h, f, normal, cknormal=False) freehex.write(txt) freehex.close() cubit.cmd('set info on') cubit.cmd('set echo on')
def free_write(self,freename=None): # free surface cubit.cmd('set info off') cubit.cmd('set echo off') cubit.cmd('set journal off') from sets import Set normal=(0,0,1) if not freename: freename=self.freename # writes free surface file print 'Writing '+freename+'.....' freehex=open(freename,'w') # # searches block definition with name face_topo for block,flag in zip(self.block_bc,self.block_bc_flag): if block == self.topography: name=cubit.get_exodus_entity_name('block',block) print 'free surface (topography) block name:',name,'id:',block quads_all=cubit.get_block_faces(block) print ' number of faces = ',len(quads_all) dic_quads_all=dict(zip(quads_all,quads_all)) freehex.write('%10i\n' % len(quads_all)) list_hex=cubit.parse_cubit_list('hex','all') for h in list_hex: faces=cubit.get_sub_elements('hex',h,2) for f in faces: if dic_quads_all.has_key(f): #print f txt=self.create_facenode_string(h,f,normal,cknormal=True) freehex.write(txt) freehex.close() elif block == self.free: name=cubit.get_exodus_entity_name('block',block) print 'free surface block name:',name,'id:',block quads_all=cubit.get_block_faces(block) print ' number of faces = ',len(quads_all) dic_quads_all=dict(zip(quads_all,quads_all)) freehex.write('%10i\n' % len(quads_all)) list_hex=cubit.parse_cubit_list('hex','all') for h in list_hex: faces=cubit.get_sub_elements('hex',h,2) for f in faces: if dic_quads_all.has_key(f): txt=self.create_facenode_string(h,f,normal,cknormal=False) freehex.write(txt) freehex.close() print 'Ok' cubit.cmd('set info on') cubit.cmd('set echo on')
def free_write(self, freename=None): # free surface cubit.cmd('set info off') cubit.cmd('set echo off') cubit.cmd('set journal off') from sets import Set normal = (0, 0, 1) if not freename: freename = self.freename # writes free surface file print 'Writing ' + freename + '.....' freehex = open(freename, 'w') # searches block definition with name face_topo for block, flag in zip(self.block_bc, self.block_bc_flag): if block == self.topography: name = cubit.get_exodus_entity_name('block', block) print ' block name:', name, 'id:', block quads_all = cubit.get_block_faces(block) print ' number of faces = ', len(quads_all) dic_quads_all = dict(zip(quads_all, quads_all)) freehex.write('%10i\n' % len(quads_all)) list_hex = cubit.parse_cubit_list('hex', 'all') for h in list_hex: faces = cubit.get_sub_elements('hex', h, 2) for f in faces: if dic_quads_all.has_key(f): #print f nodes = cubit.get_connectivity('Face', f) nodes_ok = self.normal_check(nodes, normal) txt='%10i %10i %10i %10i %10i\n' % (h,nodes_ok[0],\ nodes_ok[1],nodes_ok[2],nodes_ok[3]) freehex.write(txt) freehex.close() print 'Ok' cubit.cmd('set info on') cubit.cmd('set echo on')
def free_write(self,freename=None): # free surface cubit.cmd('set info off') cubit.cmd('set echo off') cubit.cmd('set journal off') from sets import Set normal=(0,0,1) if not freename: freename=self.freename # writes free surface file print 'Writing '+freename+'.....' freehex=open(freename,'w') # searches block definition with name face_topo for block,flag in zip(self.block_bc,self.block_bc_flag): if block == self.topography: name=cubit.get_exodus_entity_name('block',block) print ' block name:',name,'id:',block quads_all=cubit.get_block_faces(block) print ' number of faces = ',len(quads_all) dic_quads_all=dict(zip(quads_all,quads_all)) freehex.write('%10i\n' % len(quads_all)) list_hex=cubit.parse_cubit_list('hex','all') for h in list_hex: faces=cubit.get_sub_elements('hex',h,2) for f in faces: if dic_quads_all.has_key(f): #print f nodes=cubit.get_connectivity('Face',f) nodes_ok=self.normal_check(nodes,normal) txt='%10i %10i %10i %10i %10i\n' % (h,nodes_ok[0],\ nodes_ok[1],nodes_ok[2],nodes_ok[3]) freehex.write(txt) freehex.close() print 'Ok' cubit.cmd('set info on') cubit.cmd('set echo on')
def mesh_analysis(self, frequency): from sets import Set cubit.cmd('set info off') cubit.cmd('set echo off') cubit.cmd('set journal off') bins_d = [0.0001] + range(0, int(frequency) + 1) + [1000] bins_u = bins_d[1:] dt = [] ed_dt = [] r = [] ed_r = [] nstart = cubit.get_next_sideset_id() command = "del sideset all" cubit.cmd(command) for bin_d, bin_u in zip(bins_d, bins_u): nsideset = cubit.get_next_sideset_id() command = 'create sideset ' + str(nsideset) cubit.cmd(command) command = "sideset " + str(nsideset) + " name " + "'ratio-[" + str( bin_d) + "_" + str(bin_u) + "['" cubit.cmd(command) nend = cubit.get_next_sideset_id() sidelist = range(nstart, nend) for block in self.block_mat: name = cubit.get_exodus_entity_name('block', block) velocity = self.material[name][1] if velocity > 0: faces = cubit.get_block_faces(block) edges = [] for face in faces: es = cubit.get_sub_elements("face", face, 1) edges = edges + list(es) edges = Set(edges) dtstore,edgedtstore,ratiostore,edgeratiostore=self.seismic_resolution(edges,\ velocity,bins_d,bins_u,sidelist) dt.append(dtstore) ed_dt.append(edgedtstore) r.append(ratiostore) ed_r.append(edgeratiostore) self.ddt = zip(ed_dt, dt) self.dr = zip(ed_r, r) def sorter(x, y): return cmp(x[1], y[1]) self.ddt.sort(sorter) self.dr.sort(sorter) print self.ddt, self.dr print 'Deltat minimum => edge:' + str(self.ddt[0][0]) + ' dt: ' + str( self.ddt[0][1]) print 'minimum frequency resolved => edge:' + str( self.dr[0][0]) + ' frequency: ' + str(self.dr[0][1]) return self.ddt[0], self.dr[0]
def mesh_analysis(self, frequency): from sets import Set cubit.cmd("set info off") cubit.cmd("set echo off") cubit.cmd("set journal off") bins_d = [0.0001] + range(0, int(frequency) + 1) + [1000] bins_u = bins_d[1:] dt = [] ed_dt = [] r = [] ed_r = [] nstart = cubit.get_next_sideset_id() command = "del sideset all" cubit.cmd(command) for bin_d, bin_u in zip(bins_d, bins_u): nsideset = cubit.get_next_sideset_id() command = "create sideset " + str(nsideset) cubit.cmd(command) command = "sideset " + str(nsideset) + " name " + "'ratio-[" + str(bin_d) + "_" + str(bin_u) + "['" cubit.cmd(command) nend = cubit.get_next_sideset_id() sidelist = range(nstart, nend) for block in self.block_mat: name = cubit.get_exodus_entity_name("block", block) velocity = self.material[name][1] if velocity > 0: faces = cubit.get_block_faces(block) edges = [] for face in faces: es = cubit.get_sub_elements("face", face, 1) edges = edges + list(es) edges = Set(edges) dtstore, edgedtstore, ratiostore, edgeratiostore = self.seismic_resolution( edges, velocity, bins_d, bins_u, sidelist ) dt.append(dtstore) ed_dt.append(edgedtstore) r.append(ratiostore) ed_r.append(edgeratiostore) self.ddt = zip(ed_dt, dt) self.dr = zip(ed_r, r) def sorter(x, y): return cmp(x[1], y[1]) self.ddt.sort(sorter) self.dr.sort(sorter) print self.ddt, self.dr print "Deltat minimum => edge:" + str(self.ddt[0][0]) + " dt: " + str(self.ddt[0][1]) print "minimum frequency resolved => edge:" + str(self.dr[0][0]) + " frequency: " + str(self.dr[0][1]) return self.ddt[0], self.dr[0]
def nummaterial_write(self,nummaterial_name): """ Write material features on file : nummaterial_name """ print 'Writing '+nummaterial_name+'.....' nummaterial = open(nummaterial_name,'w') # Create the file "nummaterial_name" and open it for block in self.block_mat: # For each 2D block name = cubit.get_exodus_entity_name('block',block) # Extract the name of the block lineToWrite = str(self.material[name][0])+" 1 "+str(self.material[name][1])+" " \ +str(self.material[name][2])+" "+str(self.material[name][3])+" "+str(self.material[name][4])+" " \ +str(self.material[name][5])+"\n" # flag rho vp vs rho Qflag anisotropy_flag nummaterial.write(lineToWrite) #nummaterial.write(self.tomo(self.material[name][0],self.material[name][2])) nummaterial.close() print 'Ok'
def find_boundaries(outputfolder): vtk_found = False nodeset = cubit.get_nodeset_id_list() for n in nodeset: name = cubit.get_exodus_entity_name('nodeset', n) if name.find('vtk') >= 0: vtk_found = True print('found interface nodes to create vtk') values = name.split(" ") title = values[1] nodes_vtk = cubit.get_nodeset_nodes(n) write_vtk_file(nodes_vtk, title, outputfolder) if not vtk_found: print('no interface nodes to create vtk found')
def surface_write(self, pathdir=None): # optional surfaces, e.g. moho_surface # should be created like e.g.: # > block 10 face in surface 2 # > block 10 name 'moho_surface' import re from sets import Set for block in self.block_bc: if block != self.topography: name = cubit.get_exodus_entity_name('block', block) # skips block names like face_abs**, face_topo** if re.search('abs', name): continue elif re.search('topo', name): continue elif re.search('surface', name): filename = pathdir + name + '_file' else: continue # gets face elements print ' surface block name: ', name, 'id: ', block quads_all = cubit.get_block_faces(block) print ' face = ', len(quads_all) if len(quads_all) == 0: continue # writes out surface infos to file print 'Writing ' + filename + '.....' surfhex_local = open(filename, 'w') dic_quads_all = dict(zip(quads_all, quads_all)) # writes number of surface elements surfhex_local.write('%10i\n' % len(quads_all)) # writes out element node ids list_hex = cubit.parse_cubit_list('hex', 'all') for h in list_hex: faces = cubit.get_sub_elements('hex', h, 2) for f in faces: if dic_quads_all.has_key(f): nodes = cubit.get_connectivity('Face', f) txt='%10i %10i %10i %10i %10i\n' % (h,nodes[0],\ nodes[1],nodes[2],nodes[3]) surfhex_local.write(txt) # closes file surfhex_local.close() print 'Ok'
def surface_write(self,pathdir=None): # optional surfaces, e.g. moho_surface # should be created like e.g.: # > block 10 face in surface 2 # > block 10 name 'moho_surface' import re from sets import Set for block in self.block_bc : if block != self.topography: name=cubit.get_exodus_entity_name('block',block) # skips block names like face_abs**, face_topo** if re.search('abs',name): continue elif re.search('topo',name): continue elif re.search('surface',name): filename=pathdir+name+'_file' else: continue # gets face elements print ' surface block name: ',name,'id: ',block quads_all=cubit.get_block_faces(block) print ' face = ',len(quads_all) if len(quads_all) == 0 : continue # writes out surface infos to file print 'Writing '+filename+'.....' surfhex_local=open(filename,'w') dic_quads_all=dict(zip(quads_all,quads_all)) # writes number of surface elements surfhex_local.write('%10i\n' % len(quads_all)) # writes out element node ids list_hex=cubit.parse_cubit_list('hex','all') for h in list_hex: faces=cubit.get_sub_elements('hex',h,2) for f in faces: if dic_quads_all.has_key(f): nodes=cubit.get_connectivity('Face',f) txt='%10i %10i %10i %10i %10i\n' % (h,nodes[0],\ nodes[1],nodes[2],nodes[3]) surfhex_local.write(txt) # closes file surfhex_local.close() print 'Ok'
def surface_write(self, pathdir=None): # optional surfaces, e.g. moho_surface # should be created like e.g.: # > block 10 face in surface 2 # > block 10 name 'moho_surface' import re from sets import Set for block in self.block_bc: if block != self.topography: name = cubit.get_exodus_entity_name("block", block) # skips block names like face_abs**, face_topo** if re.search("abs", name): continue elif re.search("topo", name): continue elif re.search("surface", name): filename = pathdir + name + "_file" else: continue # gets face elements print " surface block name: ", name, "id: ", block quads_all = cubit.get_block_faces(block) print " face = ", len(quads_all) if len(quads_all) == 0: continue # writes out surface infos to file print "Writing " + filename + "....." surfhex_local = open(filename, "w") dic_quads_all = dict(zip(quads_all, quads_all)) # writes number of surface elements surfhex_local.write("%10i\n" % len(quads_all)) # writes out element node ids list_hex = cubit.parse_cubit_list("hex", "all") for h in list_hex: faces = cubit.get_sub_elements("hex", h, 2) for f in faces: if dic_quads_all.has_key(f): nodes = cubit.get_connectivity("Face", f) txt = "%10i %10i %10i %10i %10i\n" % (h, nodes[0], nodes[1], nodes[2], nodes[3]) surfhex_local.write(txt) # closes file surfhex_local.close() print "Ok"
def elem_write(self,mesh_name): meshfile=open(mesh_name,'w') print('Writing '+mesh_name+'.....') nelem=cubit.get_tri_count() print('number of elements:',str(nelem)) meshfile.write(str(nelem)+'\n') num_write=0 temp_tri=[] trilength=1 for block,flag in zip(self.block_mat,self.block_flag): trilength += len(cubit.get_block_tris(block)) tri_vp=range(trilength) tri_vs=range(trilength) tri_rho=range(trilength) tri_qp=range(trilength) tri_qs=range(trilength) tri_block=range(trilength) for block,flag in zip(self.block_mat,self.block_flag): tris=cubit.get_block_tris(block) name=cubit.get_exodus_entity_name('block',block) type=cubit.get_block_element_type(block) for tri in tris: nodes=cubit.get_connectivity('Tri',tri) temp_tri.append(tri) values=name.split(" ") tri_vp[tri]=float(values[2]) tri_vs[tri]=float(values[3]) tri_rho[tri]=float(values[4]) tri_qp[tri]=float(values[5]) tri_qs[tri]=float(values[6]) temp_tri.sort() for tri in temp_tri: nodes=cubit.get_connectivity('Tri',tri) txt=('%10i ')% tri txt=txt+('%10i %10i %10i\n')% nodes[:] # This is later needed for the inversion feature: #txt=txt+('%10i %10i %10i')% nodes[:] #txt=txt+(' %9.1f %9.1f %9.1f %5i %5i\n') % (tri_vp[tri],tri_vs[tri],tri_rho[tri],tri_qp[tri],tri_qs[tri]) meshfile.write(txt) meshfile.close() print('Ok')
def free_write(self, freename=None): # free surface cubit.cmd("set info off") cubit.cmd("set echo off") cubit.cmd("set journal off") from sets import Set normal = (0, 0, 1) if not freename: freename = self.freename # writes free surface file print "Writing " + freename + "....." freehex = open(freename, "w") # searches block definition with name face_topo for block, flag in zip(self.block_bc, self.block_bc_flag): if block == self.topography: name = cubit.get_exodus_entity_name("block", block) print " block name:", name, "id:", block quads_all = cubit.get_block_faces(block) print " number of faces = ", len(quads_all) dic_quads_all = dict(zip(quads_all, quads_all)) freehex.write("%10i\n" % len(quads_all)) list_hex = cubit.parse_cubit_list("hex", "all") for h in list_hex: faces = cubit.get_sub_elements("hex", h, 2) for f in faces: if dic_quads_all.has_key(f): # print f nodes = cubit.get_connectivity("Face", f) nodes_ok = self.normal_check(nodes, normal) txt = "%10i %10i %10i %10i %10i\n" % (h, nodes_ok[0], nodes_ok[1], nodes_ok[2], nodes_ok[3]) freehex.write(txt) freehex.close() print "Ok" cubit.cmd("set info on") cubit.cmd("set echo on")
def get_block(self): ''' extract block information ''' block_flag = [] block_mat = [] block_pml = [] block_pmlflag = [] ns_free = [] ns_absorb = [] material = [] # number of blocks blocks = cubit.get_block_id_list() # go through blocks for block in blocks: name = cubit.get_exodus_entity_name('block', block) type = cubit.get_block_element_type(block) print block, name, blocks, type if type == self.tri: # check if elastic if name.find('elastic') >= 0: print('found elastic block') imat = 1 # get number of attributes values = name.split(" ") print values flag = int(values[1]) vp = float(values[2]) vs = float(values[3]) rho = float(values[4]) qp = float(values[5]) qs = float(values[6]) block_flag.append(flag) block_mat.append(block) material.append([imat, vp, vs, rho, qp, qs]) elif name.find('pml') >= 0: ipml = 1 values = name.split(" ") flag = int(values[1]) block_pmlflag.append(flag) block_pml.append(block) else: print('error, no correct material in block', block) ns = cubit.get_nodeset_id_list() for n in ns: name = cubit.get_exodus_entity_name('nodeset', n) if name.find('free') >= 0: print('found free surface nodes') ns_free.append(n) elif name.find('absorb') >= 0: print('found absorb surface nodes') ns_absorb.append(n) else: print('error in boundaries', n) print('BLOCKFLAG', block_flag) self.block_flag = block_flag self.block_mat = block_mat self.block_pml = block_pml self.block_pmlflag = block_pmlflag self.mat = material self.ns_free = ns_free self.ns_absorb = ns_absorb
def abs_write(self, absname=None): # absorbing boundaries import re cubit.cmd('set info off') cubit.cmd('set echo off') cubit.cmd('set journal off') from sets import Set if not absname: absname = self.absname # # loops through all block definitions list_hex = cubit.parse_cubit_list('hex', 'all') for block, flag in zip(self.block_bc, self.block_bc_flag): if block != self.topography: name = cubit.get_exodus_entity_name('block', block) print name, block absflag = False if re.search('xmin', name): filename = absname + '_xmin' normal = (-1, 0, 0) elif re.search('xmax', name): filename = absname + '_xmax' normal = (1, 0, 0) elif re.search('ymin', name): filename = absname + '_ymin' normal = (0, -1, 0) elif re.search('ymax', name): filename = absname + '_ymax' normal = (0, 1, 0) elif re.search('bottom', name): filename = absname + '_bottom' normal = (0, 0, -1) elif re.search('abs', name): print " ...face_abs - not used so far..." continue else: continue # opens file print 'Writing ' + filename + '.....' abshex_local = open(filename, 'w') # gets face elements quads_all = cubit.get_block_faces(block) dic_quads_all = dict(zip(quads_all, quads_all)) print ' number of faces = ', len(quads_all) abshex_local.write('%10i\n' % len(quads_all)) #command = "group 'list_hex' add hex in face "+str(quads_all) #command = command.replace("["," ").replace("]"," ").replace("("," ").replace(")"," ") #cubit.cmd(command) #group=cubit.get_id_from_name("list_hex") #list_hex=cubit.get_group_hexes(group) #command = "delete group "+ str(group) #cubit.cmd(command) for h in list_hex: faces = cubit.get_sub_elements('hex', h, 2) for f in faces: if dic_quads_all.has_key(f): nodes = cubit.get_connectivity('Face', f) if not absflag: # checks with specified normal nodes_ok = self.normal_check(nodes, normal) txt='%10i %10i %10i %10i %10i\n' % (h,nodes_ok[0],\ nodes_ok[1],nodes_ok[2],nodes_ok[3]) else: txt='%10i %10i %10i %10i %10i\n' % (h,nodes[0],\ nodes[1],nodes[2],nodes[3]) abshex_local.write(txt) # closes file abshex_local.close() print 'Ok' cubit.cmd('set info on') cubit.cmd('set echo on')
def getstats(self): ''' Get statistics for differend blocks in the hexmesh ''' ############################## READ BLOCKS ###################### try: blocks = cubit.get_block_id_list() except: print('no blocks defined') nblocks = len(blocks) print('Found ', nblocks, ' Blocks') # lists of details of all elastic blocks. For each elastic block, append respective values name = [] typ = [] nattrib = [] vp = [] vs = [] rho = [] minEdge = [] maxEdge = [] minEdgeMesh = [] maxEdgeMesh = [] # k: counter of all elastic blocks k = 0 for block in blocks: minEdgeMeshTmp = [] maxEdgeMeshTmp = [] blockname = cubit.get_exodus_entity_name('block', block) # only apply stats to elastic blocks if blockname.find("elastic") >= 0: # NOW APPEND VALUES OF THIS ELASTIC BLOCK TO LISTS (defined as empty above) name.append(blockname) typ.append(cubit.get_block_element_type(block)) # attribute count nattrib.append(cubit.get_block_attribute_count(block)) # vp vp.append(cubit.get_block_attribute_value(block, 1)) # vs vs.append(cubit.get_block_attribute_value(block, 2)) # rho rho.append(cubit.get_block_attribute_value(block, 3)) hexes = cubit.get_block_hexes(block) print(block, k, 'name: ', name[k], '#hexes:', len(hexes), 'type: ', typ[k], 'attribute count: ', nattrib[k], 'vp: ', vp[k], 'vs: ', vs[k], 'rho: ', rho[k]) # minimum/maximum edgelength search for this elastic block # initiate with very large minEdge and very small maxEdge minEdge.append(1e9) maxEdge.append(1e-9) # now search for hexa in hexes: nodes = cubit.get_connectivity('Hex', hexa) # get coordinates of nodes of this hexahedron node_coords = [] for node in nodes: node_coords.append(cubit.get_nodal_coordinates(node)) minv = self.minEdgeHex(node_coords) minEdgeMeshTmp.append(minv) maxv = self.maxEdgeHex(node_coords) maxEdgeMeshTmp.append(maxv) # is minimum edgelength of this this element smaller than minimum edgelength found before? if minv < minEdge[k]: minEdge[k] = minv # is maximum edgelength of this this element larger than maximum edgelength found before? if maxv > maxEdge[k]: maxEdge[k] = maxv minEdgeMesh.append(minEdgeMeshTmp) maxEdgeMesh.append(maxEdgeMeshTmp) # end of processing of this elastic block. incremet counter k = k + 1 # if not "elastic" in this blockname else: print(blockname, '--no elastic') # now calculate maximum frequency for which the mesh is stable # and maximal dt # first do that for each block fmax = [] dtmax_05 = [] for i in range(len(name)): fmax.append(self.calcMaxFreq(maxEdge[i], vs[i])) dtmax_05.append(self.calcMaxDt(minEdge[i], vp[i], cn=0.5)) min_dx = minEdge[i] * 0.1727 max_dx = maxEdge[i] * 0.3272 * sqrt(3.) print('Block ', name[i], ' min dx: ', min_dx, ' max dx: ', max_dx, 'max frequency: ', fmax[i], ' maximal dt (C=0.5):', dtmax_05[i]) vals, bin_edges = histogram(minEdgeMesh[i], 10) nhexa = sum(vals) print(' ' + str(nhexa) + ' HEXAS IN THIS BLOCK') print(' histogram of minimum edge length of hexa of this block:') for j, val in enumerate(vals): print( ' %6.3f %% of hexas have minimum edgelength between %3.3f and %3.3f -> dt (C=0.5) is %8.3e' % (100. * val / float(nhexa), bin_edges[j], bin_edges[j + 1], self.calcMaxDt(bin_edges[j], vp[i], cn=0.5))) vals, bin_edges = histogram(maxEdgeMesh[i], 10) nhexa = sum(vals) print(' ' + str(nhexa) + ' HEXAS IN THIS BLOCK') print(' histogram of maximum edge length of hexa of this block:') for j, val in enumerate(vals): print( ' %6.3f %% of hexas have maximum edgelength between %3.3f and %3.3f -> fmax (5 GLL points per wavelength) is %8.3e' % (100. * val / float(nhexa), bin_edges[j], bin_edges[j + 1], self.calcMaxFreq(bin_edges[j + 1], vs[i]))) # now compare the values for all the blocks dtm_05 = sorted(dtmax_05) print( 'The minimum over all blocks of the respective maximal dt for C=0.5 is: ', dtm_05[0]) fmax_min = sorted(fmax) print( 'The minimum over all blocks of the respective maximal frequency is: ', fmax_min[0])
def abs_write(self,absname=None): # absorbing boundaries import re cubit.cmd('set info off') cubit.cmd('set echo off') cubit.cmd('set journal off') from sets import Set if not absname: absname=self.absname if self.cpml: if not absname: absname=self.cpmlname print 'Writing cpml'+absname+'.....' list_cpml=self.select_cpml() if list_cpml is False: print 'error writing cpml files' return else: abshex_cpml=open(absname,'w') hexcount=sum(map(len,list_cpml)) abshex_cpml.write(('%10i\n') % (hexcount)) for icpml,lcpml in enumerate(list_cpml): for hexa in lcpml: abshex_cpml.write(('%10i %10i\n') % (hexa,icpml)) stacey_absorb=True if stacey_absorb: # # if not absname: absname=self.absname # loops through all block definitions list_hex=cubit.parse_cubit_list('hex','all') for block,flag in zip(self.block_bc,self.block_bc_flag): if block != self.topography: name=cubit.get_exodus_entity_name('block',block) print ' block name:',name,'id:',block cknormal=True abshex_local=False # opens file if re.search('xmin',name): print 'xmin' abshex_local=open(absname+'_xmin','w') normal=(-1,0,0) elif re.search('xmax',name): print "xmax" abshex_local=open(absname+'_xmax','w') normal=(1,0,0) elif re.search('ymin',name): print "ymin" abshex_local=open(absname+'_ymin','w') normal=(0,-1,0) elif re.search('ymax',name): print "ymax" abshex_local=open(absname+'_ymax','w') normal=(0,1,0) elif re.search('bottom',name): print "bottom" abshex_local=open(absname+'_bottom','w') normal=(0,0,-1) elif re.search('abs',name): print "abs all - experimental, check the output" cknormal=False abshex_local=open(absname,'w') else: if block == 1003: print 'xmin' abshex_local=open(absname+'_xmin','w') normal=(-1,0,0) elif block == 1004: print "ymin" abshex_local=open(absname+'_ymin','w') normal=(0,-1,0) elif block == 1005: print "xmax" abshex_local=open(absname+'_xmax','w') normal=(1,0,0) elif block == 1006: print "ymax" abshex_local=open(absname+'_ymax','w') normal=(0,1,0) elif block == 1002: print "bottom" abshex_local=open(absname+'_bottom','w') normal=(0,0,-1) elif block == 1000: print "custumized" abshex_local=open(absname,'w') cknormal=False normal=None # # if abshex_local: # gets face elements quads_all=cubit.get_block_faces(block) dic_quads_all=dict(zip(quads_all,quads_all)) print ' number of faces = ',len(quads_all) abshex_local.write('%10i\n' % len(quads_all)) #command = "group 'list_hex' add hex in face "+str(quads_all) #command = command.replace("["," ").replace("]"," ").replace("("," ").replace(")"," ") #cubit.cmd(command) #group=cubit.get_id_from_name("list_hex") #list_hex=cubit.get_group_hexes(group) #command = "delete group "+ str(group) #cubit.cmd(command) for h in list_hex: faces=cubit.get_sub_elements('hex',h,2) for f in faces: if dic_quads_all.has_key(f): txt=self.create_facenode_string(h,f,normal=normal,cknormal=cknormal) abshex_local.write(txt) abshex_local.close() print 'Ok' cubit.cmd('set info on') cubit.cmd('set echo on')
def block_definition(self): block_flag=[] block_mat=[] block_bc=[] block_bc_flag=[] material={} bc={} blocks=cubit.get_block_id_list() for block in blocks: name=cubit.get_exodus_entity_name('block',block) ty=cubit.get_block_element_type(block) #print block,blocks,ty,self.hex,self.face if self.hex in ty: flag=None vel=None vs=None rho=None q=0 ani=0 # material domain id if "acoustic" in name : imaterial = 1 elif "elastic" in name: imaterial = 2 elif "poroelastic" in name: imaterial = 3 else : imaterial = 0 # nattrib=cubit.get_block_attribute_count(block) if nattrib > 1: # material flag: # positive => material properties, # negative => interface/tomography domain flag=int(cubit.get_block_attribute_value(block,0)) if flag > 0 and nattrib >= 2: # material properties # vp vel=cubit.get_block_attribute_value(block,1) if nattrib >= 3: # vs vs=cubit.get_block_attribute_value(block,2) if nattrib >= 4: # density rho=cubit.get_block_attribute_value(block,3) if nattrib >= 5: # next: Q_kappa or Q_mu (new/old format style) q=cubit.get_block_attribute_value(block,4) if nattrib == 6: # only 6 parameters given (skipping Q_kappa ), old format style qmu = q #Q_kappa is 10 times stronger than Q_mu qk = q * 10 # last entry is anisotropic flag ani=cubit.get_block_attribute_value(block,5) elif nattrib > 6: #Q_kappa qk=q #Q_mu qmu=cubit.get_block_attribute_value(block,5) if nattrib == 7: #anisotropy_flag ani=cubit.get_block_attribute_value(block,6) # for q to be valid: it must be positive if qk < 0 or qmu < 0: print 'error, Q value invalid:',qk,qmu break elif flag < 0: # interface/tomography domain # velocity model vel=name attrib=cubit.get_block_attribute_value(block,1) if attrib == 1: kind='interface' flag_down=cubit.get_block_attribute_value(block,2) flag_up=cubit.get_block_attribute_value(block,3) elif attrib == 2: kind='tomography' elif nattrib == 1: flag=cubit.get_block_attribute_value(block,0) #print 'only 1 attribute ', name,block,flag vel,vs,rho,qk,qmu,ani=(0,0,0,9999.,9999.,0) else: flag=block vel,vs,rho,qk,qmu,ani=(name,0,0,9999.,9999.,0) block_flag.append(int(flag)) block_mat.append(block) if (flag > 0) and nattrib != 1: par=tuple([imaterial,flag,vel,vs,rho,qk,qmu,ani]) elif flag < 0 and nattrib != 1: if kind=='interface': par=tuple([imaterial,flag,kind,name,flag_down,flag_up]) elif kind=='tomography': par=tuple([imaterial,flag,kind,name]) elif flag==0 or nattrib == 1: par=tuple([imaterial,flag,name]) material[block]=par elif ty == self.face: block_bc_flag.append(4) block_bc.append(block) bc[block]=4 #face has connectivity = 4 if name == self.topo or block == 1001: self.topography=block if self.freetxt in name: self.free=block elif ty == 'SPHERE': pass else: # block elements differ from HEX8/QUAD4/SHELL4 print '****************************************' print 'block not properly defined:' print ' name:',name print ' type:',type print print 'please check your block definitions!' print print 'only supported types are:' print ' HEX/HEX8 for volumes' print ' QUAD4 for surface' print ' SHELL4 for surface' print '****************************************' continue return None, None,None,None,None,None,None,None nsets=cubit.get_nodeset_id_list() if len(nsets) == 0: self.receivers=None for nset in nsets: name=cubit.get_exodus_entity_name('nodeset',nset) if name == self.rec: self.receivers=nset else: print 'nodeset '+name+' not defined' self.receivers=None try: self.block_mat=block_mat self.block_flag=block_flag self.block_bc=block_bc self.block_bc_flag=block_bc_flag self.material=material self.bc=bc print 'HEX Blocks:' for m,f in zip(self.block_mat,self.block_flag): print 'block ',m,'material flag ',f print 'Absorbing Boundary Conditions:' for m,f in zip(self.block_bc,self.block_bc_flag): print 'bc ',m,'bc flag ',f print 'Topography (free surface)' print self.topography print 'Free surface' print self.free except: print '****************************************' print 'sorry, no blocks or blocks not properly defined' print block_mat print block_flag print block_bc print block_bc_flag print material print bc print '****************************************'
def block_definition(self): block_flag=[] block_mat=[] block_bc=[] block_bc_flag=[] material={} bc={} blocks=cubit.get_block_id_list() for block in blocks: name=cubit.get_exodus_entity_name('block',block) type=cubit.get_block_element_type(block) print block,name,blocks,type,self.hex,self.face # block has hexahedral elements (HEX8) if type == self.hex: flag=None vel=None vs=None rho=None q=0 ani=0 # material domain id if name.find("acoustic") >= 0 : imaterial = 1 elif name.find("elastic") >= 0 : imaterial = 2 elif name.find("poroelastic") >= 0 : imaterial = 3 else : imaterial = 0 print "block: ",name print " could not find appropriate material for this block..." print "" break nattrib=cubit.get_block_attribute_count(block) if nattrib != 0: # material flag: # positive => material properties, # negative => interface/tomography domain flag=int(cubit.get_block_attribute_value(block,0)) if flag > 0 and nattrib >= 2: # vp vel=cubit.get_block_attribute_value(block,1) if nattrib >= 3: # vs vs=cubit.get_block_attribute_value(block,2) if nattrib >= 4: #density rho=cubit.get_block_attribute_value(block,3) if nattrib >= 5: #Q_mu q=cubit.get_block_attribute_value(block,4) # for q to be valid: it must be positive if q < 0 : print 'error, q value invalid:', q break if nattrib == 6: #anisotropy_flag ani=cubit.get_block_attribute_value(block,5) elif flag < 0: # velocity model vel=name attrib=cubit.get_block_attribute_value(block,1) if attrib == 1: kind='interface' flag_down=cubit.get_block_attribute_value(block,2) flag_up=cubit.get_block_attribute_value(block,3) elif attrib == 2: kind='tomography' else: flag=block vel,vs,rho,q,ani=(name,0,0,0,0) block_flag.append(int(flag)) block_mat.append(block) if flag > 0: par=tuple([imaterial,flag,vel,vs,rho,q,ani]) elif flag < 0: if kind=='interface': par=tuple([imaterial,flag,kind,name,flag_down,flag_up]) elif kind=='tomography': par=tuple([imaterial,flag,kind,name]) elif flag==0: par=tuple([imaterial,flag,name]) material[block]=par elif (type == self.face) or (type == self.face2) : # block has surface elements (QUAD4 or SHELL4) block_bc_flag.append(4) block_bc.append(block) bc[block]=4 #face has connectivity = 4 if name == self.topo: topography_face=block else: # block elements differ from HEX8/QUAD4/SHELL4 print '****************************************' print 'block not properly defined:' print ' name:',name print ' type:',type print print 'please check your block definitions!' print print 'only supported types are:' print ' HEX8 for volumes' print ' QUAD4 for surface' print ' SHELL4 for surface' print '****************************************' continue nsets=cubit.get_nodeset_id_list() if len(nsets) == 0: self.receivers=None for nset in nsets: name=cubit.get_exodus_entity_name('nodeset',nset) if name == self.rec: self.receivers=nset else: print 'nodeset '+name+' not defined' self.receivers=None try: self.block_mat=block_mat self.block_flag=block_flag self.block_bc=block_bc self.block_bc_flag=block_bc_flag self.material=material self.bc=bc self.topography=topography_face except: print '****************************************' print 'sorry, no blocks or blocks not properly defined' print block_mat print block_flag print block_bc print block_bc_flag print material print bc print topography print '****************************************'
def abs_write(self,absname=None): import re cubit.cmd('set info off') cubit.cmd('set echo off') cubit.cmd('set journal off') from sets import Set if not absname: absname=self.absname # # list_hex=cubit.parse_cubit_list('hex','all') for block,flag in zip(self.block_bc,self.block_bc_flag): if block != self.topography: name=cubit.get_exodus_entity_name('block',block) print ' block name:',name,'id:',block cknormal=True if re.search('xmin',name): print 'xmin' abshex_local=open(absname+'_xmin','w') normal=(-1,0,0) elif re.search('xmax',name): print "xmax" abshex_local=open(absname+'_xmax','w') normal=(1,0,0) elif re.search('ymin',name): print "ymin" abshex_local=open(absname+'_ymin','w') normal=(0,-1,0) elif re.search('ymax',name): print "ymax" abshex_local=open(absname+'_ymax','w') normal=(0,1,0) elif re.search('bottom',name): print "bottom" abshex_local=open(absname+'_bottom','w') normal=(0,0,-1) elif re.search('abs',name): print "abs all - no implemented yet" cknormal=False abshex_local=open(absname,'w') else: if block == 1003: print 'xmin' abshex_local=open(absname+'_xmin','w') normal=(-1,0,0) elif block == 1004: print "ymin" abshex_local=open(absname+'_ymin','w') normal=(0,-1,0) elif block == 1005: print "xmax" abshex_local=open(absname+'_xmax','w') normal=(1,0,0) elif block == 1006: print "ymax" abshex_local=open(absname+'_ymax','w') normal=(0,1,0) elif block == 1002: print "bottom" abshex_local=open(absname+'_bottom','w') normal=(0,0,-1) # # quads_all=cubit.get_block_faces(block) dic_quads_all=dict(zip(quads_all,quads_all)) print ' number of faces = ',len(quads_all) abshex_local.write('%10i\n' % len(quads_all)) #command = "group 'list_hex' add hex in face "+str(quads_all) #command = command.replace("["," ").replace("]"," ").replace("("," ").replace(")"," ") #cubit.cmd(command) #group=cubit.get_id_from_name("list_hex") #list_hex=cubit.get_group_hexes(group) #command = "delete group "+ str(group) #cubit.cmd(command) for h in list_hex: faces=cubit.get_sub_elements('hex',h,2) for f in faces: if dic_quads_all.has_key(f): txt=self.create_facenode_string(h,f,normal=normal,cknormal=True) abshex_local.write(txt) abshex_local.close() cubit.cmd('set info on') cubit.cmd('set echo on')
def get_block(self): ''' extract block information ''' block_flag=[] block_mat=[] block_pml=[] block_pmlflag=[] ns_free=[] ns_absorb=[] material=[] # number of blocks blocks=cubit.get_block_id_list() # go through blocks for block in blocks: name=cubit.get_exodus_entity_name('block',block) type=cubit.get_block_element_type(block) print block,name,blocks,type if type == self.tri: # check if elastic if name.find('elastic') >=0: print('found elastic block') imat=1 # get number of attributes values=name.split(" ") print values flag=int(values[1]) vp=float(values[2]) vs=float(values[3]) rho=float(values[4]) qp=float(values[5]) qs=float(values[6]) pml=float(values[7]) block_flag.append(flag) block_pmlflag.append(pml) block_mat.append(block) material.append([imat,vp,vs,rho,qp,qs]) elif name.find('poro') >=0: print('found poroelastic block') self.poro=True imat=2 # get number of attributes values=name.split(" ") print values flag=int(values[1]) typeid=float(values[2]) rhos=float(values[3]) lambdau=float(values[4]) my=float(values[5]) phi=float(values[6]) kappa=float(values[7]) b=float(values[8]) invT=float(values[9]) invN=float(values[10]) rho1=float(values[11]) S1=float(values[12]) K1=float(values[13]) ny1=float(values[14]) rho2=float(values[15]) S2=float(values[16]) K2=float(values[17]) ny2=float(values[18]) fitting_n=float(values[19]) fitting_chi=float(values[20]) Sr1=float(values[21]) Sr2=float(values[22]) block_flag.append(flag) block_pmlflag.append(0) block_mat.append(block) material.append([imat,typeid,rhos,lambdau,my,phi,kappa,b,invT,invN,rho1,S1,K1,ny1,rho2,S2,K2,ny2,fitting_n,fitting_chi,Sr1,Sr2]) elif name.find('pml') >=0: ipml=1 values=name.split(" ") flag=int(values[1]) block_pmlflag.append(flag) block_pml.append(block) else: print('error, no correct material in block',block) ns=cubit.get_nodeset_id_list() for n in ns: name=cubit.get_exodus_entity_name('nodeset',n) if name.find('free') >=0: print('found free surface nodes') ns_free.append(n) elif name.find('absorb') >=0: print('found absorb surface nodes') ns_absorb.append(n) elif name.find('vtk') >=0: pass else: print('error in boundaries',n) print('BLOCKFLAG',block_flag) self.block_flag=block_flag self.block_mat=block_mat self.block_pml=block_pml self.block_pmlflag=block_pmlflag self.mat=material self.ns_free=ns_free self.ns_absorb=ns_absorb
def abs_write(self,absname=None): # absorbing boundaries import re cubit.cmd('set info off') cubit.cmd('set echo off') cubit.cmd('set journal off') from sets import Set if not absname: absname=self.absname # # loops through all block definitions list_hex=cubit.parse_cubit_list('hex','all') for block,flag in zip(self.block_bc,self.block_bc_flag): if block != self.topography: name=cubit.get_exodus_entity_name('block',block) print name,block absflag=False if re.search('xmin',name): filename=absname+'_xmin' normal=(-1,0,0) elif re.search('xmax',name): filename=absname+'_xmax' normal=(1,0,0) elif re.search('ymin',name): filename=absname+'_ymin' normal=(0,-1,0) elif re.search('ymax',name): filename=absname+'_ymax' normal=(0,1,0) elif re.search('bottom',name): filename=absname+'_bottom' normal=(0,0,-1) elif re.search('abs',name): print " ...face_abs - not used so far..." continue else: continue # opens file print 'Writing '+filename+'.....' abshex_local=open(filename,'w') # gets face elements quads_all=cubit.get_block_faces(block) dic_quads_all=dict(zip(quads_all,quads_all)) print ' number of faces = ',len(quads_all) abshex_local.write('%10i\n' % len(quads_all)) #command = "group 'list_hex' add hex in face "+str(quads_all) #command = command.replace("["," ").replace("]"," ").replace("("," ").replace(")"," ") #cubit.cmd(command) #group=cubit.get_id_from_name("list_hex") #list_hex=cubit.get_group_hexes(group) #command = "delete group "+ str(group) #cubit.cmd(command) for h in list_hex: faces=cubit.get_sub_elements('hex',h,2) for f in faces: if dic_quads_all.has_key(f): nodes=cubit.get_connectivity('Face',f) if not absflag: # checks with specified normal nodes_ok=self.normal_check(nodes,normal) txt='%10i %10i %10i %10i %10i\n' % (h,nodes_ok[0],\ nodes_ok[1],nodes_ok[2],nodes_ok[3]) else: txt='%10i %10i %10i %10i %10i\n' % (h,nodes[0],\ nodes[1],nodes[2],nodes[3]) abshex_local.write(txt) # closes file abshex_local.close() print 'Ok' cubit.cmd('set info on') cubit.cmd('set echo on')
def block_definition(self): block_flag = [] block_mat = [] block_bc = [] block_bc_flag = [] material = {} bc = {} blocks = cubit.get_block_id_list() for block in blocks: name = cubit.get_exodus_entity_name("block", block) type = cubit.get_block_element_type(block) print block, name, blocks, type, self.hex, self.face # block has hexahedral elements (HEX8) if type == self.hex: flag = None vel = None vs = None rho = None q = 0 ani = 0 # material domain id if name.find("acoustic") >= 0: imaterial = 1 elif name.find("elastic") >= 0: imaterial = 2 elif name.find("poroelastic") >= 0: imaterial = 3 else: imaterial = 0 print "block: ", name print " could not find appropriate material for this block..." print "" break nattrib = cubit.get_block_attribute_count(block) if nattrib != 0: # material flag: # positive => material properties, # negative => interface/tomography domain flag = int(cubit.get_block_attribute_value(block, 0)) if flag > 0 and nattrib >= 2: # vp vel = cubit.get_block_attribute_value(block, 1) if nattrib >= 3: # vs vs = cubit.get_block_attribute_value(block, 2) if nattrib >= 4: # density rho = cubit.get_block_attribute_value(block, 3) if nattrib >= 5: # Q_mu q = cubit.get_block_attribute_value(block, 4) # for q to be valid: it must be positive if q < 0: print "error, q value invalid:", q break if nattrib == 6: # only 6 parameters given (skipping Q_kappa ), old format style # Q_kappa is 10 times stronger than Q_mu q2 = q * 10 # last entry is anisotropic flag ani = cubit.get_block_attribute_value(block, 5) elif nattrib > 6: # Q_kappa q2 = cubit.get_block_attribute_value(block, 5) # for q to be valid: it must be positive if q2 < 0: print "error, q value invalid:", q2 break if nattrib == 7: # anisotropy_flag ani = cubit.get_block_attribute_value(block, 6) elif flag < 0: # velocity model vel = name attrib = cubit.get_block_attribute_value(block, 1) if attrib == 1: kind = "interface" flag_down = cubit.get_block_attribute_value(block, 2) flag_up = cubit.get_block_attribute_value(block, 3) elif attrib == 2: kind = "tomography" else: flag = block vel, vs, rho, q, q2, ani = (name, 0, 0, 0, 0, 0) block_flag.append(int(flag)) block_mat.append(block) if flag > 0: par = tuple([imaterial, flag, vel, vs, rho, q, q2, ani]) elif flag < 0: if kind == "interface": par = tuple([imaterial, flag, kind, name, flag_down, flag_up]) elif kind == "tomography": par = tuple([imaterial, flag, kind, name]) elif flag == 0: par = tuple([imaterial, flag, name]) material[block] = par elif (type == self.face) or (type == self.face2): # block has surface elements (QUAD4 or SHELL4) block_bc_flag.append(4) block_bc.append(block) bc[block] = 4 # face has connectivity = 4 if name == self.topo: topography_face = block else: # block elements differ from HEX8/QUAD4/SHELL4 print "****************************************" print "block not properly defined:" print " name:", name print " type:", type print print "please check your block definitions!" print print "only supported types are:" print " HEX8 for volumes" print " QUAD4 for surface" print " SHELL4 for surface" print "****************************************" continue nsets = cubit.get_nodeset_id_list() if len(nsets) == 0: self.receivers = None for nset in nsets: name = cubit.get_exodus_entity_name("nodeset", nset) if name == self.rec: self.receivers = nset else: print "nodeset " + name + " not defined" self.receivers = None try: self.block_mat = block_mat self.block_flag = block_flag self.block_bc = block_bc self.block_bc_flag = block_bc_flag self.material = material self.bc = bc self.topography = topography_face except: print "****************************************" print "sorry, no blocks or blocks not properly defined" print block_mat print block_flag print block_bc print block_bc_flag print material print bc print topography print "****************************************"
def savemesh(mpiflag, iproc=0, filename=None): import start as start cfg = start.start_cfg(filename=filename) mpiflag, iproc, numproc, mpi = start.start_mpi() def runsave(meshfile, iproc, filename=None): import start as start cubit = start.start_cubit() cfg = start.start_cfg(filename=filename) flag = 0 ner = cubit.get_error_count() cubitcommand = 'save as "' + cfg.output_dir + '/' + meshfile + '.cub' + '" overwrite' cubit.cmd(cubitcommand) ner2 = cubit.get_error_count() if ner == ner2: cubitcommand = 'export mesh "' + cfg.output_dir + '/' + meshfile + '.e' + '" dimension 3 block all overwrite' cubit.cmd(cubitcommand) ner2 = cubit.get_error_count() if ner == ner2: flag = 1 return flag meshfile = 'mesh_vol_' + str(iproc) flagsaved = 0 infosave = (iproc, flagsaved) mpi.barrier() total_saved = mpi.allgather(flagsaved) if isinstance(total_saved, int): total_saved = [total_saved] ind = 0 saving = True while saving: if len(total_saved) != sum(total_saved): # if not flagsaved: flagsaved = runsave(meshfile, iproc, filename=filename) if flagsaved: infosave = (iproc, flagsaved) if numproc > 1: f = open('mesh_saved' + str(iproc), 'w') f.close() mpi.barrier() total_saved = mpi.allgather(flagsaved) if isinstance(total_saved, int): total_saved = [total_saved] ind = ind + 1 else: saving = False if ind > len(total_saved) + 10: saving = False print sum(total_saved), '/', len(total_saved), ' saved' info_total_saved = mpi.allgather(infosave) if isinstance(info_total_saved, int): info_total_saved = [info_total_saved] if iproc == 0: f = open('mesh_saving.log', 'w') f.write('\n'.join(str(x) for x in info_total_saved)) f.close() f = open(cfg.output_dir + '/' + 'blocks_' + str(iproc).zfill(5), 'w') blocks = cubit.get_block_id_list() for block in blocks: name = cubit.get_exodus_entity_name('block', block) element_count = cubit.get_exodus_element_count(block, "block") nattrib = cubit.get_block_attribute_count(block) attr = [ cubit.get_block_attribute_value(block, x) for x in range(0, nattrib) ] ty = cubit.get_block_element_type(block) f.write( str(block) + ' ; ' + name + ' ; nattr ' + str(nattrib) + ' ; ' + ' '.join(str(x) for x in attr) + ' ; ' + ty + ' ' + str(element_count) + '\n') f.close() import quality_log f = open(cfg.output_dir + '/' + 'quality_' + str(iproc).zfill(5), 'w') max_skewness, min_length = quality_log.quality_log(f) f.close() count_hex = [cubit.get_hex_count()] count_node = [cubit.get_node_count()] max_skew = [(iproc, max_skewness)] min_l = [(iproc, min_length)] mpi.barrier() total_min_l = mpi.gather(min_l) total_hex = mpi.gather(count_hex) total_node = mpi.gather(count_node) total_max_skew = mpi.gather(max_skew) mpi.barrier() if iproc == 0: min_total_min_l = min([ms[1] for ms in total_min_l]) max_total_max_skew = max([ms[1] for ms in total_max_skew]) sum_total_node = sum(total_node) sum_total_hex = sum(total_hex) totstat_file = open(cfg.output_dir + '/totstat.log', 'w') text = 'hex total number,node total number,max skew, min length\n' totstat_file.write(text) text = str(sum_total_hex) + ' , ' + str(sum_total_node) + ' , ' + str( max_total_max_skew) + ' , ' + str(min_total_min_l) + '\n' totstat_file.write(text) totstat_file.write(str(total_max_skew)) totstat_file.close() print 'meshing process end... proc ', iproc
def abs_write(self, absname=None): # absorbing boundaries import re cubit.cmd("set info off") cubit.cmd("set echo off") cubit.cmd("set journal off") from sets import Set if not absname: absname = self.absname # # loops through all block definitions list_hex = cubit.parse_cubit_list("hex", "all") for block, flag in zip(self.block_bc, self.block_bc_flag): if block != self.topography: name = cubit.get_exodus_entity_name("block", block) print name, block absflag = False if re.search("xmin", name): filename = absname + "_xmin" normal = (-1, 0, 0) elif re.search("xmax", name): filename = absname + "_xmax" normal = (1, 0, 0) elif re.search("ymin", name): filename = absname + "_ymin" normal = (0, -1, 0) elif re.search("ymax", name): filename = absname + "_ymax" normal = (0, 1, 0) elif re.search("bottom", name): filename = absname + "_bottom" normal = (0, 0, -1) elif re.search("abs", name): # print " ...face_abs - not used so far..." filename = absname + "_all" absflag = True else: continue # opens file print "Writing " + filename + "....." abshex_local = open(filename, "w") # gets face elements quads_all = cubit.get_block_faces(block) dic_quads_all = dict(zip(quads_all, quads_all)) print " number of faces = ", len(quads_all) abshex_local.write("%10i\n" % len(quads_all)) # command = "group 'list_hex' add hex in face "+str(quads_all) # command = command.replace("["," ").replace("]"," ").replace("("," ").replace(")"," ") # cubit.cmd(command) # group=cubit.get_id_from_name("list_hex") # list_hex=cubit.get_group_hexes(group) # command = "delete group "+ str(group) # cubit.cmd(command) for h in list_hex: faces = cubit.get_sub_elements("hex", h, 2) for f in faces: if dic_quads_all.has_key(f): nodes = cubit.get_connectivity("Face", f) if not absflag: # checks with specified normal nodes_ok = self.normal_check(nodes, normal) txt = "%10i %10i %10i %10i %10i\n" % ( h, nodes_ok[0], nodes_ok[1], nodes_ok[2], nodes_ok[3], ) else: txt = "%10i %10i %10i %10i %10i\n" % (h, nodes[0], nodes[1], nodes[2], nodes[3]) abshex_local.write(txt) # closes file abshex_local.close() print "Ok" cubit.cmd("set info on") cubit.cmd("set echo on")
def getstats(self): ''' Get statistics for differend blocks in the hexmesh ''' ############################## READ BLOCKS ###################### try: blocks=cubit.get_block_id_list() except: print('no blocks defined') nblocks=len(blocks) print('Found ',nblocks,' Blocks') # lists of details of all elastic blocks. For each elastic block, append respective values name=[] typ=[] nattrib=[] vp=[] vs=[] rho=[] minEdge=[] maxEdge=[] minEdgeMesh = [] maxEdgeMesh = [] # k: counter of all elastic blocks k=0 for block in blocks: minEdgeMeshTmp = [] maxEdgeMeshTmp = [] blockname=cubit.get_exodus_entity_name('block',block) # only apply stats to elastic blocks if blockname.find("elastic") >= 0: # NOW APPEND VALUES OF THIS ELASTIC BLOCK TO LISTS (defined as empty above) name.append(blockname) typ.append(cubit.get_block_element_type(block)) # attribute count nattrib.append(cubit.get_block_attribute_count(block)) # vp vp.append(cubit.get_block_attribute_value(block,1)) # vs vs.append(cubit.get_block_attribute_value(block,2)) # rho rho.append(cubit.get_block_attribute_value(block,3)) hexes=cubit.get_block_hexes(block) print(block, k,'name: ',name[k],'#hexes:',len(hexes),'type: ',typ[k],'attribute count: ',nattrib[k],'vp: ',vp[k],'vs: ',vs[k],'rho: ',rho[k]) # minimum/maximum edgelength search for this elastic block # initiate with very large minEdge and very small maxEdge minEdge.append(1e9) maxEdge.append(1e-9) # now search for hexa in hexes: nodes=cubit.get_connectivity('Hex',hexa) # get coordinates of nodes of this hexahedron node_coords=[] for node in nodes: node_coords.append(cubit.get_nodal_coordinates(node)) minv=self.minEdgeHex(node_coords) minEdgeMeshTmp.append(minv) maxv=self.maxEdgeHex(node_coords) maxEdgeMeshTmp.append(maxv) # is minimum edgelength of this this element smaller than minimum edgelength found before? if minv < minEdge[k]: minEdge[k]=minv # is maximum edgelength of this this element larger than maximum edgelength found before? if maxv > maxEdge[k]: maxEdge[k]=maxv minEdgeMesh.append(minEdgeMeshTmp) maxEdgeMesh.append(maxEdgeMeshTmp) # end of processing of this elastic block. incremet counter k=k+1 # if not "elastic" in this blockname else: print(blockname, '--no elastic') # now calculate maximum frequency for which the mesh is stable # and maximal dt # first do that for each block fmax=[] dtmax_05=[] for i in range(len(name)): fmax.append(self.calcMaxFreq(maxEdge[i],vs[i])) dtmax_05.append(self.calcMaxDt(minEdge[i],vp[i],cn=0.5)) min_dx = minEdge[i]*0.1727 max_dx = maxEdge[i]*0.3272*sqrt(3.) print('Block ', name[i], ' min dx: ',min_dx, ' max dx: ',max_dx, 'max frequency: ',fmax[i], ' maximal dt (C=0.5):',dtmax_05[i]) vals,bin_edges = histogram(minEdgeMesh[i],10) nhexa = sum(vals) print(' '+str(nhexa)+' HEXAS IN THIS BLOCK') print(' histogram of minimum edge length of hexa of this block:') for j,val in enumerate(vals): print(' %6.3f %% of hexas have minimum edgelength between %3.3f and %3.3f -> dt (C=0.5) is %8.3e'%(100.*val/float(nhexa),bin_edges[j],bin_edges[j+1],self.calcMaxDt(bin_edges[j],vp[i],cn=0.5))) vals,bin_edges = histogram(maxEdgeMesh[i],10) nhexa = sum(vals) print(' '+str(nhexa)+' HEXAS IN THIS BLOCK') print(' histogram of maximum edge length of hexa of this block:') for j,val in enumerate(vals): print(' %6.3f %% of hexas have maximum edgelength between %3.3f and %3.3f -> fmax (5 GLL points per wavelength) is %8.3e'%(100.*val/float(nhexa),bin_edges[j],bin_edges[j+1],self.calcMaxFreq(bin_edges[j+1],vs[i]))) # now compare the values for all the blocks dtm_05=sorted(dtmax_05) print('The minimum over all blocks of the respective maximal dt for C=0.5 is: ',dtm_05[0]) fmax_min=sorted(fmax) print('The minimum over all blocks of the respective maximal frequency is: ',fmax_min[0])
def block_definition(self): """ Import blocks features from Cubit """ block_flag = [] # Will contain material id (1 if fluid 2 if solid) block_mat = [] # Will contain face block ids block_bc = [] # Will contain edge block ids block_bc_flag = [] # Will contain edge id -> 2 abs_boun = [-1] * self.nabs # total 4 sides of absorbing boundaries (index 0 : bottom, index 1 : right, index 2 : top, index 3 : left) #pml_boun = [-1] * 6 # To store pml layers id (for each pml layer : x_acoust, z_acoust, xz_acoust, x_elast, z_elast, xz_elast) pml_boun = [[] for _ in range(6)] # To store the block id corresponding to pml layers id (arbitrary number of blocks for each pml layer : x_acoust, z_acoust, xz_acoust, x_elast, z_elast, xz_elast) material = {} # Will contain each material name and their properties bc = {} # Will contains each boundary name and their connectivity -> 2 blocks = cubit.get_block_id_list() # Load the blocks list for block in blocks: # Loop on the blocks name = cubit.get_exodus_entity_name('block',block) # Contains the name of the blocks ty = cubit.get_block_element_type(block) # Contains the block element type (QUAD4...) if ty == self.face: # If we are dealing with a block containing faces nAttributes = cubit.get_block_attribute_count(block) if (nAttributes != 1 and nAttributes != 6): print 'Blocks not properly defined, 2d blocks must have one attribute (material id) or 6 attributes' return None,None,None,None,None,None,None,None flag=int(cubit.get_block_attribute_value(block,0)) # Fetch the first attribute value (containing material id) print "nAttributes : ",nAttributes if nAttributes == 6: self.write_nummaterial_velocity_file = True velP = cubit.get_block_attribute_value(block,1) # Fetch the first attribute value (containing P wave velocity) velS = cubit.get_block_attribute_value(block,2) # Fetch the second attribute value (containing S wave velocity) rho = cubit.get_block_attribute_value(block,3) # Fetch the third attribute value (containing material density) qFlag = cubit.get_block_attribute_value(block,4) # Fetch the first attribute value (containing Qflag) anisotropy_flag = cubit.get_block_attribute_value(block,5) # Fetch the first attribute value (containing anisotropy_flag) # Store (material_id,rho,velP,velS,Qflag,anisotropy_flag) in par : par = tuple([flag,rho,velP,velS,qFlag,anisotropy_flag]) material[name] = par # associate the name of the block to its id and properties block_flag.append(int(flag)) # Append material id to block_flag block_mat.append(block) # Append block id to block_mat if name in self.pml_boun_name : # If the block considered refered to one of the pml layer self.abs_mesh=True self.pml_layers=True pml_boun[self.pml_boun_name.index(name)]=block # -> Put it at the correct position in pml_boun # (index 0 : pml_x_acoust, index 1 : pml_z_acoust, index 2 : pml_xz_acoust, # index 3 : pml_x_elast, index 4 : pml_z_elast, index 5 : pml_xz_elast) elif ty == self.edge: # If we are dealing with a block containing edges block_bc_flag.append(2) # Append "2" to block_bc_flag block_bc.append(block) # Append block id to block_bc bc[name] = 2 # Associate the name of the block with its connectivity : an edge has connectivity = 2 if name == self.topo: self.topo_mesh=True topography=block # If the block considered refered to topography store its id in "topography" if name == self.axisname: self.axisymmetric_mesh = True axisId = block # AXISYM If the block considered refered to the axis store its id in "axisId" if name in self.abs_boun_name : # If the block considered refered to one of the boundaries self.abs_mesh = True abs_boun[self.abs_boun_name.index(name)] = block # -> Put it at the correct position in abs_boun (index 0 : bottom, index 1 : right, index 2 : top, index 3 : left) else: print 'Blocks not properly defined', ty return None,None,None,None,None,None,None,None nsets = cubit.get_nodeset_id_list() # Get the list of all nodeset if len(nsets) == 0: self.receivers = None # If this list is empty : put None in self.receivers for nset in nsets: name = cubit.get_exodus_entity_name('nodeset',nset) # Contains the name of the nodeset if name == self.rec: # If the name considered match self.rec (receivers) self.receivers = nset # Store the id of the nodeset in self.receivers else: print 'nodeset '+name+' not defined' self.receivers = None # Store everything in the object : try: self.block_mat = block_mat self.block_flag = block_flag self.block_bc = block_bc self.block_bc_flag = block_bc_flag self.bc = bc if self.write_nummaterial_velocity_file: self.material = material if self.abs_mesh: self.abs_boun = abs_boun if self.topo_mesh: self.topography = topography if self.axisymmetric_mesh: self.axisId = axisId if self.pml_layers: self.pml_boun = pml_boun except: print 'Blocks not properly defined'
def abs_write(self,absname=None): # absorbing boundaries import re cubit.cmd('set info off') cubit.cmd('set echo off') cubit.cmd('set journal off') from sets import Set if not absname: absname=self.absname if self.cpml: if not absname: absname=self.cpmlname print 'Writing cpml'+absname+'.....' list_cpml=self.select_cpml() if list_cpml is False: print 'error writing cpml files' return else: abshex_cpml=open(absname,'w') hexcount=sum(map(len,list_cpml)) abshex_cpml.write(('%10i\n') % (hexcount)) for icpml,lcpml in enumerate(list_cpml): for hexa in lcpml: abshex_cpml.write(('%10i %10i\n') % (hexa,icpml)) stacey_absorb=True if stacey_absorb: # # if not absname: absname=self.absname # loops through all block definitions list_hex=cubit.parse_cubit_list('hex','all') for block,flag in zip(self.block_bc,self.block_bc_flag): if block != self.topography: name=cubit.get_exodus_entity_name('block',block) print ' block name:',name,'id:',block cknormal=True abshex_local=False # opens file if re.search('xmin',name): print 'xmin' abshex_local=open(absname+'_xmin','w') normal=(-1,0,0) elif re.search('xmax',name): print "xmax" abshex_local=open(absname+'_xmax','w') normal=(1,0,0) elif re.search('ymin',name): print "ymin" abshex_local=open(absname+'_ymin','w') normal=(0,-1,0) elif re.search('ymax',name): print "ymax" abshex_local=open(absname+'_ymax','w') normal=(0,1,0) elif re.search('bottom',name): print "bottom" abshex_local=open(absname+'_bottom','w') normal=(0,0,-1) elif re.search('abs',name): print "abs all - experimental, check the output" cknormal=False abshex_local=open(absname,'w') else: if block == 1003: print 'xmin' abshex_local=open(absname+'_xmin','w') normal=(-1,0,0) elif block == 1004: print "ymin" abshex_local=open(absname+'_ymin','w') normal=(0,-1,0) elif block == 1005: print "xmax" abshex_local=open(absname+'_xmax','w') normal=(1,0,0) elif block == 1006: print "ymax" abshex_local=open(absname+'_ymax','w') normal=(0,1,0) elif block == 1002: print "bottom" abshex_local=open(absname+'_bottom','w') normal=(0,0,-1) elif block == 1000: print "custumized" abshex_local=open(absname,'w') cknormal=False normal=None # # if abshex_local: # gets face elements quads_all=cubit.get_block_faces(block) dic_quads_all=dict(zip(quads_all,quads_all)) print ' number of faces = ',len(quads_all) abshex_local.write('%10i\n' % len(quads_all)) #command = "group 'list_hex' add hex in face "+str(quads_all) #command = command.replace("["," ").replace("]"," ").replace("("," ").replace(")"," ") #cubit.cmd(command) #group=cubit.get_id_from_name("list_hex") #list_hex=cubit.get_group_hexes(group) #command = "delete group "+ str(group) #cubit.cmd(command) for h in list_hex: faces=cubit.get_sub_elements('hex',h,2) for f in faces: if dic_quads_all.has_key(f): txt=self.create_facenode_string(h,f,normal=normal,cknormal=cknormal) abshex_local.write(txt) abshex_local.close() print 'Ok' cubit.cmd('set info on') cubit.cmd('set echo on')
def savemesh(mpiflag, iproc=0, filename=None): import start as start cfg = start.start_cfg(filename=filename) mpiflag, iproc, numproc, mpi = start.start_mpi() def runsave(meshfile, iproc, filename=None): import start as start cubit = start.start_cubit() cfg = start.start_cfg(filename=filename) flag = 0 ner = cubit.get_error_count() cubitcommand = 'save as "' + cfg.output_dir + \ '/' + meshfile + '.cub' + '" overwrite' cubit.cmd(cubitcommand) ner2 = cubit.get_error_count() if ner == ner2: cubitcommand = 'export mesh "' + cfg.output_dir + '/' + \ meshfile + '.e' + '" dimension 3 block all overwrite' cubit.cmd(cubitcommand) ner2 = cubit.get_error_count() if ner == ner2: flag = 1 return flag meshfile = 'mesh_vol_' + str(iproc) flagsaved = 0 infosave = (iproc, flagsaved) mpi.barrier() total_saved = mpi.allgather(flagsaved) if isinstance(total_saved, int): total_saved = [total_saved] ind = 0 saving = True while saving: if len(total_saved) != sum(total_saved): # if not flagsaved: flagsaved = runsave(meshfile, iproc, filename=filename) if flagsaved: infosave = (iproc, flagsaved) if numproc > 1: f = open('mesh_saved' + str(iproc), 'w') f.close() mpi.barrier() total_saved = mpi.allgather(flagsaved) if isinstance(total_saved, int): total_saved = [total_saved] ind = ind + 1 else: saving = False if ind > len(total_saved) + 10: saving = False print sum(total_saved), '/', len(total_saved), ' saved' info_total_saved = mpi.allgather(infosave) if isinstance(info_total_saved, int): info_total_saved = [info_total_saved] if iproc == 0: f = open('mesh_saving.log', 'w') f.write('\n'.join(str(x) for x in info_total_saved)) f.close() f = open(cfg.output_dir + '/' + 'blocks_' + str(iproc).zfill(5), 'w') blocks = cubit.get_block_id_list() for block in blocks: name = cubit.get_exodus_entity_name('block', block) element_count = cubit.get_exodus_element_count(block, "block") nattrib = cubit.get_block_attribute_count(block) attr = [cubit.get_block_attribute_value( block, x) for x in range(0, nattrib)] ty = cubit.get_block_element_type(block) f.write(str(block) + ' ; ' + name + ' ; nattr ' + str(nattrib) + ' ; ' + ' '.join(str(x) for x in attr) + ' ; ' + ty + ' ' + str(element_count) + '\n') f.close() import quality_log f = open(cfg.output_dir + '/' + 'quality_' + str(iproc).zfill(5), 'w') max_skewness, min_length = quality_log.quality_log(f) f.close() count_hex = [cubit.get_hex_count()] count_node = [cubit.get_node_count()] max_skew = [(iproc, max_skewness)] min_l = [(iproc, min_length)] mpi.barrier() total_min_l = mpi.gather(min_l) total_hex = mpi.gather(count_hex) total_node = mpi.gather(count_node) total_max_skew = mpi.gather(max_skew) mpi.barrier() if iproc == 0: min_total_min_l = min([ms[1] for ms in total_min_l]) max_total_max_skew = max([ms[1] for ms in total_max_skew]) sum_total_node = sum(total_node) sum_total_hex = sum(total_hex) totstat_file = open(cfg.output_dir + '/totstat.log', 'w') text = 'hex total number,node total number,max skew, min length\n' totstat_file.write(text) text = str(sum_total_hex) + ' , ' + str(sum_total_node) + ' , ' + \ str(max_total_max_skew) + ' , ' + str(min_total_min_l) + '\n' totstat_file.write(text) totstat_file.write(str(total_max_skew)) totstat_file.close() print 'meshing process end... proc ', iproc
def collect_new(cpuxmin=0,cpuxmax=1,cpuymin=0,cpuymax=1,cpux=1,cpuy=1,cubfiles=False,ckbound_method1=False,ckbound_method2=False,merge_tolerance=None,curverefining=False,outfilename='totalmesh_merged',qlog=False,export2SPECFEM3D=False,listblock=None,listflag=None,outdir='.',add_sea=False,decimate=False,cpml=False,cpml_size=False,top_absorbing=False,hex27=False,save_cubfile=True,check_merging=False): # cubit.cmd('set info off') cubit.cmd('set echo off') cubit.cmd('set journal off') #cubit.cmd('set error off') version_cubit=get_cubit_version() if decimate: if version_cubit >= 14.0: raise VersionException('check cubit version, decimate capability has been tested only with cubit <= 12.2') if version_cubit <= 12.2: collecting_merging(cpuxmin,cpuxmax,cpuymin,cpuymax,cpux,cpuy,cubfiles=cubfiles,ckbound_method1=ckbound_method1,ckbound_method2=ckbound_method2,merge_tolerance=merge_tolerance,decimate=decimate) elif version_cubit >= 14: collecting_merging_new(cpuxmin,cpuxmax+1,cpuymin,cpuymax+1,cpux,cpuy,cubfiles=cubfiles,check_merging=check_merging) else: raise VersionException('check cubit version, parallel capability of geocubit is working with cubit/trelis 14+ (or cubit 12.2)') cubit.cmd('set info on') cubit.cmd('set echo on') cubit.cmd('set journal on') #cubit.cmd('set error on') # if curverefining: if version_cubit <= 12.2: block=1001 #topography refine_closecurve(block,curverefining,acis=True) else: raise VersionException('check cubit version, refine curve capability has been tested only with cubit <= 12.2') # # if add_sea: if version_cubit <= 12.2: block=1001 add_sea_layer(block=block) else: raise VersionException('check cubit version, sea capability has been tested only with cubit <= 12.2') outdir2='/'.join(x for x in outfilename.split('/')[:-1]) if outdir2 == '': outdir2=outdir+'/' else: outdir2=outdir+'/'+outdir2+'/' import os try: os.makedirs(outdir2) except OSError: pass cubit.cmd('compress all') command="export mesh '"+outdir2+outfilename+".e' block all overwrite xml '"+outdir2+outfilename+".xml'" cubit.cmd(command) f=open(outdir2+'blocks.dat','w') blocks=cubit.get_block_id_list() # for block in blocks: name=cubit.get_exodus_entity_name('block',block) element_count = cubit.get_exodus_element_count(block, "block") nattrib=cubit.get_block_attribute_count(block) attr=[cubit.get_block_attribute_value(block,x) for x in range(0,nattrib)] ty=cubit.get_block_element_type(block) f.write(str(block)+' ; '+name+' ; nattr '+str(nattrib)+' ; '+' '.join(str(x) for x in attr)+' ; '+ty+' '+str(element_count)+'\n') f.close() # # cubit.cmd('set info echo journ off') cmd='del group all' cubit.silent_cmd(cmd) cubit.cmd('set info echo journ on') # # print 'end meshing' # # if qlog: print '\n\nQUALITY CHECK.... ***************\n\n' import quality_log tq=open(outdir2+outfilename+'.quality','w') max_skewness,min_length=quality_log.quality_log(tq) # # # if export2SPECFEM3D: e2SEM(files=False,listblock=listblock,listflag=listflag,outdir=outdir,cpml=cpml,cpml_size=cpml_size,top_absorbing=top_absorbing,hex27=hex27) if save_cubfile: vol_blocks=[x for x in blocks if x <=1000] cubit.cmd("create mesh geometry block "+' '.join(str(x) for x in vol_blocks)+" feature_angle 135.0") command = "save as '"+outdir2+outfilename+".cub' overwrite" print command cubit.cmd(command)
def block_definition(self): block_flag=[] block_mat=[] block_bc=[] block_bc_flag=[] material={} bc={} blocks=cubit.get_block_id_list() for block in blocks: name=cubit.get_exodus_entity_name('block',block) ty=cubit.get_block_element_type(block) #print block,blocks,ty,self.hex,self.face if self.hex in ty: nattrib=cubit.get_block_attribute_count(block) flag=None vel=None vs=None rho=None q=0 ani=0 # material domain id if "acoustic" in name : imaterial = 1 elif "elastic" in name: imaterial = 2 elif "poroelastic" in name: imaterial = 3 else : imaterial = 0 # if nattrib > 1: # material flag: # positive => material properties, # negative => interface/tomography domain flag=int(cubit.get_block_attribute_value(block,0)) if flag > 0 and nattrib >= 2: vel=cubit.get_block_attribute_value(block,1) if nattrib >= 3: vs=cubit.get_block_attribute_value(block,2) if nattrib >= 4: rho=cubit.get_block_attribute_value(block,3) if nattrib >= 5: q=cubit.get_block_attribute_value(block,4) # for q to be valid: it must be positive if q < 0 : print 'error, q value invalid:', q break if nattrib == 6: ani=cubit.get_block_attribute_value(block,5) elif flag < 0: vel=name attrib=cubit.get_block_attribute_value(block,1) if attrib == 1: kind='interface' flag_down=cubit.get_block_attribute_value(block,2) flag_up=cubit.get_block_attribute_value(block,3) elif attrib == 2: kind='tomography' elif nattrib == 1: flag=cubit.get_block_attribute_value(block,0) print 'only 1 attribute ', name,block,flag vel,vs,rho,q,ani=(0,0,0,0,0) else: flag=block vel,vs,rho,q,ani=(name,0,0,0,0) block_flag.append(int(flag)) block_mat.append(block) if flag > 0 and nattrib != 1: par=tuple([imaterial,flag,vel,vs,rho,q,ani]) elif flag < 0 and nattrib != 1: if kind=='interface': par=tuple([imaterial,flag,kind,name,flag_down,flag_up]) elif kind=='tomography': par=tuple([imaterial,flag,kind,name]) elif flag==0 or nattrib == 1: par=tuple([imaterial,flag,name]) material[block]=par elif ty == self.face: #Stacey condition, we need hex here for pml block_bc_flag.append(4) block_bc.append(block) bc[block]=4 #face has connectivity = 4 if name == self.topo or block == 1001: topography_face=block elif ty == 'SPHERE': pass else: # block elements differ from HEX8/QUAD4/SHELL4 print '****************************************' print 'block not properly defined:' print ' name:',name print ' type:',type print print 'please check your block definitions!' print print 'only supported types are:' print ' HEX/HEX8 for volumes' print ' QUAD4 for surface' print ' SHELL4 for surface' print '****************************************' continue return None, None,None,None,None,None,None,None nsets=cubit.get_nodeset_id_list() if len(nsets) == 0: self.receivers=None for nset in nsets: name=cubit.get_exodus_entity_name('nodeset',nset) if name == self.rec: self.receivers=nset else: print 'nodeset '+name+' not defined' self.receivers=None print block_mat print block_flag print block_bc print block_bc_flag print material print bc print topography_face # try: self.block_mat=block_mat self.block_flag=block_flag self.block_bc=block_bc self.block_bc_flag=block_bc_flag self.material=material self.bc=bc self.topography=topography_face except: print '****************************************' print 'sorry, no blocks or blocks not properly defined' print block_mat print block_flag print block_bc print block_bc_flag print material print bc print topography print '****************************************'
def block_definition(self): block_flag=[] block_mat=[] block_bc=[] block_bc_flag=[] material={} bc={} blocks=cubit.get_block_id_list() for block in blocks: name=cubit.get_exodus_entity_name('block',block) ty=cubit.get_block_element_type(block) #print block,blocks,ty,self.hex,self.face if self.hex in ty: flag=None vel=None vs=None rho=None q=0 ani=0 # material domain id if "acoustic" in name : imaterial = 1 elif "elastic" in name: imaterial = 2 elif "poroelastic" in name: imaterial = 3 else : imaterial = 0 # nattrib=cubit.get_block_attribute_count(block) if nattrib > 1: # material flag: # positive => material properties, # negative => interface/tomography domain flag=int(cubit.get_block_attribute_value(block,0)) if flag > 0 and nattrib >= 2: # material properties # vp vel=cubit.get_block_attribute_value(block,1) if nattrib >= 3: # vs vs=cubit.get_block_attribute_value(block,2) if nattrib >= 4: # density rho=cubit.get_block_attribute_value(block,3) if nattrib >= 5: # next: Q_kappa or Q_mu (new/old format style) q=cubit.get_block_attribute_value(block,4) if nattrib == 6: # only 6 parameters given (skipping Q_kappa ), old format style qmu = q #Q_kappa is 10 times stronger than Q_mu qk = q * 10 # last entry is anisotropic flag ani=cubit.get_block_attribute_value(block,5) elif nattrib > 6: #Q_kappa qk=q #Q_mu qmu=cubit.get_block_attribute_value(block,5) if nattrib == 7: #anisotropy_flag ani=cubit.get_block_attribute_value(block,6) # for q to be valid: it must be positive if qk < 0 or qmu < 0: print 'error, Q value invalid:',qk,qmu break elif flag < 0: # interface/tomography domain # velocity model vel=name attrib=cubit.get_block_attribute_value(block,1) if attrib == 1: kind='interface' flag_down=cubit.get_block_attribute_value(block,2) flag_up=cubit.get_block_attribute_value(block,3) elif attrib == 2: kind='tomography' elif nattrib == 1: flag=cubit.get_block_attribute_value(block,0) #print 'only 1 attribute ', name,block,flag vel,vs,rho,qk,qmu,ani=(0,0,0,9999.,9999.,0) else: flag=block vel,vs,rho,qk,qmu,ani=(name,0,0,9999.,9999.,0) block_flag.append(int(flag)) block_mat.append(block) if (flag > 0) and nattrib != 1: par=tuple([imaterial,flag,vel,vs,rho,qk,qmu,ani]) elif flag < 0 and nattrib != 1: if kind=='interface': par=tuple([imaterial,flag,kind,name,flag_down,flag_up]) elif kind=='tomography': par=tuple([imaterial,flag,kind,name]) elif flag==0 or nattrib == 1: par=tuple([imaterial,flag,name]) material[block]=par elif ty == self.face: block_bc_flag.append(4) block_bc.append(block) bc[block]=4 #face has connectivity = 4 if name == self.topo or block == 1001: self.topography=block if self.freetxt in name: self.free=block elif ty == 'SPHERE': pass else: # block elements differ from HEX8/QUAD4/SHELL4 print '****************************************' print 'block not properly defined:' print ' name:',name print ' type:',type print print 'please check your block definitions!' print print 'only supported types are:' print ' HEX/HEX8 for volumes' print ' QUAD4 for surface' print ' SHELL4 for surface' print '****************************************' continue return None, None,None,None,None,None,None,None nsets=cubit.get_nodeset_id_list() if len(nsets) == 0: self.receivers=None for nset in nsets: name=cubit.get_exodus_entity_name('nodeset',nset) if name == self.rec: self.receivers=nset else: print 'nodeset '+name+' not defined' self.receivers=None try: self.block_mat=block_mat self.block_flag=block_flag self.block_bc=block_bc self.block_bc_flag=block_bc_flag self.material=material self.bc=bc print 'HEX Blocks:' for m,f in zip(self.block_mat,self.block_flag): print 'block ',m,'material flag ',f print 'Absorbing Boundary Conditions:' for m,f in zip(self.block_bc,self.block_bc_flag): print 'bc ',m,'bc flag ',f print 'Topography (free surface)' print self.topography print 'Free surface' print self.free except: print '****************************************' print 'sorry, no blocks or blocks not properly defined' print block_mat print block_flag print block_bc print block_bc_flag print material print bc print '****************************************'
def collect(cpuxmin=0, cpuxmax=1, cpuymin=0, cpuymax=1, cpux=1, cpuy=1, cubfiles=False, ckbound_method1=False, ckbound_method2=False, merge_tolerance=None, curverefining=False, outfilename='totalmesh_merged', qlog=False, export2SPECFEM3D=False, listblock=None, listflag=None, outdir='.', add_sea=False, decimate=False, cpml=False, cpml_size=False, top_absorbing=False, hex27=False): # cubit.cmd('set journal error off') cubit.cmd('set verbose error off') collecting_merging(cpuxmin, cpuxmax, cpuymin, cpuymax, cpux, cpuy, cubfiles=cubfiles, ckbound_method1=ckbound_method1, ckbound_method2=ckbound_method2, merge_tolerance=merge_tolerance, decimate=decimate) cubit.cmd('set journal error on') cubit.cmd('set verbose error on') # if curverefining: block = 1001 #topography refine_closecurve(block, curverefining, acis=True) # # if add_sea: block = 1001 add_sea_layer(block=block) outdir2 = '/'.join(x for x in outfilename.split('/')[:-1]) if outdir2 == '': outdir2 = outdir + '/' else: outdir2 = outdir + '/' + outdir2 + '/' import os try: os.makedirs(outdir2) except OSError: pass cubit.cmd('compress all') command = "export mesh '" + outdir2 + outfilename + ".e' block all overwrite xml '" + outdir2 + outfilename + ".xml'" cubit.cmd(command) f = open(outdir2 + 'blocks.dat', 'w') blocks = cubit.get_block_id_list() # for block in blocks: name = cubit.get_exodus_entity_name('block', block) element_count = cubit.get_exodus_element_count(block, "block") nattrib = cubit.get_block_attribute_count(block) attr = [ cubit.get_block_attribute_value(block, x) for x in range(0, nattrib) ] ty = cubit.get_block_element_type(block) f.write( str(block) + ' ; ' + name + ' ; nattr ' + str(nattrib) + ' ; ' + ' '.join(str(x) for x in attr) + ' ; ' + ty + ' ' + str(element_count) + '\n') f.close() # # cubit.cmd('set info echo journ off') cmd = 'del group all' cubit.silent_cmd(cmd) cubit.cmd('set info echo journ on') # command = "save as '" + outdir2 + outfilename + ".cub' overwrite" cubit.cmd(command) # print 'end meshing' # # if qlog: print '\n\nQUALITY CHECK.... ***************\n\n' import quality_log tq = open(outdir2 + outfilename + '.quality', 'w') max_skewness, min_length = quality_log.quality_log(tq) # # # if export2SPECFEM3D: e2SEM(files=False, listblock=listblock, listflag=listflag, outdir=outdir, cpml=cpml, cpml_size=cpml_size, top_absorbing=top_absorbing, hex27=hex27)
def block_definition(self): block_flag = [] block_mat = [] block_bc = [] block_bc_flag = [] material = {} bc = {} blocks = cubit.get_block_id_list() for block in blocks: name = cubit.get_exodus_entity_name('block', block) type = cubit.get_block_element_type(block) print block, name, blocks, type, self.hex, self.face # block has hexahedral elements (HEX8) if type == self.hex: flag = None vel = None vs = None rho = None q = 0 ani = 0 # material domain id if name.find("acoustic") >= 0: imaterial = 1 elif name.find("elastic") >= 0: imaterial = 2 elif name.find("poroelastic") >= 0: imaterial = 3 else: imaterial = 0 print "block: ", name print " could not find appropriate material for this block..." print "" break nattrib = cubit.get_block_attribute_count(block) if nattrib != 0: # material flag: # positive => material properties, # negative => interface/tomography domain flag = int(cubit.get_block_attribute_value(block, 0)) if flag > 0 and nattrib >= 2: # vp vel = cubit.get_block_attribute_value(block, 1) if nattrib >= 3: # vs vs = cubit.get_block_attribute_value(block, 2) if nattrib >= 4: #density rho = cubit.get_block_attribute_value(block, 3) if nattrib >= 5: #Q_kappa qkappa = cubit.get_block_attribute_value( block, 4) # for q to be valid: it must be positive if qkappa < 0: print 'error, qkappa value invalid:', qkappa break if nattrib >= 6: #Q_mu qmu = cubit.get_block_attribute_value( block, 5) # for q to be valid: it must be positive if qmu < 0: print 'error, qmu value invalid:', qmu break if nattrib == 7: #anisotropy_flag ani = cubit.get_block_attribute_value( block, 5) elif flag < 0: # velocity model vel = name attrib = cubit.get_block_attribute_value(block, 1) if attrib == 1: kind = 'interface' flag_down = cubit.get_block_attribute_value( block, 2) flag_up = cubit.get_block_attribute_value(block, 3) elif attrib == 2: kind = 'tomography' else: flag = block vel, vs, rho, qkappa, qmu, ani = (name, 0, 0, 0, 0, 0) block_flag.append(int(flag)) block_mat.append(block) if flag > 0: par = tuple( [imaterial, flag, vel, vs, rho, qkappa, qmu, ani]) elif flag < 0: if kind == 'interface': par = tuple( [imaterial, flag, kind, name, flag_down, flag_up]) elif kind == 'tomography': par = tuple([imaterial, flag, kind, name]) elif flag == 0: par = tuple([imaterial, flag, name]) material[block] = par elif (type == self.face) or (type == self.face2): # block has surface elements (QUAD4 or SHELL4) block_bc_flag.append(4) block_bc.append(block) bc[block] = 4 #face has connectivity = 4 if name == self.topo: topography_face = block else: # block elements differ from HEX8/QUAD4/SHELL4 print '****************************************' print 'block not properly defined:' print ' name:', name print ' type:', type print print 'please check your block definitions!' print print 'only supported types are:' print ' HEX8 for volumes' print ' QUAD4 for surface' print ' SHELL4 for surface' print '****************************************' continue nsets = cubit.get_nodeset_id_list() if len(nsets) == 0: self.receivers = None for nset in nsets: name = cubit.get_exodus_entity_name('nodeset', nset) if name == self.rec: self.receivers = nset else: print 'nodeset ' + name + ' not defined' self.receivers = None try: self.block_mat = block_mat self.block_flag = block_flag self.block_bc = block_bc self.block_bc_flag = block_bc_flag self.material = material self.bc = bc self.topography = topography_face except: print '****************************************' print 'sorry, no blocks or blocks not properly defined' print block_mat print block_flag print block_bc print block_bc_flag print material print bc print topography print '****************************************'
def collect_old(cpuxmin=0,cpuxmax=1,cpuymin=0,cpuymax=1,cpux=1,cpuy=1,cubfiles=False,ckbound_method1=False,ckbound_method2=False,merge_tolerance=None,curverefining=False,outfilename='totalmesh_merged',qlog=False,export2SPECFEM3D=False,listblock=None,listflag=None,outdir='.',add_sea=False,decimate=False,cpml=False,cpml_size=False,top_absorbing=False,hex27=False): # cubit.cmd('set journal error off') cubit.cmd('set verbose error off') collecting_merging(cpuxmin,cpuxmax,cpuymin,cpuymax,cpux,cpuy,cubfiles=cubfiles,ckbound_method1=ckbound_method1,ckbound_method2=ckbound_method2,merge_tolerance=merge_tolerance,decimate=decimate) cubit.cmd('set journal error on') cubit.cmd('set verbose error on') # if curverefining: block=1001 #topography refine_closecurve(block,curverefining,acis=True) # # if add_sea: block=1001 add_sea_layer(block=block) outdir2='/'.join(x for x in outfilename.split('/')[:-1]) if outdir2 == '': outdir2=outdir+'/' else: outdir2=outdir+'/'+outdir2+'/' import os try: os.makedirs(outdir2) except OSError: pass cubit.cmd('compress all') command="export mesh '"+outdir2+outfilename+".e' block all overwrite xml '"+outdir2+outfilename+".xml'" cubit.cmd(command) f=open(outdir2+'blocks.dat','w') blocks=cubit.get_block_id_list() # for block in blocks: name=cubit.get_exodus_entity_name('block',block) element_count = cubit.get_exodus_element_count(block, "block") nattrib=cubit.get_block_attribute_count(block) attr=[cubit.get_block_attribute_value(block,x) for x in range(0,nattrib)] ty=cubit.get_block_element_type(block) f.write(str(block)+' ; '+name+' ; nattr '+str(nattrib)+' ; '+' '.join(str(x) for x in attr)+' ; '+ty+' '+str(element_count)+'\n') f.close() # # cubit.cmd('set info echo journ off') cmd='del group all' cubit.silent_cmd(cmd) cubit.cmd('set info echo journ on') # command = "save as '"+outdir2+outfilename+".cub' overwrite" cubit.cmd(command) # print 'end meshing' # # if qlog: print '\n\nQUALITY CHECK.... ***************\n\n' import quality_log tq=open(outdir2+outfilename+'.quality','w') max_skewness,min_length=quality_log.quality_log(tq) # # # if export2SPECFEM3D: e2SEM(files=False,listblock=listblock,listflag=listflag,outdir=outdir,cpml=cpml,cpml_size=cpml_size,top_absorbing=top_absorbing,hex27=hex27)
def get_block(self): ''' extract block information ''' block_flag = [] block_mat = [] block_free = [] block_absorb = [] block_pml = [] material = [] # number of blocks blocks = cubit.get_block_id_list() # go through blocks for block in blocks: name = cubit.get_exodus_entity_name('block', block) type = cubit.get_block_element_type(block) print block, name, blocks, type if type == self.tet: # check if elastic if name.find('elastic') >= 0: print('found elastic block') imat = 1 else: print('error, no correct material in block', block) # get number of attributes nattrib = cubit.get_block_attribute_count(block) if nattrib != 0: flag = int(cubit.get_block_attribute_value(block, 0)) vp = cubit.get_block_attribute_value(block, 1) vs = cubit.get_block_attribute_value(block, 2) rho = cubit.get_block_attribute_value(block, 3) qp = cubit.get_block_attribute_value(block, 4) qs = cubit.get_block_attribute_value(block, 5) pml = int(cubit.get_block_attribute_value(block, 6)) else: print('error no attributes in block', block) block_flag.append(flag) block_mat.append(block) block_pml.append(pml) material.append([imat, vp, vs, rho, qp, qs]) elif type == self.tri: if name.find('free') >= 0: print('found free surface') block_free.append(block) elif name.find('absorb') >= 0: print('found absorb surface') block_absorb.append(block) else: print('error in boundaries', block) # ns=cubit.get_nodeset_id_list() # for n in ns: # name=cubit.get_exodus_entity_name('nodeset',n) # if name.find('free') >=0: # print('found free surface nodes') # ns_free.append(n) # elif name.find('absorb') >=0: # print('found absorb surface nodes') # ns_absorb.append(n) # else: # print('error in boundaries',n) print('BLOCKFLAG', block_flag) self.block_flag = block_flag self.block_mat = block_mat self.mat = material self.block_free = block_free self.block_absorb = block_absorb self.block_pml = block_pml