DMD.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) 2020-2021 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "DMD.H"
29 #include "DMDModel.H"
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36 namespace functionObjects
37 {
38  defineTypeNameAndDebug(DMD, 0);
39  addToRunTimeSelectionTable(functionObject, DMD, dictionary);
40 }
41 }
42 
43 
44 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
45 
46 void Foam::functionObjects::DMD::snapshot()
47 {
48  bool processed = false;
49  processed = processed || getSnapshot<scalar>();
50  processed = processed || getSnapshot<vector>();
51  processed = processed || getSnapshot<sphericalTensor>();
52  processed = processed || getSnapshot<symmTensor>();
53  processed = processed || getSnapshot<tensor>();
54 
55  if (!processed)
56  {
58  << " functionObjects::" << type() << " " << name() << ":"
59  << " cannot find required input field during snapshot loading: "
60  << fieldName_ << nl
61  << " Do you execute required functionObjects"
62  << " before executing DMD, e.g. mapFields?"
63  << exit(FatalError);
64  }
65 }
66 
67 
68 void Foam::functionObjects::DMD::initialise()
69 {
70  const label nComps = DMDModelPtr_->nComponents(fieldName_);
71 
72  if (patch_.empty())
73  {
74  nSnap_ = nComps*mesh_.nCells();
75  }
76  else
77  {
78  const label patchi = mesh_.boundaryMesh().findPatchID(patch_);
79 
80  if (patchi < 0)
81  {
83  << "Cannot find patch " << patch_
84  << exit(FatalError);
85  }
86 
87  nSnap_ = nComps*(mesh_.C().boundaryField()[patchi]).size();
88  }
89 
90  const label nSnapTotal = returnReduce(nSnap_, sumOp<label>());
91 
92  if (nSnapTotal <= 0)
93  {
95  << " # Zero-size input field = " << fieldName_ << " #"
96  << exit(FatalError);
97  }
98 
99  if (nSnap_ > 0)
100  {
101  z_ = RMatrix(2*nSnap_, 1, Zero);
102  }
103  else
104  {
105  z_ = RMatrix(1, 1, Zero);
106  }
107 }
108 
109 
110 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
111 
113 (
114  const word& name,
115  const Time& runTime,
116  const dictionary& dict
117 )
118 :
120  DMDModelPtr_(DMDModel::New(mesh_, name, dict)),
121  z_(),
122  fieldName_(dict.get<word>("field")),
123  patch_(dict.getOrDefault<word>("patch", word::null)),
124  nSnap_(0),
125  step_(0)
126 {
127  // Check if computation time-step is varying
128  if (runTime.isAdjustTimeStep())
129  {
131  << " # DMD: Available only for fixed time-step computations. #"
132  << endl;
133  }
134 
135  // Check if mesh topology is changing
136  if (mesh_.topoChanging())
137  {
139  << " # DMD: Available only for non-changing mesh topology. #"
140  << exit(FatalError);
141  }
142 
143  read(dict);
144 }
145 
146 
147 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
148 
150 {
151  Info<< type() << " " << name() << ":" << endl;
152 
153  if (fvMeshFunctionObject::read(dict) && DMDModelPtr_->read(dict))
154  {
155  return true;
156  }
157 
158  return false;
159 }
160 
161 
163 {
164  Log << type() << " " << name() << " execute:" << endl;
165 
166  snapshot();
167 
168  if (step_ == 1)
169  {
170  DMDModelPtr_->initialise(z_);
171  }
172 
173  if (step_ > 1)
174  {
175  DMDModelPtr_->update(z_);
176  }
177 
178  ++step_;
179 
180  Log << tab << "Execution index = " << step_ << " for field: " << fieldName_
181  << endl;
182 
183  return true;
184 }
185 
186 
188 {
189  if (postProcess)
190  {
191  return true;
192  }
193 
194  return end();
195 }
196 
197 
199 {
200  if (step_ == 0)
201  {
202  // Avoid double execution of write() when writeControl=onEnd
203  return true;
204  }
205 
206  Log << type() << " " << name() << " write:" << endl;
207 
208  if (step_ < 2)
209  {
211  << " # DMD needs at least three snapshots to produce output #"
212  << nl
213  << " # Only " << step_ + 1 << " snapshots are available #"
214  << nl
215  << " # Skipping DMD output calculation and write #"
216  << endl;
217 
218  return false;
219  }
220 
221  z_.clear();
222 
223  DMDModelPtr_->fit();
224 
225  mesh_.time().printExecutionTime(Info);
226 
227  // Restart the incremental orthonormal basis update
228  step_ = 0;
229 
230  return true;
231 }
232 
233 
234 // ************************************************************************* //
runTime
engineTime & runTime
Definition: createEngineTime.H:13
Log
#define Log
Definition: PDRblock.C:35
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:73
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::returnReduce
T returnReduce(const T &Value, const BinaryOp &bop, const int tag=Pstream::msgType(), const label comm=UPstream::worldComm)
Definition: PstreamReduceOps.H:94
Foam::Zero
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
Foam::read
bool read(const char *buf, int32_t &val)
Same as readInt32.
Definition: int32.H:108
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::functionObjects::fvMeshFunctionObject
Specialization of Foam::functionObject for an Foam::fvMesh, providing a reference to the Foam::fvMesh...
Definition: fvMeshFunctionObject.H:64
Foam::DMDModel::New
static autoPtr< DMDModel > New(const fvMesh &mesh, const word &name, const dictionary &dict)
Return a reference to the selected DMD model.
Definition: DMDModelNew.C:33
Foam::Info
messageStream Info
Information stream (stdout output on master, null elsewhere)
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:123
Foam::functionObjects::regionFunctionObject::read
virtual bool read(const dictionary &dict)
Read optional controls.
Definition: regionFunctionObject.C:173
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
stdFoam::end
constexpr auto end(C &c) -> decltype(c.end())
Return iterator to the end of the container c.
Definition: stdFoam.H:121
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::functionObjects::DMD::execute
virtual bool execute()
Execute DMD.
Definition: DMD.C:162
Foam::functionObjects::DMD::write
virtual bool write()
Write DMD results.
Definition: DMD.C:187
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::functionObject::name
const word & name() const noexcept
Return the name of this functionObject.
Definition: functionObject.C:143
Foam::tab
constexpr char tab
Definition: Ostream.H:403
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::functionObjects::addToRunTimeSelectionTable
addToRunTimeSelectionTable(functionObject, ObukhovLength, dictionary)
Foam::functionObject::type
virtual const word & type() const =0
Runtime type information.
Foam::functionObjects::DMD::end
virtual bool end()
Write DMD results.
Definition: DMD.C:198
Foam::type
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: MSwindows.C:590
Foam::functionObjects::defineTypeNameAndDebug
defineTypeNameAndDebug(ObukhovLength, 0)
Foam::word::null
static const word null
An empty word.
Definition: word.H:80
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::functionObjects::DMD::DMD
DMD(const word &name, const Time &runTime, const dictionary &dict)
Construct from Time and dictionary.
Definition: DMD.C:113
DMDModel.H
DMD.H
Foam::functionObjects::DMD::read
virtual bool read(const dictionary &dict)
Read DMD settings.
Definition: DMD.C:149
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:328