def saveMeshPoisson(self): nNodes = cubit.get_node_count() nTets = cubit.get_tet_count() meshFile = open(folder+"meshPoisson.xml", 'w') meshFile.write('<mesh celltype="tetrahedron" dim="3">\n') meshFile.write(' <nodes size="%d">\n' % (nNodes)) for x in range(0, nNodes): coords = cubit.get_nodal_coordinates(x+1) meshFile.write(' <node id="%d" x="%f" y="%f" z="%f"/>\n' % (x,coords[0],coords[1],coords[2])) meshFile.write(' </nodes>\n') meshFile.write(' <elements size="%d">\n' % (nTets)) for x in range(0, nTets): nodes = cubit.get_connectivity("tet", x+1) meshFile.write(' <element id="%d" v0="%d" v1="%d" v2="%d" v3="%d"/>\n' % (x,nodes[0]-1,nodes[1]-1,nodes[2]-1,nodes[3]-1)) meshFile.write(' </elements>\n') meshFile.write(' <boundary celltype="triangle" dim="2">\n') surfs = cubit.get_relatives("volume", self.vol, "surface") ec = 0 for x in range(0, 2): tris = cubit.get_surface_tris(surfs[x]) for y in range(0, len(tris)): nodes = cubit.get_connectivity("tri", tris[y]) element = [nodes[0]-1, nodes[1]-1, nodes[2]-1] ec = ec+1 meshFile.write(' <element id="%d" marker="%d" v0="%d" v1="%d" v2="%d"/>\n' % (ec,x,element[0],element[1],element[2])) meshFile.write(' <element id="%d" marker="%d" v0="%d" v1="%d" v2="%d"/>\n' % (ec+1,2,element[0],element[1],element[2])) meshFile.write(' </boundary>\n') meshFile.write('</mesh>\n') meshFile.write('<poisson>\n') meshFile.write(' <neumann>\n') meshFile.write(' <node id="0" marker="0" value="-1.0" />\n') meshFile.write(' <node id="1" marker="1" value="1.0" />\n') meshFile.write(' </neumann>\n') meshFile.write(' <dirichlet>\n') meshFile.write(' <node id="2" marker="2" value="0.0" />\n') meshFile.write(' </dirichlet>\n') meshFile.write('</poisson>\n') meshFile.close()
def saveMesh2D2(self): cubit.cmd("create surface rectangle width 5000 height 5000 zplane") cubit.cmd("create surface rectangle width 1000 height 1000 zplane") cubit.cmd("subtract body 2 from body 1") surfID = cubit.get_last_id("surface") cubit.cmd("surface all size auto factor 4") cubit.cmd("surface all scheme TriMesh") cubit.cmd("mesh surface all") nNodes = cubit.get_node_count() meshFile = open(folder+"mesh2D2.xml", 'w') meshFile.write('<mesh celltype="triangle" dim="2">\n') meshFile.write(' <nodes size="%d">\n' % (nNodes)) for x in range(0, nNodes): coords = cubit.get_nodal_coordinates(x+1) meshFile.write(' <node id="%d" x="%f" y="%f" z="%f"/>\n' % (x,coords[0],coords[1],coords[2])) meshFile.write(' </nodes>\n') tris = cubit.get_surface_tris(surfID) meshFile.write(' <elements size="%d">\n' % (len(tris))) for x in range(0, len(tris)): nd = cubit.get_connectivity("tri", tris[x]) meshFile.write(' <element id="%d" v0="%d" v1="%d" v2="%d"/>\n' % (x,nd[0]-1,nd[1]-1,nd[2]-1)) meshFile.write(' </elements>\n') meshFile.write(' <element_data type="fiber_transversely_isotropic">\n') for x in range(0, len(tris)): meshFile.write(' <element id="%d">\n' %(x)) meshFile.write(' <fiber>1.000000,0.000000,0.000000</fiber>\n') meshFile.write(' </element>\n') meshFile.write(' </element_data>\n') meshFile.write(' <boundary celltype="line" dim="1">\n') eds = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] ec = 1 surf = cubit.surface(surfID) for y in range(0, len(eds)): nodes = cubit.get_connectivity("edge", eds[y]) element = [nodes[0]-1, nodes[1]-1] #cp = cubit.get_center_point("edge", eds[y]) #norm = surf.normal_at([cp[0],cp[1],cp[2]]) #cubit.cmd("create curve location %f %f %f direction %f %f %f length %f" % (cp[0],cp[1],cp[2], norm[0], norm[1], norm[2], 200)) #ec = ec+1 #meshFile.write(' <element id="%d" marker="%d" v0="%d" v1="%d" nx="%f" ny="%f" nz="%f"/>\n' % (ec,1,element[0],element[1], norm[0], norm[1], norm[2])) #meshFile.write(' <element id="%d" marker="%d" v0="%d" v1="%d" v2="%d"/>\n' % (ec+1,2,element[0],element[1],element[2])) meshFile.write(' </boundary>\n') meshFile.write('</mesh>\n') meshFile.write('<poisson>\n') meshFile.write(' <neumann>\n') meshFile.write(' <node id="1" marker="1" value="1.0" />\n') meshFile.write(' </neumann>\n') #meshFile.write(' <dirichlet>\n') #meshFile.write(' <node id="2" marker="2" value="0.0" />\n') #meshFile.write(' </dirichlet>\n') meshFile.write('</poisson>\n') meshFile.write('<electrophysiology>\n') meshFile.write(' <stimuli number="1">\n') bb = cubit.get_bounding_box("surface", surfID) x0 = bb[0] x1 = 0.8*x0 + 0.2*bb[1] y0 = bb[3] y1 = 0.8*y0 + 0.2*bb[4] #z0 = bb[6] #z1 = 0.8*z0 + 0.2*bb[7] meshFile.write(' <stim start="0.00" duration="4.00" value="-35.7140" x0="%f" x1="%f" y0="%f" y1="%f" />\n' % (x0,x1,y0,y1)) meshFile.write(' </stimuli>\n') meshFile.write('</electrophysiology>\n') meshFile.close()
def saveMesh3D(self): cubit.cmd("brick x 5000 y 5000 z 5000") cubit.cmd("create cylinder height 4000 radius 500") cubit.cmd("subtract body 2 from body 1") volID = cubit.get_last_id("volume") self.vol = volID self.mesh() nNodes = cubit.get_node_count() meshFile = open(folder+"mesh3D2.xml", 'w') meshFile.write('<mesh celltype="tetrahedron" dim="3">\n') meshFile.write(' <nodes size="%d">\n' % (nNodes)) for x in range(0, nNodes): coords = cubit.get_nodal_coordinates(x+1) meshFile.write(' <node id="%d" x="%f" y="%f" z="%f"/>\n' % (x,coords[0],coords[1],coords[2])) meshFile.write(' </nodes>\n') nTets = cubit.get_tet_count() meshFile.write(' <elements size="%d">\n' % (nTets)) for x in range(0, nTets): nodes = cubit.get_connectivity("tet", x+1) meshFile.write(' <element id="%d" v0="%d" v1="%d" v2="%d" v3="%d"/>\n' % (x,nodes[0]-1,nodes[1]-1,nodes[2]-1,nodes[3]-1)) meshFile.write(' </elements>\n') meshFile.write(' <element_data type="fiber_transversely_isotropic">\n') for x in range(0, nTets): meshFile.write(' <element id="%d">\n' %(x)) meshFile.write(' <fiber>1.000000,0.000000,0.000000</fiber>\n') meshFile.write(' </element>\n') meshFile.write(' </element_data>\n') meshFile.write(' <boundary celltype="triangle" dim="2">\n') bsurfs = [10, 11, 12] ec = 0 for x in range(0, len(bsurfs)): tris = cubit.get_surface_tris(bsurfs[x]) surf = cubit.surface(bsurfs[x]) for y in range(0, len(tris)): cp = cubit.get_center_point("tri", tris[y]) norm = surf.normal_at([cp[0],cp[1],cp[2]]) #cubit.cmd("create curve location %f %f %f direction %f %f %f length %f" % (cp[0],cp[1],cp[2],norm[0],norm[1],norm[2],200)) nodes = cubit.get_connectivity("tri", tris[y]) element = [nodes[0]-1, nodes[1]-1, nodes[2]-1] meshFile.write(' <element id="%d" marker="%d" v0="%d" v1="%d" v2="%d" nx="%f" ny="%f" nz="%f"/>\n' % (ec,1.0,element[0],element[1],element[2],norm[0],norm[1],norm[2])) ec = ec+1 meshFile.write(' </boundary>\n') meshFile.write('</mesh>\n') meshFile.write('<poisson>\n') meshFile.write(' <neumann>\n') meshFile.write(' <node id="1" marker="1" value="0.5" />\n') meshFile.write(' </neumann>\n') meshFile.write('</poisson>\n') #meshFile.write('<electrophysiology>\n') #meshFile.write(' <stimuli number="1">\n') #bb = cubit.get_bounding_box("volume", self.vol) #x0 = bb[0] #x1 = 0.8*x0 + 0.2*bb[1] #y0 = bb[3] #y1 = 0.8*y0 + 0.2*bb[4] #z0 = bb[6] #z1 = 0.8*z0 + 0.2*bb[7] #meshFile.write(' <stim start="0.00" duration="4.00" value="-35.7140" x0="%f" x1="%f" y0="%f" y1="%f" z0="%f" z1="%f" />\n' % (x0,x1,y0,y1,z0,z1)) #meshFile.write(' </stimuli>\n') #meshFile.write('</electrophysiology>\n') meshFile.close()
def saveMesh(self): nNodes = cubit.get_node_count() nTets = cubit.get_tet_count() meshFile = open(folder+"Cubo.xml", 'w') meshFile.write('<mesh celltype="tetrahedron" dim="3">\n') meshFile.write(' <nodes size="%d">\n' % (nNodes)) for x in range(0, nNodes): coords = cubit.get_nodal_coordinates(x+1) meshFile.write(' <node id="%d" x="%f" y="%f" z="%f"/>\n' % (x,coords[0],coords[1],coords[2])) meshFile.write(' </nodes>\n') meshFile.write(' <elements size="%d">\n' % (nTets)) for x in range(0, nTets): nodes = cubit.get_connectivity("tet", x+1) meshFile.write(' <element id="%d" v0="%d" v1="%d" v2="%d" v3="%d"/>\n' % (x,nodes[0]-1,nodes[1]-1,nodes[2]-1,nodes[3]-1)) meshFile.write(' </elements>\n') meshFile.write(' <element_data type="fiber_transversely_isotropic">\n') for x in range(0, nTets): meshFile.write(' <element id="%d">\n' %(x)) meshFile.write(' <fiber>1.000000,0.000000,0.000000</fiber>\n') meshFile.write(' </element>\n') meshFile.write(' </element_data>\n') meshFile.write(' <boundary celltype="triangle" dim="2">\n') bsurfs = cubit.get_relatives("volume", self.vol, "surface") ec = 0 for x in range(0, len(bsurfs)): #for x in range(6, len(bsurfs)): #if x is not 16 and x is not 14: tris = cubit.get_surface_tris(bsurfs[x]) surf = cubit.surface(bsurfs[x]) for y in range(0, len(tris)): cp = cubit.get_center_point("tri", tris[y]) norm = surf.normal_at([cp[0],cp[1],cp[2]]) #cubit.cmd("create curve location %f %f %f direction %f %f %f length %f" % (cp[0],cp[1],cp[2],norm[0],norm[1],norm[2],200)) nodes = cubit.get_connectivity("tri", tris[y]) element = [nodes[0]-1, nodes[1]-1, nodes[2]-1] meshFile.write(' <element id="%d" marker="%d" v0="%d" v1="%d" v2="%d" nx="%f" ny="%f" nz="%f"/>\n' % (ec,1.0,element[0],element[1],element[2],norm[0],norm[1],norm[2])) ec = ec+1 meshFile.write(' </boundary>\n') #meshFile.write(' <boundary celltype="triangle" dim="2">\n') #surfs = cubit.get_relatives("volume", self.vol, "surface") #ec = 0 #for x in range(0, 2): # tris = cubit.get_surface_tris(surfs[x]) # for y in range(0, len(tris)): # nodes = cubit.get_connectivity("tri", tris[y]) # element = [nodes[0]-1, nodes[1]-1, nodes[2]-1] # ec = ec+1 # meshFile.write(' <element id="%d" marker="%d" v0="%d" v1="%d" v2="%d"/>\n' % (ec,x,element[0],element[1],element[2])) #meshFile.write(' <element id="%d" marker="%d" v0="%d" v1="%d" v2="%d"/>\n' % (ec+1,2,element[0],element[1],element[2])) #meshFile.write(' </boundary>\n') meshFile.write('</mesh>\n') meshFile.write('<poisson>\n') meshFile.write(' <neumann>\n') #meshFile.write(' <node id="0" marker="0" value="-1.0" />\n') meshFile.write(' <node id="1" marker="1" value="0.05"/>\n') meshFile.write(' </neumann>\n') #meshFile.write(' <dirichlet>\n') #meshFile.write(' <node id="2" marker="2" value="0.0" />\n') #meshFile.write(' </dirichlet>\n') meshFile.write('</poisson>\n') #meshFile.write('<electrophysiology>\n') #meshFile.write(' <stimuli number="1">\n') #bb = cubit.get_bounding_box("volume", self.vol) #x0 = bb[0] #x1 = 0.8*x0 + 0.2*bb[1] #y0 = bb[3] #y1 = 0.8*y0 + 0.2*bb[4] #z0 = bb[6] #z1 = 0.8*z0 + 0.2*bb[7] #meshFile.write(' <stim start="0.00" duration="4.00" value="-35.7140" x0="%f" x1="%f" y0="%f" y1="%f" z0="%f" z1="%f" />\n' % (x0,x1,y0,y1,z0,z1)) #meshFile.write(' </stimuli>\n') #meshFile.write('</electrophysiology>\n') meshFile.close()
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 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
side = 5 elif set(nodes).issubset(set(np.array(nodes_hx)[[4, 5, 6, 7]])): side = 6 trac_el.append([hx, side]) mat_typ[hx - 1] = 2 mat = [[ 3.0E10, 0.25, 1.0E25, 1.0, 3000, 1.0E-12, 0.9664429530201342, 0.0, 2.2E9 ], [3.0E10, 0.25, 1.0E25, 1.0, 3000, 1.0E-12, 0.9664429530201342, 0.0, 2.2E9]] trac_bc = np.zeros(shape=[len(trac_el), 6]) trac_bc[:, 2] = -1E6 coord = np.empty(shape=[0, 3], dtype=np.float16) hx_node = np.empty(shape=[0, 8], dtype=np.uint16) for node in range(cubit.get_node_count()): coord = np.vstack((coord, cubit.get_nodal_coordinates(node + 1))) bcy_nodes = cubit.get_nodeset_nodes_inclusive(1) bcx_nodes = cubit.get_nodeset_nodes_inclusive(2) bcz_nodes = cubit.get_nodeset_nodes_inclusive(3) bcp_nodes = cubit.get_nodeset_nodes_inclusive(4) bc_typ = np.ones((cubit.get_node_count(), 4), dtype=np.int8) for node in bcx_nodes: bc_typ[node - 1, 0] = 0 for node in bcy_nodes: bc_typ[node - 1, 1] = 0 for node in bcz_nodes: bc_typ[node - 1, 2] = 0 for node in bcp_nodes: