optMeshMovement.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-2019 PCOpt/NTUA
9  Copyright (C) 2013-2019 FOSS GP
10  Copyright (C) 2019-2021 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 
30 #include "optMeshMovement.H"
31 #include "cellQuality.H"
32 #include "createZeroField.H"
33 
34 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35 
36 namespace Foam
37 {
40 }
41 
42 
43 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
44 
46 {
47  if (!maxAllowedDisplacement_)
48  {
50  << "maxAllowedDisplacement requested but not set" << nl
51  << exit(FatalError);
52  }
53 
54  return maxAllowedDisplacement_();
55 }
56 
57 
58 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
59 
60 Foam::optMeshMovement::optMeshMovement
61 (
62  fvMesh& mesh,
63  const dictionary& dict,
64  const labelList& patchIDs
65 )
66 :
67  maxAllowedDisplacement_(nullptr),
68  mesh_(mesh),
69  dict_(dict),
70  correction_(0),
71  patchIDs_(patchIDs),
72  pointsInit_(mesh.points()),
73  displMethodPtr_(displacementMethod::New(mesh_, patchIDs_)),
74  writeMeshQualityMetrics_
75  (
76  dict.getOrDefault("writeMeshQualityMetrics", false)
77  )
78 {
79  // Set maxAllowedDisplacement if provided
80  if (dict.found("maxAllowedDisplacement"))
81  {
82  maxAllowedDisplacement_.reset
83  (
84  new scalar(dict.get<scalar>("maxAllowedDisplacement"))
85  );
86  }
87 }
88 
89 
90 // * * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * //
91 
93 (
94  fvMesh& mesh,
95  const dictionary& dict,
96  const labelList& patchIDs
97 )
98 {
99  const word modelType(dict.get<word>("type"));
100 
101  Info<< "optMeshMovement type : " << modelType << endl;
102 
103  auto* ctorPtr = dictionaryConstructorTable(modelType);
104 
105  if (!ctorPtr)
106  {
108  (
109  dict,
110  "type",
111  modelType,
112  *dictionaryConstructorTablePtr_
113  ) << exit(FatalIOError);
114  }
116  return autoPtr<optMeshMovement>(ctorPtr(mesh, dict, patchIDs));
117 }
118 
119 
120 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * //
123 {
124  correction_ = correction;
125 }
126 
127 
129 {
130  // Move mesh
131  displMethodPtr_->update();
132 
133  // Check mesh quality
134  mesh_.checkMesh(true);
136  // If needed, plot mesh quality metrics
137  writeMeshQualityMetrics();
138 }
139 
140 
143 {
144  return displMethodPtr_;
145 }
146 
149 {
150  return patchIDs_;
151 }
152 
153 
155 {
156  if (writeMeshQualityMetrics_)
157  {
158  cellQuality cellQualityEngine(mesh_);
159  tmp<scalarField> cellNonOrtho(cellQualityEngine.nonOrthogonality());
160  tmp<scalarField> cellSkewness(cellQualityEngine.skewness());
161  Info<< "Average, Max cell non - orthogonality "
162  << gAverage(cellNonOrtho())
163  << " " << gMax(cellNonOrtho()) << endl;
164  Info<< "Average, Max cell skewness " << gAverage(cellSkewness())
165  << " " << gMax(cellSkewness()) << endl;
166  autoPtr<volScalarField> nonOrthoPtr
167  (
168  createZeroFieldPtr<scalar>(mesh_, "nonOrtho", dimless)
169  );
170  autoPtr<volScalarField> skewnessPtr
171  (
172  createZeroFieldPtr<scalar>(mesh_, "skewness", dimless)
173  );
174  nonOrthoPtr().primitiveFieldRef() = cellNonOrtho();
175  skewnessPtr().primitiveFieldRef() = cellSkewness();
176  nonOrthoPtr().write();
177  skewnessPtr().write();
178  }
179 }
180 
183 {
184  pointsInit_ = mesh_.points();
185 }
186 
187 
189 {
190  Info<< "optMeshMovement:: resetting mesh points" << endl;
191  mesh_.movePoints(pointsInit_);
192 }
193 
196 {
197  return maxAllowedDisplacement_.valid();
198 }
199 
200 
202 {
204  return labelList(0);
205 }
206 
207 
208 // ************************************************************************* //
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
const labelList patchIDs(pbm.patchSet(polyPatchNames, false, true).sortedToc())
Class calculates cell quality measures.
Definition: cellQuality.H:47
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
bool maxAllowedDisplacementSet() const
Whether maxAllowedDisplacement has been set.
Abstract base class for translating an update of the design variables into mesh movement.
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:49
virtual void resetDesignVariables()
Reset to starting point of line search.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:487
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tf1, const word &name, const dimensionSet &dimensions, const bool initCopy=false)
Global function forwards to reuseTmpDimensionedField::New.
virtual labelList getActiveDesignVariables() const
Return active design variables.
const dimensionSet dimless
Dimensionless.
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.
virtual void storeDesignVariables()
Store design variables and mesh, to act as the starting point of line search.
void reset(T *p=nullptr) noexcept
Delete managed object and set to new given pointer.
Definition: autoPtrI.H:37
static autoPtr< optMeshMovement > New(fvMesh &mesh, const dictionary &dict, const labelList &patchIDs)
dynamicFvMesh & mesh
const pointField & points
autoPtr< displacementMethod > & returnDisplacementMethod()
Return displacementMethod.
Abstract base class for displacement methods, which are a set or wrapper classes allowing to change t...
defineRunTimeSelectionTable(reactionRateFlameArea, dictionary)
Type gMax(const FieldField< Field, Type > &f)
defineTypeNameAndDebug(combustionModel, 0)
Type gAverage(const FieldField< Field, Type > &f)
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:79
messageStream Info
Information stream (stdout output on master, null elsewhere)
virtual void moveMesh()
Calculates mesh movemnt based on the correction of the design variables.
List< label > labelList
A List of labels.
Definition: List.H:62
A class for managing temporary objects.
Definition: HashPtrTable.H:50
const labelList & getPatchIDs()
Return patchIDs.
#define NotImplemented
Issue a FatalErrorIn for a function not currently implemented.
Definition: error.H:666
#define FatalIOErrorInLookup(ios, lookupTag, lookupName, lookupTable)
Report an error message using Foam::FatalIOError.
Definition: error.H:615
void setCorrection(const scalarField &correction)
Set design variable correction.
scalar getMaxAllowedDisplacement() const
Get maxAllowedDisplacement, is set.
Namespace for OpenFOAM.
void writeMeshQualityMetrics()
Write mesh quality metrics.
IOerror FatalIOError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL IO ERROR&#39; header text and ...