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 
31 
32 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36 
37 namespace incompressible
38 {
39 
40 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
41 
42 defineTypeNameAndDebug(shapeOptimisation, 0);
44 (
45  optimisationType,
46  shapeOptimisation,
47  dictionary
48 );
49 
50 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
51 
53 {
54  // Communicate the movement to optMeshMovement
55  optMeshMovement_->setCorrection(correction);
56 
57  if (updateGeometry_)
58  {
59  // Update the mesh
60  optMeshMovement_->moveMesh();
61 
62  if (writeEachMesh_)
63  {
64  Info<< " Writing new mesh points " << endl;
66  (
67  IOobject
68  (
69  "points",
72  mesh_,
75  false
76  ),
77  mesh_.points()
78  );
79  points.write();
80  }
81  }
82 }
83 
84 
86 (
88 )
89 {
90  if (!updateMethod_->initialEtaSet())
91  {
92  // In the unlikely event that eta is not set and the line search step
93  // is not 1, multiply with it
94  // if (lineSearch_) correction *= lineSearch_->step();
95 
96  // Compute eta based on desirable mesh movement size
97  scalar eta = optMeshMovement_->computeEta(correction);
98  correction *= eta;
99 
100  // Update eta known by the optimisation method and inform it that is
101  // has been set
102  updateMethod_->setStep(eta);
103  updateMethod_->initialEtaSet() = true;
104 
105  // If a backtracking should be made at the first optimisation cycle,
106  // the direction of the subsequent line searches of the same cycle
107  // should also be scaled with the newly computed eta. We do this by
108  // changing the line search step. This will happen only at the first
109  // optimisation cycle since the updated value of eta will be included
110  // in the line search direction in all subsequent optimisation cycles
111  //correction *= eta;
112  }
113 }
114 
115 
116 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
117 
118 shapeOptimisation::shapeOptimisation
119 (
120  fvMesh& mesh,
121  const dictionary& dict,
123 )
124 :
126  optMeshMovement_(nullptr),
127  writeEachMesh_
128  (
129  dict.subDict("optimisationType").
130  getOrDefault<bool>("writeEachMesh", false)
131  ),
132  updateGeometry_
133  (
134  dict.subDict("optimisationType").
135  getOrDefault<bool>("updateGeometry", true)
136  )
137 {
138  // Note: to be updated
140  (
141  mesh_.boundaryMesh().patchSet
142  (
143  dict_.subDict("sensitivities").get<wordRes>("patches")
144  )
145  );
146  if (patches.empty())
147  {
149  << "There is no patch on which to compute sensitivities. "
150  << "Check optimisationDict \n"
151  << endl;
152  }
153  labelList sensitivityPatchIDs = patches.toc();
154  optMeshMovement_.reset
155  (
157  (
158  mesh_,
159  dict_.subDict("meshMovement"),
160  sensitivityPatchIDs
161  ).ptr()
162  );
163 
164  // Sanity checks: at least one of eta or maxAllowedDisplacement must be set
165  if
166  (
167  !updateMethod_->initialEtaSet()
168  && !optMeshMovement_().maxAllowedDisplacementSet()
169  )
170  {
172  << "Neither eta (updateMethod) "
173  << "nor maxAllowedDisplacement (meshMovement) have been set"
174  << nl
175  << exit(FatalError);
176  }
177 }
178 
179 
180 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
181 
183 {
184  optMeshMovement_->storeDesignVariables();
185 }
186 
187 
189 {
190  optMeshMovement_->resetDesignVariables();
191 }
192 
193 
195 {
197  updateMethod_->writeCorrection();
198 }
199 
200 
201 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
202 
203 } // End namespace incompressible
204 } // End namespace Foam
205 
206 // ************************************************************************* //
Foam::IOobject::NO_WRITE
Definition: IOobject.H:130
Foam::polyMesh::points
virtual const pointField & points() const
Return raw points.
Definition: polyMesh.C:1069
Foam::incompressible::shapeOptimisation::storeDesignVariables
virtual void storeDesignVariables()
Store design variables, as the starting point for line search.
Definition: shapeOptimisationIncompressible.C:182
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:104
Foam::IOField
A primitive field of type <T> with automated input and output.
Definition: foamVtkLagrangianWriter.H:61
Foam::incompressible::defineTypeNameAndDebug
defineTypeNameAndDebug(adjointEikonalSolver, 0)
Foam::incompressible::optimisationType::write
virtual void write()
Write useful quantities to files.
Definition: optimisationTypeIncompressible.C:322
Foam::optMeshMovement::New
static autoPtr< optMeshMovement > New(fvMesh &mesh, const dictionary &dict, const labelList &patchIDs)
Definition: optMeshMovement.C:93
Foam::polyMesh::meshSubDir
static word meshSubDir
Return the mesh sub-directory name (usually "polyMesh")
Definition: polyMesh.H:321
Foam::incompressible::optimisationType
Abstract base class for optimisation methods.
Definition: optimisationTypeIncompressible.H:58
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
Foam::correction
tmp< fvMatrix< Type > > correction(const fvMatrix< Type > &)
Foam::HashSet< label, Hash< label > >
Foam::incompressible::shapeOptimisation::optMeshMovement_
autoPtr< optMeshMovement > optMeshMovement_
Definition: shapeOptimisationIncompressible.H:70
Foam::incompressible::shapeOptimisation::updateGeometry_
bool updateGeometry_
Definition: shapeOptimisationIncompressible.H:73
Foam::polyMesh::pointsInstance
const fileName & pointsInstance() const
Return the current instance directory for points.
Definition: polyMesh.C:846
Foam::incompressible::shapeOptimisation::write
virtual void write()
Write useful quantities to files.
Definition: shapeOptimisationIncompressible.C:194
Foam::incompressible::addToRunTimeSelectionTable
addToRunTimeSelectionTable(adjointSensitivity, sensitivityBezier, dictionary)
Foam::Field< scalar >
Foam::Info
messageStream Info
Information stream (uses stdout - output is on the master only)
adjointSolverManagers
PtrList< adjointSolverManager > & adjointSolverManagers
Definition: createFields.H:8
Foam::PtrList
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition: List.H:62
Foam::incompressible::optimisationType::mesh_
fvMesh & mesh_
Definition: optimisationTypeIncompressible.H:64
Foam::incompressible::shapeOptimisation::resetDesignVariables
virtual void resetDesignVariables()
Store design variables, as the starting point for line search.
Definition: shapeOptimisationIncompressible.C:188
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
Foam::fvMesh
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:83
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::incompressible::shapeOptimisation::updateDesignVariables
virtual void updateDesignVariables(scalarField &correction)
Update the design variables given their correction.
Definition: shapeOptimisationIncompressible.C:52
Foam::incompressible::shapeOptimisation::writeEachMesh_
bool writeEachMesh_
Definition: shapeOptimisationIncompressible.H:72
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
shapeOptimisationIncompressible.H
Shape optimisation support library.
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:381
Foam::incompressible::optimisationType::updateMethod_
autoPtr< updateMethod > updateMethod_
Definition: optimisationTypeIncompressible.H:67
Foam::nl
constexpr char nl
Definition: Ostream.H:385
Foam::List< label >
points
const pointField & points
Definition: gmvOutputHeader.H:1
Foam::wordRes
A List of wordRe with additional matching capabilities.
Definition: wordRes.H:51
patches
const polyBoundaryMesh & patches
Definition: convertProcessorPatches.H:65
Foam::incompressible::shapeOptimisation::computeEta
virtual void computeEta(scalarField &correction)
Compute eta if not set in the first step.
Definition: shapeOptimisationIncompressible.C:86
Foam::IOobject::NO_READ
Definition: IOobject.H:123
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:303