Ejemplo n.º 1
0
                    'WARNING: the requested %s database file does not have any cases to load.'
                    % restart)
                return
            case = cr.get_case(
                cases[-1])  # use last case, ideally it should be the only one

        check_simulation = cr.problem_metadata['driver']['name'] == 'Driver'
        if check_simulation:
            prev_soln = {
                'inputs':
                case.list_inputs(out_stream=None, units=True, prom_name=True),
                'outputs':
                case.list_outputs(out_stream=None, units=True, prom_name=True)
            }

            load_case(problem, prev_soln)
        else:
            # Initialize the system with values from the case.
            # We unnecessarily call setup again to make sure we obliterate the previous solution
            # First reset the connections at the top level model until fixed in OpenMDAO
            problem.setup()

            # Load the values from the previous solution
            load_case(problem, case)


def run_problem(problem,
                refine_method='hp',
                refine_iteration_limit=0,
                run_driver=True,
                simulate=False,
Ejemplo n.º 2
0
def run_problem(problem, refine=False, refine_iteration_limit=10, run_driver=True, simulate=False, no_iterate=False):
    """
    A Dymos-specific interface to execute an OpenMDAO problem containing Dymos Trajectories or
    Phases.  This function can iteratively call run_driver to perform grid refinement, and automatically
    call simulate following a run to check the validity of a result.

    Parameters
    ----------
    problem : om.Problem
        The OpenMDAO problem object to be run.
    refine : bool
        If True, perform grid refinement on the Phases found in the Problem.
    refine_iteration_limit : int
        The number of passes through the grid refinement algorithm to be made.
    run_driver : bool
        If True, run the driver (optimize the problem), otherwise just run the model one time.
    no_iterate : bool
        If True, run the driver but do not iterate.
    simulate : bool
        If True, perform a simulation of Trajectories found in the Problem after the driver
        has been run and grid refinement is complete.
    """
    problem.final_setup()  # make sure command line option hook has a chance to run

    if run_driver:
        if no_iterate:
            problem.driver.opt_settings['maxiter'] = 0
        problem.run_driver()
    else:
        problem.run_model()

    problem.record('final')  # save case for potential restart

    if refine and refine_iteration_limit > 0 and run_driver:
        out_file = 'grid_refinement.out'

        phases = find_phases(problem.model)

        ref = PHAdaptive(phases)
        with open(out_file, 'w+') as f:

            for i in range(refine_iteration_limit):
                refine_results = ref.check_error()

                ref.refine(refine_results)

                for stream in f, sys.stdout:
                    ref.write_iteration(stream, i, phases, refine_results)

                refined_phases = [phase_path for phase_path in refine_results if
                                  phases[phase_path].refine_options['refine'] and
                                  np.any(refine_results[phase_path]['need_refinement'])]

                if not refined_phases:
                    break

                prev_soln = {'inputs': problem.model.list_inputs(out_stream=None, units=True, prom_name=True),
                             'outputs': problem.model.list_outputs(out_stream=None, units=True, prom_name=True)}

                # TODO: Until this is fixed in OpenMDAO 3.0.1
                if isinstance(problem.driver, om.pyOptSparseDriver):
                    problem.driver._res_jacs = {}

                problem.setup()

                load_case(problem, prev_soln)

                problem.run_driver()
            for stream in [f, sys.stdout]:
                if i == refine_iteration_limit-1:
                    print('Iteration limit exceeded. Unable to satisfy specified tolerance', file=stream)
                else:
                    print('Successfully completed grid refinement.', file=stream)
            print(50 * '=')

    if simulate:
        for subsys, local in problem.model._all_subsystem_iter():
            if isinstance(subsys, Trajectory):
                subsys.simulate(record_file='dymos_simulation.db')
Ejemplo n.º 3
0
def _refine_iter(problem, refine_iteration_limit=0, refine_method='hp'):
    """
    This function performs grid refinement for a phases in which solve_segments is true.

    Parameters
    ----------
    problem : om.Problem
        The OpenMDAO problem object to be run.
    refine_method : String
        The choice of refinement algorithm to use for grid refinement
    refine_iteration_limit : int
        The number of passes through the grid refinement algorithm to be made.
    """
    phases = find_phases(problem.model)
    refinement_methods = {'hp': HPAdaptive, 'ph': PHAdaptive}

    if refine_iteration_limit > 0:
        out_file = 'grid_refinement.out'

        ref = refinement_methods[refine_method](phases)
        with open(out_file, 'w+') as f:
            for i in range(refine_iteration_limit):
                refine_results = check_error(phases)

                refined_phases = [
                    phase_path for phase_path in refine_results
                    if phases[phase_path].refine_options['refine']
                    and np.any(refine_results[phase_path]['need_refinement'])
                ]

                for stream in f, sys.stdout:
                    write_error(stream, i, phases, refine_results)

                if not refined_phases:
                    break

                ref.refine(refine_results, i)

                for stream in f, sys.stdout:
                    write_refine_iter(stream, i, phases, refine_results)

                prev_soln = {
                    'inputs':
                    problem.model.list_inputs(out_stream=None,
                                              units=True,
                                              prom_name=True),
                    'outputs':
                    problem.model.list_outputs(out_stream=None,
                                               units=True,
                                               prom_name=True)
                }

                problem.setup()

                load_case(problem, prev_soln)

                problem.run_driver()

            for stream in [f, sys.stdout]:
                if i == refine_iteration_limit - 1:
                    print(
                        'Iteration limit exceeded. Unable to satisfy specified tolerance',
                        file=stream)
                else:
                    print('Successfully completed grid refinement.',
                          file=stream)
            print(50 * '=')
Ejemplo n.º 4
0
def _refine_iter(problem,
                 refine_iteration_limit=0,
                 refine_method='hp',
                 case_prefix=None,
                 reset_iter_counts=True):
    """
    This function performs grid refinement for a phases in which solve_segments is true.

    Parameters
    ----------
    problem : om.Problem
        The OpenMDAO problem object to be run.
    refine_method : String
        The choice of refinement algorithm to use for grid refinement
    refine_iteration_limit : int
        The number of passes through the grid refinement algorithm to be made.
    case_prefix : str or None
        Prefix to prepend to coordinates when recording.
    reset_iter_counts : bool
        If True and model has been run previously, reset all iteration counters.
    """
    phases = find_phases(problem.model)
    refinement_methods = {'hp': HPAdaptive, 'ph': PHAdaptive}
    _case_prefix = '' if case_prefix is None else f'{case_prefix}_'

    failed = problem.run_driver(
        case_prefix=f'{_case_prefix}{refine_method}_0_'
        if refine_iteration_limit > 0 else _case_prefix,
        reset_iter_counts=reset_iter_counts)

    if refine_iteration_limit > 0:
        out_file = 'grid_refinement.out'

        ref = refinement_methods[refine_method](phases)
        with open(out_file, 'w+') as f:
            for i in range(1, refine_iteration_limit + 1):
                refine_results = check_error(phases)

                refined_phases = [
                    phase_path for phase_path in refine_results
                    if phases[phase_path].refine_options['refine']
                    and np.any(refine_results[phase_path]['need_refinement'])
                ]

                for stream in f, sys.stdout:
                    write_error(stream, i, phases, refine_results)

                if not refined_phases:
                    break

                ref.refine(refine_results, i)

                for stream in f, sys.stdout:
                    write_refine_iter(stream, i, phases, refine_results)

                prev_soln = {
                    'inputs':
                    problem.model.list_inputs(out_stream=None,
                                              units=True,
                                              prom_name=True),
                    'outputs':
                    problem.model.list_outputs(out_stream=None,
                                               units=True,
                                               prom_name=True)
                }

                problem.setup()

                load_case(problem, prev_soln)

                failed = problem.run_driver(
                    case_prefix=f'{_case_prefix}{refine_method}_{i}_')

            for stream in [f, sys.stdout]:
                if i == refine_iteration_limit - 1:
                    print(
                        'Iteration limit exceeded. Unable to satisfy specified tolerance',
                        file=stream)
                    failed = True
                else:
                    print('Successfully completed grid refinement.',
                          file=stream)
            print(50 * '=')
    return failed