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(SellarDerivativesGrouped, self).__init__() self.add('px', IndepVarComp('x', 1.0), promotes=['*']) self.add('pz', IndepVarComp('z', np.array([5.0, 2.0])), promotes=['*']) mda = self.add('mda', Group(), promotes=['*']) mda.ln_solver = ScipyGMRES() mda.add('d1', SellarDis1withDerivatives(), promotes=['*']) mda.add('d2', SellarDis2withDerivatives(), promotes=['*']) 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=['*']) self.add('con_cmp1', ExecComp('con1 = 3.16 - y1'), promotes=['*']) self.add('con_cmp2', ExecComp('con2 = y2 - 24.0'), promotes=['*']) mda.nl_solver = NLGaussSeidel() mda.d1.fd_options['force_fd'] = True mda.d2.fd_options['force_fd'] = True self.ln_solver = ScipyGMRES()
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(Sim, self).__init__() self.add('balance', Balance(), promotes=['TsTube']) self.add('cycle', CompressionCycle()) self.connect('cycle.fl_start.Fl_O:stat:T', 'balance.Ts_in') self.connect('cycle.splitter.Fl_O1:stat:area', 'balance.AtubeB') self.connect('cycle.splitter.Fl_O2:stat:area', 'balance.AtubeC') self.connect('cycle.bypass.Fl_O:stat:area', 'balance.Abypass') self.connect('cycle.inlet.Fl_O:stat:area', 'balance.Adiff') self.connect('cycle.duct.Fl_O:stat:area', 'balance.Acmprssd') self.connect('cycle.bypass_exit.Fl_O:stat:area', 'balance.AbypassExit') self.connect('cycle.nozzle.Fl_O:stat:area', 'balance.AnozzExit') self.connect('cycle.comp.power', 'balance.pwr') self.connect('cycle.tube.Fl_O:stat:T', 'TsTube') self.connect('balance.Pt', 'cycle.fl_start.P') self.connect('balance.Tt', 'cycle.fl_start.T') self.connect('balance.W', 'cycle.fl_start.W') self.connect('balance.BPR', 'cycle.splitter.BPR') self.connect('balance.byp_exit_MN', 'cycle.bypass_exit.MN_target') #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(SellarDerivatives, self).__init__() self.add('px', IndepVarComp('x', 1.0), promotes=['x']) self.add('pz', IndepVarComp('z', np.array([5.0, 2.0])), promotes=['z']) self.add('d1', SellarDis1withDerivatives(), promotes=['x', 'z', 'y1', 'y2']) self.add('d2', SellarDis2withDerivatives(), promotes=['z', 'y1', 'y2']) self.add('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']) self.add('con_cmp1', ExecComp('con1 = 3.16 - y1'), promotes=['con1', 'y1']) self.add('con_cmp2', ExecComp('con2 = y2 - 24.0'), promotes=['con2', 'y2']) self.nl_solver = NLGaussSeidel() self.ln_solver = ScipyGMRES()
def __init__(self): super(SellarNoDerivatives, self).__init__() self.add('px', IndepVarComp('x', 1.0), promotes=['x']) self.add('pz', IndepVarComp('z', np.array([5.0, 2.0])), promotes=['z']) cycle = self.add('cycle', Group(), promotes=['x', 'z', 'y1', 'y2']) cycle.ln_solver = ScipyGMRES() cycle.add('d1', SellarDis1(), promotes=['x', 'z', 'y1', 'y2']) cycle.add('d2', SellarDis2(), promotes=['z', 'y1', 'y2']) self.add('obj_cmp', ExecComp('obj = x**2 + z[1] + y1 + exp(-y2)', z=np.array([0.0, 0.0]), x=0.0), promotes=['x', 'z', 'y1', 'y2', 'obj']) self.add('con_cmp1', ExecComp('con1 = 3.16 - y1'), promotes=['con1', 'y1']) self.add('con_cmp2', ExecComp('con2 = y2 - 24.0'), promotes=['con2', 'y2']) self.nl_solver = NLGaussSeidel() self.cycle.d1.deriv_options['type'] = 'fd' self.cycle.d2.deriv_options['type'] = 'fd'
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)
num_comps = 50 pts = 2 if 'petsc' in sys.argv: from openmdao.core.petsc_impl import PetscImpl impl = PetscImpl else: from openmdao.core.basic_impl import BasicImpl impl = BasicImpl g = Group() p = Problem(impl=impl, root=g) if 'gmres' in sys.argv: from openmdao.solvers.scipy_gmres import ScipyGMRES p.root.ln_solver = ScipyGMRES() g.add("P", IndepVarComp('x', numpy.ones(vec_size))) p.driver.add_desvar("P.x") par = g.add("par", ParallelGroup()) for pt in range(pts): ptname = "G%d" % pt ptg = par.add(ptname, Group()) create_dyncomps(ptg, num_comps, 2, 2, 2, var_factory=lambda: numpy.zeros(vec_size))
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)