def hex_simulation_parameter(self, h, vp_static=None, vs_static=None): nodes = cubit.get_connectivity('Hex', h) id_nodes = [0, 1, 2, 3, 4, 5, 6, 7] id_faces = [[1, 3, 4], [0, 2, 5], [1, 3, 6], [0, 2, 7], [5, 7], [4, 6], [5, 7], [4, 6]] dt = [] pmax = [] vmax = [] vmin = [] for i in id_nodes[:-1]: for j in id_nodes[i + 1:]: x1, y1, z1 = cubit.get_nodal_coordinates(nodes[i]) x2, y2, z2 = cubit.get_nodal_coordinates(nodes[j]) nvp1, nvs1 = self.tomo(x1, y1, z1, vp_static, vs_static) nvp2, nvs2 = self.tomo(x2, y2, z2, vp_static, vs_static) d = math.sqrt((x2 - x1)**2 + (y2 - y1)**2 + (z2 - z1)**2) #pmax_tmp=d*self.gllcoeff/min(nvp1,nvp2,nvs1,nvs2)*self.Ngll_per_wavelength pmax_tmp = d * (.5 - self.gllcoeff) / min( nvp1, nvp2, nvs1, nvs2) * self.Ngll_per_wavelength #more conservative..... dt_tmp = self.Cmax * d * self.gllcoeff / max( nvp1, nvp2, nvs1, nvs2) dt.append(dt_tmp) pmax.append(pmax_tmp) vmax.append(max(nvp1, nvp2, nvs1, nvs2)) vmin.append(min(nvp1, nvp2, nvs1, nvs2)) return min(dt), max(pmax), min(vmin), max(vmax)
def hex_simulation_parameter(self, h, vp_static=None, vs_static=None): nodes = cubit.get_connectivity("Hex", h) id_nodes = [0, 1, 2, 3, 4, 5, 6, 7] id_faces = [[1, 3, 4], [0, 2, 5], [1, 3, 6], [0, 2, 7], [5, 7], [4, 6], [5, 7], [4, 6]] dt = [] pmax = [] vmax = [] vmin = [] for i in id_nodes[:-1]: for j in id_nodes[i + 1 :]: x1, y1, z1 = cubit.get_nodal_coordinates(nodes[i]) x2, y2, z2 = cubit.get_nodal_coordinates(nodes[j]) nvp1, nvs1 = self.tomo(x1, y1, z1, vp_static, vs_static) nvp2, nvs2 = self.tomo(x2, y2, z2, vp_static, vs_static) d = math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2 + (z2 - z1) ** 2) # pmax_tmp=d*self.gllcoeff/min(nvp1,nvp2,nvs1,nvs2)*self.Ngll_per_wavelength pmax_tmp = ( d * (0.5 - self.gllcoeff) / min(nvp1, nvp2, nvs1, nvs2) * self.Ngll_per_wavelength ) # more conservative..... dt_tmp = self.Cmax * d * self.gllcoeff / max(nvp1, nvp2, nvs1, nvs2) dt.append(dt_tmp) pmax.append(pmax_tmp) vmax.append(max(nvp1, nvp2, nvs1, nvs2)) vmin.append(min(nvp1, nvp2, nvs1, nvs2)) return min(dt), max(pmax), min(vmin), max(vmax)
def edge_length(self,edge): """ length=edge_length(edge) return the length of a edge """ from math import sqrt nodes=cubit.get_connectivity('Edge',edge) x0,y0,z0=cubit.get_nodal_coordinates(nodes[0]) x1,y1,z1=cubit.get_nodal_coordinates(nodes[1]) d=sqrt((x1-x0)**2+(y1-y0)**2+(z1-z0)**2) return d
def edge_length(self,edge): """ length = edge_length(edge) return the length of a edge """ from math import sqrt nodes = cubit.get_connectivity('edge',edge) x0,y0,z0 = cubit.get_nodal_coordinates(nodes[0]) x1,y1,z1 = cubit.get_nodal_coordinates(nodes[1]) d = sqrt((x1-x0)**2+(y1-y0)**2+(z1-z0)**2) return d
def jac_check(self,nodes): x0 = cubit.get_nodal_coordinates(nodes[0]) x1 = cubit.get_nodal_coordinates(nodes[1]) x2 = cubit.get_nodal_coordinates(nodes[2]) xv1 = x1[0]-x0[0] xv2 = x2[0]-x1[0] zv1 = x1[2]-x0[2] zv2 = x2[2]-x1[2] jac = -xv2*zv1+xv1*zv2 if jac > 0: return nodes elif jac < 0: # change the ordre for the local coordinate system for 9 node finite elements Page.11 in Specfem2d-manual.pdf return nodes[0],nodes[3],nodes[2],nodes[1],nodes[7],nodes[6],nodes[5],nodes[4],nodes[8] else: print 'error, jacobian = 0', jac,nodes
def jac_check(self,nodes): x0 = cubit.get_nodal_coordinates(nodes[0]) x1 = cubit.get_nodal_coordinates(nodes[1]) x2 = cubit.get_nodal_coordinates(nodes[2]) xv1 = x1[0]-x0[0] xv2 = x2[0]-x1[0] zv1 = x1[2]-x0[2] zv2 = x2[2]-x1[2] jac = -xv2*zv1+xv1*zv2 if jac > 0: return nodes elif jac < 0: return nodes[0],nodes[3],nodes[2],nodes[1] else: print 'error, jacobian = 0', jac,nodes
def jac_check(self, nodes): x0 = cubit.get_nodal_coordinates(nodes[0]) x1 = cubit.get_nodal_coordinates(nodes[1]) x2 = cubit.get_nodal_coordinates(nodes[2]) xv1 = x1[0] - x0[0] xv2 = x2[0] - x1[0] zv1 = x1[2] - x0[2] zv2 = x2[2] - x1[2] jac = -xv2 * zv1 + xv1 * zv2 if jac > 0: return nodes elif jac < 0: return nodes[0], nodes[3], nodes[2], nodes[1] else: print 'error, jacobian = 0', jac, nodes
def getTetsOverlapPassedVols(target_list, volume_ids): """ Return tetraherons overlap with mouse selected CUBIT volumes. Parameters: * target_list List of indices of target tetrahedrons Return: List of indices of tetrahedrons overlap with the volumes """ in_list = [] for t in target_list: verts = cubit.get_connectivity("tet", t) cords = [] for v in verts: c = cubit.get_nodal_coordinates(v) cords.append(c) for vol_id in volume_ids: volume = cubit.volume(vol_id) body = volume.bodies()[0] within = False for cord in cords: status = body.point_containment(cord) if status == 1: within = True break if within: in_list.append(t) break return in_list
def getNodesBoundInSelectedVols(target_list): """ Return nodes bound in mouse selected CUBIT volumes. Parameters: * target_list List of indices of target nodes Return: List of indices of nodes bound by the volumes """ group_id = cubit.create_new_group() cubit.silent_cmd("group %i add selection" % (group_id)) volume_ids = cubit.get_group_volumes(group_id) in_list = [] for v in target_list: cords = cubit.get_nodal_coordinates(v) for vol_id in volume_ids: volume = cubit.volume(vol_id) body = volume.bodies()[0] status = body.point_containment(cords) if status == 1 or status == 2: in_list.append(v) break cubit.delete_group(group_id) return in_list
def get_nodes_inside_curve(nodes, curve): vx,vy,n=curve2poly(curve) nodes_inside=[] for n in nodes: vp=cubit.get_nodal_coordinates(n) if pinpoly(vp[0],vp[1],vx,vy): nodes_inside.append(n) return nodes_inside
def nodescoord_write(self,nodecoord_name): nodecoord=open(nodecoord_name,'w') print 'Writing '+nodecoord_name+'.....' node_list=cubit.parse_cubit_list('node','all') # number of nodes num_nodes=len(node_list) print ' number of nodes:',str(num_nodes) nodecoord.write('%10i\n' % num_nodes) # coordinates for node in node_list: x,y,z=cubit.get_nodal_coordinates(node) # min/max self.xmin,self.xmax=self.get_extreme(x,self.xmin,self.xmax) self.ymin,self.ymax=self.get_extreme(y,self.ymin,self.ymax) self.zmin,self.zmax=self.get_extreme(z,self.zmin,self.zmax) if abs(x) < 1.0 and abs(y) < 1.0 and abs(z) < 1.0: # avoids problem w/ fixed floating point formatting for very small values txt=('%10i %20e %20e %20e\n') % (node,x,y,z) else: # standard float format txt=('%10i %20f %20f %20f\n') % (node,x,y,z) nodecoord.write(txt) nodecoord.close() print ' x-coordinate min/max:',str(self.xmin),str(self.xmax) print ' y-coordinate min/max:',str(self.ymin),str(self.ymax) print ' z-coordinate min/max:',str(self.zmin),str(self.zmax) print 'Ok'
def getNodesBoundInSelectedVols(target_list): """ Return nodes bound in selected CUBIT volumes. Parameters: * target_list List of indices of target nodes Return: List of indices of nodes bound by the volumes """ group_id = cubit.create_new_group() cubit.silent_cmd("group %i add selection" % (group_id)) volume_ids = cubit.get_group_volumes(group_id) in_list = [] for v in target_list: cords = cubit.get_nodal_coordinates(v) for vol_id in volume_ids: volume = cubit.volume(vol_id) body = volume.bodies()[0] status = body.point_containment(cords) if status == 1 or status == 2: in_list.append(v) break cubit.delete_group(group_id) return in_list
def normal_check(self,nodes,normal): tres=.2 p0=cubit.get_nodal_coordinates(nodes[0]) p1=cubit.get_nodal_coordinates(nodes[1]) p2=cubit.get_nodal_coordinates(nodes[2]) a=[p1[0]-p0[0],p1[1]-p0[1],p1[2]-p0[2]] b=[p2[0]-p1[0],p2[1]-p1[1],p2[2]-p1[2]] axb=[a[1]*b[2] - a[2]*b[1], a[2]*b[0] - a[0]*b[2], a[0]*b[1] - a[1]*b[0]] dot=0.0 for i in (0,1,2): dot=dot+axb[i]*normal[i] if dot > 0: return nodes elif dot < 0: return nodes[0],nodes[3],nodes[2],nodes[1] else: print 'error: surface normal, dot=0', axb,normal,dot,p0,p1,p2
def rec_write(self, recname): print 'Writing ' + self.recname + '.....' recfile = open(self.recname, 'w') nodes = cubit.get_nodeset_nodes(self.receivers) for i, n in enumerate(nodes): x, y, z = cubit.get_nodal_coordinates(n) recfile.write('ST%i XX %20f %20f 0.0 0.0 \n' % (i, x, z)) recfile.close()
def rec_write(self,recname): print 'Writing '+self.recname+'.....' recfile=open(self.recname,'w') nodes=cubit.get_nodeset_nodes(self.receivers) for i,n in enumerate(nodes): x,y,z=cubit.get_nodal_coordinates(n) recfile.write('ST%i XX %20f %20f 0.0 0.0 \n' % (i,x,z)) recfile.close()
def rec_write(self, recname): print "Writing " + self.recname + "....." recfile = open(self.recname, "w") nodes = cubit.get_nodeset_nodes(self.receivers) for i, n in enumerate(nodes): x, y, z = cubit.get_nodal_coordinates(n) recfile.write("ST%i XX %20f %20f 0.0 0.0 \n" % (i, x, z)) recfile.close() print "Ok"
def rec_write(self,recname): """ Write receivers coordinates on file recname """ print 'Writing '+self.recname+'.....' recfile = open(self.recname,'w') nodes = cubit.get_nodeset_nodes(self.receivers) # Import nodes in nodeset containing receiver positions for i,n in enumerate(nodes): # For each receiver x,y,z = cubit.get_nodal_coordinates(n) # Import its coordinates (3 coordinates even for a 2D model in cubit) recfile.write('ST%i XX %20f %20f 0.0 0.0 \n' % (i,x,z)) # Write x and z coordinates on the file -> Model must be in x,z coordinates. TODO recfile.close() print 'Ok'
def write_node(self, path): f = open(path + self.filename_nodes, 'w') nodes = cubit.parse_cubit_list('node', 'all') nnodes = len(nodes) print "writing node coordinates file '" + path + self.filename_nodes + "' containing " + str( nnodes) + " nodes" f.write('%10i\n' % nnodes) # for node in nodes: xyz = cubit.get_nodal_coordinates(node) f.write('%20f %20f %20f\n' % xyz) f.close()
def boundary(self): boundary = [] surfs = cubit.get_relatives("volume", self.vol, "surface") for x in range(6, len(surfs)): tris = cubit.get_surface_tris(surfs[x]) for y in range(0, len(tris)): nodes = cubit.get_connectivity("tri", tris[y]) p1 = cubit.get_nodal_coordinates(nodes[0]) p2 = cubit.get_nodal_coordinates(nodes[1]) p3 = cubit.get_nodal_coordinates(nodes[2]) u_0 = [p2[0]-p1[0],p2[1]-p1[1],p2[2]-p1[2]] u_1 = [p3[0]-p1[0],p3[1]-p1[1],p3[2]-p1[2]] u1 = geo2.crossProduct(u_0, u_1) u1 = geo2.getVector4_3(u1) geo2.normalizeVector(u1) # if y == 0: # center_point = cubit.get_center_point("tri", tris[y]) # cubit.cmd("create curve location %f %f %f direction %f %f %f length %f" %(center_point[0],center_point[1],center_point[2],u1[0],u1[1],u1[2],self.root.radius)) element = [nodes[0]-1, nodes[1]-1, nodes[2]-1, u1[0], u1[1], u1[2]] boundary.append(element) return boundary
def hex_metric(self, h): nodes = cubit.get_connectivity('Hex', h) equiangle_skewness = None min_angle = float('infinity') edge_length_min = float('infinity') max_angle = None edge_length_max = None #loopnode=[(i,j) for i in range(8) for j in range(i+1,8)] if len(nodes) == 4: faces = [[0, 1, 2, 3]] elif len(nodes) == 8: faces = [[0, 1, 2, 3], [4, 5, 6, 7], [1, 5, 6, 2], [0, 4, 7, 3], [2, 6, 7, 3], [1, 5, 4, 0]] else: print 'bad definition of nodes' return None, None, None x = [] y = [] z = [] for i in nodes: n = cubit.get_nodal_coordinates(i) x.append(n[0]) y.append(n[1]) z.append(n[2]) for face in faces: for i in range(-1, 3): vx1 = x[face[i - 1]] - x[face[i]] vy1 = y[face[i - 1]] - y[face[i]] vz1 = z[face[i - 1]] - z[face[i]] # vx2 = x[face[i + 1]] - x[face[i]] vy2 = y[face[i + 1]] - y[face[i]] vz2 = z[face[i + 1]] - z[face[i]] # norm1 = math.sqrt(vx1 * vx1 + vy1 * vy1 + vz1 * vz1) norm2 = math.sqrt(vx2 * vx2 + vy2 * vy2 + vz2 * vz2) # if norm1 == 0 or norm2 == 0: print 'degenerated mesh, 0 length edge' import sys sys.exit() angle = math.acos( (vx1 * vx2 + vy1 * vy2 + vz1 * vz2) / (norm1 * norm2)) # equiangle_skewness = max( equiangle_skewness, math.fabs(2. * angle - math.pi) / math.pi) min_angle = min(min_angle, angle * 180. / math.pi) max_angle = max(max_angle, angle * 180. / math.pi) edge_length_min = min(edge_length_min, norm1) edge_length_max = max(edge_length_max, norm1) return round(equiangle_skewness, 5), min_angle, max_angle, round( edge_length_min, 5), round(edge_length_max, 5)
def nodescoord_write(self,nodecoord_name): """ Write nodes coordinates on file : nodecoord_name """ nodecoord = open(nodecoord_name,'w') print 'Writing '+nodecoord_name+'.....' node_list = cubit.parse_cubit_list('node','all') # Import all the nodes of the model num_nodes = len(node_list) # Total number of nodes nodecoord.write('%10i\n' % num_nodes) # Write the number of nodes on the first line for node in node_list: # For all nodes x,y,z = cubit.get_nodal_coordinates(node) # Import its coordinates (3 coordinates even for a 2D model in cubit) txt = ('%20f %20f\n') % (x,z) nodecoord.write(txt) # Write x and z coordinates on the file -> Model must be in x,z coordinates. TODO nodecoord.close() print 'Ok'
def nodescoord_write(self,nodecoord_name): nodecoord=open(nodecoord_name,'w') print 'Writing '+nodecoord_name+'.....' node_list=cubit.parse_cubit_list('node','all') num_nodes=len(node_list) print ' number of nodes:',str(num_nodes) nodecoord.write('%10i\n' % num_nodes) # for node in node_list: x,y,z=cubit.get_nodal_coordinates(node) txt=('%10i %20f %20f %20f\n') % (node,x,y,z) nodecoord.write(txt) nodecoord.close()
def hex_metric(self, h): nodes = cubit.get_connectivity('Hex', h) equiangle_skewness = None min_angle = float('infinity') edge_length_min = float('infinity') max_angle = None edge_length_max = None if len(nodes) == 4: faces = [[0, 1, 2, 3]] elif len(nodes) == 8: faces = [[0, 1, 2, 3], [4, 5, 6, 7], [1, 5, 6, 2], [0, 4, 7, 3], [2, 6, 7, 3], [1, 5, 4, 0]] else: print 'bad definition of nodes' return None, None, None x = [] y = [] z = [] for i in nodes: n = cubit.get_nodal_coordinates(i) x.append(n[0]) y.append(n[1]) z.append(n[2]) for face in faces: for i in range(-1, 3): vx1 = x[face[i - 1]] - x[face[i]] vy1 = y[face[i - 1]] - y[face[i]] vz1 = z[face[i - 1]] - z[face[i]] # vx2 = x[face[i + 1]] - x[face[i]] vy2 = y[face[i + 1]] - y[face[i]] vz2 = z[face[i + 1]] - z[face[i]] # norm1 = math.sqrt(vx1 * vx1 + vy1 * vy1 + vz1 * vz1) norm2 = math.sqrt(vx2 * vx2 + vy2 * vy2 + vz2 * vz2) # if norm1 == 0 or norm2 == 0: print 'degenerated mesh, 0 length edge' import sys sys.exit() angle = math.acos( (vx1 * vx2 + vy1 * vy2 + vz1 * vz2) / (norm1 * norm2)) # equiangle_skewness = max(equiangle_skewness, math.fabs( 2. * angle - math.pi) / math.pi) min_angle = min(min_angle, angle * 180. / math.pi) max_angle = max(max_angle, angle * 180. / math.pi) edge_length_min = min(edge_length_min, norm1) edge_length_max = max(edge_length_max, norm1) return round(equiangle_skewness, 5), min_angle, max_angle, \ round(edge_length_min, 5), round(edge_length_max, 5)
def nodescoord_write(self, nodecoord_name): nodecoord = open(nodecoord_name, 'w') print 'Writing ' + nodecoord_name + '.....' node_list = cubit.parse_cubit_list('node', 'all') num_nodes = len(node_list) print ' number of nodes:', str(num_nodes) nodecoord.write('%10i\n' % num_nodes) # for node in node_list: x, y, z = cubit.get_nodal_coordinates(node) txt = ('%10i %20f %20f %20f\n') % (node, x, y, z) nodecoord.write(txt) nodecoord.close() print 'Ok'
def nodescoord_write(self, nodecoord_name): nodecoord = open(nodecoord_name, "w") print "Writing " + nodecoord_name + "....." node_list = cubit.parse_cubit_list("node", "all") num_nodes = len(node_list) print " number of nodes:", str(num_nodes) nodecoord.write("%10i\n" % num_nodes) # for node in node_list: x, y, z = cubit.get_nodal_coordinates(node) txt = ("%10i %20f %20f %20f\n") % (node, x, y, z) nodecoord.write(txt) nodecoord.close() print "Ok"
def curve2poly(line): curve=int(line) cubit.cmd('curve '+str(curve)+' size auto factor 1') cubit.cmd('mesh curve '+str(curve)) n=cubit.get_curve_nodes(curve) orientnode=[] vertex_list = cubit.get_relatives("curve", curve, "vertex") if len(vertex_list) != 0: startnode=cubit.get_vertex_node(vertex_list[0]) cubit.cmd('del group pgon') cubit.cmd("group 'pgon' add edge in node "+str(startnode)) group1 = cubit.get_id_from_name("pgon") edges = list(cubit.get_group_edges(group1)) edgestart=edges[0] else: startnode=n[0] cubit.cmd('del group pgon') cubit.cmd("group 'pgon' add edge in node "+str(startnode)) group1 = cubit.get_id_from_name("pgon") edges = list(cubit.get_group_edges(group1)) edgestart=edges[0] begin=startnode orientnode.append(begin) node_id_list = list(cubit.get_connectivity("edge", edgestart)) node_id_list.remove(startnode) startnode=node_id_list[0] orientnode.append(startnode) stopflag=False while startnode != begin and not stopflag: cubit.cmd('del group pgon') cubit.cmd("group 'pgon' add edge in node "+str(startnode)) group1 = cubit.get_id_from_name("pgon") edges = list(cubit.get_group_edges(group1)) if len(edges) != 1: edges.remove(edgestart) edgestart=edges[0] node_id_list = list(cubit.get_connectivity("edge", edgestart)) node_id_list.remove(startnode) orientnode.append(node_id_list[0]) startnode=node_id_list[0] else: stopflag=True vx=[] vy=[] for n in orientnode: v=cubit.get_nodal_coordinates(n) vx.append(v[0]) vy.append(v[1]) return vx,vy,orientnode
def coord_write(self, coord_name): ''' write nodes file ''' coord = open(coord_name, 'w') print('writing ' + coord_name) node_list = cubit.parse_cubit_list('node', 'all') ncoord = len(node_list) print('number of nodes:', str(ncoord)) # write ncoord coord.write('%10i\n' % ncoord) #write coords for node in node_list: x, y, z = cubit.get_nodal_coordinates(node) txt = ('%10i %20f %20f\n') % (node, x, y) coord.write(txt) coord.close() print('Ok')
def getMeshData(): ELEM = cubit.get_entities("tri") NODE = cubit.get_entities("node") nELEM = len(ELEM) nNODE = len(NODE) eCONNECT = numpy.zeros(shape=(nELEM,3),dtype="int64") COORD = numpy.zeros(shape=(nNODE,3),dtype="double") for i in range(0,nELEM): eCONNECT[i,:] = list(cubit.get_connectivity("tri",ELEM[i])) eCONNECT = eCONNECT - 1 for i in range(0,nNODE): COORD[i,:] = list(cubit.get_nodal_coordinates(NODE[i])) return ELEM, NODE, eCONNECT, COORD
def MakePolyLine(nodeset): # A polyline is a cell that represents a set of 1D lines numberOfVertices = len(nodeset) points = vtk.vtkPoints() for node in nodeset: x, y, z = cubit.get_nodal_coordinates(node) points.InsertNextPoint(x, z, y) polyline = vtk.vtkPolyLine() polyline.GetPointIds().SetNumberOfIds(numberOfVertices) for i in range(0, numberOfVertices): polyline.GetPointIds().SetId(i, i) ug = vtk.vtkUnstructuredGrid() ug.SetPoints(points) ug.InsertNextCell(polyline.GetCellType(), polyline.GetPointIds()) return ug
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 getTrisBoundInSelectedVols(target_list): """ Return triangles bound in selected CUBIT volumes. Parameters: * target_list List of indices of target triangles Return: List of indices of triangles bound by the volumes """ group_id = cubit.create_new_group() cubit.silent_cmd("group %i add selection" % (group_id)) volume_ids = cubit.get_group_volumes(group_id) in_list = [] for t in target_list: verts = cubit.get_connectivity("tri", t) with_in = True cords = [] for v in verts: c = cubit.get_nodal_coordinates(v) cords.append(c) for vol_id in volume_ids: volume = cubit.volume(vol_id) body = volume.bodies()[0] within = True for cord in cords: status = body.point_containment(cord) if status == 0: within = False break if within: in_list.append(t) break cubit.delete_group(group_id) return in_list
def getTrisOverlapSelectedVols(target_list): """ Return triangles overlap with mouse selected CUBIT volumes. Parameters: * target_list List of indices of target triangles Return: List of indices of triangles overlap with the volumes """ group_id = cubit.create_new_group() cubit.silent_cmd("group %i add selection" % (group_id)) volume_ids = cubit.get_group_volumes(group_id) in_list = [] for t in target_list: verts = cubit.get_connectivity("tri", t) cords = [] for v in verts: c = cubit.get_nodal_coordinates(v) cords.append(c) for vol_id in volume_ids: volume = cubit.volume(vol_id) body = volume.bodies()[0] within = False for cord in cords: status = body.point_containment(cord) if status == 1: within = True break if within: in_list.append(t) break cubit.delete_group(group_id) return in_list
def getNodesBoundInPassedVols(target_list, volume_ids): """ Return nodes bound in mouse selected CUBIT volumes. Parameters: * target_list List of indices of target nodes Return: List of indices of nodes bound by the volumes """ in_list = [] for v in target_list: cords = cubit.get_nodal_coordinates(v) for vol_id in volume_ids: volume = cubit.volume(vol_id) body = volume.bodies()[0] status = body.point_containment(cords) if status == 1 or status == 2: in_list.append(v) break return in_list
def get_z(node): x, y, z = cubit.get_nodal_coordinates(node) return z
def get_ordered_node_surf(lsurface, icurve): if not isinstance(lsurface, str): lsurf = list2str(lsurface) # if not isinstance(icurve, str): icurvestr = str(icurve) # initializes orient_nodes_surf = [] nodes_curve = [] # #get the nodes on a surface, I don't use the method get_surface_nodes since it has different behavior in cubit12.2 and cubit13.2+ k = cubit.get_id_from_name('sl') if k != 0: cubit.cmd('del group sl') else: print 'initializing group sl' cubit.cmd("group 'sl' add node in surf " + lsurf) group1 = cubit.get_id_from_name("sl") nodes_ls = list(cubit.get_group_nodes(group1)) nnode = len(nodes_ls) # #get the nodes on curves orient = [] k = cubit.get_id_from_name('n1') if k != 0: cubit.cmd('del group n1') else: print 'initializing group n1' cubit.cmd("group 'n1' add node in curve " + icurvestr) x = cubit.get_bounding_box('curve', icurve) # checks if anything to do if len(x) == 0: return nodes_curve, orient_nodes_surf # gets nodes if x[2] > x[5]: idx = 0 else: idx = 1 group1 = cubit.get_id_from_name("n1") nodes1 = list(cubit.get_group_nodes(group1)) for n in nodes1: v = cubit.get_nodal_coordinates(n) orient.append(v[idx]) result = zip(orient, nodes1) result.sort() nodes2 = [c[1] for c in result] for n in nodes2: try: nodes_ls.remove(n) except: pass orient_nodes_surf = orient_nodes_surf + nodes2 # while len(orient_nodes_surf) < nnode: cubit.cmd('del group n1') cubit.cmd("group 'n1' add node in edge in node " + str(nodes2).replace('[', ' ').replace(']', ' ')) group1 = cubit.get_id_from_name("n1") nodes1 = list(cubit.get_group_nodes(group1)) orient = [] nd = [] for n in nodes1: if n in nodes_ls: v = cubit.get_nodal_coordinates(n) orient.append(v[idx]) nd.append(n) result = zip(orient, nd) result.sort() nodes2 = [c[1] for c in result] for n in nodes2: try: nodes_ls.remove(n) except: pass orient_nodes_surf = orient_nodes_surf + nodes2 #get the vertical curve curve_vertical = [] for s in lsurface: lcs = cubit.get_relatives("surface", s, "curve") for l in lcs: x = cubit.get_bounding_box('curve', l) length = [(x[2], 1), (x[5], 2), (x[8], 3)] length.sort() if length[-1][1] == 3: curve_vertical.append(l) # kcurve = list2str(curve_vertical) k = cubit.get_id_from_name('curve_vertical') if k != 0: cubit.cmd('del group curve_vertical') else: print 'initializing group curve_vertical' cubit.cmd("group 'curve_vertical' add node in curve " + kcurve) group1 = cubit.get_id_from_name('curve_vertical') nodes_curve = list(cubit.get_group_nodes(group1)) for n in nodes_curve: try: orient_nodes_surf.remove(n) except: pass # return nodes_curve, orient_nodes_surf
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 get_ordered_node_surf(lsurface,icurve): if not isinstance(lsurface,str): lsurf=list2str(lsurface) # if not isinstance(icurve,str): icurvestr=str(icurve) orient_nodes_surf=[] # #get the nodes on a surface, I don't use the method get_surface_nodes since it has different behavior in cubit12.2 and cubit13.2+ k=cubit.get_id_from_name('sl') if k!=0: cubit.cmd('del group sl') else: print 'initializing group sl' cubit.cmd("group 'sl' add node in surf "+lsurf) group1 = cubit.get_id_from_name("sl") nodes_ls =list(cubit.get_group_nodes(group1)) nnode=len(nodes_ls) # #get the nodes on curves orient=[] k=cubit.get_id_from_name('n1') if k!=0: cubit.cmd('del group n1') else: print 'initializing group n1' cubit.cmd("group 'n1' add node in curve "+icurvestr) x=cubit.get_bounding_box('curve', icurve) if x[2]>x[5]: idx=0 else: idx=1 group1 = cubit.get_id_from_name("n1") nodes1 = list(cubit.get_group_nodes(group1)) for n in nodes1: v = cubit.get_nodal_coordinates(n) orient.append(v[idx]) result=zip(orient,nodes1) result.sort() nodes2=[c[1] for c in result] for n in nodes2: try: nodes_ls.remove(n) except: pass orient_nodes_surf=orient_nodes_surf+nodes2 # while len(orient_nodes_surf) < nnode: cubit.cmd('del group n1') cubit.cmd("group 'n1' add node in edge in node "+str(nodes2).replace('[',' ').replace(']',' ')) group1 = cubit.get_id_from_name("n1") nodes1 = list(cubit.get_group_nodes(group1)) orient=[] nd=[] for n in nodes1: if n in nodes_ls: v = cubit.get_nodal_coordinates(n) orient.append(v[idx]) nd.append(n) result=zip(orient,nd) result.sort() nodes2=[c[1] for c in result] for n in nodes2: try: nodes_ls.remove(n) except: pass orient_nodes_surf=orient_nodes_surf+nodes2 #get the vertical curve curve_vertical=[] for s in lsurface: lcs=cubit.get_relatives("surface",s,"curve") for l in lcs: x=cubit.get_bounding_box('curve', l) length=[(x[2],1),(x[5],2),(x[8],3)] length.sort() if length[-1][1] == 3: curve_vertical.append(l) # kcurve=list2str(curve_vertical) k=cubit.get_id_from_name('curve_vertical') if k!=0: cubit.cmd('del group curve_vertical') else: print 'initializing group curve_vertical' cubit.cmd("group 'curve_vertical' add node in curve "+kcurve) group1 = cubit.get_id_from_name('curve_vertical') nodes_curve = list(cubit.get_group_nodes(group1)) for n in nodes_curve: try: orient_nodes_surf.remove(n) except: pass # return nodes_curve,orient_nodes_surf
def check_bc(iproc, xmin, xmax, ymin, ymax, cpux, cpuy, cpuxmin, cpuxmax, cpuymin, cpuymax): """ boundary=check_bc(iproc,xmin,xmax,ymin,ymax,cpux,cpuy,cpuxmin,cpuxmax,cpuymin,cpuymax) # set the boundary condition during the collecting phase and group the nodes of the vertical surface in groups for the merging phase iproc is the value of the processor xmin,ymin,ymax,ymin are the list of iproc that have at least one absorbing boundary condition """ # absorbing_surf, abs_xmin, abs_xmax, abs_ymin, abs_ymax, top_surf, bottom_surf, surf_xmin, surf_ymin, surf_xmax, surf_ymax = define_surf( ip=iproc, cpuxmin=cpuxmin, cpuxmax=cpuxmax, cpuymin=cpuymin, cpuymax=cpuymax, cpux=cpux, cpuy=cpuy) curve_bottom_xmin, curve_bottom_ymin, curve_bottom_xmax, curve_bottom_ymax = extract_bottom_curves( surf_xmin, surf_ymin, surf_xmax, surf_ymax) print absorbing_surf, abs_xmin, abs_xmax, abs_ymin, abs_ymax, top_surf, bottom_surf, surf_xmin, surf_ymin, surf_xmax, surf_ymax # # # from sets import Set #UPGRADE.... the sets module is deprecated after python 2.6 cubit.cmd('set info off') cubit.cmd('set echo off') nodes_curve_ymax, orient_nodes_surf_ymax = get_ordered_node_surf( surf_ymax, curve_bottom_ymax) nodes_curve_xmax, orient_nodes_surf_xmax = get_ordered_node_surf( surf_xmax, curve_bottom_xmax) nodes_curve_ymin, orient_nodes_surf_ymin = get_ordered_node_surf( surf_ymin, curve_bottom_ymin) nodes_curve_xmin, orient_nodes_surf_xmin = get_ordered_node_surf( surf_xmin, curve_bottom_xmin) c_xminymin = Set(nodes_curve_xmin).intersection(nodes_curve_ymin) c_xminymax = Set(nodes_curve_xmin).intersection(nodes_curve_ymax) c_xmaxymin = Set(nodes_curve_xmax).intersection(nodes_curve_ymin) c_xmaxymax = Set(nodes_curve_xmax).intersection(nodes_curve_ymax) cubit.cmd('set info on') cubit.cmd('set echo on') # orient = [] nd = [] for n in c_xminymin: v = cubit.get_nodal_coordinates(n) orient.append(v[2]) nd.append(n) result = zip(orient, nd) result.sort() c_xminymin = [c[1] for c in result] # orient = [] nd = [] for n in c_xminymax: v = cubit.get_nodal_coordinates(n) orient.append(v[2]) nd.append(n) result = zip(orient, nd) result.sort() c_xminymax = [c[1] for c in result] # orient = [] nd = [] for n in c_xmaxymin: v = cubit.get_nodal_coordinates(n) orient.append(v[2]) nd.append(n) result = zip(orient, nd) result.sort() c_xmaxymin = [c[1] for c in result] # orient = [] nd = [] for n in c_xmaxymax: v = cubit.get_nodal_coordinates(n) orient.append(v[2]) nd.append(n) result = zip(orient, nd) result.sort() c_xmaxymax = [c[1] for c in result] # boundary = {} boundary['id'] = iproc # boundary['nodes_surf_xmin'] = orient_nodes_surf_xmin boundary['nodes_surf_xmax'] = orient_nodes_surf_xmax boundary['nodes_surf_ymin'] = orient_nodes_surf_ymin boundary['nodes_surf_ymax'] = orient_nodes_surf_ymax # boundary['node_curve_xminymin'] = c_xminymin boundary['node_curve_xminymax'] = c_xminymax boundary['node_curve_xmaxymin'] = c_xmaxymin boundary['node_curve_xmaxymax'] = c_xmaxymax #print boundary['node_curve_xminymin'] #print boundary['node_curve_xminymax'] #print boundary['node_curve_xmaxymin'] #print boundary['node_curve_xmaxymax'] entities = ['face'] # for entity in entities: if len(abs_xmin) != 0: refname = entity + '_abs_xmin' build_block_side(abs_xmin, refname, obj=entity, id_0=1003) # if len(abs_ymin) != 0: refname = entity + '_abs_ymin' build_block_side(abs_ymin, refname, obj=entity, id_0=1004) # if len(abs_xmax) != 0: refname = entity + '_abs_xmax' build_block_side(abs_xmax, refname, obj=entity, id_0=1005) # if len(abs_ymax) != 0: refname = entity + '_abs_ymax' build_block_side(abs_ymax, refname, obj=entity, id_0=1006) ## refname = entity + '_topo' block = 3 #change here..... must be 1 @@@@@@@@@ ty = None ty = cubit.get_block_element_type(block) if ty == '': pass elif ty == 'HEX8': pass else: cubit.cmd('del block ' + str(block)) build_block_side(top_surf, refname, obj=entity, id_0=1001) # refname = entity + '_bottom' block = 4 #change here..... must be 2 @@@@@@@@ ty = None ty = cubit.get_block_element_type(block) if ty == '': pass elif ty == 'HEX8': pass else: cubit.cmd('del block ' + str(block)) build_block_side(bottom_surf, refname, obj=entity, id_0=1002) # # return boundary
def check_bc(iproc,xmin,xmax,ymin,ymax,cpux,cpuy,cpuxmin,cpuxmax,cpuymin,cpuymax): """ boundary=check_bc(iproc,xmin,xmax,ymin,ymax,cpux,cpuy,cpuxmin,cpuxmax,cpuymin,cpuymax) # set the boundary condition during the collecting phase and group the nodes of the vertical surface in groups for the merging phase iproc is the value of the processor xmin,ymin,ymax,ymin are the list of iproc that have at least one absorbing boundary condition """ # absorbing_surf,abs_xmin,abs_xmax,abs_ymin,abs_ymax,top_surf,bottom_surf,surf_xmin,surf_ymin,surf_xmax,surf_ymax=define_surf(ip=iproc,cpuxmin=cpuxmin,cpuxmax=cpuxmax,cpuymin=cpuymin,cpuymax=cpuymax,cpux=cpux,cpuy=cpuy) curve_bottom_xmin,curve_bottom_ymin,curve_bottom_xmax,curve_bottom_ymax=extract_bottom_curves(surf_xmin,surf_ymin,surf_xmax,surf_ymax) print absorbing_surf,abs_xmin,abs_xmax,abs_ymin,abs_ymax,top_surf,bottom_surf,surf_xmin,surf_ymin,surf_xmax,surf_ymax # # # from sets import Set #UPGRADE.... the sets module is deprecated after python 2.6 cubit.cmd('set info off') cubit.cmd('set echo off') nodes_curve_ymax,orient_nodes_surf_ymax=get_ordered_node_surf(surf_ymax,curve_bottom_ymax) nodes_curve_xmax,orient_nodes_surf_xmax=get_ordered_node_surf(surf_xmax,curve_bottom_xmax) nodes_curve_ymin,orient_nodes_surf_ymin=get_ordered_node_surf(surf_ymin,curve_bottom_ymin) nodes_curve_xmin,orient_nodes_surf_xmin=get_ordered_node_surf(surf_xmin,curve_bottom_xmin) c_xminymin=Set(nodes_curve_xmin).intersection(nodes_curve_ymin) c_xminymax=Set(nodes_curve_xmin).intersection(nodes_curve_ymax) c_xmaxymin=Set(nodes_curve_xmax).intersection(nodes_curve_ymin) c_xmaxymax=Set(nodes_curve_xmax).intersection(nodes_curve_ymax) cubit.cmd('set info on') cubit.cmd('set echo on') # orient=[] nd=[] for n in c_xminymin: v = cubit.get_nodal_coordinates(n) orient.append(v[2]) nd.append(n) result=zip(orient,nd) result.sort() c_xminymin=[c[1] for c in result] # orient=[] nd=[] for n in c_xminymax: v = cubit.get_nodal_coordinates(n) orient.append(v[2]) nd.append(n) result=zip(orient,nd) result.sort() c_xminymax=[c[1] for c in result] # orient=[] nd=[] for n in c_xmaxymin: v = cubit.get_nodal_coordinates(n) orient.append(v[2]) nd.append(n) result=zip(orient,nd) result.sort() c_xmaxymin=[c[1] for c in result] # orient=[] nd=[] for n in c_xmaxymax: v = cubit.get_nodal_coordinates(n) orient.append(v[2]) nd.append(n) result=zip(orient,nd) result.sort() c_xmaxymax=[c[1] for c in result] # boundary={} boundary['id']=iproc # boundary['nodes_surf_xmin']=orient_nodes_surf_xmin boundary['nodes_surf_xmax']=orient_nodes_surf_xmax boundary['nodes_surf_ymin']=orient_nodes_surf_ymin boundary['nodes_surf_ymax']=orient_nodes_surf_ymax # boundary['node_curve_xminymin']=c_xminymin boundary['node_curve_xminymax']=c_xminymax boundary['node_curve_xmaxymin']=c_xmaxymin boundary['node_curve_xmaxymax']=c_xmaxymax #print boundary['node_curve_xminymin'] #print boundary['node_curve_xminymax'] #print boundary['node_curve_xmaxymin'] #print boundary['node_curve_xmaxymax'] entities=['face'] # for entity in entities: if len(abs_xmin) != 0: refname=entity+'_abs_xmin' build_block_side(abs_xmin,refname,obj=entity,id_0=1003) # if len(abs_ymin) != 0: refname=entity+'_abs_ymin' build_block_side(abs_ymin,refname,obj=entity,id_0=1004) # if len(abs_xmax) != 0: refname=entity+'_abs_xmax' build_block_side(abs_xmax,refname,obj=entity,id_0=1005) # if len(abs_ymax) != 0: refname=entity+'_abs_ymax' build_block_side(abs_ymax,refname,obj=entity,id_0=1006) ## refname=entity+'_topo' block=3 #change here..... must be 1 @@@@@@@@@ ty=None ty=cubit.get_block_element_type(block) if ty=='': pass elif ty == 'HEX8': pass else: cubit.cmd('del block '+str(block)) build_block_side(top_surf,refname,obj=entity,id_0=1001) # refname=entity+'_bottom' block=4 #change here..... must be 2 @@@@@@@@ ty=None ty=cubit.get_block_element_type(block) if ty=='': pass elif ty == 'HEX8': pass else: cubit.cmd('del block '+str(block)) build_block_side(bottom_surf,refname,obj=entity,id_0=1002) # # return boundary
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 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])
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: bc_typ[node - 1, 3] = 0
def get_z(node): x,y,z = cubit.get_nodal_coordinates(node) return z
def makeGeometry(x, y): status = 0 cubit.cmd("reset") cubit.cmd('open "circleGeom.trelis"') cubit.cmd("compress ids") num_base_vertex = len(cubit.get_entities("vertex")) x = numpy.array(x) y = numpy.array(y) target_surface = cubit.surface(1) vertex_on_surface = numpy.zeros(len(x), dtype=bool) for i in range(0, len(x)): vertex_on_surface[i] = target_surface.point_containment( [x[i], y[i], 0.]) if vertex_on_surface[i] == True: cubit.cmd("create vertex " + str(x[i]) + " " + str(y[i]) + " 0 on surface 1") nlcon = computeNonlinearConstraint(x, y) sys.stdout.write("Nonlinear Constraints: ") sys.stdout.write(str(nlcon)) sys.stdout.write("\n") sys.stdout.flush() X = x[vertex_on_surface] Y = y[vertex_on_surface] num_active_pins = len(X) if num_active_pins == 0: # No pins on board, return with nonlinear constraint values status = 1 return status, [], [], nlcon V = cubit.get_list_of_free_ref_entities("vertex") for i in range(0, len(V)): cubit.cmd("imprint volume all with vertex " + str(V[i])) cubit.cmd("delete free vertex all") cubit.cmd("compress ids") #for i in range(0,len(V)): # cubit.cmd("nodeset 1 add vertex " + str(V[i])) cubit.cmd("surface all size 0.2") cubit.cmd("mesh surf all") cubit.cmd("surface all smooth scheme mean ratio cpu 0.1") cubit.cmd("smooth surf all") cubit.cmd("compress ids") V = numpy.zeros(num_active_pins, dtype=int) #N = numpy.zeros(num_active_pins, dtype=int) bc_xyz = [[] for i in range(0, num_active_pins)] cubit.cmd('create group "cf_crease_entities"') for i in range(0, num_active_pins): bc_xyz[i] = [X[i], Y[i], 0.] N = cubit.parse_cubit_list( "node", " at " + str(X[i]) + " " + str(Y[i]) + " 0.") for n in range(0, len(N)): nodeEdges = cubit.parse_cubit_list("edge", "in node " + str(N[n])) for e in range(0, len(nodeEdges)): cubit.cmd("cf_crease_entities add Edge " + str(nodeEdges[e])) EinC = cubit.parse_cubit_list("edge", " in curve 1") #for e in range(0,len(EinC)): # cubit.cmd("cf_crease_entities add Edge " + str(EinC[e])) VinC = cubit.parse_cubit_list("node", " in curve 1") for n in range(0, len(VinC)): nxyz = cubit.get_nodal_coordinates(VinC[n]) bc_xyz.append(list(nxyz)) N = cubit.parse_cubit_list( "node", " at " + str(nxyz[0]) + " " + str(nxyz[1]) + " 0.") for n in range(0, len(N)): nodeEdges = cubit.parse_cubit_list("edge", "in node " + str(N[n])) for e in range(0, len(nodeEdges)): if nodeEdges[e] in EinC: pass else: cubit.cmd("cf_crease_entities add Edge " + str(nodeEdges[e])) #V = cubit.get_entities("vertex")[num_base_vertex:] #cubit.cmd('create group "cf_crease_entities"') #bc_xyz = [[] for i in range(0,len(V))] #for i in range(0,len(V)): # #ssID = cubit.get_next_sideset_id() # bc_xyz[i] = list(cubit.vertex(V[i]).coordinates()) # cubit.parse_cubit_list("vertex", "at " + str(bc_) # N = cubit.get_vertex_node(V[i]) # nodeEdges = cubit.parse_cubit_list('edge','in node ' + str(N)) # for e in range(0,len(nodeEdges)): # #cubit.cmd("sideset " + str(ssID) + " add Edge " + str(nodeEdges[e])) # cubit.cmd("cf_crease_entities add Edge " + str(nodeEdges[e])) # #cubit.cmd("sideset " + str(ssID) + ' name "node_' + str(N) + '_edges"') cubit.cmd('save as "mesh.cub" overwrite') num_elem = len(cubit.get_entities("Face")) return status, bc_xyz, num_elem, nlcon