assert_utils.py#

Functions for making assertions about OpenMDAO Systems.

class openmdao.utils.assert_utils.SkipParameterized[source]

Bases: object

Replaces the parameterized class, skipping decorated test cases.

classmethod expand(input, name_func=None, doc_func=None, skip_on_empty=False, **legacy)[source]

Decorate a test so that it raises a SkipTest.

Parameters:
inputiterable

Not used (part of parameterized API).

name_funcfunction

Not used (part of parameterized API).

doc_funcfunction

Not used (part of parameterized API).

skip_on_emptybool

Not used (part of parameterized API).

**legacydict

Not used (part of parameterized API).

Returns:
function

The wrapper function.

openmdao.utils.assert_utils.assert_check_partials(data, atol=1e-06, rtol=1e-06, verbose=False, max_display_shape=(20, 20))[source]

Raise assertion if any entry from the return from check_partials is above a tolerance.

Parameters:
datadict 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’,

‘vals_at_max_abs’, ‘vals_at_max_rel’, ‘directional_fd_fwd’, ‘directional_fd_rev’, ‘directional_fwd_rev’, ‘rank_inconsistent’, ‘matrix_free’, ‘directional’, ‘steps’, and ‘rank_inconsistent’].

For ‘J_fd’, ‘J_fwd’, ‘J_rev’ the value is a numpy array representing the computed Jacobian for the three different methods of computation. For ‘rel error’, ‘abs error’, ‘vals_at_max_abs’ and ‘vals_at_max_rel’ the value is a tuple containing values for forward - fd, reverse - fd, forward - reverse. For ‘magnitude’ the value is a tuple indicating the maximum magnitude of values found in Jfwd, Jrev, and Jfd.

atolfloat

Absolute error. Default is 1e-6.

rtolfloat

Relative error. Default is 1e-6.

verbosebool

When True, display more jacobian information.

max_display_shapetuple of int

Maximum shape of the jacobians to display directly in the error message. Default is (20, 20). Only active if verbose is True.

openmdao.utils.assert_utils.assert_check_totals(totals_data, atol=1e-06, rtol=1e-06, max_display_shape=(20, 20))[source]

Raise assertion if any entry from the return from check_totals is above a tolerance.

Parameters:
totals_dataDict 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’, ‘J_fd’, ‘J_fwd’, ‘J_rev’,

‘vals_at_max_abs’, ‘vals_at_max_rel’, ‘directional_fd_fwd’, ‘directional_fd_rev’, ‘directional_fwd_rev’, ‘rank_inconsistent’, ‘matrix_free’, ‘directional’, ‘steps’, and ‘rank_inconsistent’].

For ‘J_fd’, ‘J_fwd’, ‘J_rev’ the value is a numpy array representing the computed Jacobian for the three different methods of computation. For ‘rel error’, ‘abs error’, ‘vals_at_max_abs’ and ‘vals_at_max_rel’ the value is a tuple containing values for forward - fd, reverse - fd, forward - reverse. For ‘magnitude’ the value is a tuple indicating the maximum magnitude of values found in Jfwd, Jrev, and Jfd.

atolfloat

Absolute error. Default is 1e-6.

rtolfloat

Relative error. Default is 1e-6.

max_display_shapetuple of int

Maximum shape of the jacobians to display directly in the error message. Default is (20, 20).

openmdao.utils.assert_utils.assert_equal_arrays(a1, a2)[source]

Check that two arrays are equal.

This is a simplified method useful when the arrays to be compared may not be numeric. It simply compares the shapes of the two arrays and then does a value by value comparison.

Parameters:
a1array

The first array to compare.

a2array

The second array to compare.

openmdao.utils.assert_utils.assert_equal_numstrings(s1, s2, atol=1e-06, rtol=1e-06)[source]

Check that two strings containing numbers are equal after convering numerical parts to floats.

Parameters:
s1str

The first numeric string to compare.

s2str

The second numeric string to compare.

atolfloat

Absolute error tolerance. Default is 1e-6.

rtolfloat

Relative error tolerance. Default is 1e-6.

openmdao.utils.assert_utils.assert_near_equal(actual, desired, tolerance=1e-15, tol_type='rel')[source]

Check relative error.

Determine that the relative error between actual and desired is within tolerance. If desired is zero, then use absolute error.

Can handle some data structures. Generates warnings for data types it cannot handle.

Parameters:
actualfloat, array-like, dict

The value from the test.

desiredfloat, array-like, dict

The value expected.

tolerancefloat

Maximum relative or absolute error. For relative tolerance: (actual - desired) / desired. For absolute tolerance: (actual - desired).

tol_type{‘rel’, ‘abs’}

Type of error to use: ‘rel’ for relative error, ‘abs’ for absolute error. Default is set to ‘rel’.

Returns:
float

The error.

openmdao.utils.assert_utils.assert_no_approx_partials(system, include_self=True, recurse=True, method='any', excludes=None)[source]

Raise assertion error if any component within system is using approximated partials.

Parameters:
systemSystem

The system under which to search for approximated partials.

include_selfbool

If True, include this system in the iteration.

recursebool

If True, iterate over the whole tree under this system.

methodstr

Specifically look for Components with this method of approx partials. Values can be ‘cs’, ‘fd’, or ‘any’. ‘any’ means either ‘cs’ or ‘fd’. The default is ‘any’.

excludesstr, iter of str, or None

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

Raises:
AssertionError

If a subsystem of group is found to be using approximated partials.

openmdao.utils.assert_utils.assert_no_dict_jacobians(system, include_self=True, recurse=True)[source]

Raise an assertion error if any Group within system is found to be using dictionary jacobians.

Parameters:
systemSystem

The system under which to search for approximated partials.

include_selfbool

If True, include this system in the iteration.

recursebool

If True, iterate over the whole tree under this system.

Raises:
AssertionError

If a subsystem of group is found to be using approximated partials.

openmdao.utils.assert_utils.assert_no_warning(category, msg=None, contains=False)[source]

Context manager asserting that a warning is not issued.

Parameters:
categoryclass

The class of the warning.

msgstr or None

The text of the warning. If None then only the warning class will be checked.

containsbool

If True, check that the warning text contains msg, rather than checking equality.

Yields:
None
Raises:
AssertionError

If the warning is raised.

openmdao.utils.assert_utils.assert_sparsity_matches_fd(system, direction='fwd', outstream=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>)[source]

Assert that the sparsity of the system matches the finite difference sparsity.

Parameters:
systemSystem

The system to check.

directionstr

The direction to check. ‘fwd’ or ‘rev’.

outstreamfile

The stream to write the output to. If None, no output is written.

openmdao.utils.assert_utils.assert_warning(category, msg, contains_msg=False, ranks=None)[source]

Context manager asserting that a warning is issued.

Parameters:
categoryclass

The class of the expected warning.

msgstr

The text of the expected warning.

contains_msgbool

Set to True to check that the warning text contains msg, rather than checking equality.

ranksint or list of int, optional

The global ranks on which the warning is expected.

Yields:
None
Raises:
AssertionError

If the expected warning is not raised.

openmdao.utils.assert_utils.assert_warnings(expected_warnings)[source]

Context manager asserting that expected warnings are issued.

Parameters:
expected_warningsiterable of (class, str)

The category and text of the expected warnings.

Yields:
None
Raises:
AssertionError

If all the expected warnings are not raised.

openmdao.utils.assert_utils.skip_helper(msg)[source]

Raise a SkipTest.

Parameters:
msgstr

The skip messaage.

Raises:
SkipTest