Checking Partial Derivatives on a Subset of a Model#
Includes and Excludes#
When you have a model with a large number of components, you may want to reduce the number of components you check so that the output is small and readable. The check_partials
method has two arguments: “includes” and “excludes” that help you specify a reduced set. Both of these arguments are lists of strings that default to None. If you specify “includes”, and give it a list containing strings, then only the components whose full pathnames match one of the patterns in those strings are included in the check. Wildcards are acceptable in the string patterns. Likewise, if you specify excludes, then components whose pathname matches the given patterns will be excluded from the check.
You can use both arguments together to hone in on the precise set of components you wish to check.
Paraboloid
class definition
class Paraboloid(om.ExplicitComponent):
"""
Evaluates the equation f(x,y) = (x-3)^2 + xy + (y+4)^2 - 3.
"""
def setup(self):
self.add_input('x', val=0.0)
self.add_input('y', val=0.0)
self.add_output('f_xy', val=0.0)
def setup_partials(self):
self.declare_partials('*', '*')
def compute(self, inputs, outputs):
"""
f(x,y) = (x-3)^2 + xy + (y+4)^2 - 3
Optimal solution (minimum): x = 6.6667; y = -7.3333
"""
x = inputs['x']
y = inputs['y']
outputs['f_xy'] = (x-3.0)**2 + x*y + (y+4.0)**2 - 3.0
def compute_partials(self, inputs, partials):
"""
Jacobian for our paraboloid.
"""
x = inputs['x']
y = inputs['y']
partials['f_xy', 'x'] = 2.0*x - 6.0 + y
partials['f_xy', 'y'] = 2.0*y + 8.0 + x
import openmdao.api as om
from openmdao.test_suite.components.paraboloid import Paraboloid
prob = om.Problem()
model = prob.model
sub = model.add_subsystem('c1c', om.Group())
sub.add_subsystem('d1', Paraboloid())
sub.add_subsystem('e1', Paraboloid())
sub2 = model.add_subsystem('sss', om.Group())
sub3 = sub2.add_subsystem('sss2', om.Group())
sub2.add_subsystem('d1', Paraboloid())
sub3.add_subsystem('e1', Paraboloid())
model.add_subsystem('abc1cab', Paraboloid())
prob.setup()
prob.run_model()
prob.check_partials(compact_print=True, includes='*c*c*')
------------------------------
Component: Paraboloid 'c1c.d1'
------------------------------
+-----------------+------------------+-------------+-------------+-------------+-------------+------------+
| of '<variable>' | wrt '<variable>' | calc mag. | check mag. | a(cal-chk) | r(cal-chk) | error desc |
+=================+==================+=============+=============+=============+=============+============+
| 'f_xy' | 'x' | 6.0000e+00 | 6.0000e+00 | 9.9925e-07 | 1.6654e-07 | |
+-----------------+------------------+-------------+-------------+-------------+-------------+------------+
| 'f_xy' | 'y' | 8.0000e+00 | 8.0000e+00 | 9.9943e-07 | 1.2493e-07 | |
+-----------------+------------------+-------------+-------------+-------------+-------------+------------+
------------------------------
Component: Paraboloid 'c1c.e1'
------------------------------
+-----------------+------------------+-------------+-------------+-------------+-------------+------------+
| of '<variable>' | wrt '<variable>' | calc mag. | check mag. | a(cal-chk) | r(cal-chk) | error desc |
+=================+==================+=============+=============+=============+=============+============+
| 'f_xy' | 'x' | 6.0000e+00 | 6.0000e+00 | 9.9925e-07 | 1.6654e-07 | |
+-----------------+------------------+-------------+-------------+-------------+-------------+------------+
| 'f_xy' | 'y' | 8.0000e+00 | 8.0000e+00 | 9.9943e-07 | 1.2493e-07 | |
+-----------------+------------------+-------------+-------------+-------------+-------------+------------+
-------------------------------
Component: Paraboloid 'abc1cab'
-------------------------------
+-----------------+------------------+-------------+-------------+-------------+-------------+------------+
| of '<variable>' | wrt '<variable>' | calc mag. | check mag. | a(cal-chk) | r(cal-chk) | error desc |
+=================+==================+=============+=============+=============+=============+============+
| 'f_xy' | 'x' | 6.0000e+00 | 6.0000e+00 | 9.9925e-07 | 1.6654e-07 | |
+-----------------+------------------+-------------+-------------+-------------+-------------+------------+
| 'f_xy' | 'y' | 8.0000e+00 | 8.0000e+00 | 9.9943e-07 | 1.2493e-07 | |
+-----------------+------------------+-------------+-------------+-------------+-------------+------------+
#############################################################
Sub Jacobian with Largest Relative Error: Paraboloid 'c1c.d1'
#############################################################
+-----------------+------------------+-------------+-------------+-------------+-------------+
| of '<variable>' | wrt '<variable>' | calc mag. | check mag. | a(cal-chk) | r(cal-chk) |
+=================+==================+=============+=============+=============+=============+
| 'f_xy' | 'x' | 6.0000e+00 | 6.0000e+00 | 9.9925e-07 | 1.6654e-07 |
+-----------------+------------------+-------------+-------------+-------------+-------------+
{'c1c.d1': {('f_xy', 'x'): {'J_fwd': array([[-6.]]),
'J_fd': array([[-5.999999]]),
'abs error': ErrorTuple(forward=9.992502327804687e-07, reverse=None, forward_reverse=None),
'rel error': ErrorTuple(forward=1.6654173319955572e-07, reverse=None, forward_reverse=None),
'magnitude': MagnitudeTuple(forward=6.0, reverse=None, fd=5.999999000749767)},
('f_xy', 'y'): {'J_fwd': array([[8.]]),
'J_fd': array([[8.000001]]),
'abs error': ErrorTuple(forward=9.994307674787706e-07, reverse=None, forward_reverse=None),
'rel error': ErrorTuple(forward=1.2492883032763172e-07, reverse=None, forward_reverse=None),
'magnitude': MagnitudeTuple(forward=8.0, reverse=None, fd=8.000000999430767)}},
'c1c.e1': {('f_xy', 'x'): {'J_fwd': array([[-6.]]),
'J_fd': array([[-5.999999]]),
'abs error': ErrorTuple(forward=9.992502327804687e-07, reverse=None, forward_reverse=None),
'rel error': ErrorTuple(forward=1.6654173319955572e-07, reverse=None, forward_reverse=None),
'magnitude': MagnitudeTuple(forward=6.0, reverse=None, fd=5.999999000749767)},
('f_xy', 'y'): {'J_fwd': array([[8.]]),
'J_fd': array([[8.000001]]),
'abs error': ErrorTuple(forward=9.994307674787706e-07, reverse=None, forward_reverse=None),
'rel error': ErrorTuple(forward=1.2492883032763172e-07, reverse=None, forward_reverse=None),
'magnitude': MagnitudeTuple(forward=8.0, reverse=None, fd=8.000000999430767)}},
'abc1cab': {('f_xy', 'x'): {'J_fwd': array([[-6.]]),
'J_fd': array([[-5.999999]]),
'abs error': ErrorTuple(forward=9.992502327804687e-07, reverse=None, forward_reverse=None),
'rel error': ErrorTuple(forward=1.6654173319955572e-07, reverse=None, forward_reverse=None),
'magnitude': MagnitudeTuple(forward=6.0, reverse=None, fd=5.999999000749767)},
('f_xy', 'y'): {'J_fwd': array([[8.]]),
'J_fd': array([[8.000001]]),
'abs error': ErrorTuple(forward=9.994307674787706e-07, reverse=None, forward_reverse=None),
'rel error': ErrorTuple(forward=1.2492883032763172e-07, reverse=None, forward_reverse=None),
'magnitude': MagnitudeTuple(forward=8.0, reverse=None, fd=8.000000999430767)}}}
prob.check_partials(compact_print=True, includes=['*d1', '*e1'])
------------------------------
Component: Paraboloid 'c1c.d1'
------------------------------
+-----------------+------------------+-------------+-------------+-------------+-------------+------------+
| of '<variable>' | wrt '<variable>' | calc mag. | check mag. | a(cal-chk) | r(cal-chk) | error desc |
+=================+==================+=============+=============+=============+=============+============+
| 'f_xy' | 'x' | 6.0000e+00 | 6.0000e+00 | 9.9925e-07 | 1.6654e-07 | |
+-----------------+------------------+-------------+-------------+-------------+-------------+------------+
| 'f_xy' | 'y' | 8.0000e+00 | 8.0000e+00 | 9.9943e-07 | 1.2493e-07 | |
+-----------------+------------------+-------------+-------------+-------------+-------------+------------+
------------------------------
Component: Paraboloid 'c1c.e1'
------------------------------
+-----------------+------------------+-------------+-------------+-------------+-------------+------------+
| of '<variable>' | wrt '<variable>' | calc mag. | check mag. | a(cal-chk) | r(cal-chk) | error desc |
+=================+==================+=============+=============+=============+=============+============+
| 'f_xy' | 'x' | 6.0000e+00 | 6.0000e+00 | 9.9925e-07 | 1.6654e-07 | |
+-----------------+------------------+-------------+-------------+-------------+-------------+------------+
| 'f_xy' | 'y' | 8.0000e+00 | 8.0000e+00 | 9.9943e-07 | 1.2493e-07 | |
+-----------------+------------------+-------------+-------------+-------------+-------------+------------+
-----------------------------------
Component: Paraboloid 'sss.sss2.e1'
-----------------------------------
+-----------------+------------------+-------------+-------------+-------------+-------------+------------+
| of '<variable>' | wrt '<variable>' | calc mag. | check mag. | a(cal-chk) | r(cal-chk) | error desc |
+=================+==================+=============+=============+=============+=============+============+
| 'f_xy' | 'x' | 6.0000e+00 | 6.0000e+00 | 9.9925e-07 | 1.6654e-07 | |
+-----------------+------------------+-------------+-------------+-------------+-------------+------------+
| 'f_xy' | 'y' | 8.0000e+00 | 8.0000e+00 | 9.9943e-07 | 1.2493e-07 | |
+-----------------+------------------+-------------+-------------+-------------+-------------+------------+
------------------------------
Component: Paraboloid 'sss.d1'
------------------------------
+-----------------+------------------+-------------+-------------+-------------+-------------+------------+
| of '<variable>' | wrt '<variable>' | calc mag. | check mag. | a(cal-chk) | r(cal-chk) | error desc |
+=================+==================+=============+=============+=============+=============+============+
| 'f_xy' | 'x' | 6.0000e+00 | 6.0000e+00 | 9.9925e-07 | 1.6654e-07 | |
+-----------------+------------------+-------------+-------------+-------------+-------------+------------+
| 'f_xy' | 'y' | 8.0000e+00 | 8.0000e+00 | 9.9943e-07 | 1.2493e-07 | |
+-----------------+------------------+-------------+-------------+-------------+-------------+------------+
#############################################################
Sub Jacobian with Largest Relative Error: Paraboloid 'c1c.d1'
#############################################################
+-----------------+------------------+-------------+-------------+-------------+-------------+
| of '<variable>' | wrt '<variable>' | calc mag. | check mag. | a(cal-chk) | r(cal-chk) |
+=================+==================+=============+=============+=============+=============+
| 'f_xy' | 'x' | 6.0000e+00 | 6.0000e+00 | 9.9925e-07 | 1.6654e-07 |
+-----------------+------------------+-------------+-------------+-------------+-------------+
{'c1c.d1': {('f_xy', 'x'): {'J_fwd': array([[-6.]]),
'J_fd': array([[-5.999999]]),
'abs error': ErrorTuple(forward=9.992502327804687e-07, reverse=None, forward_reverse=None),
'rel error': ErrorTuple(forward=1.6654173319955572e-07, reverse=None, forward_reverse=None),
'magnitude': MagnitudeTuple(forward=6.0, reverse=None, fd=5.999999000749767)},
('f_xy', 'y'): {'J_fwd': array([[8.]]),
'J_fd': array([[8.000001]]),
'abs error': ErrorTuple(forward=9.994307674787706e-07, reverse=None, forward_reverse=None),
'rel error': ErrorTuple(forward=1.2492883032763172e-07, reverse=None, forward_reverse=None),
'magnitude': MagnitudeTuple(forward=8.0, reverse=None, fd=8.000000999430767)}},
'c1c.e1': {('f_xy', 'x'): {'J_fwd': array([[-6.]]),
'J_fd': array([[-5.999999]]),
'abs error': ErrorTuple(forward=9.992502327804687e-07, reverse=None, forward_reverse=None),
'rel error': ErrorTuple(forward=1.6654173319955572e-07, reverse=None, forward_reverse=None),
'magnitude': MagnitudeTuple(forward=6.0, reverse=None, fd=5.999999000749767)},
('f_xy', 'y'): {'J_fwd': array([[8.]]),
'J_fd': array([[8.000001]]),
'abs error': ErrorTuple(forward=9.994307674787706e-07, reverse=None, forward_reverse=None),
'rel error': ErrorTuple(forward=1.2492883032763172e-07, reverse=None, forward_reverse=None),
'magnitude': MagnitudeTuple(forward=8.0, reverse=None, fd=8.000000999430767)}},
'sss.sss2.e1': {('f_xy', 'x'): {'J_fwd': array([[-6.]]),
'J_fd': array([[-5.999999]]),
'abs error': ErrorTuple(forward=9.992502327804687e-07, reverse=None, forward_reverse=None),
'rel error': ErrorTuple(forward=1.6654173319955572e-07, reverse=None, forward_reverse=None),
'magnitude': MagnitudeTuple(forward=6.0, reverse=None, fd=5.999999000749767)},
('f_xy', 'y'): {'J_fwd': array([[8.]]),
'J_fd': array([[8.000001]]),
'abs error': ErrorTuple(forward=9.994307674787706e-07, reverse=None, forward_reverse=None),
'rel error': ErrorTuple(forward=1.2492883032763172e-07, reverse=None, forward_reverse=None),
'magnitude': MagnitudeTuple(forward=8.0, reverse=None, fd=8.000000999430767)}},
'sss.d1': {('f_xy', 'x'): {'J_fwd': array([[-6.]]),
'J_fd': array([[-5.999999]]),
'abs error': ErrorTuple(forward=9.992502327804687e-07, reverse=None, forward_reverse=None),
'rel error': ErrorTuple(forward=1.6654173319955572e-07, reverse=None, forward_reverse=None),
'magnitude': MagnitudeTuple(forward=6.0, reverse=None, fd=5.999999000749767)},
('f_xy', 'y'): {'J_fwd': array([[8.]]),
'J_fd': array([[8.000001]]),
'abs error': ErrorTuple(forward=9.994307674787706e-07, reverse=None, forward_reverse=None),
'rel error': ErrorTuple(forward=1.2492883032763172e-07, reverse=None, forward_reverse=None),
'magnitude': MagnitudeTuple(forward=8.0, reverse=None, fd=8.000000999430767)}}}
prob.check_partials(compact_print=True, includes=['abc1cab'])
-------------------------------
Component: Paraboloid 'abc1cab'
-------------------------------
+-----------------+------------------+-------------+-------------+-------------+-------------+------------+
| of '<variable>' | wrt '<variable>' | calc mag. | check mag. | a(cal-chk) | r(cal-chk) | error desc |
+=================+==================+=============+=============+=============+=============+============+
| 'f_xy' | 'x' | 6.0000e+00 | 6.0000e+00 | 9.9925e-07 | 1.6654e-07 | |
+-----------------+------------------+-------------+-------------+-------------+-------------+------------+
| 'f_xy' | 'y' | 8.0000e+00 | 8.0000e+00 | 9.9943e-07 | 1.2493e-07 | |
+-----------------+------------------+-------------+-------------+-------------+-------------+------------+
##############################################################
Sub Jacobian with Largest Relative Error: Paraboloid 'abc1cab'
##############################################################
+-----------------+------------------+-------------+-------------+-------------+-------------+
| of '<variable>' | wrt '<variable>' | calc mag. | check mag. | a(cal-chk) | r(cal-chk) |
+=================+==================+=============+=============+=============+=============+
| 'f_xy' | 'x' | 6.0000e+00 | 6.0000e+00 | 9.9925e-07 | 1.6654e-07 |
+-----------------+------------------+-------------+-------------+-------------+-------------+
{'abc1cab': {('f_xy', 'x'): {'J_fwd': array([[-6.]]),
'J_fd': array([[-5.999999]]),
'abs error': ErrorTuple(forward=9.992502327804687e-07, reverse=None, forward_reverse=None),
'rel error': ErrorTuple(forward=1.6654173319955572e-07, reverse=None, forward_reverse=None),
'magnitude': MagnitudeTuple(forward=6.0, reverse=None, fd=5.999999000749767)},
('f_xy', 'y'): {'J_fwd': array([[8.]]),
'J_fd': array([[8.000001]]),
'abs error': ErrorTuple(forward=9.994307674787706e-07, reverse=None, forward_reverse=None),
'rel error': ErrorTuple(forward=1.2492883032763172e-07, reverse=None, forward_reverse=None),
'magnitude': MagnitudeTuple(forward=8.0, reverse=None, fd=8.000000999430767)}}}
prob.check_partials(compact_print=True, includes='*c*c*', excludes=['*e*'])
------------------------------
Component: Paraboloid 'c1c.d1'
------------------------------
+-----------------+------------------+-------------+-------------+-------------+-------------+------------+
| of '<variable>' | wrt '<variable>' | calc mag. | check mag. | a(cal-chk) | r(cal-chk) | error desc |
+=================+==================+=============+=============+=============+=============+============+
| 'f_xy' | 'x' | 6.0000e+00 | 6.0000e+00 | 9.9925e-07 | 1.6654e-07 | |
+-----------------+------------------+-------------+-------------+-------------+-------------+------------+
| 'f_xy' | 'y' | 8.0000e+00 | 8.0000e+00 | 9.9943e-07 | 1.2493e-07 | |
+-----------------+------------------+-------------+-------------+-------------+-------------+------------+
-------------------------------
Component: Paraboloid 'abc1cab'
-------------------------------
+-----------------+------------------+-------------+-------------+-------------+-------------+------------+
| of '<variable>' | wrt '<variable>' | calc mag. | check mag. | a(cal-chk) | r(cal-chk) | error desc |
+=================+==================+=============+=============+=============+=============+============+
| 'f_xy' | 'x' | 6.0000e+00 | 6.0000e+00 | 9.9925e-07 | 1.6654e-07 | |
+-----------------+------------------+-------------+-------------+-------------+-------------+------------+
| 'f_xy' | 'y' | 8.0000e+00 | 8.0000e+00 | 9.9943e-07 | 1.2493e-07 | |
+-----------------+------------------+-------------+-------------+-------------+-------------+------------+
#############################################################
Sub Jacobian with Largest Relative Error: Paraboloid 'c1c.d1'
#############################################################
+-----------------+------------------+-------------+-------------+-------------+-------------+
| of '<variable>' | wrt '<variable>' | calc mag. | check mag. | a(cal-chk) | r(cal-chk) |
+=================+==================+=============+=============+=============+=============+
| 'f_xy' | 'x' | 6.0000e+00 | 6.0000e+00 | 9.9925e-07 | 1.6654e-07 |
+-----------------+------------------+-------------+-------------+-------------+-------------+
{'c1c.d1': {('f_xy', 'x'): {'J_fwd': array([[-6.]]),
'J_fd': array([[-5.999999]]),
'abs error': ErrorTuple(forward=9.992502327804687e-07, reverse=None, forward_reverse=None),
'rel error': ErrorTuple(forward=1.6654173319955572e-07, reverse=None, forward_reverse=None),
'magnitude': MagnitudeTuple(forward=6.0, reverse=None, fd=5.999999000749767)},
('f_xy', 'y'): {'J_fwd': array([[8.]]),
'J_fd': array([[8.000001]]),
'abs error': ErrorTuple(forward=9.994307674787706e-07, reverse=None, forward_reverse=None),
'rel error': ErrorTuple(forward=1.2492883032763172e-07, reverse=None, forward_reverse=None),
'magnitude': MagnitudeTuple(forward=8.0, reverse=None, fd=8.000000999430767)}},
'abc1cab': {('f_xy', 'x'): {'J_fwd': array([[-6.]]),
'J_fd': array([[-5.999999]]),
'abs error': ErrorTuple(forward=9.992502327804687e-07, reverse=None, forward_reverse=None),
'rel error': ErrorTuple(forward=1.6654173319955572e-07, reverse=None, forward_reverse=None),
'magnitude': MagnitudeTuple(forward=6.0, reverse=None, fd=5.999999000749767)},
('f_xy', 'y'): {'J_fwd': array([[8.]]),
'J_fd': array([[8.000001]]),
'abs error': ErrorTuple(forward=9.994307674787706e-07, reverse=None, forward_reverse=None),
'rel error': ErrorTuple(forward=1.2492883032763172e-07, reverse=None, forward_reverse=None),
'magnitude': MagnitudeTuple(forward=8.0, reverse=None, fd=8.000000999430767)}}}