Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
def _timing_cmd(options, user_args):
    """
    Implement the 'openmdao timing' command.

    Parameters
    ----------
    options : argparse Namespace
        Command line options.
    user_args : list of str
        Args to be passed to the user script.
    """
    if not options.funcs:
        options.funcs = _default_timer_methods.copy()

    filename = _to_filename(options.file[0])
    if filename.endswith('.py'):
        hooks._register_hook('setup',
                             'Problem',
                             pre=partial(_set_timer_setup_hook, options))

        # register an atexit function to write out all of the timing data
        atexit.register(partial(_postprocess, options))

        with timing_context(not options.use_context):
            _load_and_exec(options.file[0], user_args)

    else:  # assume file is a pickle file
        if options.use_context:
            issue_warning(
                f"Since given file '{options.file[0]}' is not a python script, the "
                "'--use_context' option is ignored.")
        _show_view(options.file[0], options)
Ejemplo n.º 3
0
def _statprof_exec(options, user_args):
    """
    Called from the command line (openmdao statprof command) to create a statistical profile.
    """

    if not options.file:
        print("No files to process.")
        sys.exit(0)

    if len(options.file) > 1:
        print("statprof can only process a single file.", file=sys.stderr)
        sys.exit(-1)

    script_name = _to_filename(options.file[0])

    if script_name.endswith('.py'):
        pyfile = options.file[0]
        outfile = 'statprof.raw'
        if MPI:
            outfile = outfile + '.' + str(MPI.COMM_WORLD.rank)

        samples_taken = _statprof_py_file(options, outfile, user_args)
    else:  # assume it's a raw statprof data file
        outfile = options.file[0]
        pyfile = None

    if options.noshow:
        _process_raw_statfile(outfile, options)
    else:
        view_statprof(options, pyfile, outfile)
Ejemplo n.º 4
0
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 _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

        hooks._register_hook('final_setup', 'Problem', post=_viewmod)

        _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,
           use_declare_partial_info=options.use_declare_partial_info)
Ejemplo n.º 5
0
def _xdsm_cmd(options, user_args):
    """
    Process command line args and call xdsm 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])

    kwargs = {}
    for name in ['box_stacking', 'box_width', 'box_lines', 'numbered_comps', 'number_alignment']:
        val = getattr(options, name)
        if val is not None:
            kwargs[name] = val

    if filename.endswith('.py'):
        # the file is a python script, run as a post_setup hook
        def _xdsm(prob):
            write_xdsm(prob, filename=options.outfile, model_path=options.model_path,
                       recurse=options.recurse,
                       include_external_outputs=not options.no_extern_outputs,
                       out_format=options.format,
                       include_solver=options.include_solver, subs=_CHAR_SUBS,
                       show_browser=not options.no_browser, show_parallel=not options.no_parallel,
                       add_process_conns=not options.no_process_conns,
                       output_side=options.output_side,
                       legend=options.legend,
                       class_names=options.class_names,
                       equations=options.equations,
                       include_indepvarcomps=not options.no_indepvarcomps,
                       **kwargs)
            exit()

        hooks._register_hook('setup', 'Problem', post=_xdsm)

        _load_and_exec(options.file[0], user_args)
    else:
        # assume the file is a recording, run standalone
        write_xdsm(options.file[0], filename=options.outfile, model_path=options.model_path,
                   recurse=options.recurse,
                   include_external_outputs=not options.no_extern_outputs,
                   out_format=options.format,
                   include_solver=options.include_solver, subs=_CHAR_SUBS,
                   show_browser=not options.no_browser, show_parallel=not options.no_parallel,
                   add_process_conns=not options.no_process_conns, output_side=options.output_side,
                   legend=options.legend,
                   class_names=options.class_names,
                   equations=options.equations,
                   **kwargs)
Ejemplo n.º 6
0
def _statprof_py_file(options, outfile, user_args):
    """
    Run statistical profiling on the given python script.

    Parameters
    ----------
    options : argparse Namespace
        Command line options.
    outfile : str
        Output filename.
    user_args : list of str
        Command line options after '--' (if any).  Passed to user script.

    Returns
    -------
    int
        Total number of samples taken.
    """
    script_name = _to_filename(options.file[0])

    sys.path.insert(0, os.path.dirname(script_name))

    # update sys.argv in case python script takes cmd line args
    sys.argv[:] = [script_name] + user_args

    with open(script_name, 'rb') as fp:
        code = compile(fp.read(), script_name, 'exec')

    prof = StatisticalProfiler(outfile,
                               interval=options.interval,
                               mode=options.sampling_mode)

    prof.start(duration=options.duration)

    if ':' in options.file[0] and not os.path.isfile(options.file[0]):
        while not (prof.stopped or prof.stopping):
            _load_and_run_test(options.file[0])
    else:
        while not (prof.stopped or prof.stopping):
            globals_dict = {
                '__file__': script_name,
                '__name__': '__main__',
                '__package__': None,
                '__cached__': None,
            }

            exec(code, globals_dict)

    prof.stop()

    return prof.samples_taken