Skip to content

Commit

Permalink
Merge pull request #1723 from Kenneth-T-Moore/ken5
Browse files Browse the repository at this point in the history
Fixed a bug where unit conversions were being ignored for driver derivatives.
  • Loading branch information
swryan committed Oct 5, 2020
2 parents 3aac08a + 20001f0 commit 2f75102
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
25 changes: 25 additions & 0 deletions openmdao/core/tests/test_driver.py
Expand Up @@ -10,6 +10,7 @@

import openmdao.api as om
from openmdao.core.driver import Driver
from openmdao.utils.units import convert_units
from openmdao.utils.assert_utils import assert_near_equal, assert_warning
from openmdao.utils.general_utils import printoptions
from openmdao.utils.testing_utils import use_tempdirs
Expand Down Expand Up @@ -638,6 +639,30 @@ def test_units_with_scaling(self):
con = case.get_constraints()
assert_near_equal(con['comp1.y1'][0], ((38.0 * 5 / 9) + 77.0) * 3.5, 1e-8)

def test_units_compute_totals(self):
p = om.Problem()

p.model.add_subsystem('stuff', om.ExecComp(['y = x', 'cy = x'],
x={'units': 'inch'},
y={'units': 'kg'},
cy={'units': 'kg'}),
promotes=['*'])

p.model.add_design_var('x', units='ft')
p.model.add_objective('y', units='lbm')
p.model.add_constraint('cy', units='lbm', lower=0)

p.setup()

p['x'] = 1.0
p.run_model()

J_driver = p.driver._compute_totals()

fact = convert_units(1.0, 'kg/inch', 'lbm/ft')
assert_near_equal(J_driver['stuff.y', 'x'][0,0], fact, 1e-5)
assert_near_equal(J_driver['stuff.cy', 'x'][0,0], fact, 1e-5)

def test_units_error_messages(self):
prob = om.Problem()
model = prob.model
Expand Down
8 changes: 4 additions & 4 deletions openmdao/core/total_jac.py
Expand Up @@ -1602,10 +1602,10 @@ def _do_driver_scaling(self, J):

if self.return_format in ('dict', 'array'):
for prom_out, odict in J.items():
oscaler = responses[prom_out]['scaler']
oscaler = responses[prom_out]['total_scaler']

for prom_in, val in odict.items():
iscaler = desvars[prom_in]['scaler']
iscaler = desvars[prom_in]['total_scaler']

# Scale response side
if oscaler is not None:
Expand All @@ -1618,8 +1618,8 @@ def _do_driver_scaling(self, J):
elif self.return_format == 'flat_dict':
for tup, val in J.items():
prom_out, prom_in = tup
oscaler = responses[prom_out]['scaler']
iscaler = desvars[prom_in]['scaler']
oscaler = responses[prom_out]['total_scaler']
iscaler = desvars[prom_in]['total_scaler']

# Scale response side
if oscaler is not None:
Expand Down

0 comments on commit 2f75102

Please sign in to comment.