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-2018 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& typeName,
45  const wordList& defaultArgs
46 )
47 {
48  if (fieldNames_.empty())
49  {
50  fieldNames_ = defaultArgs;
51  }
52 
53  if (resultName_.empty())
54  {
55  if (!fieldNames_.empty())
56  {
57  resultName_ = typeName + '(' + 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_ = typeName;
67  }
68  }
69 }
70 
71 
72 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
73 
74 Foam::functionObjects::fieldsExpression::fieldsExpression
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  Warning
131  << " functionObjects::" << type() << " " << name()
132  << " cannot find required fields " << fieldNames_ << endl;
133 
134  // Clear the result fields from the objectRegistry if present
135  clear();
136 
137  return false;
138  }
139  else
140  {
141  return true;
142  }
143 }
144 
145 
147 {
148  return writeObject(resultName_);
149 }
150 
151 
153 {
154  return clearObject(resultName_);
155 }
156 
157 
158 // ************************************************************************* //
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:431
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
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:137
tUEqn clear()
virtual bool read(const dictionary &)
Read the fieldsExpression data.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:256
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
virtual bool execute()
Calculate the result fields.
void setResultName(const word &typeName, const wordList &defaultArg=wordList::null())
A class for handling words, derived from string.
Definition: word.H:59
virtual bool clear()
Clear the result fields from the objectRegistry.
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
fileName::Type type(const fileName &, const bool followLink=true)
Return the file type: DIRECTORY or FILE.
Definition: POSIX.C:481
messageStream Warning
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:331
Specialization of Foam::functionObject for an Foam::fvMesh, providing a reference to the Foam::fvMesh...
defineTypeNameAndDebug(fvMeshFunctionObject, 0)
Namespace for OpenFOAM.
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:576
IOerror FatalIOError