objective.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-2020 PCOpt/NTUA
9  Copyright (C) 2013-2020 FOSS GP
10  Copyright (C) 2019-2020 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::objective
30 
31 Description
32  Abstract base class for objective functions. No point in making this
33  runTime selectable since its children will have different constructors.
34 
35 SourceFiles
36  objective.C
37 
38 \*---------------------------------------------------------------------------*/
39 
40 #ifndef objective_H
41 #define objective_H
42 
43 #include "localIOdictionary.H"
44 #include "autoPtr.H"
45 #include "runTimeSelectionTables.H"
46 #include "OFstream.H"
47 #include "boundaryFieldsFwd.H"
48 #include "solverControl.H"
49 #include "objectiveFwd.H"
50 
51 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
52 
53 namespace Foam
54 {
55 
56 /*---------------------------------------------------------------------------*\
57  Class objective Declaration
58 \*---------------------------------------------------------------------------*/
59 
60 class objective
61 :
62  public localIOdictionary
63 {
64 protected:
65 
66  // Protected data
67 
68  const fvMesh& mesh_;
74  bool nullified_;
75  bool normalize_;
76 
77  //- Objective function value and weight
78  scalar J_;
79 
80  //- Average objective value
81  scalar JMean_;
82 
83  //- Objective weight
84  scalar weight_;
85 
86  //- Normalization factor
88 
89  //- Target value, in case the objective is used as a constraint
90  // Should be used in caution and with updateMethods than get affected
91  // by the target value, without requiring a sqr (e.g. SQP, MMA)
93 
94  //- Objective integration start and end times (for unsteady flows)
97 
98  //- Contribution to field sensitivity derivatives
99  // Topology optimisation or other variants with
100  // as many design variables as the mesh cells
101  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
102 
103  autoPtr<volScalarField> dJdbPtr_;
104 
105  // Contribution to surface sensitivity derivatives
106  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
107 
108  //- Term from material derivative
110 
111  //- Term multiplying delta(n dS)/delta b
112  autoPtr<boundaryVectorField> bdSdbMultPtr_;
113 
114  //- Term multiplying delta(n)/delta b
115  autoPtr<boundaryVectorField> bdndbMultPtr_;
116 
117  //- Term multiplying delta(x)/delta b at the boundary
118  autoPtr<boundaryVectorField> bdxdbMultPtr_;
119 
120  //- Term multiplying delta(x)/delta b at the boundary
121  //- for objectives that directly depend on x, e.g. moment
122  //- Needed in both FI and SI computations
123  autoPtr<boundaryVectorField> bdxdbDirectMultPtr_;
124 
125  //- Contribution located in specific parts of a patch.
126  //- Mainly intended for patch boundary edges contributions, e.g.
127  //- NURBS surfaces G1 continuity
128  autoPtr<vectorField3> bEdgeContribution_;
129 
130  //- For use with discrete-like sensitivities
131  autoPtr<boundaryTensorField> bdJdStressPtr_;
132 
133  // Contribution to volume-based sensitivities from volume-based
134  // objective functions
135  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
136 
137  //- Multiplier of d(Volume)/db
138  autoPtr<volScalarField> divDxDbMultPtr_;
139 
140  //- Emerging from volume objectives that include spatial derivatives
141  autoPtr<volTensorField> gradDxDbMultPtr_;
142 
143  //- Output file variables
144  fileName objFunctionFolder_;
145 
146  //- File to keep the objective values after the end of the primal solver
147  mutable autoPtr<OFstream> objFunctionFilePtr_;
148 
149  //- File to keep the objective values at each iteration of the primal
150  //- solver
151  mutable autoPtr<OFstream> instantValueFilePtr_;
152 
153  //- File to keep the average objective values after the end of the
154  //- primal solver
155  mutable autoPtr<OFstream> meanValueFilePtr_;
156 
157  //- Default width of entries when writing in the objective files
158  unsigned int width_;
159 
160 
161  // Protected Member Functions
162 
163  //- Return objective dictionary
164  const dictionary& dict() const;
165 
166  //- Set the output file ptr
167  void setObjectiveFilePtr() const;
168 
169  //- Set the output file ptr for the instantaneous value
170  void setInstantValueFilePtr() const;
171 
172  //- Set the output file ptr for the mean value
173  void setMeanValueFilePtr() const;
174 
175 
176 private:
177 
178  // Private Member Functions
179 
180  //- No copy construct
181  objective(const objective&) = delete;
182 
183  //- No copy assignment
184  void operator=(const objective&) = delete;
185 
186  //- Make objective Function Folder
187  void makeFolder();
188 
189 
190 public:
191 
192  //- Runtime type information
193  TypeName("objective");
194 
195 
196  // Declare run-time constructor selection table
197 
199  (
200  autoPtr,
201  objective,
202  objective,
203  (
204  const fvMesh& mesh,
205  const dictionary& dict,
206  const word& adjointSolverName,
207  const word& primalSolverName
208  ),
209  (mesh, dict, adjointSolverName, primalSolverName)
210  );
211 
212 
213  // Constructors
214 
215  //- Construct from components
216  objective
217  (
218  const fvMesh& mesh,
219  const dictionary& dict,
220  const word& adjointSolverName,
221  const word& primalSolverName
222  );
223 
224 
225  // Selectors
226 
227  //- Return a reference to the selected turbulence model
228  static autoPtr<objective> New
229  (
230  const fvMesh& mesh,
231  const dictionary& dict,
232  const word& objectiveType,
233  const word& adjointSolverName,
234  const word& primalSolverName
235  );
236 
237 
238  //- Destructor
239  virtual ~objective() = default;
240 
241 
242  // Member Functions
243 
244  virtual bool readDict(const dictionary& dict);
245 
246  //- Return the instantaneous objective function value
247  virtual scalar J() = 0;
248 
249  //- Return the mean objective function value, if it exists,
250  //- otherwise the mean one
251  scalar JCycle() const;
252 
253  //- Accumulate contribution for the mean objective value
254  // For steady-state runs
255  void accumulateJMean(solverControl& solverControl);
256 
257  //- Accumulate contribution for the mean objective value
258  // For unsteady runs
259  void accumulateJMean();
260 
261  //- Return the objective function weight
262  scalar weight() const;
263 
264  //- Is the objective normalized
265  bool normalize() const;
266 
267  //- Normalize all fields allocated by the objective
268  virtual void doNormalization();
269 
270  //- Check whether this is an objective integration time
271  bool isWithinIntegrationTime() const;
272 
273  //- Increment integration times
274  void incrementIntegrationTimes(const scalar timeSpan);
275 
276  //- Contribution to field sensitivities
277  const volScalarField& dJdb();
278 
279  //- Contribution to surface sensitivities for a specific patch
280  const fvPatchVectorField& boundarydJdb(const label);
281 
282  //- Multiplier of delta(n dS)/delta b
283  const fvPatchVectorField& dSdbMultiplier(const label);
284 
285  //- Multiplier of delta(n dS)/delta b
286  const fvPatchVectorField& dndbMultiplier(const label);
287 
288  //- Multiplier of delta(x)/delta b
289  const fvPatchVectorField& dxdbMultiplier(const label);
290 
291  //- Multiplier of delta(x)/delta b
292  const fvPatchVectorField& dxdbDirectMultiplier(const label);
293 
294  //- Multiplier located at patch boundary edges
295  const vectorField& boundaryEdgeMultiplier
296  (
297  const label patchI,
298  const label edgeI
299  );
300 
301  //- Objective partial deriv wrt stress tensor
302  const fvPatchTensorField& boundarydJdStress(const label);
303 
304  //- Contribution to surface sensitivities for all patches
305  const boundaryVectorField& boundarydJdb();
306 
307  //- Multiplier of delta(n dS)/delta b for all patches
308  const boundaryVectorField& dSdbMultiplier();
309 
310  //- Multiplier of delta(n dS)/delta b for all patches
311  const boundaryVectorField& dndbMultiplier();
312 
313  //- Multiplier of delta(x)/delta b for all patches
314  const boundaryVectorField& dxdbMultiplier();
315 
316  //- Multiplier of delta(x)/delta b for all patches
317  const boundaryVectorField& dxdbDirectMultiplier();
318 
319  //- Multiplier located at patch boundary edges
320  const vectorField3& boundaryEdgeMultiplier();
321 
322  //- Objective partial deriv wrt stress tensor
323  const boundaryTensorField& boundarydJdStress();
324 
325  //- Multiplier of grad( delta(x)/delta b) for volume-based sensitivities
326  const volScalarField& divDxDbMultiplier();
327 
328  //- Multiplier of grad( delta(x)/delta b) for volume-based sensitivities
329  const volTensorField& gradDxDbMultiplier();
330 
331  //- Update objective function derivatives
332  virtual void update() = 0;
333 
334  //- Nullify adjoint contributions
335  virtual void nullify();
336 
337  //- Update normalization factors, for objectives in
338  //- which the factor is not known a priori
339  virtual void updateNormalizationFactor();
340 
341  //- Update objective function derivative term
342  virtual void update_boundarydJdb()
343  {}
344 
345  //- Update d (normal dS) / db multiplier. Surface-based sensitivity term
346  virtual void update_dSdbMultiplier()
347  {}
348 
349  //- Update d (normal) / db multiplier. Surface-based sensitivity term
350  virtual void update_dndbMultiplier()
351  {}
352 
353  //- Update d (x) / db multiplier. Surface-based sensitivity term
354  virtual void update_dxdbMultiplier()
355  {}
356 
357  //- Update d (x) / db multiplier. Surface and volume-based sensitivity
358  //- term
359  virtual void update_dxdbDirectMultiplier()
360  {}
361 
362  //- Update boundary edge contributions
363  virtual void update_boundaryEdgeContribution()
364  {}
365 
366  //- Update dJ/dStress field
367  virtual void update_dJdStressMultiplier()
368  {}
369 
370  //- Update div( dx/db multiplier). Volume-based sensitivity term
371  virtual void update_divDxDbMultiplier()
372  {}
373 
374  //- Update grad( dx/db multiplier). Volume-based sensitivity term
375  virtual void update_gradDxDbMultiplier()
376  {}
377 
378  //- Write objective function history
379  virtual bool write(const bool valid = true) const;
380 
381  //- Write objective function history at each primal solver iteration
382  virtual void writeInstantaneousValue() const;
383 
384  //- Append a blank line after the end of optimisation cycle to the
385  //- file holding the instantaneous objective values to facilitate
386  //- plotting
387  virtual void writeInstantaneousSeparator() const;
388 
389  //- Write mean objective function history
390  virtual void writeMeanValue() const;
391 
392  //- Write averaged objective for continuation
393  virtual bool writeData(Ostream& os) const;
394 
395  // Helper write functions
396 
397  //- Write any information that needs to go the header of the file
398  // (e.g. targets, directions, etc)
399  virtual void addHeaderInfo() const;
400 
401  //- Write headers for additional columns
402  virtual void addHeaderColumns() const;
403 
404  //- Write information to additional columns
405  virtual void addColumnValues() const;
406 
407  //- Return the objective name
408  inline const word& objectiveName() const;
409 
410  // Inline functions for checking whether pointers are set or not
411  inline bool hasdJdb() const;
412  inline bool hasBoundarydJdb() const;
413  inline bool hasdSdbMult() const;
414  inline bool hasdndbMult() const;
415  inline bool hasdxdbMult() const;
416  inline bool hasdxdbDirectMult() const;
417  inline bool hasBoundaryEdgeContribution() const;
418  inline bool hasBoundarydJdStress() const;
419  inline bool hasDivDxDbMult() const;
420  inline bool hasGradDxDbMult() const;
421 
422  // Inline functions for checking whether integration times are set
423  inline bool hasIntegrationStartTime() const;
424  inline bool hasIntegrationEndTime() const;
425 };
426 
427 
428 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
429 
430 } // End namespace Foam
431 
432 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
433 
434 #include "objectiveI.H"
435 
436 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
437 
438 #endif
439 
440 // ************************************************************************* //
Foam::fvPatchField< vector >
Foam::objective::normFactor_
autoPtr< scalar > normFactor_
Normalization factor.
Definition: objective.H:86
Foam::objective::integrationStartTimePtr_
autoPtr< scalar > integrationStartTimePtr_
Objective integration start and end times (for unsteady flows)
Definition: objective.H:94
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
objectiveI.H
Foam::objective::normalize_
bool normalize_
Definition: objective.H:74
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
Foam::baseIOdictionary::writeData
virtual bool writeData(Ostream &) const
The writeData function required by regIOobject write operation.
Definition: baseIOdictionary.C:109
Foam::baseIOdictionary::operator=
void operator=(const baseIOdictionary &rhs)
Copy assignment of dictionary entries (leave regIOobject untouched)
Definition: baseIOdictionary.C:118
Foam::objective::objectiveName_
const word objectiveName_
Definition: objective.H:71
Foam::dictionary::New
static autoPtr< dictionary > New(Istream &is)
Construct top-level dictionary on freestore from Istream.
Definition: dictionaryIO.C:69
update
mesh update()
declareRunTimeNewSelectionTable
#define declareRunTimeNewSelectionTable(ptrWrapper, baseType, argNames, argList, parList)
Declare a run-time selection for derived classes.
Definition: runTimeSelectionTables.H:272
Foam::solverControl
Base class for solver control classes.
Definition: solverControl.H:51
objectiveFwd.H
Foam::objective::target_
autoPtr< scalar > target_
Target value, in case the objective is used as a constraint.
Definition: objective.H:91
Foam::objective::adjointSolverName_
const word adjointSolverName_
Definition: objective.H:69
OFstream.H
Foam::Field< vector >
Foam::objective::J_
scalar J_
Objective function value and weight.
Definition: objective.H:77
Foam::boundaryTensorField
volTensorField::Boundary boundaryTensorField
Definition: boundaryFieldsFwd.H:56
Foam::baseIOdictionary::TypeName
TypeName("dictionary")
Declare type-name, virtual type (with debug switch)
Foam::objective::primalSolverName_
const word primalSolverName_
Definition: objective.H:70
Foam::objective::weight_
scalar weight_
Objective weight.
Definition: objective.H:83
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::objective::integrationEndTimePtr_
autoPtr< scalar > integrationEndTimePtr_
Definition: objective.H:95
os
OBJstream os(runTime.globalPath()/outputName)
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::autoPtr< scalar >
Foam::objective::nullified_
bool nullified_
Definition: objective.H:73
Foam::objective::computeMeanFields_
bool computeMeanFields_
Definition: objective.H:72
Foam::GeometricField::Boundary
The boundary fields.
Definition: GeometricField.H:115
boundaryFieldsFwd.H
Useful typenames for fields defined only at the boundaries.
runTimeSelectionTables.H
Macros to ease declaration of run-time selection tables.
Foam::dictionary::write
void write(Ostream &os, const bool subDict=true) const
Write dictionary, normally with sub-dictionary formatting.
Definition: dictionaryIO.C:206
Foam::objective::mesh_
const fvMesh & mesh_
Definition: objective.H:67
localIOdictionary.H
Foam::objective::dict_
dictionary dict_
Definition: objective.H:68
Foam::objective::JMean_
scalar JMean_
Average objective value.
Definition: objective.H:80
Foam::GeometricField< scalar, fvPatchField, volMesh >
Foam::objective
Abstract base class for objective functions. No point in making this runTime selectable since its chi...
Definition: objective.H:59
Foam::localIOdictionary
localIOdictionary is derived from IOdictionary but excludes parallel master reading.
Definition: localIOdictionary.H:52
Foam::normalize
quaternion normalize(const quaternion &q)
Return the normalized (unit) quaternion of the given quaternion.
Definition: quaternionI.H:661
solverControl.H
autoPtr.H