designVariables.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-2023 PCOpt/NTUA
9  Copyright (C) 2013-2023 FOSS GP
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 
28 Class
29  Foam::designVariables
30 
31 Description
32  Abstract base class for defining design variables.
33 
34 SourceFiles
35  designVariables.C
36 
37 \*---------------------------------------------------------------------------*/
38 
39 #ifndef designVariables_H
40 #define designVariables_H
41 
42 #include "fvMesh.H"
43 #include "volFieldsFwd.H"
44 #include "volFields.H"
45 #include "dictionary.H"
46 #include "primalSolver.H"
47 #include "adjointSolverManager.H"
48 #include "adjointSensitivity.H"
49 #include "runTimeSelectionTables.H"
50 
51 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
52 
53 namespace Foam
54 {
55 
56 class adjointSensitivity;
57 
58 /*---------------------------------------------------------------------------*\
59  Class designVariables Declaration
60 \*---------------------------------------------------------------------------*/
61 
62 class designVariables
63 :
64  public scalarField
65 {
66 protected:
67 
68  // Protected data
69 
70  fvMesh& mesh_;
72 
73  //- Which of the design variables will be updated
75 
76  //- Copy of old design variables. Useful when performing line-search
78 
79  //- Maximum design variables' change in the first optimisation cycle
80  // Used when eta is not used in updateMethod
82 
83  //- Lower bounds of the design variables
85 
86  //- Upper bounds of the design variables
88 
89 
90  // Protected Member Functions
91 
92  //- Read bounds for design variables, if present
94  (
95  autoPtr<scalar> lowerBoundPtr = nullptr,
96  autoPtr<scalar> upperBoundPtr = nullptr
97  );
98 
99 
100 private:
101 
102  // Private Member Functions
103 
104  //- Disallow default bitwise copy construct
105  designVariables(const designVariables&) = delete;
106 
107  //- Disallow default bitwise assignment
108  void operator=(const designVariables&) = delete;
109 
110 
111 public:
112 
113  //- Runtime type information
114  TypeName("designVariables");
115 
116 
117  // Declare run-time constructor selection table
118 
120  (
121  autoPtr,
124  (
125  fvMesh& mesh,
126  const dictionary& dict
127  ),
128  (mesh, dict)
129  );
130 
131 
132  // Constructors
133 
134  //- Construct from dictionary
136  (
137  fvMesh& mesh,
138  const dictionary& dict
139  );
140 
141  //- Construct from dictionary and size
143  (
144  fvMesh& mesh,
145  const dictionary& dict,
146  const label size
147  );
148 
149 
150  // Selectors
151 
152  //- Return a reference to the selected design variables
154  (
155  fvMesh& mesh,
156  const dictionary& dict
157  );
158 
159 
160  //- Destructor
161  virtual ~designVariables() = default;
162 
163 
164  // Member Functions
165 
166  //- Read dictionary if changed
167  virtual bool readDict(const dictionary& dict);
168 
169  //- Get the design variables
170  // Defaults to *this.
171  // Virtual for potential overriding from derived classes
172  virtual const scalarField& getVars() const;
173 
174  //- Get the design variables
175  // Defaults to *this.
176  // Virtual for potential overriding from derived classes
177  virtual scalarField& getVars();
178 
179  //- Update design variables based on a given correction
180  // Translates the scalarField of corrections to a meaningful
181  // update of the design variables
182  virtual void update(scalarField& correction) = 0;
183 
184  //- Store design variables, as the starting point for line search
185  virtual void storeDesignVariables();
186 
187  //- Reset to the starting point of line search
188  virtual void resetDesignVariables();
189 
190  //- Compute eta if not set in the first step
191  virtual scalar computeEta(scalarField& correction) = 0;
192 
193  //- Whether to use global sum when computing matrix-vector products
194  //- in update methods
195  // Depends on whether the design variables are common for all
196  // processors (e.g. volumetric B-Splines control points) or distributed
197  // across them (e.g. topology optimisation)
198  virtual bool globalSum() const = 0;
199 
200  //- Return list of active design variables
201  inline const labelList& activeDesignVariables() const;
202 
203  //- Check whether the max. initial change of the design variables has
204  //- been set
205  inline bool isMaxInitChangeSet() const;
206 
207  //- Get maxInitChange
208  inline const autoPtr<scalar>& getMaxInitChange();
209 
210  //- Set maxInitChange
211  inline void setMaxInitChange(const scalar maxInitChange);
212 
213  //- Trigger the recomputation of eta by updateMethod
214  inline virtual bool resetEta() const;
215 
216  //- Get min bounds for the design variables
217  inline const autoPtr<scalarField>& lowerBounds() const;
218 
219  //- Get max bounds for the design variables
220  inline const autoPtr<scalarField>& upperBounds() const;
221 
222  //- Get min bounds for the design variables
223  inline scalarField& lowerBoundsRef();
224 
225  //- Get max bounds for the design variables
226  inline scalarField& upperBoundsRef();
227 
228  //- Post process sensitivities if needed
229  virtual void postProcessSens
230  (
231  scalarField& objectiveSens,
232  PtrList<scalarField>& constraintSens,
233  const wordList& adjointSolversNames,
234  bool isMaster
235  );
236 
237  //- Assemble sensitivity derivatives, by combining the part related
238  //- to the primal and adjoint solution with the part related to the
239  //- design variables
241  (
242  adjointSensitivity& sens
243  ) = 0;
244 
245  //- For design variables with a dynamic character (i.e. changing
246  //- number), perform the evolution
247  virtual void evolveNumber();
248 
249  //- Set initial values of the design variables
250  // For design variables sets that need to be initialised after the
251  // construction of the primal fields.
252  // Does nothing in base
253  virtual void setInitialValues();
254 
255  //- Add fvOptions depending on the design variables
256  virtual void addFvOptions
257  (
260  );
261 
262  //- Design variables might add constraints related to themselves
263  //- (e.g. linear combinations of the design variables)
264  //- Return the values and gradients of these constraints
267 
268  //- Write useful quantities to files
269  virtual void writeDesignVars();
270 };
271 
272 
273 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
274 
275 } // End namespace Foam
276 
277 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
278 
279 #include "designVariablesI.H"
280 
281 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
282 
283 #endif
284 
285 // ************************************************************************* //
virtual PtrList< scalarField > constraintDerivatives()
labelList activeDesignVariables_
Which of the design variables will be updated.
tmp< fvMatrix< Type > > correction(const fvMatrix< Type > &)
Return the correction form of the given matrix by subtracting the matrix multiplied by the current fi...
dictionary dict
declareRunTimeNewSelectionTable(autoPtr, designVariables, designVariables,(fvMesh &mesh, const dictionary &dict),(mesh, dict))
scalarField & upperBoundsRef()
Get max bounds for the design variables.
virtual tmp< scalarField > assembleSensitivities(adjointSensitivity &sens)=0
Assemble sensitivity derivatives, by combining the part related to the primal and adjoint solution wi...
const autoPtr< scalar > & getMaxInitChange()
Get maxInitChange.
Forwards and collection of common volume field types.
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
Abstract base class for adjoint-based sensitivities.
virtual void evolveNumber()
For design variables with a dynamic character (i.e. changing number), perform the evolution...
Base class for primal solvers.
Definition: primalSolver.H:46
virtual tmp< scalarField > constraintValues()
Design variables might add constraints related to themselves (e.g. linear combinations of the design ...
const autoPtr< scalarField > & lowerBounds() const
Get min bounds for the design variables.
bool isMaxInitChangeSet() const
Check whether the max. initial change of the design variables has been set.
virtual void setInitialValues()
Set initial values of the design variables.
virtual ~designVariables()=default
Destructor.
virtual void update(scalarField &correction)=0
Update design variables based on a given correction.
virtual bool globalSum() const =0
Whether to use global sum when computing matrix-vector products in update methods.
autoPtr< scalarField > oldDesignVariables_
Copy of old design variables. Useful when performing line-search.
virtual bool readDict(const dictionary &dict)
Read dictionary if changed.
dynamicFvMesh & mesh
const labelList & activeDesignVariables() const
Return list of active design variables.
autoPtr< scalar > maxInitChange_
Maximum design variables&#39; change in the first optimisation cycle.
void readBounds(autoPtr< scalar > lowerBoundPtr=nullptr, autoPtr< scalar > upperBoundPtr=nullptr)
Read bounds for design variables, if present.
void setMaxInitChange(const scalar maxInitChange)
Set maxInitChange.
virtual void resetDesignVariables()
Reset to the starting point of line search.
virtual void addFvOptions(const PtrList< primalSolver > &primalSolver, const PtrList< adjointSolverManager > &adjointSolverManagers)
Add fvOptions depending on the design variables.
virtual const scalarField & getVars() const
Get the design variables.
label size() const noexcept
The number of elements in the container.
Definition: UList.H:671
scalarField & lowerBoundsRef()
Get min bounds for the design variables.
TypeName("designVariables")
Runtime type information.
autoPtr< scalarField > lowerBounds_
Lower bounds of the design variables.
virtual void storeDesignVariables()
Store design variables, as the starting point for line search.
virtual void postProcessSens(scalarField &objectiveSens, PtrList< scalarField > &constraintSens, const wordList &adjointSolversNames, bool isMaster)
Post process sensitivities if needed.
Abstract base class for defining design variables.
virtual void writeDesignVars()
Write useful quantities to files.
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers...
Definition: List.H:55
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:78
autoPtr< scalarField > upperBounds_
Upper bounds of the design variables.
static autoPtr< designVariables > New(fvMesh &mesh, const dictionary &dict)
Return a reference to the selected design variables.
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
virtual scalar computeEta(scalarField &correction)=0
Compute eta if not set in the first step.
Macros to ease declaration of run-time selection tables.
virtual bool resetEta() const
Trigger the recomputation of eta by updateMethod.
const autoPtr< scalarField > & upperBounds() const
Get max bounds for the design variables.
A class for managing temporary objects.
Definition: HashPtrTable.H:50
PtrList< adjointSolverManager > & adjointSolverManagers
Definition: createFields.H:8
Namespace for OpenFOAM.