Changing Check Settings for FD or CS

Changing Settings for Inputs on a Specific Component

You can change the settings for the approximation schemes that will be used to compare with your component’s derivatives by calling the set_check_partial_options method.

Component.set_check_partial_options(self, wrt, method='fd', form=None, step=None, step_calc=None, directional=False)[source]

Set options that will be used for checking partial derivatives.

Parameters
wrtstr or list of str

The name or names of the variables that derivatives are taken with respect to. This can contain the name of any input or output variable. May also contain a glob pattern.

methodstr

Method for check: “fd” for finite difference, “cs” for complex step.

formstr

Finite difference form for check, can be “forward”, “central”, or “backward”. Leave undeclared to keep unchanged from previous or default value.

stepfloat

Step size for finite difference check. Leave undeclared to keep unchanged from previous or default value.

step_calcstr

Type of step calculation for check, can be “abs” for absolute (default) or “rel” for relative. Leave undeclared to keep unchanged from previous or default value.

directionalbool

Set to True to perform a single directional derivative for each vector variable in the pattern named in wrt.

Note

if you want to use method=”cs”, then you must also pass force_alloc_complex=True to setup. See the example below.

This allows custom tailoring of the approximation settings on a variable basis.

Usage Examples

import openmdao.api as om
from openmdao.core.tests.test_check_derivs import ParaboloidTricky
from openmdao.test_suite.components.paraboloid_mat_vec import ParaboloidMatVec

prob = om.Problem()

prob.model.add_subsystem('p1', om.IndepVarComp('x', 3.0))
prob.model.add_subsystem('p2', om.IndepVarComp('y', 5.0))
comp = prob.model.add_subsystem('comp', ParaboloidTricky())
prob.model.add_subsystem('comp2', ParaboloidMatVec())

prob.model.connect('p1.x', 'comp.x')
prob.model.connect('p2.y', 'comp.y')
prob.model.connect('comp.f_xy', 'comp2.x')

prob.set_solver_print(level=0)

comp.set_check_partial_options(wrt='*', step=1e-2)

prob.setup()
prob.run_model()

prob.check_partials(compact_print=True)
----------------------------------
Component: ParaboloidTricky 'comp'
----------------------------------
'<output>' wrt '<variable>' | fwd mag.   | rev mag.   | check mag. | a(fwd-chk) | a(rev-chk) | a(fwd-rev) | r(fwd-chk) | r(rev-chk) | r(fwd-rev)
------------------------------------------------------------------------------------------------------------------------------------------------

'f_xy'     wrt 'x'          | 6.0000e-07 | n/a        | 6.0000e-07 | 1.5964e-13 | n/a        | n/a        | 2.6607e-07 | n/a        | n/a       
'f_xy'     wrt 'y'          | 8.0000e-07 | n/a        | 8.0000e-07 | 2.9146e-13 | n/a        | n/a        | 3.6433e-07 | n/a        | n/a       
-----------------------------------
Component: ParaboloidMatVec 'comp2'
-----------------------------------
'<output>' wrt '<variable>' | fwd mag.   | rev mag.   | check mag. | a(fwd-chk) | a(rev-chk) | a(fwd-rev) | r(fwd-chk) | r(rev-chk) | r(fwd-rev)
------------------------------------------------------------------------------------------------------------------------------------------------

'f_xy'     wrt 'x'          | 3.8000e+01 | 3.8000e+01 | 3.8000e+01 | 1.0747e-06 | 1.0747e-06 | 0.0000e+00 | 2.8282e-08 | 2.8282e-08 | 0.0000e+00 >ABS_TOL
'f_xy'     wrt 'y'          | 3.0000e+01 | 3.0000e+01 | 3.0000e+01 | 1.0212e-06 | 1.0212e-06 | 0.0000e+00 | 3.4039e-08 | 3.4039e-08 | 0.0000e+00 >ABS_TOL

#################################################################
Sub Jacobian with Largest Relative Error: ParaboloidTricky 'comp'
#################################################################
'<output>' wrt '<variable>' | fwd mag.   | rev mag.   | check mag. | a(fwd-chk) | a(rev-chk) | a(fwd-rev) | r(fwd-chk) | r(rev-chk) | r(fwd-rev)
------------------------------------------------------------------------------------------------------------------------------------------------
'f_xy'     wrt 'y'          | 8.0000e-07 | n/a        | 8.0000e-07 | 2.9146e-13 | n/a        | n/a        | 3.6433e-07 | n/a        | n/a

Here, we show how to set the method. In this case, we use complex step on TrickyParaboloid because the finite difference is less accurate.


Note: You need to set force_alloc_complex to True during setup to utilize complex step during a check.

import openmdao.api as om
from openmdao.core.tests.test_check_derivs import ParaboloidTricky
from openmdao.test_suite.components.paraboloid_mat_vec import ParaboloidMatVec

prob = om.Problem()

prob.model.add_subsystem('p1', om.IndepVarComp('x', 3.0))
prob.model.add_subsystem('p2', om.IndepVarComp('y', 5.0))
comp = prob.model.add_subsystem('comp', ParaboloidTricky())
prob.model.add_subsystem('comp2', ParaboloidMatVec())

prob.model.connect('p1.x', 'comp.x')
prob.model.connect('p2.y', 'comp.y')
prob.model.connect('comp.f_xy', 'comp2.x')

prob.set_solver_print(level=0)

comp.set_check_partial_options(wrt='*', method='cs')

prob.setup(force_alloc_complex=True)
prob.run_model()

prob.check_partials(compact_print=True)
----------------------------------
Component: ParaboloidTricky 'comp'
----------------------------------
'<output>' wrt '<variable>' | fwd mag.   | rev mag.   | check mag. | a(fwd-chk) | a(rev-chk) | a(fwd-rev) | r(fwd-chk) | r(rev-chk) | r(fwd-rev)
------------------------------------------------------------------------------------------------------------------------------------------------

'f_xy'     wrt 'x'          | 6.0000e-07 | n/a        | 6.0000e-07 | 0.0000e+00 | n/a        | n/a        | 0.0000e+00 | n/a        | n/a       
'f_xy'     wrt 'y'          | 8.0000e-07 | n/a        | 8.0000e-07 | 2.1176e-22 | n/a        | n/a        | 2.6470e-16 | n/a        | n/a       
-----------------------------------
Component: ParaboloidMatVec 'comp2'
-----------------------------------
'<output>' wrt '<variable>' | fwd mag.   | rev mag.   | check mag. | a(fwd-chk) | a(rev-chk) | a(fwd-rev) | r(fwd-chk) | r(rev-chk) | r(fwd-rev)
------------------------------------------------------------------------------------------------------------------------------------------------

'f_xy'     wrt 'x'          | 3.8000e+01 | 3.8000e+01 | 3.8000e+01 | 1.0747e-06 | 1.0747e-06 | 0.0000e+00 | 2.8282e-08 | 2.8282e-08 | 0.0000e+00 >ABS_TOL
'f_xy'     wrt 'y'          | 3.0000e+01 | 3.0000e+01 | 3.0000e+01 | 1.0212e-06 | 1.0212e-06 | 0.0000e+00 | 3.4039e-08 | 3.4039e-08 | 0.0000e+00 >ABS_TOL

##################################################################
Sub Jacobian with Largest Relative Error: ParaboloidMatVec 'comp2'
##################################################################
'<output>' wrt '<variable>' | fwd mag.   | rev mag.   | check mag. | a(fwd-chk) | a(rev-chk) | a(fwd-rev) | r(fwd-chk) | r(rev-chk) | r(fwd-rev)
------------------------------------------------------------------------------------------------------------------------------------------------
'f_xy'     wrt 'y'          | 3.0000e+01 | 3.0000e+01 | 3.0000e+01 | 1.0212e-06 | 1.0212e-06 | 0.0000e+00 | 3.4039e-08 | 3.4039e-08 | 0.0000e+00

Directional Derivatives

You can also specify that an input or set of inputs be checked using a directional derivative. For vector inputs, this means that, instead of calculating the derivative with respect to each element of the array, we calculate the derivative with respect to a linear combination of all array indices. For finite difference and complex step, the step value is applied simultaneously to all elements of the vector. This is a much quicker check because it only requires a single execution of the component for the variable rather than one for each element of the array.

import openmdao.api as om
from openmdao.test_suite.components.array_comp import ArrayComp

prob = om.Problem()
model = prob.model
mycomp = model.add_subsystem('mycomp', ArrayComp(), promotes=['*'])

prob.setup()
prob.run_model()

data = prob.check_partials()
-----------------------------
Component: ArrayComp 'mycomp'
-----------------------------
  mycomp: 'y1' wrt 'bb'
    Forward Magnitude : 1.640884e+01
         Fd Magnitude : 1.640884e+01 (fd:forward)
    Absolute Error (Jfor  - Jfd) : 0.000000e+00

    Relative Error (Jfor  - Jfd) : 0.000000e+00

    Raw Forward Derivative (Jfor)
[[ 1.   6.  -1.   1. ]
 [ 3.   2.5  0.   4. ]
 [-2.   2.   8.  -5. ]
 [ 7.   4.   1.   6. ]]

    Raw FD Derivative (Jfd)
[[ 1.   6.  -1.   1. ]
 [ 3.   2.5  0.   4. ]
 [-2.   2.   8.  -5. ]
 [ 7.   4.   1.   6. ]]

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  mycomp: 'y1' wrt (d)'x1'
    Forward Magnitude : 1.978004e+01
         Fd Magnitude : 1.978004e+01 (fd:forward)
    Absolute Error (Jfor  - Jfd) : 1.776357e-15

    Relative Error (Jfor  - Jfd) : 8.980552e-17

    Directional Forward Derivative (Jfor)
[[ 9. ]
 [14.5]
 [ 8. ]
 [ 6. ]]

    Directional FD Derivative (Jfd)
[[ 9. ]
 [14.5]
 [ 8. ]
 [ 6. ]]

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  mycomp: 'y1' wrt (d)'x2'
    Forward Magnitude : 6.527413e+01
         Fd Magnitude : 6.527413e+01 (fd:forward)
    Absolute Error (Jfor  - Jfd) : 3.552714e-15

    Relative Error (Jfor  - Jfd) : 5.442759e-17

    Directional Forward Derivative (Jfor)
[[29.7 ]
 [47.85]
 [26.4 ]
 [19.8 ]]

    Directional FD Derivative (Jfd)
[[29.7 ]
 [47.85]
 [26.4 ]
 [19.8 ]]

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Changing Global Settings For Whole Model

You can change the settings globally for all approximations used for all components. This is done by passing in a value for any of the following arguments:

Name

Description

method

Method for check: “fd” for finite difference, “cs” for complex step.

form

Finite difference form for check, can be “forward”, “central”, or backward.

step

Step size for finite difference check.

step_calc

Type of step calculation for check, can be “abs” for absolute (default) or “rel” for relative.

Note

the global check options take precedence over the ones defined on a component.

Usage Examples

import openmdao.api as om
from openmdao.core.tests.test_check_derivs import ParaboloidTricky
from openmdao.test_suite.components.paraboloid_mat_vec import ParaboloidMatVec

prob = om.Problem()

prob.model.add_subsystem('p1', om.IndepVarComp('x', 3.0))
prob.model.add_subsystem('p2', om.IndepVarComp('y', 5.0))
prob.model.add_subsystem('comp', ParaboloidTricky())
prob.model.add_subsystem('comp2', ParaboloidMatVec())

prob.model.connect('p1.x', 'comp.x')
prob.model.connect('p2.y', 'comp.y')
prob.model.connect('comp.f_xy', 'comp2.x')

prob.set_solver_print(level=0)

prob.setup()
prob.run_model()

prob.check_partials(step=1e-2, compact_print=True)
----------------------------------
Component: ParaboloidTricky 'comp'
----------------------------------
'<output>' wrt '<variable>' | fwd mag.   | rev mag.   | check mag. | a(fwd-chk) | a(rev-chk) | a(fwd-rev) | r(fwd-chk) | r(rev-chk) | r(fwd-rev)
------------------------------------------------------------------------------------------------------------------------------------------------

'f_xy'     wrt 'x'          | 6.0000e-07 | n/a        | 6.0000e-07 | 1.5964e-13 | n/a        | n/a        | 2.6607e-07 | n/a        | n/a       
'f_xy'     wrt 'y'          | 8.0000e-07 | n/a        | 8.0000e-07 | 2.9146e-13 | n/a        | n/a        | 3.6433e-07 | n/a        | n/a       
-----------------------------------
Component: ParaboloidMatVec 'comp2'
-----------------------------------
'<output>' wrt '<variable>' | fwd mag.   | rev mag.   | check mag. | a(fwd-chk) | a(rev-chk) | a(fwd-rev) | r(fwd-chk) | r(rev-chk) | r(fwd-rev)
------------------------------------------------------------------------------------------------------------------------------------------------

'f_xy'     wrt 'x'          | 3.8000e+01 | 3.8000e+01 | 3.8010e+01 | 1.0000e-02 | 1.0000e-02 | 0.0000e+00 | 2.6309e-04 | 2.6309e-04 | 0.0000e+00 >ABS_TOL >REL_TOL
'f_xy'     wrt 'y'          | 3.0000e+01 | 3.0000e+01 | 3.0010e+01 | 1.0000e-02 | 1.0000e-02 | 0.0000e+00 | 3.3322e-04 | 3.3322e-04 | 0.0000e+00 >ABS_TOL >REL_TOL

##################################################################
Sub Jacobian with Largest Relative Error: ParaboloidMatVec 'comp2'
##################################################################
'<output>' wrt '<variable>' | fwd mag.   | rev mag.   | check mag. | a(fwd-chk) | a(rev-chk) | a(fwd-rev) | r(fwd-chk) | r(rev-chk) | r(fwd-rev)
------------------------------------------------------------------------------------------------------------------------------------------------
'f_xy'     wrt 'y'          | 3.0000e+01 | 3.0000e+01 | 3.0010e+01 | 1.0000e-02 | 1.0000e-02 | 0.0000e+00 | 3.3322e-04 | 3.3322e-04 | 0.0000e+00

Note: You need to set force_alloc_complex to True during setup to utilize complex step during a check.

import openmdao.api as om
from openmdao.core.tests.test_check_derivs import ParaboloidTricky
from openmdao.test_suite.components.paraboloid_mat_vec import ParaboloidMatVec

prob = om.Problem()

prob.model.add_subsystem('p1', om.IndepVarComp('x', 3.0))
prob.model.add_subsystem('p2', om.IndepVarComp('y', 5.0))
prob.model.add_subsystem('comp', ParaboloidTricky())
prob.model.add_subsystem('comp2', ParaboloidMatVec())

prob.model.connect('p1.x', 'comp.x')
prob.model.connect('p2.y', 'comp.y')
prob.model.connect('comp.f_xy', 'comp2.x')

prob.set_solver_print(level=0)

prob.setup(force_alloc_complex=True)
prob.run_model()

prob.check_partials(method='cs', compact_print=True)
----------------------------------
Component: ParaboloidTricky 'comp'
----------------------------------
'<output>' wrt '<variable>' | fwd mag.   | rev mag.   | check mag. | a(fwd-chk) | a(rev-chk) | a(fwd-rev) | r(fwd-chk) | r(rev-chk) | r(fwd-rev)
------------------------------------------------------------------------------------------------------------------------------------------------

'f_xy'     wrt 'x'          | 6.0000e-07 | n/a        | 6.0000e-07 | 0.0000e+00 | n/a        | n/a        | 0.0000e+00 | n/a        | n/a       
'f_xy'     wrt 'y'          | 8.0000e-07 | n/a        | 8.0000e-07 | 2.1176e-22 | n/a        | n/a        | 2.6470e-16 | n/a        | n/a       
-----------------------------------
Component: ParaboloidMatVec 'comp2'
-----------------------------------
'<output>' wrt '<variable>' | fwd mag.   | rev mag.   | check mag. | a(fwd-chk) | a(rev-chk) | a(fwd-rev) | r(fwd-chk) | r(rev-chk) | r(fwd-rev)
------------------------------------------------------------------------------------------------------------------------------------------------

'f_xy'     wrt 'x'          | 3.8000e+01 | 3.8000e+01 | 3.8000e+01 | 0.0000e+00 | 0.0000e+00 | 0.0000e+00 | 0.0000e+00 | 0.0000e+00 | 0.0000e+00
'f_xy'     wrt 'y'          | 3.0000e+01 | 3.0000e+01 | 3.0000e+01 | 0.0000e+00 | 0.0000e+00 | 0.0000e+00 | 0.0000e+00 | 0.0000e+00 | 0.0000e+00

#################################################################
Sub Jacobian with Largest Relative Error: ParaboloidTricky 'comp'
#################################################################
'<output>' wrt '<variable>' | fwd mag.   | rev mag.   | check mag. | a(fwd-chk) | a(rev-chk) | a(fwd-rev) | r(fwd-chk) | r(rev-chk) | r(fwd-rev)
------------------------------------------------------------------------------------------------------------------------------------------------
'f_xy'     wrt 'y'          | 8.0000e-07 | n/a        | 8.0000e-07 | 2.1176e-22 | n/a        | n/a        | 2.6470e-16 | n/a        | n/a

import openmdao.api as om
from openmdao.core.tests.test_check_derivs import ParaboloidTricky
from openmdao.test_suite.components.paraboloid_mat_vec import ParaboloidMatVec

prob = om.Problem()

prob.model.add_subsystem('p1', om.IndepVarComp('x', 3.0))
prob.model.add_subsystem('p2', om.IndepVarComp('y', 5.0))
prob.model.add_subsystem('comp', ParaboloidTricky())
prob.model.add_subsystem('comp2', ParaboloidMatVec())

prob.model.connect('p1.x', 'comp.x')
prob.model.connect('p2.y', 'comp.y')
prob.model.connect('comp.f_xy', 'comp2.x')

prob.set_solver_print(level=0)

prob.setup()
prob.run_model()

prob.check_partials(form='central', compact_print=True)
----------------------------------
Component: ParaboloidTricky 'comp'
----------------------------------
'<output>' wrt '<variable>' | fwd mag.   | rev mag.   | check mag. | a(fwd-chk) | a(rev-chk) | a(fwd-rev) | r(fwd-chk) | r(rev-chk) | r(fwd-rev)
------------------------------------------------------------------------------------------------------------------------------------------------

'f_xy'     wrt 'x'          | 6.0000e-07 | n/a        | 6.0041e-07 | 4.0872e-10 | n/a        | n/a        | 6.8074e-04 | n/a        | n/a        >REL_TOL
'f_xy'     wrt 'y'          | 8.0000e-07 | n/a        | 7.9936e-07 | 6.3955e-10 | n/a        | n/a        | 8.0008e-04 | n/a        | n/a        >REL_TOL
-----------------------------------
Component: ParaboloidMatVec 'comp2'
-----------------------------------
'<output>' wrt '<variable>' | fwd mag.   | rev mag.   | check mag. | a(fwd-chk) | a(rev-chk) | a(fwd-rev) | r(fwd-chk) | r(rev-chk) | r(fwd-rev)
------------------------------------------------------------------------------------------------------------------------------------------------

'f_xy'     wrt 'x'          | 3.8000e+01 | 3.8000e+01 | 3.8000e+01 | 5.1532e-08 | 5.1532e-08 | 0.0000e+00 | 1.3561e-09 | 1.3561e-09 | 0.0000e+00
'f_xy'     wrt 'y'          | 3.0000e+01 | 3.0000e+01 | 3.0000e+01 | 2.6415e-08 | 2.6415e-08 | 0.0000e+00 | 8.8051e-10 | 8.8051e-10 | 0.0000e+00

#################################################################
Sub Jacobian with Largest Relative Error: ParaboloidTricky 'comp'
#################################################################
'<output>' wrt '<variable>' | fwd mag.   | rev mag.   | check mag. | a(fwd-chk) | a(rev-chk) | a(fwd-rev) | r(fwd-chk) | r(rev-chk) | r(fwd-rev)
------------------------------------------------------------------------------------------------------------------------------------------------
'f_xy'     wrt 'y'          | 8.0000e-07 | n/a        | 7.9936e-07 | 6.3955e-10 | n/a        | n/a        | 8.0008e-04 | n/a        | n/a

import openmdao.api as om
from openmdao.core.tests.test_check_derivs import ParaboloidTricky

prob = om.Problem()

prob.model.add_subsystem('p1', om.IndepVarComp('x', 3.0))
prob.model.add_subsystem('p2', om.IndepVarComp('y', 5.0))
prob.model.add_subsystem('comp', ParaboloidTricky())

prob.model.connect('p1.x', 'comp.x')
prob.model.connect('p2.y', 'comp.y')

prob.set_solver_print(level=0)

prob.setup()
prob.run_model()

prob.check_partials(step_calc='rel', compact_print=True)
----------------------------------
Component: ParaboloidTricky 'comp'
----------------------------------
'<output>' wrt '<variable>' | fwd mag.   | check mag. | a(fwd-chk) | r(fwd-chk)
-------------------------------------------------------------------------------

'f_xy'     wrt 'x'          | 6.0000e-07 | 6.0159e-07 | 1.5930e-09 | 2.6479e-03 >REL_TOL
'f_xy'     wrt 'y'          | 8.0000e-07 | 8.0007e-07 | 7.0990e-11 | 8.8730e-05 >REL_TOL

#################################################################
Sub Jacobian with Largest Relative Error: ParaboloidTricky 'comp'
#################################################################
'<output>' wrt '<variable>' | fwd mag.   | check mag. | a(fwd-chk) | r(fwd-chk)
-------------------------------------------------------------------------------
'f_xy'     wrt 'x'          | 6.0000e-07 | 6.0159e-07 | 1.5930e-09 | 2.6479e-03