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 -------------------------------------------------------------------------------
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 
29 #include "designVariables.H"
30 #include "adjointSensitivity.H"
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36  defineTypeNameAndDebug(designVariables, 0);
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  {
52  scalarField lowerBounds(dict_.get<scalarField>("lowerBounds"));
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.valid())
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.valid())
95  {
96  upperBounds_.reset(new scalarField(getVars().size(), upperBoundPtr()));
97  }
98 }
99 
100 
101 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
102 
103 Foam::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 
126 Foam::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 cstrIter = designVariablesConstructorTablePtr_->cfind(modelType);
168 
169  if (!cstrIter.found())
170  {
172  (
173  "designVariables",
174  modelType,
175  *designVariablesConstructorTablePtr_
176  ) << exit(FatalError);
177  }
179  return autoPtr<designVariables>(cstrIter()(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 
205 {
206  return *this;
207 }
208 
209 
211 {
212  if (!oldDesignVariables_)
213  {
214  oldDesignVariables_.reset(new scalarField(getVars().size(), Zero));
215  }
216 
217  oldDesignVariables_.ref() = getVars();
218 }
219 
220 
222 {
224  << "Reseting design variables" << endl;
225  getVars() = (oldDesignVariables_());
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 
246 
248 {
249  // Does nothing in base
250 }
251 
252 
254 (
257 )
258 {
259  // Does nothing in base
260 }
261 
264 {
265  return tmp<scalarField>(nullptr);
266 }
267 
270 {
271  return PtrList<scalarField>();
272 }
273 
274 
276 {
277  // Does nothing in base
278 }
279 
280 
281 // ************************************************************************* //
virtual PtrList< scalarField > constraintDerivatives()
dictionary dict
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:116
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:598
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.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
virtual void setInitialValues()
Set initial values of the design variables.
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.
#define FatalErrorInLookup(lookupTag, lookupName, lookupTable)
Report an error message using Foam::FatalError.
Definition: error.H:605
virtual bool readDict(const dictionary &dict)
Read dictionary if changed.
bool found(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Find an entry (const access) with the given keyword.
Definition: dictionaryI.H:104
void reset(T *p=nullptr) noexcept
Delete managed object and set to new given pointer.
Definition: autoPtrI.H:37
dynamicFvMesh & mesh
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
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.
virtual void resetDesignVariables()
Reset to the starting point of line search.
#define DebugInfo
Report an information message using Foam::Info.
virtual void addFvOptions(const PtrList< primalSolver > &primalSolver, const PtrList< adjointSolverManager > &adjointSolverManagers)
Add fvOptions depending on the design variables.
bool valid() const noexcept
Identical to good(), or bool operator.
Definition: autoPtr.H:415
virtual const scalarField & getVars() const
Get the design variables.
label size() const noexcept
The number of elements in the container.
Definition: UList.H:671
defineRunTimeSelectionTable(reactionRateFlameArea, dictionary)
defineTypeNameAndDebug(combustionModel, 0)
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
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.
messageStream Info
Information stream (stdout output on master, null elsewhere)
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.
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:127