objectiveManager.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 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 "objectiveManager.H"
31 
32 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36 
37 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
38 
39 defineTypeNameAndDebug(objectiveManager, 0);
40 defineRunTimeSelectionTable(objectiveManager, dictionary);
41 
42 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
43 
44 objectiveManager::objectiveManager
45 (
46  const fvMesh& mesh,
47  const dictionary& dict,
48  const word& adjointSolverName,
49  const word& primalSolverName
50 )
51 :
53  (
54  IOobject
55  (
56  "objectiveManager" + adjointSolverName,
57  mesh.time().system(),
58  mesh,
61  true //register object
62  )
63  ),
64  mesh_(mesh),
65  dict_(dict),
66  adjointSolverName_(adjointSolverName),
67  primalSolverName_(primalSolverName),
68  objectives_(0)
69 {
70  // Construct objectives
71  //~~~~~~~~~~~~~~~~~~~~~
72  Info << "Constructing objective functions " << nl << endl;
73  const word objectiveType = dict.get<word>("type");
74  const dictionary& objectiveNamesDict(dict.subDict("objectiveNames"));
75  wordList objectiveNames(objectiveNamesDict.toc());
76  objectives_.setSize(objectiveNames.size());
77 
78  forAll(objectiveNames, objectivei)
79  {
80  const word& objectiveName = objectiveNames[objectivei];
81 
82  objectives_.set
83  (
84  objectivei,
86  (
87  mesh_,
88  objectiveNamesDict.subDict(objectiveName),
89  objectiveType,
90  adjointSolverName,
91  primalSolverName
92  )
93  );
94  }
95 
96  if (objectives_.empty())
97  {
98  FatalIOErrorInFunction(objectiveNamesDict)
99  << "No objectives have been set - cannot perform an optimisation"
100  << exit(FatalIOError);
101  }
102 }
103 
104 
105 // * * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * //
106 
108 (
109  const fvMesh& mesh,
110  const dictionary& dict,
111  const word& adjointSolverName,
112  const word& primalSolverName
113 )
114 {
115  // Determine type of objectiveManager from objectiveType
116  const word objectiveType(dict.get<word>("type"));
117  const word managerType("objectiveManager" & objectiveType);
118 
119  auto cstrIter = dictionaryConstructorTablePtr_->cfind(managerType);
120 
121  if (!cstrIter.found())
122  {
124  (
125  dict,
126  "objectiveManagerType",
127  managerType,
128  *dictionaryConstructorTablePtr_
129  ) << exit(FatalIOError);
130  }
131 
133  (
134  cstrIter()(mesh, dict, adjointSolverName, primalSolverName)
135  );
136 }
137 
138 
139 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
140 
142 {
143  for (objective& obj : objectives_)
144  {
145  obj.readDict
146  (
147  dict.subDict("objectiveNames").subDict(obj.objectiveName())
148  );
149  }
150 
151  return true;
152 }
153 
155 {
156  // Update normalization factors for all objectives
157  for (objective& obj : objectives_)
158  {
159  obj.updateNormalizationFactor();
160  }
161 }
162 
163 
165 {
166  // Update all fields related to the objective function
167  for (objective& obj : objectives_)
168  {
169  obj.update();
170  }
171 }
172 
173 
175 {
176  //- Update contributions to adjoint if true, otherwise return nulls
177  for (objective& obj : objectives_)
178  {
179  if (obj.isWithinIntegrationTime())
180  {
181  obj.update();
182  }
183  else
184  {
185  obj.nullify();
186  }
187  }
188 }
189 
190 
192 {
193  // Update start and end integration times by adding the timeSpan
194  // of the optimisation cycle
195  for (objective& obj : objectives_)
196  {
197  obj.incrementIntegrationTimes(timeSpan);
198  }
199 }
200 
201 
203 {
204  scalar objValue(Zero);
205  for (objective& obj : objectives_)
206  {
207  scalar cost = obj.JCycle();
208  scalar weight = obj.weight();
209  objValue += weight*cost;
210 
211  Info<< obj.type() << " : " << cost << endl;
212  }
213 
214  Info<< "Objective function manager" << nl
215  << " Weighted Lagrangian " << " : " << objValue << nl << endl;
216 
217  return objValue;
218 }
219 
220 
221 bool objectiveManager::write(const bool valid) const
222 {
223  for (const objective& obj : objectives_)
224  {
225  // Write objective function to file
226  obj.write();
227  obj.writeMeanValue();
228  }
229 
230  return true;
231 }
232 
233 
235 {
237  update();
238  print();
239  write();
240 }
241 
242 
244 {
245  return objectives_;
246 }
247 
248 
250 {
251  return objectives_;
252 }
253 
254 
256 {
257  return adjointSolverName_;
258 }
259 
260 
262 {
263  return primalSolverName_;
264 }
265 
266 
268 {
269  for (const objective& obj : objectives_)
270  {
271  if (!obj.hasIntegrationStartTime() || !obj.hasIntegrationEndTime())
272  {
274  << "Objective function " << obj.objectiveName()
275  << " does not have a defined integration start or end time "
276  << exit(FatalError);
277  }
278  }
279 }
280 
281 
282 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
283 
284 } // End namespace Foam
285 
286 // ************************************************************************* //
Foam::objectiveManager::updateNormalizationFactor
void updateNormalizationFactor()
Update objective function related values.
Definition: objectiveManager.C:154
Foam::IOobject::NO_WRITE
Definition: IOobject.H:130
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:104
Foam::objectiveManager::primalSolverName
const word & primalSolverName() const
Return name of the primalSolver.
Definition: objectiveManager.C:261
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::objectiveManager::print
scalar print()
Print to screen.
Definition: objectiveManager.C:202
Foam::Zero
static constexpr const zero Zero
Global zero.
Definition: zero.H:128
Foam::objectiveManager::objectives_
PtrList< objective > objectives_
Definition: objectiveManager.H:66
Foam::defineRunTimeSelectionTable
defineRunTimeSelectionTable(reactionRateFlameArea, dictionary)
Foam::objective::New
static autoPtr< objective > New(const fvMesh &mesh, const dictionary &dict, const word &objectiveType, const word &adjointSolverName, const word &primalSolverName)
Return a reference to the selected turbulence model.
Definition: objective.C:182
Foam::objectiveManager::readDict
virtual bool readDict(const dictionary &dict)
Definition: objectiveManager.C:141
Foam::objectiveManager::update
void update()
Update objective function related values.
Definition: objectiveManager.C:164
Foam::FatalIOError
IOerror FatalIOError
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:337
Foam::dictionary::get
T get(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:81
objectiveManager.H
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:290
Foam::objectiveManager::write
virtual bool write(const bool valid=true) const
Write objective function history.
Definition: objectiveManager.C:221
FatalIOErrorInLookup
#define FatalIOErrorInLookup(ios, lookupTag, lookupName, lookupTable)
Report an error message using Foam::FatalIOError.
Definition: error.H:380
Foam::objectiveManager::incrementIntegrationTimes
void incrementIntegrationTimes(const scalar timeSpan)
Increment integration times by the optimisation cycle time-span.
Definition: objectiveManager.C:191
Foam::Info
messageStream Info
Information stream (uses stdout - output is on the master only)
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:523
Foam::PtrList
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition: List.H:65
dict
dictionary dict
Definition: searchingEngine.H:14
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:121
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
Foam::objectiveManager::primalSolverName_
const word primalSolverName_
Definition: objectiveManager.H:65
Foam::fvMesh
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:84
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::objectiveManager::adjointSolverName_
const word adjointSolverName_
Definition: objectiveManager.H:64
Foam::objectiveManager::checkIntegrationTimes
void checkIntegrationTimes() const
Check integration times for unsteady runs.
Definition: objectiveManager.C:267
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
Foam::regIOobject
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:67
Foam::TimePaths::system
const word & system() const
Return system name.
Definition: TimePathsI.H:94
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:355
Foam::objectiveManager::New
static autoPtr< objectiveManager > New(const fvMesh &mesh, const dictionary &dict, const word &adjointSolverName, const word &primalSolverName)
Return a reference to the selected turbulence model.
Definition: objectiveManager.C:108
Foam::nl
constexpr char nl
Definition: Ostream.H:372
Foam::List< word >
Foam::objectiveManager::updateAndWrite
void updateAndWrite()
Definition: objectiveManager.C:234
Foam::fvMesh::time
const Time & time() const
Return the top-level database.
Definition: fvMesh.H:246
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:375
Foam::objectiveManager::updateOrNullify
void updateOrNullify()
Update contributions to adjoint if true, otherwise return nulls.
Definition: objectiveManager.C:174
Foam::objectiveManager::adjointSolverName
const word & adjointSolverName() const
Return name of the adjointSolver.
Definition: objectiveManager.C:255
Foam::List::setSize
void setSize(const label newSize)
Alias for resize(const label)
Definition: ListI.H:146
Foam::IOobject::NO_READ
Definition: IOobject.H:123
Foam::objective
Abstract base class for objective functions. No point in making this runTime selectable since its chi...
Definition: objective.H:58
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)
Foam::objectiveManager::getObjectiveFunctions
PtrList< objective > & getObjectiveFunctions()
Return reference to objective functions.
Definition: objectiveManager.C:243