optimisationManager.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 "optimisationManager.H"
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36  defineTypeNameAndDebug(optimisationManager, 0);
37  defineRunTimeSelectionTable(optimisationManager, dictionary);
38 }
39 
40 
41 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
42 
43 Foam::optimisationManager::optimisationManager(fvMesh& mesh)
44 :
46  (
47  IOobject
48  (
49  "optimisationDict",
50  mesh.time().system(),
51  mesh,
52  IOobject::MUST_READ_IF_MODIFIED,
53  IOobject::NO_WRITE,
54  true
55  )
56  ),
57  mesh_(mesh),
58  time_(const_cast<Time&>(mesh.time())),
59  primalSolvers_(),
60  adjointSolverManagers_(),
61  managerType_(get<word>("optimisationManager")),
62  optType_(nullptr)
63 {
64  const dictionary& primalSolversDict = subDict("primalSolvers");
65  const wordList& primalSolverNames = primalSolversDict.toc();
66 
67  // Construct primal solvers
68  primalSolvers_.setSize(primalSolverNames.size());
69  forAll(primalSolvers_, solveri)
70  {
71  primalSolvers_.set
72  (
73  solveri,
75  (
76  mesh,
78  primalSolversDict.subDict(primalSolverNames[solveri])
79  )
80  );
81  }
82 
83  // Construct adjointSolverManagers
84  const dictionary& adjointManagersDict = subDict("adjointManagers");
85  const wordList& adjointManagerNames = adjointManagersDict.toc();
86  adjointSolverManagers_.setSize(adjointManagerNames.size());
87 
88  label nAdjointSolvers(0);
90  {
92  (
93  manageri,
95  (
96  mesh,
98  adjointManagersDict.subDict(adjointManagerNames[manageri])
99  )
100  );
101  nAdjointSolvers += adjointSolverManagers_[manageri].nAdjointSolvers();
102  }
103 
104  // Sanity checks on the naming convention
105  if (primalSolvers_.size() > 1)
106  {
107  for (const primalSolver& solveri : primalSolvers_)
108  {
109  if (!solveri.useSolverNameForFields())
110  {
112  << "Multiple primal solvers are present but "
113  << "useSolverNameForFields is set to false in "
114  << "primal solver " << solveri.solverName() << nl
115  << "This is considered fatal."
116  << exit(FatalError);
117  }
118  }
119  }
120 
121  if (nAdjointSolvers > 1)
122  {
124  {
125  const PtrList<adjointSolver>& adjointSolvers = amI.adjointSolvers();
126  for (const adjointSolver& asI : adjointSolvers)
127  {
128  if (!asI.useSolverNameForFields())
129  {
131  << "Multiple adjoint solvers are present but "
132  << "useSolverNameForFields is set to false in "
133  << "adjoint solver " << asI.solverName() << nl
134  << "This is considered fatal."
135  << exit(FatalError);
136  }
137  }
138  }
139  }
140 }
141 
142 
143 // * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * //
144 
146 (
147  fvMesh& mesh
148 )
149 {
150  const IOdictionary dict
151  (
152  IOobject
153  (
154  "optimisationDict",
155  mesh.time().system(),
156  mesh,
159  false // Do not register
160  )
161  );
162 
163  const word modelType(dict.get<word>("optimisationManager"));
164 
165  Info<< "optimisationManager type : " << modelType << endl;
166 
167  auto* ctorPtr = dictionaryConstructorTable(modelType);
168 
169  if (!ctorPtr)
170  {
172  (
173  dict,
174  "optimisationManager",
175  modelType,
176  *dictionaryConstructorTablePtr_
177  ) << exit(FatalIOError);
178  }
179 
180  return autoPtr<optimisationManager>(ctorPtr(mesh));
181 }
182 
183 
184 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * //
185 
187 {
188  if (regIOobject::read())
189  {
190  // Note: Only changing existing solvers - not adding any new
191  const dictionary& primalSolversDict = subDict("primalSolvers");
192  for (primalSolver& sol : primalSolvers_)
193  {
194  sol.readDict(primalSolversDict.subDict(sol.solverName()));
195  }
196 
197  const dictionary& adjointManagersDict = subDict("adjointManagers");
198  for (adjointSolverManager& man : adjointSolverManagers_)
199  {
200  man.readDict(adjointManagersDict.subDict(man.managerName()));
201  }
202 
203  return true;
204  }
205 
206  return false;
207 }
208 
209 
211 {
212  return primalSolvers_;
213 }
214 
215 
218 {
219  return adjointSolverManagers_;
220 }
221 
222 
224 {
225  // Solve all primal equations
226  forAll(primalSolvers_, psI)
227  {
228  primalSolvers_[psI].solve();
229  }
230 }
231 
232 
234 {
235  // Solve all adjoint solver equations
236  forAll(adjointSolverManagers_, amI)
237  {
238  adjointSolverManagers_[amI].solveAdjointEquations();
239  }
240 }
241 
242 
244 {
245  // Compute senstivities from all adjoint solvers
246  forAll(adjointSolverManagers_, amI)
247  {
248  adjointSolverManagers_[amI].computeAllSensitivities();
249  }
250 }
251 
252 
254 {
255  forAll(adjointSolverManagers_, amI)
256  {
257  PtrList<adjointSolver>& adjointSolvers =
258  adjointSolverManagers_[amI].adjointSolvers();
259 
260  forAll(adjointSolvers, asI)
261  {
262  adjointSolvers[asI].updatePrimalBasedQuantities();
263  }
264  }
265 }
266 
267 
268 // ************************************************************************* //
Foam::adjointSolver
Base class for adjoint solvers.
Definition: adjointSolver.H:57
Foam::IOobject::NO_WRITE
Definition: IOobject.H:195
Foam::IOdictionary
IOdictionary is derived from dictionary and IOobject to give the dictionary automatic IO functionalit...
Definition: IOdictionary.H:54
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:169
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:73
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::optimisationManager::adjointSolverManagers_
PtrList< adjointSolverManager > adjointSolverManagers_
Definition: optimisationManager.H:68
Foam::defineRunTimeSelectionTable
defineRunTimeSelectionTable(reactionRateFlameArea, dictionary)
optimisationManager.H
Foam::optimisationManager::updatePrimalBasedQuantities
virtual void updatePrimalBasedQuantities()
Solve all primal equations.
Definition: optimisationManager.C:253
Foam::system
int system(const std::string &command, const bool bg=false)
Execute the specified command via the shell.
Definition: MSwindows.C:1150
Foam::regIOobject::read
virtual bool read()
Read object.
Definition: regIOobjectRead.C:191
Foam::primalSolver::New
static autoPtr< primalSolver > New(fvMesh &mesh, const word &managerType, const dictionary &dict)
Return a reference to the selected primal solver.
Definition: primalSolver.C:59
Foam::FatalIOError
IOerror FatalIOError
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::dictionary::get
T get(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:107
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
FatalIOErrorInLookup
#define FatalIOErrorInLookup(ios, lookupTag, lookupName, lookupTable)
Report an error message using Foam::FatalIOError.
Definition: error.H:478
Foam::optimisationManager::read
virtual bool read()
Read object.
Definition: optimisationManager.C:186
Foam::optimisationManager::primalSolvers_
PtrList< primalSolver > primalSolvers_
Definition: optimisationManager.H:67
Foam::Info
messageStream Info
Information stream (stdout output on master, null elsewhere)
Foam::optimisationManager::solvePrimalEquations
virtual void solvePrimalEquations()
Solve all primal equations.
Definition: optimisationManager.C:223
Foam::optimisationManager::primalSolvers
virtual PtrList< primalSolver > & primalSolvers()
Definition: optimisationManager.C:210
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::optimisationManager::New
static autoPtr< optimisationManager > New(fvMesh &mesh)
Return a reference to the selected turbulence model.
Definition: optimisationManager.C:146
Foam::PtrList
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition: List.H:59
Foam::adjointSolverManager
Class for managing adjoint solvers, which may be more than one per operating point.
Definition: adjointSolverManager.H:54
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::optimisationManager::solveAdjointEquations
virtual void solveAdjointEquations()
Solve all adjoint equations.
Definition: optimisationManager.C:233
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
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::optimisationManager::computeSensitivities
virtual void computeSensitivities()
Compute all adjoint sensitivities.
Definition: optimisationManager.C:243
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
Foam::TimePaths::system
const word & system() const
Return system name.
Definition: TimePathsI.H:102
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::optimisationManager::managerType_
const word managerType_
Definition: optimisationManager.H:69
Foam::List< word >
Foam::primalSolver
Base class for primal solvers.
Definition: primalSolver.H:52
Foam::IOobject::MUST_READ_IF_MODIFIED
Definition: IOobject.H:186
Foam::PtrListOps::get
List< ReturnType > get(const UPtrList< T > &list, const AccessOp &aop)
Foam::fvMesh::time
const Time & time() const
Return the top-level database.
Definition: fvMesh.H:280
Foam::dictionary::toc
wordList toc() const
Return the table of contents.
Definition: dictionary.C:602
Foam::optimisationManager::adjointSolverManagers
virtual PtrList< adjointSolverManager > & adjointSolverManagers()
Definition: optimisationManager.C:217
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)