Skip to content

Instantly share code, notes, and snippets.

@bbrelje
Last active July 27, 2020 20:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bbrelje/de79ffcbb62e6f339bb5649be05a3228 to your computer and use it in GitHub Desktop.
Save bbrelje/de79ffcbb62e6f339bb5649be05a3228 to your computer and use it in GitHub Desktop.
import openmdao.api as om
import numpy as np
class WorkingGroup(om.Group):
def setup(self):
iv = self.add_subsystem('iv', om.IndepVarComp('x2', val=3.0*np.ones((2,))))
iv.add_output('x', 2.0)
self.add_subsystem('ec1', om.ExecComp('y=x', x={'value':1.0}))
self.add_subsystem('ec2', om.ExecComp('y2=x2', y2={'value':np.ones((2,))}, x2={'value':np.ones((2,))}))
self.connect('iv.x2', 'ec2.x2')
self.connect('iv.x', 'ec1.x')
self.set_order(['iv','ec2','ec1'])
class BrokenGroup(om.Group):
def setup(self):
iv = self.add_subsystem('iv', om.IndepVarComp('x2', val=3.0*np.ones((2,))))
iv.add_output('x', 2.0)
self.add_subsystem('ec1', om.ExecComp('y=x', x={'value':1.0}))
self.add_subsystem('ec2', om.ExecComp('y2=x2', y2={'value':np.ones((2,))}, x2={'value':np.ones((2,))}))
self.connect('iv.x2', 'ec2.x2')
self.connect('iv.x', 'ec1.x')
def configure(self):
self.set_order(['iv','ec2','ec1'])
class PotentiallyScaryGroup(om.Group):
def setup(self):
iv = self.add_subsystem('iv', om.IndepVarComp('x2', val=3.0))
iv.add_output('x', 2.0)
self.add_subsystem('ec1', om.ExecComp('y=x', x={'value':1.0}))
self.add_subsystem('ec2', om.ExecComp('y2=x2', x2={'value':1.0}))
self.connect('iv.x2', 'ec2.x2')
self.connect('iv.x', 'ec1.x')
def configure(self):
self.set_order(['iv','ec2','ec1'])
if __name__ == "__main__":
print('================ Run a group that works - reorder done in setup')
prob = om.Problem(WorkingGroup())
prob.setup()
prob.run_model()
prob.model.list_inputs(print_arrays=True)
print('================ Run a group that shows values go to the right place when the dimensions match - reorder done in configure')
prob = om.Problem(PotentiallyScaryGroup())
prob.setup()
prob.run_model()
prob.model.list_inputs(print_arrays=True)
print('================ Run a group that fails - reorder done in configure')
prob = om.Problem(BrokenGroup())
prob.setup()
prob.run_model()
prob.model.list_inputs(print_arrays=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment