Skip to content

Commit 1b56851

Browse files
authored
Added an option to use step-averaged states for steady-state solvers. (#613)
1 parent fed0263 commit 1b56851

12 files changed

+401
-16
lines changed

dafoam/pyDAFoam.py

+13
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,19 @@ def __init__(self):
527527
"defaultOutputValue": 0.0,
528528
}
529529

530+
## whether to use averaged states
531+
## This is useful for cases when steady-state solvers do not converge very well, e.g., flow
532+
## separation. In these cases, the flow field and the objective function will oscillate and
533+
## to get a better flow field and obj func value, we can use step-averaged (mean) states
534+
## the start can be from 0 to 1. 0 means we use all time steps for averaging, while 1 means
535+
## we use no time steps for averaging. 0.8 means we use the last 20% of the time step for averaging.
536+
## We usually don't use 0 because the flow will need some spin up time, so using the spin-up
537+
## flow field for meanStates will be slightly inaccurate.
538+
self.useMeanStates = {
539+
"active": False,
540+
"start": 0.5
541+
}
542+
530543
# *********************************************************************************************
531544
# ************************************ Advance Options ****************************************
532545
# *********************************************************************************************

src/adjoint/DASolver/DARhoSimpleCFoam/DARhoSimpleCFoam.C

+8
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ label DARhoSimpleCFoam::solvePrimal(
132132
// check if the parameters are set in the Python layer
133133
daRegressionPtr_->validate();
134134

135+
// if useMeanStates is used, we need to zero meanStates before the primal run
136+
this->zeroMeanStates();
135137

136138
label printInterval = daOptionPtr_->getOption<label>("printInterval");
137139
label printToScreen = 0;
@@ -181,6 +183,9 @@ label DARhoSimpleCFoam::solvePrimal(
181183
<< nl << endl;
182184
}
183185

186+
// if useMeanStates is used, we need to calculate the meanStates
187+
this->calcMeanStates();
188+
184189
runTime.write();
185190
}
186191

@@ -189,6 +194,9 @@ label DARhoSimpleCFoam::solvePrimal(
189194
return 1;
190195
}
191196

197+
// if useMeanStates is used, we need to assign meanStates to states right after the case converges
198+
this->assignMeanStatesToStates();
199+
192200
this->calcPrimalResidualStatistics("print");
193201

194202
// primal converged, assign the OpenFoam fields to the state vec wVec

src/adjoint/DASolver/DARhoSimpleFoam/DARhoSimpleFoam.C

+9
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ label DARhoSimpleFoam::solvePrimal(
149149
// check if the parameters are set in the Python layer
150150
daRegressionPtr_->validate();
151151

152+
// if useMeanStates is used, we need to zero meanStates before the primal run
153+
this->zeroMeanStates();
154+
152155
label printInterval = daOptionPtr_->getOption<label>("printInterval");
153156
label printToScreen = 0;
154157
label regModelFail = 0;
@@ -197,6 +200,9 @@ label DARhoSimpleFoam::solvePrimal(
197200
<< nl << endl;
198201
}
199202

203+
// if useMeanStates is used, we need to calculate the meanStates
204+
this->calcMeanStates();
205+
200206
runTime.write();
201207
}
202208

@@ -205,6 +211,9 @@ label DARhoSimpleFoam::solvePrimal(
205211
return 1;
206212
}
207213

214+
// if useMeanStates is used, we need to assign meanStates to states right after the case converges
215+
this->assignMeanStatesToStates();
216+
208217
this->writeAssociatedFields();
209218

210219
this->calcPrimalResidualStatistics("print");

src/adjoint/DASolver/DASimpleFoam/DASimpleFoam.C

+9
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@ label DASimpleFoam::solvePrimal(
160160
// check if the parameters are set in the Python layer
161161
daRegressionPtr_->validate();
162162

163+
// if useMeanStates is used, we need to zero meanStates before the primal run
164+
this->zeroMeanStates();
165+
163166
label printInterval = daOptionPtr_->getOption<label>("printInterval");
164167
label printToScreen = 0;
165168
label regModelFail = 0;
@@ -209,6 +212,9 @@ label DASimpleFoam::solvePrimal(
209212
<< nl << endl;
210213
}
211214

215+
// if useMeanStates is used, we need to calculate the meanStates
216+
this->calcMeanStates();
217+
212218
runTime.write();
213219
}
214220

@@ -217,6 +223,9 @@ label DASimpleFoam::solvePrimal(
217223
return 1;
218224
}
219225

226+
// if useMeanStates is used, we need to assign meanStates to states right after the case converges
227+
this->assignMeanStatesToStates();
228+
220229
this->writeAssociatedFields();
221230

222231
this->calcPrimalResidualStatistics("print");

src/adjoint/DASolver/DASimpleTFoam/DASimpleTFoam.C

+9
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ label DASimpleTFoam::solvePrimal(
139139
// check if the parameters are set in the Python layer
140140
daRegressionPtr_->validate();
141141

142+
// if useMeanStates is used, we need to zero meanStates before the primal run
143+
this->zeroMeanStates();
144+
142145
label printInterval = daOptionPtr_->getOption<label>("printInterval");
143146
label printToScreen = 0;
144147
label regModelFail = 0;
@@ -189,6 +192,9 @@ label DASimpleTFoam::solvePrimal(
189192
<< nl << endl;
190193
}
191194

195+
// if useMeanStates is used, we need to calculate the meanStates
196+
this->calcMeanStates();
197+
192198
runTime.write();
193199
}
194200

@@ -197,6 +203,9 @@ label DASimpleTFoam::solvePrimal(
197203
return 1;
198204
}
199205

206+
// if useMeanStates is used, we need to assign meanStates to states right after the case converges
207+
this->assignMeanStatesToStates();
208+
200209
this->calcPrimalResidualStatistics("print");
201210

202211
// primal converged, assign the OpenFoam fields to the state vec wVec

0 commit comments

Comments
 (0)