class HybridGSNewton(NonLinearSolver): def __init__(self): super(HybridGSNewton, self).__init__() self.nlgs = NLGaussSeidel() self.newton = Newton() self.nlgs.options['maxiter'] = 5 self.newton.options['maxiter'] = 1 self.nlgs.options['atol'] = 1e-10 self.newton.options['atol'] = 1e-10 def setup(self, sub): """ Initialize sub solvers. Args ---- sub: `System` System that owns this solver. """ self.nlgs.setup(sub) self.newton.setup(sub) # Declare to the world we need derivs self.ln_solver = self.newton.ln_solver def solve(self, params, unknowns, resids, system, metadata=None): self.nlgs.solve(params, unknowns, resids, system, metadata) self.newton.solve(params, unknowns, resids, system, metadata)
def __init__(self): super(HybridGSNewton, self).__init__() self.nlgs = NLGaussSeidel() self.newton = Newton() self.nlgs.options['maxiter'] = 5
def __init__(self): super(TubeTemp, self).__init__() self.add('tm', TubeWallTemp(), promotes=[ 'length_tube','tube_area','tube_thickness','num_pods', 'nozzle_air_W','nozzle_air_Tt']) self.add('tmp_balance', TempBalance(), promotes=['temp_boundary']) #self.add('nozzle_air', FlowStart(thermo_data=janaf, elements=AIR_MIX)) #self.add('bearing_air', FlowStart(thermo_data=janaf, elements=AIR_MIX)) #self.connect("nozzle_air.Fl_O:tot:T", "tm.nozzle_air_Tt") #self.connect("nozzle_air.Fl_O:tot:Cp", "tm.nozzle_air_Cp") #self.connect("nozzle_air.Fl_O:stat:W", "tm.nozzle_air_W") self.connect('tm.ss_temp_residual', 'tmp_balance.ss_temp_residual') self.connect('temp_boundary', 'tm.temp_boundary') self.nl_solver = Newton() self.nl_solver.options['atol'] = 1e-5 self.nl_solver.options['iprint'] = 1 self.nl_solver.options['rtol'] = 1e-5 self.nl_solver.options['maxiter'] = 50 self.ln_solver = ScipyGMRES() self.ln_solver.options['atol'] = 1e-6 self.ln_solver.options['maxiter'] = 100 self.ln_solver.options['restart'] = 100
def __init__(self): super(SellarStateConnection, self).__init__() self.add('px', IndepVarComp('x', 1.0), promotes=['*']) self.add('pz', IndepVarComp('z', np.array([5.0, 2.0])), promotes=['*']) sub = self.add('sub', Group(), promotes=['*']) sub.ln_solver = ScipyGMRES() subgrp = sub.add('state_eq_group', Group(), promotes=['*']) subgrp.ln_solver = ScipyGMRES() subgrp.add('state_eq', StateConnection()) sub.add('d1', SellarDis1withDerivatives(), promotes=['x', 'z', 'y1']) sub.add('d2', SellarDis2withDerivatives(), promotes=['z', 'y1']) self.connect('state_eq.y2_command', 'd1.y2') self.connect('d2.y2', 'state_eq.y2_actual') self.add('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=['x', 'z', 'y1', 'obj']) self.connect('d2.y2', 'obj_cmp.y2') self.add('con_cmp1', ExecComp('con1 = 3.16 - y1'), promotes=['con1', 'y1']) self.add('con_cmp2', ExecComp('con2 = y2 - 24.0'), promotes=['con2']) self.connect('d2.y2', 'con_cmp2.y2') self.nl_solver = Newton()
def __init__(self): super(SellarStateConnection, self).__init__() self.add('px', ParamComp('x', 1.0), promotes=['*']) self.add('pz', ParamComp('z', np.array([5.0, 2.0])), promotes=['*']) self.add('state_eq', StateConnection()) self.add('d1', SellarDis1(), promotes=['x', 'z', 'y1']) self.add('d2', SellarDis2(), promotes=['z', 'y1']) self.connect('state_eq.y2_command', 'd1.y2') self.connect('d2.y2', 'state_eq.y2_actual') self.add('obj_cmp', ExecComp('obj = x**2 + z[1] + y1 + exp(-y2)', z=np.array([0.0, 0.0]), x=0.0, d1=0.0, d2=0.0), promotes=['x', 'z', 'y1', 'obj']) self.connect('d2.y2', 'obj_cmp.y2') self.add('con_cmp1', ExecComp('con1 = 3.16 - y1'), promotes=['*']) self.add('con_cmp2', ExecComp('con2 = y2 - 24.0'), promotes=['con2']) self.connect('d2.y2', 'con_cmp2.y2') self.nl_solver = Newton()
def test_newton_with_backtracking(self): top = Problem() root = top.root = Group() root.add('comp', TrickyComp()) root.add('p', IndepVarComp('y', 1.2278849186466743)) root.connect('p.y', 'comp.y') root.nl_solver = Newton() root.ln_solver = ScipyGMRES() root.nl_solver.line_search = BackTracking() root.nl_solver.line_search.options['maxiter'] = 100 root.nl_solver.line_search.options['c'] = 0.5 root.nl_solver.options['alpha'] = 10.0 top.setup(check=False) top['comp.x'] = 1.0 top.print_all_convergence(level=1) top.run() assert_rel_error(self, top['comp.x'], .3968459, .0001)
def __init__(self, thermo_data=species_data.janaf, elements=AIR_MIX): super(Assembly, self).__init__() self.add('tm', NacelleWallTemp(), promotes=['radius_outer_tube']) self.add('tmp_balance', TempBalance()) self.connect('tm.q_total_out', 'tmp_balance.q_total_out') self.connect('tm.q_total_in', 'tmp_balance.q_total_in') self.connect('tmp_balance.temp_boundary', 'tm.temp_boundary') self.nl_solver = Newton() self.nl_solver.options['atol'] = 1e-8 self.nl_solver.options['iprint'] = 1 self.nl_solver.options['rtol'] = 1e-8 # self.nl_solver.options['maxiter'] = 50 self.nl_solver.options['maxiter'] = 10 # self.ln_solver = ScipyGMRES() # self.ln_solver.options['atol'] = 1e-6 # self.ln_solver.options['maxiter'] = 100 # self.ln_solver.options['restart'] = 100 self.ln_solver = DirectSolver()
def test_bounds_backtracking(self): class SimpleImplicitComp(Component): """ A Simple Implicit Component with an additional output equation. f(x,z) = xz + z - 4 y = x + 2z Sol: when x = 0.5, z = 2.666 Sol: when x = 2.0, z = 1.333 Coupled derivs: y = x + 8/(x+1) dy_dx = 1 - 8/(x+1)**2 = -2.5555555555555554 z = 4/(x+1) dz_dx = -4/(x+1)**2 = -1.7777777777777777 """ def __init__(self): super(SimpleImplicitComp, self).__init__() # Params self.add_param('x', 0.5) # Unknowns self.add_output('y', 0.0) # States self.add_state('z', 2.0, lower=1.5, upper=2.5) self.maxiter = 10 self.atol = 1.0e-12 def solve_nonlinear(self, params, unknowns, resids): pass def apply_nonlinear(self, params, unknowns, resids): """ Don't solve; just calculate the residual.""" x = params['x'] z = unknowns['z'] resids['z'] = x * z + z - 4.0 # Output equations need to evaluate a residual just like an explicit comp. resids['y'] = x + 2.0 * z - unknowns['y'] def linearize(self, params, unknowns, resids): """Analytical derivatives.""" J = {} # Output equation J[('y', 'x')] = np.array([1.0]) J[('y', 'z')] = np.array([2.0]) # State equation J[('z', 'z')] = np.array([params['x'] + 1.0]) J[('z', 'x')] = np.array([unknowns['z']]) return J #------------------------------------------------------ # Test that Newton doesn't drive it past lower bounds #------------------------------------------------------ top = Problem() top.root = Group() top.root.add('comp', SimpleImplicitComp()) top.root.ln_solver = ScipyGMRES() top.root.nl_solver = Newton() top.root.nl_solver.options['maxiter'] = 5 top.root.add('px', IndepVarComp('x', 1.0)) top.root.connect('px.x', 'comp.x') top.setup(check=False) top['px.x'] = 2.0 top.run() self.assertEqual(top['comp.z'], 1.5) #------------------------------------------------------ # Test that Newton doesn't drive it past upper bounds #------------------------------------------------------ top = Problem() top.root = Group() top.root.add('comp', SimpleImplicitComp()) top.root.ln_solver = ScipyGMRES() top.root.nl_solver = Newton() top.root.nl_solver.options['maxiter'] = 5 top.root.add('px', IndepVarComp('x', 1.0)) top.root.connect('px.x', 'comp.x') top.setup(check=False) top['px.x'] = 0.5 top.run() self.assertEqual(top['comp.z'], 2.5)
def test_bounds_backtracking(self): class SimpleImplicitComp1(Component): """ A Simple Implicit Component with an additional output equation. f(x,z) = xz + z - 4 y = x + 2z Sol: when x = 0.5, z = 2.666 Sol: when x = 2.0, z = 1.333 Coupled derivs: y = x + 8/(x+1) dy_dx = 1 - 8/(x+1)**2 = -2.5555555555555554 z = 4/(x+1) dz_dx = -4/(x+1)**2 = -1.7777777777777777 """ def __init__(self): super(SimpleImplicitComp1, self).__init__() # Params self.add_param('x', 0.5) # Unknowns self.add_output('y', 0.0) # States self.add_state('z', 2.0, lower=1.4, upper=2.5) self.maxiter = 10 self.atol = 1.0e-12 def solve_nonlinear(self, params, unknowns, resids): pass def apply_nonlinear(self, params, unknowns, resids): """ Don't solve; just calculate the residual.""" x = params['x'] z = unknowns['z'] resids['z'] = x*z + z - 4.0 # Output equations need to evaluate a residual just like an explicit comp. resids['y'] = x + 2.0*z - unknowns['y'] def linearize(self, params, unknowns, resids): """Analytical derivatives.""" J = {} # Output equation J[('y', 'x')] = np.array([1.0]) J[('y', 'z')] = np.array([2.0]) # State equation J[('z', 'z')] = np.array([params['x'] + 1.0]) J[('z', 'x')] = np.array([unknowns['z']]) return J class SimpleImplicitComp2(Component): """ A Simple Implicit Component with an additional output equation. f(x,z) = xz + z - 4 y = x + 2z Sol: when x = 0.5, z = 2.666 Sol: when x = 2.0, z = 1.333 Coupled derivs: y = x + 8/(x+1) dy_dx = 1 - 8/(x+1)**2 = -2.5555555555555554 z = 4/(x+1) dz_dx = -4/(x+1)**2 = -1.7777777777777777 """ def __init__(self): super(SimpleImplicitComp2, self).__init__() # Params self.add_param('x', 0.5) # Unknowns self.add_output('y', 0.0) # States self.add_state('z', 2.0, lower=1.5, upper=2.5) self.maxiter = 10 self.atol = 1.0e-12 def solve_nonlinear(self, params, unknowns, resids): pass def apply_nonlinear(self, params, unknowns, resids): """ Don't solve; just calculate the residual.""" x = params['x'] z = unknowns['z'] resids['z'] = x*z + z - 4.0 # Output equations need to evaluate a residual just like an explicit comp. resids['y'] = x + 2.0*z - unknowns['y'] def linearize(self, params, unknowns, resids): """Analytical derivatives.""" J = {} # Output equation J[('y', 'x')] = np.array([1.0]) J[('y', 'z')] = np.array([2.0]) # State equation J[('z', 'z')] = np.array([params['x'] + 1.0]) J[('z', 'x')] = np.array([unknowns['z']]) return J #------------------------------------------------------ # Test that Newton doesn't drive it past lower bounds #------------------------------------------------------ top = Problem(impl=impl) top.root = Group() par = top.root.add('par', ParallelGroup()) par.add('comp1', SimpleImplicitComp1()) par.add('comp2', SimpleImplicitComp2()) top.root.ln_solver = lin_solver() top.root.nl_solver = Newton() top.root.nl_solver.options['maxiter'] = 5 top.root.add('px1', IndepVarComp('x', 1.0)) top.root.add('px2', IndepVarComp('x', 1.0)) # TODO: This is to get around a bug in checks. It should be fixed soon. par.ln_solver = lin_solver() top.root.connect('px1.x', 'par.comp1.x') top.root.connect('px2.x', 'par.comp2.x') top.setup(check=False) top['px1.x'] = 2.0 top['px2.x'] = 2.0 top.run() # Comp2 has a tighter lower bound. This test makes sure that we # allgathered and used the lowest alpha. if top.root.par.comp1.is_active(): self.assertEqual(top['par.comp1.z'], 1.5) if top.root.par.comp2.is_active(): self.assertEqual(top['par.comp2.z'], 1.5)