Skip to content

Instantly share code, notes, and snippets.

@bbrelje
Created May 8, 2020 02:36
Show Gist options
  • Save bbrelje/7f9deabd735d2edd53f88e0596d4b1e1 to your computer and use it in GitHub Desktop.
Save bbrelje/7f9deabd735d2edd53f88e0596d4b1e1 to your computer and use it in GitHub Desktop.
Check_totals fails when objective is an output of a parallel component
import numpy as np
import openmdao.api as om
import time
class DelayComp(om.ExplicitComponent):
def initialize(self):
self.options.declare('time', default=3.0)
self.options.declare('size', default=1)
def setup(self):
size = self.options['size']
self.add_input('x', shape=size)
self.add_output('y', shape=size)
self.declare_partials('y', 'x')
def compute(self, inputs, outputs):
waittime = self.options['time']
if not inputs._under_complex_step:
print('sleeping: ')
time.sleep(waittime)
outputs['y'] = inputs['x']
def compute_partials(self, inputs, J):
size = self.options['size']
J['y', 'x'] = np.eye(size)
if __name__ == "__main__":
model = om.Group()
iv = om.IndepVarComp()
size = 1
iv.add_output('x', val=3.0 * np.ones((size, )))
model.add_subsystem('iv', iv)
pg = model.add_subsystem('pg', om.ParallelGroup(), promotes=['*'])
pg.add_subsystem('dc1', DelayComp(size=size, time=2.0))
pg.add_subsystem('dc2', DelayComp(size=size, time=2.0))
pg.add_subsystem('dc3', DelayComp(size=size, time=2.0))
model.connect('iv.x', ['dc1.x', 'dc2.x', 'dc3.x'])
model.add_subsystem('adder', om.ExecComp('z = sum(y1)+sum(y2)+sum(y3)', y1={'value': np.zeros((size, ))},
y2={'value': np.zeros((size, ))},
y3={'value': np.zeros((size, ))}))
model.connect('dc1.y', 'adder.y1')
model.connect('dc2.y', 'adder.y2')
model.connect('dc3.y', 'adder.y3')
model.add_design_var('iv.x', lower=-1.0, upper=1.0)
# this objective works fine
# model.add_objective('adder.z')
# this objective raises a concatenation error whether under fd or cs
model.add_objective('dc1.y')
# for some reason this constraint is fine even though only lives on proc 3
model.add_constraint('dc3.y', lower=-1.0, upper=1.0)
prob = om.Problem(model=model)
prob.setup(force_alloc_complex=True)
prob.run_model()
# prob.model.list_outputs(print_arrays=True)
prob.check_totals(method='cs', compact_print=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment