Loading...
Searching...
No Matches
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-------------------------------------------------------------------------------
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#include "createZeroField.H"
32#include "IOmanip.H"
34
35// * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * * //
36
37namespace Foam
38{
39 defineRunTimeSelectionTable(morphingBoxConstraint, dictionary);
40 defineTypeNameAndDebug(morphingBoxConstraint, 0);
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
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
77Foam::morphingBoxConstraint::morphingBoxConstraint
78(
79 const fvMesh& mesh,
80 const dictionary& dict,
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* ctorPtr = dictionaryConstructorTable(modelType);
125
126 if (!ctorPtr)
127 {
129 (
130 "constraintType",
131 modelType,
132 *dictionaryConstructorTablePtr_
133 ) << exit(FatalError);
134 }
135
136 return autoPtr<morphingBoxConstraint>
137 (
139 );
140}
141
142
143// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * //
144
146(
147 autoPtr<scalarField>& lowerBounds,
148 autoPtr<scalarField>& upperBounds
149)
150{
151 if (lowerBounds || upperBounds)
152 {
154 }
155}
156
157
159(
160 autoPtr<scalarField>& lowerBounds,
161 autoPtr<scalarField>& upperBounds
162)
163{
164 if (designVariables_.updateBounds() && (lowerBounds || upperBounds))
165 {
167 }
168}
169
170
172(
173 const scalarField& controlPointSens,
174 const word& adjointSolverName
175)
176{
177 // Sensitivities w.r.t. the design variables
178 auto tdvSens
179 (tmp<scalarField>::New(designVariables_.scalarField::size(), Zero));
180 scalarField& dvSens = tdvSens.ref();
181 computeDVsSensitivities(dvSens, controlPointSens);
182 writeDVSensitivities(dvSens, adjointSolverName);
183
184 return tdvSens;
185}
186
187
189(
191 const scalar maxInitChange
192)
193{
194 vectorField cpMovement(designVariables_.controlPointMovement(correction));
195 const scalar maxDisplacement
196 (
197 volBSplinesBase_.computeMaxBoundaryDisplacement
198 (
199 cpMovement,
200 designVariables_.getPatches().toc()
201 )
202 );
203
204 Info<< "maxAllowedDisplacement/maxDisplacement of boundary\t"
205 << maxInitChange << "/" << maxDisplacement << endl;
206 const scalar eta(maxInitChange/ maxDisplacement);
207
208 Info<< "Setting eta value to " << eta << endl;
209 correction *= eta;
210
211 return eta;
212}
213
214
216{
217 return true;
218}
219
220
221// ************************************************************************* //
Istream and Ostream manipulators taking arguments.
Macros for easy insertion into run-time selection tables.
static unsigned int defaultPrecision() noexcept
Return the default precision.
Definition IOstream.H:437
NURBS3DVolume morpher. Includes support functions for gradient computations Base class providing supp...
Output to file stream as an OSstream, normally using std::ofstream for the actual output.
Definition OFstream.H:75
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition PtrList.H:67
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
static bool master(const label communicator=worldComm)
True if process corresponds to the master rank in the communicator.
Definition UPstream.H:1714
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.
const labelList & activeDesignVariables() const
Return list of active design variables.
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
Mesh data needed to do the Finite Volume discretisation.
Definition fvMesh.H:85
const Time & time() const
Return the top-level database.
Definition fvMesh.H:360
Abstract base class for defining constraints for the control points of volumetric B-Splines morphing ...
virtual tmp< scalarField > postProcessSens(const scalarField &controlPointSens, const word &adjointSolverName)
Chain rule from control points to design variables.
volumetricBSplinesDesignVariables & designVariables_
Reference to underlaying volumetric B-Splines morpher.
virtual void computeDVsSensitivities(scalarField &dvSens, const scalarField &cpSens)=0
Compute sensitivities wrt the design variables (chain rule).
const fvMesh & mesh_
Mesh reference.
virtual scalar computeEta(scalarField &correction, const scalar maxInitChange)
Compute eta if not set in the first step.
virtual void computeBounds(autoPtr< scalarField > &lowerBounds, autoPtr< scalarField > &upperBounds)
Transform bounds from control points to design variables.
static autoPtr< morphingBoxConstraint > New(const fvMesh &mesh, const dictionary &dict, volumetricBSplinesDesignVariables &designVariables)
Construct and return the selected morphingBoxConstraint.
virtual bool writeData(Ostream &os) const
Append useful information to the design variables IOdictionary.
fileName derivativesFolder_
Folder holding the twist sensitivities.
scalarField initialCPs_
Initial CPs stored in scalarField.
virtual void updateBounds(autoPtr< scalarField > &lowerBounds, autoPtr< scalarField > &upperBounds)
Update the bounds of the design variables.
bool initialiseVars_
Initialise the design variables.
const dictionary dict_
Volumetric B-Splines variables dict.
virtual void writeDVSensitivities(const scalarField &sens, const word &name)
Write sensitivities w.r.t. the design variables.
volBSplinesBase & volBSplinesBase_
Easy access to the volBSplinesBase resting in the designVariables_.
A class for managing temporary objects.
Definition tmp.H:75
static tmp< T > New(Args &&... args)
Construct tmp with forwarding arguments.
Definition tmp.H:215
Volumetric B-Splines design variables for shape optimisation.
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 NotImplemented
Issue a FatalErrorIn for a function not currently implemented.
Definition error.H:688
OBJstream os(runTime.globalPath()/outputName)
Namespace for OpenFOAM.
List< label > labelList
A List of labels.
Definition List.H:62
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:616
messageStream Info
Information stream (stdout output on master, null elsewhere).
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
Omanip< int > setw(const int i)
Definition IOmanip.H:199
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
Field< vector > vectorField
Specialisation of Field<T> for vector.
static constexpr const zero Zero
Global zero (0).
Definition zero.H:127
tmp< fvMatrix< Type > > correction(const fvMatrix< Type > &)
Return the correction form of the given matrix by subtracting the matrix multiplied by the current fi...
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...
Vector< scalar > vector
Definition vector.H:57
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
#define forAll(list, i)
Loop across all elements in list.
Definition stdFoam.H:299