Skip to content

Instantly share code, notes, and snippets.

@thearn
Created August 28, 2018 14:37
Show Gist options
  • Save thearn/8b9a793541b5f3a4055723b71358a6f0 to your computer and use it in GitHub Desktop.
Save thearn/8b9a793541b5f3a4055723b71358a6f0 to your computer and use it in GitHub Desktop.
from openmdao.core.explicitcomponent import ExplicitComponent
from openmdao.api import Problem, Group, ExecComp, IndepVarComp
from openmdao.devtools.generate_derivative import GenerateDerivative
class Paraboloid(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)
self.declare_partials('*', '*')
@GenerateDerivative
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.
"""
return
model = Group()
ivc = IndepVarComp()
ivc.add_output('x', 3.0)
ivc.add_output('y', -4.0)
model.add_subsystem('des_vars', ivc)
model.add_subsystem('parab_comp', Paraboloid())
model.connect('des_vars.x', 'parab_comp.x')
model.connect('des_vars.y', 'parab_comp.y')
prob = Problem(model)
prob.setup()
prob.run_model()
print(prob['parab_comp.f_xy'])
prob['des_vars.x'] = 5.0
prob['des_vars.y'] = -2.0
prob.run_model()
prob.check_partials(compact_print=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment