DAPisoFoam.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/pisoFoam
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 "DAPisoFoam.H"
31 
32 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36 
37 defineTypeNameAndDebug(DAPisoFoam, 0);
38 addToRunTimeSelectionTable(DASolver, DAPisoFoam, dictionary);
39 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
40 
42  char* argsAll,
43  PyObject* pyOptions)
44  : DASolver(argsAll, pyOptions),
45  pisoPtr_(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 DAPisoFoam" << endl;
66  Time& runTime = runTimePtr_();
67  fvMesh& mesh = meshPtr_();
69 #include "createFieldsPiso.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 "createRefsPiso.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 PISO corrector
211  {
212 #include "UEqnPiso.H"
213 
214  // --- PISO loop
215  while (piso.correct())
216  {
217 #include "pEqnPiso.H"
218  }
219  }
220 
221  laminarTransport.correct();
222  daTurbulenceModelPtr_->correct();
223 
224  if (printToScreen)
225  {
226 #include "CourantNo.H"
227 
228  daTurbulenceModelPtr_->printYPlus();
229 
230  this->printAllObjFuncs();
231 
232  if (daOptionPtr_->getOption<label>("debug"))
233  {
234  this->calcPrimalResidualStatistics("print");
235  }
236 
237  Info << "ExecutionTime = " << runTime.elapsedCpuTime() << " s"
238  << " ClockTime = " << runTime.elapsedClockTime() << " s"
239  << nl << endl;
240  }
241 
242  runTime.write();
243 
244  if (mode_ == "hybridAdjoint")
245  {
246  this->saveTimeInstanceFieldHybrid(timeInstanceI);
247  }
248 
249  if (mode_ == "timeAccurateAdjoint")
250  {
251  this->saveTimeInstanceFieldTimeAccurate(timeInstanceI);
252  }
253  }
254 
255  this->calcPrimalResidualStatistics("print");
256 
257  // primal converged, assign the OpenFoam fields to the state vec wVec
258  this->ofField2StateVec(wVec);
259 
260  // write the mesh to files
261  mesh.write();
262 
263  Info << "End\n"
264  << endl;
265 
266  return 0;
267 }
268 
269 } // End namespace Foam
270 
271 // ************************************************************************* //
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
Foam::DAPisoFoam::daTurbulenceModelPtr_
autoPtr< DATurbulenceModel > daTurbulenceModelPtr_
DATurbulenceModel pointer.
Definition: DAPisoFoam.H:78
pEqnPiso.H
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::DAPisoFoam::initSolver
virtual void initSolver()
initialize fields and variables
Definition: DAPisoFoam.C:58
Foam::DASolver
Definition: DASolver.H:49
Foam::DASolver::runTimeIndexAllInstances_
List< label > runTimeIndexAllInstances_
the runtime index for all instances (unsteady)
Definition: DASolver.H:186
Foam::DAPisoFoam::solvePrimal
virtual label solvePrimal(const Vec xvVec, Vec wVec)
solve the primal equations
Definition: DAPisoFoam.C:147
laminarTransport
singlePhaseTransportModel & laminarTransport
Definition: createRefsPimple.H:9
Foam::DAPisoFoam::hasFvSource_
label hasFvSource_
whether to have fvSource term
Definition: DAPisoFoam.H:87
Foam::DAPisoFoam::daFvSourcePtr_
autoPtr< DAFvSource > daFvSourcePtr_
DASource pointer.
Definition: DAPisoFoam.H:81
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
fvSource
volScalarField & fvSource
Definition: createRefsHeatTransfer.H:7
piso
pisoControl & piso
Definition: createRefsPiso.H:5
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::DAPisoFoam::mode_
word mode_
whether the hybrid adjoint or time accurate adjoint is active
Definition: DAPisoFoam.H:99
createFieldsPiso.H
mesh
fvMesh & mesh
Definition: createRefsHeatTransfer.H:4
Foam::DASolver::isPrintTime
label isPrintTime(const Time &runTime, const label printInterval) const
Definition: DASolver.C:7460
createPisoControlPython.H
Foam::DASolver::daLinearEqnPtr_
autoPtr< DALinearEqn > daLinearEqnPtr_
DALinearEqn pointer.
Definition: DASolver.H:94
Foam::DASolver::saveTimeInstanceFieldTimeAccurate
void saveTimeInstanceFieldTimeAccurate(label &timeInstanceI)
save primal variable to time instance list for time accurate adjoint (unsteady)
Definition: DASolver.C:7772
Foam::DAPisoFoam::DAPisoFoam
DAPisoFoam(char *argsAll, PyObject *pyOptions)
Definition: DAPisoFoam.C:41
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::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::DASolver::runTimePtr_
autoPtr< Time > runTimePtr_
runTime pointer
Definition: DASolver.H:70
Foam::DASolver::meshPtr_
autoPtr< fvMesh > meshPtr_
fvMesh pointer
Definition: DASolver.H:73
createRefsPiso.H
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::DASolver::nTimeInstances_
label nTimeInstances_
number of time instances for hybird adjoint (unsteady)
Definition: DASolver.H:189
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
UEqnPiso.H
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
Foam::DASolver::stateBoundaryAllInstances_
List< List< scalar > > stateBoundaryAllInstances_
state boundary variable list for all instances (unsteady)
Definition: DASolver.H:177
DAPisoFoam.H
createAdjointIncompressible.H