DAPimpleFoam.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2 
3  DAFoam : Discrete Adjoint with OpenFOAM
4  Version : v3
5 
6  This class is modified from OpenFOAM's source code
7  applications/solvers/incompressible/pimpleFoam
8 
9  OpenFOAM: The Open Source CFD Toolbox
10 
11  Copyright (C): 2011-2016 OpenFOAM Foundation
12 
13  OpenFOAM License:
14 
15  OpenFOAM is free software: you can redistribute it and/or modify it
16  under the terms of the GNU General Public License as published by
17  the Free Software Foundation, either version 3 of the License, or
18  (at your option) any later version.
19 
20  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
21  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
22  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23  for more details.
24 
25  You should have received a copy of the GNU General Public License
26  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
27 
28 \*---------------------------------------------------------------------------*/
29 
30 #include "DAPimpleFoam.H"
31 
32 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36 
37 defineTypeNameAndDebug(DAPimpleFoam, 0);
38 addToRunTimeSelectionTable(DASolver, DAPimpleFoam, dictionary);
39 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
40 
42  char* argsAll,
43  PyObject* pyOptions)
44  : DASolver(argsAll, pyOptions),
45  pimplePtr_(nullptr),
46  pPtr_(nullptr),
47  UPtr_(nullptr),
48  phiPtr_(nullptr),
49  laminarTransportPtr_(nullptr),
50  turbulencePtr_(nullptr),
51  daTurbulenceModelPtr_(nullptr),
52  daFvSourcePtr_(nullptr),
53  fvSourcePtr_(nullptr)
54 {
55 }
56 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
57 
59 {
60  /*
61  Description:
62  Initialize variables for DASolver
63  */
64 
65  Info << "Initializing fields for DAPimpleFoam" << endl;
66  Time& runTime = runTimePtr_();
67  fvMesh& mesh = meshPtr_();
69 #include "createFieldsPimple.H"
71  // initialize checkMesh
73 
75 
76  this->setDAObjFuncList();
77 
78  mode_ = daOptionPtr_->getSubDictOption<word>("unsteadyAdjoint", "mode");
79 
80  if (mode_ == "hybridAdjoint")
81  {
82 
84  daOptionPtr_->getSubDictOption<label>("unsteadyAdjoint", "nTimeInstances");
85 
86  periodicity_ =
87  daOptionPtr_->getSubDictOption<scalar>("unsteadyAdjoint", "periodicity");
88 
89  if (periodicity_ <= 0)
90  {
91  FatalErrorIn("") << "periodicity <= 0!" << abort(FatalError);
92  }
93  }
94  else if (mode_ == "timeAccurateAdjoint")
95  {
96 
98  daOptionPtr_->getSubDictOption<label>("unsteadyAdjoint", "nTimeInstances");
99 
100  scalar endTime = runTimePtr_->endTime().value();
101  scalar deltaT = runTimePtr_->deltaTValue();
102  label maxNTimeInstances = round(endTime / deltaT) + 1;
103  if (nTimeInstances_ != maxNTimeInstances)
104  {
105  FatalErrorIn("") << "nTimeInstances in timeAccurateAdjoint is not equal to "
106  << "the maximal possible value!" << abort(FatalError);
107  }
108  }
109 
110  if (mode_ == "hybridAdjoint" || mode_ == "timeAccurateAdjoint")
111  {
112 
113  if (nTimeInstances_ <= 0)
114  {
115  FatalErrorIn("") << "nTimeInstances <= 0!" << abort(FatalError);
116  }
117 
123 
125  {
126  stateAllInstances_[idxI].setSize(daIndexPtr_->nLocalAdjointStates);
127  stateBoundaryAllInstances_[idxI].setSize(daIndexPtr_->nLocalAdjointBoundaryStates);
128  runTimeAllInstances_[idxI] = 0.0;
129  runTimeIndexAllInstances_[idxI] = 0;
130  }
131  }
132 
133  // initialize fvSource and the source term
134  const dictionary& allOptions = daOptionPtr_->getAllOptions();
135  if (allOptions.subDict("fvSource").toc().size() != 0)
136  {
137  hasFvSource_ = 1;
138  Info << "Computing fvSource" << endl;
139  word sourceName = allOptions.subDict("fvSource").toc()[0];
140  word fvSourceType = allOptions.subDict("fvSource").subDict(sourceName).getWord("type");
142  fvSourceType, mesh, daOptionPtr_(), daModelPtr_(), daIndexPtr_()));
143  daFvSourcePtr_->calcFvSource(fvSource);
144  }
145 }
146 
148  const Vec xvVec,
149  Vec wVec)
150 {
151  /*
152  Description:
153  Call the primal solver to get converged state variables
154 
155  Input:
156  xvVec: a vector that contains all volume mesh coordinates
157 
158  Output:
159  wVec: state variable vector
160  */
161 
162 #include "createRefsPimple.H"
163 
164  // change the run status
165  daOptionPtr_->setOption<word>("runStatus", "solvePrimal");
166 
167  // call correctNut, this is equivalent to turbulence->validate();
168  daTurbulenceModelPtr_->updateIntermediateVariables();
169 
170  Info << "\nStarting time loop\n"
171  << endl;
172 
173  // deform the mesh based on the xvVec
174  this->pointVec2OFMesh(xvVec);
175 
176  // check mesh quality
177  label meshOK = this->checkMesh();
178 
179  if (!meshOK)
180  {
181  this->writeFailedMesh();
182  return 1;
183  }
184 
185  // We need to set the mesh moving to false, otherwise we will get V0 not found error.
186  // Need to dig into this issue later
187  // NOTE: we have commented this out. Setting mesh.moving(false) has been done
188  // right after mesh.movePoints() calls.
189  //mesh.moving(false);
190 
191  primalMinRes_ = 1e10;
192  label printInterval = daOptionPtr_->getOption<label>("printIntervalUnsteady");
193  label printToScreen = 0;
194  label timeInstanceI = 0;
195  // for time accurate adjoints, we need to save states for Time = 0
196  if (mode_ == "timeAccurateAdjoint")
197  {
198  this->saveTimeInstanceFieldTimeAccurate(timeInstanceI);
199  }
200  // main loop
201  while (this->loop(runTime)) // using simple.loop() will have seg fault in parallel
202  {
203  printToScreen = this->isPrintTime(runTime, printInterval);
204 
205  if (printToScreen)
206  {
207  Info << "Time = " << runTime.timeName() << nl << endl;
208  }
209 
210  // --- Pressure-velocity PIMPLE corrector loop
211  while (pimple.loop())
212  {
213 
214 #include "UEqnPimple.H"
215 
216  // --- Pressure corrector loop
217  while (pimple.correct())
218  {
219 #include "pEqnPimple.H"
220  }
221 
222  laminarTransport.correct();
223  daTurbulenceModelPtr_->correct();
224  }
225 
226  if (printToScreen)
227  {
228 #include "CourantNo.H"
229 
230  daTurbulenceModelPtr_->printYPlus();
231 
232  this->printAllObjFuncs();
233 
234  if (daOptionPtr_->getOption<label>("debug"))
235  {
236  this->calcPrimalResidualStatistics("print");
237  }
238 
239  Info << "ExecutionTime = " << runTime.elapsedCpuTime() << " s"
240  << " ClockTime = " << runTime.elapsedClockTime() << " s"
241  << nl << endl;
242  }
243 
244  runTime.write();
245 
246  if (mode_ == "hybridAdjoint")
247  {
248  this->saveTimeInstanceFieldHybrid(timeInstanceI);
249  }
250 
251  if (mode_ == "timeAccurateAdjoint")
252  {
253  this->saveTimeInstanceFieldTimeAccurate(timeInstanceI);
254  }
255  }
256 
257  this->calcPrimalResidualStatistics("print");
258 
259  // primal converged, assign the OpenFoam fields to the state vec wVec
260  this->ofField2StateVec(wVec);
261 
262  // write the mesh to files
263  mesh.write();
264 
265  Info << "End\n"
266  << endl;
267 
268  return 0;
269 }
270 
271 } // End namespace Foam
272 
273 // ************************************************************************* //
Foam::DASolver::writeFailedMesh
void writeFailedMesh()
write the failed mesh to disk
Definition: DASolver.C:7955
allOptions
const dictionary & allOptions
Definition: createRefsRhoSimpleC.H:15
Foam::DASolver::loop
label loop(Time &runTime)
return whether to loop the primal solution, similar to runTime::loop() except we don't do file IO
Definition: DASolver.C:112
Foam::DASolver::daCheckMeshPtr_
autoPtr< DACheckMesh > daCheckMeshPtr_
DACheckMesh object pointer.
Definition: DASolver.H:91
forAll
forAll(pseudoP.boundaryField(), patchI)
Definition: solvePseudoPEqn.H:10
Foam::DASolver::daModelPtr_
autoPtr< DAModel > daModelPtr_
DAModel pointer.
Definition: DASolver.H:79
Foam::DACheckMesh
Definition: DACheckMesh.H:29
Foam::DASolver::daIndexPtr_
autoPtr< DAIndex > daIndexPtr_
DAIndex pointer.
Definition: DASolver.H:82
Foam::DASolver
Definition: DASolver.H:49
Foam::DASolver::runTimeIndexAllInstances_
List< label > runTimeIndexAllInstances_
the runtime index for all instances (unsteady)
Definition: DASolver.H:186
laminarTransport
singlePhaseTransportModel & laminarTransport
Definition: createRefsPimple.H:9
DAPimpleFoam.H
Foam::DASolver::daOptionPtr_
autoPtr< DAOption > daOptionPtr_
DAOption pointer.
Definition: DASolver.H:76
Foam::DASolver::saveTimeInstanceFieldHybrid
void saveTimeInstanceFieldHybrid(label &timeInstanceI)
save primal variable to time instance list for hybrid adjoint (unsteady)
Definition: DASolver.C:7727
Foam::DAPimpleFoam::hasFvSource_
label hasFvSource_
whether to have fvSource term
Definition: DAPimpleFoam.H:87
fvSource
volScalarField & fvSource
Definition: createRefsHeatTransfer.H:7
Foam::DASolver::calcPrimalResidualStatistics
void calcPrimalResidualStatistics(const word mode, const label writeRes=0)
calculate the norms of all residuals and print to screen
Definition: DASolver.C:2559
Foam::DAPimpleFoam::DAPimpleFoam
DAPimpleFoam(char *argsAll, PyObject *pyOptions)
Definition: DAPimpleFoam.C:41
pimple
pimpleControlDF & pimple
Definition: createRefsPimple.H:5
mesh
fvMesh & mesh
Definition: createRefsHeatTransfer.H:4
Foam::DASolver::isPrintTime
label isPrintTime(const Time &runTime, const label printInterval) const
Definition: DASolver.C:7460
Foam::DAPimpleFoam::mode_
word mode_
whether the hybrid adjoint or time accurate adjoint is active
Definition: DAPimpleFoam.H:99
Foam::DASolver::daLinearEqnPtr_
autoPtr< DALinearEqn > daLinearEqnPtr_
DALinearEqn pointer.
Definition: DASolver.H:94
createFieldsPimple.H
Foam::DASolver::saveTimeInstanceFieldTimeAccurate
void saveTimeInstanceFieldTimeAccurate(label &timeInstanceI)
save primal variable to time instance list for time accurate adjoint (unsteady)
Definition: DASolver.C:7772
Foam::DAPimpleFoam::solvePrimal
virtual label solvePrimal(const Vec xvVec, Vec wVec)
solve the primal equations
Definition: DAPimpleFoam.C:147
Foam::DAFvSource::New
static autoPtr< DAFvSource > New(const word modelType, const fvMesh &mesh, const DAOption &daOption, const DAModel &daModel, const DAIndex &daIndex)
Definition: DAFvSource.C:47
Foam
Definition: multiFreqScalarFvPatchField.C:144
Foam::DAPimpleFoam::initSolver
virtual void initSolver()
initialize fields and variables
Definition: DAPimpleFoam.C:58
Foam::DASolver::runTimeAllInstances_
List< scalar > runTimeAllInstances_
the runtime value for all instances (unsteady)
Definition: DASolver.H:183
Foam::DASolver::setDAObjFuncList
void setDAObjFuncList()
initialize DASolver::daObjFuncPtrList_ one needs to call this before calling printAllObjFuncs
Definition: DASolver.C:235
Foam::DASolver::printAllObjFuncs
void printAllObjFuncs()
calculate the values of all objective functions and print them to screen
Definition: DASolver.C:160
Foam::DASolver::objFuncsAllInstances_
List< dictionary > objFuncsAllInstances_
objective function for all instances (unsteady)
Definition: DASolver.H:180
Foam::DASolver::primalMinRes_
scalar primalMinRes_
the maximal residual for primal solution
Definition: DASolver.H:139
Foam::DAPimpleFoam::daTurbulenceModelPtr_
autoPtr< DATurbulenceModel > daTurbulenceModelPtr_
DATurbulenceModel pointer.
Definition: DAPimpleFoam.H:78
Foam::DASolver::runTimePtr_
autoPtr< Time > runTimePtr_
runTime pointer
Definition: DASolver.H:70
Foam::DASolver::meshPtr_
autoPtr< fvMesh > meshPtr_
fvMesh pointer
Definition: DASolver.H:73
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(DAFvSource, 0)
Foam::addToRunTimeSelectionTable
addToRunTimeSelectionTable(DAFvSource, DAFvSourceActuatorDisk, dictionary)
Foam::DASolver::ofField2StateVec
void ofField2StateVec(Vec stateVec) const
set the state vector based on the latest fields in OpenFOAM
Definition: DASolver.H:596
Foam::DASolver::checkMesh
label checkMesh() const
check the mesh quality and return meshOK
Definition: DASolver.H:710
Foam::DALinearEqn
Definition: DALinearEqn.H:31
Foam::DAPimpleFoam::daFvSourcePtr_
autoPtr< DAFvSource > daFvSourcePtr_
DASource pointer.
Definition: DAPimpleFoam.H:81
UEqnPimple.H
Foam::DASolver::nTimeInstances_
label nTimeInstances_
number of time instances for hybird adjoint (unsteady)
Definition: DASolver.H:189
createRefsPimple.H
runTime
Time & runTime
Definition: createRefsHeatTransfer.H:1
Foam::DASolver::stateAllInstances_
List< List< scalar > > stateAllInstances_
state variable list for all instances (unsteady)
Definition: DASolver.H:174
Foam::DASolver::periodicity_
scalar periodicity_
periodicity of oscillating flow variables (unsteady)
Definition: DASolver.H:192
Foam::DASolver::pointVec2OFMesh
void pointVec2OFMesh(const Vec xvVec) const
assign the points in fvMesh of OpenFOAM based on the point vector
Definition: DASolver.H:608
pEqnPimple.H
Foam::DASolver::stateBoundaryAllInstances_
List< List< scalar > > stateBoundaryAllInstances_
state boundary variable list for all instances (unsteady)
Definition: DASolver.H:177
createAdjointIncompressible.H
createPimpleControlPython.H