-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathtest_om_gradients.py
309 lines (248 loc) · 12.9 KB
/
test_om_gradients.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
import os
import unittest
import numpy as np
import openmdao.api as om
from openmdao.utils.assert_utils import assert_check_partials
from ccblade.ccblade_component import CCBladeLoads, CCBladeTwist, CCBladeEvaluate, CCBladeGeometry
np.random.seed(314)
class Test(unittest.TestCase):
def test_ccblade_geometry(self):
n_span = 10
prob = om.Problem()
comp = CCBladeGeometry(n_span=n_span)
prob.model.add_subsystem("comp", comp, promotes=["*"])
prob.setup(force_alloc_complex=True)
prob.set_val("Rtip", 80.0, units="m")
prob.set_val("precurve_in", np.random.rand(n_span), units="m")
prob.set_val("presweep_in", np.random.rand(n_span), units="m")
prob.set_val("precone", 2.2, units="deg")
prob.run_model()
check = prob.check_partials(out_stream=None, compact_print=True, method="fd")
assert_check_partials(check)
def test_ccblade_loads(self):
prob = om.Problem()
# Load in airfoil and blade shape inputs for NREL 5MW
npzfile = np.load(
os.path.dirname(os.path.abspath(__file__)) + os.path.sep + "smaller_dataset.npz", allow_pickle=True
)
n_span = npzfile["r"].size
n_aoa = npzfile["aoa"].size
n_Re = npzfile["Re"].size
modeling_options = {}
modeling_options["WISDEM"] = {}
modeling_options["WISDEM"]["RotorSE"] = {}
modeling_options["WISDEM"]["RotorSE"]["n_span"] = n_span
modeling_options["WISDEM"]["RotorSE"]["n_aoa"] = n_aoa
modeling_options["WISDEM"]["RotorSE"]["n_Re"] = n_Re
modeling_options["WISDEM"]["RotorSE"]["n_tab"] = 1
n_span, n_aoa, n_Re, n_tab = np.moveaxis(npzfile["cl"][:, :, :, np.newaxis], 0, 1).shape
modeling_options["airfoils"] = {}
modeling_options["airfoils"]["n_aoa"] = n_aoa
modeling_options["airfoils"]["n_Re"] = n_Re
modeling_options["airfoils"]["n_tab"] = n_tab
comp = CCBladeLoads(modeling_options=modeling_options)
prob.model.add_subsystem("comp", comp, promotes=["*"])
prob.setup(force_alloc_complex=True)
# Add some arbitrary inputs
prob.set_val("airfoils_aoa", npzfile["aoa"], units="deg")
prob.set_val("airfoils_Re", npzfile["Re"])
prob.set_val("airfoils_cl", np.moveaxis(npzfile["cl"][:, :, :, np.newaxis], 0, 1))
prob.set_val("airfoils_cd", np.moveaxis(npzfile["cd"][:, :, :, np.newaxis], 0, 1))
prob.set_val("airfoils_cm", np.moveaxis(npzfile["cm"][:, :, :, np.newaxis], 0, 1))
prob.set_val("r", npzfile["r"], units="m")
prob.set_val("chord", npzfile["chord"], units="m")
prob.set_val("theta", npzfile["theta"], units="deg")
# parameters
prob.set_val("V_load", 12.0, units="m/s")
prob.set_val("Omega_load", 7.0, units="rpm")
prob.set_val("pitch_load", 2.0, units="deg")
prob.set_val("azimuth_load", 3.0, units="deg")
prob.set_val("Rhub", 1.0, units="m")
prob.set_val("Rtip", 70.0, units="m")
prob.set_val("hub_height", 100.0, units="m")
prob.set_val("precone", 0.0, units="deg")
prob.set_val("tilt", 0.0, units="deg")
prob.set_val("yaw", 0.0, units="deg")
prob.set_val("precurve", np.zeros(n_span), units="m")
prob.set_val("precurveTip", 0.0, units="m")
prob.set_val("rho", 1.225, units="kg/m**3")
prob.set_val("mu", 1.81206e-5, units="kg/(m*s)")
prob.set_val("shearExp", 0.25)
prob.set_val("nBlades", 3)
prob.set_val("nSector", 4)
prob.set_val("tiploss", True)
prob.set_val("hubloss", True)
prob.set_val("wakerotation", True)
prob.set_val("usecd", True)
prob.run_model()
check = prob.check_partials(out_stream=None, compact_print=True)
# Manually filter some entries out of the assert_check_partials call.
# Will want to add this functionality to OpenMDAO itself at some point.
new_check = {}
for comp_name in check:
new_check[comp_name] = {}
for (output_name, input_name) in check[comp_name]:
if "airfoil" not in input_name and "rho" not in input_name and "mu" not in input_name:
new_check[comp_name][(output_name, input_name)] = check[comp_name][(output_name, input_name)]
assert_check_partials(new_check, rtol=5e-5, atol=10.)
@unittest.skip("Not useful and now OpenMDAO complains")
def test_ccblade_twist(self):
"""
Right now this just compares fd to fd so it is not a meaningful test.
However, it ensures that we have the derivatives set up in the component
to actually be finite differenced.
"""
prob = om.Problem()
# Add some arbitrary inputs
# Load in airfoil and blade shape inputs for NREL 5MW
npzfile = np.load(
os.path.dirname(os.path.abspath(__file__)) + os.path.sep + "smaller_dataset.npz", allow_pickle=True
)
n_span = npzfile["r"].size
n_aoa = npzfile["aoa"].size
n_Re = npzfile["Re"].size
modeling_options = {}
modeling_options["WISDEM"] = {}
modeling_options["WISDEM"]["RotorSE"] = {}
modeling_options["WISDEM"]["RotorSE"]["n_span"] = n_span
modeling_options["WISDEM"]["RotorSE"]["n_aoa"] = n_aoa
modeling_options["WISDEM"]["RotorSE"]["n_Re"] = n_Re
modeling_options["WISDEM"]["RotorSE"]["n_tab"] = 1
modeling_options["assembly"] = {}
modeling_options["assembly"]["number_of_blades"] = 3
n_span, n_aoa, n_Re, n_tab = np.moveaxis(npzfile["cl"][:, :, :, np.newaxis], 0, 1).shape
modeling_options["airfoils"] = {}
modeling_options["airfoils"]["n_aoa"] = n_aoa
modeling_options["airfoils"]["n_Re"] = n_Re
modeling_options["airfoils"]["n_tab"] = n_tab
opt_options = {}
opt_options["design_variables"] = {}
opt_options["design_variables"]["blade"] = {}
opt_options["design_variables"]["blade"]["aero_shape"] = {}
opt_options["design_variables"]["blade"]["aero_shape"]["chord"] = {}
opt_options["design_variables"]["blade"]["aero_shape"]["chord"]["n_opt"] = 8
opt_options["design_variables"]["blade"]["aero_shape"]["twist"] = {}
opt_options["design_variables"]["blade"]["aero_shape"]["twist"]["n_opt"] = 8
opt_options["design_variables"]["blade"]["aero_shape"]["twist"]["inverse"] = False
opt_options["constraints"] = {}
opt_options["constraints"]["blade"] = {}
opt_options["constraints"]["blade"]["stall"] = {}
opt_options["constraints"]["blade"]["stall"]["margin"] = 0.05233
comp = CCBladeTwist(modeling_options=modeling_options, opt_options=opt_options)
prob.model.add_subsystem("comp", comp, promotes=["*"])
prob.setup(force_alloc_complex=True)
prob.set_val("airfoils_aoa", npzfile["aoa"], units="deg")
prob.set_val("airfoils_Re", npzfile["Re"])
prob.set_val("airfoils_cl", np.moveaxis(npzfile["cl"][:, :, :, np.newaxis], 0, 1))
prob.set_val("airfoils_cd", np.moveaxis(npzfile["cd"][:, :, :, np.newaxis], 0, 1))
prob.set_val("airfoils_cm", np.moveaxis(npzfile["cm"][:, :, :, np.newaxis], 0, 1))
prob.set_val("r", npzfile["r"], units="m")
prob.set_val("chord", npzfile["chord"], units="m")
prob.set_val("Rhub", 1.0, units="m")
prob.set_val("Rtip", 70.0, units="m")
prob.set_val("hub_height", 100.0, units="m")
prob.set_val("precone", 0.0, units="deg")
prob.set_val("tilt", 0.0, units="deg")
prob.set_val("yaw", 0.0, units="deg")
prob.set_val("precurve", np.zeros(n_span), units="m")
prob.set_val("precurveTip", 0.0, units="m")
prob.set_val("rho", 1.225, units="kg/m**3")
prob.set_val("mu", 1.81206e-5, units="kg/(m*s)")
prob.set_val("shearExp", 0.25)
prob.set_val("nBlades", 3)
prob.set_val("nSector", 4)
prob.set_val("tiploss", True)
prob.set_val("hubloss", True)
prob.set_val("wakerotation", True)
prob.set_val("usecd", True)
prob.run_model()
check = prob.check_partials(out_stream=None, compact_print=True, step=1e-7)
# Manually filter some entries out of the assert_check_partials call.
# Will want to add this functionality to OpenMDAO itself at some point.
new_check = {}
for comp_name in check:
new_check[comp_name] = {}
for (output_name, input_name) in check[comp_name]:
if not input_name in ["airfoil", "rho", "mu"]:
new_check[comp_name][(output_name, input_name)] = check[comp_name][(output_name, input_name)]
assert_check_partials(new_check) # , rtol=5e-5, atol=1e-4)
def test_ccblade_standalone(self):
""""""
prob = om.Problem()
# Add some arbitrary inputs
# Load in airfoil and blade shape inputs for NREL 5MW
npzfile = np.load(
os.path.dirname(os.path.abspath(__file__)) + os.path.sep + "smaller_dataset.npz", allow_pickle=True
)
n_span = npzfile["r"].size
n_aoa = npzfile["aoa"].size
n_Re = npzfile["Re"].size
modeling_options = {}
modeling_options["WISDEM"] = {}
modeling_options["WISDEM"]["RotorSE"] = {}
modeling_options["WISDEM"]["RotorSE"]["n_span"] = n_span
modeling_options["WISDEM"]["RotorSE"]["n_aoa"] = n_aoa
modeling_options["WISDEM"]["RotorSE"]["n_Re"] = n_Re
modeling_options["WISDEM"]["RotorSE"]["n_tab"] = 1
modeling_options["assembly"] = {}
modeling_options["assembly"]["number_of_blades"] = 3
n_span, n_aoa, n_Re, n_tab = np.moveaxis(npzfile["cl"][:, :, :, np.newaxis], 0, 1).shape
modeling_options["airfoils"] = {}
modeling_options["airfoils"]["n_aoa"] = n_aoa
modeling_options["airfoils"]["n_Re"] = n_Re
modeling_options["airfoils"]["n_tab"] = n_tab
comp = CCBladeEvaluate(modeling_options=modeling_options)
prob.model.add_subsystem("comp", comp, promotes=["*"])
prob.setup(force_alloc_complex=True)
prob.set_val("airfoils_aoa", npzfile["aoa"], units="deg")
prob.set_val("airfoils_Re", npzfile["Re"])
prob.set_val("airfoils_cl", np.moveaxis(npzfile["cl"][:, :, :, np.newaxis], 0, 1))
prob.set_val("airfoils_cd", np.moveaxis(npzfile["cd"][:, :, :, np.newaxis], 0, 1))
prob.set_val("airfoils_cm", np.moveaxis(npzfile["cm"][:, :, :, np.newaxis], 0, 1))
prob.set_val("r", npzfile["r"], units="m")
prob.set_val("chord", npzfile["chord"], units="m")
prob.set_val("theta", npzfile["theta"], units="deg")
# parameters
prob.set_val("V_load", 12.0, units="m/s")
prob.set_val("Omega_load", 7.0, units="rpm")
prob.set_val("pitch_load", 0.5, units="deg")
prob.set_val("Rhub", 1.0, units="m")
prob.set_val("Rtip", 70.0, units="m")
prob.set_val("hub_height", 100.0, units="m")
prob.set_val("precone", 0.1, units="deg")
prob.set_val("tilt", 0.2, units="deg")
prob.set_val("yaw", 0.2, units="deg")
prob.set_val("precurve", np.linspace(0.0, 0.9, n_span), units="m")
prob.set_val("precurveTip", 0.1, units="m")
prob.set_val("presweep", np.linspace(0.0, 0.4, n_span), units="m")
prob.set_val("presweepTip", 0.5, units="m")
prob.set_val("rho", 1.225, units="kg/m**3")
prob.set_val("mu", 1.81206e-5, units="kg/(m*s)")
prob.set_val("shearExp", 0.25)
prob.set_val("nBlades", 3)
prob.set_val("nSector", 4)
prob.set_val("tiploss", True)
prob.set_val("hubloss", True)
prob.set_val("wakerotation", True)
prob.set_val("usecd", True)
prob.run_model()
check = prob.check_partials(out_stream=None, compact_print=True)
# Manually filter some entries out of the assert_check_partials call.
# Will want to add this functionality to OpenMDAO itself at some point.
new_check = {}
for comp_name in check:
new_check[comp_name] = {}
for (output_name, input_name) in check[comp_name]:
if "airfoil" not in input_name and "rho" not in input_name and "mu" not in input_name:
new_check[comp_name][(output_name, input_name)] = check[comp_name][(output_name, input_name)]
assert_check_partials(new_check, rtol=5e-4, atol=50.)
def suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(Test))
return suite
if __name__ == "__main__":
result = unittest.TextTestRunner().run(suite())
if result.wasSuccessful():
exit(0)
else:
exit(1)