def test_with_subsolves(self): prob = Problem() model = prob.model = DoubleSellar() g1 = model.get_subsystem('g1') g1.nonlinear_solver = NewtonSolver() g1.nonlinear_solver.options['rtol'] = 1.0e-5 g1.linear_solver = DirectSolver() g2 = model.get_subsystem('g2') g2.nonlinear_solver = NewtonSolver() g2.nonlinear_solver.options['rtol'] = 1.0e-5 g2.linear_solver = DirectSolver() model.nonlinear_solver = NewtonSolver() model.linear_solver = ScipyIterativeSolver() model.nonlinear_solver.options['solve_subsystems'] = True model.nonlinear_solver.options['max_sub_solves'] = 4 ls = model.nonlinear_solver.linesearch = ArmijoGoldsteinLS(bound_enforcement='vector') # This is pretty bogus, but it ensures that we get a few LS iterations. ls.options['c'] = 100.0 prob.set_solver_print(level=0) prob.setup() prob.run_model() assert_rel_error(self, prob['g1.y1'], 0.64, .00001) assert_rel_error(self, prob['g1.y2'], 0.80, .00001) assert_rel_error(self, prob['g2.y1'], 0.64, .00001) assert_rel_error(self, prob['g2.y2'], 0.80, .00001)
def test_feature_boundscheck_vector(self): top = Problem() top.model = Group() top.model.add_subsystem('px', IndepVarComp('x', np.ones((3, 1)))) top.model.add_subsystem('comp', ImplCompTwoStatesArrays()) top.model.connect('px.x', 'comp.x') top.model.nonlinear_solver = NewtonSolver() top.model.nonlinear_solver.options['maxiter'] = 10 top.model.linear_solver = ScipyIterativeSolver() ls = top.model.nonlinear_solver.linesearch = BoundsEnforceLS() ls.options['bound_enforcement'] = 'vector' top.setup(check=False) # Test lower bounds: should go to the lower bound and stall top['px.x'] = 2.0 top['comp.y'] = 0. top['comp.z'] = 1.6 top.run_model() assert_rel_error(self, top['comp.z'][0], [1.5], 1e-8) assert_rel_error(self, top['comp.z'][1], [1.5], 1e-8) assert_rel_error(self, top['comp.z'][2], [1.5], 1e-8)
def test_feature_boundscheck_wall(self): import numpy as np from openmdao.api import Problem, Group, IndepVarComp, NewtonSolver, ScipyIterativeSolver, BoundsEnforceLS from openmdao.test_suite.components.implicit_newton_linesearch import ImplCompTwoStatesArrays top = Problem() top.model = Group() top.model.add_subsystem('px', IndepVarComp('x', np.ones((3, 1)))) top.model.add_subsystem('comp', ImplCompTwoStatesArrays()) top.model.connect('px.x', 'comp.x') top.model.nonlinear_solver = NewtonSolver() top.model.nonlinear_solver.options['maxiter'] = 10 top.model.linear_solver = ScipyIterativeSolver() ls = top.model.nonlinear_solver.linesearch = BoundsEnforceLS() ls.options['bound_enforcement'] = 'wall' top.setup(check=False) # Test upper bounds: should go to the upper bound and stall top['px.x'] = 0.5 top['comp.y'] = 0. top['comp.z'] = 2.4 top.run_model() assert_rel_error(self, top['comp.z'][0], [2.6], 1e-8) assert_rel_error(self, top['comp.z'][1], [2.5], 1e-8) assert_rel_error(self, top['comp.z'][2], [2.65], 1e-8)
def test_feature_print_bound_enforce(self): import numpy as np from openmdao.api import Problem, Group, IndepVarComp, NewtonSolver, ScipyIterativeSolver, BoundsEnforceLS from openmdao.test_suite.components.implicit_newton_linesearch import ImplCompTwoStatesArrays top = Problem() top.model = Group() top.model.add_subsystem('px', IndepVarComp('x', np.ones((3, 1)))) top.model.add_subsystem('comp', ImplCompTwoStatesArrays()) top.model.connect('px.x', 'comp.x') newt = top.model.nonlinear_solver = NewtonSolver() top.model.nonlinear_solver.options['maxiter'] = 2 top.model.linear_solver = ScipyIterativeSolver() ls = newt.linesearch = BoundsEnforceLS(bound_enforcement='vector') ls.options['print_bound_enforce'] = True top.set_solver_print(level=2) top.setup() # Test lower bounds: should go to the lower bound and stall top['px.x'] = 2.0 top['comp.y'] = 0. top['comp.z'] = 1.6 top.run_model() assert_rel_error(self, top['comp.z'][0], [1.5], 1e-8) assert_rel_error(self, top['comp.z'][1], [1.5], 1e-8) assert_rel_error(self, top['comp.z'][2], [1.5], 1e-8)
def test_feature_boundscheck_scalar(self): import numpy as np from openmdao.api import Problem, Group, IndepVarComp, NewtonSolver, ScipyIterativeSolver, BoundsEnforceLS from openmdao.test_suite.components.implicit_newton_linesearch import ImplCompTwoStatesArrays top = Problem() top.model = Group() top.model.add_subsystem('px', IndepVarComp('x', np.ones((3, 1)))) top.model.add_subsystem('comp', ImplCompTwoStatesArrays()) top.model.connect('px.x', 'comp.x') top.model.nonlinear_solver = NewtonSolver() top.model.nonlinear_solver.options['maxiter'] = 10 top.model.linear_solver = ScipyIterativeSolver() ls = top.model.nonlinear_solver.linesearch = BoundsEnforceLS() ls.options['bound_enforcement'] = 'scalar' top.setup(check=False) top.run_model() # Test lower bounds: should stop just short of the lower bound top['px.x'] = 2.0 top['comp.y'] = 0. top['comp.z'] = 1.6 top.run_model() print(top['comp.z'][0]) print(top['comp.z'][1]) print(top['comp.z'][2])
def test_options(self): """Verify that the SciPy solver specific options are declared.""" group = Group() group.linear_solver = ScipyIterativeSolver() assert (group.linear_solver.options['solver'] == gmres)
def test_feature_simple(self): """Tests feature for adding a Scipy GMRES solver and calculating the derivatives.""" # Tests derivatives on a simple comp that defines compute_jacvec. prob = Problem() model = prob.model = Group() model.add_subsystem('x_param', IndepVarComp('length', 3.0), promotes=['length']) model.add_subsystem('mycomp', TestExplCompSimpleDense(), promotes=['length', 'width', 'area']) model.linear_solver = ScipyIterativeSolver() prob.set_solver_print(level=0) prob.setup(check=False, mode='fwd') prob['width'] = 2.0 prob.run_model() of = ['area'] wrt = ['length'] J = prob.compute_totals(of=of, wrt=wrt, return_format='flat_dict') assert_rel_error(self, J['area', 'length'][0][0], 2.0, 1e-6)
def test_solve_linear_scipy(self): """Solve implicit system with ScipyKrylov.""" # use ScipyIterativeSolver here to check for deprecation warning and verify that the deprecated # class still gets the right answer without duplicating this test. msg = "ScipyIterativeSolver is deprecated. Use ScipyKrylov instead." with assert_warning(DeprecationWarning, msg): group = TestImplicitGroup(lnSolverClass=lambda : ScipyIterativeSolver(solver=self.linear_solver_name)) p = Problem(group) p.setup(check=False) p.set_solver_print(level=0) # Conclude setup but don't run model. p.final_setup() d_inputs, d_outputs, d_residuals = group.get_linear_vectors() # forward d_residuals.set_const(1.0) d_outputs.set_const(0.0) group.run_solve_linear(['linear'], 'fwd') output = d_outputs._data assert_rel_error(self, output, group.expected_solution, 1e-15) # reverse d_outputs.set_const(1.0) d_residuals.set_const(0.0) group.run_solve_linear(['linear'], 'rev') output = d_residuals._data assert_rel_error(self, output, group.expected_solution, 1e-15)
def test_specify_subgroup_solvers(self): from openmdao.api import Problem, NewtonSolver, ScipyIterativeSolver, DirectSolver, NonlinearBlockGS, LinearBlockGS from openmdao.test_suite.components.double_sellar import DoubleSellar prob = Problem() model = prob.model = DoubleSellar() # each SubSellar group converges itself g1 = model.get_subsystem('g1') g1.nonlinear_solver = NewtonSolver() g1.linear_solver = DirectSolver() # used for derivatives g2 = model.get_subsystem('g2') g2.nonlinear_solver = NewtonSolver() g2.linear_solver = DirectSolver() # Converge the outer loop with Gauss Seidel, with a looser tolerance. model.nonlinear_solver = NonlinearBlockGS() model.nonlinear_solver.options['rtol'] = 1.0e-5 model.linear_solver = ScipyIterativeSolver() model.linear_solver.precon = LinearBlockGS() prob.setup() prob.run_model() assert_rel_error(self, prob['g1.y1'], 0.64, .00001) assert_rel_error(self, prob['g1.y2'], 0.80, .00001) assert_rel_error(self, prob['g2.y1'], 0.64, .00001) assert_rel_error(self, prob['g2.y2'], 0.80, .00001)
def setUp(self): top = Problem() top.model = Group() top.model.add_subsystem('px', IndepVarComp('x', 1.0)) top.model.add_subsystem('comp', ImplCompTwoStates()) top.model.connect('px.x', 'comp.x') top.model.nonlinear_solver = NewtonSolver() top.model.nonlinear_solver.options['maxiter'] = 10 top.model.linear_solver = ScipyIterativeSolver() top.setup(check=False) self.top = top
def test_specify_solver(self): import numpy as np from openmdao.api import Problem, Group, IndepVarComp, ScipyIterativeSolver, \ NonlinearBlockGS, 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 = ScipyIterativeSolver() 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 setUp(self): top = Problem() top.model = Group() top.model.add_subsystem('px', IndepVarComp('x', np.ones((3, 1)))) top.model.add_subsystem('comp', ImplCompTwoStatesArrays()) top.model.connect('px.x', 'comp.x') top.model.nonlinear_solver = NewtonSolver() top.model.nonlinear_solver.options['maxiter'] = 10 top.model.linear_solver = ScipyIterativeSolver() top.set_solver_print(level=0) top.setup(check=False) self.top = top self.ub = np.array([2.6, 2.5, 2.65])
def test_specify_precon(self): import numpy as np from openmdao.api import Problem, Group, IndepVarComp, ScipyIterativeSolver, NewtonSolver, \ LinearBlockGS, 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']) prob.model.nonlinear_solver = NewtonSolver() prob.model.linear_solver = ScipyIterativeSolver() prob.model.linear_solver.precon = LinearBlockGS() prob.model.linear_solver.precon.options['maxiter'] = 2 prob.setup() prob.run_model() assert_rel_error(self, prob['y1'], 25.58830273, .00001) assert_rel_error(self, prob['y2'], 12.05848819, .00001)
def setup(self): self.add_subsystem('px', IndepVarComp('x', 1.0), promotes=['x']) self.add_subsystem('pz', IndepVarComp('z', np.array([5.0, 2.0])), promotes=['z']) mda = self.add_subsystem('mda', Group(), promotes=['x', 'z', 'y1', 'y2']) mda.add_subsystem('d1', SellarDis1withDerivatives(), promotes=['x', 'z', 'y1', 'y2']) mda.add_subsystem('d2', SellarDis2withDerivatives(), promotes=['z', 'y1', 'y2']) self.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']) self.add_subsystem('con_cmp1', ExecComp('con1 = 3.16 - y1'), promotes=['con1', 'y1']) self.add_subsystem('con_cmp2', ExecComp('con2 = y2 - 24.0'), promotes=['con2', 'y2']) self.linear_solver = ScipyIterativeSolver() self.nonlinear_solver = self.metadata['nonlinear_solver'] if self.metadata['nl_atol']: self.nonlinear_solver.options['atol'] = self.metadata['nl_atol'] if self.metadata['nl_maxiter']: self.nonlinear_solver.options['maxiter'] = self.metadata[ 'nl_maxiter'] self.linear_solver = self.metadata['linear_solver'] if self.metadata['ln_atol']: self.linear_solver.options['atol'] = self.metadata['ln_atol'] if self.metadata['ln_maxiter']: self.linear_solver.options['maxiter'] = self.metadata['ln_maxiter']
def test_specify_solver(self): prob = Problem() model = prob.model = SellarDerivatives() model.nonlinear_solver = newton = NewtonSolver() # using a different linear solver for Newton with a looser tolerance newton.linear_solver = ScipyIterativeSolver() newton.linear_solver.options['atol'] = 1e-4 # used for analytic derivatives model.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_atol(self): 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 = ScipyIterativeSolver() model.linear_solver.options['atol'] = 1.0e-20 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.61001055699, .00001) assert_rel_error(self, J['obj', 'z'][0][1], 1.78448533563, .00001)
def initialize(self): self.metadata.declare('nonlinear_solver', default=NewtonSolver(), desc='Nonlinear solver for Sellar MDA') self.metadata.declare('nl_atol', default=None, desc='User-specified atol for nonlinear solver.') self.metadata.declare('nl_maxiter', default=None, desc='Iteration limit for nonlinear solver.') self.metadata.declare('linear_solver', default=ScipyIterativeSolver(), desc='Linear solver') self.metadata.declare('ln_atol', default=None, desc='User-specified atol for linear solver.') self.metadata.declare('ln_maxiter', default=None, desc='Iteration limit for linear solver.')
def test_feature_specification(self): top = Problem() top.model = Group() top.model.add_subsystem('px', IndepVarComp('x', 1.0)) top.model.add_subsystem('comp', ImplCompTwoStates()) top.model.connect('px.x', 'comp.x') top.model.nonlinear_solver = NewtonSolver() top.model.nonlinear_solver.options['maxiter'] = 10 top.model.linear_solver = ScipyIterativeSolver() ls = top.model.nonlinear_solver.linesearch = ArmijoGoldsteinLS() ls.options['maxiter'] = 10 top.setup(check=False) top['px.x'] = 2.0 top['comp.y'] = 0.0 top['comp.z'] = 1.6 top.run_model() assert_rel_error(self, top['comp.z'], 1.5, 1e-8)
def test_solve_linear_scipy(self): """Solve implicit system with ScipyKrylov.""" # use ScipyIterativeSolver here to check for deprecation warning and verify that the deprecated # class still gets the right answer without duplicating this test. with warnings.catch_warnings(record=True) as w: group = TestImplicitGroup( lnSolverClass=lambda: ScipyIterativeSolver(solver=self. linear_solver_name)) self.assertEqual(len(w), 1) self.assertTrue(issubclass(w[0].category, DeprecationWarning)) self.assertEqual( str(w[0].message), "ScipyIterativeSolver is deprecated. Use ScipyKrylov instead.") p = Problem(group) p.setup(check=False) p.set_solver_print(level=0) # Conclude setup but don't run model. p.final_setup() d_inputs, d_outputs, d_residuals = group.get_linear_vectors() # forward d_residuals.set_const(1.0) d_outputs.set_const(0.0) group.run_solve_linear(['linear'], 'fwd') output = d_outputs._data assert_rel_error(self, output[1], group.expected_solution[0], 1e-15) assert_rel_error(self, output[5], group.expected_solution[1], 1e-15) # reverse d_outputs.set_const(1.0) d_residuals.set_const(0.0) group.run_solve_linear(['linear'], 'rev') output = d_residuals._data assert_rel_error(self, output[1], group.expected_solution[0], 1e-15) assert_rel_error(self, output[5], group.expected_solution[1], 1e-15)
def setup(self): self.add_subsystem('px', IndepVarComp('x', 1.0)) self.add_subsystem('pz', IndepVarComp('z', np.array([5.0, 2.0]))) self.add_subsystem('d1', SellarDis1withDerivatives()) self.add_subsystem('d2', SellarDis2withDerivatives()) self.add_subsystem( 'obj_cmp', ExecComp('obj = x**2 + z[1] + y1 + exp(-y2)', z=np.array([0.0, 0.0]), x=0.0)) self.add_subsystem('con_cmp1', ExecComp('con1 = 3.16 - y1')) self.add_subsystem('con_cmp2', ExecComp('con2 = y2 - 24.0')) self.connect('px.x', ['d1.x', 'obj_cmp.x']) self.connect('pz.z', ['d1.z', 'd2.z', 'obj_cmp.z']) self.connect('d1.y1', ['d2.y1', 'obj_cmp.y1', 'con_cmp1.y1']) self.connect('d2.y2', ['d1.y2', 'obj_cmp.y2', 'con_cmp2.y2']) self.nonlinear_solver = NonlinearBlockGS() self.linear_solver = ScipyIterativeSolver()
def configure(self): self.sub.linear_solver = ScipyIterativeSolver() self.sub.state_eq_group.linear_solver = ScipyIterativeSolver()
var_factory=lambda: numpy.zeros(vec_size)) cname = "C%d" % (num_comps - 1) self.add_objective("%s.o0" % cname) self.add_constraint("%s.o1" % cname, lower=0.0) if 'petsc' in sys.argv: vec_class = PetscVector else: vec_class = DefaultVector p = Problem() g = p.model if 'gmres' in sys.argv: from openmdao.solvers.linear.scipy_iter_solver import ScipyIterativeSolver p.root.linear_solver = ScipyIterativeSolver() g.add_subsystem("P", IndepVarComp('x', numpy.ones(vec_size))) g.add_design_var("P.x") par = g.add_subsystem("par", ParallelGroup()) for pt in range(pts): ptname = "G%d" % pt ptg = par.add_subsystem(ptname, SubGroup()) #create_dyncomps(ptg, num_comps, 2, 2, 2, #var_factory=lambda: numpy.zeros(vec_size)) g.connect("P.x", "par.%s.C0.i0" % ptname) #cname = ptname + '.' + "C%d"%(num_comps-1) #g.add_objective("par.%s.o0" % cname)
def configure(self): self.mda.linear_solver = ScipyIterativeSolver() self.mda.nonlinear_solver = NonlinearBlockGS()
def test_deep_analysis_error_iprint(self): class ImplCompTwoStatesAE(ImplicitComponent): def setup(self): self.add_input('x', 0.5) self.add_output('y', 0.0) self.add_output('z', 2.0, lower=1.5, upper=2.5) self.maxiter = 10 self.atol = 1.0e-12 self.declare_partials(of='*', wrt='*') self.counter = 0 def apply_nonlinear(self, inputs, outputs, residuals): """ Don't solve; just calculate the residual. """ x = inputs['x'] y = outputs['y'] z = outputs['z'] residuals['y'] = y - x - 2.0*z residuals['z'] = x*z + z - 4.0 self.counter += 1 if self.counter > 5 and self.counter < 11: raise AnalysisError('catch me') def linearize(self, inputs, outputs, jac): """ Analytical derivatives. """ # Output equation jac[('y', 'x')] = -1.0 jac[('y', 'y')] = 1.0 jac[('y', 'z')] = -2.0 # State equation jac[('z', 'z')] = -inputs['x'] + 1.0 jac[('z', 'x')] = -outputs['z'] top = Problem() top.model = Group() top.model.add_subsystem('px', IndepVarComp('x', 7.0)) sub = top.model.add_subsystem('sub', Group()) sub.add_subsystem('comp', ImplCompTwoStatesAE()) top.model.connect('px.x', 'sub.comp.x') top.model.nonlinear_solver = NewtonSolver() top.model.nonlinear_solver.options['maxiter'] = 2 top.model.nonlinear_solver.options['solve_subsystems'] = True top.model.linear_solver = ScipyIterativeSolver() sub.nonlinear_solver = NewtonSolver() sub.nonlinear_solver.options['maxiter'] = 2 sub.linear_solver = ScipyIterativeSolver() ls = top.model.nonlinear_solver.linesearch = ArmijoGoldsteinLS(bound_enforcement='wall') ls.options['maxiter'] = 5 ls.options['alpha'] = 10.0 ls.options['retry_on_analysis_error'] = True ls.options['c'] = 10000.0 top.setup(check=False) top.set_solver_print(level=2) stdout = sys.stdout strout = StringIO() sys.stdout = strout try: top.run_model() finally: sys.stdout = stdout output = strout.getvalue().split('\n') self.assertTrue(output[26].startswith('| LS: AG 5'))
def test_analysis_error(self): class ParaboloidAE(ExplicitComponent): """ Evaluates the equation f(x,y) = (x-3)^2 + xy + (y+4)^2 - 3 This version raises an analysis error if x < 2.0 The AE in ParaboloidAE stands for AnalysisError.""" def __init__(self): super(ParaboloidAE, self).__init__() self.fail_hard = False def setup(self): self.add_input('x', val=0.0) self.add_input('y', val=0.0) self.add_output('f_xy', val=0.0) self.declare_partials(of='*', wrt='*') def compute(self, inputs, outputs): """f(x,y) = (x-3)^2 + xy + (y+4)^2 - 3 Optimal solution (minimum): x = 6.6667; y = -7.3333 """ x = inputs['x'] y = inputs['y'] if x < 1.75: raise AnalysisError('Try Again.') outputs['f_xy'] = (x-3.0)**2 + x*y + (y+4.0)**2 - 3.0 def compute_partials(self, inputs, partials): """ Jacobian for our paraboloid.""" x = inputs['x'] y = inputs['y'] partials['f_xy','x'] = 2.0*x - 6.0 + y partials['f_xy','y'] = 2.0*y + 8.0 + x top = Problem() top.model = Group() top.model.add_subsystem('px', IndepVarComp('x', 1.0)) top.model.add_subsystem('comp', ImplCompTwoStates()) top.model.add_subsystem('par', ParaboloidAE()) top.model.connect('px.x', 'comp.x') top.model.connect('comp.z', 'par.x') top.model.nonlinear_solver = NewtonSolver() top.model.nonlinear_solver.options['maxiter'] = 1 top.model.linear_solver = ScipyIterativeSolver() ls = top.model.nonlinear_solver.linesearch = ArmijoGoldsteinLS(bound_enforcement='vector') ls.options['maxiter'] = 10 ls.options['alpha'] = 1.0 top.set_solver_print(level=0) top.setup(check=False) # Test lower bound: should go as far as it can without going past 1.75 and triggering an # AnalysisError. It doesn't do a great job, so ends up at 1.8 instead of 1.75 top['px.x'] = 2.0 top['comp.y'] = 0.0 top['comp.z'] = 2.1 top.run_model() assert_rel_error(self, top['comp.z'], 1.8, 1e-8) # Test the behavior with the switch turned off. top = Problem() top.model = Group() top.model.add_subsystem('px', IndepVarComp('x', 1.0)) top.model.add_subsystem('comp', ImplCompTwoStates()) top.model.add_subsystem('par', ParaboloidAE()) top.model.connect('px.x', 'comp.x') top.model.connect('comp.z', 'par.x') top.model.nonlinear_solver = NewtonSolver() top.model.nonlinear_solver.options['maxiter'] = 1 top.model.linear_solver = ScipyIterativeSolver() ls = top.model.nonlinear_solver.linesearch = ArmijoGoldsteinLS(bound_enforcement='vector') ls.options['maxiter'] = 10 ls.options['alpha'] = 1.0 ls.options['retry_on_analysis_error'] = False top.set_solver_print(level=0) top.setup(check=False) top['px.x'] = 2.0 top['comp.y'] = 0.0 top['comp.z'] = 2.1 with self.assertRaises(AnalysisError) as context: top.run_model() self.assertEqual(str(context.exception), 'Try Again.')