adjointSolver.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2007-2019 PCOpt/NTUA
9  Copyright (C) 2013-2019 FOSS GP
10  Copyright (C) 2019-2021 OpenCFD Ltd.
11 -------------------------------------------------------------------------------
12 License
13  This file is part of OpenFOAM.
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 "adjointSolver.H"
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36  defineTypeNameAndDebug(adjointSolver, 0);
37  defineRunTimeSelectionTable(adjointSolver, adjointSolver);
38 }
39 
40 
41 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
42 
43 Foam::adjointSolver::adjointSolver
44 (
45  fvMesh& mesh,
46  const word& managerType,
47  const dictionary& dict,
48  const word& primalSolverName
49 )
50 :
51  solver(mesh, managerType, dict),
52  primalSolverName_(primalSolverName),
53  objectiveManagerPtr_
54  (
56  (
57  mesh,
58  dict.subDict("objectives"),
59  solverName_,
60  primalSolverName
61  )
62  ),
63  sensitivities_(nullptr),
64  computeSensitivities_
65  (
66  dict.getOrDefault<bool>("computeSensitivities", true)
67  ),
68  isConstraint_(dict.getOrDefault<bool>("isConstraint", false))
69 {
70  // Update objective-related quantities to get correct derivatives
71  // in case of continuation
72  objectiveManagerPtr_().update();
73 }
74 
75 
76 // * * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * //
77 
79 (
80  fvMesh& mesh,
81  const word& managerType,
82  const dictionary& dict,
83  const word& primalSolverName
84 )
85 {
86  const word solverType(dict.get<word>("type"));
87 
88  auto* ctorPtr = adjointSolverConstructorTable(solverType);
89 
90  if (!ctorPtr)
91  {
93  (
94  dict,
95  "adjointSolver",
96  solverType,
97  *adjointSolverConstructorTablePtr_
98  ) << exit(FatalIOError);
99  }
100 
102  (
103  ctorPtr(mesh, managerType, dict, primalSolverName)
104  );
105 }
106 
107 
108 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
109 
111 {
113 }
114 
115 
117 {
118  return
119  const_cast<primalSolver&>
120  (
121  mesh_.lookupObject<primalSolver>(primalSolverName_)
122  );
123 }
124 
125 
127 {
128  if (solver::readDict(dict))
129  {
130  computeSensitivities_ =
131  dict.getOrDefault<bool>("computeSensitivities", true);
132 
133  objectiveManagerPtr_->readDict(dict.subDict("objectives"));
134 
135  return true;
136  }
137 
138  return false;
139 }
140 
141 
143 {
144  return objectiveManagerPtr_();
145 }
146 
147 
149 {
150  return objectiveManagerPtr_();
151 }
152 
153 
155 {
156  return isConstraint_;
157 }
158 
159 
161 {
162  sensitivities_.clear();
163 }
164 
165 
167 {
168  // Does nothing in base
169 }
170 
171 
172 // ************************************************************************* //
Foam::objectiveManager
class for managing incompressible objective functions.
Definition: objectiveManager.H:54
Foam::adjointSolver::primalSolverName_
const word primalSolverName_
Name of primal solver.
Definition: adjointSolver.H:77
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::defineRunTimeSelectionTable
defineRunTimeSelectionTable(reactionRateFlameArea, dictionary)
Foam::adjointSolver::isConstraint
virtual bool isConstraint()
Is the solving referring to a constraint.
Definition: adjointSolver.C:154
Foam::solver::mesh_
fvMesh & mesh_
Reference to the mesh database.
Definition: solver.H:60
Foam::adjointSolver::updatePrimalBasedQuantities
virtual void updatePrimalBasedQuantities()
Definition: adjointSolver.C:166
Foam::FatalIOError
IOerror FatalIOError
Foam::adjointSolver::readDict
virtual bool readDict(const dictionary &dict)
Definition: adjointSolver.C:126
FatalIOErrorInLookup
#define FatalIOErrorInLookup(ios, lookupTag, lookupName, lookupTable)
Report an error message using Foam::FatalIOError.
Definition: error.H:478
adjointSolver.H
Foam::solver
Base class for solution control classes.
Definition: solver.H:51
Foam::dictionary::subDict
const dictionary & subDict(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Find and return a sub-dictionary.
Definition: dictionary.C:460
Foam::objectRegistry::lookupObject
const Type & lookupObject(const word &name, const bool recursive=false) const
Definition: objectRegistryTemplates.C:434
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
Foam::adjointSolver::clearSensitivities
virtual void clearSensitivities()
Clears the sensitivity field known by the adjoint solver.
Definition: adjointSolver.C:160
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
Foam::fvMesh
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:85
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::New
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tdf1, const word &name, const dimensionSet &dimensions)
Global function forwards to reuseTmpDimensionedField::New.
Definition: DimensionedFieldReuseFunctions.H:105
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
Foam::adjointSolver::getObjectiveManager
const objectiveManager & getObjectiveManager() const
Return a const reference to the objective manager.
Definition: adjointSolver.C:142
Foam::primalSolver
Base class for primal solvers.
Definition: primalSolver.H:52
Foam::solver::readDict
virtual bool readDict(const dictionary &dict)
Definition: solver.C:82
Foam::dictionary::getOrDefault
T getOrDefault(const word &keyword, const T &deflt, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:148
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)
Foam::adjointSolver::getPrimalSolver
const primalSolver & getPrimalSolver() const
Definition: adjointSolver.C:110
Foam::adjointSolver::New
static autoPtr< adjointSolver > New(fvMesh &mesh, const word &managerType, const dictionary &dict, const word &primalSolverName)
Return a reference to the selected turbulence model.
Definition: adjointSolver.C:79