def test_specify_solver(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() 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_specify_precon(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']) 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 test_specify_solver(self): from openmdao.api import Problem, NewtonSolver, ScipyIterativeSolver, DirectSolver from openmdao.test_suite.components.sellar import SellarDerivatives 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_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 test_feature_boundscheck_basic(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.model.nonlinear_solver.linesearch = BoundsEnforceLS() 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_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.')
def configure(self): self.sub.linear_solver = ScipyIterativeSolver() self.sub.state_eq_group.linear_solver = ScipyIterativeSolver()
def configure(self): self.mda.linear_solver = ScipyIterativeSolver() self.mda.nonlinear_solver = NonlinearBlockGS()
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)