updateMethod.H
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 Class
29  Foam::updateMethod
30 
31 Description
32  Abstract base class for optimisation methods
33 
34 SourceFiles
35  updateMethod.C
36 
37 \*---------------------------------------------------------------------------*/
38 
39 #ifndef updateMethod_H
40 #define updateMethod_H
41 
42 #include "runTimeSelectionTables.H"
43 #include "IOdictionary.H"
44 #include "fvMesh.H"
45 
46 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
47 
48 namespace Foam
49 {
50 
51 /*---------------------------------------------------------------------------*\
52  Class updateMethod Declaration
53 \*---------------------------------------------------------------------------*/
54 
55 class updateMethod
56 {
57 protected:
58 
59  // Protected data
60 
61  const fvMesh& mesh_;
62 
63  const dictionary dict_;
64 
65  //- Used to output values useful for continuation runs
67 
68  //- Derivatives of the objective functions
70 
71  //- Derivatives of the constraints
73 
74  //- Objective value
75  scalar objectiveValue_;
76 
77  //- Constraint values
79 
80  //- Design variables correction
82 
83  //- Cumulative design variables correction throughout the optimisation
84  //- loop
86 
87  //- Step multiplying the correction
88  scalar eta_;
89 
90  //- Is initially set?
91  bool initialEtaSet_;
92 
93  //- Folder storing the corrections to file
94  // For some optimisation methods with a very high number of
95  // design variables (e.g. topology), it doesn't make much sense
96  // to write all updates in the updateMethodDict. Hence, a
97  // separate file is used to write the corrections, in case they are
98  // needed for post-processing
100 
101  //- Whether to use gSum or sum in the inner products
102  bool globalSum_;
103 
104  // Scalar -- matrix multiplications
105  const scalarField leftMult
106  (
107  const scalarField&,
108  const SquareMatrix<scalar>&
109  );
110 
111  const scalarField rightMult
112  (
113  const SquareMatrix<scalar>&,
114  const scalarField&
115  );
116 
118  (
119  const scalarField&,
120  const scalarField&
121  );
122 
124 
125  //- Compute either global or local sum, based on globalSum flag
126  scalar globalSum(const scalarField& field);
127 
128  //- Compute either global or local sum, based on globalSum flag
129  scalar globalSum(tmp<scalarField>& tfield);
130 
131  //- Return optional dictionary with parameters specific to each method
133 
134 
135 private:
136 
137  // Private Member Functions
138 
139  //- No copy construct
140  updateMethod(const updateMethod&) = delete;
141 
142  //- No copy assignment
143  void operator=(const updateMethod&) = delete;
144 
145 
146 public:
147 
148  //- Runtime type information
149  TypeName("updateMethod");
150 
151 
152  // Declare run-time constructor selection table
153 
155  (
156  autoPtr,
157  updateMethod,
158  dictionary,
159  (
160  const fvMesh& mesh,
161  const dictionary& dict
162  ),
163  (mesh, dict)
164  );
165 
166 
167  // Constructors
168 
169  //- Construct from components
171  (
172  const fvMesh& mesh,
173  const dictionary& dict
174  );
175 
176 
177  // Selectors
178 
179  //- Return a reference to the selected turbulence model
181  (
182  const fvMesh& mesh,
183  const dictionary& dict
184  );
185 
186 
187  //- Destructor
188  virtual ~updateMethod() = default;
189 
190 
191  // Member Functions
192 
193  //- Set objective derivative
194  void setObjectiveDeriv(const scalarField& derivs);
195 
196  //- Set constraints derivative
197  void setConstraintDeriv(const PtrList<scalarField>& derivs);
198 
199  //- Set constraints derivative
200  void setObjectiveValue(const scalar value);
201 
202  //- Set constraints derivative
204 
205  //- Set step for optimisation methods
206  void setStep(const scalar eta);
207 
208  //- Set globalSum variable.
209  // Should be set by the optimisationType owining the updateMethod
210  void setGlobalSum(const bool useGlobalSum);
211 
212  //- Return the correction of the design variables
213  virtual void computeCorrection()=0;
214 
215  //- Return the correction of the design variables
216  //const scalarField& returnCorrection() const;
217 
218  //- Return the correction of the design variables
220 
221  void writeCorrection();
222 
223  //- Compute merit function. Could be different than the objective
224  //- in the presence of constraints
225  virtual scalar computeMeritFunction();
226 
227  //- Directional derivative of the merit function, in the direction of
228  //- the correction. Could be different than the objective directional
229  //- derivative in the presence of constraints
230  virtual scalar meritFunctionDirectionalDerivative();
231 
232  //- Return whether initial eta was set
233  bool& initialEtaSet();
234 
235  //- Update old correction. useful for quasi-newton methods coupled with
236  //- line search
237  virtual void updateOldCorrection(const scalarField& oldCorrection);
238 
239  //- Write useful quantities to files
240  virtual void write();
241 };
242 
243 
244 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
245 
246 } // End namespace Foam
247 
248 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
249 
250 #endif
251 
252 // ************************************************************************* //
Foam::updateMethod::returnCorrection
scalarField & returnCorrection()
Return the correction of the design variables.
Definition: updateMethod.C:330
Foam::IOdictionary
IOdictionary is derived from dictionary and IOobject to give the dictionary automatic IO functionalit...
Definition: IOdictionary.H:54
Foam::updateMethod::mesh_
const fvMesh & mesh_
Definition: updateMethod.H:60
Foam::updateMethod::initialEtaSet_
bool initialEtaSet_
Is initially set?
Definition: updateMethod.H:90
Foam::updateMethod::rightMult
const scalarField rightMult(const SquareMatrix< scalar > &, const scalarField &)
Definition: updateMethod.C:72
Foam::updateMethod::declareRunTimeSelectionTable
declareRunTimeSelectionTable(autoPtr, updateMethod, dictionary,(const fvMesh &mesh, const dictionary &dict),(mesh, dict))
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::updateMethod::cValues_
scalarField cValues_
Constraint values.
Definition: updateMethod.H:77
Foam::updateMethod::setGlobalSum
void setGlobalSum(const bool useGlobalSum)
Set globalSum variable.
Definition: updateMethod.C:324
Foam::updateMethod::setConstraintValues
void setConstraintValues(const scalarField &values)
Set constraints derivative.
Definition: updateMethod.C:312
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:59
Foam::HashTableOps::values
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
List of values from HashTable, optionally sorted.
Definition: HashOps.H:149
Foam::updateMethod::meritFunctionDirectionalDerivative
virtual scalar meritFunctionDirectionalDerivative()
Definition: updateMethod.C:377
Foam::updateMethod::constraintDerivatives_
PtrList< scalarField > constraintDerivatives_
Derivatives of the constraints.
Definition: updateMethod.H:71
A
static const Foam::dimensionedScalar A("", Foam::dimPressure, 611.21)
Foam::updateMethod::outerProd
SquareMatrix< scalar > outerProd(const scalarField &, const scalarField &)
Definition: updateMethod.C:99
Foam::updateMethod::computeMeritFunction
virtual scalar computeMeritFunction()
Definition: updateMethod.C:371
Foam::updateMethod::globalSum_
bool globalSum_
Whether to use gSum or sum in the inner products.
Definition: updateMethod.H:101
Foam::updateMethod::setConstraintDeriv
void setConstraintDeriv(const PtrList< scalarField > &derivs)
Set constraints derivative.
Definition: updateMethod.C:298
Foam::updateMethod::writeCorrection
void writeCorrection()
Definition: updateMethod.C:337
Foam::updateMethod::dict_
const dictionary dict_
Definition: updateMethod.H:62
Foam::updateMethod::~updateMethod
virtual ~updateMethod()=default
Destructor.
Foam::updateMethod::updateOldCorrection
virtual void updateOldCorrection(const scalarField &oldCorrection)
Definition: updateMethod.C:390
Foam::Field< scalar >
Foam::updateMethod
Abstract base class for optimisation methods.
Definition: updateMethod.H:54
Foam::updateMethod::computeCorrection
virtual void computeCorrection()=0
Return the correction of the design variables.
Foam::updateMethod::New
static autoPtr< updateMethod > New(const fvMesh &mesh, const dictionary &dict)
Return a reference to the selected turbulence model.
Definition: updateMethod.C:263
field
rDeltaTY field()
Foam::PtrList
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition: List.H:62
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:121
Foam::updateMethod::correction_
scalarField correction_
Design variables correction.
Definition: updateMethod.H:80
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
Foam::fvMesh
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:84
fvMesh.H
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::updateMethod::initialEtaSet
bool & initialEtaSet()
Return whether initial eta was set.
Definition: updateMethod.C:383
Foam::updateMethod::setStep
void setStep(const scalar eta)
Set step for optimisation methods.
Definition: updateMethod.C:318
Foam::updateMethod::coeffsDict
dictionary coeffsDict()
Return optional dictionary with parameters specific to each method.
Definition: updateMethod.C:254
IOdictionary.H
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
Foam::SquareMatrix< scalar >
Foam::updateMethod::write
virtual void write()
Write useful quantities to files.
Definition: updateMethod.C:398
Foam::updateMethod::objectiveValue_
scalar objectiveValue_
Objective value.
Definition: updateMethod.H:74
Foam::updateMethod::setObjectiveValue
void setObjectiveValue(const scalar value)
Set constraints derivative.
Definition: updateMethod.C:306
runTimeSelectionTables.H
Macros to ease declaration of run-time selection tables.
Foam::updateMethod::globalSum
scalar globalSum(const scalarField &field)
Compute either global or local sum, based on globalSum flag.
Definition: updateMethod.C:175
Foam::updateMethod::TypeName
TypeName("updateMethod")
Runtime type information.
Foam::updateMethod::leftMult
const scalarField leftMult(const scalarField &, const SquareMatrix< scalar > &)
Definition: updateMethod.C:45
Foam::updateMethod::eta_
scalar eta_
Step multiplying the correction.
Definition: updateMethod.H:87
Foam::updateMethod::inv
SquareMatrix< scalar > inv(SquareMatrix< scalar > A)
Definition: updateMethod.C:125
Foam::updateMethod::setObjectiveDeriv
void setObjectiveDeriv(const scalarField &derivs)
Set objective derivative.
Definition: updateMethod.C:291
Foam::updateMethod::cumulativeCorrection_
scalarField cumulativeCorrection_
Definition: updateMethod.H:84
Foam::updateMethod::objectiveDerivatives_
scalarField objectiveDerivatives_
Derivatives of the objective functions.
Definition: updateMethod.H:68
Foam::updateMethod::correctionFolder_
word correctionFolder_
Folder storing the corrections to file.
Definition: updateMethod.H:98
Foam::updateMethod::optMethodIODict_
IOdictionary optMethodIODict_
Used to output values useful for continuation runs.
Definition: updateMethod.H:65