fieldsExpression.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) 2016-2023 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 "fieldsExpression.H"
27 #include "dictionary.H"
28 
29 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
30 
31 namespace Foam
32 {
33 namespace functionObjects
34 {
36 }
37 }
38 
39 
40 // * * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * //
41 
43 (
44  const word& functionName,
45  const wordList& defaultFieldNames
46 )
47 {
48  if (fieldNames_.empty())
49  {
50  fieldNames_ = defaultFieldNames;
51  }
52 
53  if (resultName_.empty())
54  {
55  if (!fieldNames_.empty())
56  {
58  for (label i=1; i<fieldNames_.size(); i++)
59  {
60  resultName_ += ',' + fieldNames_[i];
61  }
62  resultName_ += ')';
63  }
64  else
65  {
67  }
68  }
69 }
70 
71 
72 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
73 
75 (
76  const word& name,
77  const Time& runTime,
78  const dictionary& dict,
79  const wordList& fieldNames,
80  const word& resultName
81 )
82 :
83  fvMeshFunctionObject(name, runTime, dict),
84  fieldNames_(fieldNames),
85  resultName_(resultName)
86 {
87  read(dict);
88 
89  if (fieldNames_.size() < 2)
90  {
92  << "functionObject::" << type() << " " << name
93  << " requires at least 2 fields only "
94  << fieldNames_.size() << " provided: " << fieldNames_
95  << exit(FatalIOError);
96  }
97 }
98 
99 
100 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
101 
103 {}
104 
105 
106 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
107 
109 {
111 
112  if (fieldNames_.empty() || dict.found("fields"))
113  {
114  dict.lookup("fields") >> fieldNames_;
115  }
116 
117  if (dict.found("result"))
118  {
119  dict.lookup("result") >> resultName_;
120  }
121 
122  return true;
123 }
124 
125 
127 {
128  return fieldNames_;
129 }
130 
131 
133 {
134  if (!calc())
135  {
136  DynamicList<word> notFoundFieldNames;
137  forAll(fieldNames_, i)
138  {
139  bool found = false;
140 
141  #define findFieldType(Type, GeoField) \
142  found = \
143  found \
144  || mesh_.foundObject<GeoField<Type>>(fieldNames_[i]);
148  #undef findFieldType
149 
150  if (!found)
151  {
152  notFoundFieldNames.append(fieldNames_[i]);
153  }
154  }
155 
156  if (!notFoundFieldNames.empty())
157  {
158  Warning
159  << "functionObjects::" << type() << " " << name()
160  << " cannot find fields " << notFoundFieldNames << endl;
161  }
162  else
163  {
164  Warning
165  << "functionObjects::" << type() << " " << name()
166  << " fields are not compatible with the " << type()
167  << " function" << endl;
168  }
169 
170  // Clear the result fields from the objectRegistry if present
171  clear();
172 
173  return false;
174  }
175  else
176  {
177  return true;
178  }
179 }
180 
181 
183 {
184  return writeObject(resultName_);
185 }
186 
187 
189 {
190  return clearObject(resultName_);
191 }
192 
193 
194 // ************************************************************************* //
bool found
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
DynamicList< T, SizeInc, SizeMult, SizeDiv > & append(const T &)
Append an element at the end of the list.
Definition: DynamicListI.H:296
Generic GeometricField class.
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:76
bool empty() const
Return true if the UList is empty (ie, size() is zero)
Definition: UListI.H:325
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:162
A functionName is a word starting with '#'.
Definition: functionName.H:60
virtual const word & type() const =0
Runtime type information.
const word & name() const
Return the name of this functionObject.
Base class for expressions involving multiple fields.
wordList fieldNames_
Names of fields to process.
virtual wordList fields() const
Return the list of fields required.
word resultName_
Name of result fields.
void setResultName(const word &functionName, const wordList &defaultFieldNames=wordList::null())
Set the result name.
virtual bool clear()
Clear the result fields from the objectRegistry.
fieldsExpression(const word &name, const Time &runTime, const dictionary &dict, const wordList &fieldNames=wordList::null(), const word &resultName=word::null)
Construct from Time and dictionary.
virtual bool execute()
Calculate the result fields.
virtual bool write()
Write the result fields.
virtual bool read(const dictionary &)
Read the fieldsExpression data.
Specialisation of Foam::functionObject for an Foam::fvMesh, providing a reference to the Foam::fvMesh...
virtual bool read(const dictionary &)
Read optional controls.
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 findFieldType(Type, GeoField)
static List< word > fieldNames
Definition: globalFoam.H:46
tUEqn clear()
defineTypeNameAndDebug(adjustTimeStepToCombustion, 0)
Namespace for OpenFOAM.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
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
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:257
word name(const bool)
Return a word representation of a bool.
Definition: boolIO.C:39
typename VolField< Type >::Internal VolInternalField
Definition: volFieldsFwd.H:61
IOerror FatalIOError
FOR_ALL_FIELD_TYPES(makeFieldSourceTypedef)
fileType type(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return the file type: directory or file.
Definition: POSIX.C:488
messageStream Warning
dictionary dict