def _setup_model(self, solver_class): prob = om.Problem() model = prob.model model.add_subsystem('px', om.IndepVarComp('x', 1.0), promotes=['x']) model.add_subsystem('pz', om.IndepVarComp('z', np.array([5.0, 2.0])), promotes=['z']) proms = [ 'x', 'z', 'y1', 'state_eq.y2_actual', 'state_eq.y2_command', 'd1.y2', 'd2.y2' ] sub = model.add_subsystem('sub', om.Group(), promotes=proms) subgrp = sub.add_subsystem( 'state_eq_group', om.Group(), promotes=['state_eq.y2_actual', 'state_eq.y2_command']) subgrp.add_subsystem('state_eq', StateConnection()) sub.add_subsystem('d1', SellarDis1withDerivatives(), promotes=['x', 'z', 'y1']) sub.add_subsystem('d2', SellarDis2withDerivatives(), promotes=['z', 'y1']) model.connect('state_eq.y2_command', 'd1.y2') model.connect('d2.y2', 'state_eq.y2_actual') model.add_subsystem('obj_cmp', om.ExecComp('obj = x**2 + z[1] + y1 + exp(-y2)', z=np.array([0.0, 0.0]), x=0.0, y1=0.0, y2=0.0), promotes=['x', 'z', 'y1', 'obj']) model.connect('d2.y2', 'obj_cmp.y2') model.add_subsystem('con_cmp1', om.ExecComp('con1 = 3.16 - y1'), promotes=['con1', 'y1']) model.add_subsystem('con_cmp2', om.ExecComp('con2 = y2 - 24.0'), promotes=['con2']) # splice a group containing discrete vars into the model model.add_subsystem('discrete_g', InternalDiscreteGroup()) model.connect('d2.y2', 'discrete_g.x') model.connect('discrete_g.y', 'con_cmp2.y2') model.nonlinear_solver = solver_class() prob.set_solver_print(level=0) prob.setup() return prob
def test_aitken(self): prob = om.Problem() model = prob.model model.add_subsystem('px', om.IndepVarComp('x', 1.0), promotes=['x']) model.add_subsystem('pz', om.IndepVarComp('z', np.array([5.0, 2.0])), promotes=['z']) p1 = model.add_subsystem('p1', om.ParallelGroup(), promotes=['*']) p1.add_subsystem('d1a', SellarDis1withDerivatives(), promotes=['x', 'z']) p1.add_subsystem('d1b', SellarDis1withDerivatives(), promotes=['x', 'z']) p2 = model.add_subsystem('p2', om.ParallelGroup(), promotes=['*']) p2.add_subsystem('d2a', SellarDis2withDerivatives(), promotes=['z']) p2.add_subsystem('d2b', SellarDis2withDerivatives(), promotes=['z']) model.connect('d1a.y1', 'd2a.y1') model.connect('d1b.y1', 'd2b.y1') model.connect('d2a.y2', 'd1a.y2') model.connect('d2b.y2', 'd1b.y2') model.nonlinear_solver = om.NonlinearBlockGS() prob.setup() prob.set_solver_print(level=2) model.nonlinear_solver.options['use_aitken'] = True # Set one branch of Sellar close to the solution. prob.set_val('d2b.y2', 12.05848815) prob.set_val('d1b.y1', 25.58830237) prob.run_model() print(prob.get_val('d1a.y1', get_remote=True)) print(prob.get_val('d2a.y1', get_remote=True)) print(prob.get_val('d1b.y2', get_remote=True)) print(prob.get_val('d2b.y2', get_remote=True)) assert_near_equal(prob.get_val('d1a.y1', get_remote=True), 25.58830273, .00001) assert_near_equal(prob.get_val('d1b.y1', get_remote=True), 25.58830273, .00001) assert_near_equal(prob.get_val('d2a.y2', get_remote=True), 12.05848819, .00001) assert_near_equal(prob.get_val('d2b.y2', get_remote=True), 12.05848819, .00001) # Test that Aitken accelerated the convergence, normally takes 7. self.assertTrue(model.nonlinear_solver._iter_count == 6)
def test_api_on_subsystems(self): prob = Problem() model = prob.model = Group() model.add_subsystem('px', IndepVarComp('x', 1.0)) model.add_subsystem('pz', IndepVarComp('z', np.array([5.0, 2.0]))) model.add_subsystem('d1', SellarDis1withDerivatives()) model.add_subsystem('d2', SellarDis2withDerivatives()) model.add_subsystem( 'obj_cmp', ExecComp('obj = x**2 + z[1] + y1 + exp(-y2)', z=np.array([0.0, 0.0]), x=0.0)) model.add_subsystem('con_cmp1', ExecComp('con1 = 3.16 - y1')) model.add_subsystem('con_cmp2', ExecComp('con2 = y2 - 24.0')) model.connect('px.x', ['d1.x', 'obj_cmp.x']) model.connect('pz.z', ['d1.z', 'd2.z', 'obj_cmp.z']) model.connect('d1.y1', ['d2.y1', 'obj_cmp.y1', 'con_cmp1.y1']) model.connect('d2.y2', ['d1.y2', 'obj_cmp.y2', 'con_cmp2.y2']) model.nonlinear_solver = NonlinearBlockGS() model.linear_solver = ScipyKrylov() px = prob.model.px px.add_design_var('x', lower=-100, upper=100) pz = prob.model.pz pz.add_design_var('z', lower=-100, upper=100) obj = prob.model.obj_cmp obj.add_objective('obj') con_comp1 = prob.model.con_cmp1 con_comp1.add_constraint('con1') con_comp2 = prob.model.con_cmp2 con_comp2.add_constraint('con2') prob.setup(check=False) des_vars = prob.model.get_design_vars() obj = prob.model.get_objectives() constraints = prob.model.get_constraints() self.assertEqual(set(des_vars.keys()), {'px.x', 'pz.z'}) self.assertEqual(set(obj.keys()), { 'obj_cmp.obj', }) self.assertEqual(set(constraints.keys()), {'con_cmp1.con1', 'con_cmp2.con2'})
def setup(self): # construct the Sellar model with `y1` and `y2` as independent variables self.set_input_defaults('x', 5.) self.set_input_defaults('y1', 5.) self.set_input_defaults('y2', 5.) self.set_input_defaults('z', np.array([2., 0.])) self.add_subsystem('d1', SellarDis1withDerivatives(), promotes_inputs=['x', 'z', 'y2']) self.add_subsystem('d2', SellarDis2withDerivatives(), promotes_inputs=['y1', 'z']) self.add_subsystem('obj_cmp', om.ExecComp('obj = x**2 + z[1] + y1 + exp(-y2)', x=0., z=np.array([0., 0.])), promotes_inputs=['x', 'z', 'y1', 'y2']) self.add_subsystem('con_cmp1', om.ExecComp('con1 = 3.16 - y1'), promotes_inputs=['y1']) self.add_subsystem('con_cmp2', om.ExecComp('con2 = y2 - 24.0'), promotes_inputs=['y2']) # rather than create a cycle by connecting d1.y1 to d2.y1 and d2.y2 to d1.y2 # we will constrain y1 and y2 to be equal for the two disciplines equal = om.EQConstraintComp() self.add_subsystem('equal', equal, promotes_inputs=[('lhs:y1', 'y1'), ('lhs:y2', 'y2')]) equal.add_eq_output('y1', add_constraint=True) equal.add_eq_output('y2', add_constraint=True) self.connect('d1.y1', 'equal.rhs:y1') self.connect('d2.y2', 'equal.rhs:y2') # the driver will effectively solve the cycle # by satisfying the equality constraints self.add_design_var('x', lower=0., upper=5.) self.add_design_var('y1', lower=0., upper=5.) self.add_design_var('y2', lower=0., upper=5.) self.add_design_var('z', lower=np.array([-5., 0.]), upper=np.array([5., 5.])) self.add_objective('obj_cmp.obj') self.add_constraint('con_cmp1.con1', upper=0.) self.add_constraint('con_cmp2.con2', upper=0.)
def __init__(self, units=None, scaling=None, **kwargs): super(SubSellar, self).__init__(**kwargs) self.add_subsystem('d1', SellarDis1withDerivatives(units=units, scaling=scaling), promotes=['x', 'z', 'y1', 'y2']) self.add_subsystem('d2', SellarDis2withDerivatives(units=units, scaling=scaling), promotes=['z', 'y1', 'y2'])
def test_cs_around_broyden_linesearch(self): prob = om.Problem() model = prob.model sub = model.add_subsystem('sub', om.ParallelGroup(), promotes=['*']) model.add_subsystem('px', om.IndepVarComp('x', 1.0), promotes=['x']) model.add_subsystem('pz', om.IndepVarComp('z', np.array([5.0, 2.0])), promotes=['z']) sub.add_subsystem('d1', SellarDis1withDerivatives(), promotes=['x', 'z', 'y1', 'y2']) sub.add_subsystem('d2', SellarDis2withDerivatives(), promotes=['z', 'y1', 'y2']) model.add_subsystem('obj_cmp', om.ExecComp('obj = x**2 + z[1] + y1 + exp(-y2)', z=np.array([0.0, 0.0]), x=0.0), promotes=['obj', 'x', 'z', 'y1', 'y2']) model.add_subsystem('con_cmp1', om.ExecComp('con1 = 3.16 - y1'), promotes=['con1', 'y1']) model.add_subsystem('con_cmp2', om.ExecComp('con2 = y2 - 24.0'), promotes=['con2', 'y2']) sub.nonlinear_solver = om.BroydenSolver() sub.nonlinear_solver.linesearch = om.BoundsEnforceLS( bound_enforcement='vector') sub.linear_solver = om.DirectSolver() model.linear_solver = om.DirectSolver() prob.model.add_design_var('x', lower=-100, upper=100) prob.model.add_design_var('z', lower=-100, upper=100) prob.model.add_objective('obj') prob.model.add_constraint('con1', upper=0.0) prob.model.add_constraint('con2', upper=0.0) prob.setup(check=False, force_alloc_complex=True) prob.set_solver_print(level=2) prob.run_model() assert_rel_error(self, prob.get_val('y1', get_remote=True), 25.58830237, .00001) totals = prob.check_totals(method='cs', out_stream=None) for key, val in iteritems(totals): assert_rel_error(self, val['rel error'][0], 0.0, 1e-6)
def test_cs_around_broyden_compute_jac_dense(self): # Basic sellar test. prob = Problem() model = prob.model sub = model.add_subsystem('sub', Group(), promotes=['*']) model.add_subsystem('px', IndepVarComp('x', 1.0), promotes=['x']) model.add_subsystem('pz', IndepVarComp('z', np.array([5.0, 2.0])), promotes=['z']) sub.add_subsystem('d1', SellarDis1withDerivatives(), promotes=['x', 'z', 'y1', 'y2']) sub.add_subsystem('d2', SellarDis2withDerivatives(), promotes=['z', 'y1', 'y2']) model.add_subsystem('obj_cmp', ExecComp('obj = x**2 + z[1] + y1 + exp(-y2)', z=np.array([0.0, 0.0]), x=0.0), promotes=['obj', 'x', 'z', 'y1', 'y2']) model.add_subsystem('con_cmp1', ExecComp('con1 = 3.16 - y1'), promotes=['con1', 'y1']) model.add_subsystem('con_cmp2', ExecComp('con2 = y2 - 24.0'), promotes=['con2', 'y2']) sub.nonlinear_solver = BroydenSolver() sub.linear_solver = DirectSolver() model.linear_solver = DirectSolver() prob.model.add_design_var('x', lower=-100, upper=100) prob.model.add_design_var('z', lower=-100, upper=100) prob.model.add_objective('obj') prob.model.add_constraint('con1', upper=0.0) prob.model.add_constraint('con2', upper=0.0) prob.setup(check=False, force_alloc_complex=True) prob.set_solver_print(level=0) prob.run_model() sub.nonlinear_solver.options['compute_jacobian'] = True totals = prob.check_totals(method='cs', out_stream=None) for key, val in iteritems(totals): assert_rel_error(self, val['rel error'][0], 0.0, 1e-6)
def test_sellar_specify_linear_solver(self): prob = om.Problem() model = prob.model model.add_subsystem('px', om.IndepVarComp('x', 1.0), promotes=['x']) model.add_subsystem('pz', om.IndepVarComp('z', np.array([5.0, 2.0])), promotes=['z']) proms = ['x', 'z', 'y1', 'state_eq.y2_actual', 'state_eq.y2_command', 'd1.y2', 'd2.y2'] sub = model.add_subsystem('sub', om.Group(), promotes=proms) subgrp = sub.add_subsystem('state_eq_group', om.Group(), promotes=['state_eq.y2_actual', 'state_eq.y2_command']) subgrp.linear_solver = om.ScipyKrylov() subgrp.add_subsystem('state_eq', StateConnection()) sub.add_subsystem('d1', SellarDis1withDerivatives(), promotes=['x', 'z', 'y1']) sub.add_subsystem('d2', SellarDis2withDerivatives(), promotes=['z', 'y1']) model.connect('state_eq.y2_command', 'd1.y2') model.connect('d2.y2', 'state_eq.y2_actual') model.add_subsystem('obj_cmp', om.ExecComp('obj = x**2 + z[1] + y1 + exp(-y2)', z=np.array([0.0, 0.0]), x=0.0, y1=0.0, y2=0.0), promotes=['x', 'z', 'y1', 'obj']) model.connect('d2.y2', 'obj_cmp.y2') model.add_subsystem('con_cmp1', om.ExecComp('con1 = 3.16 - y1'), promotes=['con1', 'y1']) model.add_subsystem('con_cmp2', om.ExecComp('con2 = y2 - 24.0'), promotes=['con2']) model.connect('d2.y2', 'con_cmp2.y2') model.nonlinear_solver = om.NewtonSolver(solve_subsystems=False) # Use bad settings for this one so that problem doesn't converge. # That way, we test that we are really using Newton's Lin Solver # instead. model.linear_solver = om.ScipyKrylov() model.linear_solver.options['maxiter'] = 1 # The good solver model.nonlinear_solver.linear_solver = om.ScipyKrylov() prob.set_solver_print(level=0) prob.setup() prob.run_model() assert_near_equal(prob.get_val('y1'), 25.58830273, .00001) assert_near_equal(prob['state_eq.y2_command'], 12.05848819, .00001) # Make sure we aren't iterating like crazy self.assertLess(model.nonlinear_solver._iter_count, 8) self.assertEqual(model.linear_solver._iter_count, 0) self.assertGreater(model.nonlinear_solver.linear_solver._iter_count, 0)
def __init__(self): super(SellarModified, self).__init__() self.add_subsystem('d1', SellarDis1withDerivatives(), promotes=['x', 'z', 'y1', 'y2']) self.add_subsystem('d2', SellarDis2withDerivatives(), promotes=['z', 'y1', 'y2']) self.nonlinear_solver = NonlinearBlockGS() self.linear_solver = ScipyKrylov()
def test_sellar(self, vec_class): # Basic sellar test. if not vec_class: raise unittest.SkipTest("PETSc is not installed") prob = self.prob model = prob.model = Group() model.add_subsystem('px', IndepVarComp('x', 1.0), promotes=['x']) model.add_subsystem('pz', IndepVarComp('z', np.array([5.0, 2.0])), promotes=['z']) model.add_subsystem('d1', SellarDis1withDerivatives(), promotes=['x', 'z', 'y1', 'y2']) model.add_subsystem('d2', SellarDis2withDerivatives(), promotes=['z', 'y1', 'y2']) model.add_subsystem('obj_cmp', ExecComp('obj = x**2 + z[1] + y1 + exp(-y2)', z=np.array([0.0, 0.0]), x=0.0), promotes=['obj', 'x', 'z', 'y1', 'y2']) model.add_subsystem('con_cmp1', ExecComp('con1 = 3.16 - y1'), promotes=['con1', 'y1']) model.add_subsystem('con_cmp2', ExecComp('con2 = y2 - 24.0'), promotes=['con2', 'y2']) nlbgs = prob.model.nonlinear_solver = NonlinearBlockGS() # Had to make this step larger so that solver would reconverge adequately. model.approx_totals(method='cs', step=1.0e-1) prob.setup(check=False, vector_class=vec_class) prob.set_solver_print(level=0) prob.run_model() assert_rel_error(self, prob['y1'], 25.58830273, .00001) assert_rel_error(self, prob['y2'], 12.05848819, .00001) wrt = ['z'] of = ['obj'] J = prob.compute_totals(of=of, wrt=wrt, return_format='flat_dict') assert_rel_error(self, J['obj', 'z'][0][0], 9.61001056, .00001) assert_rel_error(self, J['obj', 'z'][0][1], 1.78448534, .00001)
def test_feature_maxiter(self): import numpy as np import openmdao.api as om from openmdao.test_suite.components.sellar import SellarDis1withDerivatives, SellarDis2withDerivatives prob = om.Problem() model = prob.model model.add_subsystem('d1', SellarDis1withDerivatives(), promotes=['x', 'z', 'y1', 'y2']) model.add_subsystem('d2', SellarDis2withDerivatives(), promotes=['z', 'y1', 'y2']) model.add_subsystem('obj_cmp', om.ExecComp('obj = x**2 + z[1] + y1 + exp(-y2)', z=np.array([0.0, 0.0]), x=0.0), promotes=['obj', 'x', 'z', 'y1', 'y2']) model.add_subsystem('con_cmp1', om.ExecComp('con1 = 3.16 - y1'), promotes=['con1', 'y1']) model.add_subsystem('con_cmp2', om.ExecComp('con2 = y2 - 24.0'), promotes=['con2', 'y2']) prob.setup() nlbgs = model.nonlinear_solver = om.NonlinearBlockGS() #basic test of number of iterations nlbgs.options['maxiter'] = 1 prob.run_model() self.assertEqual(model.nonlinear_solver._iter_count, 1) nlbgs.options['maxiter'] = 5 prob.run_model() self.assertEqual(model.nonlinear_solver._iter_count, 5) #test of number of iterations AND solution after exit at maxiter prob.set_val('x', 1.) prob.set_val('z', np.array([5.0, 2.0])) nlbgs.options['maxiter'] = 3 prob.set_solver_print() prob.run_model() assert_near_equal(prob.get_val('y1'), 25.58914915, .00001) assert_near_equal(prob.get_val('y2'), 12.05857185, .00001) self.assertEqual(model.nonlinear_solver._iter_count, 3)
def setup(self): self.set_input_defaults('x', 1.0) self.set_input_defaults('z', np.array([5.0, 2.0])) cycle = self.add_subsystem('cycle', om.Group(), promotes=['*']) cycle.add_subsystem('d1', SellarDis1withDerivatives(), promotes_inputs=['x', 'z', 'y2'], promotes_outputs=['y1']) cycle.add_subsystem('d2', SellarDis2withDerivatives(), promotes_inputs=['z', 'y1'], promotes_outputs=['y2']) cycle.linear_solver = om.ScipyKrylov() cycle.nonlinear_solver = om.NewtonSolver(solve_subsystems=False)
def test_newton_with_densejac_under_full_model_fd(self): # Basic sellar test. prob = Problem() model = prob.model = Group() sub = model.add_subsystem('sub', Group(), promotes=['*']) model.add_subsystem('px', IndepVarComp('x', 1.0), promotes=['x']) model.add_subsystem('pz', IndepVarComp('z', np.array([5.0, 2.0])), promotes=['z']) sub.add_subsystem('d1', SellarDis1withDerivatives(), promotes=['x', 'z', 'y1', 'y2']) sub.add_subsystem('d2', SellarDis2withDerivatives(), promotes=['z', 'y1', 'y2']) model.add_subsystem('obj_cmp', ExecComp('obj = x**2 + z[1] + y1 + exp(-y2)', z=np.array([0.0, 0.0]), x=0.0), promotes=['obj', 'x', 'z', 'y1', 'y2']) model.add_subsystem('con_cmp1', ExecComp('con1 = 3.16 - y1'), promotes=['con1', 'y1']) model.add_subsystem('con_cmp2', ExecComp('con2 = y2 - 24.0'), promotes=['con2', 'y2']) sub.nonlinear_solver = NewtonSolver() sub.linear_solver = ScipyKrylov() model.jacobian = DenseJacobian() model.approx_totals(method='fd', step=1e-5) prob.setup(check=False) prob.set_solver_print(level=0) prob.run_model() assert_rel_error(self, prob['y1'], 25.58830273, .00001) assert_rel_error(self, prob['y2'], 12.05848819, .00001) wrt = ['z'] of = ['obj'] J = prob.compute_totals(of=of, wrt=wrt, return_format='flat_dict') assert_rel_error(self, J['obj', 'z'][0][0], 9.61001056, .00001) assert_rel_error(self, J['obj', 'z'][0][1], 1.78448534, .00001)
def __init__(self, units=None, scaling=None, **kwargs): super().__init__(**kwargs) self.add_subsystem('d1', SellarDis1withDerivatives(units=units, scaling=scaling), promotes=['x', 'z', 'y1', 'y2']) self.add_subsystem('d2', SellarDis2withDerivatives(units=units, scaling=scaling), promotes=['z', 'y1', 'y2']) if units: # auto_ivc update requires this since two 'z' inputs have different units self.set_input_defaults('z', units='ft')
def test_specify_ksp_type(self): import numpy as np from openmdao.api import Problem, Group, IndepVarComp, NonlinearBlockGS, PETScKrylov, \ ExecComp from openmdao.test_suite.components.sellar import SellarDis1withDerivatives, \ SellarDis2withDerivatives prob = Problem() model = prob.model = Group() model.add_subsystem('px', IndepVarComp('x', 1.0), promotes=['x']) model.add_subsystem('pz', IndepVarComp('z', np.array([5.0, 2.0])), promotes=['z']) model.add_subsystem('d1', SellarDis1withDerivatives(), promotes=['x', 'z', 'y1', 'y2']) model.add_subsystem('d2', SellarDis2withDerivatives(), promotes=['z', 'y1', 'y2']) model.add_subsystem('obj_cmp', ExecComp('obj = x**2 + z[1] + y1 + exp(-y2)', z=np.array([0.0, 0.0]), x=0.0), promotes=['obj', 'x', 'z', 'y1', 'y2']) model.add_subsystem('con_cmp1', ExecComp('con1 = 3.16 - y1'), promotes=['con1', 'y1']) model.add_subsystem('con_cmp2', ExecComp('con2 = y2 - 24.0'), promotes=['con2', 'y2']) model.nonlinear_solver = NonlinearBlockGS() model.linear_solver = PETScKrylov() model.linear_solver.options['ksp_type'] = 'gmres' prob.setup() prob.run_model() wrt = ['z'] of = ['obj'] J = prob.compute_totals(of=of, wrt=wrt, return_format='flat_dict') assert_rel_error(self, J['obj', 'z'][0][0], 9.61001056, .00001) assert_rel_error(self, J['obj', 'z'][0][1], 1.78448534, .00001)
def test_complex_step(self): prob = om.Problem() model = prob.model sub = model.add_subsystem('sub', om.ParallelGroup(), promotes=['*']) model.add_subsystem('px', om.IndepVarComp('x', 1.0), promotes=['x']) model.add_subsystem('pz', om.IndepVarComp('z', np.array([5.0, 2.0])), promotes=['z']) sub.add_subsystem('d1', SellarDis1withDerivatives(), promotes=['x', 'z', 'y1', 'y2']) sub.add_subsystem('d2', SellarDis2withDerivatives(), promotes=['z', 'y1', 'y2']) model.add_subsystem('obj_cmp', om.ExecComp('obj = x**2 + z[1] + y1 + exp(-y2)', z=np.array([0.0, 0.0]), x=0.0), promotes=['obj', 'x', 'z', 'y1', 'y2']) model.add_subsystem('con_cmp1', om.ExecComp('con1 = 3.16 - y1'), promotes=['con1', 'y1']) model.add_subsystem('con_cmp2', om.ExecComp('con2 = y2 - 24.0'), promotes=['con2', 'y2']) sub.nonlinear_solver = om.BroydenSolver() sub.linear_solver = om.DirectSolver() model.linear_solver = om.DirectSolver() prob.model.add_design_var('x', lower=-100, upper=100) prob.model.add_design_var('z', lower=-100, upper=100) prob.model.add_objective('obj') prob.model.add_constraint('con1', upper=0.0) prob.model.add_constraint('con2', upper=0.0) prob.setup(check=False, force_alloc_complex=True) prob.set_solver_print(level=0) prob.run_model() totals = prob.check_totals(method='cs', out_stream=None) for key, val in totals.items(): assert_near_equal(val['rel error'][0], 0.0, 1e-7)
def test_nonlinear_analysis_error(self): prob = om.Problem() model = prob.model model.add_subsystem('px', om.IndepVarComp('x', 1.0), promotes=['x']) model.add_subsystem('pz', om.IndepVarComp('z', np.array([5.0, 2.0])), promotes=['z']) p1 = model.add_subsystem('p1', om.ParallelGroup(), promotes=['*']) p1.add_subsystem('d1a', SellarDis1withDerivatives(), promotes=['x', 'z']) p1.add_subsystem('d1b', SellarDis1withDerivatives(), promotes=['x', 'z']) p2 = model.add_subsystem('p2', om.ParallelGroup(), promotes=['*']) p2.add_subsystem('d2a', SellarDis2withDerivatives(), promotes=['z']) p2.add_subsystem('d2b', SellarDis2withDerivatives(), promotes=['z']) model.connect('d1a.y1', 'd2a.y1') model.connect('d1b.y1', 'd2b.y1') model.connect('d2a.y2', 'd1a.y2') model.connect('d2b.y2', 'd1b.y2') model.nonlinear_solver = om.NonlinearBlockGS(maxiter=2, err_on_non_converge=True) prob.setup() prob.set_solver_print(level=2) # Set one branch of Sellar close to the solution. prob.set_val('d2b.y2', 12.05848815) prob.set_val('d1b.y1', 25.58830237) # test if the analysis error is raised properly on all procs try: prob.run_model() except om.AnalysisError as err: self.assertEqual(str(err), "Solver 'NL: NLBGS' on system '' failed to converge in 2 iterations.") else: self.fail("expected AnalysisError")
def test_feature_maxiter(self): import numpy as np from openmdao.api import Problem, Group, IndepVarComp, ExecComp, LinearBlockGS, NonlinearBlockGS from openmdao.test_suite.components.sellar import SellarDis1withDerivatives, SellarDis2withDerivatives prob = Problem() model = prob.model = Group() model.add_subsystem('px', IndepVarComp('x', 1.0), promotes=['x']) model.add_subsystem('pz', IndepVarComp('z', np.array([5.0, 2.0])), promotes=['z']) model.add_subsystem('d1', SellarDis1withDerivatives(), promotes=['x', 'z', 'y1', 'y2']) model.add_subsystem('d2', SellarDis2withDerivatives(), promotes=['z', 'y1', 'y2']) model.add_subsystem('obj_cmp', ExecComp('obj = x**2 + z[1] + y1 + exp(-y2)', z=np.array([0.0, 0.0]), x=0.0), promotes=['obj', 'x', 'z', 'y1', 'y2']) model.add_subsystem('con_cmp1', ExecComp('con1 = 3.16 - y1'), promotes=['con1', 'y1']) model.add_subsystem('con_cmp2', ExecComp('con2 = y2 - 24.0'), promotes=['con2', 'y2']) model.nonlinear_solver = NonlinearBlockGS() model.linear_solver = LinearBlockGS() model.linear_solver.options['maxiter'] = 2 prob.setup() prob.run_model() wrt = ['z'] of = ['obj'] J = prob.compute_totals(of=of, wrt=wrt, return_format='flat_dict') assert_rel_error(self, J['obj', 'z'][0][0], 9.60230118004, .00001) assert_rel_error(self, J['obj', 'z'][0][1], 1.78022500547, .00001)
def test_specify_precon_left(self): import numpy as np from openmdao.api import Problem, Group, IndepVarComp, DirectSolver, PETScKrylov, \ NewtonSolver, ExecComp from openmdao.test_suite.components.sellar import SellarDis1withDerivatives, \ SellarDis2withDerivatives prob = Problem() model = prob.model = Group() model.add_subsystem('px', IndepVarComp('x', 1.0), promotes=['x']) model.add_subsystem('pz', IndepVarComp('z', np.array([5.0, 2.0])), promotes=['z']) model.add_subsystem('d1', SellarDis1withDerivatives(), promotes=['x', 'z', 'y1', 'y2']) model.add_subsystem('d2', SellarDis2withDerivatives(), promotes=['z', 'y1', 'y2']) model.add_subsystem('obj_cmp', ExecComp('obj = x**2 + z[1] + y1 + exp(-y2)', z=np.array([0.0, 0.0]), x=0.0), promotes=['obj', 'x', 'z', 'y1', 'y2']) model.add_subsystem('con_cmp1', ExecComp('con1 = 3.16 - y1'), promotes=['con1', 'y1']) model.add_subsystem('con_cmp2', ExecComp('con2 = y2 - 24.0'), promotes=['con2', 'y2']) model.nonlinear_solver = NewtonSolver() model.linear_solver = PETScKrylov() model.linear_solver.precon = DirectSolver() model.linear_solver.options['precon_side'] = 'left' model.linear_solver.options['ksp_type'] = 'richardson' prob.setup() prob.run_model() assert_rel_error(self, prob['y1'], 25.58830273, .00001) assert_rel_error(self, prob['y2'], 12.05848819, .00001)
def test_feature_linear_solver(self): import numpy as np from openmdao.api import Problem, Group, IndepVarComp, NewtonSolver, LinearBlockGS, \ ExecComp, DirectSolver from openmdao.test_suite.components.sellar import SellarDis1withDerivatives, \ SellarDis2withDerivatives prob = Problem() model = prob.model model.add_subsystem('px', IndepVarComp('x', 1.0), promotes=['x']) model.add_subsystem('pz', IndepVarComp('z', np.array([5.0, 2.0])), promotes=['z']) model.add_subsystem('d1', SellarDis1withDerivatives(), promotes=['x', 'z', 'y1', 'y2']) model.add_subsystem('d2', SellarDis2withDerivatives(), promotes=['z', 'y1', 'y2']) model.add_subsystem('obj_cmp', ExecComp('obj = x**2 + z[1] + y1 + exp(-y2)', z=np.array([0.0, 0.0]), x=0.0), promotes=['obj', 'x', 'z', 'y1', 'y2']) model.add_subsystem('con_cmp1', ExecComp('con1 = 3.16 - y1'), promotes=['con1', 'y1']) model.add_subsystem('con_cmp2', ExecComp('con2 = y2 - 24.0'), promotes=['con2', 'y2']) model.linear_solver = LinearBlockGS() nlgbs = model.nonlinear_solver = NewtonSolver() nlgbs.linear_solver = DirectSolver() prob.setup() prob.run_model() assert_rel_error(self, prob['y1'], 25.58830273, .00001) assert_rel_error(self, prob['y2'], 12.05848819, .00001)
def test_feature_linear_solver(self): import numpy as np import openmdao.api as om from openmdao.test_suite.components.sellar import SellarDis1withDerivatives, \ SellarDis2withDerivatives prob = om.Problem() model = prob.model model.add_subsystem('px', om.IndepVarComp('x', 1.0), promotes=['x']) model.add_subsystem('pz', om.IndepVarComp('z', np.array([5.0, 2.0])), promotes=['z']) model.add_subsystem('d1', SellarDis1withDerivatives(), promotes=['x', 'z', 'y1', 'y2']) model.add_subsystem('d2', SellarDis2withDerivatives(), promotes=['z', 'y1', 'y2']) model.add_subsystem('obj_cmp', om.ExecComp('obj = x**2 + z[1] + y1 + exp(-y2)', z=np.array([0.0, 0.0]), x=0.0), promotes=['obj', 'x', 'z', 'y1', 'y2']) model.add_subsystem('con_cmp1', om.ExecComp('con1 = 3.16 - y1'), promotes=['con1', 'y1']) model.add_subsystem('con_cmp2', om.ExecComp('con2 = y2 - 24.0'), promotes=['con2', 'y2']) model.linear_solver = om.LinearBlockGS() newton = model.nonlinear_solver = om.NewtonSolver( solve_subsystems=False) newton.linear_solver = om.DirectSolver() prob.setup() prob.run_model() assert_near_equal(prob['y1'], 25.58830273, .00001) assert_near_equal(prob['y2'], 12.05848819, .00001)
def test_feature_rtol(self): import numpy as np import openmdao.api as om from openmdao.test_suite.components.sellar import SellarDis1withDerivatives, SellarDis2withDerivatives prob = om.Problem() model = prob.model model.add_subsystem('px', om.IndepVarComp('x', 1.0), promotes=['x']) model.add_subsystem('pz', om.IndepVarComp('z', np.array([5.0, 2.0])), promotes=['z']) model.add_subsystem('d1', SellarDis1withDerivatives(), promotes=['x', 'z', 'y1', 'y2']) model.add_subsystem('d2', SellarDis2withDerivatives(), promotes=['z', 'y1', 'y2']) model.add_subsystem('obj_cmp', om.ExecComp('obj = x**2 + z[1] + y1 + exp(-y2)', z=np.array([0.0, 0.0]), x=0.0), promotes=['obj', 'x', 'z', 'y1', 'y2']) model.add_subsystem('con_cmp1', om.ExecComp('con1 = 3.16 - y1'), promotes=['con1', 'y1']) model.add_subsystem('con_cmp2', om.ExecComp('con2 = y2 - 24.0'), promotes=['con2', 'y2']) model.nonlinear_solver = om.NonlinearBlockGS() model.linear_solver = om.LinearBlockGS() model.linear_solver.options['rtol'] = 1.0e-3 prob.setup() prob.run_model() wrt = ['z'] of = ['obj'] J = prob.compute_totals(of=of, wrt=wrt, return_format='flat_dict') assert_rel_error(self, J['obj', 'z'][0][0], 9.61016296175, .00001) assert_rel_error(self, J['obj', 'z'][0][1], 1.78456955704, .00001)
def test_reading_metadata(self): prob = Problem() model = prob.model # the Sellar problem but with units model.add_subsystem('px', IndepVarComp('x', 1.0, units='m', lower=-1000, upper=1000), promotes=['x']) model.add_subsystem('pz', IndepVarComp('z', np.array([5.0, 2.0])), promotes=['z']) model.add_subsystem('d1', SellarDis1withDerivatives(), promotes=['x', 'z', 'y1', 'y2']) model.add_subsystem('d2', SellarDis2withDerivatives(), promotes=['z', 'y1', 'y2']) model.add_subsystem('obj_cmp', ExecComp('obj = x**2 + z[1] + y1 + exp(-y2)', z=np.array([0.0, 0.0]), x={'value': 0.0, 'units': 'm'}, y1={'units': 'm'}, y2={'units': 'cm'}), promotes=['obj', 'x', 'z', 'y1', 'y2']) model.add_subsystem('con_cmp1', ExecComp('con1 = 3.16 - y1'), promotes=['con1', 'y1']) model.add_subsystem('con_cmp2', ExecComp('con2 = y2 - 24.0'), promotes=['con2', 'y2']) model.nonlinear_solver = NonlinearBlockGS() model.linear_solver = LinearBlockGS() model.add_design_var('z', lower=np.array([-10.0, 0.0]), upper=np.array([10.0, 10.0])) model.add_design_var('x', lower=0.0, upper=10.0) model.add_objective('obj') model.add_constraint('con1', upper=0.0) model.add_constraint('con2', upper=0.0) prob.driver.add_recorder(self.recorder) prob.setup() prob.run_driver() prob.cleanup() cr = CaseReader(self.filename) self.assertEqual(cr.output2meta['x']['units'], 'm') self.assertEqual(cr.input2meta['obj_cmp.y1']['units'], 'm') self.assertEqual(cr.input2meta['obj_cmp.y2']['units'], 'cm') self.assertEqual(cr.input2meta['d1.x']['units'], None) self.assertEqual(cr.input2meta['d1.y1']['units'], None) self.assertEqual(cr.input2meta['d1.y2']['units'], None) self.assertEqual(cr.output2meta['x']['explicit'], True) self.assertEqual(cr.output2meta['x']['type'], {'output', 'desvar'}) self.assertEqual(cr.input2meta['obj_cmp.y1']['explicit'], True) self.assertEqual(cr.input2meta['obj_cmp.y2']['explicit'], True) self.assertEqual(cr.output2meta['x']['lower'], -1000) self.assertEqual(cr.output2meta['x']['upper'], 1000) self.assertEqual(cr.output2meta['y2']['upper'], None) self.assertEqual(cr.output2meta['y2']['lower'], None)
def setup_sellar_model_with_units(self): self.prob = Problem() model = self.prob.model = Group() model.add_subsystem('px', IndepVarComp('x', 1.0, units='m', lower=-1000, upper=1000), promotes=['x']) model.add_subsystem('pz', IndepVarComp('z', np.array([5.0, 2.0])), promotes=['z']) model.add_subsystem('d1', SellarDis1withDerivatives(), promotes=['x', 'z', 'y1', 'y2']) model.add_subsystem('d2', SellarDis2withDerivatives(), promotes=['z', 'y1', 'y2']) model.add_subsystem('obj_cmp', ExecComp('obj = x**2 + z[1] + y1 + exp(-y2)', z=np.array([0.0, 0.0]), x={ 'value': 0.0, 'units': 'm' }, y1={'units': 'm'}, y2={'units': 'cm'}), promotes=['obj', 'x', 'z', 'y1', 'y2']) model.add_subsystem('con_cmp1', ExecComp('con1 = 3.16 - y1'), promotes=['con1', 'y1']) model.add_subsystem('con_cmp2', ExecComp('con2 = y2 - 24.0'), promotes=['con2', 'y2']) self.prob.model.nonlinear_solver = NonlinearBlockGS() self.prob.model.linear_solver = LinearBlockGS() self.prob.model.add_design_var('z', lower=np.array([-10.0, 0.0]), upper=np.array([10.0, 10.0])) self.prob.model.add_design_var('x', lower=0.0, upper=10.0) self.prob.model.add_objective('obj') self.prob.model.add_constraint('con1', upper=0.0) self.prob.model.add_constraint('con2', upper=0.0)
def test_feature_maxiter(self): import numpy as np import openmdao.api as om from openmdao.test_suite.components.sellar import SellarDis1withDerivatives, SellarDis2withDerivatives prob = om.Problem() model = prob.model model.add_subsystem('d1', SellarDis1withDerivatives(), promotes=['x', 'z', 'y1', 'y2']) model.add_subsystem('d2', SellarDis2withDerivatives(), promotes=['z', 'y1', 'y2']) model.add_subsystem('obj_cmp', om.ExecComp('obj = x**2 + z[1] + y1 + exp(-y2)', z=np.array([0.0, 0.0]), x=0.0), promotes=['obj', 'x', 'z', 'y1', 'y2']) model.add_subsystem('con_cmp1', om.ExecComp('con1 = 3.16 - y1'), promotes=['con1', 'y1']) model.add_subsystem('con_cmp2', om.ExecComp('con2 = y2 - 24.0'), promotes=['con2', 'y2']) model.nonlinear_solver = om.NonlinearBlockGS() model.linear_solver = om.PETScKrylov() model.linear_solver.options['maxiter'] = 3 prob.setup() prob.set_val('x', 1.) prob.set_val('z', np.array([5.0, 2.0])) prob.run_model() wrt = ['z'] of = ['obj'] J = prob.compute_totals(of=of, wrt=wrt, return_format='flat_dict') assert_near_equal(J['obj', 'z'][0][0], 4.93218027, .00001) assert_near_equal(J['obj', 'z'][0][1], 1.73406455, .00001)
def test_feature_err_on_maxiter(self): import numpy as np from openmdao.api import Problem, Group, IndepVarComp, NewtonSolver, LinearBlockGS, \ ExecComp, AnalysisError, DirectSolver from openmdao.test_suite.components.sellar import SellarDis1withDerivatives, SellarDis2withDerivatives prob = Problem() model = prob.model model.add_subsystem('px', IndepVarComp('x', 1.0), promotes=['x']) model.add_subsystem('pz', IndepVarComp('z', np.array([5.0, 2.0])), promotes=['z']) model.add_subsystem('d1', SellarDis1withDerivatives(), promotes=['x', 'z', 'y1', 'y2']) model.add_subsystem('d2', SellarDis2withDerivatives(), promotes=['z', 'y1', 'y2']) model.add_subsystem('obj_cmp', ExecComp('obj = x**2 + z[1] + y1 + exp(-y2)', z=np.array([0.0, 0.0]), x=0.0), promotes=['obj', 'x', 'z', 'y1', 'y2']) model.add_subsystem('con_cmp1', ExecComp('con1 = 3.16 - y1'), promotes=['con1', 'y1']) model.add_subsystem('con_cmp2', ExecComp('con2 = y2 - 24.0'), promotes=['con2', 'y2']) model.linear_solver = DirectSolver() nlgbs = model.nonlinear_solver = NewtonSolver() nlgbs.options['maxiter'] = 1 nlgbs.options['err_on_maxiter'] = True prob.setup() try: prob.run_model() except AnalysisError: pass
def test_sellar_analysis_error(self): # Tests Sellar behavior when AnalysisError is raised. prob = Problem() model = prob.model model.add_subsystem('px', IndepVarComp('x', 1.0), promotes=['x']) model.add_subsystem('pz', IndepVarComp('z', np.array([5.0, 2.0])), promotes=['z']) model.add_subsystem('d1', SellarDis1withDerivatives(), promotes=['x', 'z', 'y1', 'y2']) model.add_subsystem('d2', SellarDis2withDerivatives(), promotes=['z', 'y1', 'y2']) model.add_subsystem('obj_cmp', ExecComp('obj = x**2 + z[1] + y1 + exp(-y2)', z=np.array([0.0, 0.0]), x=0.0), promotes=['obj', 'x', 'z', 'y1', 'y2']) model.add_subsystem('con_cmp1', ExecComp('con1 = 3.16 - y1'), promotes=['con1', 'y1']) model.add_subsystem('con_cmp2', ExecComp('con2 = y2 - 24.0'), promotes=['con2', 'y2']) nlgbs = model.nonlinear_solver = NonlinearBlockGS() nlgbs.options['maxiter'] = 2 nlgbs.options['err_on_maxiter'] = True prob.setup(check=False) prob.set_solver_print(level=0) try: prob.run_model() except AnalysisError as err: self.assertEqual( str(err), "Solver 'NL: NLBGS' on system '' failed to converge in 2 iterations." ) else: self.fail("expected AnalysisError")
def test_feature_err_on_non_converge(self): import numpy as np import openmdao.api as om from openmdao.test_suite.components.sellar import SellarDis1withDerivatives, SellarDis2withDerivatives prob = om.Problem() model = prob.model model.add_subsystem('px', om.IndepVarComp('x', 1.0), promotes=['x']) model.add_subsystem('pz', om.IndepVarComp('z', np.array([5.0, 2.0])), promotes=['z']) model.add_subsystem('d1', SellarDis1withDerivatives(), promotes=['x', 'z', 'y1', 'y2']) model.add_subsystem('d2', SellarDis2withDerivatives(), promotes=['z', 'y1', 'y2']) model.add_subsystem('obj_cmp', om.ExecComp('obj = x**2 + z[1] + y1 + exp(-y2)', z=np.array([0.0, 0.0]), x=0.0), promotes=['obj', 'x', 'z', 'y1', 'y2']) model.add_subsystem('con_cmp1', om.ExecComp('con1 = 3.16 - y1'), promotes=['con1', 'y1']) model.add_subsystem('con_cmp2', om.ExecComp('con2 = y2 - 24.0'), promotes=['con2', 'y2']) model.linear_solver = om.DirectSolver() newton = model.nonlinear_solver = om.NewtonSolver( solve_subsystems=False) newton.options['maxiter'] = 1 newton.options['err_on_non_converge'] = True prob.setup() try: prob.run_model() except om.AnalysisError: pass
def setup_sellar_grouped_model(self): self.prob = Problem() model = self.prob.model = Group() model.add_subsystem('px', IndepVarComp('x', 1.0), promotes=['x']) model.add_subsystem('pz', IndepVarComp('z', np.array([5.0, 2.0])), promotes=['z']) mda = model.add_subsystem('mda', Group(), promotes=['x', 'z', 'y1', 'y2']) mda.linear_solver = ScipyIterativeSolver() mda.add_subsystem('d1', SellarDis1withDerivatives(), promotes=['x', 'z', 'y1', 'y2']) mda.add_subsystem('d2', SellarDis2withDerivatives(), promotes=['z', 'y1', 'y2']) model.add_subsystem('obj_cmp', ExecComp('obj = x**2 + z[1] + y1 + exp(-y2)', z=np.array([0.0, 0.0]), x=0.0, y1=0.0, y2=0.0), promotes=['obj', 'x', 'z', 'y1', 'y2']) model.add_subsystem('con_cmp1', ExecComp('con1 = 3.16 - y1'), promotes=['con1', 'y1']) model.add_subsystem('con_cmp2', ExecComp('con2 = y2 - 24.0'), promotes=['con2', 'y2']) mda.nonlinear_solver = NonlinearBlockGS() model.linear_solver = ScipyIterativeSolver() model.add_design_var('z', lower=np.array([-10.0, 0.0]), upper=np.array([10.0, 10.0])) model.add_design_var('x', lower=0.0, upper=10.0) model.add_objective('obj') model.add_constraint('con1', upper=0.0) model.add_constraint('con2', upper=0.0)
def test_specify_precon_left(self): import numpy as np import openmdao.api as om from openmdao.test_suite.components.sellar import SellarDis1withDerivatives, \ SellarDis2withDerivatives prob = om.Problem() model = prob.model model.add_subsystem('d1', SellarDis1withDerivatives(), promotes=['x', 'z', 'y1', 'y2']) model.add_subsystem('d2', SellarDis2withDerivatives(), promotes=['z', 'y1', 'y2']) model.add_subsystem('obj_cmp', om.ExecComp('obj = x**2 + z[1] + y1 + exp(-y2)', z=np.array([0.0, 0.0]), x=0.0), promotes=['obj', 'x', 'z', 'y1', 'y2']) model.add_subsystem('con_cmp1', om.ExecComp('con1 = 3.16 - y1'), promotes=['con1', 'y1']) model.add_subsystem('con_cmp2', om.ExecComp('con2 = y2 - 24.0'), promotes=['con2', 'y2']) model.nonlinear_solver = om.NewtonSolver(solve_subsystems=False) model.linear_solver = om.PETScKrylov() model.linear_solver.precon = om.DirectSolver() model.linear_solver.options['precon_side'] = 'left' model.linear_solver.options['ksp_type'] = 'richardson' prob.setup() prob.set_val('x', 1.) prob.set_val('z', np.array([5.0, 2.0])) prob.run_model() assert_near_equal(prob.get_val('y1'), 25.58830273, .00001) assert_near_equal(prob.get_val('y2'), 12.05848819, .00001)