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 -------------------------------------------------------------------------------
11 License
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>
37 
38 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
39 
40 namespace Foam
41 {
42 namespace expressions
43 {
44  defineTypeName(exprResultDelayed);
45 
47  (
48  exprResult,
49  exprResultDelayed,
50  dictionary
51  );
53  (
54  exprResult,
55  exprResultDelayed,
56  empty
57  );
58 
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 (
78  const exprResultDelayed& rhs
79 )
80 :
81  exprResult(rhs),
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());
165 
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");
257  os << static_cast<const exprResult&>(*this);
258 
259  os.endBlock();
260 }
261 
262 
263 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
264 
265 void Foam::expressions::exprResultDelayed::operator=
266 (
267  const exprResultDelayed& rhs
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 
286 void Foam::expressions::exprResultDelayed::operator=
287 (
288  const exprResult& rhs
289 )
290 {
291  settingResult_ = rhs;
292 }
293 
294 
295 void Foam::expressions::exprResultDelayed::operator=
296 (
297  exprResult&& rhs
298 )
299 {
300  settingResult_ = std::move(rhs);
301 }
302 
303 
304 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
305 
306 Foam::Istream& Foam::operator>>
307 (
308  Istream& is,
309  expressions::exprResultDelayed& data
310 )
311 {
312  dictionary dict(is);
313 
315 
316  return is;
317 }
318 
319 
320 Foam::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 // ************************************************************************* //
List< ReturnType > get(const UPtrList< T > &list, const AccessOp &aop)
List of values generated by applying the access operation to each list item.
void setReadValue(const exprResult &val)
Set the readValue with a calculated value.
dictionary dict
bool updateReadValue(const scalar &timeVal)
Update the read-value.
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:129
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:598
A polymorphic field/result from evaluating an expression.
Definition: exprResult.H:121
Template class for non-intrusive linked lists.
Definition: LList.H:46
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
Macros for easy insertion into run-time selection tables.
void storeValue(const scalar &timeVal)
Add a stored value.
defineTypeName(fvExprDriverWriter)
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
A class for handling words, derived from Foam::string.
Definition: word.H:63
String literal.
Definition: keyType.H:82
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
OBJstream os(runTime.globalPath()/outputName)
An exprResult with an additional delay before evaluation.
labelList f(nPoints)
addToRunTimeSelectionTable(exprResult, exprResult, dictionary)
rAUs append(new volScalarField(IOobject::groupName("rAU", phase1.name()), 1.0/(U1Eqn.A()+byDt(max(phase1.residualAlpha() - alpha1, scalar(0)) *rho1))))
virtual bool isStream() const noexcept
Return true if this entry is a stream.
Definition: entry.H:273
virtual void operator=(const exprResult &rhs)
Copy assignment.
Definition: exprResult.C:467
virtual ITstream & stream() const =0
Return token stream, if entry is a primitive entry.
Namespace for OpenFOAM.
A keyword and a list of tokens is an &#39;entry&#39;.
Definition: entry.H:63