def create_panel(Lx, Ly, use_hole=True): ''' Create a panel with a whole in it ''' # Set the number of load cases nu = 2 nv = 2 x = np.linspace(-0.5 * Lx, 0.5 * Lx, nu) y = np.linspace(-0.5 * Ly, 0.5 * Ly, nv) pts = np.zeros((nu, nv, 3)) for j in range(nv): for i in range(nu): pts[i, j, 0] = x[i] pts[i, j, 1] = y[j] # Create the b-spline surface surf = TMR.BsplineSurface(pts) face = TMR.FaceFromSurface(surf) r = 0.2 c = 0.5 v1 = TMR.VertexFromFace(face, 0.0, 0.0) v2 = TMR.VertexFromFace(face, 1.0, 0.0) v3 = TMR.VertexFromFace(face, 1.0, 1.0) v4 = TMR.VertexFromFace(face, 0.0, 1.0) v5 = TMR.VertexFromFace(face, c - r, c) # Set up the first edge pcurve1 = TMR.BsplinePcurve(np.array([[0.0, 0.0], [1.0, 0.0]])) edge1 = TMR.EdgeFromFace(face, pcurve1) edge1.setVertices(v1, v2) edge1.setAttribute('y-') # Set up the first edge pcurve2 = TMR.BsplinePcurve(np.array([[1.0, 0.0], [1.0, 1.0]])) edge2 = TMR.EdgeFromFace(face, pcurve2) edge2.setVertices(v2, v3) edge2.setAttribute('x+') # Set up the first edge pcurve3 = TMR.BsplinePcurve(np.array([[1.0, 1.0], [0.0, 1.0]])) edge3 = TMR.EdgeFromFace(face, pcurve3) edge3.setVertices(v3, v4) edge3.setAttribute('y+') # Set up the first edge pcurve4 = TMR.BsplinePcurve(np.array([[0.0, 1.0], [0.0, 0.0]])) edge4 = TMR.EdgeFromFace(face, pcurve4) edge4.setVertices(v4, v1) edge4.setAttribute('x-') # Create the inner edge loop # (c-r, c+r) -- (c, c+r) -- (c+r, c+r) # | | # | | # (c-r, c) (c+r, c) # | | # | | # (c-r, c-r) -- (c, c-r) -- (c+r, c-r) pts = [[c - r, c], [c - r, c + r], [c, c + r], [c + r, c + r], [c + r, c], [c + r, c - r], [c, c - r], [c - r, c - r], [c - r, c]] wts = [ 1.0, 1.0 / np.sqrt(2), 1.0, 1.0 / np.sqrt(2), 1.0, 1.0 / np.sqrt(2), 1.0, 1.0 / np.sqrt(2), 1.0 ] Tu = [0.0, 0.0, 0.0, 0.25, 0.25, 0.5, 0.5, 0.75, 0.75, 1.0, 1.0, 1.0] pcurve5 = TMR.BsplinePcurve(np.array(pts), tu=np.array(Tu), wts=np.array(wts), k=3) edge5 = TMR.EdgeFromFace(face, pcurve5) edge5.setVertices(v5, v5) # Create the loop dirs = [1, 1, 1, 1] loop = TMR.EdgeLoop([edge1, edge2, edge3, edge4], dirs) face.addEdgeLoop(loop) if use_hole: # Create the second edge loop loop = TMR.EdgeLoop([edge5], [1]) face.addEdgeLoop(loop) verts = [v1, v2, v3, v4, v5] edges = [edge1, edge2, edge3, edge4, edge5] else: verts = [v1, v2, v3, v4] edges = [edge1, edge2, edge3, edge4] faces = [face] # Create the TMRModel geo = TMR.Model(verts, edges, faces) return geo
edges = [] faces = [] vols = [] # sfi = [0, 4] for i, geo in enumerate([geo1, geo2]): vols = geo.getVolumes() fail = vols[0].setExtrudeFaces() if fail: print("setExtrudeFaces failed for volume {}".format(i)) verts.extend(geo.getVertices()) edges.extend(geo.getEdges()) faces.extend(geo.getFaces()) vols.extend(geo.getVolumes()) geo = TMR.Model(verts, edges, faces) # Create the new mesh mesh = TMR.Mesh(comm, geo) # Set the meshing options opts = TMR.MeshOptions() opts.write_mesh_quality_histogram = 1 opts.triangularize_print_iter = 50000 # Create the surface mesh mesh.mesh(htarget, opts) # Write the surface mesh to a file mesh.writeToVTK('output.vtk', 'quad')
def load_model(): geo = TMR.LoadModel('model.step') # Get the faces/volume from the model faces = geo.getFaces() edges = geo.getEdges() verts = geo.getVertices() # Create the edge loops elist = [edges[3], edges[5], edges[45], edges[32]] ex, dx, vx = get_edge_dirs_verts(elist) elist = [edges[45], edges[6], edges[7], edges[51]] ey, dy, vy = get_edge_dirs_verts(elist) elist = [edges[2], edges[32], edges[51], edges[8]] ez, dz, vz = get_edge_dirs_verts(elist) # Create the faces fx = TMR.TFIFace(ex, dx, vx) fy = TMR.TFIFace(ey, dy, vy) fz = TMR.TFIFace(ez, dz, vz) faces.extend([fx, fy, fz]) # Make the volumes s1 = [fx, faces[3], faces[19], faces[6], faces[10], faces[9], faces[11], faces[12]] d1 = [1, -1, -1, -1, 1, 1, -1, -1] s2 = [fy, faces[16], faces[8], faces[5], faces[23], faces[15], faces[17], faces[18]] d2 = [-1, 1, -1, -1, 1, 1, -1, -1] s3 = [fz, faces[4], faces[20], faces[13], faces[7], faces[14], faces[21], faces[22]] d3 = [1, -1, 1, 1, 1, 1, -1, -1] s4 = [fx, fy, fz, faces[0], faces[1], faces[2]] d4 = [-1, 1, -1, -1, -1, -1] # Set the names faces[11].setName('fx') faces[12].setName('fx') faces[17].setName('fy') faces[18].setName('fy') faces[21].setName('fz') faces[22].setName('fz') # Form the 4 independent bodies that are connected through v1 = TMR.Volume(s1, d1) v2 = TMR.Volume(s2, d2) v3 = TMR.Volume(s3, d3) v4 = TMR.Volume(s4, d4) vols = [v1, v2, v3, v4] # Set the source/destination faces faces[19].setSource(v1, faces[3]) faces[5].setSource(v2, faces[23]) faces[7].setSource(v3, faces[13]) faces[1].setSource(v4, fz) # Create a new model geo = TMR.Model(verts, edges, faces, vols) return geo
ksweight = args.ksweight # Set the number of AMR steps to use steps = args.steps # Store whether to use a structured/unstructed mesh structured = args.structured # Load the geometry model geo = TMR.LoadModel('cylinder.stp') verts = geo.getVertices() edges = geo.getEdges() faces = geo.getFaces() # Create the simplified geometry with only faces geo = TMR.Model(verts, edges, [faces[0]]) # Set the boundary conditions verts[0].setAttribute('Clamped') verts[1].setAttribute('Restrained') edges[0].setAttribute('Restrained') edges[2].setAttribute('Restrained') # Create the new mesh mesh = TMR.Mesh(comm, geo) # Set the meshing options opts = TMR.MeshOptions() # Set the mesh type opts.mesh_type_default = TMR.UNSTRUCTURED
filename = args.filename if not os.path.isfile(filename): raise ValueError('File %s does not exist' % (filename)) # Set the value of the target length scale in the mesh htarget = args.htarget # Load the geometry model geo = TMR.LoadModel(filename) geo.writeModelToTecplot('tecplot_surfaces.dat') # Create a model by discarding the volumes verts = geo.getVertices() edges = geo.getEdges() faces = geo.getFaces() geo_new = TMR.Model(verts, edges, faces) # Create the new mesh mesh = TMR.Mesh(comm, geo_new) # Set the meshing options opts = TMR.MeshOptions() opts.frontal_quality_factor = 1.25 opts.num_smoothing_steps = 10 opts.write_mesh_quality_histogram = 1 opts.triangularize_print_iter = 50000 # Create the surface mesh mesh.mesh(htarget, opts) # Write the surface mesh to a file
v4 = TMR.VertexFromEdge(curves[3], 0.0) vertices = [v1, v2, v3, v4] # Set the vertices in the curves curves[0].setVertices(v1, v2) curves[1].setVertices(v2, v3) curves[2].setVertices(v3, v4) curves[3].setVertices(v4, v1) # Set the curve segments around the surface direct = [1, 1, 1, 1] loop = TMR.EdgeLoop(curves, direct) face.addEdgeLoop(1, loop) # Create the model object geo = TMR.Model(vertices, curves, faces) # Mesh the geometry comm = MPI.COMM_WORLD mesh = TMR.Mesh(comm, geo) hval = 0.1 mesh.mesh(hval) # Extract the quadrilaterals/points pts = mesh.getMeshPoints() quads = mesh.getQuadConnectivity() npts = pts.shape[0] nquads = quads.shape[0] # Flip the quadrilateral ordering to for i in range(nquads):
def create_3d_unstruct_mesh(n, AR, prob, ratio1, ratio2, hole_r, forced_portion, force_magnitude, MBB_bc_portion, loaded_thickness, ly): comm = MPI.COMM_WORLD # Create the egads context ctx = egads.context() # dimensions Lx = ly * AR Ly = ly h = ly parts = [] if prob == 'lbracket': # Create base x0 = [0, 0, 0] x1 = [Lx * ratio1, Ly * ratio2, h] B0 = ctx.makeSolidBody(egads.BOX, rdata=[x0, x1]) parts.append(ctx.makeTopology(egads.MODEL, children=[B0])) # Create arm 1 x0 = [0, Ly * ratio2, 0] x1 = [Lx * ratio1, Ly * (1 - ratio2), h] B1 = ctx.makeSolidBody(egads.BOX, rdata=[x0, x1]) parts.append(ctx.makeTopology(egads.MODEL, children=[B1])) # Create arm 2 x0 = [Lx * ratio1, 0, 0] x1 = [Lx * (1 - ratio1), Ly * ratio2, h] B2 = ctx.makeSolidBody(egads.BOX, rdata=[x0, x1]) # Create the cylinder cutout xc = Lx * (ratio1 + 1) * 0.5 yc = Ly * ratio2 * 0.5 x0 = [xc, yc, 0] x1 = [xc, yc, h] r = hole_r * Ly * ratio2 C1 = ctx.makeSolidBody(egads.CYLINDER, rdata=[x0, x1, r]) parts.append(B2.solidBoolean(C1, egads.SUBTRACTION)) else: # Create the box x0 = [0, 0, 0] x1 = [Lx, Ly, h] B1 = ctx.makeSolidBody(egads.BOX, rdata=[x0, x1]) # Create the cylinder cutout xc = Lx / 2 yc = Ly / 2 x0 = [xc, yc, 0] x1 = [xc, yc, h] r = hole_r * Ly C1 = ctx.makeSolidBody(egads.CYLINDER, rdata=[x0, x1, r]) # parts.append(ctx.makeTopology(egads.MODEL, children=[B1])) parts.append(B1.solidBoolean(C1, egads.SUBTRACTION)) # Create all of the models geos = [] for p in parts: geos.append(TMR.ConvertEGADSModel(p)) # Create the full list of vertices, edges, faces and volumes verts = [] edges = [] faces = [] vols = [] for geo in geos: verts.extend(geo.getVertices()) edges.extend(geo.getEdges()) faces.extend(geo.getFaces()) vols.extend(geo.getVolumes()) # Set all of the matching faces TMR.setMatchingFaces(geos) # Create the geometry geo = TMR.Model(verts, edges, faces, vols) # Create the new mesh mesh = TMR.Mesh(comm, geo) # Set the meshing options opts = TMR.MeshOptions() # opts.mesh_type_default = TMR.UNSTRUCTURED opts.write_mesh_quality_histogram = 1 opts.triangularize_print_iter = 50000 # Create the surface mesh htarget = ly / n mesh.mesh(htarget, opts) # Write the surface mesh to a file # mesh.writeToVTK('block.vtk', 'hex') # Create the model from the unstructured volume mesh model = mesh.createModelFromMesh() # Create the corresponding mesh topology from the mesh-model topo = TMR.Topology(comm, model) # Create the quad forest and set the topology of the forest forest = TMR.OctForest(comm) forest.setTopology(topo) forest.createTrees() forest.balance(1) # Create the nodes forest.createNodes() # Get the mesh connectivity conn = forest.getMeshConn() nnodes = np.max(conn) + 1 nelems = len(conn) # Get the node locations X = forest.getPoints() # Set boundary conditions dof = -np.ones((nnodes, 3), dtype=int) geo_bc_tol = 1e-6 if prob == 'cantilever' or prob == 'michell': bc_x_max = geo_bc_tol bc_x_min = -geo_bc_tol bc_y_max = Ly + geo_bc_tol bc_y_min = -geo_bc_tol bc_z_max = h + geo_bc_tol bc_z_min = -geo_bc_tol ndof = 0 for i in range(nnodes): if (bc_x_min <= X[i, 0] <= bc_x_max and bc_y_min <= X[i, 1] <= bc_y_max and bc_z_min <= X[i, 2] <= bc_z_max): # This is the bc node pass else: dof[i, 0] = ndof ndof += 1 dof[i, 1] = ndof ndof += 1 dof[i, 2] = ndof ndof += 1 elif prob == 'MBB': bc1_x_max = geo_bc_tol bc1_x_min = -geo_bc_tol bc1_y_max = Ly + geo_bc_tol bc1_y_min = -geo_bc_tol bc1_z_max = h + geo_bc_tol bc1_z_min = -geo_bc_tol bc2_x_max = Lx + geo_bc_tol bc2_x_min = Lx * (1 - MBB_bc_portion) - geo_bc_tol bc2_y_max = geo_bc_tol + geo_bc_tol bc2_y_min = -geo_bc_tol bc2_z_max = h + geo_bc_tol bc2_z_min = -geo_bc_tol ndof = 0 for i in range(nnodes): if (bc1_x_min <= X[i, 0] <= bc1_x_max and bc1_y_min <= X[i, 1] <= bc1_y_max and bc1_z_min <= X[i, 2] <= bc1_z_max): # This is the bc node dof[i, 1] = ndof ndof += 1 elif (bc2_x_min <= X[i, 0] <= bc2_x_max and bc2_y_min <= X[i, 1] <= bc2_y_max and bc2_z_min <= X[i, 2] <= bc2_z_max): # This is also bc node dof[i, 0] = ndof ndof += 1 else: dof[i, 0] = ndof ndof += 1 dof[i, 1] = ndof ndof += 1 dof[i, 2] = ndof ndof += 1 elif prob == 'lbracket': bc_x_max = Lx * ratio1 + geo_bc_tol bc_x_min = -geo_bc_tol bc_y_max = Ly + geo_bc_tol bc_y_min = Ly - geo_bc_tol bc_z_max = h + geo_bc_tol bc_z_min = -geo_bc_tol ndof = 0 for i in range(nnodes): if (bc_x_min <= X[i, 0] <= bc_x_max and bc_y_min <= X[i, 1] <= bc_y_max and bc_z_min <= X[i, 2] <= bc_z_max): # This is the bc node pass else: dof[i, 0] = ndof ndof += 1 dof[i, 1] = ndof ndof += 1 dof[i, 2] = ndof ndof += 1 # Set loading force = np.zeros(ndof) geo_tol = 1e-6 if prob == 'cantilever': load_x_max = Lx + geo_tol load_x_min = Lx - geo_tol load_y_max = Ly * forced_portion + geo_tol load_y_min = -geo_tol load_z_max = 0.5 * h * (1 + loaded_thickness) load_z_min = 0.5 * h * (1 - loaded_thickness) force_dir = 1 force_scal = -1.0 elif prob == 'MBB': load_x_max = Lx * MBB_bc_portion + geo_tol load_x_min = -geo_tol load_y_max = Ly + geo_tol load_y_min = Ly - geo_tol load_z_max = 0.5 * h * (1 + loaded_thickness) + geo_tol load_z_min = 0.5 * h * (1 - loaded_thickness) - geo_tol force_dir = 1 force_scal = -1.0 elif prob == 'michell': load_x_max = Lx + geo_tol load_x_min = Lx - geo_tol load_y_max = 0.5 * Ly * (1 + forced_portion) + geo_tol load_y_min = 0.5 * Ly * (1 - forced_portion) - geo_tol load_z_max = 0.5 * h * (1 + loaded_thickness) + geo_tol load_z_min = 0.5 * h * (1 - loaded_thickness) - geo_tol force_dir = 1 force_scal = -1.0 elif prob == 'lbracket': load_x_max = Lx + geo_tol load_x_min = Lx - geo_tol load_y_max = Ly * ratio2 + geo_tol load_y_min = Ly * ratio2 * (1 - forced_portion) - geo_tol load_z_max = 0.5 * h * (1 + loaded_thickness) + geo_tol load_z_min = 0.5 * h * (1 - loaded_thickness) - geo_tol force_dir = 1 force_scal = -1.0 nforce = 0 for i in range(nnodes): if (load_x_min <= X[i, 0] <= load_x_max and load_y_min <= X[i, 1] <= load_y_max and load_z_min <= X[i, 2] <= load_z_max): force[dof[i, force_dir]] = force_scal * force_magnitude nforce += 1 force /= nforce # for n in range(X.shape[0]): # print('node:{:4d} x:{:6f} y:{:6f} z:{:6f}'.format(n, X[n, 0], X[n, 1], X[n, 2])) # for ne in range(conn.shape[0]): # print('elem:{:4d} conn = [{:3d} {:3d} {:3d} {:3d}]'.format(ne, conn[ne,0], conn[ne,1], conn[ne,2], conn[ne,3])) return nelems, nnodes, ndof, conn, X, dof, force
def create_geo(AR, prob, forced_portion, MBB_bc_portion, ratio1, ratio2, use_hole, hole_radius): # Create the surface in the x-y plane Ly = 1.0 Lx = Ly*AR nu = 2 nv = 2 x = np.linspace(0.0, Lx, nu) y = np.linspace(0.0, Ly, nv) pts = np.zeros((nu, nv, 3)) for j in range(nv): for i in range(nu): pts[i,j,0] = x[i] pts[i,j,1] = y[j] tu = np.array([0.0, 0.0, Lx, Lx]) tv = np.array([0.0, 0.0, Ly, Ly]) # Create the b-spline surface surf = TMR.BsplineSurface(pts, tu=tu, tv=tv) face = TMR.FaceFromSurface(surf) if prob == 'cantilever': # Create the vertices on the surface v1 = TMR.VertexFromFace(face, 0.0, 0.0) v2 = TMR.VertexFromFace(face, Lx, 0.0) v3 = TMR.VertexFromFace(face, Lx, forced_portion*Ly) v4 = TMR.VertexFromFace(face, Lx, Ly) v5 = TMR.VertexFromFace(face, 0.0, Ly) verts = [v1, v2, v3, v4, v5] # Set up the edges pcurve1 = TMR.BsplinePcurve(np.array([[0.0, 0.0], [Lx, 0.0]])) pcurve2 = TMR.BsplinePcurve(np.array([[Lx, 0.0], [Lx, forced_portion*Ly]])) pcurve3 = TMR.BsplinePcurve(np.array([[Lx, forced_portion*Ly], [Lx, Ly]])) pcurve4 = TMR.BsplinePcurve(np.array([[Lx, Ly], [0.0, Ly]])) pcurve5 = TMR.BsplinePcurve(np.array([[0.0, Ly], [0.0, 0.0]])) edge1 = TMR.EdgeFromFace(face, pcurve1) edge2 = TMR.EdgeFromFace(face, pcurve2) edge3 = TMR.EdgeFromFace(face, pcurve3) edge4 = TMR.EdgeFromFace(face, pcurve4) edge5 = TMR.EdgeFromFace(face, pcurve5) edge1.setVertices(v1, v2) edge2.setVertices(v2, v3) edge3.setVertices(v3, v4) edge4.setVertices(v4, v5) edge5.setVertices(v5, v1) edge1.setName('1') edge2.setName('2') edge3.setName('3') edge4.setName('4') edge5.setName('5') edges = [edge1, edge2, edge3, edge4, edge5] dirs = [1, 1, 1, 1, 1] loop = TMR.EdgeLoop([edge1, edge2, edge3, edge4, edge5], dirs) face.addEdgeLoop(1, loop) elif prob == 'michell': # Create the vertices on the surface v1 = TMR.VertexFromFace(face, 0.0, 0.0) v2 = TMR.VertexFromFace(face, Lx, 0.0) v3 = TMR.VertexFromFace(face, Lx, 0.5*(1-forced_portion)*Ly) v4 = TMR.VertexFromFace(face, Lx, 0.5*(1+forced_portion)*Ly) v5 = TMR.VertexFromFace(face, Lx, Ly) v6 = TMR.VertexFromFace(face, 0.0, Ly) verts = [v1, v2, v3, v4, v5, v6] # Set up the edges pcurve1 = TMR.BsplinePcurve(np.array([[0.0, 0.0], [Lx, 0.0]])) pcurve2 = TMR.BsplinePcurve(np.array([[Lx, 0.0], [Lx, 0.5*(1-forced_portion)*Ly]])) pcurve3 = TMR.BsplinePcurve(np.array([[Lx, 0.5*(1-forced_portion)*Ly], [Lx, 0.5*(1+forced_portion)*Ly]])) pcurve4 = TMR.BsplinePcurve(np.array([[Lx, 0.5*(1+forced_portion)*Ly], [Lx, Ly]])) pcurve5 = TMR.BsplinePcurve(np.array([[Lx, Ly], [0.0, Ly]])) pcurve6 = TMR.BsplinePcurve(np.array([[0.0, Ly], [0.0, 0.0]])) edge1 = TMR.EdgeFromFace(face, pcurve1) edge2 = TMR.EdgeFromFace(face, pcurve2) edge3 = TMR.EdgeFromFace(face, pcurve3) edge4 = TMR.EdgeFromFace(face, pcurve4) edge5 = TMR.EdgeFromFace(face, pcurve5) edge6 = TMR.EdgeFromFace(face, pcurve6) edge1.setVertices(v1, v2) edge2.setVertices(v2, v3) edge3.setVertices(v3, v4) edge4.setVertices(v4, v5) edge5.setVertices(v5, v6) edge6.setVertices(v6, v1) edge1.setName('1') edge2.setName('2') edge3.setName('3') edge4.setName('4') edge5.setName('5') edge6.setName('6') edges = [edge1, edge2, edge3, edge4, edge5, edge6] dirs = [1, 1, 1, 1, 1, 1] loop = TMR.EdgeLoop([edge1, edge2, edge3, edge4, edge5, edge6], dirs) face.addEdgeLoop(1, loop) elif prob == 'MBB': # Create the vertices on the surface v1 = TMR.VertexFromFace(face, 0.0, 0.0) v2 = TMR.VertexFromFace(face, Lx*(1-MBB_bc_portion), 0.0) v3 = TMR.VertexFromFace(face, Lx, 0.0) v4 = TMR.VertexFromFace(face, Lx, Ly) v5 = TMR.VertexFromFace(face, Lx*forced_portion, Ly) v6 = TMR.VertexFromFace(face, 0.0, Ly) verts = [v1, v2, v3, v4, v5, v6] # Set up the edges pcurve1 = TMR.BsplinePcurve(np.array([[0.0, 0.0], [Lx*(1-MBB_bc_portion), 0.0]])) pcurve2 = TMR.BsplinePcurve(np.array([[Lx*(1-MBB_bc_portion), 0.0], [Lx, 0.0]])) pcurve3 = TMR.BsplinePcurve(np.array([[Lx, 0.0], [Lx, Ly]])) pcurve4 = TMR.BsplinePcurve(np.array([[Lx, Ly], [Lx*forced_portion, Ly]])) pcurve5 = TMR.BsplinePcurve(np.array([[Lx*forced_portion, Ly], [0.0, Ly]])) pcurve6 = TMR.BsplinePcurve(np.array([[0.0, Ly], [0.0, 0.0]])) edge1 = TMR.EdgeFromFace(face, pcurve1) edge2 = TMR.EdgeFromFace(face, pcurve2) edge3 = TMR.EdgeFromFace(face, pcurve3) edge4 = TMR.EdgeFromFace(face, pcurve4) edge5 = TMR.EdgeFromFace(face, pcurve5) edge6 = TMR.EdgeFromFace(face, pcurve6) edge1.setVertices(v1, v2) edge2.setVertices(v2, v3) edge3.setVertices(v3, v4) edge4.setVertices(v4, v5) edge5.setVertices(v5, v6) edge6.setVertices(v6, v1) edge1.setName('1') edge2.setName('2') edge3.setName('3') edge4.setName('4') edge5.setName('5') edge6.setName('6') edges = [edge1, edge2, edge3, edge4, edge5, edge6] dirs = [1, 1, 1, 1, 1, 1] loop = TMR.EdgeLoop([edge1, edge2, edge3, edge4, edge5, edge6], dirs) face.addEdgeLoop(1, loop) elif prob == 'lbracket': # Create the vertices on the surface v1 = TMR.VertexFromFace(face, 0.0, 0.0) v2 = TMR.VertexFromFace(face, Lx, 0.0) v3 = TMR.VertexFromFace(face, Lx, Ly*ratio2*(1-forced_portion)) v4 = TMR.VertexFromFace(face, Lx, Ly*ratio2) v5 = TMR.VertexFromFace(face, Lx*ratio1, Ly*ratio2) v6 = TMR.VertexFromFace(face, Lx*ratio1, Ly) v7 = TMR.VertexFromFace(face, 0.0, Ly) verts = [v1, v2, v3, v4, v5, v6, v7] # Set up the edges pcurve1 = TMR.BsplinePcurve(np.array([[0.0, 0.0], [Lx, 0.0]])) pcurve2 = TMR.BsplinePcurve(np.array([[Lx, 0.0], [Lx, Ly*ratio2*(1-forced_portion)]])) pcurve3 = TMR.BsplinePcurve(np.array([[Lx, Ly*ratio2*(1-forced_portion)], [Lx, Ly*ratio2]])) pcurve4 = TMR.BsplinePcurve(np.array([[Lx, Ly*ratio2], [Lx*ratio1, Ly*ratio2]])) pcurve5 = TMR.BsplinePcurve(np.array([[Lx*ratio1, Ly*ratio2], [Lx*ratio1, Ly]])) pcurve6 = TMR.BsplinePcurve(np.array([[Lx*ratio1, Ly], [0.0, Ly]])) pcurve7 = TMR.BsplinePcurve(np.array([[0.0, Ly], [0.0, 0.0]])) edge1 = TMR.EdgeFromFace(face, pcurve1) edge2 = TMR.EdgeFromFace(face, pcurve2) edge3 = TMR.EdgeFromFace(face, pcurve3) edge4 = TMR.EdgeFromFace(face, pcurve4) edge5 = TMR.EdgeFromFace(face, pcurve5) edge6 = TMR.EdgeFromFace(face, pcurve6) edge7 = TMR.EdgeFromFace(face, pcurve7) edge1.setVertices(v1, v2) edge2.setVertices(v2, v3) edge3.setVertices(v3, v4) edge4.setVertices(v4, v5) edge5.setVertices(v5, v6) edge6.setVertices(v6, v7) edge7.setVertices(v7, v1) edge1.setName('1') edge2.setName('2') edge3.setName('3') edge4.setName('4') edge5.setName('5') edge6.setName('6') edge7.setName('7') edges = [edge1, edge2, edge3, edge4, edge5, edge6, edge7] dirs = [1, 1, 1, 1, 1, 1, 1] loop = TMR.EdgeLoop([edge1, edge2, edge3, edge4, edge5, edge6, edge7], dirs) face.addEdgeLoop(1, loop) # Set up the hole if use_hole: if prob == 'lbracket': r = hole_radius*Ly*ratio2 xc = 0.5*(1+ratio1)*Lx yc = 0.5*Ly*ratio2 else: r = hole_radius *Ly xc = 0.5*Lx yc = 0.5*Ly vc = TMR.VertexFromFace(face, xc - r, yc) pts = [[-r, 0.0], [-r, r], [0.0, r], [r, r], [r, 0.0], [r, -r], [0.0, -r], [-r, -r], [-r, 0.0]] pts = np.array(pts) for i in range(pts.shape[0]): pts[i,0] += xc pts[i,1] += yc wts = [1.0, 1.0/np.sqrt(2), 1.0, 1.0/np.sqrt(2), 1.0, 1.0/np.sqrt(2), 1.0, 1.0/np.sqrt(2), 1.0] Tu = [0.0, 0.0, 0.0, 0.25, 0.25, 0.5, 0.5, 0.75, 0.75, 1.0, 1.0, 1.0] pcurvec = TMR.BsplinePcurve(np.array(pts), tu=np.array(Tu), wts=np.array(wts), k=3) edgec = TMR.EdgeFromFace(face, pcurvec) edgec.setVertices(vc, vc) loop = TMR.EdgeLoop([edgec], [1]) face.addEdgeLoop(-1, loop) verts.append(vc) edges.append(edgec) # Create the TMRModel faces = [face] geo = TMR.Model(verts, edges, faces) return geo
def geomach_to_tmr(bse): ''' Convert a BSE-GeoMach model to a TMR model ''' # Compute the number of vertices, edges and faces num = bse._num nverts = num['vert'] nedges = num['edge'] nfaces = num['surf'] # Create the list of edges, faces and verts = nverts * [None] edges = nedges * [None] faces = nfaces * [None] surfs = nfaces * [None] # Set the topology object topo = bse._topo size = bse._size str_indices = bse._str_indices bspline = bse._bspline # Extract the control point array cp_str = bse.vec['cp_str'].array # Point from the edge index on each face to the i/j locations # in the surf_ptrs array face_to_edge = [[1, 0], [2, 1], [1, 2], [0, 1]] # Point from the edge index on each face to the corresponding # vertices on each face face_to_edge_verts = [[[0, 0], [2, 0]], [[2, 0], [2, 2]], [[0, 2], [2, 2]], [[0, 0], [0, 2]]] # Create the surfaces surf_ptrs = topo['surf_ptrs'] edge_ptrs = topo['edge_ptrs'] for i in range(nfaces): cp_offset = str_indices['cp'][i, 0] ku = bspline['order'][topo['surf_group'][i, 0] - 1] kv = bspline['order'][topo['surf_group'][i, 1] - 1] nu = bspline['num_cp'][topo['surf_group'][i, 0] - 1] nv = bspline['num_cp'][topo['surf_group'][i, 1] - 1] # Extract and create the b-spline surfaces cp = np.zeros((nu, nv, 3), dtype=np.double) for jj in range(nv): for ii in range(nu): cp_index = cp_offset + ii + jj * nu cp[ii, jj, :] = cp_str[cp_index] surfs[i] = TMR.BsplineSurface(cp, ku=ku, kv=kv) faces[i] = TMR.FaceFromSurface(surfs[i]) # Create the vertices from the faces for i in range(nfaces): for jj in range(2): for ii in range(2): # Get the vertex index index = surf_ptrs[i, 2 * ii, 2 * jj] - 1 # If the vertex has not be allocated, create it now if verts[index] is None: u = 1.0 * ii v = 1.0 * jj verts[index] = TMR.VertexFromFace(faces[i], u, v) for i in range(nverts): if verts[i] is None: raise ValueError('TMRVertex %d was not initialized\n' % (i)) # Create the edges for i in range(nfaces): for ii in range(4): i1 = face_to_edge[ii][0] j1 = face_to_edge[ii][1] # Keep track of the direction edge_num = surf_ptrs[i, i1, j1] index = abs(edge_num) - 1 # Find the vertex numbers v1 = edge_ptrs[index, 0] - 1 v2 = edge_ptrs[index, 1] - 1 # The start/end vertex location vert1 = None vert2 = None # Get the indices of the vertices within the surf_ptrs array i1 = face_to_edge_verts[ii][0][0] j1 = face_to_edge_verts[ii][0][1] i2 = face_to_edge_verts[ii][1][0] j2 = face_to_edge_verts[ii][1][1] if (v1 == surf_ptrs[i, i1, j1] - 1 and v2 == surf_ptrs[i, i2, j2] - 1): vert1 = verts[v1] vert2 = verts[v2] pts = np.array( [face_to_edge_verts[ii][0], face_to_edge_verts[ii][1]], dtype=np.double) elif (v2 == surf_ptrs[i, i1, j1] - 1 and v1 == surf_ptrs[i, i2, j2] - 1): vert1 = verts[v2] vert2 = verts[v1] pts = np.array( [face_to_edge_verts[ii][1], face_to_edge_verts[ii][0]], dtype=np.double) pts = pts / 2.0 # Check whether this is a degenerate edge is_degen = 0 if v1 == v2: is_degen = 1 # Create the parametric curve pcurve = TMR.BsplinePcurve(pts) if edges[index] is None: edges[index] = TMR.EdgeFromFace(faces[i], pcurve, is_degen) edges[index].setVertices(vert1, vert2) else: edges[index].addEdgeFromFace(faces[i], pcurve) for i in range(nedges): if edges[i] is None: raise ValueError('TMREdge %d was not initialized\n' % (i)) # After all of the edges are created, create the edge loops # for each face. Account for the CCW orientation. ccw_order = [1, 1, -1, -1] for i in range(nfaces): # Find the edges and directions for this edge loop e = [] dirs = [] for ii in range(4): i1 = face_to_edge[ii][0] j1 = face_to_edge[ii][1] edge_num = surf_ptrs[i, i1, j1] e.append(edges[abs(edge_num) - 1]) dirs.append(np.sign(edge_num) * ccw_order[ii]) # Set the loop loop = TMR.EdgeLoop(e, dirs) faces[i].addEdgeLoop(loop) # Create the model geo = TMR.Model(verts, edges, faces) return geo
def create_forest(comm, depth, htarget): ''' Load the 3D step file and convert it to a 2D model ''' geo = TMR.LoadModel('battery_3d.step') verts = [] edges = [] faces = [] all_faces = geo.getFaces() # Select only the facs on one face faces.append(all_faces[3]) # The structure's face faces.extend(all_faces[(len(all_faces) - 9):len(all_faces)]) # The face of the 9 batteries # Add only the verts and edges associated with the faces that we selected vert_ids = [] edge_ids = [] for f in faces: num_loops = f.getNumEdgeLoops() for i in range(num_loops): el = f.getEdgeLoop(i) edge_list, _ = el.getEdgeLoop() for e in edge_list: if e.getEntityId() not in edge_ids: edges.append(e) edge_ids.append(e.getEntityId()) v1, v2 = e.getVertices() if v1.getEntityId() not in vert_ids: verts.append(v1) vert_ids.append(v1.getEntityId()) if v2.getEntityId() not in vert_ids: verts.append(v2) vert_ids.append(v2.getEntityId()) # Create the new 2D geometry model geo = TMR.Model(verts, edges, faces) # Name the faces that we need to use faces[7].setName('battery_0') # Corner battery face faces[8].setName('battery_1') # Adjacent battery faces[1].setName('battery_2') # Diagonal battery faces[2].setName('battery') # All the other batteries faces[3].setName('battery') faces[4].setName('battery') faces[5].setName('battery') faces[6].setName('battery') faces[9].setName('battery') faces[0].setName('aluminum') # Create the mesh object mesh = TMR.Mesh(comm, geo) # Mesh the part opts = TMR.MeshOptions() opts.write_mesh_quality_histogram = 1 # Mesh the geometry with the given target size mesh.mesh(htarget, opts=opts) # mesh.writeToVTK('battery_mesh.vtk') # Create a model from the mesh model = mesh.createModelFromMesh() # Create the corresponding mesh topology from the mesh-model topo = TMR.Topology(comm, model) # Create the quad forest and set the topology of the forest forest = TMR.QuadForest(comm) forest.setTopology(topo) forest.createTrees(depth) return forest