streamEntry.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2025 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Class
25  Foam::functionEntries::streamEntry
26 
27 Description
28  Compiles and executes C++ OpenFOAM code string expressions
29 
30  inserting the result into the dictionary or dictionary entry. The
31  functionality of \c #stream is equivalent to \c #codeStream but with a
32  simpler syntax, relying on separate \c #codeInclude entries to provide
33  required include files. For more details see
34  Foam::functionEntries::codeStream.
35 
36 Usage
37  Example to find the root of f(x) = x + B*x/sqrt(1 + sqr(x)) - A
38  using Newton-Raphson iteration
39 
40  \verbatim
41  A 1;
42  B 1;
43 
44  x #stream
45  #{
46  // Lookup the coefficients
47  const scalar A = $A;
48  const scalar B = $B;
49 
50  // Initial guess for x
51  scalar x = 0;
52 
53  scalar x0 = x;
54 
55  do
56  {
57  // Store the previous iteration x for the convergence check
58  x0 = x;
59 
60  // Temporary sub-function evaluations
61  const scalar f1 = 1 + sqr(x);
62  const scalar f2 = sqrt(f1);
63 
64  // Evaluate the function
65  const scalar f = x + B*x/f2 - A;
66 
67  // Evaluate the derivative
68  const scalar df = 1 + B/(f1*f2);
69 
70  // Update x
71  x = x0 - f/df;
72 
73  // Test for convergence
74  } while (mag(x - x0) > 1e-6);
75 
76  os << x;
77  #};
78  \endverbatim
79 
80 See also
81  Foam::functionEntries::codeIncludeEntry
82  Foam::functionEntries::codeStream
83  Foam::functionEntries::calc
84 
85 SourceFiles
86  streamEntry.C
87 
88 \*---------------------------------------------------------------------------*/
89 
90 #ifndef streamEntry_H
91 #define streamEntry_H
92 
93 #include "functionEntry.H"
94 #include "OTstream.H"
95 
96 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
97 
98 namespace Foam
99 {
100 namespace functionEntries
101 {
102 
103 // Forward declaration of friend classes
104 class codeBlockEntry;
105 
106 /*---------------------------------------------------------------------------*\
107  Class streamEntry Declaration
108 \*---------------------------------------------------------------------------*/
109 
110 class streamEntry
111 :
112  public functionEntry
113 {
114 protected:
115 
116  // Protected Member Functions
117 
118  //- Return the code string
119  static string codeString
120  (
121  const label index,
122  const dictionary& codeDict,
123  Istream&,
124  const string& startString = string::null,
125  const string& endString = string::null
126  );
127 
128  //- Perform the calculation and return the result in an OTstream
129  static OTstream resultStream
130  (
131  const dictionary& dict,
132  Istream& is,
133  const string& startString = string::null,
134  const string& endString = string::null
135  );
136 
137 
138 public:
139 
140  // Related types
141 
142  //- Declare friendship with the streamEntry class
143  friend class codeBlockEntry;
144 
145 
146  //- Runtime type information
147  FunctionTypeName("#stream");
148 
149 
150  // Constructors
151 
152  //- Construct from line number, dictionary and Istream
154  (
155  const label lineNumber,
156  const dictionary& parentDict,
157  Istream& is
158  );
159 
160  //- Copy construct
161  streamEntry(const streamEntry&) = default;
162 
163  //- Clone
164  virtual autoPtr<entry> clone(const dictionary&) const
165  {
166  return autoPtr<entry>(new streamEntry(*this));
167  }
168 
169 
170  // Member Functions
171 
172  //- Expand the functionEntry into the contextDict
173  virtual bool execute(dictionary& contextDict, Istream&);
174 
175  //- Expand the functionEntry into the contextEntry
176  static bool execute
177  (
178  const dictionary& contextDict,
179  primitiveEntry& contextEntry,
180  Istream&
181  );
182 
183 
184  // Member Operators
185 
186  //- Disallow default bitwise assignment
187  void operator=(const streamEntry&) = delete;
188 };
189 
190 
191 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
192 
193 } // End namespace functionEntries
194 } // End namespace Foam
195 
196 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
197 
198 #endif
199 
200 // ************************************************************************* //
label lineNumber() const
Return current stream line number.
Definition: IOstream.H:450
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
Output token stream.
Definition: OTstream.H:56
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: autoPtr.H:51
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
virtual autoPtr< entry > clone() const
Construct on freestore as copy.
Definition: entry.C:56
Part of the #codeBlock...#codeBlock clause.
Compiles and executes C++ OpenFOAM code string expressions.
Definition: codeDict.H:131
Compiles and executes C++ OpenFOAM code string expressions.
Definition: streamEntry.H:112
void operator=(const streamEntry &)=delete
Disallow default bitwise assignment.
static OTstream resultStream(const dictionary &dict, Istream &is, const string &startString=string::null, const string &endString=string::null)
Perform the calculation and return the result in an OTstream.
Definition: streamEntry.C:94
streamEntry(const label lineNumber, const dictionary &parentDict, Istream &is)
Construct from line number, dictionary and Istream.
Definition: streamEntry.C:156
FunctionTypeName("#stream")
Runtime type information.
static string codeString(const label index, const dictionary &codeDict, Istream &, const string &startString=string::null, const string &endString=string::null)
Return the code string.
Definition: streamEntry.C:57
virtual bool execute(dictionary &contextDict, Istream &)
Expand the functionEntry into the contextDict.
Definition: streamEntry.C:169
A functionEntry causes entries to be added/manipulated on the specified dictionary given an input str...
Definition: functionEntry.H:66
A keyword and a list of tokens is a 'primitiveEntry'. An primitiveEntry can be read,...
const dictionary & dict() const
This entry is not a dictionary,.
static const string null
An empty string.
Definition: string.H:88
Namespace for OpenFOAM.
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59