DAResidual.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2 
3  DAFoam : Discrete Adjoint with OpenFOAM
4  Version : v4
5 
6 \*---------------------------------------------------------------------------*/
7 
8 #include "DAResidual.H"
9 
10 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
11 
12 namespace Foam
13 {
14 
15 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
16 
17 defineTypeNameAndDebug(DAResidual, 0);
18 defineRunTimeSelectionTable(DAResidual, dictionary);
19 
20 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
21 
22 DAResidual::DAResidual(
23  const word modelType,
24  const fvMesh& mesh,
25  const DAOption& daOption,
26  const DAModel& daModel,
27  const DAIndex& daIndex)
28  : regIOobject(
29  IOobject(
30  "DAResidual", // always use DAResidual for the db name
31  mesh.time().timeName(),
32  mesh, // register to mesh
33  IOobject::NO_READ,
34  IOobject::NO_WRITE,
35  true // always register object
36  )),
37  mesh_(mesh),
38  daOption_(daOption),
39  daModel_(daModel),
40  daIndex_(daIndex),
41  daField_(mesh, daOption, daModel, daIndex)
42 {
43 }
44 
45 // * * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * //
46 
47 autoPtr<DAResidual> DAResidual::New(
48  const word modelType,
49  const fvMesh& mesh,
50  const DAOption& daOption,
51  const DAModel& daModel,
52  const DAIndex& daIndex)
53 {
54  // standard setup for runtime selectable classes
55 
56  if (daOption.getAllOptions().lookupOrDefault<label>("debug", 0))
57  {
58  Info << "Selecting " << modelType << " for DAResidual" << endl;
59  }
60 
61  dictionaryConstructorTable::iterator cstrIter =
62  dictionaryConstructorTablePtr_->find(modelType);
63 
64  // if the solver name is not found in any child class, print an error
65  if (cstrIter == dictionaryConstructorTablePtr_->end())
66  {
67  FatalErrorIn(
68  "DAResidual::New"
69  "("
70  " const word,"
71  " const fvMesh&,"
72  " const DAOption&,"
73  " const DAModel&,"
74  " const DAIndex&"
75  ")")
76  << "Unknown DAResidual type "
77  << modelType << nl << nl
78  << "Valid DAResidual types:" << endl
79  << dictionaryConstructorTablePtr_->sortedToc()
80  << exit(FatalError);
81  }
82 
83  // child class found
84  return autoPtr<DAResidual>(
85  cstrIter()(modelType, mesh, daOption, daModel, daIndex));
86 }
87 
88 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
89 
91  const dictionary& options,
92  const Vec xvVec,
93  const Vec wVec,
94  Vec resVec)
95 {
96  /*
97  Description:
98  A master function that takes the volume mesh points and state variable vecs
99  as input, and compute the residual vector.
100 
101  Input:
102  options.updateState: whether to assign the values in wVec to the state
103  variables of the OpenFOAM fields (e.g., U, p). This will also update boundary conditions
104  and update all intermediate variables that are dependent on the state
105  variables.
106 
107  options.updateMesh: whether to assign the values in xvVec to the OpenFOAM mesh
108  coordinates in Foam::fvMesh. This will also call mesh.movePoints() to update
109  all the mesh metrics such as mesh volume, cell centers, mesh surface area, etc.
110 
111  options.setResVec: whether to assign the residuals (e.g., URes_) from the OpenFOAM fields
112  to the resVec. If set to 0, nothing will be written to resVec
113 
114  options.isPC: whether to compute residual for constructing PC matrix
115 
116  xvVec: the volume coordinates vector (flatten)
117 
118  wVec: the state variable vector
119 
120  Output:
121  resVec: the residual vector
122 
123  NOTE1: the ordering of the xvVec, wVec, and resVec depends on adjStateOrdering, see
124  Foam::DAIndex for details
125 
126  NOTE2: the calcResiduals function will be implemented in the child classes
127  */
128 
129  VecZeroEntries(resVec);
130 
131  DAModel& daModel = const_cast<DAModel&>(daModel_);
132 
133  label updateState = options.getLabel("updateState");
134 
135  label updateMesh = options.getLabel("updateMesh");
136 
137  label setResVec = options.getLabel("setResVec");
138 
139  if (updateMesh)
140  {
141  FatalErrorIn("DAResidual::masterFunction")
142  << "updateMesh=true not supported!"
143  << abort(FatalError);
144  }
145 
146  if (updateState)
147  {
149 
150  // now update intermediate states and boundry conditions
153  daModel.correctBoundaryConditions();
154  daModel.updateIntermediateVariables();
155  // if there are special boundary conditions, apply special treatment
157  }
158 
159  this->calcResiduals(options);
160  daModel.calcResiduals(options);
161 
162  if (setResVec)
163  {
164  // asssign the openfoam residual field to resVec
165  daField_.ofResField2ResVec(resVec);
166  }
167 }
168 
170 {
171  FatalErrorIn("DAResidual::calcPCMatWithFvMatrix")
172  << "Child class not implemented!"
173  << abort(FatalError);
174 }
175 
176 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
177 
178 } // End namespace Foam
179 
180 // ************************************************************************* //
Foam::DAResidual::correctBoundaryConditions
virtual void correctBoundaryConditions()=0
update the boundary condition for all the states in the selected solver
Foam::DAModel::calcResiduals
void calcResiduals(const dictionary &options)
calculate the residuals for model state variables
Definition: DAModel.C:190
Foam::DAResidual::calcPCMatWithFvMatrix
virtual void calcPCMatWithFvMatrix(Mat PCMat)
calculating the adjoint preconditioner matrix using fvMatrix
Definition: DAResidual.C:169
Foam::DAOption
Definition: DAOption.H:29
Foam::DAResidual::updateIntermediateVariables
virtual void updateIntermediateVariables()=0
update any intermdiate variables that are dependent on state variables and are used in calcResiduals
DAResidual.H
Foam::DAResidual::daField_
DAField daField_
DAField object.
Definition: DAResidual.H:61
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::DAIndex
Definition: DAIndex.H:32
Foam::defineRunTimeSelectionTable
defineRunTimeSelectionTable(DAFunction, dictionary)
Foam::DAResidual::New
static autoPtr< DAResidual > New(const word modelType, const fvMesh &mesh, const DAOption &daOption, const DAModel &daModel, const DAIndex &daIndex)
Definition: DAResidual.C:47
Foam::DAModel
Definition: DAModel.H:57
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(DAFunction, 0)
Foam
Definition: checkGeometry.C:32
Foam::DAModel::correctBoundaryConditions
void correctBoundaryConditions()
correct boundary conditions for model states
Definition: DAModel.C:214
Foam::DAField::stateVec2OFField
void stateVec2OFField(const Vec stateVec) const
assign the fields in OpenFOAM based on the state vector
Definition: DAField.C:300
Foam::DAResidual::daModel_
const DAModel & daModel_
DAModel object.
Definition: DAResidual.H:55
Foam::DAResidual::masterFunction
void masterFunction(const dictionary &options, const Vec xvVec, const Vec wVec, Vec resVec)
the master function that compute the residual vector given the state and point vectors
Definition: DAResidual.C:90
Foam::DAField::ofResField2ResVec
void ofResField2ResVec(Vec resVec) const
assign the residual vector based on the residual field in OpenFOAM
Definition: DAField.C:477
Foam::DAField::specialBCTreatment
void specialBCTreatment()
apply special treatment for boundary conditions
Definition: DAField.C:608
Foam::DAResidual::calcResiduals
virtual void calcResiduals(const dictionary &options)=0
compute residuals
Foam::DAModel::updateIntermediateVariables
void updateIntermediateVariables()
update intermediate variables that are dependent on the model states
Definition: DAModel.C:234