/
test_examples.py
389 lines (284 loc) · 12.5 KB
/
test_examples.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
""" Test the examples directory to keep them in working order.
NOTE: If you make any changes to this file, you must make the corresponding
change to the example file.
"""
import unittest
from six.moves import cStringIO
import numpy as np
from openmdao.api import Problem, Group, IndepVarComp, ExecComp, ScipyOptimizer, \
Newton, ScipyGMRES
from openmdao.test.util import assert_rel_error
from beam_tutorial import BeamTutorial
from fd_comp_example import Model as Model1
from fd_group_example import Model as Model2
from fd_model_example import Model as Model3
from implicit import SimpleImplicitComp
from implicit_ext_solve import SimpleImplicitComp as SIC2
from intersect_parabola_line import Balance, Parabola, Line
from krig_sin import TrigMM
from paraboloid_example import Paraboloid
from paraboloid_optimize_constrained import Paraboloid as ParaboloidOptCon
from paraboloid_optimize_unconstrained import Paraboloid as ParaboloidOptUnCon
from sellar_MDF_optimize import SellarDerivatives
from sellar_state_MDF_optimize import SellarStateConnection
from sellar_sand_architecture import SellarSAND
class TestExamples(unittest.TestCase):
def test_paraboloid_example(self):
top = Problem()
root = top.root = Group()
root.add('p1', IndepVarComp('x', 3.0))
root.add('p2', IndepVarComp('y', -4.0))
root.add('p', Paraboloid())
root.connect('p1.x', 'p.x')
root.connect('p2.y', 'p.y')
top.setup(check=False)
top.run()
assert_rel_error(self, root.p.unknowns['f_xy'], -15.0, 1e-6)
def test_paraboloid_optimize_constrained(self):
top = Problem()
root = top.root = Group()
root.add('p1', IndepVarComp('x', 3.0))
root.add('p2', IndepVarComp('y', -4.0))
root.add('p', ParaboloidOptCon())
# Constraint Equation
root.add('con', ExecComp('c = x-y'))
root.connect('p1.x', 'p.x')
root.connect('p2.y', 'p.y')
root.connect('p.x', 'con.x')
root.connect('p.y', 'con.y')
top.driver = ScipyOptimizer()
top.driver.options['optimizer'] = 'SLSQP'
top.driver.options['disp'] = False
top.driver.add_desvar('p1.x', lower=-50, upper=50)
top.driver.add_desvar('p2.y', lower=-50, upper=50)
top.driver.add_objective('p.f_xy')
top.driver.add_constraint('con.c', lower=15.0)
top.setup(check=False)
top.run()
assert_rel_error(self, top['p.x'], 7.166667, 1e-6)
assert_rel_error(self, top['p.y'], -7.833333, 1e-6)
def test_paraboloid_optimize_unconstrained(self):
top = Problem()
root = top.root = Group()
root.add('p1', IndepVarComp('x', 3.0))
root.add('p2', IndepVarComp('y', -4.0))
root.add('p', ParaboloidOptUnCon())
root.connect('p1.x', 'p.x')
root.connect('p2.y', 'p.y')
top.driver = ScipyOptimizer()
top.driver.options['optimizer'] = 'SLSQP'
top.driver.options['disp'] = False
top.driver.add_desvar('p1.x', lower=-50, upper=50)
top.driver.add_desvar('p2.y', lower=-50, upper=50)
top.driver.add_objective('p.f_xy')
top.setup(check=False)
top.run()
assert_rel_error(self, top['p.x'], 6.666667, 1e-6)
assert_rel_error(self, top['p.y'], -7.333333, 1e-6)
def test_beam_tutorial(self):
top = Problem()
top.root = BeamTutorial()
top.driver = ScipyOptimizer()
top.driver.options['optimizer'] = 'SLSQP'
top.driver.options['tol'] = 1.0e-8
top.driver.options['maxiter'] = 10000 #maximum number of solver iterations
top.driver.options['disp'] = False
#room length and width bounds
top.driver.add_desvar('ivc_rlength.room_length', lower=5.0*12.0, upper=50.0*12.0) #domain: 1in <= length <= 50ft
top.driver.add_desvar('ivc_rwidth.room_width', lower=5.0*12.0, upper=30.0*12.0) #domain: 1in <= width <= 30ft
top.driver.add_objective('d_neg_area.neg_room_area') #minimize negative area (or maximize area)
top.driver.add_constraint('d_len_minus_wid.length_minus_width', lower=0.0) #room_length >= room_width
top.driver.add_constraint('d_deflection.deflection', lower=720.0) #deflection >= 720
top.driver.add_constraint('d_bending.bending_stress_ratio', upper=0.5) #bending < 0.5
top.driver.add_constraint('d_shear.shear_stress_ratio', upper=1.0/3.0) #shear < 1/3
top.setup(check=False)
top.run()
assert_rel_error(self, -top['d_neg_area.neg_room_area'], 51655.257618, .01)
assert_rel_error(self, top['ivc_rwidth.room_width'], 227.277956, .01)
assert_rel_error(self,top['ivc_rlength.room_length'], 227.277904, .01)
assert_rel_error(self,top['d_deflection.deflection'], 720, .01)
assert_rel_error(self,top['d_bending.bending_stress_ratio'], 0.148863, .001)
assert_rel_error(self,top['d_shear.shear_stress_ratio'], 0.007985, .0001)
def test_line_parabola_intersect(self):
from intersect_parabola_line import Line, Parabola, Balance
top = Problem()
root = top.root = Group()
root.add('line', Line())
root.add('parabola', Parabola())
root.add('bal', Balance())
root.connect('line.y', 'bal.y1')
root.connect('parabola.y', 'bal.y2')
root.connect('bal.x', 'line.x')
root.connect('bal.x', 'parabola.x')
root.nl_solver = Newton()
root.ln_solver = ScipyGMRES()
top.setup(check=False)
stream = cStringIO()
# Positive solution
top['bal.x'] = 7.0
root.list_states(stream)
top.run()
assert_rel_error(self, top['bal.x'], 1.430501, 1e-5)
assert_rel_error(self, top['line.y'], 1.138998, 1e-5)
# Negative solution
top['bal.x'] = -7.0
root.list_states(stream)
top.run()
assert_rel_error(self, top['bal.x'], -2.097168, 1e-5)
assert_rel_error(self, top['line.y'], 8.194335, 1e-5)
def test_sellar_MDF_optimize(self):
top = Problem()
top.root = SellarDerivatives()
top.driver = ScipyOptimizer()
top.driver.options['optimizer'] = 'SLSQP'
top.driver.options['tol'] = 1.0e-8
top.driver.options['disp'] = False
top.driver.add_desvar('z', lower=np.array([-10.0, 0.0]),
upper=np.array([10.0, 10.0]))
top.driver.add_desvar('x', lower=0.0, upper=10.0)
top.driver.add_objective('obj')
top.driver.add_constraint('con1', upper=0.0)
top.driver.add_constraint('con2', upper=0.0)
top.setup(check=False)
top.run()
assert_rel_error(self, top['z'][0], 1.977639, 1e-5)
assert_rel_error(self, top['z'][1], 0.0, 1e-5)
assert_rel_error(self, top['x'], 0.0, 1e-5)
assert_rel_error(self, top['obj'], 3.1833940, 1e-5)
def test_sellar_state_connection(self):
top = Problem()
top.root = SellarStateConnection()
top.driver = ScipyOptimizer()
top.driver.options['optimizer'] = 'SLSQP'
top.driver.options['tol'] = 1.0e-8
top.driver.options['disp'] = False
top.driver.add_desvar('z', lower=np.array([-10.0, 0.0]),
upper=np.array([10.0, 10.0]))
top.driver.add_desvar('x', lower=0.0, upper=10.0)
top.driver.add_objective('obj')
top.driver.add_constraint('con1', upper=0.0)
top.driver.add_constraint('con2', upper=0.0)
top.setup(check=False)
top.run()
assert_rel_error(self, top['z'][0], 1.977639, 1e-5)
assert_rel_error(self, top['z'][1], 0.0, 1e-5)
assert_rel_error(self, top['x'], 0.0, 1e-5)
assert_rel_error(self, top['obj'], 3.1833940, 1e-5)
def test_intersect_parabola_line(self):
top = Problem()
root = top.root = Group()
root.add('line', Line())
root.add('parabola', Parabola())
root.add('bal', Balance())
root.connect('line.y', 'bal.y1')
root.connect('parabola.y', 'bal.y2')
root.connect('bal.x', 'line.x')
root.connect('bal.x', 'parabola.x')
root.nl_solver = Newton()
root.ln_solver = ScipyGMRES()
top.setup(check=False)
# Positive solution
top['bal.x'] = 7.0
top.run()
assert_rel_error(self, top['bal.x'], 1.430501, 1e-5)
assert_rel_error(self, top['line.y'], 1.1389998, 1e-5)
# Negative solution
top['bal.x'] = -7.0
top.run()
assert_rel_error(self, top['bal.x'], -2.097168, 1e-5)
assert_rel_error(self, top['line.y'], 8.194335, 1e-5)
def test_implicit(self):
top = Problem()
root = top.root = Group()
root.add('comp', SimpleImplicitComp())
root.ln_solver = ScipyGMRES()
top.setup(check=False)
top.run()
assert_rel_error(self, top['comp.z'], 2.666667, 1e-5)
def test_implicit_ext_solve(self):
top = Problem()
root = top.root = Group()
root.add('p1', IndepVarComp('x', 0.5))
root.add('comp', SimpleImplicitComp())
root.add('comp2', ExecComp('zz = 2.0*z'))
root.connect('p1.x', 'comp.x')
root.connect('comp.z', 'comp2.z')
root.ln_solver = ScipyGMRES()
root.nl_solver = Newton()
top.setup(check=False)
top.run()
assert_rel_error(self, top['comp.z'], 2.666667, 1e-5)
def test_fd_comp_example(self):
top = Problem()
top.root = Model1()
top.setup(check=False)
top.root.comp1.print_output = False
top.root.comp2.print_output = False
top.root.comp3.print_output = False
top.root.comp4.print_output = False
top.run()
J = top.calc_gradient(['px.x'], ['comp4.y'])
assert_rel_error(self, J[0][0], 81.0, 1e-5)
def test_fd_group_example(self):
top = Problem()
top.root = Model2()
top.setup(check=False)
top.root.comp1.print_output = False
top.root.sub.comp2.print_output = False
top.root.sub.comp3.print_output = False
top.root.comp4.print_output = False
top.run()
J = top.calc_gradient(['px.x'], ['comp4.y'])
assert_rel_error(self, J[0][0], 81.0, 1e-5)
def test_fd_model_example(self):
top = Problem()
top.root = Model3()
top.setup(check=False)
top.root.comp1.print_output = False
top.root.comp2.print_output = False
top.root.comp3.print_output = False
top.root.comp4.print_output = False
top.run()
J = top.calc_gradient(['px.x'], ['comp4.y'])
assert_rel_error(self, J[0][0], 81.0, 1e-5)
def test_krig_sin(self):
prob = Problem()
prob.root = TrigMM()
prob.setup(check=False)
#traning data is just set manually. No connected input needed, since
# we're assuming the data is pre-existing
prob['sin_mm.train:x'] = np.linspace(0,10,20)
prob['sin_mm.train:f_x:float'] = np.sin(prob['sin_mm.train:x'])
prob['sin_mm.train:f_x:norm_dist'] = np.cos(prob['sin_mm.train:x'])
prob['sin_mm.x'] = 2.1 #prediction happens at this value
prob.run()
assert_rel_error(self, prob['sin_mm.f_x:float'], 0.8632, 1e-3)
assert_rel_error(self, prob['sin_mm.f_x:norm_dist'][0], -0.5048, 1e-3)
def test_sellar_sand_architecture(self):
top = Problem()
top.root = SellarSAND()
top.driver = ScipyOptimizer()
top.driver.options['optimizer'] = 'SLSQP'
top.driver.options['tol'] = 1.0e-12
top.driver.add_desvar('z', lower=np.array([-10.0, 0.0]),upper=np.array([10.0, 10.0]))
top.driver.add_desvar('x', lower=0.0, upper=10.0)
top.driver.add_desvar('y1', lower=-10.0, upper=10.0)
top.driver.add_desvar('y2', lower=-10.0, upper=10.0)
top.driver.add_objective('obj')
top.driver.add_constraint('con1', upper=0.0)
top.driver.add_constraint('con2', upper=0.0)
top.driver.add_constraint('resid1', equals=0.0)
top.driver.add_constraint('resid2', equals=0.0)
top.setup()
top.run()
assert_rel_error(self, top['z'][0], 1.9776, 1e-3)
assert_rel_error(self, top['z'][1], 0.0000, 1e-3)
assert_rel_error(self, top['x'], 0.0000, 1e-3)
assert_rel_error(self, top['d1.y1'], 3.1600, 1e-3)
assert_rel_error(self, top['d1.y2'], 3.7553, 1e-3)
assert_rel_error(self, top['obj'], 3.1834, 1e-3)
# Minimum found at (z1,z2,x) = (1.9776, 0.0000, 0.0000)
# Coupling vars: 3.1600, 3.7553
# Minimum objective: 3.1834
if __name__ == "__main__":
unittest.main()