def test_add_constraint(self): n = 50 prob = om.Problem() model = prob.model prob.driver = om.ScipyOptimizeDriver() model.add_subsystem('comp', om.ExecComp('y = -3.0*x**2 + k', x=np.zeros((n, )), y=np.zeros((n, )), k=0.0), promotes_inputs=['x', 'k']) model.add_subsystem('ks', om.KSComp(width=n, upper=4.0, add_constraint=True)) model.add_design_var('k', lower=-10, upper=10) model.add_objective('k', scaler=-1) model.connect('comp.y', 'ks.g') prob.setup() prob.set_val('x', np.linspace(-np.pi/2, np.pi/2, n)) prob.set_val('k', 5.) prob.run_driver() self.assertTrue(max(prob.get_val('comp.y')) <= 4.0)
def test_vectorized(self): prob = om.Problem() model = prob.model x = np.zeros((3, 5)) x[0, :] = np.array([3.0, 5.0, 11.0, 13.0, 17.0]) x[1, :] = np.array([13.0, 11.0, 5.0, 17.0, 3.0])*2 x[2, :] = np.array([11.0, 3.0, 17.0, 5.0, 13.0])*3 model.add_subsystem('px', om.IndepVarComp(name="x", val=x)) model.add_subsystem('ks', om.KSComp(width=5, vec_size=3)) model.connect('px.x', 'ks.g') model.add_design_var('px.x') model.add_constraint('ks.KS', upper=0.0) prob.setup() prob.run_driver() assert_near_equal(prob['ks.KS'][0], 17.0) assert_near_equal(prob['ks.KS'][1], 34.0) assert_near_equal(prob['ks.KS'][2], 51.0) partials = prob.check_partials(includes=['ks'], out_stream=None) for (of, wrt) in partials['ks']: assert_near_equal(partials['ks'][of, wrt]['abs error'][0], 0.0, 1e-6)
def test_vectorized(self): prob = om.Problem() model = prob.model x = np.zeros((3, 5)) x[0, :] = np.array([3.0, 5.0, 11.0, 13.0, 17.0]) x[1, :] = np.array([13.0, 11.0, 5.0, 17.0, 3.0])*2 x[2, :] = np.array([11.0, 3.0, 17.0, 5.0, 13.0])*3 model.add_subsystem('ks', om.KSComp(width=5, vec_size=3)) model.add_design_var('ks.g') model.add_constraint('ks.KS', upper=0.0) prob.setup() prob.set_val('ks.g', x) prob.run_driver() assert_near_equal(prob.get_val('ks.KS', indices=0), 17.0) assert_near_equal(prob.get_val('ks.KS', indices=1), 34.0) assert_near_equal(prob.get_val('ks.KS', indices=2), 51.0) prob.model.ks._no_check_partials = False # override skipping of check_partials partials = prob.check_partials(includes=['ks'], out_stream=None) for (of, wrt) in partials['ks']: assert_near_equal(partials['ks'][of, wrt]['abs error'][0], 0.0, 1e-6)
def test_partials_no_compute(self): prob = om.Problem() model = prob.model model.add_subsystem('px', om.IndepVarComp('x', val=np.array([5.0, 4.0]))) ks_comp = model.add_subsystem('ks', om.KSComp(width=2)) model.connect('px.x', 'ks.g') prob.setup() prob.run_driver() # compute partials with the current model inputs inputs = { 'g': prob['ks.g'] } partials = {} ks_comp.compute_partials(inputs, partials) assert_near_equal(partials[('KS', 'g')], np.array([1., 0.]), 1e-6) # swap inputs and call compute partials again, without calling compute inputs['g'][0][0] = 4 inputs['g'][0][1] = 5 ks_comp.compute_partials(inputs, partials) assert_near_equal(partials[('KS', 'g')], np.array([0., 1.]), 1e-6)
def test_units(self): n = 10 model = om.Group() model.add_subsystem('ks', om.KSComp(width=n, units='m'), promotes_inputs=[('g', 'x')]) model.set_input_defaults('x', range(n), units='ft') prob = om.Problem(model=model) prob.setup() prob.run_model() assert_near_equal(prob.get_val('ks.KS', indices=0), np.amax(prob.get_val('x')), tolerance=1e-8)
def test_add_constraint(self): import numpy as np import openmdao.api as om import matplotlib.pyplot as plt n = 50 prob = om.Problem() model = prob.model prob.driver = om.ScipyOptimizeDriver() ivc = model.add_subsystem('ivc', om.IndepVarComp()) ivc.add_output('x', val=np.linspace(-np.pi / 2, np.pi / 2, n)) ivc.add_output('k', val=5.0) model.add_subsystem( 'comp', om.ExecComp('y = -3.0*x**2 + k', x=np.zeros((n, )), y=np.zeros((n, )), k=0.0)) model.add_subsystem('ks', om.KSComp(width=n, upper=4.0, add_constraint=True)) model.add_design_var('ivc.k', lower=-10, upper=10) model.add_objective('ivc.k', scaler=-1) model.connect('ivc.x', 'comp.x') model.connect('ivc.k', 'comp.k') model.connect('comp.y', 'ks.g') prob.setup() prob.run_driver() self.assertTrue(max(prob.get_val('comp.y')) <= 4.0) fig, ax = plt.subplots() x = prob.get_val('ivc.x') y = prob.get_val('comp.y') ax.plot(x, y, 'r.') ax.plot(x, 4.0 * np.ones_like(x), 'k--') ax.set_xlabel('x') ax.set_ylabel('y') ax.grid(True) ax.text(-0.25, 0, f"k = {prob.get_val('ivc.k')[0]:6.3f}") plt.show()
def test_upper(self): prob = om.Problem() model = prob.model model.add_subsystem('comp', om.ExecComp('y = 3.0*x', x=np.zeros((2, )), y=np.zeros((2, )))) model.add_subsystem('ks', om.KSComp(width=2)) model.connect('comp.y', 'ks.g') model.ks.options['upper'] = 16.0 prob.setup() prob.set_val('comp.x', np.array([5.0, 4.0])) prob.run_model() assert_near_equal(prob.get_val('ks.KS', indices=0), -1.0)
def test_lower_flag(self): prob = om.Problem() model = prob.model model.add_subsystem('px', om.IndepVarComp('x', val=np.array([5.0, 4.0]))) model.add_subsystem('comp', om.ExecComp('y = 3.0*x', x=np.zeros((2, )), y=np.zeros((2, )))) model.add_subsystem('ks', om.KSComp(width=2)) model.connect('px.x', 'comp.x') model.connect('comp.y', 'ks.g') model.ks.options['lower_flag'] = True prob.setup() prob.run_model() assert_near_equal(prob['ks.KS'][0], -12.0)
def test_vectorized(self): prob = om.Problem() model = prob.model model.add_subsystem('comp', om.ExecComp('y = 3.0*x', x=np.zeros((2, 2)), y=np.zeros((2, 2))), promotes_inputs=['x']) model.add_subsystem('ks', om.KSComp(width=2, vec_size=2)) model.connect('comp.y', 'ks.g') prob.setup() prob.set_val('x', np.array([[5.0, 4.0], [10.0, 8.0]])) prob.run_model() assert_near_equal(prob.get_val('ks.KS'), np.array([[15], [30]]))
def test_upper(self): prob = om.Problem() model = prob.model model.add_subsystem('px', om.IndepVarComp('x', val=np.array([5.0, 4.0]))) model.add_subsystem('comp', om.ExecComp('y = 3.0*x', x=np.zeros((2, )), y=np.zeros((2, )))) model.add_subsystem('ks', om.KSComp(width=2)) model.connect('px.x', 'comp.x') model.connect('comp.y', 'ks.g') model.ks.options['upper'] = 16.0 prob.setup() prob.run_model() assert_rel_error(self, prob['ks.KS'][0], -1.0)
def test_basic_ks(self): prob = om.Problem() model = prob.model model.add_subsystem('px', om.IndepVarComp(name="x", val=np.ones((2, )))) model.add_subsystem('comp', DoubleArrayComp()) model.add_subsystem('ks', om.KSComp(width=2)) model.connect('px.x', 'comp.x1') model.connect('comp.y2', 'ks.g') model.add_design_var('px.x') model.add_objective('comp.y1') model.add_constraint('ks.KS', upper=0.0) prob.setup() prob.run_driver() assert_near_equal(max(prob['comp.y2']), prob['ks.KS'][0])
def test_lower_flag(self): prob = om.Problem() model = prob.model model.add_subsystem('comp', om.ExecComp('y = 3.0*x', x=np.zeros((2, )), y=np.zeros((2, ))), promotes_inputs=['x']) model.add_subsystem('ks', om.KSComp(width=2)) model.connect('comp.y', 'ks.g') model.ks.options['lower_flag'] = True prob.setup() prob.set_val('x', np.array([5.0, 4.0])) prob.run_model() assert_near_equal(prob.get_val('ks.KS'), [[-12.0]])
def test_basic(self): import numpy as np import openmdao.api as om prob = om.Problem() model = prob.model model.add_subsystem('px', om.IndepVarComp('x', val=np.array([5.0, 4.0]))) model.add_subsystem('comp', om.ExecComp('y = 3.0*x', x=np.zeros((2, )), y=np.zeros((2, )))) model.add_subsystem('ks', om.KSComp(width=2)) model.connect('px.x', 'comp.x') model.connect('comp.y', 'ks.g') prob.setup() prob.run_model() assert_rel_error(self, prob['ks.KS'][0], 15.0)
def test_upper(self): import numpy as np import openmdao.api as om prob = om.Problem() model = prob.model model.add_subsystem('comp', om.ExecComp('y = 3.0*x', x=np.zeros((2, )), y=np.zeros((2, ))), promotes_inputs=['x']) model.add_subsystem('ks', om.KSComp(width=2)) model.connect('comp.y', 'ks.g') model.ks.options['upper'] = 16.0 prob.setup() prob.set_val('x', np.array([5.0, 4.0])) prob.run_model() assert_near_equal(prob['ks.KS'], np.array([[-1.0]]))
def test_vectorized(self): import numpy as np import openmdao.api as om prob = om.Problem() model = prob.model model.add_subsystem('px', om.IndepVarComp('x', val=np.array([[5.0, 4.0], [10.0, 8.0]]))) model.add_subsystem('comp', om.ExecComp('y = 3.0*x', x=np.zeros((2, 2)), y=np.zeros((2, 2)))) model.add_subsystem('ks', om.KSComp(width=2, vec_size=2)) model.connect('px.x', 'comp.x') model.connect('comp.y', 'ks.g') prob.setup() prob.run_model() assert_near_equal(prob['ks.KS'], np.array([[15], [30]]))
def test_units(self): import openmdao.api as om from openmdao.utils.units import convert_units n = 10 model = om.Group() model.add_subsystem('indep', om.IndepVarComp('x', val=range(n), units='ft')) model.add_subsystem('ks', om.KSComp(width=n, units='m')) model.connect('indep.x', 'ks.g') prob = om.Problem(model=model) prob.setup() prob.run_model() # KS is expressed in meters, while the independent variable 'x' is in feet assert_near_equal(prob['ks.KS'][0], convert_units(max(prob['indep.x']), 'ft', 'm'), tolerance=1e-8) assert_near_equal(convert_units(prob['ks.KS'][0], 'm', 'ft'), max(prob['indep.x']), tolerance=1e-8)
def test_bad_units(self): with self.assertRaises(ValueError) as ctx: om.KSComp(units='wtfu') self.assertEqual(str(ctx.exception), "The units 'wtfu' are invalid.")
def setup(self): nn = self.options['num_nodes'] nv = self.options['num_vehicles'] separation = self.options['separation'] self.add_subsystem('vehicles', Vehicles(num_nodes=nn, num_v=nv), promotes=['*']) if separation == 'grid': self.add_subsystem('distances', GridDistComp(num_nodes=nn, num_v=nv, limit=limit), promotes=['*']) elif separation == 'pairwise': self.add_subsystem('demux', DeMux(num_nodes=nn, nv=nv), promotes=['*']) nc = 0 for i in range(nv): for k in range(i + 1, nv): self.add_subsystem( 'dist_%i_%i' % (i, k), AllDistComp(num_nodes=nn, limit=limit)) self.connect('x_%i' % i, 'dist_%i_%i.x1' % (i, k)) self.connect('y_%i' % i, 'dist_%i_%i.y1' % (i, k)) self.connect('x_%i' % k, 'dist_%i_%i.x2' % (i, k)) self.connect('y_%i' % k, 'dist_%i_%i.y2' % (i, k)) nc += 1 if aggregate == 'mine': self.add_subsystem('aggmux', AggregateMux(num_nodes=nn, nc=nc)) nc = 0 for i in range(nv): for k in range(i + 1, nv): self.connect('dist_%i_%i.dist' % (i, k), 'aggmux.dist_%i' % nc) nc += 1 elif aggregate == 'ks': self.add_subsystem('aggmux', AggregateMuxKS(num_nodes=nn, nc=nc)) nc = 0 for i in range(nv): for k in range(i + 1, nv): self.connect('dist_%i_%i.dist' % (i, k), 'aggmux.dist_%i' % nc) nc += 1 self.add_subsystem('ks', om.KSComp(width=nc, vec_size=nn)) self.connect('aggmux.distks', 'ks.g') elif aggregate == 'none': self.add_subsystem('aggmux', AggregateMuxKS(num_nodes=nn, nc=nc)) nc = 0 for i in range(nv): for k in range(i + 1, nv): self.connect('dist_%i_%i.dist' % (i, k), 'aggmux.dist_%i' % nc) nc += 1
def setup(self): E = self.options['E'] L = self.options['L'] b = self.options['b'] volume = self.options['volume'] max_bending = self.options['max_bending'] num_elements = self.options['num_elements'] num_nodes = num_elements + 1 num_cp = self.options['num_cp'] num_load_cases = self.options['num_load_cases'] parallel_derivs = self.options['parallel_derivs'] inputs_comp = om.IndepVarComp() inputs_comp.add_output('h_cp', shape=num_cp) self.add_subsystem('inputs_comp', inputs_comp) comp = om.BsplinesComp(num_control_points=num_cp, num_points=num_elements, in_name='h_cp', out_name='h') self.add_subsystem('interp', comp) I_comp = MomentOfInertiaComp(num_elements=num_elements, b=b) self.add_subsystem('I_comp', I_comp) comp = LocalStiffnessMatrixComp(num_elements=num_elements, E=E, L=L) self.add_subsystem('local_stiffness_matrix_comp', comp) # Parallel Subsystem for load cases. par = self.add_subsystem('parallel', om.ParallelGroup()) # Determine how to split cases up over the available procs. nprocs = self.comm.size divide = divide_cases(num_load_cases, nprocs) for j, this_proc in enumerate(divide): num_rhs = len(this_proc) name = 'sub_%d' % j sub = par.add_subsystem(name, om.Group()) # Load is a sinusoidal distributed force of varying spatial frequency. force_vector = np.zeros((2 * num_nodes, num_rhs)) for i, k in enumerate(this_proc): end = 1.5 * np.pi if num_load_cases > 1: end += k * 0.5 * np.pi / (num_load_cases - 1) x = np.linspace(0, end, num_nodes) f = -np.sin(x) force_vector[0:-1:2, i] = f comp = MultiStatesComp(num_elements=num_elements, force_vector=force_vector, num_rhs=num_rhs) sub.add_subsystem('states_comp', comp) comp = MultiDisplacementsComp(num_elements=num_elements, num_rhs=num_rhs) sub.add_subsystem('displacements_comp', comp) comp = MultiStressComp(num_elements=num_elements, E=E, num_rhs=num_rhs) sub.add_subsystem('stress_comp', comp) self.connect('local_stiffness_matrix_comp.K_local', 'parallel.%s.states_comp.K_local' % name) for k in range(num_rhs): sub.connect('states_comp.d_%d' % k, 'displacements_comp.d_%d' % k) sub.connect('displacements_comp.displacements_%d' % k, 'stress_comp.displacements_%d' % k) comp = om.KSComp(width=num_elements) comp.options['upper'] = max_bending sub.add_subsystem('KS_%d' % k, comp) sub.connect('stress_comp.stress_%d' % k, 'KS_%d.g' % k) if parallel_derivs: color = 'red_%d' % k else: color = None sub.add_constraint('KS_%d.KS' % k, upper=0.0, parallel_deriv_color=color) comp = VolumeComp(num_elements=num_elements, b=b, L=L) self.add_subsystem('volume_comp', comp) self.connect('inputs_comp.h_cp', 'interp.h_cp') self.connect('interp.h', 'I_comp.h') self.connect('I_comp.I', 'local_stiffness_matrix_comp.I') self.connect('interp.h', 'volume_comp.h') self.add_design_var('inputs_comp.h_cp', lower=1e-2, upper=10.) self.add_objective('volume_comp.volume')