Skip to content

Commit

Permalink
Merge pull request #928 from Kenneth-T-Moore/ken4
Browse files Browse the repository at this point in the history
Better reporting of relative error when fd is zero during drivative checks.
  • Loading branch information
swryan committed May 15, 2019
2 parents c79e6b0 + 3ee1f13 commit 9c861fb
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
11 changes: 10 additions & 1 deletion openmdao/core/problem.py
Expand Up @@ -1770,7 +1770,16 @@ def _assemble_derivative_data(derivative_data, rel_error_tol, abs_error_tol, out
derivative_info['magnitude'] = magnitude = MagnitudeTuple(fwd_norm, rev_norm, fd_norm)

if fd_norm == 0.:
derivative_info['rel error'] = rel_err = ErrorTuple(nan, nan, nan)
if fwd_norm == 0.:
derivative_info['rel error'] = rel_err = ErrorTuple(nan, nan, nan)

else:
# If fd_norm is zero, let's use fwd_norm as the divisor for relative
# check. That way we don't accidentally squelch a legitimate problem.
derivative_info['rel error'] = rel_err = ErrorTuple(fwd_error / fwd_norm,
rev_error / fwd_norm,
fwd_rev_error / fwd_norm)

else:
if totals:
derivative_info['rel error'] = rel_err = ErrorTuple(fwd_error / fd_norm,
Expand Down
33 changes: 33 additions & 0 deletions openmdao/core/tests/test_check_derivs.py
Expand Up @@ -1485,6 +1485,39 @@ def test_bug_local_method(self):
for key, val in iteritems(data['comp3']):
assert_rel_error(self, val['rel error'][0], 0.0, 1e-15)

def test_rel_error_fd_zero(self):
# When the fd turns out to be zero, test that we switch the definition of relative
# to divide by the forward derivative instead of reporting NaN.

class SimpleComp2(ExplicitComponent):
def setup(self):
self.add_input('x', val=3.0)
self.add_output('y', val=4.0)

self.declare_partials(of='y', wrt='x')

def compute(self, inputs, outputs):
# Mimics forgetting to set a variable.
pass

def compute_partials(self, inputs, partials):
partials['y', 'x'] = 3.0

prob = Problem()
prob.model = Group()

prob.model.add_subsystem('p1', IndepVarComp('x', 3.5))
prob.model.add_subsystem('comp', SimpleComp2())
prob.model.connect('p1.x', 'comp.x')

prob.setup(check=False)

stream = cStringIO()
data = prob.check_partials(out_stream=stream)
lines = stream.getvalue().splitlines()

self.assertTrue("Relative Error (Jfor - Jfd) : 1." in lines[8])


class TestCheckPartialsFeature(unittest.TestCase):

Expand Down

0 comments on commit 9c861fb

Please sign in to comment.