class Test(unittest.TestCase): #============================================================================== # def setUp(self): # self.airLogger = AirLogger() # self.airServer = AirServer(self.airLogger) # self.airClient = AirClient(client) # # def tearDown(self): # self.airClient.disconnect() # self.airServer.stop(reactor_stop = False) # pass #============================================================================== def test_init(self): self.opt = Optimizer('test', 0.15, Optimizer.prob_types.comp) self.opt.set_problem_dimensions(20, 20, 40) self.opt.fix_nodes(np.arange(441), directions=['x', 'y', 'z']) self.opt.load_nodes([17860], 1, direction = 'x') def load_topy(self, filename): t = topy.Topology() t.load_tpd_file(filename) return t def test_compare(self): self.test_init() t = self.load_topy('test_optimizer.tpd') assert np.all(self.opt.topy_dict['FIX_DOF'] == t.topydict['FIX_DOF']) assert list(self.opt.loaded_dof) == list(t.topydict['LOAD_DOF']) assert list(self.opt.loads) == list(t.topydict['LOAD_VAL']) assert set(t.topydict.keys()) - set(self.opt.topy_dict.keys()) - set(allowed_missing) == set([]) for key in self.opt.topy_dict.keys(): if type(t.topydict[key]) == np.ndarray: assert np.all(self.opt.topy_dict[key] == t.topydict[key]) else: print key, type(self.opt.topy_dict[key]), type(t.topydict[key]) assert self.opt.topy_dict[key] == t.topydict[key], (self.opt.topy_dict[key], t.topydict[key])
class Test(unittest.TestCase): #============================================================================== # def setUp(self): # self.airLogger = AirLogger() # self.airServer = AirServer(self.airLogger) # self.airClient = AirClient(client) # # def tearDown(self): # self.airClient.disconnect() # self.airServer.stop(reactor_stop = False) # pass #============================================================================== def test_init(self): self.opt = Optimizer('test', 0.15, Optimizer.prob_types.comp) self.opt.set_problem_dimensions(20, 20, 40) self.opt.fix_nodes(np.arange(441), directions=['x', 'y', 'z']) self.opt.load_nodes([17860], 1, direction='x') def load_topy(self, filename): t = topy.Topology() t.load_tpd_file(filename) return t def test_compare(self): self.test_init() t = self.load_topy('test_optimizer.tpd') assert np.all(self.opt.topy_dict['FIX_DOF'] == t.topydict['FIX_DOF']) assert list(self.opt.loaded_dof) == list(t.topydict['LOAD_DOF']) assert list(self.opt.loads) == list(t.topydict['LOAD_VAL']) assert set(t.topydict.keys()) - set( self.opt.topy_dict.keys()) - set(allowed_missing) == set([]) for key in self.opt.topy_dict.keys(): if type(t.topydict[key]) == np.ndarray: assert np.all(self.opt.topy_dict[key] == t.topydict[key]) else: print key, type(self.opt.topy_dict[key]), type(t.topydict[key]) assert self.opt.topy_dict[key] == t.topydict[key], ( self.opt.topy_dict[key], t.topydict[key])
""" Example demonstrating optimization with multiple load cases. This enables the ability to optimize the structure with respect to loads in different directions. """ from topy.optimizer import Optimizer import numpy as np # Initialize problem opt = Optimizer('cylinder', 0.1, void_density = 0.001, max_iterations=50) ex, ey, ez = 20, 20, 10 opt.set_problem_dimensions(ex, ey, ez) # Create cylinder by removing material around z0 = ez/2 x0, y0, r0 = ex/2+0.5,ey/2+.5,ex/2-1 x, y, z = opt.element_indices mask = opt.elements[(x-x0)**2+(y-y0)**2>=r0**2] opt.add_passive_elements(mask) # Create hole in the middle: r1 = r0/3 mask = opt.elements[(x-x0)**2+(y-y0)**2<=r1**2] opt.add_passive_elements(mask) # Fix points in center bearing1 = opt.nodes[x0+r1+1,y0+1-1:y0+1+2,z0+1-1:z0+1+2]
def test_init(self): self.opt = Optimizer('test', 0.15, Optimizer.prob_types.comp) self.opt.set_problem_dimensions(20, 20, 40) self.opt.fix_nodes(np.arange(441), directions=['x', 'y', 'z']) self.opt.load_nodes([17860], 1, direction='x')
""" Example demonstrating optimization with multiple load cases. This enables the ability to optimize the structure with respect to loads in different directions. """ from topy.optimizer import Optimizer # Initialize problem opt = Optimizer('multi_load', 0.10, load_case='vertical') opt.set_problem_dimensions(10, 10, 20) # Define fixed nodes (entire z = 0 plane) left_face = opt.nodes[:, :, 0] opt.fix_nodes(left_face, directions=['x', 'y', 'z']) # Fixed in all directions # Apply a load in -y direction at a single node in the center of the z = 20 plane right_center = opt.nodes[(opt.num_nodes[0]) / 2, (opt.num_nodes[1]) / 2, -1] opt.load_nodes([right_center], -1, direction='y') """ Create a second load case to also optimize for horizontal loads. Note that this is very different than applying loads in both directions to the same load case, try to comment out the line below and observe the difference in the output """ opt.add_load_case('horizontal') # Apply a load in -x direction opt.load_nodes([right_center], -1, direction='x') opt.run_optimization()
""" Example demonstrating basic use of the Optimizer class. Note that by defining fixed and loaded nodes by reference to the node array the number of elements can easily be changed for increased resolution """ from topy.optimizer import Optimizer # Initialize problem opt = Optimizer('basic', 0.15) opt.set_problem_dimensions(10, 10, 20) # Define fixed nodes (entire z = 0 plane) left_face = opt.nodes[:,:,0] opt.fix_nodes(left_face, directions = ['x', 'y', 'z']) # Fixed in all directions # Apply a load in -y direction at a single node in the center of the z = 20 plane right_center = opt.nodes[(opt.num_nodes[0])/2,(opt.num_nodes[1])/2,-1] opt.load_nodes([right_center], -1, direction = 'y') opt.run_optimization()
def test_init(self): self.opt = Optimizer('test', 0.15, Optimizer.prob_types.comp) self.opt.set_problem_dimensions(20, 20, 40) self.opt.fix_nodes(np.arange(441), directions=['x', 'y', 'z']) self.opt.load_nodes([17860], 1, direction = 'x')