def test_deprecated_ks_component(self): # run same test as above, only with the deprecated component, # to ensure we get the warning and the correct answer. # self-contained, to be removed when class name goes away. from openmdao.components.ks_comp import KSComponent # deprecated prob = Problem() prob.model = model = Group() model.add_subsystem('px', IndepVarComp(name="x", val=np.ones((2, )))) model.add_subsystem('comp', DoubleArrayComp()) msg = "'KSComponent' has been deprecated. Use 'KSComp' instead." with assert_warning(DeprecationWarning, msg): model.add_subsystem('ks', KSComponent(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(check=False) prob.run_driver() assert_rel_error(self, max(prob['comp.y2']), prob['ks.KS'][0])
def test_double_arraycomp(self): # Mainly testing an old bug in the array return for multiple arrays group = Group() group.add_subsystem('x_param1', IndepVarComp('x1', np.ones((2))), promotes=['x1']) group.add_subsystem('x_param2', IndepVarComp('x2', np.ones((2))), promotes=['x2']) mycomp = group.add_subsystem('mycomp', DoubleArrayComp(), promotes=['x1', 'x2', 'y1', 'y2']) prob = Problem() model = prob.model = group model.linear_solver = self.linear_solver_class() prob.set_solver_print(level=0) prob.setup(check=False, mode='fwd') prob.run_model() Jbase = mycomp.JJ of = ['y1', 'y2'] wrt = ['x1', 'x2'] J = prob.compute_totals(of=of, wrt=wrt, return_format='flat_dict') diff = np.linalg.norm(J['y1', 'x1'] - Jbase[0:2, 0:2]) assert_near_equal(diff, 0.0, 1e-8) diff = np.linalg.norm(J['y1', 'x2'] - Jbase[0:2, 2:4]) assert_near_equal(diff, 0.0, 1e-8) diff = np.linalg.norm(J['y2', 'x1'] - Jbase[2:4, 0:2]) assert_near_equal(diff, 0.0, 1e-8) diff = np.linalg.norm(J['y2', 'x2'] - Jbase[2:4, 2:4]) assert_near_equal(diff, 0.0, 1e-8)
def test_vector_bounds_inf(self): # make sure no overflow when there is no specified upper/lower bound and significatn scaling prob = om.Problem() model = prob.model model.add_subsystem('px', om.IndepVarComp(name="x", val=np.ones( (2, )))) comp = model.add_subsystem('comp', DoubleArrayComp()) model.connect('px.x', 'comp.x1') model.add_design_var('px.x', ref=np.array([.1, 1e-6])) model.add_constraint('comp.y2', ref=np.array([.2, 2e-6])) prob.setup() desvars = model.get_design_vars() self.assertFalse(np.any(np.isinf(desvars['px.x']['upper']))) self.assertFalse(np.any(np.isinf(-desvars['px.x']['lower']))) responses = prob.model.get_responses() self.assertFalse(np.any(np.isinf(responses['comp.y2']['upper']))) self.assertFalse(np.any(np.isinf(-responses['comp.y2']['lower'])))
def test_vector_scaled_derivs(self): prob = om.Problem() model = prob.model model.add_subsystem('px', om.IndepVarComp(name="x", val=np.ones( (2, )))) comp = model.add_subsystem('comp', DoubleArrayComp()) model.connect('px.x', 'comp.x1') model.add_design_var('px.x', ref=np.array([2.0, 3.0]), ref0=np.array([0.5, 1.5])) model.add_objective('comp.y1', ref=np.array([[7.0, 11.0]]), ref0=np.array([5.2, 6.3])) model.add_constraint('comp.y2', lower=0.0, upper=1.0, ref=np.array([[2.0, 4.0]]), ref0=np.array([1.2, 2.3])) prob.setup() prob.run_driver() with assert_warning( OMDeprecationWarning, "'global_names' is deprecated in calls to _compute_totals. Use 'use_abs_names' instead." ): derivs = prob.driver._compute_totals(of=['comp.y1'], wrt=['px.x'], global_names=True, return_format='dict') oscale = np.array([1.0 / (7.0 - 5.2), 1.0 / (11.0 - 6.3)]) iscale = np.array([2.0 - 0.5, 3.0 - 1.5]) J = comp.JJ[0:2, 0:2] # doing this manually so that I don't inadvertantly make an error in the vector math in both the code and test. J[0, 0] *= oscale[0] * iscale[0] J[0, 1] *= oscale[0] * iscale[1] J[1, 0] *= oscale[1] * iscale[0] J[1, 1] *= oscale[1] * iscale[1] assert_near_equal(J, derivs['comp.y1']['px.x'], 1.0e-3) obj = prob.driver.get_objective_values() obj_base = np.array([(prob['comp.y1'][0] - 5.2) / (7.0 - 5.2), (prob['comp.y1'][1] - 6.3) / (11.0 - 6.3)]) assert_near_equal(obj['comp.y1'], obj_base, 1.0e-3) con = prob.driver.get_constraint_values() con_base = np.array([(prob['comp.y2'][0] - 1.2) / (2.0 - 1.2), (prob['comp.y2'][1] - 2.3) / (4.0 - 2.3)]) assert_near_equal(con['comp.y2'], con_base, 1.0e-3)
def test_vector_scaled_derivs(self): prob = Problem() prob.model = model = Group() model.add_subsystem('px', IndepVarComp(name="x", val=np.ones((2, )))) comp = model.add_subsystem('comp', DoubleArrayComp()) model.connect('px.x', 'comp.x1') model.add_design_var('px.x', ref=np.array([2.0, 3.0]), ref0=np.array([0.5, 1.5])) model.add_objective('comp.y1', ref=np.array([[7.0, 11.0]]), ref0=np.array([5.2, 6.3])) model.add_constraint('comp.y2', lower=0.0, upper=1.0, ref=np.array([[2.0, 4.0]]), ref0=np.array([1.2, 2.3])) prob.setup(check=False) prob.run_driver() derivs = prob.driver._compute_totals(of=['comp.y1'], wrt=['px.x'], return_format='dict') oscale = np.array([1.0 / (7.0 - 5.2), 1.0 / (11.0 - 6.3)]) iscale = np.array([2.0 - 0.5, 3.0 - 1.5]) J = comp.JJ[0:2, 0:2] # doing this manually so that I don't inadvertantly make an error in the vector math in both the code and test. J[0, 0] *= oscale[0] * iscale[0] J[0, 1] *= oscale[0] * iscale[1] J[1, 0] *= oscale[1] * iscale[0] J[1, 1] *= oscale[1] * iscale[1] assert_rel_error(self, J, derivs['comp.y1']['px.x'], 1.0e-3) obj = prob.driver.get_objective_values() obj_base = np.array([(prob['comp.y1'][0] - 5.2) / (7.0 - 5.2), (prob['comp.y1'][1] - 6.3) / (11.0 - 6.3)]) assert_rel_error(self, obj['comp.y1'], obj_base, 1.0e-3) con = prob.driver.get_constraint_values() con_base = np.array([(prob['comp.y2'][0] - 1.2) / (2.0 - 1.2), (prob['comp.y2'][1] - 2.3) / (4.0 - 2.3)]) assert_rel_error(self, con['comp.y2'], con_base, 1.0e-3)
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_basic_ks(self): prob = Problem() prob.model = model = Group() model.add_subsystem('px', IndepVarComp(name="x", val=np.ones((2, )))) model.add_subsystem('comp', DoubleArrayComp()) model.add_subsystem('ks', KSComponent(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(check=False) prob.run_driver() assert_rel_error(self, max(prob['comp.y2']), prob['ks.KS'])