fvFieldSource.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-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 \*---------------------------------------------------------------------------*/
25 
26 #include "fvFieldSource.H"
27 #include "fvSource.H"
28 #include "volMesh.H"
29 
30 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
31 
32 template<class Type>
34 (
36 )
37 :
38  libs_(fileNameList::null()),
39  internalField_(iF)
40 {}
41 
42 
43 template<class Type>
45 (
47  const dictionary& dict
48 )
49 :
50  libs_
51  (
52  dict.lookupOrDefault
53  (
54  "libs",
55  fileNameList::null(),
56  dictionary::writeOptionalEntries > 1
57  )
58  ),
59  internalField_(iF)
60 {}
61 
62 
63 template<class Type>
65 (
66  const fvFieldSource<Type>& field,
68 )
69 :
70  libs_(field.libs_),
71  internalField_(iF)
72 {}
73 
74 
75 // * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //
76 
77 template<class Type>
80 (
81  const word& fieldSourceType,
83 )
84 {
85  typename nullConstructorTable::iterator cstrIter =
86  nullConstructorTablePtr_->find(fieldSourceType);
87 
88  if (cstrIter == nullConstructorTablePtr_->end())
89  {
91  << "Unknown null-constructable fieldSource type " << fieldSourceType
92  << nl << nl << "Valid fieldSource types are :" << endl
93  << nullConstructorTablePtr_->sortedToc()
94  << exit(FatalIOError);
95  }
96 
97  return cstrIter()(iF);
98 }
99 
100 
101 template<class Type>
104 (
106  const dictionary& dict
107 )
108 {
109  const word fieldSourceType(dict.lookup("type"));
110 
111  libs.open(dict, "libs", dictionaryConstructorTablePtr_);
112 
113  typename dictionaryConstructorTable::iterator cstrIter =
114  dictionaryConstructorTablePtr_->find(fieldSourceType);
115 
116  if (cstrIter == dictionaryConstructorTablePtr_->end())
117  {
118  if (!disallowGenericFvFieldSource)
119  {
120  cstrIter = dictionaryConstructorTablePtr_->find("generic");
121  }
122 
123  if (cstrIter == dictionaryConstructorTablePtr_->end())
124  {
126  << "Unknown fieldSource type " << fieldSourceType
127  << " for model " << dict.dictName() << nl << nl
128  << "Valid fieldSource types are :" << endl
129  << dictionaryConstructorTablePtr_->sortedToc()
130  << exit(FatalIOError);
131  }
132  }
133 
134  return cstrIter()(iF, dict);
135 }
136 
137 
138 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
139 
140 template<class Type>
142 {}
143 
144 
145 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
146 
147 template<class Type>
149 {
150  return internalField_.mesh();
151 }
152 
153 
154 template<class Type>
157 {
158  return internalField_;
159 }
160 
161 
162 template<class Type>
164 (
165  const fvSource& model,
166  const scalarField& source,
167  const labelUList& cells
168 ) const
169 {
171  << "Condition of type " << type() << " cannot be used for source "
172  << model.name() << " of field " << internalField().name()
173  << " in file " << internalField().objectPath() << " as it is not "
174  << "defined for cell sets. It can only be used for sources that "
175  << "span the entire mesh." << exit(FatalError);
176 
177  return tmp<Field<Type>>(nullptr);
178 }
179 
180 
181 template<class Type>
183 (
184  const fvSource& model,
185  const scalarField& source,
186  const labelUList& cells
187 ) const
188 {
190  << "Condition of type " << type() << " cannot be used for source "
191  << model.name() << " of field " << internalField().name()
192  << " in file " << internalField().objectPath() << " as it is not "
193  << "defined for cell sets. It can only be used for sources that "
194  << "span the entire mesh." << exit(FatalError);
195 
196  return tmp<scalarField>(nullptr);
197 }
198 
199 
200 template<class Type>
203 (
204  const fvSource& model,
206 ) const
207 {
208  return
209  (1 - internalCoeff(model, source))
210  *sourceValue(model, source);
211 }
212 
213 
214 template<class Type>
216 (
217  const fvSource& model,
218  const scalarField& source,
219  const labelUList& cells
220 ) const
221 {
222  return
223  (1 - internalCoeff(model, source, cells))
224  *sourceValue(model, source, cells);
225 }
226 
227 
228 template<class Type>
231 (
232  const fvSource& model,
234 ) const
235 {
236  return
237  sourceCoeff(model, source)
238  + internalCoeff(model, source)*internalField();
239 }
240 
241 
242 template<class Type>
244 (
245  const fvSource& model,
246  const scalarField& source,
247  const labelUList& cells
248 ) const
249 {
250  return
251  sourceCoeff(model, source, cells)
252  + internalCoeff(model, source, cells)*Field<Type>(internalField(), cells);
253 }
254 
255 
256 template<class Type>
257 template<class OtherType>
259 (
260  const word& name,
261  const fvSource& model
262 ) const
263 {
264  const VolField<OtherType>& vf =
265  db().template lookupObject<VolField<OtherType>>(name);
266 
267  return vf.sources()[model.name()];
268 }
269 
270 
271 template<class Type>
272 template<class OtherType>
275 (
276  const word& name,
277  const fvSource& model,
279 ) const
280 {
281  return fieldSource<OtherType>(name, model).value(model, source);
282 }
283 
284 
285 template<class Type>
286 template<class OtherType>
288 (
289  const word& name,
290  const fvSource& model,
291  const scalarField& source,
292  const labelUList& cells
293 ) const
294 {
295  return fieldSource<OtherType>(name, model).value(model, source, cells);
296 }
297 
298 
299 template<class Type>
301 {
302  writeEntry(os, "type", type());
303 
304  if (libs_.size())
305  {
306  writeEntry(os, "libs", libs_);
307  }
308 }
309 
310 
311 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
312 
313 template<class Type>
315 {
316  ptf.write(os);
317 
318  os.check("Ostream& operator<<(Ostream&, const fvFieldSource<Type>&");
319 
320  return os;
321 }
322 
323 
324 // ************************************************************************* //
Field with dimensions and associated with geometry type GeoMesh which is used to size the field and a...
Generic GeometricField class.
const Sources & sources() const
Return const-reference to the sources.
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: autoPtr.H:51
const word dictName() const
Return the local dictionary name (final part of scoped name)
Definition: dictionary.H:123
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:740
bool open(const fileName &libName, const bool verbose=true)
Open the named library, optionally with warnings if problems occur.
Base class for finite-volume field sources.
Definition: fvFieldSource.H:62
virtual tmp< DimensionedField< scalar, volMesh > > internalCoeff(const fvSource &model, const DimensionedField< scalar, volMesh > &source) const =0
Return the internal coefficient.
virtual tmp< DimensionedField< Type, volMesh > > sourceValue(const fvSource &model, const DimensionedField< scalar, volMesh > &source) const =0
Return the source value.
virtual void write(Ostream &) const
Write.
fvFieldSource(const DimensionedField< Type, volMesh > &)
Construct from internal field.
Definition: fvFieldSource.C:34
const objectRegistry & db() const
Return the local object registry.
const fvFieldSource< OtherType > & fieldSource(const word &name, const fvSource &model) const
Lookup and return another field source.
static autoPtr< fvFieldSource< Type > > New(const word &fieldSourceType, const DimensionedField< Type, volMesh > &)
Return a pointer to a new field source.
Definition: fvFieldSource.C:80
virtual tmp< DimensionedField< Type, volMesh > > sourceCoeff(const fvSource &model, const DimensionedField< scalar, volMesh > &source) const
Return the source coefficient.
virtual ~fvFieldSource()
Destructor.
tmp< DimensionedField< Type, volMesh > > value(const fvSource &model, const DimensionedField< scalar, volMesh > &source) const
Return the value.
const DimensionedField< Type, volMesh > & internalField() const
Return the internal field reference.
const word & name() const
Return const access to the source name.
Definition: fvModelI.H:57
Base class for finite volume sources.
Definition: fvSource.H:52
Registry of regIOobjects.
A class for managing temporary objects.
Definition: tmp.H:55
A class for handling words, derived from string.
Definition: word.H:62
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:346
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
const cellShapeList & cells
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
dlLibraryTable libs
Table of loaded dynamic libraries.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:258
void writeEntry(Ostream &os, const HashTable< T, Key, Hash > &ht)
Definition: HashTableIO.C:96
IOerror FatalIOError
word name(const LagrangianState state)
Return a string representation of a Lagrangian state enumeration.
Ostream & operator<<(Ostream &os, const fvConstraints &constraints)
error FatalError
static const char nl
Definition: Ostream.H:267
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