Loading...
Searching...
No Matches
exprResultDelayed.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) 2012-2018 Bernhard Gschaider
9 Copyright (C) 2019-2022 OpenCFD Ltd.
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
29#include "exprResultDelayed.H"
30#include "vector.H"
31#include "tensor.H"
32#include "symmTensor.H"
33#include "sphericalTensor.H"
35
36// #include <cassert>
38// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
40namespace Foam
41{
42namespace expressions
43{
47 (
51 );
53 (
56 empty
57 );
59} // End namespace expressions
60} // End namespace Foam
61
62
63// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
64
66:
67 exprResult(),
68 name_("none"),
69 startExpr_(),
70 settingResult_(),
71 storeInterval_(1),
72 delay_(10)
73{}
74
75
77(
79)
80:
82 name_(rhs.name_),
83 startExpr_(rhs.startExpr_),
84 settingResult_(rhs.settingResult_),
85 storedValues_(rhs.storedValues_),
86 storeInterval_(rhs.storeInterval_),
87 delay_(rhs.delay_)
88{}
89
90
92(
93 const dictionary& dict
94)
95:
96 exprResult(dict.subOrEmptyDict("value")),
97 name_(dict.get<word>("name")),
98 startExpr_("startupValue", dict),
99 storeInterval_(dict.get<scalar>("storeInterval")),
100 delay_(dict.get<scalar>("delay"))
101{
102 const entry *eptr = dict.findEntry("storedValues", keyType::LITERAL);
103
104 if (eptr && eptr->isStream())
105 {
106 storedValues_ = DLList<ValueAtTime>(eptr->stream());
107 }
108}
109
110
111// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
112
114(
115 const scalar& timeVal
116)
117{
118 if (storedValues_.empty())
119 {
120 return false;
121 }
122
123 if (storedValues_.front().first() > (timeVal-delay_))
124 {
125 // No matching data yet
126 return false;
127 }
128
129 if (storedValues_.size() <= 1)
130 {
132 << "Only one stored value at time " << timeVal
133 << " for delayedVariable " << name() << nl
134 << "Check the values for the interval " << storeInterval_
135 << " and delay " << delay_ << nl
136 << "Probably the interval is too large" << nl << endl
137 << exit(FatalError);
138 }
139
140 auto current = storedValues_.cbegin();
141 auto next = current;
142 ++next;
143
144 // The time without the delay offset
145 const scalar newTime = (timeVal - delay_);
146
147 while (next != storedValues_.end())
148 {
149 if (newTime >= current().first() && newTime <= next().first())
150 {
151 break;
152 }
153
154 current = next;
155 ++next;
156 }
157
158 const scalar f =
159 (
160 (newTime - current().first())
161 / (next().first() - current().first())
162 );
163
164 exprResult val((1-f)*current().second() + f*next().second());
166 setReadValue(val);
167
168 return true;
169}
170
171
173(
174 const exprResult& val
175)
176{
178}
179
180
182(
183 const scalar& currTime
184)
185{
186 bool append = storedValues_.empty();
187
188 if (!append)
189 {
190 const scalar lastTime = storedValues_.back().first();
191
192 if (lastTime + SMALL >= currTime)
193 {
194 // Times are essentially identical - replace value
195 }
196 else if ((currTime - lastTime) >= 0.999*storeInterval_)
197 {
198 append = true;
199 }
200 else
201 {
202 // Cannot store in the middle - abandon the attempt
203 return;
204 }
205 }
206
207 if (append)
208 {
209 // Append value
210
211 const scalar oldLastTime =
212 (
213 storedValues_.empty()
214 ? 0
215 : storedValues_.back().first()
216 );
217
218 storedValues_.push_back(ValueAtTime(currTime, settingResult_));
219
220 while
221 (
222 storedValues_.size() > 1
223 && (oldLastTime - storedValues_.front().first()) >= delay_
224 )
225 {
226 // Remove values that are older than delay_
227 storedValues_.pop_front();
228 }
229 }
230 else
231 {
232 // Replace value
233
234 storedValues_.back().second() = settingResult_;
235 }
236}
237
238
240{
241 os.beginBlock();
242
243 os.writeEntry("name", name_);
244
245 os.writeEntry("startupValue", startExpr_);
246
247 if (!settingResult_.valueType().empty())
248 {
249 os.writeEntry("settingResult", settingResult_);
250 }
251
252 os.writeEntry("storedValues", storedValues_);
253 os.writeEntry("storeInterval", storeInterval_);
254 os.writeEntry("delay", delay_);
255
256 os.writeKeyword("value");
259 os.endBlock();
260}
261
262
263// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
264
265void Foam::expressions::exprResultDelayed::operator=
266(
268)
269{
270 if (this == &rhs)
271 {
272 return; // Self-assignment is a no-op
273 }
274
276
277 name_ = rhs.name_;
278 startExpr_ = rhs.startExpr_;
279 settingResult_ = rhs.settingResult_;
280 storedValues_ = rhs.storedValues_;
281 storeInterval_ = rhs.storeInterval_;
282 delay_ = rhs.delay_;
283}
284
285
286void Foam::expressions::exprResultDelayed::operator=
287(
289)
290{
291 settingResult_ = rhs;
292}
293
294
295void Foam::expressions::exprResultDelayed::operator=
296(
298)
300 settingResult_ = std::move(rhs);
301}
302
303
304// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
305
306Foam::Istream& Foam::operator>>
307(
308 Istream& is,
309 expressions::exprResultDelayed& data
310)
311{
312 dictionary dict(is);
315
316 return is;
317}
318
319
320Foam::Ostream& Foam::operator<<
321(
322 Ostream& os,
323 const expressions::exprResultDelayed& data
324)
325{
326 data.writeDict(os);
327 return os;
328}
329
330
331// ************************************************************************* //
Macros for easy insertion into run-time selection tables.
#define addToRunTimeSelectionTable(baseType, thisType, argNames)
Add to construction table with typeName as the key.
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
A keyword and a list of tokens is an 'entry'.
Definition entry.H:66
virtual bool isStream() const noexcept
True if this entry is a stream.
Definition entry.H:267
An exprResult with an additional delay before evaluation.
void storeValue(const scalar &timeVal)
Add a stored value.
bool updateReadValue(const scalar &timeVal)
Update the read-value.
void setReadValue(const exprResult &val)
Set the readValue with a calculated value.
A polymorphic field/result from evaluating an expression.
Definition exprResult.H:122
virtual void operator=(const exprResult &rhs)
Copy assignment.
Definition exprResult.C:467
exprResult()
Default construct.
Definition exprResult.C:173
@ LITERAL
String literal.
Definition keyType.H:82
A class for handling words, derived from Foam::string.
Definition word.H:66
#define defineTypeName(Type)
Define the typeName.
Definition className.H:113
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition error.H:600
OBJstream os(runTime.globalPath()/outputName)
auto & name
rAUs append(new volScalarField(IOobject::groupName("rAU", phase1.name()), 1.0/(U1Eqn.A()+byDt(max(phase1.residualAlpha() - alpha1, scalar(0)) *rho1))))
A namespace for expression-related classes/traits etc.
Namespace for OpenFOAM.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
void rhs(fvMatrix< typename Expr::value_type > &m, const Expr &expression)
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition errorManip.H:125
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
labelList f(nPoints)
dictionary dict