def create_forest(comm, depth, htarget): """ Create an initial forest for analysis and optimization. This code loads in the model, sets names, meshes the geometry and creates an OctForest from the mesh. The forest is populated with octrees with the specified depth. Args: comm (MPI_Comm): MPI communicator depth (int): Depth of the initial trees htarget (float): Target global element mesh size Returns: OctForest: Initial forest for topology optimization """ # Load the geometry model geo = TMR.LoadModel('../cantilever/cantilever.stp') # Mark the boundary condition faces verts = geo.getVertices() edges = geo.getEdges() faces = geo.getFaces() volumes = geo.getVolumes() faces[3].setName('fixed') faces[4].setSource(volumes[0], faces[5]) verts[4].setName('pt1') verts[3].setName('pt2') # Set the boundary conditions for the problem bcs = TMR.BoundaryConditions() bcs.addBoundaryCondition('fixed') # Create the mesh mesh = TMR.Mesh(comm, geo) # Set the meshing options opts = TMR.MeshOptions() # Create the surface mesh mesh.mesh(args.htarget, opts) # 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.OctForest(comm) forest.setTopology(topo) # Create the trees forest.createTrees(depth) return forest
def create_forest(comm, depth, htarget): """ Create an initial forest for analysis. and optimization This code loads in the model, sets names, meshes the geometry and creates a QuadForest from the mesh. The forest is populated with quadtrees with the specified depth. Args: comm (MPI_Comm): MPI communicator depth (int): Depth of the initial trees htarget (float): Target global element mesh size Returns: QuadForest: Initial forest for topology optimization """ # Load the geometry model geo = TMR.LoadModel('biclamped_traction.stp') # Mark the boundary condition faces verts = geo.getVertices() edges = geo.getEdges() faces = geo.getFaces() edges[1].setName('fixed') edges[9].setName('fixed') edges[4].setName('traction') # Create the mesh mesh = TMR.Mesh(comm, geo) # Set the meshing options opts = TMR.MeshOptions() # Create the surface mesh mesh.mesh(htarget, opts) # 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) # Set parameters for later usage forest.createTrees(depth) return forest
def load_model(): geo = TMR.LoadModel('beam.step') # Get the faces/volume from the model faces = geo.getFaces() edges = geo.getEdges() verts = geo.getVertices() vols = geo.getVolumes() # Set the names faces[6].setName('hole') faces[7].setName('hole') faces[0].setName('fixed') # Set the source/destination faces faces[1].setSource(vols[0], faces[3]) return geo
x = [0, 0, 0.5] d = [0.5, 0.5, 0.5] b2 = ctx.makeSolidBody(egads.BOX, rdata=[x, d]) # Write out to a STEP file m1 = ctx.makeTopology(egads.MODEL, children=[b1]) m2 = ctx.makeTopology(egads.MODEL, children=[b2]) # Save the egads models m1.saveModel('box1.egads', overwrite=True) m2.saveModel('box2.egads', overwrite=True) comm = MPI.COMM_WORLD htarget = 0.25 geo1 = TMR.LoadModel('box1.egads', print_lev=1) geo2 = TMR.LoadModel('box2.egads', print_lev=1) TMR.setMatchingFaces([geo1, geo2]) verts = [] 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))
p.add_argument('--output', type=str, default='surface-mesh.vtk', help='output file name') args = p.parse_args() # Get the value of the filename 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
# Set the x/y locations of the density "nodes" n = 100 x = np.linspace(0, xlen, n) pts = [] for j in range(n): for i in range(n): if x[i] <= a or x[j] <= a: pts.append([x[i], x[j], 0.0]) locator = TMR.PointLocator(np.array(pts)) # Set the number of design variables num_design_vars = len(pts) # Load in the L-bracket model geo = TMR.LoadModel('2d-bracket-fillet.stp') verts = geo.getVertices() edges = geo.getEdges() faces = geo.getFaces() geo = TMR.Model(verts, edges, faces) # Set the edges edges[5].setName('clamped') edges[1].setName('traction') # Initial target mesh spacing htarget = args.htarget # Create the new mesh mesh = TMR.Mesh(comm, geo)
htarget = args.htarget # Set the order order = args.order # Set the KS parameter 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)
x1 = [50, 0, 0] x2 = [50, 0, 100.0] radius = 75.0 c2 = ctx.makeSolidBody(egads.CYLINDER, rdata=[x1, x2, radius]) model = c1.solidBoolean(c2, egads.SUBTRACTION) model.saveModel('cylinder_sweep.%s'%(extension), overwrite=True) # Set the meshing options opts = TMR.MeshOptions() opts.write_mesh_quality_histogram = 1 opts.triangularize_print_iter = 50000 # Load the separate geometries and mesh each geo = TMR.LoadModel('cylinder_sweep.%s'%(extension)) faces = geo.getFaces() vols = geo.getVolumes() faces[3].setSource(vols[0], faces[1]) # 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)
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
import os import numpy as np from mpi4py import MPI from tmr import TMR from tacs import TACS # Set the communicator comm = MPI.COMM_WORLD # The fine octree forest fine = None stepfile = 'beam.stp' if os.path.isfile(stepfile): # Load the geometry model geo = TMR.LoadModel(stepfile) # Mark the boundary condition faces faces = geo.getFaces() volumes = geo.getVolumes() faces[4].setSource(volumes[0], faces[5]) # Create the mesh mesh = TMR.Mesh(comm, geo) # Set the meshing options opts = TMR.MeshOptions() opts.frontal_quality_factor = 1.25 opts.num_smoothing_steps = 10 opts.write_mesh_quality_histogram = 0
if functional == 'ks_grad' or functional == 'pnorm_grad': if comm.rank == 0: print('functional: %10s parameter: %g' % (functional, ksweight)) print('%10s %25s %25s' % ('n', 'max', 'func. estimate')) for n in nrange: ks_max, approx = get_disk_aggregate(comm, functional, ksweight, R, n=n) if comm.rank == 0: print('%10d %25.16e %25.16e' % (n, ks_max, approx)) exact_functional = approx geo = TMR.LoadModel('2d-disk.stp') verts = geo.getVertices() edges = geo.getEdges() faces = geo.getFaces() # Set the names verts[1].setName('midpoint') for index in [0, 2, 3, 4]: verts[index].setName('clamped') for index in [2, 4, 6, 7]: edges[index].setName('clamped') # Set the boundary conditions bcs.addBoundaryCondition('clamped', [0]) if comm.rank == 0:
E, nu, kcorr, ys, L, R, alpha, beta, load, n=n) if comm.rank == 0: print('%10d %25.16e' % (n, approx)) exact_functional = approx # 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].setName('Clamped') verts[1].setName('Restrained') edges[0].setName('Restrained') edges[2].setName('Restrained') # Set the boundary conditions bcs.addBoundaryCondition('Clamped', [0, 1, 2, 5])
p.add_argument('--max_lbfgs', type=int, default=10) p.add_argument('--hessian_reset', type=int, default=10) p.add_argument('--use_paropt', action='store_true', default=True) p.add_argument('--use_mma', action='store_true', default=False) args = p.parse_args() # Set the parameter to use paropt or MMA use_paropt = True if args.use_mma: use_paropt = False # The communicator comm = MPI.COMM_WORLD # Load the geometry model geo = TMR.LoadModel('long-beam.stp') # Mark the boundary condition faces verts = geo.getVertices() faces = geo.getFaces() volumes = geo.getVolumes() # faces[3].setAttribute('fixed') # faces[4].setSource(volumes[0], faces[5]) # verts[4].setAttribute('pt1') # verts[3].setAttribute('pt2') # faces[1].setAttribute('load') faces[4].setAttribute('fixed') faces[4].setSource(volumes[0], faces[5]) faces[5].setAttribute('load') # Set the boundary conditions for the problem
options['max_iter'] = 10000 elif args.optimizer == 'paropt': # options['this is an option'] = value options['algorithm'] = 'tr' options['abs_optimality_tol'] = 1e-8 options['max_iterations'] = 20 options['qn_subspace_size'] = 10 # Set the trust region size options['tr_init_size'] = 0.5 options['tr_max_size'] = 2.0 options['tr_min_size'] = 1e-5 options['tr_penalty_gamma'] = 20.0 # Create the CRM wingbox model and set the names geo = TMR.LoadModel('ucrm_9_model.step') verts = geo.getVertices() edges = geo.getEdges() faces = geo.getFaces() geo = TMR.Model(verts, edges, faces) # Set the edge names for i, e in enumerate(edges): attr = 'E%d' % (i) e.setName(attr) # Set the face names and create the constitutive objects elem_dict = [{}, {}, {}, {}] for i, f in enumerate(faces): attr = 'F%d' % (i) f.setName(attr)
assemblers.append(creator.createTACS(order, forest)) varmaps.append(creator.getMap()) vecindices.append(creator.getIndices()) # Create the multigrid object mg = TMR.createMg(assemblers, forests) # Create the topology optimization problem problem = TMR.TopoProblem(assemblers, filters, varmaps, vecindices, mg) return assemblers[0], problem comm = MPI.COMM_WORLD # Load the geometry model geo = TMR.LoadModel('bracket_solid.stp') # Mark the boundary condition faces faces = geo.getFaces() volumes = geo.getVolumes() faces[15].setAttribute('fixed') faces[4].setSource(volumes[0], faces[1]) faces[4].setAttribute('surface') # Set the boundary conditions for the problem bcs = TMR.BoundaryConditions() bcs.addBoundaryCondition('fixed') # Create the mesh mesh = TMR.Mesh(comm, geo)
p.add_argument('--opt_barrier_power', type=float, default=1.0) p.add_argument('--output_freq', type=int, default=1) p.add_argument('--init_depth', type=int, default=2) p.add_argument('--mg_levels', type=int, nargs='+', default=[2, 3]) p.add_argument('--max_lbfgs', type=int, default=10) p.add_argument('--hessian_reset', type=int, default=10) args = p.parse_args() # Set the parameter to use paropt or MMA use_paropt = False # The communicator comm = MPI.COMM_WORLD # Load the geometry model geo = TMR.LoadModel('beam2d.stp') # Mark the boundary condition faces verts = geo.getVertices() edges = geo.getEdges() volumes = geo.getVolumes() edges[1].setAttribute('fixed') verts[0].setAttribute('pt1') # Set the boundary conditions for the problem bcs = TMR.BoundaryConditions() bcs.addBoundaryCondition('fixed') # Create the mesh mesh = TMR.Mesh(comm, geo)
# Set the meshing options opts = TMR.MeshOptions() opts.write_mesh_quality_histogram = 1 opts.triangularize_print_iter = 50000 # Load the separate geometries and mesh each if extension == 'egads': shell_geo = TMR.ConvertEGADSModel(shell_model) ring_geo = TMR.ConvertEGADSModel(ring_model) plate_geo = TMR.ConvertEGADSModel(plate_model) else: # Load in the files comm.Barrier() for rank in range(comm.size): if comm.rank == rank: shell_geo = TMR.LoadModel('shell.%s' % (extension)) ring_geo = TMR.LoadModel('ring.%s' % (extension)) plate_geo = TMR.LoadModel('plate.%s' % (extension)) comm.Barrier() # All the model objects if model_type == 'full': all_geos = [ring_geo, plate_geo, shell_geo] else: all_geos = [plate_geo, ring_geo] # Create the full list of vertices, edges, faces and volumes verts = [] edges = [] faces = [] vols = []
from mpi4py import MPI from tmr import TMR import argparse # Create the communicator comm = MPI.COMM_WORLD # Create an argument parser to read in command-line arguments p = argparse.ArgumentParser() p.add_argument('--reverse', default=False, action='store_true') args = p.parse_args() # Load the model from the STEP file geo = TMR.LoadModel('first-section.stp') # Get the volumes vols = geo.getVolumes() # Get the edges/faces from the geometry faces = geo.getFaces() edges = geo.getEdges() # Set the source/target relationships if args.reverse: faces[4].setSource(vols[0], faces[5]) else: faces[5].setSource(vols[0], faces[4]) edges[8].setSource(edges[5]) # Create the geometry mesh = TMR.Mesh(comm, 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