Loading...
Searching...
No Matches
designVariables.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-2023 PCOpt/NTUA
9 Copyright (C) 2013-2023 FOSS GP
10-------------------------------------------------------------------------------
11License
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
31
32// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33
34namespace Foam
35{
36 defineTypeNameAndDebug(designVariables, 0);
37 defineRunTimeSelectionTable(designVariables, designVariables);
38}
39
40
41// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
42
44(
45 autoPtr<scalar> lowerBoundPtr,
46 autoPtr<scalar> upperBoundPtr
47)
48{
49 // Read lower bounds for the design variables, if present
50 if (dict_.found("lowerBounds"))
51 {
53 if (lowerBounds.size() != getVars().size())
54 {
56 << "Inconsistent dimensions for lowerBounds ("
57 << lowerBounds.size()
58 << ") and design variables ("
59 << getVars().size() << ")"
60 << exit(FatalError);
61 }
63 }
64 else if (dict_.found("lowerBound"))
65 {
66 scalar lowerBound(dict_.get<scalar>("lowerBound"));
67 lowerBounds_.reset(new scalarField(getVars().size(), lowerBound));
68 }
69 else if (lowerBoundPtr)
70 {
71 lowerBounds_.reset(new scalarField(getVars().size(), *lowerBoundPtr));
72 }
73
74 // Read upper bounds for the design variables, if present
75 if (dict_.found("upperBounds"))
76 {
77 scalarField upperBounds(dict_.get<scalarField>("upperBounds"));
78 if (upperBounds.size() != getVars().size())
79 {
81 << "Inconsistent dimensions for upperBounds ("
82 << upperBounds.size()
83 << ") and design variables ("
84 << getVars().size() << ")"
85 << exit(FatalError);
86 }
88 }
89 else if (dict_.found("upperBound"))
90 {
91 scalar upperBound(dict_.get<scalar>("upperBound"));
92 upperBounds_.reset(new scalarField(getVars().size(), upperBound));
93 }
94 else if (upperBoundPtr)
95 {
96 upperBounds_.reset(new scalarField(getVars().size(), *upperBoundPtr));
97 }
98}
99
100
101// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
102
103Foam::designVariables::designVariables
104(
105 fvMesh& mesh,
106 const dictionary& dict
107)
108:
109 scalarField(0),
110 mesh_(mesh),
111 dict_(dict),
112 activeDesignVariables_(0),
113 oldDesignVariables_(nullptr),
114 maxInitChange_(nullptr),
115 lowerBounds_(nullptr),
116 upperBounds_(nullptr)
117{
118 // Read max initial change of design variables if present
119 if (dict.found("maxInitChange"))
120 {
121 maxInitChange_.reset(new scalar(dict_.get<scalar>("maxInitChange")));
122 }
123}
124
125
126Foam::designVariables::designVariables
127(
128 fvMesh& mesh,
129 const dictionary& dict,
130 const label size
131)
132:
133 scalarField(size, Zero),
134 mesh_(mesh),
135 dict_(dict),
136 activeDesignVariables_(0),
137 oldDesignVariables_(nullptr),
138 maxInitChange_(nullptr),
139 lowerBounds_(nullptr),
140 upperBounds_(nullptr)
141{
142 // Read max initial change of design variables if present
143 if (dict.found("maxInitChange"))
144 {
145 maxInitChange_.reset(new scalar(dict_.get<scalar>("maxInitChange")));
146 }
147}
148
149
150// * * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * //
151
153(
154 fvMesh& mesh,
155 const dictionary& dict
156)
157{
158 if (!dict.found("type"))
159 {
160 return autoPtr<designVariables>(nullptr);
161 }
162
163 const word modelType(dict.get<word>("type"));
164
165 Info<< "designVariables type : " << modelType << endl;
166
167 auto* ctorPtr = designVariablesConstructorTable(modelType);
168
169 if (!ctorPtr)
170 {
172 (
173 "designVariables",
174 modelType,
175 *designVariablesConstructorTablePtr_
176 ) << exit(FatalError);
177 }
179 return autoPtr<designVariables>(ctorPtr(mesh, dict));
180}
181
182
183// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * //
184
186{
187 dict_ = dict;
188
189 if (dict.found("maxInitChange"))
190 {
191 maxInitChange_.reset(new scalar(dict_.get<scalar>("maxInitChange")));
192 }
193
194 return true;
195}
196
199{
200 return *this;
201}
202
212 if (!oldDesignVariables_)
213 {
215 }
216
217 oldDesignVariables_.ref() = getVars();
218}
219
220
224 << "Reseting design variables" << endl;
226}
227
228
230(
231 scalarField& objectiveSens,
232 PtrList<scalarField>& constraintSens,
233 const wordList& adjointSolversNames,
234 bool isMaster
235)
236{
237 // Does nothing in base
238}
239
242{
243 // Does nothing in base
244}
245
248{
249 // Does nothing in base
250}
251
252
254(
257)
258{
259 // Does nothing in base
260}
261
266}
267
272}
273
274
276{
277 // Does nothing in base
278}
279
280
281// ************************************************************************* //
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition PtrList.H:67
label size() const noexcept
Definition UList.H:706
void size(const label n)
Older name for setAddressableSize.
Definition UList.H:118
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition autoPtr.H:65
Abstract base class for defining design variables.
autoPtr< scalarField > oldDesignVariables_
Copy of old design variables. Useful when performing line-search.
virtual void evolveNumber()
For design variables with a dynamic character (i.e. changing number), perform the evolution.
static autoPtr< designVariables > New(fvMesh &mesh, const dictionary &dict)
Return a reference to the selected design variables.
autoPtr< scalarField > upperBounds_
Upper bounds of the design variables.
autoPtr< scalar > maxInitChange_
Maximum design variables' change in the first optimisation cycle.
const autoPtr< scalarField > & upperBounds() const
Get max bounds for the design variables.
virtual void resetDesignVariables()
Reset to the starting point of line search.
virtual PtrList< scalarField > constraintDerivatives()
virtual void addFvOptions(const PtrList< primalSolver > &primalSolver, const PtrList< adjointSolverManager > &adjointSolverManagers)
Add fvOptions depending on the design variables.
void readBounds(autoPtr< scalar > lowerBoundPtr=nullptr, autoPtr< scalar > upperBoundPtr=nullptr)
Read bounds for design variables, if present.
virtual const scalarField & getVars() const
Get the design variables.
virtual tmp< scalarField > constraintValues()
Design variables might add constraints related to themselves (e.g. linear combinations of the design ...
autoPtr< scalarField > lowerBounds_
Lower bounds of the design variables.
virtual bool readDict(const dictionary &dict)
Read dictionary if changed.
virtual void setInitialValues()
Set initial values of the design variables.
const autoPtr< scalarField > & lowerBounds() const
Get min bounds for the design variables.
labelList activeDesignVariables_
Which of the design variables will be updated.
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.
virtual void writeDesignVars()
Write useful quantities to files.
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
T get(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Find and return a T. FatalIOError if not found, or if the number of tokens is incorrect.
bool found(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Find an entry (const access) with the given keyword.
Mesh data needed to do the Finite Volume discretisation.
Definition fvMesh.H:85
Base class for primal solvers.
A class for managing temporary objects.
Definition tmp.H:75
A class for handling words, derived from Foam::string.
Definition word.H:66
#define defineTypeNameAndDebug(Type, DebugSwitch)
Define the typeName and debug information.
Definition className.H:142
dynamicFvMesh & mesh
#define FatalErrorInLookup(lookupTag, lookupName, lookupTable)
Report an error message using Foam::FatalError.
Definition error.H:607
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition error.H:600
#define DebugInfo
Report an information message using Foam::Info.
Namespace for OpenFOAM.
List< word > wordList
List of word.
Definition fileName.H:60
messageStream Info
Information stream (stdout output on master, null elsewhere).
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
static constexpr const zero Zero
Global zero (0).
Definition zero.H:127
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition errorManip.H:125
#define defineRunTimeSelectionTable(baseType, argNames)
Define run-time selection table.
dictionary dict
PtrList< adjointSolverManager > & adjointSolverManagers
Definition createFields.H:8