def test_n2_connection_error(self): """ Test that an n2 html file is generated from a Problem even if it has connection errors. """ from openmdao.test_suite.scripts.bad_connection import BadConnectionModel p = Problem(BadConnectionModel()) # this would be set by the command line hook p.model._raise_connection_errors = False expected = "Group (sub): Attempted to connect from 'tgt.x' to 'cmp.x', but " + \ "'tgt.x' is an input. All connections must be from an output to an input." with assert_warning(UserWarning, expected): p.setup() n2(p, outfile=self.conn_html_filename, show_browser=DEBUG_BROWSER, title="Bad Connection") # Check that the html file has been created and has something in it. self.assertTrue(os.path.isfile(self.conn_html_filename), (self.conn_html_filename + " is not a valid file.")) self.assertTrue( 'OpenMDAO Model Hierarchy and N2 diagram: Bad Connection' in open( self.conn_html_filename).read())
def _viewmod(prob): n2(prob, outfile=options.outfile, show_browser=not options.no_browser, title=options.title, embeddable=options.embeddable) exit() # could make this command line selectable later
def run_n2_report_inner(prob, report_filename=None): if not _should_report_run(prob._reports, 'n2'): return run_n2_report_inner.calls[prob] += 1 if run_n2_report_inner.calls[ prob] > 1: # Only do the report once per Problem return problem_reports_dirpath = get_reports_dir(prob) if _is_rank_0(prob): pathlib.Path(problem_reports_dirpath).mkdir(parents=True, exist_ok=True) n2_filepath = str( pathlib.Path(problem_reports_dirpath).joinpath(report_filename)) try: n2(prob, show_browser=False, outfile=n2_filepath, display_in_notebook=False) except RuntimeError as err: # We ignore this error if str(err) != "Can't compute total derivatives unless " \ "both 'of' or 'wrt' variables have been specified.": raise err
def test_n2_from_sqlite(self): """ Test that an n2 html file is generated from a sqlite file. """ p = Problem() p.model = SellarStateConnection() r = SqliteRecorder(self.sqlite_db_filename2) p.driver.add_recorder(r) p.setup() p.final_setup() r.shutdown() n2(p, outfile=self.compare_html_filename, show_browser=DEBUG_BROWSER) n2(self.sqlite_db_filename2, outfile=self.sqlite_html_filename, show_browser=DEBUG_BROWSER) # Check that the html file has been created and has something in it. self.assertTrue(os.path.isfile(self.sqlite_html_filename), (self.sqlite_html_filename + " is not a valid file.")) self.assertGreater(os.path.getsize(self.sqlite_html_filename), 100) # Check that there are no errors when running from the command line with a recording. check_call('openmdao n2 --no_browser %s' % self.sqlite_db_filename2) # Compare the sizes of the files generated from the Problem and the recording size1 = os.path.getsize(self.sqlite_html_filename) size2 = os.path.getsize(self.compare_html_filename) self.assertTrue( size1 == size2, 'File size of ' + self.sqlite_html_filename + ' is ' + str(size1) + ', but size of ' + self.compare_html_filename + ' is ' + str(size2))
def test_n2_from_sqlite(self): """ Test that an n2 html file is generated from a sqlite file. """ p = om.Problem() p.model = SellarStateConnection() r = SqliteRecorder(self.sqlite_db_filename2) p.driver.add_recorder(r) p.setup() p.final_setup() r.shutdown() n2(p, outfile=self.compare_html_filename, show_browser=DEBUG_BROWSER) n2(self.sqlite_db_filename2, outfile=self.sqlite_html_filename, show_browser=DEBUG_BROWSER) # Check that the html file has been created and has something in it. self.assertTrue(os.path.isfile(self.sqlite_html_filename), (self.sqlite_html_filename + " is not a valid file.")) self.assertGreater(os.path.getsize(self.sqlite_html_filename), 100) # Check that there are no errors when running from the command line with a recording. check_call('openmdao n2 --no_browser %s' % self.sqlite_db_filename2) # Compare models from the files generated from the Problem and the recording sqlite_model_data = self._extract_compressed_model( self.sqlite_html_filename) compare_model_data = self._extract_compressed_model( self.compare_html_filename) self.assertTrue( sqlite_model_data == compare_model_data, 'Model data from sqlite does not match data from Problem.')
def _n2_cmd(options, user_args): """ Process command line args and call n2 on the specified file. Parameters ---------- options : argparse Namespace Command line options. user_args : list of str Command line options after '--' (if any). Passed to user script. """ filename = _to_filename(options.file[0]) if filename.endswith('.py'): # the file is a python script, run as a post_setup hook def _noraise(prob): prob.model._raise_connection_errors = False if options.use_declare_partial_info: warn_deprecation("'--use_declare_partial_info' is now the" " default and the option is ignored.") def _viewmod(prob): n2(prob, outfile=options.outfile, show_browser=not options.no_browser, title=options.title, embeddable=options.embeddable) hooks._register_hook('setup', 'Problem', pre=_noraise, ncalls=1) hooks._register_hook('final_setup', 'Problem', post=_viewmod, exit=True) ignore_errors(True) _load_and_exec(options.file[0], user_args) else: # assume the file is a recording, run standalone n2(filename, outfile=options.outfile, title=options.title, show_browser=not options.no_browser, embeddable=options.embeddable)
def test_non_recordable(self): dummyModule = types.ModuleType('dummyModule', 'The dummyModule module') class myComp(om.ExplicitComponent): def initialize(self): self.options['distributed'] = True self.options.declare('foo', recordable=False) def setup(self): self.add_input('x2') self.add_output('x3') def compute(self, inputs, outputs): outputs['x3'] = inputs['x2'] + 1 p = om.Problem() ivc = p.model.add_subsystem('ivc', om.IndepVarComp()) ivc.add_output('x1') p.model.add_subsystem('myComp', myComp(foo=dummyModule)) p.model.connect('ivc.x1', 'myComp.x2') p.setup() # Test for bug where assembling the options metadata under MPI caused a lockup when # they were gathered. n2(p, show_browser=False)
def _n2_cmd(options): """ Process command line args and call n2 on the specified file. Parameters ---------- options : argparse Namespace Command line options. """ filename = options.file[0] if filename.endswith('.py'): # the file is a python script, run as a post_setup hook def _viewmod(prob): n2(prob, outfile=options.outfile, show_browser=not options.no_browser, title=options.title, embeddable=options.embeddable, use_declare_partial_info=options.use_declare_partial_info) exit() # could make this command line selectable later options.func = lambda options: _viewmod _post_setup_exec(options) else: # assume the file is a recording, run standalone n2(filename, outfile=options.outfile, title=options.title, show_browser=not options.no_browser, embeddable=options.embeddable, use_declare_partial_info=options.use_declare_partial_info)
def _n2_cmd(options, user_args): """ Process command line args and call n2 on the specified file. Parameters ---------- options : argparse Namespace Command line options. user_args : list of str Command line options after '--' (if any). Passed to user script. """ filename = options.file[0] if filename.endswith('.py'): # the file is a python script, run as a post_setup hook def _viewmod(prob): n2(prob, outfile=options.outfile, show_browser=not options.no_browser, title=options.title, embeddable=options.embeddable, use_declare_partial_info=options.use_declare_partial_info) exit() # could make this command line selectable later options.func = lambda options: _viewmod hooks._register_hook('final_setup', 'Problem', pre=_viewmod) _simple_exec(options, user_args) else: # assume the file is a recording, run standalone n2(filename, outfile=options.outfile, title=options.title, show_browser=not options.no_browser, embeddable=options.embeddable, use_declare_partial_info=options.use_declare_partial_info)
def _viewmod(prob): n2(prob, outfile=options.outfile, show_browser=not options.no_browser, title=options.title, embeddable=options.embeddable, use_declare_partial_info=options.use_declare_partial_info) exit() # could make this command line selectable later
def test_n2_from_problem(self): """ Test that an n2 html file is generated from a Problem. """ p = Problem() p.model = SellarStateConnection() p.setup() n2(p, outfile=self.problem_html_filename, show_browser=DEBUG) # Check that the html file has been created and has something in it. self.assertTrue(os.path.isfile(self.problem_html_filename), (self.problem_html_filename + " is not a valid file.")) self.assertGreater(os.path.getsize(self.problem_html_filename), 100)
def test_n2_set_title(self): """ Test that an n2 html file is generated from a Problem. """ p = Problem() p.model = SellarStateConnection() p.setup() n2(p, outfile=self.title_html_filename, show_browser=DEBUG_BROWSER, title="Sellar State Connection") # Check that the html file has been created and has something in it. self.assertTrue(os.path.isfile(self.title_html_filename), (self.title_html_filename + " is not a valid file.")) self.assertTrue('OpenMDAO Model Hierarchy and N2 diagram: Sellar State Connection' in open(self.title_html_filename).read())
def _viewmod(prob): n2(prob, outfile=options.outfile, show_browser=not options.no_browser, title=options.title, embeddable=options.embeddable)
# self.linear_solver = DirectSolver() def setup(self): super(ManufacturingCost, self).setup() class ManufacturingCostFactory(ManufacturingCostFactoryBase): """ A factory to create disciplines of ManufacturingCost analysis """ pass if __name__ == "__main__": parser = OptionParser() parser.add_option("-n", "--no-n2", action="store_false", dest='n2_view', default=True, help="display N2 openmdao viewer") (options, args) = parser.parse_args() problem = Problem() problem.model = ManufacturingCost() problem.setup() problem.final_setup() if options.n2_view: from openmdao.visualization.n2_viewer.n2_viewer import n2 n2(problem)