shapeOptimisationIncompressible.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-2020 PCOpt/NTUA
9  Copyright (C) 2013-2020 FOSS GP
10  Copyright (C) 2019-2020 OpenCFD Ltd.
11 -------------------------------------------------------------------------------
12 License
13  This file is part of OpenFOAM.
14 
15  OpenFOAM is free software: you can redistribute it and/or modify it
16  under the terms of the GNU General Public License as published by
17  the Free Software Foundation, either version 3 of the License, or
18  (at your option) any later version.
19 
20  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
21  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
22  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23  for more details.
24 
25  You should have received a copy of the GNU General Public License
26  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
27 
28 \*---------------------------------------------------------------------------*/
29 
32 
33 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
34 
35 namespace Foam
36 {
37 
38 namespace incompressible
39 {
40 
41 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
42 
43 defineTypeNameAndDebug(shapeOptimisation, 0);
45 (
49 );
50 
51 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
52 
54 {
55  // Communicate the movement to optMeshMovement
56  optMeshMovement_->setCorrection(correction);
57 
58  if (updateGeometry_)
59  {
60  // Update the mesh
61  optMeshMovement_->moveMesh();
62 
63  if (writeEachMesh_)
64  {
65  Info<< " Writing new mesh points " << endl;
67  (
68  IOobject
69  (
70  "points",
73  mesh_,
76  false
77  ),
78  mesh_.points()
79  );
80  points.write();
81  }
82  }
83 }
84 
85 
87 (
89 )
90 {
91  if (!updateMethod_->initialEtaSet())
92  {
93  // In the unlikely event that eta is not set and the line search step
94  // is not 1, multiply with it
95  // if (lineSearch_) correction *= lineSearch_->step();
96 
97  // Compute eta based on desirable mesh movement size
98  scalar eta = optMeshMovement_->computeEta(correction);
99  correction *= eta;
100 
101  // Update eta known by the optimisation method and inform it that is
102  // has been set
103  updateMethod_->setStep(eta);
104  updateMethod_->initialEtaSet() = true;
105 
106  // If a backtracking should be made at the first optimisation cycle,
107  // the direction of the subsequent line searches of the same cycle
108  // should also be scaled with the newly computed eta. We do this by
109  // changing the line search step. This will happen only at the first
110  // optimisation cycle since the updated value of eta will be included
111  // in the line search direction in all subsequent optimisation cycles
112  //correction *= eta;
113  }
114 }
115 
116 
117 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
118 
119 shapeOptimisation::shapeOptimisation
120 (
121  fvMesh& mesh,
122  const dictionary& dict,
124 )
125 :
126  optimisationType(mesh, dict, adjointSolverManagers),
127  optMeshMovement_(nullptr),
128  writeEachMesh_
129  (
130  dict.subDict("optimisationType").
131  getOrDefault<bool>("writeEachMesh", false)
132  ),
133  updateGeometry_
134  (
135  dict.subDict("optimisationType").
136  getOrDefault<bool>("updateGeometry", true)
137  )
138 {
139  // Note: to be updated
141  (
143  (
144  dict_.subDict("sensitivities").get<wordRes>("patches")
145  )
146  );
147  if (patches.empty())
148  {
150  << "There is no patch on which to compute sensitivities. "
151  << "Check optimisationDict \n"
152  << endl;
153  }
154  labelList sensitivityPatchIDs = patches.toc();
155  optMeshMovement_.reset
156  (
158  (
159  mesh_,
160  dict_.subDict("meshMovement"),
161  sensitivityPatchIDs
162  ).ptr()
163  );
164 
165  // Sanity checks: at least one of eta or maxAllowedDisplacement must be set
166  if
167  (
168  !updateMethod_->initialEtaSet()
169  && !optMeshMovement_().maxAllowedDisplacementSet()
170  )
171  {
173  << "Neither eta (updateMethod) "
174  << "nor maxAllowedDisplacement (meshMovement) have been set"
175  << nl
177  }
178 }
179 
180 
181 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
184 {
185  optMeshMovement_->storeDesignVariables();
186 }
187 
190 {
191  optMeshMovement_->resetDesignVariables();
192 }
193 
194 
196 {
198  updateMethod_->writeCorrection();
199 }
200 
201 
202 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
203 
204 } // End namespace incompressible
205 } // End namespace Foam
206 
207 // ************************************************************************* //
const polyBoundaryMesh & boundaryMesh() const
Return boundary mesh.
Definition: polyMesh.H:584
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
defineTypeNameAndDebug(adjointEikonalSolver, 0)
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:120
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:578
virtual void write()
Write useful quantities to files.
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:49
static word meshSubDir
Return the mesh sub-directory name (usually "polyMesh")
Definition: polyMesh.H:402
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:487
Shape optimisation support library.
Ignore writing from objectRegistry::writeObject()
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.
const dictionary & subDict(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Find and return a sub-dictionary.
Definition: dictionary.C:453
addToRunTimeSelectionTable(adjointSensitivity, sensitivityBezier, dictionary)
Macros for easy insertion into run-time selection tables.
Abstract base class for optimisation methods.
virtual const pointField & points() const
Return raw points.
Definition: polyMesh.C:1066
static autoPtr< optMeshMovement > New(fvMesh &mesh, const dictionary &dict, const labelList &patchIDs)
const fileName & pointsInstance() const
Return the current instance directory for points.
Definition: polyMesh.C:847
dynamicFvMesh & mesh
const pointField & points
virtual void updateDesignVariables(scalarField &correction)
Update the design variables given their correction.
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
A List of wordRe with additional matching capabilities.
Definition: wordRes.H:47
virtual void computeEta(scalarField &correction)
Compute eta if not set in the first step.
virtual void storeDesignVariables()
Store design variables, as the starting point for line search.
#define WarningInFunction
Report a warning using Foam::Warning.
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:79
const polyBoundaryMesh & patches
Nothing to be read.
virtual void resetDesignVariables()
Store design variables, as the starting point for line search.
messageStream Info
Information stream (stdout output on master, null elsewhere)
labelHashSet patchSet(const UList< wordRe > &patchNames, const bool warnNotFound=true, const bool useGroups=true) const
Return the set of patch IDs corresponding to the given names.
List< label > labelList
A List of labels.
Definition: List.H:62
PtrList< adjointSolverManager > & adjointSolverManagers
Definition: createFields.H:8
Defines the attributes of an object for which implicit objectRegistry management is supported...
Definition: IOobject.H:166
A primitive field of type <T> with automated input and output.
Namespace for OpenFOAM.
Calculates shape sensitivities using the adjoint approach, computes boundaryMesh movement and propaga...
virtual void write()
Write useful quantities to files.