problem.py#

Define the Problem class and a FakeComm class for non-MPI users.

class openmdao.core.problem.Problem(model=None, driver=None, comm=None, name=None, reports=UNDEFINED, **options)[source]

Bases: object

Top-level container for the systems and drivers.

Parameters:
model<System> or None

The top-level <System>. If not specified, an empty <Group> will be created.

driver<Driver> or None

The driver for the problem. If not specified, a simple “Run Once” driver will be used.

commMPI.Comm or <FakeComm> or None

The MPI communicator for this Problem. If not specified, comm will be MPI.COMM_WORLD if MPI is active, else it will be None.

namestr

Problem name. Can be used to specify a Problem instance when multiple Problems exist.

reportsstr, bool, None, _UNDEFINED

If _UNDEFINED, the OPENMDAO_REPORTS variable is used. Defaults to _UNDEFINED. If given, reports overrides OPENMDAO_REPORTS. If boolean, enable/disable all reports. Since none is acceptable in the environment variable, a value of reports=None is equivalent to reports=False. Otherwise, reports may be a sequence of strings giving the names of the reports to run.

**optionsnamed args

All remaining named args are converted to options.

Attributes:
model<System>

Pointer to the top-level <System> object (root node in the tree).

commMPI.Comm or <FakeComm>

The global communicator.

_driver<Driver>

Slot for the driver. The default driver is Driver, which just runs the model once.

_mode‘fwd’ or ‘rev’

Return the derivative mode.

_orig_mode‘fwd’, ‘rev’, or ‘auto’

Derivatives calculation mode assigned by the user. If set to ‘auto’, _mode will be automatically assigned to ‘fwd’ or ‘rev’ based on relative sizes of design variables vs. responses.

citestr

Listing of relevant citations that should be referenced when publishing work that uses this class.

options<OptionsDictionary>

Dictionary with general options for the problem.

model_optionsdict

A dictionary of options to be passed to subsystems in the problem’s model during the setup process. This dictionary is keyed by a path pattern string, and the associated value for each path pattern is a dictionary of {option_name: option_val}. Those subsystems within the hierarchy which match the path pattern and that have an option of the given name, will have the value of that option overridden by value given in the dictionary.

recording_options<OptionsDictionary>

Dictionary with problem recording options.

_rec_mgr<RecordingManager>

Object that manages all recorders added to this problem.

_reportslist of str

Names of reports to activate for this Problem.

_checkbool

If True, call check_config at the end of final_setup.

_filtered_vars_to_recorddict

Dictionary of lists of design vars, constraints, etc. to record.

_loggerobject or None

Object for logging config checks if _check is True.

_namestr

Problem name. If no name given, a default name of the form ‘problemN’, where N is an integer, will be given to the problem so it can be referenced in command line tools that have an optional problem name argument

_metadatadict

Problem level metadata.

_run_counterint

The number of times run_driver or run_model has been called.

_warnedbool

Bool to check if value deprecation warning has occured yet

_computing_coloringbool

When True, we are computing coloring.

__getitem__(name)[source]

Get an output/input variable.

Parameters:
namestr

Promoted or relative variable name in the root system’s namespace.

Returns:
float or ndarray or any python object

the requested output/input variable.

__init__(model=None, driver=None, comm=None, name=None, reports=UNDEFINED, **options)[source]

Initialize attributes.

__setitem__(name, value)[source]

Set an output/input variable.

Parameters:
namestr

Promoted or relative variable name in the root system’s namespace.

valuefloat or ndarray or any python object

value to set this variable to.

add_recorder(recorder)[source]

Add a recorder to the problem.

Parameters:
recorderCaseRecorder

A recorder instance.

check_config(logger=None, checks={'auto_ivc_warnings': <function _check_auto_ivc_warnings>, 'comp_has_no_outputs': <function _check_comp_has_no_outputs>, 'dup_inputs': <function _check_dup_comp_inputs>, 'missing_recorders': <function _check_missing_recorders>, 'out_of_order': <function _check_ubcs_prob>, 'solvers': <function _check_solvers>, 'system': <function _check_system_configs>, 'unserializable_options': <function _check_unserializable_options>}, out_file='openmdao_checks.out')[source]

Perform optional error checks on a Problem.

Parameters:
loggerobject

Logging object.

checkslist of str or None or the str ‘all’

Determines what config checks are run. If None, no checks are run If list of str, run those config checks If ‘all’, all the checks (‘auto_ivc_warnings’, ‘comp_has_no_outputs’, ‘cycles’, ‘dup_inputs’, ‘missing_recorders’, ‘out_of_order’, ‘promotions’, ‘solvers’, ‘system’, ‘unconnected_inputs’) are run.

out_filestr or None

If not None, output will be written to this file in addition to stdout.

check_partials(out_stream=DEFAULT_OUT_STREAM, includes=None, excludes=None, compact_print=False, abs_err_tol=1e-06, rel_err_tol=1e-06, method='fd', step=None, form='forward', step_calc='abs', minimum_step=1e-12, force_dense=True, show_only_incorrect=False)[source]

Check partial derivatives comprehensively for all components in your model.

Parameters:
out_streamfile-like object

Where to send human readable output. By default it goes to stdout. Set to None to suppress.

includesNone or list_like

List of glob patterns for pathnames to include in the check. Default is None, which includes all components in the model.

excludesNone or list_like

List of glob patterns for pathnames to exclude from the check. Default is None, which excludes nothing.

compact_printbool

Set to True to just print the essentials, one line per input-output pair.

abs_err_tolfloat

Threshold value for absolute error. Errors about this value will have a ‘*’ displayed next to them in output, making them easy to search for. Default is 1.0E-6.

rel_err_tolfloat

Threshold value for relative error. Errors about this value will have a ‘*’ displayed next to them in output, making them easy to search for. Note at times there may be a significant relative error due to a minor absolute error. Default is 1.0E-6.

methodstr

Method, ‘fd’ for finite difference or ‘cs’ for complex step. Default is ‘fd’.

stepNone, float, or list/tuple of float

Step size(s) for approximation. Default is None, which means 1e-6 for ‘fd’ and 1e-40 for ‘cs’.

formstr

Form for finite difference, can be ‘forward’, ‘backward’, or ‘central’. Default ‘forward’.

step_calcstr

Step type for computing the size of the finite difference step. It can be ‘abs’ for absolute, ‘rel_avg’ for a size relative to the absolute value of the vector input, or ‘rel_element’ for a size relative to each value in the vector input. In addition, it can be ‘rel_legacy’ for a size relative to the norm of the vector. For backwards compatibilty, it can be ‘rel’, which is now equivalent to ‘rel_avg’. Defaults to None, in which case the approximation method provides its default value.

minimum_stepfloat

Minimum step size allowed when using one of the relative step_calc options.

force_densebool

If True, analytic derivatives will be coerced into arrays. Default is True.

show_only_incorrectbool, optional

Set to True if output should print only the subjacs found to be incorrect.

Returns:
dict of dicts of dicts

First key is the component name. Second key is the (output, input) tuple of strings. Third key is one of [‘rel error’, ‘abs error’, ‘magnitude’, ‘J_fd’, ‘J_fwd’, ‘J_rev’, ‘rank_inconsistent’]. For ‘rel error’, ‘abs error’, and ‘magnitude’ the value is a tuple containing norms for forward - fd, adjoint - fd, forward - adjoint. For ‘J_fd’, ‘J_fwd’, ‘J_rev’ the value is a numpy array representing the computed Jacobian for the three different methods of computation. The boolean ‘rank_inconsistent’ indicates if the derivative wrt a serial variable is inconsistent across MPI ranks.

check_totals(of=None, wrt=None, out_stream=DEFAULT_OUT_STREAM, compact_print=False, driver_scaling=False, abs_err_tol=1e-06, rel_err_tol=1e-06, method='fd', step=None, form=None, step_calc='abs', show_progress=False, show_only_incorrect=False, directional=False, sort=False)[source]

Check total derivatives for the model vs. finite difference.

Parameters:
oflist of variable name str or None

Variables whose derivatives will be computed. Default is None, which uses the driver’s objectives and constraints.

wrtlist of variable name str or None

Variables with respect to which the derivatives will be computed. Default is None, which uses the driver’s desvars.

out_streamfile-like object

Where to send human readable output. By default it goes to stdout. Set to None to suppress.

compact_printbool

Set to True to just print the essentials, one line per input-output pair.

driver_scalingbool

When True, return derivatives that are scaled according to either the adder and scaler or the ref and ref0 values that were specified when add_design_var, add_objective, and add_constraint were called on the model. Default is False, which is unscaled.

abs_err_tolfloat

Threshold value for absolute error. Errors about this value will have a ‘*’ displayed next to them in output, making them easy to search for. Default is 1.0E-6.

rel_err_tolfloat

Threshold value for relative error. Errors about this value will have a ‘*’ displayed next to them in output, making them easy to search for. Note at times there may be a significant relative error due to a minor absolute error. Default is 1.0E-6.

methodstr

Method, ‘fd’ for finite difference or ‘cs’ for complex step. Default is ‘fd’.

stepNone, float, or list/tuple of float

Step size for approximation. Default is None, which means 1e-6 for ‘fd’ and 1e-40 for ‘cs’.

formstr

Form for finite difference, can be ‘forward’, ‘backward’, or ‘central’. Default None, which defaults to ‘forward’ for FD.

step_calcstr

Step type for computing the size of the finite difference step. It can be ‘abs’ for absolute, ‘rel_avg’ for a size relative to the absolute value of the vector input, or ‘rel_element’ for a size relative to each value in the vector input. In addition, it can be ‘rel_legacy’ for a size relative to the norm of the vector. For backwards compatibilty, it can be ‘rel’, which is now equivalent to ‘rel_avg’. Defaults to None, in which case the approximation method provides its default value..

show_progressbool

True to show progress of check_totals.

show_only_incorrectbool, optional

Set to True if output should print only the subjacs found to be incorrect.

directionalbool

If True, compute a single directional derivative for each ‘of’ in rev mode or each ‘wrt’ in fwd mode.

sortbool

If True, sort the subjacobian keys alphabetically.

Returns:
Dict of Dicts of Tuples of Floats
First key:

is the (output, input) tuple of strings;

Second key:

is one of [‘rel error’, ‘abs error’, ‘magnitude’, ‘fdstep’];

For ‘rel error’, ‘abs error’, ‘magnitude’ the value is: A tuple containing norms for

forward - fd, adjoint - fd, forward - adjoint.

cleanup()[source]

Clean up resources prior to exit.

compute_jacvec_product(of, wrt, mode, seed)[source]

Given a seed and ‘of’ and ‘wrt’ variables, compute the total jacobian vector product.

Parameters:
oflist of str

Variables whose derivatives will be computed.

wrtlist of str

Derivatives will be computed with respect to these variables.

modestr

Derivative direction (‘fwd’ or ‘rev’).

seeddict or list

Either a dict keyed by ‘wrt’ varnames (fwd) or ‘of’ varnames (rev), containing dresidual (fwd) or doutput (rev) values, OR a list of dresidual or doutput values that matches the corresponding ‘wrt’ (fwd) or ‘of’ (rev) varname list.

Returns:
dict

The total jacobian vector product, keyed by variable name.

compute_totals(of=None, wrt=None, return_format='flat_dict', debug_print=False, driver_scaling=False, use_abs_names=False, get_remote=True, coloring_info=None)[source]

Compute derivatives of desired quantities with respect to desired inputs.

Parameters:
oflist of variable name str or None

Variables whose derivatives will be computed. Default is None, which uses the driver’s objectives and constraints.

wrtlist of variable name str or None

Variables with respect to which the derivatives will be computed. Default is None, which uses the driver’s desvars.

return_formatstr

Format to return the derivatives. Can be ‘dict’, ‘flat_dict’, or ‘array’. Default is a ‘flat_dict’, which returns them in a dictionary whose keys are tuples of form (of, wrt).

debug_printbool

Set to True to print out some debug information during linear solve.

driver_scalingbool

When True, return derivatives that are scaled according to either the adder and scaler or the ref and ref0 values that were specified when add_design_var, add_objective, and add_constraint were called on the model. Default is False, which is unscaled.

use_abs_namesbool

This is deprecated and has no effect.

get_remotebool

If True, the default, the full distributed total jacobian will be retrieved.

coloring_infoColoringMeta, None, or False

If False, do no coloring. If None, use driver coloring info to compute the coloring. Otherwise use the given coloring info object to provide the coloring, if it exists.

Returns:
object

Derivatives in form requested by ‘return_format’.

property driver

Get the Driver for this Problem.

final_setup()[source]

Perform final setup phase on problem in preparation for run.

This is the second phase of setup, and is done automatically at the start of run_driver and run_model. At the beginning of final_setup, we have a model hierarchy with defined variables, solvers, case_recorders, and derivative settings. During this phase, the vectors are created and populated, the drivers and solvers are initialized, and the recorders are started, and the rest of the framework is prepared for execution.

get_reports_dir(force=False)[source]

Get the path to the directory where the report files should go.

If it doesn’t exist, it will be created.

Parameters:
forcebool

If True, create the reports directory if it doesn’t exist, even if this Problem does not have any active reports. This can happen when running testflo.

Returns:
str

The path to the directory where reports should be written.

get_total_coloring(coloring_info=None, of=None, wrt=None, run_model=None)[source]

Get the total coloring.

If necessary, dynamically generate it.

Parameters:
coloring_infodict

Coloring metadata dict.

oflist of str or None

List of response names.

wrtlist of str or None

List of design variable names.

run_modelbool or None

If False, don’t run model. If None, use problem._run_counter to determine if model should be run.

Returns:
Coloring or None

Coloring object, possibly dynamically generated, or None.

get_val(name, units=None, indices=None, get_remote=False)[source]

Get an output/input variable.

Function is used if you want to specify display units.

Parameters:
namestr

Promoted or relative variable name in the root system’s namespace.

unitsstr, optional

Units to convert to before return.

indicesint or list of ints or tuple of ints or int ndarray or Iterable or None, optional

Indices or slice to return.

get_remotebool or None

If True, retrieve the value even if it is on a remote process. Note that if the variable is remote on ANY process, this function must be called on EVERY process in the Problem’s MPI communicator. If False, only retrieve the value if it is on the current process, or only the part of the value that’s on the current process for a distributed variable. If None and the variable is remote or distributed, a RuntimeError will be raised.

Returns:
object

The value of the requested output/input variable.

is_local(name)[source]

Return True if the named variable or system is local to the current process.

Parameters:
namestr

Name of a variable or system.

Returns:
bool

True if the named system or variable is local to this process.

iter_count_iter(include_driver=True, include_solvers=True, include_systems=False)[source]

Yield iteration counts for driver, solvers and/or systems.

Parameters:
include_driverbool

If True, include the driver in the iteration counts.

include_solversbool

If True, include solvers in the iteration counts.

include_systemsbool

If True, include systems in the iteration counts.

Yields:
str

Name of the object.

str

Name of the counter.

int

Value of the counter.

list_driver_vars(show_promoted_name=True, print_arrays=False, driver_scaling=True, desvar_opts=[], cons_opts=[], objs_opts=[], out_stream=DEFAULT_OUT_STREAM)[source]

Print all design variables and responses (objectives and constraints).

Parameters:
show_promoted_namebool

If True, then show the promoted names of the variables.

print_arraysbool, optional

When False, in the columnar display, just display norm of any ndarrays with size > 1. The norm is surrounded by vertical bars to indicate that it is a norm. When True, also display full values of the ndarray below the row. Format is affected by the values set with numpy.set_printoptions. Default is False.

driver_scalingbool, optional

When True, return values that are scaled according to either the adder and scaler or the ref and ref0 values that were specified when add_design_var, add_objective, and add_constraint were called on the model. Default is True.

desvar_optslist of str

List of optional columns to be displayed in the desvars table. Allowed values are: [‘lower’, ‘upper’, ‘ref’, ‘ref0’, ‘indices’, ‘adder’, ‘scaler’, ‘parallel_deriv_color’, ‘cache_linear_solution’, ‘units’, ‘min’, ‘max’].

cons_optslist of str

List of optional columns to be displayed in the cons table. Allowed values are: [‘lower’, ‘upper’, ‘equals’, ‘ref’, ‘ref0’, ‘indices’, ‘adder’, ‘scaler’, ‘linear’, ‘parallel_deriv_color’, ‘cache_linear_solution’, ‘units’, ‘min’, ‘max’].

objs_optslist of str

List of optional columns to be displayed in the objs table. Allowed values are: [‘ref’, ‘ref0’, ‘indices’, ‘adder’, ‘scaler’, ‘units’, ‘parallel_deriv_color’, ‘cache_linear_solution’].

out_streamfile-like object

Where to send human readable output. Default is sys.stdout. Set to None to suppress.

Returns:
dict

Name, size, val, and other requested parameters of design variables, constraints, and objectives.

list_indep_vars(include_design_vars=True, options=None, print_arrays=False, out_stream=DEFAULT_OUT_STREAM)[source]

Retrieve the independent variables in the Problem.

Returns a dictionary mapping the promoted names of indep_vars which the user is expected to provide to the metadata for the associated independent variable.

A output is designated as an independent variable if it is tagged with ‘openmdao:indep_var’. This includes IndepVarComp by default, and users are able to apply this tag to their own component outputs if they wish to provide components with IndepVarComp-like capability.

Parameters:
include_design_varsbool

If True, include design variables in the list of problem inputs. The user may provide values for these but ultimately they will be overwritten by the Driver. Default is False.

optionslist of str or None

List of optional columns to be displayed in the independent variable table. Allowed values are: [‘name’, ‘units’, ‘shape’, ‘size’, ‘desc’, ‘ref’, ‘ref0’, ‘res_ref’, ‘distributed’, ‘lower’, ‘upper’, ‘tags’, ‘shape_by_conn’, ‘copy_shape’, ‘compute_shape’, ‘global_size’, ‘global_shape’, ‘value’].

print_arraysbool, optional

When False, in the columnar display, just display norm of any ndarrays with size > 1. The norm is surrounded by vertical bars to indicate that it is a norm. When True, also display full values of the ndarray below the row. Format is affected by the values set with numpy.set_printoptions.

out_streamfile-like object

Where to send human readable output. Default is sys.stdout. Set to None to suppress.

Returns:
dict

A dictionary mapping the promoted names of all independent variables in the model to their metadata.

list_pre_post(outfile=None)[source]

Display the pre and post optimization components.

Parameters:
outfilefile-like or str or None

Where to send human readable output. Default is None, which sends output to stdout.

list_problem_vars(show_promoted_name=True, print_arrays=False, driver_scaling=True, desvar_opts=[], cons_opts=[], objs_opts=[], out_stream=DEFAULT_OUT_STREAM)[source]

Print all design variables and responses (objectives and constraints).

Parameters:
show_promoted_namebool

If True, then show the promoted names of the variables.

print_arraysbool, optional

When False, in the columnar display, just display norm of any ndarrays with size > 1. The norm is surrounded by vertical bars to indicate that it is a norm. When True, also display full values of the ndarray below the row. Format is affected by the values set with numpy.set_printoptions. Default is False.

driver_scalingbool, optional

When True, return values that are scaled according to either the adder and scaler or the ref and ref0 values that were specified when add_design_var, add_objective, and add_constraint were called on the model. Default is True.

desvar_optslist of str

List of optional columns to be displayed in the desvars table. Allowed values are: [‘lower’, ‘upper’, ‘ref’, ‘ref0’, ‘indices’, ‘adder’, ‘scaler’, ‘parallel_deriv_color’, ‘cache_linear_solution’, ‘units’, ‘min’, ‘max’].

cons_optslist of str

List of optional columns to be displayed in the cons table. Allowed values are: [‘lower’, ‘upper’, ‘equals’, ‘ref’, ‘ref0’, ‘indices’, ‘adder’, ‘scaler’, ‘linear’, ‘parallel_deriv_color’, ‘cache_linear_solution’, ‘units’, ‘min’, ‘max’].

objs_optslist of str

List of optional columns to be displayed in the objs table. Allowed values are: [‘ref’, ‘ref0’, ‘indices’, ‘adder’, ‘scaler’, ‘units’, ‘parallel_deriv_color’, ‘cache_linear_solution’].

out_streamfile-like object

Where to send human readable output. Default is sys.stdout. Set to None to suppress.

Returns:
dict

Name, size, val, and other requested parameters of design variables, constraints, and objectives.

load_case(case)[source]

Pull all input and output variables from a case into the model.

Parameters:
caseCase or dict

A Case from a CaseReader, or a dictionary with key ‘inputs’ mapped to the output of problem.model.list_inputs and key ‘outputs’ mapped to the output of prob.model.list_outputs. Both list_inputs and list_outputs should be called with prom_name=True and return_format=’dict’.

property msginfo

Return info to prepend to messages.

Returns:
str

Info to prepend to messages.

record(case_name)[source]

Record the variables at the Problem level.

Must be called after final_setup has been called. This can either happen automatically through run_driver or run_model, or it can be called manually.

Parameters:
case_namestr

Name used to identify this Problem case.

run_driver(case_prefix=None, reset_iter_counts=True)[source]

Run the driver on the model.

Parameters:
case_prefixstr or None

Prefix to prepend to coordinates when recording. None means keep the preexisting prefix.

reset_iter_countsbool

If True and model has been run previously, reset all iteration counters.

Returns:
bool

Failure flag; True if failed to converge, False is successful.

run_model(case_prefix=None, reset_iter_counts=True)[source]

Run the model by calling the root system’s solve_nonlinear.

Parameters:
case_prefixstr or None

Prefix to prepend to coordinates when recording. None means keep the preexisting prefix.

reset_iter_countsbool

If True and model has been run previously, reset all iteration counters.

set_complex_step_mode(active)[source]

Turn on or off complex stepping mode.

Parameters:
activebool

Complex mode flag; set to True prior to commencing complex step.

set_solver_print(level=2, depth=1e+99, type_='all')[source]

Control printing for solvers and subsolvers in the model.

Parameters:
levelint

Iprint level. Set to 2 to print residuals each iteration; set to 1 to print just the iteration totals; set to 0 to disable all printing except for failures, and set to -1 to disable all printing including failures.

depthint

How deep to recurse. For example, you can set this to 0 if you only want to print the top level linear and nonlinear solver messages. Default prints everything.

type_str

Type of solver to set: ‘LN’ for linear, ‘NL’ for nonlinear, or ‘all’ for all.

set_val(name, val=None, units=None, indices=None)[source]

Set an output/input variable.

Function is used if you want to set a value using a different unit.

Parameters:
namestr

Promoted or relative variable name in the root system’s namespace.

valobject

Value to set this variable to.

unitsstr, optional

Units that value is defined in.

indicesint or list of ints or tuple of ints or int ndarray or Iterable or None, optional

Indices or slice to set to specified value.

setup(check=False, logger=None, mode='auto', force_alloc_complex=False, distributed_vector_class=None, local_vector_class=<class 'openmdao.vectors.default_vector.DefaultVector'>, derivatives=True)[source]

Set up the model hierarchy.

When setup is called, the model hierarchy is assembled, the processors are allocated (for MPI), and variables and connections are all assigned. This method traverses down the model hierarchy to call setup on each subsystem, and then traverses up the model hierarchy to call configure on each subsystem.

Parameters:
checkNone, bool, list of str, or the strs ‘all’

Determines what config checks, if any, are run after setup is complete. If None or False, no checks are run If True, the default checks (‘out_of_order’, ‘system’, ‘solvers’, ‘dup_inputs’, ‘missing_recorders’, ‘unserializable_options’, ‘comp_has_no_outputs’, ‘auto_ivc_warnings’) are run If list of str, run those config checks If ‘all’, all the checks (‘auto_ivc_warnings’, ‘comp_has_no_outputs’, ‘cycles’, ‘dup_inputs’, ‘missing_recorders’, ‘all_unserializable_options’, ‘out_of_order’, ‘promotions’, ‘solvers’, ‘system’, ‘unconnected_inputs’) are run.

loggerobject

Object for logging config checks if check is True.

modestr

Derivatives calculation mode, ‘fwd’ for forward, and ‘rev’ for reverse (adjoint). Default is ‘auto’, which will pick ‘fwd’ or ‘rev’ based on the direction resulting in the smallest number of linear solves required to compute derivatives.

force_alloc_complexbool

If True, sufficient memory will be allocated to allow nonlinear vectors to store complex values while operating under complex step.

distributed_vector_classtype

Reference to the <Vector> class or factory function used to instantiate vectors and associated transfers involved in interprocess communication.

local_vector_classtype

Reference to the <Vector> class or factory function used to instantiate vectors and associated transfers involved in intraprocess communication.

derivativesbool

If True, perform any memory allocations necessary for derivative computation.

Returns:
<Problem>

This enables the user to instantiate and setup in one line.