-
Notifications
You must be signed in to change notification settings - Fork 0
/
runScript.py
executable file
·192 lines (173 loc) · 6.51 KB
/
runScript.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
#!/usr/bin/env python
"""
DAFoam run script to verify the adjoint derivative accuracy
"""
# =============================================================================
# Imports
# =============================================================================
import os
import argparse
from mpi4py import MPI
from dafoam import PYDAFOAM, optFuncs
from pygeo import *
from pyspline import *
from idwarp import USMesh
import numpy as np
# =============================================================================
# Input Parameters
# =============================================================================
parser = argparse.ArgumentParser()
parser.add_argument("--task", help="type of run to do", type=str, default="runAdjoint")
parser.add_argument("--mode", help="AD mode: either reverse or forward", type=str, default="reverse")
parser.add_argument("--dvName", help="design variable name for forward AD", type=str, default="shape")
parser.add_argument("--seedIndex", help="which design variable index to set seeds", type=int, default=0)
args = parser.parse_args()
gcomm = MPI.COMM_WORLD
# Define the global parameters here
U0 = 100.0
p0 = 101325.0
T0 = 300.0
rho0 = 1.178
nuTilda0 = 4.5e-5
alpha0 = 3.0
A0 = 0.1
# Set the parameters for optimization
daOptions = {
"designSurfaces": ["wing"],
"solverName": "DARhoSimpleFoam",
"primalMinResTol": 1.0e-14,
"useAD": {"mode": args.mode},
"primalBC": {
"U0": {"variable": "U", "patches": ["inout"], "value": [U0, 0.0, 0.0]},
"p0": {"variable": "p", "patches": ["inout"], "value": [p0]},
"T0": {"variable": "T", "patches": ["inout"], "value": [T0]},
"nuTilda0": {"variable": "nuTilda", "patches": ["inout"], "value": [nuTilda0]},
"useWallFunction": True,
},
# variable bounds for compressible flow conditions
"primalVarBounds": {
"UMax": 1000.0,
"UMin": -1000.0,
"pMax": 500000.0,
"pMin": 20000.0,
"eMax": 500000.0,
"eMin": 100000.0,
"rhoMax": 5.0,
"rhoMin": 0.2,
"nuTildaMin": -1e16},
"objFunc": {
"CD": {
"part1": {
"type": "force",
"source": "patchToFace",
"patches": ["wing"],
"directionMode": "parallelToFlow",
"alphaName": "alpha",
"scale": 1.0 / (0.5 * U0 * U0 * A0),
"addToAdjoint": True,
}
},
"CL": {
"part1": {
"type": "force",
"source": "patchToFace",
"patches": ["wing"],
"directionMode": "normalToFlow",
"alphaName": "alpha",
"scale": 1.0 / (0.5 * U0 * U0 * A0),
"addToAdjoint": True,
}
},
"CMZ": {
"part1": {
"type": "moment",
"source": "patchToFace",
"patches": ["wing"],
"axis": [0.0, 0.0, 1.0],
"center": [0.25, 0.0, 0.05],
"scale": 1.0 / (0.5 * U0 * U0 * A0 * 1.0),
"addToAdjoint": True,
}
},
},
"adjStateOrdering": "cell",
"adjEqnOption": {"gmresRelTol": 1.0e-14, "pcFillLevel": 1, "jacMatReOrdering": "rcm"},
"normalizeStates": {
"U": U0,
"p": p0,
"nuTilda": nuTilda0 * 10.0,
"T": T0,
"phi": 1.0,
},
"adjPartDerivFDStep": {"State": 1e-6},
"designVar": {},
}
# mesh warping parameters, users need to manually specify the symmetry plane and their normals
meshOptions = {
"gridFile": os.getcwd(),
"fileType": "OpenFOAM",
# point and normal for the symmetry plane
"symmetryPlanes": [[[0.0, 0.0, 0.0], [0.0, 0.0, 1.0]], [[0.0, 0.0, 0.1], [0.0, 0.0, 1.0]]],
}
# =============================================================================
# Design variable setup
# =============================================================================
DVGeo = DVGeometry("./FFD/wingFFD.xyz")
nTwists = DVGeo.addRefAxis("bodyAxis", xFraction=0.25, alignIndex="k")
def alpha(val, geo):
aoa = val[0] * np.pi / 180.0
inletU = [float(U0 * np.cos(aoa)), float(U0 * np.sin(aoa)), 0]
DASolver.setOption("primalBC", {"U0": {"variable": "U", "patches": ["inout"], "value": inletU}})
DASolver.updateDAOption()
def pitch(val, geo):
for i in range(nTwists):
geo.rot_z["bodyAxis"].coef[i] = -val[0]
# select points
iVol = 0
pts = DVGeo.getLocalIndex(iVol)
indexList = pts[:, :, :].flatten()
PS = geo_utils.PointSelect("list", indexList)
# shape
DVGeo.addLocalDV("shape", lower=-1.0, upper=1.0, axis="y", scale=1.0, pointSelect=PS)
daOptions["designVar"]["shape"] = {"designVarType": "FFD"}
# pitch
DVGeo.addGlobalDV("pitch", np.zeros(1), pitch, lower=-10.0, upper=10.0, scale=1.0)
daOptions["designVar"]["pitch"] = {"designVarType": "FFD"}
# AOA
DVGeo.addGlobalDV("alpha", value=[alpha0], func=alpha, lower=0.0, upper=10.0, scale=1.0)
daOptions["designVar"]["alpha"] = {"designVarType": "AOA", "patches": ["inout"], "flowAxis": "x", "normalAxis": "y"}
# =============================================================================
# DAFoam initialization
# =============================================================================
DASolver = PYDAFOAM(options=daOptions, comm=gcomm)
DASolver.setDVGeo(DVGeo)
mesh = USMesh(options=meshOptions, comm=gcomm)
DASolver.addFamilyGroup(DASolver.getOption("designSurfaceFamily"), DASolver.getOption("designSurfaces"))
DASolver.printFamilyList()
DASolver.setMesh(mesh)
evalFuncs = []
DASolver.setEvalFuncs(evalFuncs)
# =============================================================================
# Constraint setup
# =============================================================================
DVCon = DVConstraints()
DVCon.setDVGeo(DVGeo)
DVCon.setSurface(DASolver.getTriangulatedMeshSurface(groupName=DASolver.getOption("designSurfaceFamily")))
# =============================================================================
# Initialize optFuncs for optimization
# =============================================================================
optFuncs.DASolver = DASolver
optFuncs.DVGeo = DVGeo
optFuncs.DVCon = DVCon
optFuncs.evalFuncs = evalFuncs
optFuncs.gcomm = gcomm
# =============================================================================
# Task
# =============================================================================
if args.task == "runAdjoint":
optFuncs.runAdjoint()
elif args.task == "runForwardAD":
optFuncs.runForwardAD(args.dvName, args.seedIndex)
else:
print("task arg not found!")
exit(0)