DAFunctionResidualNorm.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2 
3  DAFoam : Discrete Adjoint with OpenFOAM
4  Version : v4
5 
6 \*---------------------------------------------------------------------------*/
7 
9 
10 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
11 
12 namespace Foam
13 {
14 
15 defineTypeNameAndDebug(DAFunctionResidualNorm, 0);
16 addToRunTimeSelectionTable(DAFunction, DAFunctionResidualNorm, dictionary);
17 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
18 
20  const fvMesh& mesh,
21  const DAOption& daOption,
22  const DAModel& daModel,
23  const DAIndex& daIndex,
24  const word functionName)
25  : DAFunction(
26  mesh,
27  daOption,
28  daModel,
29  daIndex,
30  functionName)
31 {
32  // initialize stateInfo_
33  word solverName = daOption.getOption<word>("solverName");
34  autoPtr<DAStateInfo> daStateInfo(DAStateInfo::New(solverName, mesh, daOption, daModel));
35  stateInfo_ = daStateInfo->getStateInfo();
36 
37  dictionary resWeightDict = daOption.getAllOptions().subDict("function").subDict(functionName).subDict("resWeight");
38 
39  forAll(resWeightDict.toc(), idxI)
40  {
41  word resName = resWeightDict.toc()[idxI];
42  scalar weight = resWeightDict.getScalar(resName);
43  resWeight_.set(resName, weight);
44  }
45  Info << "residual weights for DAFunctionResidualNorm " << resWeight_;
46 }
47 
50 {
51  /*
52  Description:
53  Calculate the L2 norm of all residuals
54  */
55 
56  // initialize objFunValue
57  scalar functionValue = 0.0;
58 
59  const objectRegistry& db = mesh_.thisDb();
60  DAResidual& daResidual = const_cast<DAResidual&>(db.lookupObject<DAResidual>("DAResidual"));
61  DAModel& daModel = const_cast<DAModel&>(daModel_);
62 
63  dictionary options;
64  options.set("isPC", 0);
65  daResidual.calcResiduals(options);
66  daModel.calcResiduals(options);
67 
68  forAll(stateInfo_["volVectorStates"], idxI)
69  {
70  const word stateName = stateInfo_["volVectorStates"][idxI];
71  const word resName = stateName + "Res";
72  const volVectorField& stateRes = mesh_.thisDb().lookupObject<volVectorField>(resName);
73  scalar weight2 = resWeight_[resName] * resWeight_[resName];
74 
75  forAll(stateRes, cellI)
76  {
77  for (label i = 0; i < 3; i++)
78  {
79  functionValue += weight2 * stateRes[cellI][i] * stateRes[cellI][i];
80  }
81  }
82  }
83 
84  forAll(stateInfo_["volScalarStates"], idxI)
85  {
86  const word stateName = stateInfo_["volScalarStates"][idxI];
87  const word resName = stateName + "Res";
88  const volScalarField& stateRes = mesh_.thisDb().lookupObject<volScalarField>(resName);
89  scalar weight2 = resWeight_[resName] * resWeight_[resName];
90 
91  forAll(stateRes, cellI)
92  {
93  functionValue += weight2 * stateRes[cellI] * stateRes[cellI];
94  }
95  }
96 
97  forAll(stateInfo_["modelStates"], idxI)
98  {
99  const word stateName = stateInfo_["modelStates"][idxI];
100  const word resName = stateName + "Res";
101  const volScalarField& stateRes = mesh_.thisDb().lookupObject<volScalarField>(resName);
102  scalar weight2 = resWeight_[resName] * resWeight_[resName];
103 
104  forAll(stateRes, cellI)
105  {
106  functionValue += weight2 * stateRes[cellI] * stateRes[cellI];
107  }
108  }
109 
110  forAll(stateInfo_["surfaceScalarStates"], idxI)
111  {
112  const word stateName = stateInfo_["surfaceScalarStates"][idxI];
113  const word resName = stateName + "Res";
114  const surfaceScalarField& stateRes = mesh_.thisDb().lookupObject<surfaceScalarField>(resName);
115  scalar weight2 = resWeight_[resName] * resWeight_[resName];
116 
117  forAll(stateRes, faceI)
118  {
119  functionValue += weight2 * stateRes[faceI] * stateRes[faceI];
120  }
121  forAll(stateRes.boundaryField(), patchI)
122  {
123  forAll(stateRes.boundaryField()[patchI], faceI)
124  {
125  scalar bPhiRes = stateRes.boundaryField()[patchI][faceI];
126  functionValue += weight2 * bPhiRes * bPhiRes;
127  }
128  }
129  }
130 
131  // need to reduce the sum of force across all processors
132  reduce(functionValue, sumOp<scalar>());
133 
134  functionValue /= daIndex_.nGlobalAdjointStates;
135 
136  // check if we need to calculate refDiff.
137  this->calcRefVar(functionValue);
138 
139  return functionValue;
140 }
141 
142 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
143 
144 } // End namespace Foam
145 
146 // ************************************************************************* //
Foam::DAFunctionResidualNorm::resWeight_
HashTable< scalar > resWeight_
weights of the residuals
Definition: DAFunctionResidualNorm.H:33
Foam::DAStateInfo::New
static autoPtr< DAStateInfo > New(const word modelType, const fvMesh &mesh, const DAOption &daOption, const DAModel &daModel)
Definition: DAStateInfo.C:43
Foam::DAModel::calcResiduals
void calcResiduals(const dictionary &options)
calculate the residuals for model state variables
Definition: DAModel.C:190
Foam::DAOption
Definition: DAOption.H:29
Foam::addToRunTimeSelectionTable
addToRunTimeSelectionTable(DAFunction, DAFunctionForce, dictionary)
Foam::DAOption::getOption
classType getOption(const word key) const
get an option from subDict and key
Definition: DAOption.H:92
Foam::DAFunction::calcRefVar
void calcRefVar(scalar &functionValue)
calculate (var-ref)^2
Definition: DAFunction.C:216
solverName
word solverName
Definition: createAdjoint.H:14
Foam::DAFunction::daIndex_
const DAIndex & daIndex_
DAIndex object.
Definition: DAFunction.H:52
Foam::DAOption::getAllOptions
const dictionary & getAllOptions() const
return a reference of allOptions_ dictionary
Definition: DAOption.H:56
mesh
fvMesh & mesh
Definition: createRefsHeatTransfer.H:4
Foam::DAFunction::daModel_
const DAModel & daModel_
DAModel object.
Definition: DAFunction.H:49
Foam::DAFunctionResidualNorm::calcFunction
virtual scalar calcFunction()
calculate the value of objective function
Definition: DAFunctionResidualNorm.C:49
Foam::DAIndex
Definition: DAIndex.H:32
Foam::DAModel
Definition: DAModel.H:57
Foam::DAFunction
Definition: DAFunction.H:31
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(DAFunction, 0)
Foam::DAFunctionResidualNorm::stateInfo_
HashTable< wordList > stateInfo_
the stateInfo_ list from DAStateInfo object
Definition: DAFunctionResidualNorm.H:36
Foam::DAFunctionResidualNorm::DAFunctionResidualNorm
DAFunctionResidualNorm(const fvMesh &mesh, const DAOption &daOption, const DAModel &daModel, const DAIndex &daIndex, const word functionName)
Definition: DAFunctionResidualNorm.C:19
Foam
Definition: checkGeometry.C:32
forAll
forAll(nuTilda1, cellI)
Definition: nuTilda1EqnIrkPimple.H:19
Foam::DAResidual
Definition: DAResidual.H:36
DAFunctionResidualNorm.H
Foam::DAIndex::nGlobalAdjointStates
label nGlobalAdjointStates
number of global adjoint states (including all cells and faces)
Definition: DAIndex.H:148
Foam::DAResidual::calcResiduals
virtual void calcResiduals(const dictionary &options)=0
compute residuals
Foam::DAFunction::mesh_
const fvMesh & mesh_
fvMesh
Definition: DAFunction.H:43