fieldsExpression.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2016 OpenFOAM Foundation
9  Copyright (C) 2019 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "fieldsExpression.H"
30 #include "dictionary.H"
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36 namespace functionObjects
37 {
38  defineTypeNameAndDebug(fieldsExpression, 0);
39 }
40 }
41 
42 
43 // * * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * //
44 
46 (
47  const word& typeName,
48  const wordList& defaultArgs
49 )
50 {
51  if (fieldNames_.empty())
52  {
53  fieldNames_ = defaultArgs;
54  }
55 
56  if (resultName_.empty())
57  {
58  if (!fieldNames_.empty())
59  {
60  resultName_ = typeName + '(' + fieldNames_[0];
61  for (label i=1; i<fieldNames_.size(); ++i)
62  {
63  resultName_ += ',' + fieldNames_[i];
64  }
65  resultName_ += ')';
66  }
67  else
68  {
69  resultName_ = typeName;
70  }
71  }
72 }
73 
74 
75 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
76 
78 (
79  const word& name,
80  const Time& runTime,
81  const dictionary& dict,
82  const wordList& fieldNames,
83  const word& resultName
84 )
85 :
87  fieldNames_(fieldNames),
88  resultName_(resultName)
89 {
90  read(dict);
91 
92  if (fieldNames_.size() < 2)
93  {
95  << "functionObject::" << type() << " " << name
96  << " requires at least 2 fields only "
97  << fieldNames_.size() << " provided: " << fieldNames_
98  << exit(FatalIOError);
99  }
100 }
101 
102 
103 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
104 
106 {
108 
109  if (fieldNames_.empty() || dict.found("fields"))
110  {
111  dict.readEntry("fields", fieldNames_);
112  }
113 
114  dict.readIfPresent("result", resultName_);
115 
116  return true;
117 }
118 
119 
121 {
122  if (!calc())
123  {
124  Warning
125  << " functionObjects::" << type() << " " << name()
126  << " cannot find required fields " << fieldNames_ << endl;
127 
128  // Clear the result fields from the objectRegistry if present
129  clear();
130 
131  return false;
132  }
133 
134  return true;
135 }
136 
137 
139 {
140  return writeObject(resultName_);
141 }
142 
143 
145 {
146  return clearObject(resultName_);
147 }
148 
149 
150 // ************************************************************************* //
runTime
engineTime & runTime
Definition: createEngineTime.H:13
Foam::functionObjects::fieldsExpression::read
virtual bool read(const dictionary &dict)
Read the fieldsExpression data.
Definition: fieldsExpression.C:105
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:73
Foam::functionObjects::fieldsExpression::execute
virtual bool execute()
Calculate the result fields.
Definition: fieldsExpression.C:120
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::Warning
messageStream Warning
Foam::FatalIOError
IOerror FatalIOError
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::functionObjects::fvMeshFunctionObject
Specialization of Foam::functionObject for an Foam::fvMesh, providing a reference to the Foam::fvMesh...
Definition: fvMeshFunctionObject.H:64
fieldsExpression.H
Foam::functionObjects::fieldsExpression::fieldsExpression
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.
Definition: fieldsExpression.C:78
Foam::blockMeshTools::read
void read(Istream &, label &val, const dictionary &)
In-place read with dictionary lookup.
Definition: blockMeshTools.C:57
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
fieldNames
const wordRes fieldNames(propsDict.getOrDefault< wordRes >("fields", wordRes()))
Foam::functionObjects::regionFunctionObject::read
virtual bool read(const dictionary &dict)
Read optional controls.
Definition: regionFunctionObject.C:173
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::functionObjects::fieldsExpression::write
virtual bool write()
Write the result fields.
Definition: fieldsExpression.C:138
clear
patchWriters clear()
Foam::List< word >
Foam::type
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: MSwindows.C:590
Foam::functionObjects::fieldsExpression::fieldNames_
wordList fieldNames_
Names of fields to process.
Definition: fieldsExpression.H:112
dictionary.H
Foam::functionObjects::defineTypeNameAndDebug
defineTypeNameAndDebug(ObukhovLength, 0)
Foam::functionObjects::fieldsExpression::resultName_
word resultName_
Name of result fields.
Definition: fieldsExpression.H:115
Foam::roots::type
type
Types of root.
Definition: Roots.H:54
Foam::functionObjects::fieldsExpression::setResultName
void setResultName(const word &typeName, const wordList &defaultArg=wordList::null())
Definition: fieldsExpression.C:46
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
Foam::functionObjects::fieldsExpression::clear
virtual bool clear()
Clear the result fields from the objectRegistry.
Definition: fieldsExpression.C:144