def setup(self): #F is in N, A is in m^2, L is in m, and sigma is in MPa F = 4 * 10 ** 7 indeps = self.add_subsystem("indeps", IndepVarComp()) indeps.add_output("P1", -F, desc = "Force in member 1") indeps.add_output("P2", F, desc = "Force in member 2") indeps.add_output("A1", desc = "Cross sectional area of member 1") indeps.add_output("A2", desc = "Cross sectional area of member 2") indeps.add_output("L", desc = "Length of members") trussAB = self.add_subsystem("trussAB", truss()) trussBC = self.add_subsystem("trussBC", truss()) self.connect("indeps.P1", ["trussAB.P"]) self.connect("indeps.P2", ["trussBC.P"]) self.add_subsystem("obj_cmp", ExecComp("obj = L * (A1 + A2)", A1 = 1.0, A2 = 1.0, L = 1.0)) self.add_subsystem("con1", ExecComp("con = 400 - abs(sigma)")) self.add_subsystem("con2", ExecComp("con = 400 - abs(sigma)")) # What about the units for the subsystem? self.connect("indeps.L", ["obj_cmp.L"]) self.connect("trussAB.sigma", ["con1.sigma"]) self.connect("trussBC.sigma", ["con2.sigma"]) self.connect("indeps.A1", ["trussAB.A", "obj_cmp.A1"]) self.connect("indeps.A2", ["trussBC.A", "obj_cmp.A2"])
def setup(self): ##F is in N, A is in m^2, L is in m, and sigma is in MPa F = 4 * 10**7 indeps = self.add_subsystem("indeps", IndepVarComp()) indeps.add_output("P1", 0) indeps.add_output("P2", F) indeps.add_output("P3", F) indeps.add_output("P4", F) indeps.add_output("P5", -F * (2**.5)) indeps.add_output("A1") indeps.add_output("A2") indeps.add_output("A3") indeps.add_output("A4") indeps.add_output("A5") indeps.add_output("L1") indeps.add_output("L2") # What about the units for the subsystem? trussAB = self.add_subsystem("trussAB", truss()) trussBC = self.add_subsystem("trussBC", truss()) trussCD = self.add_subsystem("trussCD", truss()) trussDE = self.add_subsystem("trussDA", truss()) trussEA = self.add_subsystem("trussAC", truss()) self.connect("indeps.P1", ["trussAB.P"]) self.connect("indeps.P2", ["trussBC.P"]) self.connect("indeps.P3", ["trussCD.P"]) self.connect("indeps.P4", ["trussDA.P"]) self.connect("indeps.P5", ["trussAC.P"]) self.add_subsystem( "obj_cmp", ExecComp("obj = L1 * (A1 + A2 + A3 + A4) + L2 * A5", A1=0.0, A2=0.0, A3=0.0, A4=0.0, A5=0.0, L1=1.0, L2=2**.5)) self.add_subsystem("con1", ExecComp("con = 400 - abs(sigma)")) self.add_subsystem("con2", ExecComp("con = 400 - abs(sigma)")) self.add_subsystem("con3", ExecComp("con = 400 - abs(sigma)")) self.add_subsystem("con4", ExecComp("con = 400 - abs(sigma)")) self.add_subsystem("con5", ExecComp("con = 400 - abs(sigma)")) self.connect("indeps.L1", ["obj_cmp.L1"]) self.connect("indeps.L2", ["obj_cmp.L2"]) self.connect("trussAB.sigma", ["con1.sigma"]) self.connect("trussBC.sigma", ["con2.sigma"]) self.connect("trussCD.sigma", ["con3.sigma"]) self.connect("trussDA.sigma", ["con4.sigma"]) self.connect("trussAC.sigma", ["con5.sigma"]) self.connect("indeps.A1", ["trussAB.A", "obj_cmp.A1"]) self.connect("indeps.A2", ["trussBC.A", "obj_cmp.A2"]) self.connect("indeps.A3", ["trussCD.A", "obj_cmp.A3"]) self.connect("indeps.A4", ["trussDA.A", "obj_cmp.A4"]) self.connect("indeps.A5", ["trussAC.A", "obj_cmp.A5"])
def book_example(): """ setting up the list of nodes """ # input all nodes with their respective nr and x,y position # initialize an empty node table NTble = nodeTable() # add elements to the node table usig addNode_to_table NTble.addNode_to_table(node(0, 0, 0, np.nan, np.nan, np.nan, np.nan)) NTble.addNode_to_table(node(1, 0, 0, 0, 0, np.nan, np.nan)) NTble.addNode_to_table(node(2, 3 * 12, 0, np.nan, np.nan, np.nan, np.nan)) NTble.addNode_to_table(node(3, 0, 3 * 12, 0, 0, np.nan, np.nan)) NTble.addNode_to_table( node(4, 3 * 12, 3 * 12, np.nan, np.nan, np.nan, -500)) NTble.addNode_to_table( node(5, 6 * 12, 3 * 12, np.nan, np.nan, np.nan, -500)) """ setting up the list of elements """ #create an empty truss Tr = truss(1) #add elements using addElementByNode Tr.addElementByNode(NTble, 1, 2) Tr.addElementByNode(NTble, 3, 2) Tr.addElementByNode(NTble, 3, 4) Tr.addElementByNode(NTble, 2, 4) Tr.addElementByNode(NTble, 2, 5) Tr.addElementByNode(NTble, 4, 5) return Tr
def objcon(x): mass, stress = truss.truss(x) f = mass / 100 g = 1.0 - stress / stressmax g = np.append(g, stress / stressmax + 1) g[8] = 1.0 - stress[8] / 75e3 g[18] = 1.0 + stress[8] / 75e3 return f, g
def objfunc(self, data): x = data['areas'] funcs = {} mass, stress = truss(x) funcs['mass'] = mass funcs['stress_arr'] = stress self.mass_hist.append(mass) self.stress_hist.append(stress) return funcs, False
def func(x): mass, stress = truss(x) f = mass / 100 file.write(str(flast * 100)) file.write('\n') g = (stressmax - stress) / stressmax g = np.append(g, (stress + stressmax) / stressmax) g[8] = (75e3 - stress[8]) / 75e3 g[18] = (75e3 + stress[8]) / 75e3 return f, g
def constraintAll(x): mass, stress = truss(x) global num_evals num_evals += 1 area = x - 0.1 stress_ub = 25e3 - stress stress_ub[8] = 75e3 - stress[8] stress_lb = stress + 25e3 stress_lb[8] = stress[8] + 75e3 return np.hstack((area, stress_ub, stress_lb))
def setup(self): #F is in N, A is in m^2, L is in m, and sigma is in MPa F = 4 * 10**7 indeps = self.add_subsystem("indeps", IndepVarComp()) indeps.add_output("P1", F / 2) indeps.add_output("P2", -F) indeps.add_output("P3", F) indeps.add_output("A1") indeps.add_output("A2") indeps.add_output("A3") indeps.add_output("L") # What about the units for the subsystem? trussAB = self.add_subsystem("trussAB", truss()) trussBC = self.add_subsystem("trussBC", truss()) trussCA = self.add_subsystem("trussCA", truss()) self.connect("indeps.P1", ["trussAB.P"]) self.connect("indeps.P2", ["trussBC.P"]) self.connect("indeps.P3", ["trussCA.P"]) self.add_subsystem( "obj_cmp", ExecComp("obj = L * (A1 + A2 + A3)", A1=0.0, A2=0.0, A3=0.0, L=1.0)) self.add_subsystem("con1", ExecComp("con = 400 - abs(sigma)")) self.add_subsystem("con2", ExecComp("con = 400 - abs(sigma)")) self.add_subsystem("con3", ExecComp("con = 400 - abs(sigma)")) self.connect("indeps.L", ["obj_cmp.L"]) self.connect("trussAB.sigma", ["con1.sigma"]) self.connect("trussBC.sigma", ["con2.sigma"]) self.connect("trussCA.sigma", ["con3.sigma"]) self.connect("indeps.A1", ["trussAB.A", "obj_cmp.A1"]) self.connect("indeps.A2", ["trussBC.A", "obj_cmp.A2"]) self.connect("indeps.A3", ["trussCA.A", "obj_cmp.A3"])
def objfunc(xx): x = xx['xvars'] mass = -1 stress = [-1] * 10 try: fail = False mass, stress = truss(x) except: print('error') fail = True # set_trace() funcs = {} funcs['obj'] = mass conval = [0] * 10 for i in range(10): conval[i] = stress[i] funcs['con'] = conval # set_trace() return funcs, fail
def findMaxLoad(mesh, model, ele, loadNode, start, interval, end): failed = False nodal_load = {loadNode: [0, start - interval]} while failed == False: U, Q = displmethod.solver(mesh, model, ele, nodal_load) t = truss.truss(model.XYZ, model.CON, Q) #print(nodal_load) #print(t.failed) if (t.failed): if interval <= 0.5: return nodal_load[loadNode][1] return findMaxLoad(mesh, model, ele, loadNode, nodal_load[loadNode][1] + interval, interval / 2, nodal_load[loadNode][1]) failed = True nodal_load[loadNode][1] -= interval
x0, bounds=Bounds(0.1, 10), constraints=constraints, options=options) # print("x = ", res.x) # print('f = ', res.fun) # print(res.success) # print(res.message) x_star = res.x return x_star if __name__ == '__main__': file = open('mass_file.txt', 'w') params = 0 stressmax = 25e3 x_star = runoptimization(params, stressmax) mass, stress = truss.truss(x_star) print('The optimized diameters are: ', x_star) print('Mass is: ', mass) print('Stress in each member is: ', stress) running_mass = [] with open('mass_file.txt') as my_file: running_mass = my_file.readlines() print(running_mass)
def solve(x): global num_evals num_evals += 1 mass, stress = truss(x) return mass
all_constraints = {'type': 'ineq', 'fun': constraintAll} callback = callbackF2 #### Solve start = np.ones(10) * 0.1 fit = minimize(solve, start, method="SLSQP", constraints=all_constraints, options={'disp': True}, callback=callback) #### Output values for debugging print(truss(fit.x)) print(fit.x) print(num_evals) #### Plot Results fig = plt.figure() ax = fig.add_subplot(211) ax.set_title("Truss Optimization") ax.plot(range(len(all_functions)), all_functions) ax.set_ylabel("Mass (lbs)") ax = fig.add_subplot(212) ax.plot(range(len(max_const_vio)), max_const_vio) plt.gcf().subplots_adjust(left=0.15) ax.set_ylabel("Max Constraint") ax.set_xlabel("Iterations")
def setup(self): #F is in N, A is in m^2, L is in m, and sigma is in MPa F = 4 * 10**7 indeps = self.add_subsystem("indeps", IndepVarComp()) indeps.add_output( "x_reaction_n1", F * (3**.5) / 2, units="N", desc="Horizontal reaction force of pinnned joint on node 1") indeps.add_output( "y_reaction_n1", F, units="N", desc="Vertical reaction force of pinnned joint on node 1") indeps.add_output( "x_reaction_direction_n1", math.pi, units="rad", desc= "Direction of horizontal reaction force of pinned joint on node 1") indeps.add_output( "y_reaction_direction_n1", math.pi / 2, units="rad", desc= "Direction of vertical reaction force of pinned joint on node 1") indeps.add_output("reaction_n2", F * (3**.5) / 2, units="N", desc="Reaction force of roller joint on node 2") indeps.add_output( "reaction_direction_n2", 0, units="rad", desc="Direction of reaction force of roller joint on node 2") indeps.add_output("truss1_n1", math.pi * 11 / 6, units="rad", desc="Direction of truss1 at node 1") indeps.add_output("truss2_n1", math.pi * 3 / 2, units="rad", desc="Direction of truss2 at node 1") indeps.add_output("truss2_n2", math.pi / 2, units="rad", desc="Direction of truss2 at node 2") indeps.add_output("truss3_n2", math.pi / 6, units="rad", desc="Direction of truss2 at node 2") indeps.add_output("A1") indeps.add_output("A2") indeps.add_output("A3") indeps.add_output("L") # What about the units for the subsystem? self.add_subsystem("node1", Node(n_known=2, n_unknown=2)) self.add_subsystem("node2", Node(n_known=2, n_unknown=1)) truss1 = self.add_subsystem("truss1", truss()) truss2 = self.add_subsystem("truss2", truss()) truss3 = self.add_subsystem("truss3", truss()) self.connect("indeps.x_reaction_n1", "node1.known_force 1") self.connect("indeps.y_reaction_n1", "node1.known_force 2") self.connect("indeps.x_reaction_direction_n1", "node1.old_direction 1") self.connect("indeps.y_reaction_direction_n1", "node1.old_direction 2") self.connect("indeps.truss1_n1", "node1.new_direction 1") self.connect("indeps.truss2_n1", "node1.new_direction 2") self.connect("indeps.reaction_n2", "node2.known_force 1") self.connect("indeps.reaction_direction_n2", "node2.old_direction 1") self.connect("node1.new_truss 2", "node2.known_force 2") self.connect("indeps.truss2_n2", "node2.old_direction 2") self.connect("indeps.truss3_n2", "node2.new_direction 1") self.connect("node1.new_truss 1", ["truss1.P"]) self.connect("node1.new_truss 2", ["truss2.P"]) self.connect("node2.new_truss 1", ["truss3.P"]) self.add_subsystem( "obj_cmp", ExecComp("obj = L * (A1 + A2 + A3)", A1=0.0, A2=0.0, A3=0.0, L=1.0)) self.add_subsystem("con1", ExecComp("con = 400 - abs(sigma)")) self.add_subsystem("con2", ExecComp("con = 400 - abs(sigma)")) self.add_subsystem("con3", ExecComp("con = 400 - abs(sigma)")) self.connect("indeps.L", ["obj_cmp.L"]) self.connect("truss1.sigma", ["con1.sigma"]) self.connect("truss2.sigma", ["con2.sigma"]) self.connect("truss3.sigma", ["con3.sigma"]) self.connect("indeps.A1", ["truss1.A", "obj_cmp.A1"]) self.connect("indeps.A2", ["truss2.A", "obj_cmp.A2"]) self.connect("indeps.A3", ["truss3.A", "obj_cmp.A3"])
def bridge_JensFinalV2(): """ setting up the list of nodes """ # input all nodes with their respective nr and x,y position # initialize an empty node table NTble = nodeTable() # =====================INPUTS=============================== #How many nodes on the bottom line of the truss? nodesQuantity = 2+16 turnTrussUpsideDown = False #How long should the distance between the nodes on the bottom line be? Leng = 50*10**-3 #Diagonal = the length of the elements connecting the top and bottom line of the truss. Diagonal = 40*10**-3 Height = np.sqrt(Diagonal**2-(Leng/2)**2) #The number of the node where the load attaches ForceNodeNr = 17 #Components of the force of the load Fx = 0 Fy = -80 #Number of the nodes that cannot move. (attached to the mounting) LockNodeNrs = (0, 1, 101) #angle of the truss. angleTop = 0 radTop = np.radians(angleTop) angleBottom = angleTop radBottom = np.radians(angleBottom) # =========================================================== if turnTrussUpsideDown == True: inv = -1 else: inv = 1 ''' setting up the list of nodes ''' # creating the bottom line of the triangled truss NTble.addNode_to_table(node(0,0,0,np.nan,np.nan,np.nan,np.nan)) for x in range(1, nodesQuantity): if x == ForceNodeNr: tempFx = Fx tempFy = Fy else: tempFx = np.nan tempFy = np.nan if x in LockNodeNrs: tempDispX = 0 tempDispY = 0 else: tempDispX = np.nan tempDispY = np.nan NTble.addNode_to_table(node(x,(x-1)*Leng*np.cos(radBottom),(x-1)*Leng*np.sin(radBottom),tempDispX,tempDispY,tempFx,tempFy)) # creating the top line of the triangled truss for x in range(1, nodesQuantity-1): tempNr = 21+x if tempNr == ForceNodeNr: tempFx = Fx tempFy = Fy else: tempFx = np.nan tempFy = np.nan if tempNr in LockNodeNrs: tempDispX = 0 tempDispY = 0 else: tempDispX = np.nan tempDispY = np.nan NTble.addNode_to_table(node(tempNr,(x*Leng-Leng/2)*np.cos(radTop)-inv*Height*np.sin(radTop),inv*Height*np.cos(radTop)+(x*Leng-Leng/2)*np.sin(radTop),tempDispX,tempDispY,tempFx,tempFy)) """ setting up the list of elements """ #create an empty truss Tr = truss(1) #add elements using addElementByNode for x in range(1, nodesQuantity-1): Tr.addElementByNode(NTble,x,x+1) for x in range(1, nodesQuantity-2): Tr.addElementByNode(NTble,21+x,22+x) for y in range(22, 22 + nodesQuantity - 2): Tr.addElementByNode(NTble,y-21,y) Tr.addElementByNode(NTble,y-20,y) # === Manually add NODES below === NTble.addNode_to_table(node(101,0,0.375,0,0,np.nan,np.nan)) # === Manually add ELEMENTS below === Tr.addElementByNode(NTble,101,37) #doubling Tr.addElementByNode(NTble,1,2) Tr.addElementByNode(NTble,2,3) Tr.addElementByNode(NTble,3,4) # === == == == == == == == == === return Tr
for i in range(25): ele.E[i] = 100. ele.A[i] = 10. ele.TYPE[i] = 'Truss' loadNode = 3 model = structure.Builder(mesh, ele, bound) maxLoad = trussAnalyzer.findMaxLoad(mesh, model, ele, loadNode, 0, 10, -100) print(maxLoad) nodal_load = {loadNode: [0, -10]} U, Q = displmethod.solver(mesh, model, ele, nodal_load) trus = truss.truss(model.XYZ, model.CON, Q) print(trus.failed) print(trus.lengths) print(trus.totalLength) #print('Broken member: ' + str(trus.findBroken()+1)) print('Performance rating: ' + str(pV(maxLoad, trus.totalLength))) plotter.axialforce(model, Q) #plotter.undeformed(model) #plotter.deformed(model, U) plt.show()