/
paraboloid.py
65 lines (48 loc) · 1.62 KB
/
paraboloid.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
""" Definition of the Paraboloid component, which evaluates the equation
(x-3)^2 + xy + (y+4)^2 = 3
"""
from __future__ import division, print_function
import openmdao.api as om
class Paraboloid(om.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('*', '*')
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.
"""
x = inputs['x']
y = inputs['y']
partials['f_xy', 'x'] = 2.0*x - 6.0 + y
partials['f_xy', 'y'] = 2.0*y + 8.0 + x
if __name__ == "__main__":
model = om.Group()
ivc = om.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 = om.Problem(model)
prob.driver = om.ScipyOptimizeDriver() # so 'openmdao cite' will report it for cite docs
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()
print(prob['parab_comp.f_xy'])