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  if (!calc())
129  {
130  DynamicList<word> notFoundFieldNames;
131  forAll(fieldNames_, i)
132  {
133  bool found = false;
134 
135  #define findFieldType(Type, GeoField) \
136  found = \
137  found \
138  || mesh_.foundObject<GeoField<Type>>(fieldNames_[i]);
141  #undef findFieldType
142 
143  if (!found)
144  {
145  notFoundFieldNames.append(fieldNames_[i]);
146  }
147  }
148 
149  if (!notFoundFieldNames.empty())
150  {
151  Warning
152  << "functionObjects::" << type() << " " << name()
153  << " cannot find fields " << notFoundFieldNames << endl;
154  }
155  else
156  {
157  Warning
158  << "functionObjects::" << type() << " " << name()
159  << " fields are not compatible with the " << type()
160  << " function" << endl;
161  }
162 
163  // Clear the result fields from the objectRegistry if present
164  clear();
165 
166  return false;
167  }
168  else
169  {
170  return true;
171  }
172 }
173 
174 
176 {
177  return writeObject(resultName_);
178 }
179 
180 
182 {
183  return clearObject(resultName_);
184 }
185 
186 
187 // ************************************************************************* //
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:643
#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()
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
#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
#define FOR_ALL_FIELD_TYPES(Macro,...)
Definition: fieldTypes.H:50
Generic GeometricField class.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:68
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:335
fileType type(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return the file type: directory or file.
Definition: POSIX.C:488
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:844
IOerror FatalIOError