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-2021 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 {
35  defineTypeNameAndDebug(fieldsExpression, 0);
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  {
57  resultName_ = functionName + '(' + fieldNames_[0];
58  for (label i=1; i<fieldNames_.size(); i++)
59  {
60  resultName_ += ',' + fieldNames_[i];
61  }
62  resultName_ += ')';
63  }
64  else
65  {
66  resultName_ = functionName;
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]);
147  #undef findFieldType
148 
149  if (!found)
150  {
151  notFoundFieldNames.append(fieldNames_[i]);
152  }
153  }
154 
155  if (!notFoundFieldNames.empty())
156  {
157  Warning
158  << "functionObjects::" << type() << " " << name()
159  << " cannot find fields " << notFoundFieldNames << endl;
160  }
161  else
162  {
163  Warning
164  << "functionObjects::" << type() << " " << name()
165  << " fields are not compatible with the " << type()
166  << " function" << endl;
167  }
168 
169  // Clear the result fields from the objectRegistry if present
170  clear();
171 
172  return false;
173  }
174  else
175  {
176  return true;
177  }
178 }
179 
180 
182 {
183  return writeObject(resultName_);
184 }
185 
186 
188 {
189  return clearObject(resultName_);
190 }
191 
192 
193 // ************************************************************************* //
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:663
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
bool empty() const
Return true if the UList is empty (ie, size() is zero)
Definition: UListI.H:325
tUEqn clear()
#define findFieldType(Type, GeoField)
virtual bool write()
Write the result fields.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:156
void setResultName(const word &functionName, const wordList &defaultFieldNames=wordList::null())
Set the result name.
virtual bool read(const dictionary &)
Read the fieldsExpression data.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Generic GeometricField class.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
virtual bool read(const dictionary &)
Read optional controls.
bool read(const char *, int32_t &)
Definition: int32IO.C:85
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.
A class for handling words, derived from string.
Definition: word.H:59
virtual bool clear()
Clear the result fields from the objectRegistry.
DynamicList< T, SizeInc, SizeMult, SizeDiv > & append(const T &)
Append an element at the end of the list.
Definition: DynamicListI.H:296
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
defineTypeNameAndDebug(Qdot, 0)
messageStream Warning
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:318
virtual wordList fields() const
Return the list of fields required.
fileType type(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return the file type: directory or file.
Definition: POSIX.C:488
FOR_ALL_FIELD_TYPES(DefineFvWallInfoType)
Specialisation of Foam::functionObject for an Foam::fvMesh, providing a reference to the Foam::fvMesh...
bool found
Namespace for OpenFOAM.
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:864
IOerror FatalIOError