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-2022 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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
34namespace Foam
35{
36namespace functionObjects
37{
38namespace fieldValues
39{
42}
43}
44}
45
46
47const 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 const wordList& foNames,
67 const List<wordList>& entries,
68 const List<wordList>& types,
69 Ostream& os
70) const
71{
72 const word groupPrefix("Group");
73
74 forAll(entries, i)
75 {
76 writeCommented(os, groupPrefix + Foam::name(i));
77 os << nl;
78
79 forAll(entries[i], functioni)
80 {
82 (
83 os,
84 " - " + foNames[functioni] + ":" + entries[i][functioni]
85 );
86 os << nl;
87 }
88 }
89
90 writeHeaderValue(os, "Operation", operationTypeNames_[operation_]);
91 writeCommented(os, "Time");
92
93 forAll(entries, entryi)
94 {
95 writeTabbed(os, groupPrefix + Foam::name(entryi));
96 }
97
98 os << endl;
99}
100
101
102// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
103
105(
106 const word& name,
107 const Time& runTime,
108 const dictionary& dict
109)
110:
112 writeFile(runTime, name, typeName, dict),
113 operation_(opSubtract),
114 functions_()
115{
116 read(dict);
117}
118
119
120// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
121
123(
124 const dictionary& dict
125)
126{
128 {
129 return false;
130 }
131
132 operation_ = operationTypeNames_.get("operation", dict);
133
134 const dictionary& functionsDict = dict.subDict("functions");
135 functions_.resize(functionsDict.size());
136
137 if (functions_.empty())
138 {
140 << "No functions specified"
141 << endl;
142 return false;
143 }
144
145 resultFields_.resize(functions_.size());
146
147 label functioni = 0;
148 for (const entry& dEntry : functionsDict)
149 {
150 if (!dEntry.isDict())
151 {
153 << "Functions must be specified in dictionary format"
154 << exit(FatalIOError);
155 }
156
157 const dictionary& localDict = dEntry.dict();
158
159 functions_.set
160 (
161 functioni,
163 (
164 IOobject::scopedName(name(), localDict.dictName()),
165 time(),
166 localDict
167 ).ptr()
168 );
169
170 // Deactivate logging for child function objects
171 //functions_[functioni].log = false;
172
173 // Get result field names; not specified implies all
174 resultFields_[functioni] =
175 localDict.getOrDefault<wordList>("resultFields", wordList());
176
177 Info<< type() << ' ' << name() << ':' << nl;
178 if (resultFields_[functioni].size())
179 {
180 Info<< " " << functions_[functioni].name()
181 << " " << resultFields_[functioni];
182 }
183 else
184 {
185 Info<< " " << functions_[functioni].name()
186 << " - using all available entries";
187 }
188 Info<< nl << endl;
189
190 ++functioni;
191 }
192
193 return true;
194}
195
196
198{
199 if (functions_.empty())
200 {
201 return false;
202 }
203
204 Log << type() << " " << name() << " write:" << endl;
205
206 const label nFunction = functions_.size();
207 wordList entries0;
208 label nEntries = -1;
209
210 wordList foNames(nFunction);
211 List<wordList> entries;
212 List<wordList> types;
213
214 forAll(functions_, functioni)
215 {
216 auto& f = functions_[functioni];
217 foNames[functioni] = f.name();
218
219 // Note: replicating functionObjectList execute() and write()
220 // - results may be written on either
221 f.execute();
222 f.write();
223
224 wordList e = resultFields_[functioni];
225 if (e.empty())
226 {
227 e = objectResultEntries(f.name());
228 }
229
230 if (functioni == 0)
231 {
232 entries0 = e;
233 nEntries = e.size();
234 entries.resize(nEntries);
235 types.resize(nEntries);
236
237 forAll(entries, entryi)
238 {
239 entries[entryi].resize(nFunction);
240 types[entryi].resize(nFunction);
241 }
242 }
243
244 if (e.size() != nEntries)
245 {
246 const word& f0Name = functions_[0].name();
247
249 << "Inconsistent number of result entries" << nl
250 << " " << f0Name << " entries:" << entries0 << nl
251 << " " << f.name() << " entries:" << e
252 << exit(FatalError);
253 }
254
255 forAll(e, entryi)
256 {
257 entries[entryi][functioni] = e[entryi];
258 types[entryi][functioni] = objectResultType(f.name(), e[entryi]);
259
260 if (types[entryi][functioni] == word::null)
261 {
263 << "Unable to find function object result" << nl
264 << " function object : " << f.name() << nl
265 << " result name : " << e[entryi] << nl
266 << " available results : "
267 << objectResultEntries(f.name())
268 << exit(FatalError);
269 }
270 }
271 }
272
273 if (!writtenHeader_)
274 {
275 writeFileHeader(foNames, entries, types, file());
276 writtenHeader_ = true;
277 }
278
279 writeCurrentTime(file());
280
281 forAll(entries, i)
282 {
283 const wordList& entryi = entries[i];
284 const word& expectedType = types[i][0];
285 const wordList& foTypes = types[i];
286
287 forAll(foTypes, functioni)
288 {
289 const word& foType = foTypes[functioni];
290
291 if (foType != expectedType)
292 {
294 << "Inconsistent function result types" << nl
295 << " " << functions_[0].name()
296 << " result type:" << expectedType << nl
297 << " " << functions_[functioni].name()
298 << " result type:" << foType
299 << exit(FatalError);
300 }
301 }
302
303 const bool ok
304 (
305 applyOperation<scalar>(expectedType, foNames, entryi)
306 || applyOperation<vector>(expectedType, foNames, entryi)
307 || applyOperation<sphericalTensor>(expectedType, foNames, entryi)
308 || applyOperation<symmTensor>(expectedType, foNames, entryi)
309 || applyOperation<tensor>(expectedType, foNames, entryi)
310 );
311
312 if (!ok)
313 {
314 Log << "Operation not applied between functions:" << nl
315 << flatOutput(foNames, FlatOutput::BareComma{}) << nl
316 << "with result names:" << nl
318 << endl;
319 }
320 }
321
322 Log << (nEntries == 0 ? " none" : "") << endl;
323
324 file()<< endl;
325
326 return true;
327}
328
329
331{
332 return true;
333}
334
335
336// ************************************************************************* //
#define Log
Definition: PDRblock.C:35
Macros for easy insertion into run-time selection tables.
#define addToRunTimeSelectionTable(baseType, thisType, argNames)
Add to construction table with typeName as the key.
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition: Enum.H:61
static word scopedName(const std::string &scope, const word &name)
Create scope:name or scope_name string.
Definition: IOobjectI.H:47
void resize(const label len)
Adjust allocated size of list.
Definition: ListI.H:139
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
virtual bool read()
Re-read model coefficients if they have changed.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:80
static autoPtr< Time > New()
Construct (dummy) Time - no functionObjects or libraries.
Definition: Time.C:717
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
T getOrDefault(const word &keyword, const T &deflt, enum keyType::option matchOpt=keyType::REGEX) const
entry * set(entry *entryPtr)
Assign a new entry, overwriting any existing entry.
Definition: dictionary.C:780
word dictName() const
The local dictionary name (final part of scoped name)
Definition: dictionaryI.H:60
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:70
Abstract base-class for Time/database function objects.
Computes a selected operation between multiple function objects.
virtual bool read(const dictionary &dict)
Read from dictionary.
virtual void writeFileHeader(const wordList &foNames, const List< wordList > &entries, const List< wordList > &types, Ostream &os) const
Output file header information.
static const Enum< operationType > operationTypeNames_
Operation type names.
Base class for function objects, adding functionality to read/write state information (data required ...
Base class for writing single files from the function objects.
Definition: writeFile.H:120
virtual void writeTabbed(Ostream &os, const string &str) const
Write a tabbed string to stream.
Definition: writeFile.C:285
void writeHeaderValue(Ostream &os, const string &property, const Type &value) const
Write a (commented) header property and value pair.
virtual void writeCommented(Ostream &os, const string &str) const
Write a commented string to stream.
Definition: writeFile.C:269
A class for handling words, derived from Foam::string.
Definition: word.H:68
#define defineTypeNameAndDebug(Type, DebugSwitch)
Define the typeName and debug information.
Definition: className.H:121
engineTime & runTime
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
OBJstream os(runTime.globalPath()/outputName)
#define WarningInFunction
Report a warning using Foam::Warning.
Namespace for OpenFOAM.
List< word > wordList
A List of words.
Definition: fileName.H:63
messageStream Info
Information stream (stdout output on master, null elsewhere)
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: MSwindows.C:598
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition: FlatOutput.H:215
IOerror FatalIOError
error FatalError
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
labelList f(nPoints)
dictionary dict
volScalarField & e
Definition: createFields.H:11
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:333
Surround with '\0' and '\0' separate with ','.
Definition: FlatOutput.H:84