Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowed using existing objFunc for refDiffSquare #590

Merged
merged 2 commits into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/adjoint/DAObjFunc/DAObjFunc.C
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ DAObjFunc::DAObjFunc(
calcRefDiffSquare_ = objFuncDict_.lookupOrDefault<label>("calcRefDiffSquare", 0);
if (calcRefDiffSquare_)
{
objFuncDict_.readEntry<scalar>("ref", ref_);
objFuncDict_.readEntry<scalarList>("ref", ref_);
}
}

Expand Down Expand Up @@ -318,6 +318,29 @@ scalar DAObjFunc::getObjFuncValue()
return objFuncValue_;
}

void DAObjFunc::calcRefDiffSquare(scalar& objFuncValue)
{
/*
Description:
Call the variable difference with respect to a given reference and take a square of it.
This can be used in FIML. This function is for calcRefDiffSquare_ == 1
*/

if (calcRefDiffSquare_)
{
if (ref_.size() == 1)
{
objFuncValue = (objFuncValue - ref_[0]) * (objFuncValue - ref_[0]);
}
else
{
label idxI = mesh_.time().timeIndex() - 1;
objFuncValue = (objFuncValue - ref_[idxI]) * (objFuncValue - ref_[idxI]);
}
}

}

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

} // End namespace Foam
Expand Down
8 changes: 5 additions & 3 deletions src/adjoint/DAObjFunc/DAObjFunc.H
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,8 @@ protected:
/// whether to calculate (obj-ref)^2
label calcRefDiffSquare_;

/// if calcRefDiffSquare_ is True, set the reference value
scalar ref_;

/// if calcRefDiffSquare_ is True, set the reference value list
scalarList ref_;

public:
/// Runtime type information
Expand Down Expand Up @@ -243,6 +242,9 @@ public:

/// whether to compute reference coefficients for special objFunc treatment such as totalPressureRatio
label calcRefCoeffs = 1;

/// calculate the varDiffSquare
void calcRefDiffSquare(scalar& objFuncValue);
};

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Expand Down
6 changes: 2 additions & 4 deletions src/adjoint/DAObjFunc/DAObjFuncCenterOfPressure.C
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,8 @@ void DAObjFuncCenterOfPressure::calcObjFunc(

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

if (calcRefDiffSquare_)
{
objFuncValue = (objFuncValue - ref_) * (objFuncValue - ref_);
}
// check if we need to calculate refDiffSquare.
this->calcRefDiffSquare(objFuncValue);

return;
}
Expand Down
6 changes: 2 additions & 4 deletions src/adjoint/DAObjFunc/DAObjFuncForce.C
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,8 @@ void DAObjFuncForce::calcObjFunc(
// need to reduce the sum of force across all processors
reduce(objFuncValue, sumOp<scalar>());

if (calcRefDiffSquare_)
{
objFuncValue = (objFuncValue - ref_) * (objFuncValue - ref_);
}
// check if we need to calculate refDiffSquare.
this->calcRefDiffSquare(objFuncValue);

return;
}
Expand Down
6 changes: 2 additions & 4 deletions src/adjoint/DAObjFunc/DAObjFuncLocation.C
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,8 @@ void DAObjFuncLocation::calcObjFunc(
<< abort(FatalError);
}

if (calcRefDiffSquare_)
{
objFuncValue = (objFuncValue - ref_) * (objFuncValue - ref_);
}
// check if we need to calculate refDiffSquare.
this->calcRefDiffSquare(objFuncValue);

return;
}
Expand Down
6 changes: 2 additions & 4 deletions src/adjoint/DAObjFunc/DAObjFuncMass.C
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,8 @@ void DAObjFuncMass::calcObjFunc(
// need to reduce the sum of force across all processors
reduce(objFuncValue, sumOp<scalar>());

if (calcRefDiffSquare_)
{
objFuncValue = (objFuncValue - ref_) * (objFuncValue - ref_);
}
// check if we need to calculate refDiffSquare.
this->calcRefDiffSquare(objFuncValue);

return;
}
Expand Down
6 changes: 2 additions & 4 deletions src/adjoint/DAObjFunc/DAObjFuncMassFlowRate.C
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,8 @@ void DAObjFuncMassFlowRate::calcObjFunc(
// need to reduce the sum of force across all processors
reduce(objFuncValue, sumOp<scalar>());

if (calcRefDiffSquare_)
{
objFuncValue = (objFuncValue - ref_) * (objFuncValue - ref_);
}
// check if we need to calculate refDiffSquare.
this->calcRefDiffSquare(objFuncValue);

return;
}
Expand Down
6 changes: 2 additions & 4 deletions src/adjoint/DAObjFunc/DAObjFuncMeshQualityKS.C
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,8 @@ void DAObjFuncMeshQualityKS::calcObjFunc(

objFuncValue = log(objFuncValue) / coeffKS_;

if (calcRefDiffSquare_)
{
objFuncValue = (objFuncValue - ref_) * (objFuncValue - ref_);
}
// check if we need to calculate refDiffSquare.
this->calcRefDiffSquare(objFuncValue);

return;
}
Expand Down
6 changes: 2 additions & 4 deletions src/adjoint/DAObjFunc/DAObjFuncMoment.C
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,8 @@ void DAObjFuncMoment::calcObjFunc(
// need to reduce the sum of force across all processors
reduce(objFuncValue, sumOp<scalar>());

if (calcRefDiffSquare_)
{
objFuncValue = (objFuncValue - ref_) * (objFuncValue - ref_);
}
// check if we need to calculate refDiffSquare.
this->calcRefDiffSquare(objFuncValue);

return;
}
Expand Down
6 changes: 2 additions & 4 deletions src/adjoint/DAObjFunc/DAObjFuncPatchMean.C
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,8 @@ void DAObjFuncPatchMean::calcObjFunc(
// need to reduce the sum of force across all processors
reduce(objFuncValue, sumOp<scalar>());

if (calcRefDiffSquare_)
{
objFuncValue = (objFuncValue - ref_) * (objFuncValue - ref_);
}
// check if we need to calculate refDiffSquare.
this->calcRefDiffSquare(objFuncValue);

return;
}
Expand Down
6 changes: 2 additions & 4 deletions src/adjoint/DAObjFunc/DAObjFuncPower.C
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,8 @@ void DAObjFuncPower::calcObjFunc(
// need to reduce the sum of force across all processors
reduce(objFuncValue, sumOp<scalar>());

if (calcRefDiffSquare_)
{
objFuncValue = (objFuncValue - ref_) * (objFuncValue - ref_);
}
// check if we need to calculate refDiffSquare.
this->calcRefDiffSquare(objFuncValue);

return;
}
Expand Down
6 changes: 2 additions & 4 deletions src/adjoint/DAObjFunc/DAObjFuncTotalPressure.C
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,8 @@ void DAObjFuncTotalPressure::calcObjFunc(
// need to reduce the sum of force across all processors
reduce(objFuncValue, sumOp<scalar>());

if (calcRefDiffSquare_)
{
objFuncValue = (objFuncValue - ref_) * (objFuncValue - ref_);
}
// check if we need to calculate refDiffSquare.
this->calcRefDiffSquare(objFuncValue);

return;
}
Expand Down
6 changes: 2 additions & 4 deletions src/adjoint/DAObjFunc/DAObjFuncTotalPressureRatio.C
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,8 @@ void DAObjFuncTotalPressureRatio::calcObjFunc(
}
}

if (calcRefDiffSquare_)
{
objFuncValue = (objFuncValue - ref_) * (objFuncValue - ref_);
}
// check if we need to calculate refDiffSquare.
this->calcRefDiffSquare(objFuncValue);

return;
}
Expand Down
6 changes: 2 additions & 4 deletions src/adjoint/DAObjFunc/DAObjFuncTotalTemperatureRatio.C
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,8 @@ void DAObjFuncTotalTemperatureRatio::calcObjFunc(
}
}

if (calcRefDiffSquare_)
{
objFuncValue = (objFuncValue - ref_) * (objFuncValue - ref_);
}
// check if we need to calculate refDiffSquare.
this->calcRefDiffSquare(objFuncValue);

return;
}
Expand Down
6 changes: 2 additions & 4 deletions src/adjoint/DAObjFunc/DAObjFuncVariableVolSum.C
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,8 @@ void DAObjFuncVariableVolSum::calcObjFunc(

objFuncValue /= totalVol;

if (calcRefDiffSquare_)
{
objFuncValue = (objFuncValue - ref_) * (objFuncValue - ref_);
}
// check if we need to calculate refDiffSquare.
this->calcRefDiffSquare(objFuncValue);

return;
}
Expand Down
6 changes: 2 additions & 4 deletions src/adjoint/DAObjFunc/DAObjFuncVonMisesStressKS.C
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,8 @@ void DAObjFuncVonMisesStressKS::calcObjFunc(

objFuncValue = log(objValTmp) / coeffKS_;

if (calcRefDiffSquare_)
{
objFuncValue = (objFuncValue - ref_) * (objFuncValue - ref_);
}
// check if we need to calculate refDiffSquare.
this->calcRefDiffSquare(objFuncValue);

return;
}
Expand Down
6 changes: 2 additions & 4 deletions src/adjoint/DAObjFunc/DAObjFuncWallHeatFlux.C
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,8 @@ void DAObjFuncWallHeatFlux::calcObjFunc(
// need to reduce the sum of force across all processors
reduce(objFuncValue, sumOp<scalar>());

if (calcRefDiffSquare_)
{
objFuncValue = (objFuncValue - ref_) * (objFuncValue - ref_);
}
// check if we need to calculate refDiffSquare.
this->calcRefDiffSquare(objFuncValue);

return;
}
Expand Down
26 changes: 26 additions & 0 deletions tests/refs/DAFoam_Test_DAPimpleFoamRef.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
Dictionary Key: CD
@value 7.747035248407224 1e-08 1e-10
Dictionary Key: CMZVAR
@value 41.05825112111668 1e-08 1e-10
Dictionary Key: fail
@value 0 1e-08 1e-10
Dictionary Key: CD
Expand All @@ -25,5 +27,29 @@ Dictionary Key: twist
@value 0.5092926662504045 0.0001 1e-06
Dictionary Key: uin
@value 0.1887121251618032 0.0001 1e-06
Dictionary Key: CMZVAR
Dictionary Key: actuator
@value 0.0129164206206973 0.0001 1e-06
@value -0.06721779867591618 0.0001 1e-06
@value 1.01218774787428e-06 0.0001 1e-06
@value 0 0.0001 1e-06
@value -0.03989829709014971 0.0001 1e-06
@value -1.174777939902339e-05 0.0001 1e-06
@value -0.01318488544862298 0.0001 1e-06
@value 0.02098447283371081 0.0001 1e-06
@value 7.847578532670927e-05 0.0001 1e-06
@value 1.135345283703712e-06 0.0001 1e-06
@value -0.00490063870041851 0.0001 1e-06
@value -0.007270668470157968 0.0001 1e-06
@value 0 0.0001 1e-06
Dictionary Key: alpha
@value 0.1694703610086252 0.0001 1e-06
Dictionary Key: parameter
@value 0.01322002733662093 0.0001 1e-06
Dictionary Key: twist
@value 2.800461713324844 0.0001 1e-06
Dictionary Key: uin
@value 0.5352121060453168 0.0001 1e-06

Dictionary Key: fail
@value 0 0.0001 1e-06
16 changes: 16 additions & 0 deletions tests/runTests_DAPimpleFoam.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,19 @@
"addToAdjoint": False,
}
},
"CMZVAR": {
"part1": {
"type": "moment",
"source": "patchToFace",
"patches": ["wing"],
"axis": [0.0, 0.0, 1.0],
"center": [0.25, 0.0, 0.05],
"scale": 1.0,
"addToAdjoint": True,
"calcRefDiffSquare": True,
"ref": [0.1, 0.05, 0.04, 0.02, 0.02, 0.01, 0.0, -0.01, -0.01, -0.02]
}
}
},
"adjStateOrdering": "cell",
"adjEqnOption": {
Expand Down Expand Up @@ -267,6 +280,9 @@ def regModel(val, geo):
parameterNormU = np.linalg.norm(funcsSens["CD"]["parameter"])
funcsSens["CD"]["parameter"] = parameterNormU

parameterNormM = np.linalg.norm(funcsSens["CMZVAR"]["parameter"])
funcsSens["CMZVAR"]["parameter"] = parameterNormM

if gcomm.rank == 0:
reg_write_dict(funcs, 1e-8, 1e-10)
reg_write_dict(funcsSens, 1e-4, 1e-6)
2 changes: 1 addition & 1 deletion tests/runTests_DASimpleFoamAD.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@
"scale": 1.0 / (0.5 * U0 * U0 * A0 * 1.0),
"addToAdjoint": True,
"calcRefDiffSquare": True,
"ref": 0.1
"ref": [0.1]
}
},
},
Expand Down
Loading