Skip to content

Commit dd84636

Browse files
committed
Added an option to use time-dependent ref for refDiffSquare.
1 parent e64dbf9 commit dd84636

18 files changed

+60
-65
lines changed

src/adjoint/DAObjFunc/DAObjFunc.C

+24-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ DAObjFunc::DAObjFunc(
5959
calcRefDiffSquare_ = objFuncDict_.lookupOrDefault<label>("calcRefDiffSquare", 0);
6060
if (calcRefDiffSquare_)
6161
{
62-
objFuncDict_.readEntry<scalar>("ref", ref_);
62+
objFuncDict_.readEntry<scalarList>("ref", ref_);
6363
}
6464
}
6565

@@ -318,6 +318,29 @@ scalar DAObjFunc::getObjFuncValue()
318318
return objFuncValue_;
319319
}
320320

321+
void DAObjFunc::calcRefDiffSquare(scalar& objFuncValue)
322+
{
323+
/*
324+
Description:
325+
Call the variable difference with respect to a given reference and take a square of it.
326+
This can be used in FIML. This function is for calcRefDiffSquare_ == 1
327+
*/
328+
329+
if (calcRefDiffSquare_)
330+
{
331+
if (ref_.size() == 1)
332+
{
333+
objFuncValue = (objFuncValue - ref_) * (objFuncValue - ref_);
334+
}
335+
else
336+
{
337+
label idxI = runTime.timeIndex() - 1;
338+
objFuncValue = (objFuncValue - ref_[idxI]) * (objFuncValue - ref_[idxI]);
339+
}
340+
}
341+
342+
}
343+
321344
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
322345

323346
} // End namespace Foam

src/adjoint/DAObjFunc/DAObjFunc.H

+5-3
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,8 @@ protected:
9595
/// whether to calculate (obj-ref)^2
9696
label calcRefDiffSquare_;
9797

98-
/// if calcRefDiffSquare_ is True, set the reference value
99-
scalar ref_;
100-
98+
/// if calcRefDiffSquare_ is True, set the reference value list
99+
scalarList ref_;
101100

102101
public:
103102
/// Runtime type information
@@ -243,6 +242,9 @@ public:
243242

244243
/// whether to compute reference coefficients for special objFunc treatment such as totalPressureRatio
245244
label calcRefCoeffs = 1;
245+
246+
/// calculate the varDiffSquare
247+
void calcRefDiffSquare(scalar& objFuncValue);
246248
};
247249

248250
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

src/adjoint/DAObjFunc/DAObjFuncCenterOfPressure.C

+2-4
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,8 @@ void DAObjFuncCenterOfPressure::calcObjFunc(
128128

129129
objFuncValue = scale_ * (weightedPressure / totalPressure + (center_ & axis_));
130130

131-
if (calcRefDiffSquare_)
132-
{
133-
objFuncValue = (objFuncValue - ref_) * (objFuncValue - ref_);
134-
}
131+
// check if we need to calculate refDiffSquare.
132+
this->calcRefDiffSquare(objFuncValue);
135133

136134
return;
137135
}

src/adjoint/DAObjFunc/DAObjFuncForce.C

+2-4
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,8 @@ void DAObjFuncForce::calcObjFunc(
191191
// need to reduce the sum of force across all processors
192192
reduce(objFuncValue, sumOp<scalar>());
193193

194-
if (calcRefDiffSquare_)
195-
{
196-
objFuncValue = (objFuncValue - ref_) * (objFuncValue - ref_);
197-
}
194+
// check if we need to calculate refDiffSquare.
195+
this->calcRefDiffSquare(objFuncValue);
198196

199197
return;
200198
}

src/adjoint/DAObjFunc/DAObjFuncLocation.C

+2-4
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,8 @@ void DAObjFuncLocation::calcObjFunc(
219219
<< abort(FatalError);
220220
}
221221

222-
if (calcRefDiffSquare_)
223-
{
224-
objFuncValue = (objFuncValue - ref_) * (objFuncValue - ref_);
225-
}
222+
// check if we need to calculate refDiffSquare.
223+
this->calcRefDiffSquare(objFuncValue);
226224

227225
return;
228226
}

src/adjoint/DAObjFunc/DAObjFuncMass.C

+2-4
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,8 @@ void DAObjFuncMass::calcObjFunc(
9696
// need to reduce the sum of force across all processors
9797
reduce(objFuncValue, sumOp<scalar>());
9898

99-
if (calcRefDiffSquare_)
100-
{
101-
objFuncValue = (objFuncValue - ref_) * (objFuncValue - ref_);
102-
}
99+
// check if we need to calculate refDiffSquare.
100+
this->calcRefDiffSquare(objFuncValue);
103101

104102
return;
105103
}

src/adjoint/DAObjFunc/DAObjFuncMassFlowRate.C

+2-4
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,8 @@ void DAObjFuncMassFlowRate::calcObjFunc(
119119
// need to reduce the sum of force across all processors
120120
reduce(objFuncValue, sumOp<scalar>());
121121

122-
if (calcRefDiffSquare_)
123-
{
124-
objFuncValue = (objFuncValue - ref_) * (objFuncValue - ref_);
125-
}
122+
// check if we need to calculate refDiffSquare.
123+
this->calcRefDiffSquare(objFuncValue);
126124

127125
return;
128126
}

src/adjoint/DAObjFunc/DAObjFuncMeshQualityKS.C

+2-4
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,8 @@ void DAObjFuncMeshQualityKS::calcObjFunc(
219219

220220
objFuncValue = log(objFuncValue) / coeffKS_;
221221

222-
if (calcRefDiffSquare_)
223-
{
224-
objFuncValue = (objFuncValue - ref_) * (objFuncValue - ref_);
225-
}
222+
// check if we need to calculate refDiffSquare.
223+
this->calcRefDiffSquare(objFuncValue);
226224

227225
return;
228226
}

src/adjoint/DAObjFunc/DAObjFuncMoment.C

+2-4
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,8 @@ void DAObjFuncMoment::calcObjFunc(
165165
// need to reduce the sum of force across all processors
166166
reduce(objFuncValue, sumOp<scalar>());
167167

168-
if (calcRefDiffSquare_)
169-
{
170-
objFuncValue = (objFuncValue - ref_) * (objFuncValue - ref_);
171-
}
168+
// check if we need to calculate refDiffSquare.
169+
this->calcRefDiffSquare(objFuncValue);
172170

173171
return;
174172
}

src/adjoint/DAObjFunc/DAObjFuncPatchMean.C

+2-4
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,8 @@ void DAObjFuncPatchMean::calcObjFunc(
132132
// need to reduce the sum of force across all processors
133133
reduce(objFuncValue, sumOp<scalar>());
134134

135-
if (calcRefDiffSquare_)
136-
{
137-
objFuncValue = (objFuncValue - ref_) * (objFuncValue - ref_);
138-
}
135+
// check if we need to calculate refDiffSquare.
136+
this->calcRefDiffSquare(objFuncValue);
139137

140138
return;
141139
}

src/adjoint/DAObjFunc/DAObjFuncPower.C

+2-4
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,8 @@ void DAObjFuncPower::calcObjFunc(
168168
// need to reduce the sum of force across all processors
169169
reduce(objFuncValue, sumOp<scalar>());
170170

171-
if (calcRefDiffSquare_)
172-
{
173-
objFuncValue = (objFuncValue - ref_) * (objFuncValue - ref_);
174-
}
171+
// check if we need to calculate refDiffSquare.
172+
this->calcRefDiffSquare(objFuncValue);
175173

176174
return;
177175
}

src/adjoint/DAObjFunc/DAObjFuncTotalPressure.C

+2-4
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,8 @@ void DAObjFuncTotalPressure::calcObjFunc(
132132
// need to reduce the sum of force across all processors
133133
reduce(objFuncValue, sumOp<scalar>());
134134

135-
if (calcRefDiffSquare_)
136-
{
137-
objFuncValue = (objFuncValue - ref_) * (objFuncValue - ref_);
138-
}
135+
// check if we need to calculate refDiffSquare.
136+
this->calcRefDiffSquare(objFuncValue);
139137

140138
return;
141139
}

src/adjoint/DAObjFunc/DAObjFuncTotalPressureRatio.C

+2-4
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,8 @@ void DAObjFuncTotalPressureRatio::calcObjFunc(
217217
}
218218
}
219219

220-
if (calcRefDiffSquare_)
221-
{
222-
objFuncValue = (objFuncValue - ref_) * (objFuncValue - ref_);
223-
}
220+
// check if we need to calculate refDiffSquare.
221+
this->calcRefDiffSquare(objFuncValue);
224222

225223
return;
226224
}

src/adjoint/DAObjFunc/DAObjFuncTotalTemperatureRatio.C

+2-4
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,8 @@ void DAObjFuncTotalTemperatureRatio::calcObjFunc(
207207
}
208208
}
209209

210-
if (calcRefDiffSquare_)
211-
{
212-
objFuncValue = (objFuncValue - ref_) * (objFuncValue - ref_);
213-
}
210+
// check if we need to calculate refDiffSquare.
211+
this->calcRefDiffSquare(objFuncValue);
214212

215213
return;
216214
}

src/adjoint/DAObjFunc/DAObjFuncVariableVolSum.C

+2-4
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,8 @@ void DAObjFuncVariableVolSum::calcObjFunc(
167167

168168
objFuncValue /= totalVol;
169169

170-
if (calcRefDiffSquare_)
171-
{
172-
objFuncValue = (objFuncValue - ref_) * (objFuncValue - ref_);
173-
}
170+
// check if we need to calculate refDiffSquare.
171+
this->calcRefDiffSquare(objFuncValue);
174172

175173
return;
176174
}

src/adjoint/DAObjFunc/DAObjFuncVonMisesStressKS.C

+2-4
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,8 @@ void DAObjFuncVonMisesStressKS::calcObjFunc(
121121

122122
objFuncValue = log(objValTmp) / coeffKS_;
123123

124-
if (calcRefDiffSquare_)
125-
{
126-
objFuncValue = (objFuncValue - ref_) * (objFuncValue - ref_);
127-
}
124+
// check if we need to calculate refDiffSquare.
125+
this->calcRefDiffSquare(objFuncValue);
128126

129127
return;
130128
}

src/adjoint/DAObjFunc/DAObjFuncWallHeatFlux.C

+2-4
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,8 @@ void DAObjFuncWallHeatFlux::calcObjFunc(
246246
// need to reduce the sum of force across all processors
247247
reduce(objFuncValue, sumOp<scalar>());
248248

249-
if (calcRefDiffSquare_)
250-
{
251-
objFuncValue = (objFuncValue - ref_) * (objFuncValue - ref_);
252-
}
249+
// check if we need to calculate refDiffSquare.
250+
this->calcRefDiffSquare(objFuncValue);
253251

254252
return;
255253
}

tests/runTests_DASimpleFoamAD.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@
131131
"scale": 1.0 / (0.5 * U0 * U0 * A0 * 1.0),
132132
"addToAdjoint": True,
133133
"calcRefDiffSquare": True,
134-
"ref": 0.1
134+
"ref": [0.1]
135135
}
136136
},
137137
},

0 commit comments

Comments
 (0)