Example #1
0
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
Example #2
0
opts.num_smoothing_steps = 20
opts.write_mesh_quality_histogram = 1

# Mesh the geometry with the given target size
htarget = 10.0
mesh.mesh(htarget, opts=opts)
mesh.writeToVTK('surface-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)

# Make the creator class
bcs = TMR.BoundaryConditions()
bcs.addBoundaryCondition('x+')
bcs.addBoundaryCondition('x-')
bcs.addBoundaryCondition('y+')
bcs.addBoundaryCondition('y-')

# Allocate the creator class
creator = CreateMe(bcs)

# Create the initial forest
nlevels = 2
forest.createTrees(nlevels - 1)
def create_mesh(n, AR, prob, ratio1, ratio2, forced_portion, MBB_bc_portion,
    force_magnitude, use_concentrated_force, use_hole, hole_radius):

    Ly = 1.0
    Lx = Ly*AR

    # Create tmr geometry object
    geo = create_geo(AR, prob, forced_portion, MBB_bc_portion,
        ratio1, ratio2, use_hole, hole_radius)

    # Create the mesh
    comm = MPI.COMM_WORLD
    mesh = TMR.Mesh(comm, geo)

    # Mesh the part
    opts = TMR.MeshOptions()
    opts.frontal_quality_factor = 1.25
    opts.num_smoothing_steps = 20
    opts.write_mesh_quality_histogram = 1

    # Mesh the geometry with the given target size
    htarget = Ly / n
    mesh.mesh(htarget, opts=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)

    # Create random trees and balance the mesh. Print the output file
    forest.createTrees()
    forest.balance(1)

    # Create the nodes
    forest.createNodes()

    # Get the mesh connectivity
    conn = forest.getMeshConn()

    # Get the node locations
    X = forest.getPoints()

    # Set the nodal positions using only the x and y locations
    X = np.array(X[:,:2])

    # Get the nodes with the specified name
    if prob == 'cantilever':
        bcs = forest.getNodesWithName('5')
    elif prob == 'MBB':
        bc1 = forest.getNodesWithName('6')
        bc2 = forest.getNodesWithName('2')
    elif prob == 'michell':
        bcs = forest.getNodesWithName('6')
    elif prob == 'lbracket':
        bcs = forest.getNodesWithName('6')

    # Create the vars array with the number of nodes
    nnodes = np.max(conn)+1
    dof = np.zeros((nnodes, 2), dtype=int)

    # Set the vars to a negative index where we have a constraint
    if prob == 'MBB':

        # If it is MBB problem, we only fix x degree of freedom at left edge
        dof[bc1, 0] = -1
        dof[bc2, 1] = -1

    else:
        dof[bcs, :] = -1

    # Assign variables to the nodes
    ndof = 0
    for i in range(nnodes):
        if dof[i,0] >= 0:
            dof[i,0] = ndof
            ndof += 1
        if dof[i,1] >= 0:
            dof[i,1] = ndof
            ndof += 1

    # Set up the force vector
    force = np.zeros(ndof)

    # Assign the force vector
    if prob == 'cantilever':

        if use_concentrated_force:

            south_east_corner_node = -1
            xpos = X[0, 0]
            ypos = X[0, 0]
            for i in range(nnodes):
                if X[i, 0] >= xpos and X[i, 1] <= ypos:
                    south_east_corner_node = i
                    xpos = X[i, 0]
                    ypos = X[i, 1]

            force[dof[south_east_corner_node, 1]] = -force_magnitude

        else:

            quads_on_edge = forest.getQuadsWithName('2')

            for q in quads_on_edge:
                if q.info < 2:
                    n1 = q.info
                    n2 = q.info + 2
                else:
                    n1 = q.info//2
                    n2 = n1 + 1

                v1 = dof[conn[q.face, n1], 1]
                v2 = dof[conn[q.face, n2], 1]

                force[v1] = -force_magnitude
                force[v2] = -force_magnitude

            force[:] /= np.count_nonzero(force)

    elif prob == 'michell':

        if use_concentrated_force:

            distance = Lx**2 + Ly**2
            xtarget = Lx
            ytarget = Ly / 2
            middle_node = -1
            for i in range(nnodes):
                xpos = X[i, 0]
                ypos = X[i, 1]
                if (xpos-xtarget)**2 + (ypos-ytarget)**2 <= distance:
                    middle_node = i
                    distance = (xpos-xtarget)**2 + (ypos-ytarget)**2

            force[dof[middle_node, 1]] = -force_magnitude

        else:

            quads_on_edge = forest.getQuadsWithName('3')

            for q in quads_on_edge:
                if q.info < 2:
                    n1 = q.info
                    n2 = q.info + 2
                else:
                    n1 = q.info//2
                    n2 = n1 + 1

                v1 = dof[conn[q.face, n1], 1]
                v2 = dof[conn[q.face, n2], 1]

                force[v1] = -force_magnitude
                force[v2] = -force_magnitude

            force[:] /= np.count_nonzero(force)

    elif prob == 'MBB':

        if use_concentrated_force:

            distance = Lx**2 + Ly**2
            xtarget = 0
            ytarget = Ly
            north_west_node = -1
            for i in range(nnodes):
                xpos = X[i, 0]
                ypos = X[i, 1]
                if (xpos-xtarget)**2 + (ypos-ytarget)**2 <= distance:
                    north_west_node = i
                    distance = (xpos-xtarget)**2 + (ypos-ytarget)**2

            force[dof[north_west_node, 1]] = -force_magnitude

        else:

            quads_on_edge = forest.getQuadsWithName('5')

            for q in quads_on_edge:
                if q.info < 2:
                    n1 = q.info
                    n2 = q.info + 2
                else:
                    n1 = q.info//2
                    n2 = n1 + 1

                v1 = dof[conn[q.face, n1], 1]
                v2 = dof[conn[q.face, n2], 1]

                force[v1] = -force_magnitude
                force[v2] = -force_magnitude

            force[:] /= np.count_nonzero(force)


    elif prob == 'lbracket':

        if use_concentrated_force:

            distance = Lx**2 + Ly**2
            xtarget = Lx
            ytarget = Ly*ratio2
            middle_node = -1
            for i in range(nnodes):
                xpos = X[i, 0]
                ypos = X[i, 1]
                if (xpos-xtarget)**2 + (ypos-ytarget)**2 <= distance:
                    middle_node = i
                    distance = (xpos-xtarget)**2 + (ypos-ytarget)**2

            force[dof[middle_node, 1]] = -force_magnitude

        else:

            quads_on_edge = forest.getQuadsWithName('3')

            for q in quads_on_edge:
                if q.info < 2:
                    n1 = q.info
                    n2 = q.info + 2
                else:
                    n1 = q.info//2
                    n2 = n1 + 1

                v1 = dof[conn[q.face, n1], 1]
                v2 = dof[conn[q.face, n2], 1]

                force[v1] = -force_magnitude
                force[v2] = -force_magnitude

            force[:] /= np.count_nonzero(force)

    nelems = len(conn)

    return nelems, nnodes, ndof, conn, X, dof, force
Example #4
0
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