def _view_metamodel(prob): if bokeh is None: print("bokeh must be installed to view a MetaModel. Use the command:\n", " pip install bokeh") exit() hooks._unregister_hook('final_setup', 'Problem') mm_types = (MetaModelStructuredComp, MetaModelUnStructuredComp, MetaModelSemiStructuredComp) pathname = options.pathname port_number = options.port_number resolution = options.resolution browser = options.browser if pathname: comp = prob.model._get_subsystem(pathname) if comp and isinstance(comp, mm_types): view_metamodel(comp, resolution, port_number, browser) exit() else: comp = None metamodels = {mm.pathname: mm for mm in prob.model.system_iter(include_self=True, typ=mm_types)} mm_names = list(metamodels.keys()) mm_count = len(mm_names) def _mm_list(mm_names, options): for mm in mm_names: print("openmdao view_mm -m {} {}".format(mm, options.file[0])) if mm_count == 0: print("No Metamodel components found in model.") elif mm_count == 1 and not pathname: comp = metamodels[mm_names[0]] view_metamodel(comp, resolution, port_number, browser) else: if not pathname: print("\nMetamodel not specified. Try one of the following:\n") _mm_list(mm_names, options) elif not comp: if len(mm_names) > 1: print("\nMetamodel '{}' not found. Try one of the " "following:\n".format(pathname)) else: print("\nMetamodel '{}' not found. Try the " "following:\n".format(pathname)) _mm_list(mm_names, options) else: if len(mm_names) > 1: print("\n'{}' is not a Metamodel. Try one of the " "following:\n".format(pathname)) else: print("\n'{}' is not a Metamodel. Try the following:\n".format(pathname)) _mm_list(mm_names, options)
def clear_reports(): """ Clear all of the reports from the registry. """ global _reports_registry # need to remove the hooks for name, report in _reports_registry.items(): if getattr(report, 'pre_or_post') == "pre": _unregister_hook(getattr(report, 'method'), getattr(report, 'class_name'), inst_id=getattr(report, 'inst_id'), pre=getattr(report, 'func')) else: _unregister_hook(getattr(report, 'method'), getattr(report, 'class_name'), inst_id=getattr(report, 'inst_id'), post=getattr(report, 'func')) _reports_registry = {}
def _scaling(problem): hooks._unregister_hook('final_setup', 'Problem') # avoid recursive loop hooks._unregister_hook('run_driver', 'Problem') driver = problem.driver if options.title: title = options.title else: title = "Driver scaling for %s" % os.path.basename(options.file[0]) view_driver_scaling(driver, outfile=options.outfile, show_browser=not options.no_browser, title=title, jac=not options.nojac) exit()
def test_problem_hooks(self): hooks._register_hook('setup', 'Problem', pre=make_hook('pre_setup'), post=make_hook('post_setup')) hooks._register_hook('final_setup', 'Problem', pre=make_hook('pre_final'), post=make_hook('post_final')) hooks._register_hook('run_model', 'Problem', pre=make_hook('pre_run_model'), post=make_hook('post_run_model')) prob = self.build_model() prob.run_model() prob.run_model() prob.run_model() self.assertEqual(prob.calls, [ 'pre_setup', 'post_setup', 'pre_run_model', 'pre_final', 'post_final', 'post_run_model', 'pre_run_model', 'pre_final', 'post_final', 'post_run_model', 'pre_run_model', 'pre_final', 'post_final', 'post_run_model', ]) np.testing.assert_allclose(prob['comp.f_xy'], -6.0) hooks._unregister_hook('setup', 'Problem', pre=False) hooks._unregister_hook('final_setup', 'Problem') hooks._unregister_hook('run_model', 'Problem', post=False) prob.calls = [] prob.setup() prob.run_model() prob.run_model() prob.run_model() self.assertEqual(prob.calls, [ 'pre_setup', 'post_run_model', 'post_run_model', 'post_run_model' ]) hooks._unregister_hook('setup', 'Problem') msg = "No hook found for method 'final_setup' for class 'Problem' and instance 'None'." # already removed final_setup hooks earlier, so expect a warning here with assert_warning(UserWarning, msg): hooks._unregister_hook('final_setup', 'Problem') hooks._unregister_hook('run_model', 'Problem') prob.calls = [] prob.setup() prob.run_model() prob.run_model() self.assertEqual(prob.calls, []) self.assertEqual(len(hooks._hooks), 0) # should be no hooks left
def test_multiwrap_mixed_inst_None(self): pre_final = make_hook('pre_final') post_final = make_hook('post_final') hooks._register_hook('final_setup', 'Problem', inst_id='problem1', ncalls=2, pre=make_hook('pre_final1'), post=make_hook('post_final1')) hooks._register_hook('final_setup', 'Problem', inst_id='problem2', pre=make_hook('pre_final2'), post=make_hook('post_final2')) hooks._register_hook('final_setup', 'Problem', pre=pre_final, post=post_final) probs = [self.build_model(f"problem{i+1}") for i in range(2)] for prob in probs: prob.calls = [] for i in range(3): prob.run_model() self.assertEqual(probs[0].calls, [ 'pre_final1', 'pre_final', 'post_final1', 'post_final', 'pre_final1', 'pre_final', 'post_final1', 'post_final', 'pre_final', 'post_final' ]) self.assertEqual(probs[1].calls, [ 'pre_final2', 'pre_final', 'post_final2', 'post_final', 'pre_final2', 'pre_final', 'post_final2', 'post_final', 'pre_final2', 'pre_final', 'post_final2', 'post_final' ]) hooks._unregister_hook('final_setup', 'Problem', pre=pre_final, post=False) for prob in probs: prob.calls = [] for i in range(3): prob.run_model() self.assertEqual(probs[0].calls, ['post_final', 'post_final', 'post_final']) self.assertEqual(probs[1].calls, [ 'pre_final2', 'post_final2', 'post_final', 'pre_final2', 'post_final2', 'post_final', 'pre_final2', 'post_final2', 'post_final' ]) hooks._unregister_hook('final_setup', 'Problem', pre=True, post=False) for prob in probs: prob.calls = [] for i in range(3): prob.run_model() self.assertEqual(probs[0].calls, ['post_final', 'post_final', 'post_final']) self.assertEqual(probs[1].calls, [ 'post_final2', 'post_final', 'post_final2', 'post_final', 'post_final2', 'post_final' ])
def test_multiwrap(self): pre_final = make_hook('pre_final') post_final = make_hook('post_final') hooks._register_hook('final_setup', 'Problem', pre=pre_final, post=post_final) hooks._register_hook('final_setup', 'Problem', pre=make_hook('pre_final2'), post=make_hook('post_final2')) prob = self.build_model() prob.run_model() prob.run_model() prob.run_model() self.assertEqual(prob.calls, [ 'pre_final', 'pre_final2', 'post_final', 'post_final2', 'pre_final', 'pre_final2', 'post_final', 'post_final2', 'pre_final', 'pre_final2', 'post_final', 'post_final2', ]) hooks._unregister_hook('final_setup', 'Problem', pre=pre_final, post=False) prob.calls = [] prob.run_model() prob.run_model() prob.run_model() self.assertEqual(prob.calls, [ 'pre_final2', 'post_final', 'post_final2', 'pre_final2', 'post_final', 'post_final2', 'pre_final2', 'post_final', 'post_final2', ]) hooks._unregister_hook('final_setup', 'Problem', pre=True, post=False) prob.calls = [] prob.run_model() prob.run_model() prob.run_model() self.assertEqual(prob.calls, [ 'post_final', 'post_final2', 'post_final', 'post_final2', 'post_final', 'post_final2', ])
def test_problem_hooks(self): def make_hook(name): def hook_func(prob): prob.calls.append(name) return hook_func hooks.use_hooks = True hooks._register_hook('setup', 'Problem', pre=make_hook('pre_setup'), post=make_hook('post_setup')) hooks._register_hook('final_setup', 'Problem', pre=make_hook('pre_final'), post=make_hook('post_final')) hooks._register_hook('run_model', 'Problem', pre=make_hook('pre_run_model'), post=make_hook('post_run_model')) try: prob = om.Problem() prob.calls = [] model = prob.model model.add_subsystem('p1', om.IndepVarComp('x', 3.0)) model.add_subsystem('p2', om.IndepVarComp('y', -4.0)) model.add_subsystem('comp', om.ExecComp("f_xy=2.0*x+3.0*y")) model.connect('p1.x', 'comp.x') model.connect('p2.y', 'comp.y') prob.setup() prob.run_model() prob.run_model() prob.run_model() self.assertEqual(prob.calls, ['pre_setup', 'post_setup', 'pre_run_model', 'pre_final', 'post_final', 'post_run_model', 'pre_run_model', 'pre_final', 'post_final', 'post_run_model', 'pre_run_model', 'pre_final', 'post_final', 'post_run_model', ]) np.testing.assert_allclose(prob['comp.f_xy'], -6.0) hooks._unregister_hook('setup', 'Problem', pre=False) hooks._unregister_hook('final_setup', 'Problem') hooks._unregister_hook('run_model', 'Problem', post=False) prob.calls = [] prob.setup() prob.run_model() prob.run_model() prob.run_model() self.assertEqual(prob.calls, ['pre_setup', 'post_run_model', 'post_run_model', 'post_run_model']) hooks._unregister_hook('setup', 'Problem') msg = "No hook found for method 'final_setup' for class 'Problem' and instance 'None'." # already removed final_setup hooks earlier, so expect a warning here with assert_warning(UserWarning, msg): hooks._unregister_hook('final_setup', 'Problem') hooks._unregister_hook('run_model', 'Problem') prob.calls = [] prob.setup() prob.run_model() prob.run_model() self.assertEqual(prob.calls, []) self.assertEqual(len(hooks._hooks), 0) # should be no hooks left finally: hooks.use_hooks = False hooks._reset_all_hooks()