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-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 "fvFieldSource.H"
27 #include "fvSource.H"
28 #include "fvMesh.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_(dict.lookupOrDefault("libs", fileNameList::null())),
51  internalField_(iF)
52 {}
53 
54 
55 template<class Type>
57 (
58  const fvFieldSource<Type>& field,
60 )
61 :
62  libs_(field.libs_),
63  internalField_(iF)
64 {}
65 
66 
67 // * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //
68 
69 template<class Type>
72 (
73  const word& fieldSourceType,
75 )
76 {
77  typename nullConstructorTable::iterator cstrIter =
78  nullConstructorTablePtr_->find(fieldSourceType);
79 
80  if (cstrIter == nullConstructorTablePtr_->end())
81  {
83  << "Unknown null-constructable fieldSource type " << fieldSourceType
84  << nl << nl << "Valid fieldSource types are :" << endl
85  << nullConstructorTablePtr_->sortedToc()
86  << exit(FatalIOError);
87  }
88 
89  return cstrIter()(iF);
90 }
91 
92 
93 template<class Type>
96 (
98  const dictionary& dict
99 )
100 {
101  const word fieldSourceType(dict.lookup("type"));
102 
103  libs.open(dict, "libs", dictionaryConstructorTablePtr_);
104 
105  typename dictionaryConstructorTable::iterator cstrIter =
106  dictionaryConstructorTablePtr_->find(fieldSourceType);
107 
108  if (cstrIter == dictionaryConstructorTablePtr_->end())
109  {
110  if (!disallowGenericFvFieldSource)
111  {
112  cstrIter = dictionaryConstructorTablePtr_->find("generic");
113  }
114 
115  if (cstrIter == dictionaryConstructorTablePtr_->end())
116  {
118  << "Unknown fieldSource type " << fieldSourceType
119  << " for model " << dict.dictName() << nl << nl
120  << "Valid fieldSource types are :" << endl
121  << dictionaryConstructorTablePtr_->sortedToc()
122  << exit(FatalIOError);
123  }
124  }
125 
126  return cstrIter()(iF, dict);
127 }
128 
129 
130 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
131 
132 template<class Type>
134 {}
135 
136 
137 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
138 
139 template<class Type>
141 {
142  return internalField_.mesh();
143 }
144 
145 
146 template<class Type>
148 {
149  return internalField_.mesh().time();
150 }
151 
152 
153 template<class Type>
156 {
157  return internalField_;
158 }
159 
160 
161 template<class Type>
163 (
164  const fvSource& model,
165  const scalarField& source,
166  const labelUList& cells
167 ) const
168 {
170  << "Condition of type " << type() << " cannot be used for source "
171  << model.name() << " of field " << internalField().name()
172  << " in file " << internalField().objectPath() << " as it is not "
173  << "defined for cell sets. It can only be used for sources that "
174  << "span the entire mesh." << exit(FatalError);
175 
176  return tmp<Field<Type>>(nullptr);
177 }
178 
179 
180 template<class Type>
182 (
183  const fvSource& model,
184  const scalarField& source,
185  const labelUList& cells
186 ) const
187 {
189  << "Condition of type " << type() << " cannot be used for source "
190  << model.name() << " of field " << internalField().name()
191  << " in file " << internalField().objectPath() << " as it is not "
192  << "defined for cell sets. It can only be used for sources that "
193  << "span the entire mesh." << exit(FatalError);
194 
195  return tmp<scalarField>(nullptr);
196 }
197 
198 
199 template<class Type>
202 (
203  const fvSource& model,
205 ) const
206 {
207  return
208  (1 - internalCoeff(model, source))
209  *sourceValue(model, source);
210 }
211 
212 
213 template<class Type>
215 (
216  const fvSource& model,
217  const scalarField& source,
218  const labelUList& cells
219 ) const
220 {
221  return
222  (1 - internalCoeff(model, source, cells))
223  *sourceValue(model, source, cells);
224 }
225 
226 
227 template<class Type>
230 (
231  const fvSource& model,
233 ) const
234 {
235  return
236  sourceCoeff(model, source)
237  + internalCoeff(model, source)*internalField();
238 }
239 
240 
241 template<class Type>
243 (
244  const fvSource& model,
245  const scalarField& source,
246  const labelUList& cells
247 ) const
248 {
249  return
250  sourceCoeff(model, source, cells)
251  + internalCoeff(model, source, cells)*Field<Type>(internalField(), cells);
252 }
253 
254 
255 template<class Type>
256 template<class OtherType>
258 (
259  const word& name,
260  const fvSource& model
261 ) const
262 {
263  const VolField<OtherType>& vf =
264  db().template lookupObject<VolField<OtherType>>(name);
265 
266  return vf.sources()[model.name()];
267 }
268 
269 
270 template<class Type>
271 template<class OtherType>
274 (
275  const word& name,
276  const fvSource& model,
278 ) const
279 {
280  return fieldSource<OtherType>(name, model).value(model, source);
281 }
282 
283 
284 template<class Type>
285 template<class OtherType>
287 (
288  const word& name,
289  const fvSource& model,
290  const scalarField& source,
291  const labelUList& cells
292 ) const
293 {
294  return fieldSource<OtherType>(name, model).value(model, source, cells);
295 }
296 
297 
298 template<class Type>
300 {
301  writeEntry(os, "type", type());
302 
303  if (libs_.size())
304  {
305  writeEntry(os, "libs", libs_);
306  }
307 }
308 
309 
310 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
311 
312 template<class Type>
314 {
315  ptf.write(os);
316 
317  os.check("Ostream& operator<<(Ostream&, const fvFieldSource<Type>&");
318 
319  return os;
320 }
321 
322 
323 // ************************************************************************* //
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:108
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:76
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:669
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
const DimensionedField< Type, fvMesh > & internalField() const
Return the internal field reference.
virtual tmp< DimensionedField< Type, fvMesh > > sourceCoeff(const fvSource &model, const DimensionedField< scalar, fvMesh > &source) const
Return the source coefficient.
static autoPtr< fvFieldSource< Type > > New(const word &fieldSourceType, const DimensionedField< Type, fvMesh > &)
Return a pointer to a new field source.
Definition: fvFieldSource.C:72
const Time & time() const
Return time.
virtual void write(Ostream &) const
Write.
fvFieldSource(const DimensionedField< Type, fvMesh > &)
Construct from internal field.
Definition: fvFieldSource.C:34
virtual tmp< DimensionedField< Type, fvMesh > > sourceValue(const fvSource &model, const DimensionedField< scalar, fvMesh > &source) const =0
Return the source value.
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.
tmp< DimensionedField< Type, fvMesh > > value(const fvSource &model, const DimensionedField< scalar, fvMesh > &source) const
Return the value.
virtual ~fvFieldSource()
Destructor.
virtual tmp< DimensionedField< scalar, fvMesh > > internalCoeff(const fvSource &model, const DimensionedField< scalar, fvMesh > &source) const =0
Return the internal coefficient.
const word & name() const
Return const access to the source name.
Definition: fvModelI.H:57
Base class for finite volume sources.
Definition: fvSource.H:53
Registry of regIOobjects.
const Time & time() const
Return time.
A class for managing temporary objects.
Definition: tmp.H:55
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
#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:288
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
void writeEntry(Ostream &os, const word &key, const DimensionedFieldFunction< DimensionedFieldType > &f)
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