morphingBoxConstraint.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) 2021-2023 PCOpt/NTUA
9  Copyright (C) 2021-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 "morphingBoxConstraint.H"
31 #include "createZeroField.H"
32 #include "IOmanip.H"
34 
35 // * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * * //
36 
37 namespace Foam
38 {
39  defineRunTimeSelectionTable(morphingBoxConstraint, dictionary);
41 }
42 
43 
44 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
45 
47 (
48  const scalarField& sens,
49  const word& solverName
50 )
51 {
52  if (Pstream::master())
53  {
54  OFstream derivFile
55  (derivativesFolder_/solverName + mesh_.time().timeName());
56 
57  unsigned int width = IOstream::defaultPrecision() + 7;
58  derivFile
59  << setw(width) << "#varID" << " "
60  << setw(width) << "adjointSensitivity"
61  << endl;
62 
63  const labelList& activeVars = designVariables_.activeDesignVariables();
64  forAll(activeVars, varI)
65  {
66  const label activeVarI = activeVars[varI];
67  derivFile
68  << setw(width) << activeVarI << " "
69  << setw(width) << sens[activeVarI] << endl;
70  }
71  }
72 }
73 
74 
75 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
76 
77 Foam::morphingBoxConstraint::morphingBoxConstraint
78 (
79  const fvMesh& mesh,
80  const dictionary& dict,
81  volumetricBSplinesDesignVariables& designVariables
82 )
83 :
84  mesh_(mesh),
85  dict_(dict),
86  designVariables_(designVariables),
87  volBSplinesBase_(designVariables.getVolBSplinesBase()),
88  initialCPs_(3*volBSplinesBase_.getTotalControlPointsNumber()),
89  initialiseVars_(true),
90  derivativesFolder_("optimisation"/type() + "Derivatives")
91 {
92  // Store initial control points
93  const PtrList<NURBS3DVolume>& boxes = volBSplinesBase_.boxes();
94  label varID(0);
95  for (const NURBS3DVolume& boxI : boxes)
96  {
97  const vectorField& cps = boxI.getControlPoints();
98  for (const vector& cpI : cps)
99  {
100  initialCPs_[varID++] = cpI.x();
101  initialCPs_[varID++] = cpI.y();
102  initialCPs_[varID++] = cpI.z();
103  }
104  }
105 
106  // Create sensitivities folder
108 }
109 
110 
111 // * * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * //
112 
114 (
115  const fvMesh& mesh,
116  const dictionary& dict,
118 )
119 {
120  const word modelType(dict.getOrDefault<word>("constraintType", "none"));
121 
122  Info<< "morphingBoxConstraint type : " << modelType << endl;
123 
124  auto cstrIter = dictionaryConstructorTablePtr_->cfind(modelType);
125 
126  if (!cstrIter.found())
127  {
129  (
130  "constraintType",
131  modelType,
132  *dictionaryConstructorTablePtr_
133  ) << exit(FatalError);
134  }
135 
136  return autoPtr<morphingBoxConstraint>
137  (
138  cstrIter()(mesh, dict, designVariables)
139  );
140 }
141 
142 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * //
143 
145 (
146  autoPtr<scalarField>& lowerBounds,
147  autoPtr<scalarField>& upperBounds
148 )
149 {
150  if (lowerBounds || upperBounds)
151  {
153  }
154 }
155 
156 
158 (
159  autoPtr<scalarField>& lowerBounds,
160  autoPtr<scalarField>& upperBounds
161 )
162 {
163  if (designVariables_.updateBounds() && (lowerBounds || upperBounds))
164  {
166  }
167 }
168 
169 
171 (
172  const scalarField& controlPointSens,
173  const word& adjointSolverName
174 )
175 {
176  // Sensitivities w.r.t. the design variables
177  auto tdvSens
178  (tmp<scalarField>::New(designVariables_.scalarField::size(), Zero));
179  scalarField& dvSens = tdvSens.ref();
180  computeDVsSensitivities(dvSens, controlPointSens);
181  writeDVSensitivities(dvSens, adjointSolverName);
182 
183  return tdvSens;
184 }
185 
186 
188 (
190  const scalar maxInitChange
191 )
192 {
193  vectorField cpMovement(designVariables_.controlPointMovement(correction));
194  const scalar maxDisplacement
195  (
196  volBSplinesBase_.computeMaxBoundaryDisplacement
197  (
198  cpMovement,
199  designVariables_.getPatches().toc()
200  )
201  );
202 
203  Info<< "maxAllowedDisplacement/maxDisplacement of boundary\t"
204  << maxInitChange << "/" << maxDisplacement << endl;
205  const scalar eta(maxInitChange/ maxDisplacement);
206 
207  Info<< "Setting eta value to " << eta << endl;
208  correction *= eta;
209 
210  return eta;
211 }
212 
213 
215 {
216  return true;
217 }
218 
219 
220 // ************************************************************************* //
virtual scalar computeEta(scalarField &correction, const scalar maxInitChange)
Compute eta if not set in the first step.
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
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
const fvMesh & mesh_
Mesh reference.
Output to file stream, using an OSstream.
Definition: OFstream.H:49
fileName derivativesFolder_
Folder holding the twist sensitivities.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
virtual void computeBounds(autoPtr< scalarField > &lowerBounds, autoPtr< scalarField > &upperBounds)
Transform bounds from control points to design variables.
static unsigned int defaultPrecision() noexcept
Return the default precision.
Definition: IOstream.H:423
virtual bool writeData(Ostream &os) const
Append useful information to the design variables IOdictionary.
const Time & time() const
Return the top-level database.
Definition: fvMesh.H:360
#define FatalErrorInLookup(lookupTag, lookupName, lookupTable)
Report an error message using Foam::FatalError.
Definition: error.H:605
volBSplinesBase & volBSplinesBase_
Easy access to the volBSplinesBase resting in the designVariables_.
Macros for easy insertion into run-time selection tables.
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:421
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: POSIX.C:799
virtual tmp< scalarField > postProcessSens(const scalarField &controlPointSens, const word &adjointSolverName)
Chain rule from control points to design variables.
dynamicFvMesh & mesh
bool mkDir(const fileName &pathName, mode_t mode=0777)
Make a directory and return an error if it could not be created.
Definition: POSIX.C:614
static autoPtr< morphingBoxConstraint > New(const fvMesh &mesh, const dictionary &dict, volumetricBSplinesDesignVariables &designVariables)
Construct and return the selected morphingBoxConstraint.
A class for handling words, derived from Foam::string.
Definition: word.H:63
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
const labelList & activeDesignVariables() const
Return list of active design variables.
const PtrList< NURBS3DVolume > & boxes() const
Get const reference to the vol. B-splines boxes.
static tmp< T > New(Args &&... args)
Construct tmp with forwarding arguments.
Definition: tmp.H:206
Vector< scalar > vector
Definition: vector.H:57
Volumetric B-Splines design variables for shape optimisation.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
Istream and Ostream manipulators taking arguments.
static word timeName(const scalar t, const int precision=precision_)
Return a time name for the given scalar time value formatted with the given precision.
Definition: Time.C:714
defineRunTimeSelectionTable(reactionRateFlameArea, dictionary)
OBJstream os(runTime.globalPath()/outputName)
defineTypeNameAndDebug(combustionModel, 0)
volumetricBSplinesDesignVariables & designVariables_
Reference to underlaying volumetric B-Splines morpher.
Abstract base class for defining constraints for the control points of volumetric B-Splines morphing ...
Abstract base class for defining design variables.
scalarField initialCPs_
Initial CPs stored in scalarField.
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:78
static bool master(const label communicator=worldComm)
True if process corresponds to the master rank in the communicator.
Definition: UPstream.H:1082
messageStream Info
Information stream (stdout output on master, null elsewhere)
virtual void updateBounds(autoPtr< scalarField > &lowerBounds, autoPtr< scalarField > &upperBounds)
Update the bounds of the design variables.
Field< vector > vectorField
Specialisation of Field<T> for vector.
virtual void writeDVSensitivities(const scalarField &sens, const word &name)
Write sensitivities w.r.t. the design variables.
Omanip< int > setw(const int i)
Definition: IOmanip.H:199
T getOrDefault(const word &keyword, const T &deflt, enum keyType::option matchOpt=keyType::REGEX) const
Find and return a T, or return the given default value. FatalIOError if it is found and the number of...
A class for managing temporary objects.
Definition: HashPtrTable.H:50
#define NotImplemented
Issue a FatalErrorIn for a function not currently implemented.
Definition: error.H:686
Namespace for OpenFOAM.
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:127