def _cite_cmd(options, user_args): """ Run the `openmdao cite` command. Parameters ---------- options : argparse Namespace Command line options. user_args : list of str Args to be passed to the user script. """ if options.outfile is None: out = sys.stdout else: out = open(options.outfile, 'w') if not options.classes: options.classes = None def _cite(prob): if not MPI or MPI.COMM_WORLD.rank == 0: print_citations(prob, classes=options.classes, out_stream=out) exit() hooks._register_hook('setup', 'Problem', post=_cite) ignore_errors(True) _load_and_exec(options.file[0], user_args)
def _view_connections_cmd(options, user_args): """ Return the post_setup hook function for 'openmdao view_connections'. Parameters ---------- options : argparse Namespace Command line options. user_args : list of str Args to be passed to the user script. """ def _viewconns(prob): if options.title: title = options.title else: title = "Connections for %s" % os.path.basename(options.file[0]) view_connections(prob, outfile=options.outfile, show_browser=not options.no_browser, show_values=options.show_values, title=title) exit() # register the hook if options.show_values: funcname = 'final_setup' else: funcname = 'setup' hooks._register_hook(funcname, class_name='Problem', inst_id=options.problem, post=_viewconns) ignore_errors(True) _load_and_exec(options.file[0], user_args)
def _view_dyn_shapes_cmd(options, user_args): """ Return the post_setup hook function for 'openmdao view_dyn_shapes'. Parameters ---------- options : argparse Namespace Command line options. user_args : list of str Args to be passed to the user script. """ def _view_shape_graph(model): view_dyn_shapes(model, outfile=options.outfile, show=not options.no_display, title=options.title) exit() def _set_dyn_hook(prob): # we can't wait until the end of Problem.setup because we'll die in _setup_sizes # if there were any unresolved dynamic shapes, so put the hook immediately after # _setup_dynamic_shapes. inst_id is None here because no system's pathname will # have been set at the time this hook is triggered. hooks._register_hook('_setup_dynamic_shapes', class_name='Group', inst_id=None, post=_view_shape_graph) hooks._setup_hooks(prob.model) # register the hooks hooks._register_hook('setup', 'Problem', pre=_set_dyn_hook) ignore_errors(True) _load_and_exec(options.file[0], user_args)
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 _scaling_cmd(options, user_args): """ Return the post_setup hook function for 'openmdao driver_scaling'. Parameters ---------- options : argparse Namespace Command line options. user_args : list of str Args to be passed to the user script. """ def _set_flag(problem): global _run_driver_called _run_driver_called = True def _scaling_check(problem): if _run_driver_called: # If run_driver has been called, we know no more user changes are coming. _scaling(problem) 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() # register the hooks hooks._register_hook('final_setup', class_name='Problem', inst_id=options.problem, post=_scaling_check) hooks._register_hook('run_driver', class_name='Problem', inst_id=options.problem, pre=_set_flag) # register an atexit function to check if scaling report was triggered during the script import atexit atexit.register(_exitfunc) ignore_errors(True) _load_and_exec(options.file[0], user_args)
def _config_summary_cmd(options, user_args): """ Return the post_setup hook function for 'openmdao summary'. Parameters ---------- options : argparse Namespace Command line options. user_args : list of str Args to be passed to the user script. """ hooks._register_hook('final_setup', 'Problem', post=config_summary, exit=True) ignore_errors(True) _load_and_exec(options.file[0], user_args)
def _check_config_cmd(options, user_args): """ Return the post_setup hook function for 'openmdao check'. Parameters ---------- options : argparse Namespace Command line options. user_args : list of str Args to be passed to the user script. Returns ------- function The post-setup hook function. """ def _check_config(prob): if not MPI or MPI.COMM_WORLD.rank == 0: if options.outfile is None: logger = get_logger('check_config', out_stream='stdout', out_file=None, use_format=True) else: logger = get_logger('check_config', out_file=options.outfile, use_format=True) if not options.checks: options.checks = sorted(_default_checks) elif 'all' in options.checks: options.checks = sorted(_all_checks) prob.check_config(logger, options.checks) exit() # register the hook _register_hook('final_setup', class_name='Problem', inst_id=options.problem, post=_check_config) ignore_errors(True) _load_and_exec(options.file[0], user_args)
def _tree_cmd(options, user_args): """ Return the post_setup hook function for 'openmdao tree'. Parameters ---------- options : argparse Namespace Command line options. user_args : list of str Args to be passed to the user script. """ if options.outfile is None: out = sys.stdout else: out = open(options.outfile, 'w') if options.attrs or options.vecvars: filt = _get_tree_filter(options.attrs, options.vecvars) else: filt = None def _tree(prob): tree(prob, show_colors=options.show_colors, show_sizes=options.show_sizes, show_approx=options.show_approx, filter=filt, max_depth=options.depth, rank=options.rank, stream=out) exit() # register the hook if options.vecvars or options.show_sizes or options.show_approx: funcname = 'final_setup' else: funcname = 'setup' hooks._register_hook(funcname, class_name='Problem', inst_id=options.problem, post=_tree) ignore_errors(True) _load_and_exec(options.file[0], user_args)
def _dist_idxs_exec(options, user_args): """ Return the post_setup hook function for 'openmdao dump_idxs'. Parameters ---------- options : argparse Namespace Command line options. user_args : list of str Args to be passed to the user script. """ if options.outfile is None: out = sys.stdout else: out = open(options.outfile, 'w') def _dumpdist(prob): dump_dist_idxs(prob, full=options.full, stream=out) exit() _register_hook('final_setup', 'Problem', post=_dumpdist) ignore_errors(True) _load_and_exec(options.file[0], user_args)
def _scaling_cmd(options, user_args): """ Return the post_setup hook function for 'openmdao driver_scaling'. Parameters ---------- options : argparse Namespace Command line options. user_args : list of str Args to be passed to the user script. """ def _set_run_driver_flag(problem): global _run_driver_called _run_driver_called.add(problem._name) def _set_run_model_start(problem): global _run_model_start _run_model_start.add(problem._name) def _set_run_model_done(problem): global _run_model_done _run_model_done.add(problem._name) def _scaling_check(problem): if problem._name in _run_driver_called: # If run_driver has been called, we know no more user changes are coming. if problem._name not in _run_model_start: problem.run_model() if problem._name in _run_model_done: _scaling(problem) def _scaling(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() # register the hooks hooks._register_hook('final_setup', class_name='Problem', inst_id=options.problem, post=_scaling_check) hooks._register_hook('run_model', class_name='Problem', inst_id=options.problem, pre=_set_run_model_start, post=_set_run_model_done, ncalls=1) hooks._register_hook('run_driver', class_name='Problem', inst_id=options.problem, pre=_set_run_driver_flag, ncalls=1) # register an atexit function to check if scaling report was triggered during the script import atexit atexit.register(functools.partial(_exitfunc, options.problem)) ignore_errors(True) _load_and_exec(options.file[0], user_args)