GeometricFieldSources.C
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) 2023-2026 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 \*---------------------------------------------------------------------------*/
25 
26 #include "GeometricFieldSources.H"
27 #include "error.H"
28 
29 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
30 
31 template<class Type, class GeoMesh, template<class> class PrimitiveField>
34 :
36  errorLocation_()
37 {}
38 
39 
40 template<class Type, class GeoMesh, template<class> class PrimitiveField>
43 (
45  const HashPtrTable<Source>& mtf
46 )
47 :
48  HashPtrTable<Source>(mtf.capacity()),
49  errorLocation_()
50 {
51  forAllConstIter(typename HashPtrTable<Source>, mtf, iter)
52  {
53  this->set(iter.key(), iter()->clone(iF).ptr());
54  }
55 }
56 
57 
58 template<class Type, class GeoMesh, template<class> class PrimitiveField>
61 (
64 )
65 :
66  HashPtrTable<Source>(mtf.capacity()),
67  errorLocation_(mtf.errorLocation_)
68 {
69  forAllConstIter(typename HashPtrTable<Source>, mtf, iter)
70  {
71  this->set(iter.key(), iter()->clone(iF).ptr());
72  }
73 }
74 
75 
76 template<class Type, class GeoMesh, template<class> class PrimitiveField>
77 template<template<class> class PrimitiveField2>
80 (
83 )
84 :
85  HashPtrTable<Source>(mtf.capacity()),
86  errorLocation_(mtf.errorLocation_)
87 {
88  forAllConstIter(typename HashPtrTable<Source>, mtf, iter)
89  {
90  this->set(iter.key(), iter()->clone(iF).ptr());
91  }
92 }
93 
94 
95 template<class Type, class GeoMesh, template<class> class PrimitiveField>
98 (
100  const dictionary& dict
101 )
102 :
103  HashPtrTable<Source>(),
104  errorLocation_()
105 {
106  readField(iF, dict);
107 }
108 
109 
110 template<class Type, class GeoMesh, template<class> class PrimitiveField>
113 (
115  const HashTable<word>& types,
116  const IOerrorLocation& errorLocation
117 )
118 :
119  HashPtrTable<Source>(),
120  errorLocation_(errorLocation)
121 {
122  forAllConstIter(typename HashTable<word>, types, iter)
123  {
124  this->set
125  (
126  iter.key(),
127  Source::New(iter(), iF).ptr()
128  );
129  }
130 }
131 
132 
133 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
134 
135 template<class Type, class GeoMesh, template<class> class PrimitiveField>
138 {}
139 
140 
141 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
142 
143 template<class Type, class GeoMesh, template<class> class PrimitiveField>
144 const Foam::HashPtrTable
145 <
147 >&
149 {
150  return *this;
151 }
152 
153 
154 template<class Type, class GeoMesh, template<class> class PrimitiveField>
156 <
158 >&
160 {
161  return *this;
162 }
163 
164 
165 template<class Type, class GeoMesh, template<class> class PrimitiveField>
168 const
169 {
170  return errorLocation_;
171 }
172 
173 
174 template<class Type, class GeoMesh, template<class> class PrimitiveField>
177 {
178  HashTable<word> result;
179 
180  forAllConstIter(typename HashPtrTable<Source>, *this, iter)
181  {
182  result.insert(iter.key(), iter()->type());
183  }
184 
185  return result;
186 }
187 
188 
189 template<class Type, class GeoMesh, template<class> class PrimitiveField>
191 (
193  const dictionary& dict
194 )
195 {
196  this->clear();
197 
198  errorLocation_ = IOerrorLocation(dict);
199 
201  {
202  if (iter().isDict())
203  {
204  this->set
205  (
206  iter().keyword(),
207  Source::New(field, iter().dict()).ptr()
208  );
209  }
210  }
211 }
212 
213 
214 template<class Type, class GeoMesh, template<class> class PrimitiveField>
216 (
219 )
220 {
221  this->clear();
222 
223  errorLocation_ = mtf.errorLocation_;
224 
225  forAllConstIter(typename HashPtrTable<Source>, mtf, iter)
226  {
227  this->set(iter.key(), iter()->clone(field).ptr());
228  }
229 }
230 
231 
232 template<class Type, class GeoMesh, template<class> class PrimitiveField>
234 (
235  const word& keyword,
236  Ostream& os
237 ) const
238 {
239  os << keyword << nl << token::BEGIN_BLOCK << incrIndent << nl;
240 
241  forAllConstIter(typename HashPtrTable<Source>, *this, iter)
242  {
243  os << indent << iter.key() << nl
244  << indent << token::BEGIN_BLOCK << nl
245  << incrIndent << *iter() << decrIndent
246  << indent << token::END_BLOCK << endl;
247  }
248 
249  os << decrIndent << token::END_BLOCK << endl;
250 
251  // Check state of IOstream
252  os.check
253  (
254  "GeometricFieldSources<Type, GeoMesh, PrimitiveField>::"
255  "writeEntry(const word& keyword, Ostream& os) const"
256  );
257 }
258 
259 
260 // * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * * //
261 
262 template<class Type, class GeoMesh, template<class> class PrimitiveField>
263 const typename
266 (
267  const word& sourceName
268 ) const
269 {
270  typename HashPtrTable<Source>::const_iterator iter = this->find(sourceName);
271 
272  if (iter == this->end())
273  {
274  FatalIOErrorInFunction(errorLocation_)
275  << "Cannot find fieldSource entry for " << sourceName
276  << exit(FatalIOError);
277  }
278 
279  return **iter;
280 }
281 
282 
283 // * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * * //
284 
285 template<class Type, class GeoMesh, template<class> class PrimitiveField>
286 Foam::Ostream& Foam::operator<<
287 (
288  Ostream& os,
290 )
291 {
292  typedef typename GeoMesh::template FieldSource<Type> Source;
293  os << static_cast<const HashPtrTable<Source>&>(bf);
294  return os;
295 }
296 
297 
298 // ************************************************************************* //
#define forAllConstIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:492
Field with dimensions and associated with geometry type GeoMesh which is used to size the field and a...
Part of a geometric field used for setting the values associated with optional sources.
const IOerrorLocation & errorLocation() const
Access the error location.
void readField(const Internal &field, const dictionary &dict)
Read the sources.
const HashPtrTable< Source > & table() const
Access the underlying field table.
void writeEntry(const word &keyword, Ostream &os) const
Write sources as dictionary entry.
GeoMesh::template FieldSource< Type > Source
Type of the field source of which this field sources is composed.
HashTable< word > types() const
Return a map from the source name to the field source type.
friend class GeometricFieldSources
Declare friendship with other geometric field sources.
void reset(const Internal &, const GeometricFieldSources &)
Reset the boundary field contents to the given field.
A HashTable specialisation for hashing pointers.
Definition: HashPtrTable.H:68
An STL-conforming const_iterator.
Definition: HashTable.H:494
An STL-conforming hash table.
Definition: HashTable.H:127
bool insert(const Key &, const T &newElmt)
Insert a new hashedEntry.
Definition: HashTableI.H:80
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:108
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
@ BEGIN_BLOCK
Definition: token.H:114
@ END_BLOCK
Definition: token.H:115
A class for handling words, derived from string.
Definition: word.H:63
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:346
tUEqn clear()
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
Ostream & decrIndent(Ostream &os)
Decrement the indent level.
Definition: Ostream.H:272
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:288
Ostream & incrIndent(Ostream &os)
Increment the indent level.
Definition: Ostream.H:265
T clone(const T &t)
Definition: List.H:55
IOerror FatalIOError
Ostream & indent(Ostream &os)
Indent stream.
Definition: Ostream.H:243
tmp< DimensionedField< TypeR, GeoMesh, Field > > New(const tmp< DimensionedField< TypeR, GeoMesh, Field >> &tdf1, const word &name, const dimensionSet &dimensions)
static const char nl
Definition: Ostream.H:297
fileType type(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return the file type: directory or file.
Definition: POSIX.C:488
dictionary dict