DASolver.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2 
3  DAFoam : Discrete Adjoint with OpenFOAM
4  Version : v4
5 
6  Description:
7  Solver class that calls primal and adjoint solvers,
8  and compute the total derivatives
9 
10 \*---------------------------------------------------------------------------*/
11 
12 #ifndef DASolver_H
13 #define DASolver_H
14 
15 #include <petscksp.h>
16 #include "Python.h"
17 #include "fvCFD.H"
18 #include "fvMesh.H"
19 #include "runTimeSelectionTables.H"
20 #include "OFstream.H"
21 #include "functionObjectList.H"
22 #include "fvOptions.H"
23 #include "DAUtility.H"
24 #include "DACheckMesh.H"
25 #include "DAOption.H"
26 #include "DAStateInfo.H"
27 #include "DAModel.H"
28 #include "DAIndex.H"
29 #include "DAFunction.H"
30 #include "DAJacCon.H"
31 #include "DAColoring.H"
32 #include "DAResidual.H"
33 #include "DAField.H"
34 #include "DAPartDeriv.H"
35 #include "DALinearEqn.H"
36 #include "DARegression.H"
37 #include "volPointInterpolation.H"
38 #include "IOMRFZoneListDF.H"
39 #include "interpolateSplineXY.H"
40 #include "DAInput.H"
41 #include "DAOutput.H"
42 #include "DAGlobalVar.H"
43 #include "DATimeOp.H"
44 
45 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
46 
47 namespace Foam
48 {
49 
50 /*---------------------------------------------------------------------------*\
51  Class DASolver Declaration
52 \*---------------------------------------------------------------------------*/
53 
54 class DASolver
55 {
56 
57 private:
59  DASolver(const DASolver&);
60 
62  void operator=(const DASolver&);
63 
64 protected:
66  char* argsAll_;
67 
69  PyObject* pyOptions_;
70 
72  autoPtr<argList> argsPtr_;
73 
75  autoPtr<Time> runTimePtr_;
76 
78  autoPtr<fvMesh> meshPtr_;
79 
81  autoPtr<DAOption> daOptionPtr_;
82 
84  autoPtr<DAModel> daModelPtr_;
85 
87  autoPtr<DAIndex> daIndexPtr_;
88 
90  autoPtr<DAField> daFieldPtr_;
91 
93  UPtrList<DAFunction> daFunctionPtrList_;
94 
96  UPtrList<DATimeOp> daTimeOpPtrList_;
97 
99  autoPtr<DACheckMesh> daCheckMeshPtr_;
100 
102  autoPtr<DALinearEqn> daLinearEqnPtr_;
103 
105  autoPtr<DAResidual> daResidualPtr_;
106 
108  autoPtr<DAStateInfo> daStateInfoPtr_;
109 
111  autoPtr<DARegression> daRegressionPtr_;
112 
114  autoPtr<DAGlobalVar> daGlobalVarPtr_;
115 
117  autoPtr<pointField> points0Ptr_;
118 
120  HashTable<wordList> stateInfo_;
121 
123  scalar prevPrimalSolTime_ = -1e10;
124 
125  label isPrintTime(
126  const Time& runTime,
127  const label printInterval) const;
128 
130  label checkPrimalFailure();
131 
134  const dictionary& maxResConLv4JacPCMat,
135  HashTable<List<List<word>>>& stateResConInfo) const;
136 
138  void writeAssociatedFields();
139 
141  Mat dRdWTMF_;
142 
145 
147  scalar primalMinResTol_ = 0.0;
148 
150  label printToScreen_ = 0;
151 
153  label regModelFail_ = 0;
154 
156  label printInterval_ = 100;
157 
159  label primalMinIters_ = -1;
160 
163 
165  List<scalarList> functionTimeSteps_;
166 
169 
171  HashTable<scalar> initStateVals_;
172 
173 public:
175  TypeName("DASolver");
176 
177  // Declare run-time constructor selection table
179  autoPtr,
180  DASolver,
181  dictionary,
182  (char* argsAll,
183  PyObject* pyOptions),
184  (argsAll, pyOptions));
185 
186  // Constructors
187 
188  //- Construct from components
189  DASolver(
190  char* argsAll,
191  PyObject* pyOptions);
192 
193  // Selectors
194 
195  //- Return a reference to the selected model
196  static autoPtr<DASolver> New(
197  char* argsAll,
198  PyObject* pyOptions);
199 
200  //- Destructor
201  virtual ~DASolver()
202  {
203  }
204 
205  // Member functions
206 
208  virtual void initSolver() = 0;
209 
211  virtual label solvePrimal() = 0;
212 
214  virtual label runFPAdj(
215  Vec dFdW,
216  Vec psi);
217 
219  virtual label solveAdjointFP(
220  Vec dFdW,
221  Vec psi);
222 
224  void setTime(scalar time, label timeIndex)
225  {
226  runTimePtr_->setTime(time, timeIndex);
227  }
228 
231  {
232  const fvSchemes& myFvSchemes = meshPtr_->thisDb().lookupObject<fvSchemes>("fvSchemes");
233 
234  word ddtSchemeName = myFvSchemes.subDict("ddtSchemes").getWord("default");
235 
236  if (ddtSchemeName == "steadyState")
237  {
238  return 0;
239  }
240  if (ddtSchemeName == "Euler")
241  {
242  return 1;
243  }
244  else if (ddtSchemeName == "backward")
245  {
246  return 2;
247  }
248  else
249  {
250  FatalErrorIn("") << "ddtScheme " << ddtSchemeName << " not supported! Options: steadyState, Euler, or backward"
251  << abort(FatalError);
252  return -1;
253  }
254  }
255 
256  void printElapsedTime(const Time& runTime, const label printToScreen)
257  {
258  if (printToScreen)
259  {
260  Info << "ExecutionTime = " << runTime.elapsedCpuTime() << " s"
261  << " ClockTime = " << runTime.elapsedClockTime() << " s"
262  << nl << endl;
263  }
264  }
265 
267  void calcdRdWT(
268  const label isPC,
269  Mat dRdWT);
270 
272  void updateKSPPCMat(
273  Mat PCMat,
274  KSP ksp);
275 
277  label solveLinearEqn(
278  const KSP ksp,
279  const Vec rhsVec,
280  Vec solVec);
281 
283  void updateOFFields(const scalar* states);
284 
286  void updateOFMesh(const scalar* volCoords);
287 
289  void getOFFields(scalar* states)
290  {
291  daFieldPtr_->ofField2State(states);
292  }
293 
295  void getOFField(
296  const word fieldName,
297  const word fieldType,
298  double* field);
299 
301  void getOFMeshPoints(double* points);
302 
304  label getInputSize(
305  const word inputName,
306  const word inputType);
307 
309  label getOutputSize(
310  const word outputName,
311  const word outputType);
312 
314  label getInputDistributed(
315  const word inputName,
316  const word inputType);
317 
319  label getOutputDistributed(
320  const word outputName,
321  const word outputType);
322 
324  void calcOutput(
325  const word outputName,
326  const word outputType,
327  double* output);
328 
330  void runColoring();
331 
333  void calcJacTVecProduct(
334  const word inputName,
335  const word inputType,
336  const double* input,
337  const word outputName,
338  const word outputType,
339  const double* seed,
340  double* product);
341 
342  void setSolverInput(
343  const word inputName,
344  const word inputType,
345  const int inputSize,
346  const double* input,
347  const double* seed);
348 
351  const Mat jacPCMat,
352  KSP ksp);
353 
355  void calcdRdWOldTPsiAD(
356  const label oldTimeLevel,
357  const double* psi,
358  double* dRdWOldTPsi);
359 
362  const scalar* volCoords,
363  scalar* surfCoords);
364 
366  void getCouplingPatchList(wordList& patchList, word groupName = "NONE");
367 
369  static PetscErrorCode dRdWTMatVecMultFunction(
370  Mat dRdWT,
371  Vec vecX,
372  Vec vecY);
373 
376 
378  void destroydRdWTMatrixFree();
379 
381  void registerStateVariableInput4AD(const label oldTimeLevel = 0);
382 
384  void deactivateStateVariableInput4AD(const label oldTimeLevel = 0);
385 
388 
390  void assignVec2ResidualGradient(const double* vecX);
391 
394  double* vecY,
395  const label oldTimeLevel = 0);
396 
398  void normalizeGradientVec(double* vecY);
399 
402  const word inputName,
403  double* product);
404 
407 
409  label hasVolCoordInput();
410 
412  void initDynamicMesh();
413 
415  label loop(Time& runTime);
416 
419 
421  void initInputFieldUnsteady();
422 
424  void meanStatesToStates();
425 
428  const label idxPoint,
429  const label idxCoord) const
430  {
431  return daIndexPtr_->getGlobalXvIndex(idxPoint, idxCoord);
432  }
433 
436  const Mat matIn,
437  const word prefix)
438  {
439  DAUtility::writeMatrixBinary(matIn, prefix);
440  }
441 
444  const Mat matIn,
445  const word prefix)
446  {
447  DAUtility::writeMatrixASCII(matIn, prefix);
448  }
449 
452  Mat matIn,
453  const word prefix)
454  {
455  DAUtility::readMatrixBinary(matIn, prefix);
456  }
457 
460  const Vec vecIn,
461  const word prefix)
462  {
463  DAUtility::writeVectorASCII(vecIn, prefix);
464  }
465 
468  Vec vecIn,
469  const word prefix)
470  {
471  DAUtility::readVectorBinary(vecIn, prefix);
472  }
473 
476  const Vec vecIn,
477  const word prefix)
478  {
479  DAUtility::writeVectorBinary(vecIn, prefix);
480  }
481 
484  {
485  return daIndexPtr_->nLocalAdjointStates;
486  }
487 
490  {
491  return daIndexPtr_->nLocalAdjointBoundaryStates;
492  }
493 
495  label getNLocalCells() const
496  {
497  return meshPtr_->nCells();
498  }
499 
501  label getNLocalPoints() const
502  {
503  return meshPtr_->nPoints();
504  }
505 
507  void setDAFunctionList();
508 
510  void calcAllFunctions(label print = 0);
511 
513  double getTimeOpFuncVal(const word functionName);
514 
516  label getFunctionListIndex(const word functionName)
517  {
519  {
520  DAFunction& daFunction = daFunctionPtrList_[idxI];
521  word functionName1 = daFunction.getFunctionName();
522  if (functionName1 == functionName)
523  {
524  return idxI;
525  }
526  }
527  }
528 
530  label checkMesh() const
531  {
532  return daCheckMeshPtr_->run();
533  }
534 
536  scalar getdFScaling(const word functionName, const label timeIdx);
537 
540  {
541  Info << "DAFoam option dictionary: ";
542  Info << daOptionPtr_->getAllOptions() << endl;
543  }
544 
547  const word mode,
548  const label writeRes = 0);
549 
551  void updateDAOption(PyObject* pyOptions)
552  {
553  daOptionPtr_->updateDAOption(pyOptions);
554  }
555 
558  {
559  return prevPrimalSolTime_;
560  }
561 
564  const word fieldName,
565  const word fieldType);
566 
569 
571  void calcResiduals(label isPC = 0);
572 
575  const fvMesh& getMesh()
576  {
577  return meshPtr_();
578  }
579 
581  const Time& getRunTime()
582  {
583  return runTimePtr_();
584  }
585 
588  {
589  return daOptionPtr_();
590  }
591 
594  {
595  return daStateInfoPtr_();
596  }
597 
600  {
601  return daIndexPtr_();
602  }
603 
606  {
607  return daModelPtr_();
608  }
609 
612  {
613  return daResidualPtr_();
614  }
615 
618  {
619  return daFieldPtr_();
620  }
621 
624  {
625  return daLinearEqnPtr_();
626  }
627 
630  {
631  return daCheckMeshPtr_();
632  }
633 
635  label getNRegressionParameters(word modelName)
636  {
637  return daRegressionPtr_->nParameters(modelName);
638  }
639 
641  scalar getRegressionParameter(word modelName, const label idxI)
642  {
643  return daRegressionPtr_->getParameter(modelName, idxI);
644  }
645 
647  void setRegressionParameter(word modelName, const label idxI, scalar val)
648  {
649  daRegressionPtr_->setParameter(modelName, idxI, val);
650  }
651 
654  {
655  daRegressionPtr_->compute();
656  }
657 
659  void setPrimalBoundaryConditions(const label printInfo = 1);
660 
662  void writeFailedMesh();
663 
665  void readStateVars(
666  scalar timeVal,
667  label oldTimeLevel = 0);
668 
670  void readMeshPoints(const scalar timeVal);
671 
673  void writeMeshPoints(const double* points, const scalar timeVal);
674 
676  void calcPCMatWithFvMatrix(Mat PCMat, const label turbOnly = 0);
677 
680  pyComputeInterface computeInterface,
681  void* compute,
682  pyJacVecProdInterface jacVecProdInterface,
683  void* jacVecProd,
684  pySetCharInterface setModelNameInterface,
685  void* setModelName)
686  {
687  DAUtility::pyCalcBetaInterface = computeInterface;
688  DAUtility::pyCalcBeta = compute;
689 
690  DAUtility::pyCalcBetaJacVecProdInterface = jacVecProdInterface;
691  DAUtility::pyCalcBetaJacVecProd = jacVecProd;
692 
693  DAUtility::pySetModelNameInterface = setModelNameInterface;
694  DAUtility::pySetModelName = setModelName;
695  }
696 
698  void writeAdjStates(
699  const label writeMesh,
700  const wordList& additionalOutput);
701 
704  {
705  return runTimePtr_->elapsedClockTime();
706  }
707 
710  {
711  return runTimePtr_->elapsedCpuTime();
712  }
713 
714 #ifdef CODI_ADR
715 
717  codi::RealReverse::Tape& globalADTape_;
718 
719 #endif
720 
722  template<class classType>
723  label validateField(const classType& field);
724 
726  template<class classType>
727  label validateVectorField(const classType& field);
728 
730  label validateStates();
731 
733  void getInitStateVals(HashTable<scalar>& initState);
734 
736  void resetStateVals();
737 
739  void writeSensMapSurface(
740  const word name,
741  const double* dFdXs,
742  const double* Xs,
743  const label size,
744  const double timeName);
745 
747  void writeSensMapField(
748  const word name,
749  const double* dFdField,
750  const word fieldType,
751  const double timeName);
752 
754  scalar getLatestTime()
755  {
756  instantList timeDirs = runTimePtr_->findTimes(runTimePtr_->path(), runTimePtr_->constant());
757  scalar latestTime = timeDirs.last().value();
758  return latestTime;
759  }
760 
762  void writeAdjointFields(
763  const word function,
764  const double writeTime,
765  const double* psi);
766 };
767 
768 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
769 
770 template<class classType>
771 label DASolver::validateField(const classType& field)
772 {
773  /*
774  Description:
775  Check if a field variable has invalid in it. If the values are valid, return 0
776  */
777 
778  forAll(field, idxI)
779  {
780  const scalar& val = field[idxI];
781  if (std::isnan(val) || std::isinf(val) || fabs(val) > 1e15)
782  {
783  return 1;
784  }
785  }
786  forAll(field.boundaryField(), patchI)
787  {
788  forAll(field.boundaryField()[patchI], faceI)
789  {
790  const scalar& val = field.boundaryField()[patchI][faceI];
791  if (std::isnan(val) || std::isinf(val) || fabs(val) > 1e15)
792  {
793  return 1;
794  }
795  }
796  }
797 
798  return 0;
799 }
800 
801 template<class classType>
802 label DASolver::validateVectorField(const classType& field)
803 {
804  /*
805  Description:
806  Check if a field variable has invalid in it. If the values are valid, return 0
807  */
808 
809  forAll(field, idxI)
810  {
811  for (label compI = 0; compI < 3; compI++)
812  {
813  const scalar& val = field[idxI][compI];
814  if (std::isnan(val) || std::isinf(val) || fabs(val) > 1e15)
815  {
816  return 1;
817  }
818  }
819  }
820  forAll(field.boundaryField(), patchI)
821  {
822  forAll(field.boundaryField()[patchI], faceI)
823  {
824  for (label compI = 0; compI < 3; compI++)
825  {
826  const scalar& val = field.boundaryField()[patchI][faceI][compI];
827  if (std::isnan(val) || std::isinf(val) || fabs(val) > 1e15)
828  {
829  return 1;
830  }
831  }
832  }
833  }
834 
835  return 0;
836 }
837 
838 } // End namespace Foam
839 
840 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
841 
842 #endif
843 
844 // ************************************************************************* //
Foam::DASolver::writeSensMapField
void writeSensMapField(const word name, const double *dFdField, const word fieldType, const double timeName)
write the sensitivity map for the entire field
Definition: DASolver.C:3557
Foam::DAUtility::readMatrixBinary
static void readMatrixBinary(Mat matIn, const word prefix)
read petsc matrix in binary format
Definition: DAUtility.C:378
Foam::DASolver::argsAll_
char * argsAll_
all the arguments
Definition: DASolver.H:66
Foam::DASolver::setTime
void setTime(scalar time, label timeIndex)
setTime for OF fields
Definition: DASolver.H:224
DAOutput.H
Foam::DASolver::writeFailedMesh
void writeFailedMesh()
write the failed mesh to disk
Definition: DASolver.C:3172
Foam::DASolver::getMesh
const fvMesh & getMesh()
Definition: DASolver.H:575
Foam::DASolver::writeMatrixBinary
void writeMatrixBinary(const Mat matIn, const word prefix)
write the matrix in binary format
Definition: DASolver.H:435
Foam::DASolver::calcCouplingFaceCoords
void calcCouplingFaceCoords(const scalar *volCoords, scalar *surfCoords)
return the face coordinates based on vol coords
Definition: DASolver.C:1592
Foam::DASolver::checkPrimalFailure
label checkPrimalFailure()
check whether the primal fails based on residual and regression fail flag
Definition: DASolver.C:2472
DAOption.H
Foam::DASolver::getInputDistributed
label getInputDistributed(const word inputName, const word inputType)
get whether the input is distributed among processors
Definition: DASolver.C:1411
Foam::DASolver::updateStateBoundaryConditions
void updateStateBoundaryConditions()
update the boundary conditions for all states and intermediate variables
Definition: DASolver.C:2608
Foam::DAUtility::pyCalcBetaJacVecProdInterface
static pyJacVecProdInterface pyCalcBetaJacVecProdInterface
Definition: DAUtility.H:121
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:147
Foam::DASolver::printToScreen_
label printToScreen_
whether to print primal information to the screen
Definition: DASolver.H:150
Foam::DASolver::calcdRdWOldTPsiAD
void calcdRdWOldTPsiAD(const label oldTimeLevel, const double *psi, double *dRdWOldTPsi)
compute dRdWOld^T*Psi
Definition: DASolver.C:1661
Foam::DASolver::daCheckMeshPtr_
autoPtr< DACheckMesh > daCheckMeshPtr_
DACheckMesh object pointer.
Definition: DASolver.H:99
Foam::DASolver::getDAModel
const DAModel & getDAModel()
get DAModel object
Definition: DASolver.H:605
Foam::DASolver::stateInfo_
HashTable< wordList > stateInfo_
the stateInfo_ list from DAStateInfo object
Definition: DASolver.H:120
Foam::DASolver::initDynamicMesh
void initDynamicMesh()
resetting internal info in fvMesh, which is needed for multiple primal runs
Definition: DASolver.C:3761
Foam::DAUtility::readVectorBinary
static void readVectorBinary(Vec vecIn, const word prefix)
read petsc vector in binary format
Definition: DAUtility.C:282
Foam::DAUtility::pyCalcBeta
static void * pyCalcBeta
define a function pointer template for Python call back
Definition: DAUtility.H:117
Foam::DASolver::getDALinearEqn
const DALinearEqn & getDALinearEqn()
get DALinearEqn object
Definition: DASolver.H:623
Foam::DASolver::daRegressionPtr_
autoPtr< DARegression > daRegressionPtr_
DARegression pointer.
Definition: DASolver.H:111
Foam::DASolver::daModelPtr_
autoPtr< DAModel > daModelPtr_
DAModel pointer.
Definition: DASolver.H:84
Foam::DASolver::runColoring
void runColoring()
run the coloring solver
Definition: DASolver.C:554
Foam::DAUtility::pyCalcBetaInterface
static pyComputeInterface pyCalcBetaInterface
Definition: DAUtility.H:118
Foam::DASolver::initInputFieldUnsteady
void initInputFieldUnsteady()
initialize inputFieldUnsteady from the GlobalVar class
Definition: DASolver.C:3885
Foam::DACheckMesh
Definition: DACheckMesh.H:29
Foam::DASolver::getNLocalPoints
label getNLocalPoints() const
return the number of local points
Definition: DASolver.H:501
Foam::DASolver::getInputSize
label getInputSize(const word inputName, const word inputType)
get the array size of an input type
Definition: DASolver.C:1348
Foam::DASolver::registerStateVariableInput4AD
void registerStateVariableInput4AD(const label oldTimeLevel=0)
register all state variables as the input for reverse-mode AD
Definition: DASolver.C:1722
Foam::DASolver::hasVolCoordInput
label hasVolCoordInput()
whether the volCoord input is defined
Definition: DASolver.C:3744
Foam::DASolver::daIndexPtr_
autoPtr< DAIndex > daIndexPtr_
DAIndex pointer.
Definition: DASolver.H:87
Foam::DASolver::primalFinalTimeIndex_
label primalFinalTimeIndex_
the final time index from the primal solve. for steady state cases it can converge before endTime
Definition: DASolver.H:168
Foam::DASolver::calcdRdWT
void calcdRdWT(const label isPC, Mat dRdWT)
compute dRdWT
Definition: DASolver.C:782
Foam::DASolver::reduceStateResConLevel
void reduceStateResConLevel(const dictionary &maxResConLv4JacPCMat, HashTable< List< List< word >>> &stateResConInfo) const
reduce the connectivity level for Jacobian connectivity mat
Definition: DASolver.C:422
Foam::DASolver::getDACheckMesh
const DACheckMesh & getDACheckMesh()
get DACheckMesh object
Definition: DASolver.H:629
Foam::DASolver::daResidualPtr_
autoPtr< DAResidual > daResidualPtr_
DAResidual pointer.
Definition: DASolver.H:105
DAIndex.H
IOMRFZoneListDF.H
DALinearEqn.H
Foam::DASolver::normalizeGradientVec
void normalizeGradientVec(double *vecY)
normalize the reverse-mode AD derivatives stored in vecY
Definition: DASolver.C:2107
Foam::DASolver::initializedRdWTMatrixFree
void initializedRdWTMatrixFree()
initialize matrix free dRdWT
Definition: DASolver.C:1076
Foam::DASolver::destroydRdWTMatrixFree
void destroydRdWTMatrixFree()
destroy the matrix free dRdWT
Definition: DASolver.C:1108
Foam::DASolver::getTimeOpFuncVal
double getTimeOpFuncVal(const word functionName)
get the function value based on timeOp
Definition: DASolver.C:261
Foam::DASolver::primalMinResTol_
scalar primalMinResTol_
primal residual tolerance
Definition: DASolver.H:147
Foam::DASolver
Definition: DASolver.H:54
Foam::pySetCharInterface
void(* pySetCharInterface)(const char *, void *)
Definition: DAUtility.H:29
Foam::DAOption
Definition: DAOption.H:29
Foam::DASolver::updateOFMesh
void updateOFMesh(const scalar *volCoords)
Update the OpenFoam mesh point coordinates based on the volume point coords array.
Definition: DASolver.C:1057
DACheckMesh.H
DAFunction.H
Foam::DASolver::getNLocalCells
label getNLocalCells() const
return the number of local cells
Definition: DASolver.H:495
Foam::DASolver::initTensorFlowFuncs
void initTensorFlowFuncs(pyComputeInterface computeInterface, void *compute, pyJacVecProdInterface jacVecProdInterface, void *jacVecProd, pySetCharInterface setModelNameInterface, void *setModelName)
initialize tensorflow functions and interfaces for callback
Definition: DASolver.H:679
DAUtility.H
Foam::DASolver::calcOutput
void calcOutput(const word outputName, const word outputType, double *output)
get whether the output is distributed among processors
Definition: DASolver.C:1383
Foam::DASolver::daFieldPtr_
autoPtr< DAField > daFieldPtr_
DAField pointer.
Definition: DASolver.H:90
Foam::DASolver::initializeGlobalADTape4dRdWT
void initializeGlobalADTape4dRdWT()
initialize the CoDiPack reverse-mode AD global tape for computing dRdWT*psi
Definition: DASolver.C:1166
Foam::DASolver::solvePrimal
virtual label solvePrimal()=0
solve the primal equations
Foam::DASolver::getDdtSchemeOrder
label getDdtSchemeOrder()
get the ddtScheme order
Definition: DASolver.H:230
Foam::DASolver::assignVec2ResidualGradient
void assignVec2ResidualGradient(const double *vecX)
assign the reverse-mode AD input seeds from vecX to the residuals in OpenFOAM
Definition: DASolver.C:2208
Foam::DAUtility::pyCalcBetaJacVecProd
static void * pyCalcBetaJacVecProd
Definition: DAUtility.H:120
Foam::DAUtility::pySetModelNameInterface
static pySetCharInterface pySetModelNameInterface
Definition: DAUtility.H:124
Foam::DASolver::getNLocalAdjointBoundaryStates
label getNLocalAdjointBoundaryStates() const
return the number of local adjoint boundary states
Definition: DASolver.H:489
Foam::DASolver::getDAField
const DAField & getDAField()
get DAField object
Definition: DASolver.H:617
Foam::DASolver::daOptionPtr_
autoPtr< DAOption > daOptionPtr_
DAOption pointer.
Definition: DASolver.H:81
Foam::DASolver::pyOptions_
PyObject * pyOptions_
all options in DAFoam
Definition: DASolver.H:69
DAGlobalVar.H
Foam::DASolver::registerResidualOutput4AD
void registerResidualOutput4AD()
register all residuals as the output for reverse-mode AD
Definition: DASolver.C:2035
Foam::DASolver::calcJacTVecProduct
void calcJacTVecProduct(const word inputName, const word inputType, const double *input, const word outputName, const word outputType, const double *seed, double *product)
calculate the Jacobian-matrix-transposed and vector product for product = [dOutput/dInput]^T * seed
Definition: DASolver.C:1445
Foam::DASolver::getOFMeshPoints
void getOFMeshPoints(double *points)
get the flatten mesh points coordinates
Definition: DASolver.C:991
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:591
DAColoring.H
Foam::DASolver::getRunTime
const Time & getRunTime()
return the runTime object
Definition: DASolver.H:581
DAJacCon.H
Foam::DASolver::updateKSPPCMat
void updateKSPPCMat(Mat PCMat, KSP ksp)
Update the preconditioner matrix for the ksp object.
Definition: DASolver.C:925
Foam::DASolver::solveLinearEqn
label solveLinearEqn(const KSP ksp, const Vec rhsVec, Vec solVec)
solve the linear equation given a ksp and right-hand-side vector
Definition: DASolver.C:955
DAResidual.H
Foam::DASolver::resetStateVals
void resetStateVals()
reset the states to its initial values this usually happens when we have nan in states
Definition: DASolver.C:3310
Foam::DASolver::readVectorBinary
void readVectorBinary(Vec vecIn, const word prefix)
read petsc vector in binary format
Definition: DASolver.H:467
Foam::DASolver::readMeshPoints
void readMeshPoints(const scalar timeVal)
read the mesh points from the disk and run movePoints to deform the mesh
Definition: DASolver.C:2875
Foam::DASolver::getDAStateInfo
const DAStateInfo & getDAStateInfo()
get DAStateInfo object
Definition: DASolver.H:593
Foam::DASolver::setDAFunctionList
void setDAFunctionList()
initialize DASolver::daFunctionPtrList_ one needs to call this before calling printAllFunctions
Definition: DASolver.C:330
Foam::DASolver::globalADTape4dRdWTInitialized
label globalADTape4dRdWTInitialized
a flag in dRdWTMatVecMultFunction to determine if the global tap is initialized
Definition: DASolver.H:144
Foam::DASolver::getInitStateVals
void getInitStateVals(HashTable< scalar > &initState)
calculate the initial value for validate states
Definition: DASolver.C:3240
Foam::DASolver::daFunctionPtrList_
UPtrList< DAFunction > daFunctionPtrList_
a list of DAFunction pointers
Definition: DASolver.H:93
Foam::DASolver::printIntervalUnsteady_
label printIntervalUnsteady_
how frequent do you want to print the primal info default is every 100 steps
Definition: DASolver.H:162
Foam::DASolver::writeMeshPoints
void writeMeshPoints(const double *points, const scalar timeVal)
write the mesh points to the disk for the given timeVal
Definition: DASolver.C:2898
DAModel.H
Foam::DASolver::normalizeJacTVecProduct
void normalizeJacTVecProduct(const word inputName, double *product)
normalize the jacobian vector product that has states as the input such as dFdW and dRdW
Definition: DASolver.C:1198
Foam::DASolver::daTimeOpPtrList_
UPtrList< DATimeOp > daTimeOpPtrList_
a list of DATimeOp pointers
Definition: DASolver.H:96
Foam::DASolver::isPrintTime
label isPrintTime(const Time &runTime, const label printInterval) const
Definition: DASolver.C:2508
Foam::DASolver::getDAResidual
const DAResidual & getDAResidual()
get DAResidual object
Definition: DASolver.H:611
Foam::DAUtility::pySetModelName
static void * pySetModelName
Definition: DAUtility.H:123
Foam::DASolver::daLinearEqnPtr_
autoPtr< DALinearEqn > daLinearEqnPtr_
DALinearEqn pointer.
Definition: DASolver.H:102
Foam::DASolver::getDAIndex
const DAIndex & getDAIndex()
get DAIndex object
Definition: DASolver.H:599
Foam::DASolver::updateOFFields
void updateOFFields(const scalar *states)
Update the OpenFOAM field values (including both internal and boundary fields) based on the state arr...
Definition: DASolver.C:1044
Foam::DAUtility::writeVectorBinary
static void writeVectorBinary(const Vec vecIn, const word prefix)
write petsc vector in binary format
Definition: DAUtility.C:315
Foam::DAFunction::getFunctionName
word getFunctionName()
return the name of objective function
Definition: DAFunction.H:142
Foam::DASolver::writeSensMapSurface
void writeSensMapSurface(const word name, const double *dFdXs, const double *Xs, const label size, const double timeName)
write the sensitivity map for all wall surfaces
Definition: DASolver.C:3435
Foam::DASolver::updateBoundaryConditions
void updateBoundaryConditions(const word fieldName, const word fieldType)
update the boundary condition for a field
Definition: DASolver.C:2559
Foam::DASolver::validateVectorField
label validateVectorField(const classType &field)
check if a field variable has nan
Definition: DASolver.H:802
Foam::DASolver::initStateVals_
HashTable< scalar > initStateVals_
initial values for validateStates
Definition: DASolver.H:171
Foam::DASolver::writeVectorASCII
void writeVectorASCII(const Vec vecIn, const word prefix)
write petsc vector in ascii format
Definition: DASolver.H:459
Foam::DAIndex
Definition: DAIndex.H:32
Foam::DASolver::getPrevPrimalSolTime
scalar getPrevPrimalSolTime()
get the solution time folder for previous primal solution
Definition: DASolver.H:557
Foam::DASolver::validateStates
label validateStates()
check if the state variables have valid values
Definition: DASolver.C:3382
DAInput.H
Foam::DASolver::setRegressionParameter
void setRegressionParameter(word modelName, const label idxI, scalar val)
set the regression parameter
Definition: DASolver.H:647
Foam::DASolver::deactivateStateVariableInput4AD
void deactivateStateVariableInput4AD(const label oldTimeLevel=0)
deactivate all state variables as the input for reverse-mode AD
Definition: DASolver.C:1879
Foam::DASolver::getOutputDistributed
label getOutputDistributed(const word outputName, const word outputType)
get whether the output is distributed among processors
Definition: DASolver.C:1427
Foam::DASolver::writeMatrixASCII
void writeMatrixASCII(const Mat matIn, const word prefix)
write the matrix in ASCII format
Definition: DASolver.H:443
Foam::DAField
Definition: DAField.H:36
Foam::DASolver::primalMinIters_
label primalMinIters_
primal min number of iterations
Definition: DASolver.H:159
Foam::DAModel
Definition: DAModel.H:57
Foam::DAFunction
Definition: DAFunction.H:31
Foam::DASolver::regressionModelCompute
void regressionModelCompute()
call the compute method of the regression model
Definition: DASolver.H:653
Foam::DASolver::calcResiduals
void calcResiduals(label isPC=0)
calculate the residuals
Definition: DASolver.C:2592
Foam::DASolver::readStateVars
void readStateVars(scalar timeVal, label oldTimeLevel=0)
read the state variables from the disk and assign the value to the prescribe time level
Definition: DASolver.C:2938
Foam::DASolver::prevPrimalSolTime_
scalar prevPrimalSolTime_
the solution time for the previous primal solution
Definition: DASolver.H:123
Foam::DASolver::validateField
label validateField(const classType &field)
check if a field variable has nan
Definition: DASolver.H:771
Foam
Definition: checkGeometry.C:32
Foam::DASolver::getDAOption
const DAOption & getDAOption()
get DAOption object
Definition: DASolver.H:587
Foam::DASolver::setSolverInput
void setSolverInput(const word inputName, const word inputType, const int inputSize, const double *input, const double *seed)
Definition: DASolver.C:1310
Foam::DASolver::writeAssociatedFields
void writeAssociatedFields()
write associated fields such as relative velocity
Definition: DASolver.C:2526
Foam::DASolver::getdFScaling
scalar getdFScaling(const word functionName, const label timeIdx)
get the scaling factor for dF/d? derivative computation
Definition: DASolver.C:302
Foam::DASolver::getOutputSize
label getOutputSize(const word outputName, const word outputType)
get the array size of an output type
Definition: DASolver.C:1364
Foam::DASolver::dRdWTMatVecMultFunction
static PetscErrorCode dRdWTMatVecMultFunction(Mat dRdWT, Vec vecX, Vec vecY)
matrix free matrix-vector product function to compute vecY=dRdWT*vecX
Definition: DASolver.C:1119
Foam::DASolver::getLatestTime
scalar getLatestTime()
get the latest time solution from the case folder.
Definition: DASolver.H:754
Foam::DASolver::~DASolver
virtual ~DASolver()
Definition: DASolver.H:201
Foam::pyJacVecProdInterface
void(* pyJacVecProdInterface)(const double *, double *, int, const double *, const double *, int, void *)
Definition: DAUtility.H:28
Foam::DASolver::assignStateGradient2Vec
void assignStateGradient2Vec(double *vecY, const label oldTimeLevel=0)
set the reverse-mode AD derivatives from the state variables in OpenFOAM to vecY
Definition: DASolver.C:2295
Foam::DASolver::regModelFail_
label regModelFail_
whether the regModel compute fails
Definition: DASolver.H:153
Foam::DASolver::getElapsedClockTime
scalar getElapsedClockTime()
return the elapsed clock time for testing speed
Definition: DASolver.H:703
Foam::DASolver::meanStatesToStates
void meanStatesToStates()
assign the mean states values to states
Definition: DASolver.C:3805
Foam::DASolver::getElapsedCpuTime
scalar getElapsedCpuTime()
return the elapsed CPU time for testing speed
Definition: DASolver.H:709
forAll
forAll(nuTilda1, cellI)
Definition: nuTilda1EqnIrkPimple.H:19
Foam::DAResidual
Definition: DAResidual.H:36
Foam::DASolver::getGlobalXvIndex
label getGlobalXvIndex(const label idxPoint, const label idxCoord) const
basically, we call DAIndex::getGlobalXvIndex
Definition: DASolver.H:427
DAPartDeriv.H
Foam::DASolver::calcPCMatWithFvMatrix
void calcPCMatWithFvMatrix(Mat PCMat, const label turbOnly=0)
calculate the PC mat using fvMatrix
Definition: DASolver.C:2633
Foam::DASolver::runTimePtr_
autoPtr< Time > runTimePtr_
runTime pointer
Definition: DASolver.H:75
Foam::DAUtility::writeMatrixBinary
static void writeMatrixBinary(const Mat matIn, const word prefix)
write petsc matrix in binary format
Definition: DAUtility.C:411
Foam::DASolver::getCouplingPatchList
void getCouplingPatchList(wordList &patchList, word groupName="NONE")
return the coupling patch list if any scenario is active on couplingInfo dict otherwise return design...
Foam::DASolver::getRegressionParameter
scalar getRegressionParameter(word modelName, const label idxI)
get the regression parameter
Definition: DASolver.H:641
Foam::DASolver::writeVectorBinary
void writeVectorBinary(const Vec vecIn, const word prefix)
write petsc vector in binary format
Definition: DASolver.H:475
Foam::DASolver::getNLocalAdjointStates
label getNLocalAdjointStates() const
return the number of local adjoint states
Definition: DASolver.H:483
Foam::DASolver::printInterval_
label printInterval_
how frequent do you want to print the primal info default is every 100 steps
Definition: DASolver.H:156
Foam::DASolver::initSolver
virtual void initSolver()=0
initialize fields and variables
Foam::DASolver::meshPtr_
autoPtr< fvMesh > meshPtr_
fvMesh pointer
Definition: DASolver.H:78
DARegression.H
Foam::DASolver::getNRegressionParameters
label getNRegressionParameters(word modelName)
get the number of regression model parameters
Definition: DASolver.H:635
DAStateInfo.H
Foam::DAUtility::writeVectorASCII
static void writeVectorASCII(const Vec vecIn, const word prefix)
write petsc vector in ascii format
Definition: DAUtility.C:347
Foam::DASolver::declareRunTimeSelectionTable
declareRunTimeSelectionTable(autoPtr, DASolver, dictionary,(char *argsAll, PyObject *pyOptions),(argsAll, pyOptions))
Foam::DASolver::argsPtr_
autoPtr< argList > argsPtr_
args pointer
Definition: DASolver.H:72
Foam::DASolver::printAllOptions
void printAllOptions()
print all DAOption
Definition: DASolver.H:539
Foam::DAUtility::writeMatrixASCII
static void writeMatrixASCII(const Mat matIn, const word prefix)
write petsc matrix in ascii format
Definition: DAUtility.C:443
Foam::DASolver::getOFFields
void getOFFields(scalar *states)
assign the state variables from OpenFoam layer to the states array
Definition: DASolver.H:289
Foam::DASolver::checkMesh
label checkMesh() const
check the mesh quality and return meshOK
Definition: DASolver.H:530
Foam::DASolver::solveAdjointFP
virtual label solveAdjointFP(Vec dFdW, Vec psi)
solve the adjoint equation using the fixed-point iteration method
Definition: DASolver.C:3224
Foam::DASolver::updateDAOption
void updateDAOption(PyObject *pyOptions)
update the allOptions_ dict in DAOption based on the pyOptions from pyDAFoam
Definition: DASolver.H:551
Foam::DALinearEqn
Definition: DALinearEqn.H:31
Foam::pyComputeInterface
void(* pyComputeInterface)(const double *, int, double *, int, void *)
Definition: DAUtility.H:27
DATimeOp.H
Foam::DASolver::writeAdjointFields
void writeAdjointFields(const word function, const double writeTime, const double *psi)
write the adjoint variables for all states
Definition: DASolver.C:3650
Foam::DASolver::updateInputFieldUnsteady
void updateInputFieldUnsteady()
assign the inputFieldUnsteady values to the OF field vars
Definition: DASolver.C:3924
Foam::DAStateInfo
Definition: DAStateInfo.H:30
Foam::DASolver::createMLRKSPMatrixFree
void createMLRKSPMatrixFree(const Mat jacPCMat, KSP ksp)
create a multi-level, Richardson KSP object with matrix-free Jacobians
Definition: DASolver.C:936
Foam::DASolver::setPrimalBoundaryConditions
void setPrimalBoundaryConditions(const label printInfo=1)
update the primal state boundary condition based on the primalBC dict
Definition: DASolver.C:3187
Foam::DASolver::daStateInfoPtr_
autoPtr< DAStateInfo > daStateInfoPtr_
DAStateInfo pointer.
Definition: DASolver.H:108
runTime
Time & runTime
Definition: createRefsHeatTransfer.H:1
Foam::DASolver::runFPAdj
virtual label runFPAdj(Vec dFdW, Vec psi)
solve the adjoint equation using the fixed-point iteration method
Definition: DASolver.C:3208
Foam::DASolver::getOFField
void getOFField(const word fieldName, const word fieldType, double *field)
get a field variable from OF layer
Definition: DASolver.C:1006
Foam::DASolver::readMatrixBinary
void readMatrixBinary(Mat matIn, const word prefix)
read petsc matrix in binary format
Definition: DASolver.H:451
DAField.H
Foam::DASolver::points0Ptr_
autoPtr< pointField > points0Ptr_
the initial points for dynamicMesh without volCoord inputs
Definition: DASolver.H:117
Foam::DASolver::writeAdjStates
void writeAdjStates(const label writeMesh, const wordList &additionalOutput)
write state variables that are NO_WRITE to disk
Definition: DASolver.C:2778
Foam::DASolver::printElapsedTime
void printElapsedTime(const Time &runTime, const label printToScreen)
Definition: DASolver.H:256
psi
const volScalarField & psi
Definition: createRefsRhoPimple.H:14
Foam::DASolver::dRdWTMF_
Mat dRdWTMF_
matrix-free dRdWT matrix used in GMRES solution
Definition: DASolver.H:141
Foam::DASolver::functionTimeSteps_
List< scalarList > functionTimeSteps_
a list list that saves the function value for all time steps
Definition: DASolver.H:165
Foam::DASolver::TypeName
TypeName("DASolver")
Runtime type information.
dafoam_plot3dtransform.mode
mode
Definition: dafoam_plot3dtransform.py:21
Foam::DASolver::getFunctionListIndex
label getFunctionListIndex(const word functionName)
return the index of a give functionName in daFunctionPtrList_
Definition: DASolver.H:516
Foam::DASolver::calcAllFunctions
void calcAllFunctions(label print=0)
calculate the values of all objective functions and print them to screen
Definition: DASolver.C:204
Foam::DASolver::New
static autoPtr< DASolver > New(char *argsAll, PyObject *pyOptions)
Definition: DASolver.C:104
Foam::DASolver::daGlobalVarPtr_
autoPtr< DAGlobalVar > daGlobalVarPtr_
DAGlobalVar pointer.
Definition: DASolver.H:114