def __init__(self, cmdows_path=None, kb_path=None, data_folder=None, base_xml_file=None, **kwargs): # type: (Optional[str], Optional[str], Optional[str], Optional[str]) -> None """Initialize a CMDOWS Problem from a given CMDOWS file and knowledge base. It is also possible to specify where (temporary) data should be stored, and if a base XML file should be kept up-to-data. Parameters ---------- cmdows_path : str, optional Path to the CMDOWS file. kb_path : str, optional Path to the knowledge base. data_folder : str, optional Path to the data folder in which to store all files and output from the problem. base_xml_file : str, optional Path to a base XML file to update with the problem data. """ self._cmdows_path = cmdows_path self._kb_path = kb_path self.data_folder = data_folder self.base_xml_file = base_xml_file super(LEGOModel, self).__init__(**kwargs) self.linear_solver = LinearRunOnce() self.nonlinear_solver = NonLinearRunOnce()
def test_record_solver_nonlinear_nonlinear_run_once(self, m): self.setup_endpoints(m) recorder = WebRecorder(self._accepted_token, suppress_output=True) self.setup_sellar_model() self.prob.model.nonlinear_solver = NonLinearRunOnce() self.prob.model.nonlinear_solver.add_recorder(recorder) self.prob.setup(check=False) t0, t1 = run_driver(self.prob) self.prob.cleanup() # No norms so no expected norms expected_abs_error = 0.0 expected_rel_error = 0.0 expected_solver_residuals = None expected_solver_output = None solver_iteration = json.loads(self.solver_iterations) self.assertEqual(expected_abs_error, solver_iteration['abs_err']) self.assertEqual(expected_rel_error, solver_iteration['rel_err']) self.assertEqual(solver_iteration['solver_residuals'], []) self.assertEqual(solver_iteration['solver_output'], [])
def test_set_order_feature(self): # this list will record the execution order of our C1, C2, and C3 components order_list = [] prob = Problem() model = prob.model model.nonlinear_solver = NonLinearRunOnce() model.add_subsystem('indeps', IndepVarComp('x', 1.)) model.add_subsystem('C1', ReportOrderComp(order_list)) model.add_subsystem('C2', ReportOrderComp(order_list)) model.add_subsystem('C3', ReportOrderComp(order_list)) prob.set_solver_print(level=0) self.assertEqual(['indeps', 'C1', 'C2', 'C3'], [s.name for s in model._static_subsystems_allprocs]) prob.setup(check=False) prob.run_model() self.assertEqual(['C1', 'C2', 'C3'], order_list) # reset the shared order list order_list[:] = [] # now swap C2 and C1 in the order model.set_order(['indeps', 'C2', 'C1', 'C3']) # after changing the order, we must call setup again prob.setup(check=False) prob.run_model() self.assertEqual(['C2', 'C1', 'C3'], order_list)
def test_deprecated_runonce(self): p = Problem() p.model.add_subsystem('indep', IndepVarComp('x', 5.0)) p.model.add_subsystem('comp', ExecComp('b=2*a')) msg = "NonLinearRunOnce is deprecated. Use NonlinearRunOnce instead." with assert_warning(DeprecationWarning, msg): p.model.nonlinear_solver = NonLinearRunOnce()
def test_deprecated_runonce(self): p = Problem() p.model.add_subsystem('indep', IndepVarComp('x', 5.0)) p.model.add_subsystem('comp', ExecComp('b=2*a')) with warnings.catch_warnings(record=True) as w: p.model.nonlinear_solver = NonLinearRunOnce() self.assertEqual(len(w), 1) self.assertTrue(issubclass(w[0].category, DeprecationWarning)) self.assertEqual(str(w[0].message), "NonLinearRunOnce is deprecated. Use NonlinearRunOnce instead.")
def test_simple_implicit_self_solve(self): prob = Problem(Group()) prob.model.add_subsystem('p1', IndepVarComp('x', 0.5)) comp = prob.model.add_subsystem('comp', SimpleImplicitComp()) comp.self_solve = True prob.model.linear_solver = ScipyGMRES() prob.model.nonlinear_solver = NonLinearRunOnce() prob.set_solver_print(level=0) prob.model.connect('p1.x', 'comp.x') prob.setup(check=False, mode='fwd') prob.run_model() assert_rel_error(self, prob['comp.z'], 2.666, 1e-3) self.assertLess(prob.model.nonlinear_solver._iter_count, 5) J = prob.compute_totals(of=['comp.y', 'comp.z'], wrt=['p1.x']) assert_rel_error(self, J[('comp.y', 'p1.x')][0][0], -2.5555511, 1e-5) assert_rel_error(self, J[('comp.z', 'p1.x')][0][0], -1.77777777, 1e-5) prob.setup(check=False, mode='rev') prob.run_model() assert_rel_error(self, prob['comp.z'], 2.666, 1e-3) self.assertLess(prob.model.nonlinear_solver._iter_count, 5) J = prob.compute_totals(of=['comp.y', 'comp.z'], wrt=['p1.x']) assert_rel_error(self, J[('comp.y', 'p1.x')][0][0], -2.5555511, 1e-5) assert_rel_error(self, J[('comp.z', 'p1.x')][0][0], -1.77777777, 1e-5) # Check partials data = prob.check_partials() for key1, val1 in iteritems(data): for key2, val2 in iteritems(val1): assert_rel_error(self, val2['abs error'][0], 0.0, 1e-5) assert_rel_error(self, val2['abs error'][1], 0.0, 1e-5) assert_rel_error(self, val2['abs error'][2], 0.0, 1e-5) assert_rel_error(self, data['comp'][('y', 'x')]['J_fwd'][0][0], 1.0, 1e-6) assert_rel_error(self, data['comp'][('y', 'z')]['J_fwd'][0][0], 2.0, 1e-6) assert_rel_error(self, data['comp'][('z', 'x')]['J_fwd'][0][0], 2.66666667, 1e-6) assert_rel_error(self, data['comp'][('z', 'z')]['J_fwd'][0][0], 1.5, 1e-6)
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 test_set_order(self): order_list = [] prob = Problem() model = prob.model model.nonlinear_solver = NonLinearRunOnce() model.add_subsystem('indeps', IndepVarComp('x', 1.)) model.add_subsystem('C1', ReportOrderComp(order_list)) model.add_subsystem('C2', ReportOrderComp(order_list)) model.add_subsystem('C3', ReportOrderComp(order_list)) model.connect('indeps.x', 'C1.x') model.connect('C1.y', 'C2.x') model.connect('C2.y', 'C3.x') prob.set_solver_print(level=0) self.assertEqual(['indeps', 'C1', 'C2', 'C3'], [s.name for s in model._static_subsystems_allprocs]) prob.setup(check=False) prob.run_model() self.assertEqual(['C1', 'C2', 'C3'], order_list) order_list[:] = [] # Big boy rules model.set_order(['indeps', 'C2', 'C1', 'C3']) prob.setup(check=False) prob.run_model() self.assertEqual(['C2', 'C1', 'C3'], order_list) # Extra with self.assertRaises(ValueError) as cm: model.set_order(['indeps', 'C2', 'junk', 'C1', 'C3']) self.assertEqual( str(cm.exception), ": subsystem(s) ['junk'] found in subsystem order but don't exist." ) # Missing with self.assertRaises(ValueError) as cm: model.set_order(['indeps', 'C2', 'C3']) self.assertEqual( str(cm.exception), ": ['C1'] expected in subsystem order and not found.") # Extra and Missing with self.assertRaises(ValueError) as cm: model.set_order(['indeps', 'C2', 'junk', 'C1', 'junk2']) self.assertEqual( str(cm.exception), ": ['C3'] expected in subsystem order and not found.\n" ": subsystem(s) ['junk', 'junk2'] found in subsystem order but don't exist." ) # Dupes with self.assertRaises(ValueError) as cm: model.set_order(['indeps', 'C2', 'C1', 'C3', 'C1']) self.assertEqual( str(cm.exception), ": Duplicate name(s) found in subsystem order list: ['C1']")
dRdy[j, j] = -1.0 if __name__ == "__main__": import time from openmdao.api import Problem, Group, IndepVarComp, NonLinearRunOnce, LinearRunOnce from pycycle.cea import species_data # thermo = species_data.Thermo(species_data.co2_co_o2) thermo = species_data.Thermo(species_data.janaf) prob = Problem() prob.model = Group() prob.model.nonlinear_solver = NonLinearRunOnce() prob.model.linear_solver = LinearRunOnce() des_vars = prob.model.add_subsystem('des_vars', IndepVarComp(), promotes=["*"]) des_vars.add_output('P', 1.034210, units='psi') des_vars.add_output('h', -24.26682261, units='cal/g') des_vars.add_output('init_prod_amounts', thermo.init_prod_amounts) chemeq = prob.model.add_subsystem('chemeq', ChemEq(thermo=thermo, mode="h"), promotes=["*"]) # prob.model.suppress_solver_output = True prob.setup() st = time.time() prob.run_model() print("time: ", time.time()-st)