-
Notifications
You must be signed in to change notification settings - Fork 993
Expand file tree
/
Copy pathshape_optimization.py
More file actions
executable file
·313 lines (283 loc) · 9.93 KB
/
Copy pathshape_optimization.py
File metadata and controls
executable file
·313 lines (283 loc) · 9.93 KB
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
#!/usr/bin/env python
## \file shape_optimization.py
# \brief Python script for performing the shape optimization.
# \author T. Economon, T. Lukaczyk, F. Palacios
# \version 8.5.0 "Harrier"
#
# SU2 Project Website: https://su2code.github.io
#
# The SU2 Project is maintained by the SU2 Foundation
# (http://su2foundation.org)
#
# Copyright 2012-2026, SU2 Contributors (cf. AUTHORS.md)
#
# SU2 is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# SU2 is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with SU2. If not, see <http://www.gnu.org/licenses/>.
import os, sys, shutil
import argparse
sys.path.append(os.environ["SU2_RUN"])
import SU2
# -------------------------------------------------------------------
# Main
# -------------------------------------------------------------------
def main():
parser = argparse.ArgumentParser(description="SU2 shape optimization.")
parser.add_argument(
"-f", "--file", dest="filename", help="read config from FILE", metavar="FILE"
)
parser.add_argument(
"-r",
"--name",
dest="projectname",
default="",
help="try to restart from project file NAME",
metavar="NAME",
)
parser.add_argument(
"-n",
"--partitions",
dest="partitions",
default=1,
help="number of PARTITIONS",
metavar="PARTITIONS",
)
parser.add_argument(
"-g",
"--gradient",
dest="gradient",
default="DISCRETE_ADJOINT",
help="Method for computing the GRADIENT (CONTINUOUS_ADJOINT, DISCRETE_ADJOINT, FINDIFF, NONE)",
metavar="GRADIENT",
)
parser.add_argument(
"-o",
"--optimization",
dest="optimization",
default="SLSQP",
help="OPTIMIZATION techique (SLSQP, CG, BFGS, POWELL)",
metavar="OPTIMIZATION",
)
parser.add_argument(
"-q",
"--quiet",
dest="quiet",
default="True",
help="True/False Quiet all SU2 output (optimizer output only)",
metavar="QUIET",
)
parser.add_argument(
"-z",
"--zones",
dest="nzones",
default="1",
help="Number of Zones",
metavar="ZONES",
)
options = parser.parse_args()
# process inputs
options.partitions = int(options.partitions)
options.quiet = options.quiet.upper() == "TRUE"
options.gradient = options.gradient.upper()
options.nzones = int(options.nzones)
sys.stdout.write(
"\n-------------------------------------------------------------------------\n"
)
sys.stdout.write(
"| ___ _ _ ___ |\n"
)
sys.stdout.write(
'| / __| | | |_ ) Release 8.5.0 "Harrier" |\n'
)
sys.stdout.write(
"| \\__ \\ |_| |/ / |\n"
)
sys.stdout.write(
"| |___/\\___//___| Aerodynamic Shape Optimization Script |\n"
)
sys.stdout.write(
"| |\n"
)
sys.stdout.write(
"-------------------------------------------------------------------------\n"
)
sys.stdout.write(
"| SU2 Project Website: https://su2code.github.io |\n"
)
sys.stdout.write(
"| |\n"
)
sys.stdout.write(
"| The SU2 Project is maintained by the SU2 Foundation |\n"
)
sys.stdout.write(
"| (http://su2foundation.org) |\n"
)
sys.stdout.write(
"-------------------------------------------------------------------------\n"
)
sys.stdout.write(
"| Copyright 2012-2026, SU2 Contributors (cf. AUTHORS.md) |\n"
)
sys.stdout.write(
"| |\n"
)
sys.stdout.write(
"| SU2 is free software; you can redistribute it and/or |\n"
)
sys.stdout.write(
"| modify it under the terms of the GNU Lesser General Public |\n"
)
sys.stdout.write(
"| License as published by the Free Software Foundation; either |\n"
)
sys.stdout.write(
"| version 2.1 of the License, or (at your option) any later version. |\n"
)
sys.stdout.write(
"| |\n"
)
sys.stdout.write(
"| SU2 is distributed in the hope that it will be useful, |\n"
)
sys.stdout.write(
"| but WITHOUT ANY WARRANTY; without even the implied warranty of |\n"
)
sys.stdout.write(
"| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |\n"
)
sys.stdout.write(
"| Lesser General Public License for more details. |\n"
)
sys.stdout.write(
"| |\n"
)
sys.stdout.write(
"| You should have received a copy of the GNU Lesser General Public |\n"
)
sys.stdout.write(
"| License along with SU2. If not, see <http://www.gnu.org/licenses/>. |\n"
)
sys.stdout.write(
"-------------------------------------------------------------------------\n"
)
shape_optimization(
options.filename,
options.projectname,
options.partitions,
options.gradient,
options.optimization,
options.quiet,
options.nzones,
)
#: main()
def shape_optimization(
filename,
projectname="",
partitions=0,
gradient="CONTINUOUS_ADJOINT",
optimization="SLSQP",
quiet=False,
nzones=1,
):
# Config
config = SU2.io.Config(filename)
config.NUMBER_PART = partitions
config.NZONES = int(nzones)
if quiet:
config.CONSOLE = "CONCISE"
config.GRADIENT_METHOD = gradient
its = int(config.OPT_ITERATIONS) # number of opt iterations
bound_upper = float(
config.OPT_BOUND_UPPER
) # variable bound to be scaled by the line search
bound_lower = float(
config.OPT_BOUND_LOWER
) # variable bound to be scaled by the line search
relax_factor = float(config.OPT_RELAX_FACTOR) # line search scale
gradient_factor = float(
config.OPT_GRADIENT_FACTOR
) # objective function and gradient scale
def_dv = config.DEFINITION_DV # complete definition of the desing variable
n_dv = sum(def_dv["SIZE"]) # number of design variables
accu = float(config.OPT_ACCURACY) * gradient_factor # optimizer accuracy
x0 = [0.0] * n_dv # initial design
xb_low = [
float(bound_lower) / float(relax_factor)
] * n_dv # lower dv bound it includes the line search acceleration factor
xb_up = [
float(bound_upper) / float(relax_factor)
] * n_dv # upper dv bound it includes the line search acceleration fa
xb = list(zip(xb_low, xb_up)) # design bounds
# State
state = SU2.io.State()
state.find_files(config)
# add restart files to state.FILES
if (
config.get("TIME_DOMAIN", "NO") == "YES"
and config.get("RESTART_SOL", "NO") == "YES"
and gradient != "CONTINUOUS_ADJOINT"
):
# needs to be handled...
filename = config["RESTART_FILENAME"].split(".")
# join everything except the last extension
if filename[-1] == ".dat":
restart_name = ".".join(filename[:-1])
else:
restart_name = config["RESTART_FILENAME"]
restart_filename = (
restart_name + "_" + str(int(config["RESTART_ITER"]) - 1).zfill(5) + ".dat"
)
if not os.path.isfile(
restart_filename
): # throw, if restart files does not exist
sys.exit("Error: Restart file <" + restart_filename + "> not found.")
state["FILES"]["RESTART_FILE_1"] = restart_filename
# use only, if time integration is second order
if config.get("TIME_MARCHING", "NO") == "DUAL_TIME_STEPPING-2ND_ORDER":
restart_filename = (
restart_name
+ "_"
+ str(int(config["RESTART_ITER"]) - 2).zfill(5)
+ ".dat"
)
if not os.path.isfile(
restart_filename
): # throw, if restart files does not exist
sys.exit("Error: Restart file <" + restart_filename + "> not found.")
state["FILES"]["RESTART_FILE_2"] = restart_filename
# Project
if os.path.exists(projectname):
project = SU2.io.load_data(projectname)
project.config = config
else:
project = SU2.opt.Project(config, state)
# Optimize
if optimization == "SLSQP":
SU2.opt.SLSQP(project, x0, xb, its, accu)
if optimization == "CG":
SU2.opt.CG(project, x0, xb, its, accu)
if optimization == "BFGS":
SU2.opt.BFGS(project, x0, xb, its, accu)
if optimization == "POWELL":
SU2.opt.POWELL(project, x0, xb, its, accu)
# rename project file
if projectname:
shutil.move("project.pkl", projectname)
return project
#: shape_optimization()
# -------------------------------------------------------------------
# Run Main Program
# -------------------------------------------------------------------
# this is only accessed if running from command prompt
if __name__ == "__main__":
main()