multiFieldValue.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) 2012-2016 OpenFOAM Foundation
9  Copyright (C) 2015-2021 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 "multiFieldValue.H"
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36 namespace functionObjects
37 {
38 namespace fieldValues
39 {
42 }
43 }
44 }
45 
46 
47 const Foam::Enum
48 <
50 >
52 ({
53  { operationType::opSum, "sum" },
54  { operationType::opAdd, "add" },
55  { operationType::opSubtract, "subtract" },
56  { operationType::opMin, "min" },
57  { operationType::opMax, "max" },
58  { operationType::opAverage, "average" },
59 });
60 
61 
62 // * * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * //
63 
65 (
66  Ostream& os
67 ) const
68 {
69  const wordList& fields0 = functions_[0].fields();
70 
71  DynamicList<word> commonFields(fields0.size());
72 
73  for (const word& fieldName : fields0)
74  {
75  bool common = true;
76 
77  for (label functioni=1; functioni < functions_.size(); ++functioni)
78  {
79  if (!functions_[functioni].fields().found(fieldName))
80  {
81  common = false;
82  break;
83  }
84  }
85 
86  if (common)
87  {
88  commonFields.append(fieldName);
89  }
90  }
91 
92  forAll(functions_, functioni)
93  {
94  writeHeaderValue
95  (
96  os,
97  "Source" + Foam::name(functioni),
98  functions_[functioni].name()
99  );
100  }
101 
102  writeHeaderValue(os, "Operation", operationTypeNames_[operation_]);
103  writeCommented(os, "Time");
104 
105  for (const word& fieldName : commonFields)
106  {
107  os << tab << fieldName;
108  }
109 
110  os << endl;
111 }
112 
113 
114 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
115 
117 (
118  const word& name,
119  const Time& runTime,
120  const dictionary& dict
121 )
122 :
124  writeFile(runTime, name, typeName, dict),
125  operation_(opSubtract),
126  functions_()
127 {
128  if (read(dict))
129  {
130  writeFileHeader(file());
131  }
132 }
133 
134 
135 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
136 
138 (
139  const dictionary& dict
140 )
141 {
143  {
144  const dictionary& functionsDict = dict.subDict("functions");
145  functions_.resize(functionsDict.size());
146 
147  if (functions_.empty())
148  {
150  << "No functions specified"
151  << endl;
152  return false;
153  }
154 
155  label functioni = 0;
156  for (const entry& dEntry : functionsDict)
157  {
158  if (!dEntry.isDict())
159  {
161  << "Functions must be specified in dictionary format"
162  << exit(FatalIOError);
163  }
164 
165  const dictionary& localDict = dEntry.dict();
166 
167  functions_.set
168  (
169  functioni,
171  (
172  IOobject::scopedName(name(), localDict.dictName()),
173  time(),
174  localDict,
175  false
176  )
177  );
178 
179  ++functioni;
180  }
181 
182  operation_ = operationTypeNames_.get("operation", dict);
183 
184  return true;
185  }
186 
187  return false;
188 }
189 
190 
192 {
193  if (functions_.empty())
194  {
195  return false;
196  }
197 
198  Log << type() << " " << name() << " write:" << endl;
199 
200  const label nFunction = functions_.size();
201  wordList entries0;
202  label nEntries = -1;
203 
204  wordList names(nFunction);
205  List<wordList> entries;
206  List<wordList> types;
207 
208  forAll(functions_, functioni)
209  {
210  auto& f = functions_[functioni];
211  names[functioni] = f.name();
212 
213  // Note: results are not available until the call to write()
214  f.write();
215 
216  const wordList e(objectResultEntries(f.name()));
217 
218  if (functioni == 0)
219  {
220  entries0 = e;
221  nEntries = e.size();
222  entries.resize(nEntries);
223  types.resize(nEntries);
224 
225  forAll(entries, entryi)
226  {
227  entries[entryi].resize(nFunction);
228  types[entryi].resize(nFunction);
229  }
230  }
231 
232  if (e.size() != nEntries)
233  {
234  const word& f0Name = functions_[0].name();
235 
237  << "Inconsistent number of result entries" << nl
238  << " " << f0Name << " entries:" << entries0 << nl
239  << " " << f.name() << " entries:" << e
240  << abort(FatalError);
241  }
242 
243  forAll(e, entryi)
244  {
245  entries[entryi][functioni] = e[entryi];
246  types[entryi][functioni] = objectResultType(f.name(), e[entryi]);
247  }
248  }
249 
251 
252  forAll(entries, entryi)
253  {
254  const wordList& entriesi = entries[entryi];
255  const word& t0 = types[entryi][0];
256  const wordList& typesi = types[entryi];
257  forAll(typesi, functioni)
258  {
259  const word& t = typesi[functioni];
260 
261  if (t != t0)
262  {
264  << "Inconsistent function result types" << nl
265  << " " << functions_[0].name()
266  << " result type:" << t0 << nl
267  << " " << functions_[functioni].name()
268  << " result type:" << typesi[functioni]
269  << abort(FatalError);
270  }
271  }
272 
273  const bool ok
274  (
275  applyOperation<scalar>(t0, names, entriesi)
276  || applyOperation<vector>(t0, names, entriesi)
277  || applyOperation<sphericalTensor>(t0, names, entriesi)
278  || applyOperation<symmTensor>(t0, names, entriesi)
279  || applyOperation<tensor>(t0, names, entriesi)
280  );
281 
282  if (!ok)
283  {
284  Log << "Operation not applied between functions:" << nl
286  << "with result names:" << nl
287  << flatOutput(entriesi, FlatOutput::BareComma{})
288  << endl;
289  }
290  }
291 
292  Log << (nEntries == 0 ? " none" : "") << endl;
293 
294  file()<< endl;
295 
296  return true;
297 }
298 
299 
301 {
302  return true;
303 }
304 
305 
306 // ************************************************************************* //
Foam::entry
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:67
runTime
engineTime & runTime
Definition: createEngineTime.H:13
Foam::functionObjects::writeFile::writeCurrentTime
virtual void writeCurrentTime(Ostream &os) const
Write the current time to stream.
Definition: writeFile.C:308
Foam::functionObjects::fieldValues::multiFieldValue::writeFileHeader
virtual void writeFileHeader(Ostream &os) const
Output file header information.
Definition: multiFieldValue.C:65
Foam::Enum
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition: IOstreamOption.H:57
Log
#define Log
Definition: PDRblock.C:35
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:73
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::functionObjects::fieldValues::multiFieldValue::multiFieldValue
multiFieldValue(const word &name, const Time &runTime, const dictionary &dict)
Construct from Time and dictionary.
Definition: multiFieldValue.C:117
Foam::functionObjects::fieldValues::defineTypeNameAndDebug
defineTypeNameAndDebug(multiFieldValue, 0)
Foam::functionObjects::writeFile::file
virtual OFstream & file()
Return access to the file (if only 1)
Definition: writeFile.C:236
multiFieldValue.H
Foam::List::resize
void resize(const label len)
Adjust allocated size of list.
Definition: ListI.H:139
Foam::functionObjects::fieldValues::multiFieldValue::write
virtual bool write()
Calculate and write.
Definition: multiFieldValue.C:191
Foam::DynamicList< word >
Foam::functionObjects::stateFunctionObject
Base class for function objects, adding functionality to read/write state information (data required ...
Definition: stateFunctionObject.H:69
Foam::functionObjects::fieldValues::multiFieldValue::read
virtual bool read(const dictionary &dict)
Read from dictionary.
Definition: multiFieldValue.C:138
Foam::FatalIOError
IOerror FatalIOError
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::functionObjects::fieldValues::multiFieldValue
Computes a selected operation between multiple fieldValue function objects.
Definition: multiFieldValue.H:182
Foam::dictionary::set
entry * set(entry *entryPtr)
Assign a new entry, overwriting any existing entry.
Definition: dictionary.C:780
Foam::functionObjects::fieldValues::multiFieldValue::operationTypeNames_
static const Enum< operationType > operationTypeNames_
Operation type names.
Definition: multiFieldValue.H:203
Foam::functionObject
Abstract base-class for Time/database function objects.
Definition: functionObject.H:332
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::blockMeshTools::read
void read(Istream &, label &val, const dictionary &)
In-place read with dictionary lookup.
Definition: blockMeshTools.C:57
Foam::functionObjects::fieldValues::multiFieldValue::operationType
operationType
Operation type enumeration.
Definition: multiFieldValue.H:192
Foam::functionObjects::stateFunctionObject::objectResultEntries
wordList objectResultEntries() const
Retrieve the result entries.
Definition: stateFunctionObject.C:149
Foam::DynamicList::append
DynamicList< T, SizeMin > & append(const T &val)
Append an element to the end of this list.
Definition: DynamicListI.H:511
Foam::functionObjects::fieldValues::addToRunTimeSelectionTable
addToRunTimeSelectionTable(functionObject, multiFieldValue, dictionary)
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
os
OBJstream os(runTime.globalPath()/outputName)
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::flatOutput
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition: FlatOutput.H:216
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:144
Foam::functionObjects::stateFunctionObject::objectResultType
word objectResultType(const word &objectName, const word &entryName) const
Return the type of result.
Definition: stateFunctionObject.C:139
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::New
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tdf1, const word &name, const dimensionSet &dimensions)
Global function forwards to reuseTmpDimensionedField::New.
Definition: DimensionedFieldReuseFunctions.H:105
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::functionObject::name
const word & name() const noexcept
Return the name of this functionObject.
Definition: functionObject.C:143
Foam::tab
constexpr char tab
Definition: Ostream.H:403
Foam::nl
constexpr char nl
Definition: Ostream.H:404
f
labelList f(nPoints)
Foam::entry::get
T get() const
Definition: entry.H:269
Foam::functionObject::type
virtual const word & type() const =0
Runtime type information.
Foam::FlatOutput::BareComma
Surround with '\0' and '\0' separate with ','.
Definition: FlatOutput.H:84
Foam::List< word >
Foam::constant::electromagnetic::e
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
Foam::functionObjects::fieldValues::multiFieldValue::execute
virtual bool execute()
Do nothing.
Definition: multiFieldValue.C:300
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::functionObjects::writeFile
Base class for writing single files from the function objects.
Definition: writeFile.H:119
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::PtrListOps::names
List< word > names(const UPtrList< T > &list, const UnaryMatchPredicate &matcher)
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:328
fields
multivariateSurfaceInterpolationScheme< scalar >::fieldTable fields
Definition: createFields.H:97
Foam::dictionary::dictName
word dictName() const
The local dictionary name (final part of scoped name)
Definition: dictionaryI.H:60