def test_specify_solver(self): import numpy as np from openmdao.api import Problem, Group, IndepVarComp, ExecComp, LinearBlockJac, 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 = LinearBlockJac() 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_globaljac_err(self): 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 = LinearBlockJac() prob.set_solver_print(level=0) prob.model.jacobian = AssembledJacobian() prob.setup(check=False, mode='fwd') prob['width'] = 2.0 prob.run_model() of = ['area'] wrt = ['length'] with self.assertRaises(RuntimeError) as context: prob.compute_totals(of=of, wrt=wrt, return_format='flat_dict') self.assertEqual(str(context.exception), "A block linear solver 'LN: LNBJ' is being used with" " an AssembledJacobian in system ''")
def test_record_solver_linear_block_jac(self, m): self.setup_endpoints(m) self.setup_sellar_model() self.prob.model.nonlinear_solver = NewtonSolver() # used for analytic derivatives self.prob.model.nonlinear_solver.linear_solver = LinearBlockJac() self.recorder.options['record_abs_error'] = True self.recorder.options['record_rel_error'] = True self.recorder.options['record_solver_output'] = True self.recorder.options['record_solver_residuals'] = True self.prob.model.nonlinear_solver.linear_solver.add_recorder( self.recorder) self.prob.setup(check=False) t0, t1 = run_driver(self.prob) upload(self.filename, self._accepted_token) solver_iteration = json.loads(self.solver_iterations) expected_abs_error = 9.947388408259769e-11 expected_rel_error = 4.330301334141486e-08 expected_solver_output = [ { 'name': 'px.x', 'values': [0.0] }, { 'name': 'pz.z', 'values': [0.0, 0.0] }, { 'name': 'd1.y1', 'values': [4.55485639e-09] }, { 'name': 'd2.y2', 'values': [-2.27783334e-08] }, { 'name': 'obj_cmp.obj', 'values': [-2.28447051e-07] }, { 'name': 'con_cmp1.con1', 'values': [2.28461863e-07] }, { 'name': 'con_cmp2.con2', 'values': [-2.27742837e-08] }, ] self.assertAlmostEqual(expected_abs_error, solver_iteration['abs_err']) self.assertAlmostEqual(expected_rel_error, solver_iteration['rel_err']) for o in expected_solver_output: self.assert_array_close(o, solver_iteration['solver_output'])
def test_globaljac_err(self): prob = Problem() model = prob.model = Group(assembled_jac_type='dense') model.add_subsystem('x_param', IndepVarComp('length', 3.0), promotes=['length']) model.add_subsystem('mycomp', TestExplCompSimpleDense(), promotes=['length', 'width', 'area']) model.linear_solver = LinearBlockJac(assemble_jac=True) prob.setup(check=False) with self.assertRaises(RuntimeError) as context: prob.run_model() self.assertEqual(str(context.exception), "Linear solver 'LN: LNBJ' doesn't support assembled jacobians.")
def test_feature_maxiter(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 = LinearBlockJac() model.linear_solver.options['maxiter'] = 5 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 coupled_group(self): # type: () -> Optional[Group] """:obj:`Group`, optional: Group wrapping the coupled blocks with a converger specified in the CMDOWS file. If no coupled blocks are specified in the CMDOWS file this property is `None`. """ if self.coupled_blocks: coupled_group = Group() for uid in self.coupled_blocks: # Get the correct DisciplineComponent discipline_component = self.discipline_components[uid] # Change input variable names if they are provided as copies of coupling variables promotes = ['*'] # type: List[Union[str, Tuple[str, str]]] if not self.has_converger: for i in discipline_component.inputs_from_xml.keys(): if i in self.coupling_vars: promotes.append((i, self.coupling_vars[i]['copy'])) # Add the DisciplineComponent to the group coupled_group.add_subsystem(uid, self.discipline_components[uid], promotes) # Find the convergence type of the coupled group if self.has_converger: conv_type = self.elem_problem_def.find('problemFormulation/convergerType').text if conv_type == 'Gauss-Seidel': coupled_group.linear_solver = LinearBlockGS() coupled_group.nonlinear_solver = NonlinearBlockGS() elif conv_type == 'Jacobi': coupled_group.linear_solver = LinearBlockJac() coupled_group.nonlinear_solver = NonlinearBlockJac() else: raise RuntimeError('Specified convergerType "%s" is not supported.' % conv_type) else: coupled_group.linear_solver = LinearRunOnce() coupled_group.nonlinear_solver = NonLinearRunOnce() return coupled_group return None
def setup(self): meta = self.metadata general_allocation_data = meta['general_allocation_data'] allocation_data = meta['allocation_data'] ref_area_m2 = meta['ref_area_m2'] Wac_1e6_N = meta['Wac_1e6_N'] Wpax_N = general_allocation_data['weight_pax_N'] Mach_mode = meta['Mach_mode'] propulsion_model = meta['propulsion_model'] aerodynamics_model = meta['aerodynamics_model'] initial_mission_vars = meta['initial_mission_vars'] num_routes = allocation_data['num'] for ind_r in range(num_routes): num_points = int(allocation_data['num_pt'][ind_r]) num_control_points = int(allocation_data['num_cp'][ind_r]) range_1e3_km = allocation_data['range_km'][ind_r] / 1e3 self.add_subsystem('mission_{}'.format(ind_r), MissionGroup( num_control_points=num_control_points, num_points=num_points, range_1e3_km=range_1e3_km, ref_area_m2=ref_area_m2, Wac_1e6_N=Wac_1e6_N, Wpax_N=Wpax_N, Mach_mode=Mach_mode, mission_index=ind_r, propulsion_model=propulsion_model, aerodynamics_model=aerodynamics_model, initial_mission_vars=initial_mission_vars, ), promotes=['pax_flt', 'CLt', 'CDt'], ) self.nonlinear_solver = NonlinearBlockJac() self.linear_solver = LinearBlockJac()