Skip to content

Commit 42b2d10

Browse files
committed
Added patch and field Max objs.
1 parent a57881f commit 42b2d10

File tree

5 files changed

+443
-0
lines changed

5 files changed

+443
-0
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*---------------------------------------------------------------------------*\
2+
3+
DAFoam : Discrete Adjoint with OpenFOAM
4+
Version : v3
5+
6+
\*---------------------------------------------------------------------------*/
7+
8+
#include "DAObjFuncFieldMaxKS.H"
9+
10+
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
11+
12+
namespace Foam
13+
{
14+
15+
defineTypeNameAndDebug(DAObjFuncFieldMaxKS, 0);
16+
addToRunTimeSelectionTable(DAObjFunc, DAObjFuncFieldMaxKS, dictionary);
17+
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
18+
19+
DAObjFuncFieldMaxKS::DAObjFuncFieldMaxKS(
20+
const fvMesh& mesh,
21+
const DAOption& daOption,
22+
const DAModel& daModel,
23+
const DAIndex& daIndex,
24+
const DAResidual& daResidual,
25+
const word objFuncName,
26+
const word objFuncPart,
27+
const dictionary& objFuncDict)
28+
: DAObjFunc(
29+
mesh,
30+
daOption,
31+
daModel,
32+
daIndex,
33+
daResidual,
34+
objFuncName,
35+
objFuncPart,
36+
objFuncDict)
37+
{
38+
// Assign type, this is common for all objectives
39+
objFuncDict_.readEntry<word>("type", objFuncType_);
40+
41+
objFuncDict_.readEntry<word>("varName", varName_);
42+
43+
objFuncDict_.readEntry<word>("varType", varType_);
44+
45+
objFuncDict_.readEntry<label>("component", component_);
46+
47+
objFuncDict_.readEntry<scalar>("coeffKS", coeffKS_);
48+
}
49+
50+
/// calculate the value of objective function
51+
void DAObjFuncFieldMaxKS::calcObjFunc(
52+
const labelList& objFuncFaceSources,
53+
const labelList& objFuncCellSources,
54+
scalarList& objFuncFaceValues,
55+
scalarList& objFuncCellValues,
56+
scalar& objFuncValue)
57+
{
58+
/*
59+
Description:
60+
Calculate the patch approximated max using the KS function
61+
62+
Input:
63+
objFuncFaceSources: List of face source (index) for this objective
64+
65+
objFuncCellSources: List of cell source (index) for this objective
66+
67+
Output:
68+
objFuncFaceValues: the discrete value of objective for each face source (index).
69+
This will be used for computing df/dw in the adjoint.
70+
71+
objFuncCellValues: the discrete value of objective on each cell source (index).
72+
This will be used for computing df/dw in the adjoint.
73+
74+
objFuncValue: the sum of objective, reduced across all processors and scaled by "scale"
75+
*/
76+
77+
// initialize objFunValue
78+
objFuncValue = 0.0;
79+
80+
const objectRegistry& db = mesh_.thisDb();
81+
82+
if (varType_ == "scalar")
83+
{
84+
const volScalarField& var = db.lookupObject<volScalarField>(varName_);
85+
86+
forAll(var, cellI)
87+
{
88+
objFuncValue += exp(coeffKS_ * var[cellI]);
89+
90+
if (objFuncValue > 1e200)
91+
{
92+
FatalErrorIn(" ") << "KS function summation term too large! "
93+
<< "Reduce coeffKS! " << abort(FatalError);
94+
}
95+
}
96+
}
97+
else if (varType_ == "vector")
98+
{
99+
const volVectorField& var = db.lookupObject<volVectorField>(varName_);
100+
forAll(var, cellI)
101+
{
102+
objFuncValue += exp(coeffKS_ * var[cellI][component_]);
103+
104+
if (objFuncValue > 1e200)
105+
{
106+
FatalErrorIn(" ") << "KS function summation term too large! "
107+
<< "Reduce coeffKS! " << abort(FatalError);
108+
}
109+
}
110+
}
111+
else
112+
{
113+
FatalErrorIn("DAObjFuncFieldMaxKS::calcObjFunc")
114+
<< "varType not valid. Options are scalar or vector"
115+
<< abort(FatalError);
116+
}
117+
118+
// need to reduce the sum of force across all processors
119+
reduce(objFuncValue, sumOp<scalar>());
120+
121+
objFuncValue = log(objFuncValue) / coeffKS_;
122+
123+
// check if we need to calculate refDiff.
124+
this->calcRefVar(objFuncValue);
125+
126+
return;
127+
}
128+
129+
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
130+
131+
} // End namespace Foam
132+
133+
// ************************************************************************* //
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*---------------------------------------------------------------------------*\
2+
3+
DAFoam : Discrete Adjoint with OpenFOAM
4+
Version : v3
5+
6+
Description:
7+
Child class for approximated max variable (in tne
8+
entire field) using the KS function
9+
10+
\*---------------------------------------------------------------------------*/
11+
12+
#ifndef DAObjFuncFieldMaxKS_H
13+
#define DAObjFuncFieldMaxKS_H
14+
15+
#include "DAObjFunc.H"
16+
#include "addToRunTimeSelectionTable.H"
17+
18+
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
19+
20+
namespace Foam
21+
{
22+
23+
/*---------------------------------------------------------------------------*\
24+
Class DAObjFuncFieldMaxKS Declaration
25+
\*---------------------------------------------------------------------------*/
26+
27+
class DAObjFuncFieldMaxKS
28+
: public DAObjFunc
29+
{
30+
31+
protected:
32+
33+
/// name of the variable
34+
word varName_;
35+
36+
/// type of the variable either vector or scalar
37+
word varType_;
38+
39+
/// if vector which element?
40+
label component_;
41+
42+
/// the KS coefficient
43+
scalar coeffKS_;
44+
45+
public:
46+
TypeName("fieldMaxKS");
47+
// Constructors
48+
49+
//- Construct from components
50+
DAObjFuncFieldMaxKS(
51+
const fvMesh& mesh,
52+
const DAOption& daOption,
53+
const DAModel& daModel,
54+
const DAIndex& daIndex,
55+
const DAResidual& daResidual,
56+
const word objFuncName,
57+
const word objFuncPart,
58+
const dictionary& objFuncDict);
59+
60+
//- Destructor
61+
virtual ~DAObjFuncFieldMaxKS()
62+
{
63+
}
64+
65+
/// calculate the value of objective function
66+
virtual void calcObjFunc(
67+
const labelList& objFuncFaceSources,
68+
const labelList& objFuncCellSources,
69+
scalarList& objFuncFaceValues,
70+
scalarList& objFuncCellValues,
71+
scalar& objFuncValue);
72+
};
73+
74+
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
75+
76+
} // End namespace Foam
77+
78+
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
79+
80+
#endif
81+
82+
// ************************************************************************* //
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/*---------------------------------------------------------------------------*\
2+
3+
DAFoam : Discrete Adjoint with OpenFOAM
4+
Version : v3
5+
6+
\*---------------------------------------------------------------------------*/
7+
8+
#include "DAObjFuncPatchMaxKS.H"
9+
10+
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
11+
12+
namespace Foam
13+
{
14+
15+
defineTypeNameAndDebug(DAObjFuncPatchMaxKS, 0);
16+
addToRunTimeSelectionTable(DAObjFunc, DAObjFuncPatchMaxKS, dictionary);
17+
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
18+
19+
DAObjFuncPatchMaxKS::DAObjFuncPatchMaxKS(
20+
const fvMesh& mesh,
21+
const DAOption& daOption,
22+
const DAModel& daModel,
23+
const DAIndex& daIndex,
24+
const DAResidual& daResidual,
25+
const word objFuncName,
26+
const word objFuncPart,
27+
const dictionary& objFuncDict)
28+
: DAObjFunc(
29+
mesh,
30+
daOption,
31+
daModel,
32+
daIndex,
33+
daResidual,
34+
objFuncName,
35+
objFuncPart,
36+
objFuncDict)
37+
{
38+
// Assign type, this is common for all objectives
39+
objFuncDict_.readEntry<word>("type", objFuncType_);
40+
41+
objFuncDict_.readEntry<word>("varName", varName_);
42+
43+
objFuncDict_.readEntry<word>("varType", varType_);
44+
45+
objFuncDict_.readEntry<label>("component", component_);
46+
47+
objFuncDict_.readEntry<scalar>("coeffKS", coeffKS_);
48+
}
49+
50+
/// calculate the value of objective function
51+
void DAObjFuncPatchMaxKS::calcObjFunc(
52+
const labelList& objFuncFaceSources,
53+
const labelList& objFuncCellSources,
54+
scalarList& objFuncFaceValues,
55+
scalarList& objFuncCellValues,
56+
scalar& objFuncValue)
57+
{
58+
/*
59+
Description:
60+
Calculate the patch approximated max using the KS function
61+
62+
Input:
63+
objFuncFaceSources: List of face source (index) for this objective
64+
65+
objFuncCellSources: List of cell source (index) for this objective
66+
67+
Output:
68+
objFuncFaceValues: the discrete value of objective for each face source (index).
69+
This will be used for computing df/dw in the adjoint.
70+
71+
objFuncCellValues: the discrete value of objective on each cell source (index).
72+
This will be used for computing df/dw in the adjoint.
73+
74+
objFuncValue: the sum of objective, reduced across all processors and scaled by "scale"
75+
*/
76+
77+
// initialize objFunValue
78+
objFuncValue = 0.0;
79+
80+
const objectRegistry& db = mesh_.thisDb();
81+
82+
if (varType_ == "scalar")
83+
{
84+
const volScalarField& var = db.lookupObject<volScalarField>(varName_);
85+
86+
forAll(objFuncFaceSources, idxI)
87+
{
88+
const label& objFuncFaceI = objFuncFaceSources[idxI];
89+
label bFaceI = objFuncFaceI - daIndex_.nLocalInternalFaces;
90+
const label patchI = daIndex_.bFacePatchI[bFaceI];
91+
const label faceI = daIndex_.bFaceFaceI[bFaceI];
92+
93+
objFuncValue += exp(coeffKS_ * var.boundaryField()[patchI][faceI]);
94+
95+
if (objFuncValue > 1e200)
96+
{
97+
FatalErrorIn(" ") << "KS function summation term too large! "
98+
<< "Reduce coeffKS! " << abort(FatalError);
99+
}
100+
}
101+
}
102+
else if (varType_ == "vector")
103+
{
104+
const volVectorField& var = db.lookupObject<volVectorField>(varName_);
105+
106+
// calculate area weighted heat flux
107+
forAll(objFuncFaceSources, idxI)
108+
{
109+
const label& objFuncFaceI = objFuncFaceSources[idxI];
110+
label bFaceI = objFuncFaceI - daIndex_.nLocalInternalFaces;
111+
const label patchI = daIndex_.bFacePatchI[bFaceI];
112+
const label faceI = daIndex_.bFaceFaceI[bFaceI];
113+
114+
objFuncValue += exp(coeffKS_ * var.boundaryField()[patchI][faceI][component_]);
115+
116+
if (objFuncValue > 1e200)
117+
{
118+
FatalErrorIn(" ") << "KS function summation term too large! "
119+
<< "Reduce coeffKS! " << abort(FatalError);
120+
}
121+
}
122+
}
123+
else
124+
{
125+
FatalErrorIn("DAObjFuncPatchMaxKS::calcObjFunc")
126+
<< "varType not valid. Options are scalar or vector"
127+
<< abort(FatalError);
128+
}
129+
130+
// need to reduce the sum of force across all processors
131+
reduce(objFuncValue, sumOp<scalar>());
132+
133+
objFuncValue = log(objFuncValue) / coeffKS_;
134+
135+
// check if we need to calculate refDiff.
136+
this->calcRefVar(objFuncValue);
137+
138+
return;
139+
}
140+
141+
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
142+
143+
} // End namespace Foam
144+
145+
// ************************************************************************* //

0 commit comments

Comments
 (0)