fieldValueDelta.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 | Copyright (C) 2012-2015 OpenFOAM Foundation
6  \\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd.
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 "fieldValueDelta.H"
27 #include "ListOps.H"
28 #include "Time.H"
29 #include "volFields.H"
30 #include "surfaceFields.H"
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36  namespace fieldValues
37  {
38  defineTypeNameAndDebug(fieldValueDelta, 0);
39  }
40 
41  template<>
42  const char*
44  {
45  "add",
46  "subtract",
47  "min",
48  "max",
49  "average"
50  };
51 
54 }
55 
56 
57 // * * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * //
58 
60 {
61  const wordList& fields1 = source1Ptr_->fields();
62  const wordList& fields2 = source2Ptr_->fields();
63 
64  DynamicList<word> commonFields(fields1.size());
65  forAll(fields1, i)
66  {
67  label index = findIndex(fields2, fields1[i]);
68  if (index != -1)
69  {
70  commonFields.append(fields1[i]);
71  }
72  }
73 
74  writeHeaderValue(os, "Source1", source1Ptr_->name());
75  writeHeaderValue(os, "Source2", source2Ptr_->name());
77  writeCommented(os, "Time");
78 
79  forAll(commonFields, i)
80  {
81  os << tab << commonFields[i];
82  }
83 
84  os << endl;
85 }
86 
87 
88 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
89 
91 (
92  const word& name,
93  const objectRegistry& obr,
94  const dictionary& dict,
95  const bool loadFromFiles
96 )
97 :
99  functionObjectFile(obr, name, typeName, dict),
100  obr_(obr),
101  loadFromFiles_(loadFromFiles),
102  log_(true),
103  operation_(opSubtract),
104  source1Ptr_(NULL),
105  source2Ptr_(NULL)
106 {
107  if (setActive<fvMesh>())
108  {
109  read(dict);
110  writeFileHeader(file());
111  }
112 }
113 
114 
115 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
116 
118 {}
119 
120 
121 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
122 
124 {
125  if (active_)
126  {
128 
129  log_ = dict.lookupOrDefault<Switch>("log", true);
130  source1Ptr_.reset
131  (
133  (
134  name_ + ".source1",
135  obr_,
136  dict.subDict("source1"),
137  loadFromFiles_,
138  false
139  ).ptr()
140  );
141  source2Ptr_.reset
142  (
144  (
145  name_ + ".source2",
146  obr_,
147  dict.subDict("source2"),
148  loadFromFiles_,
149  false
150  ).ptr()
151  );
152 
153  operation_ = operationTypeNames_.read(dict.lookup("operation"));
154  }
155 }
156 
157 
159 {
160  // Do nothing
161 }
162 
163 
165 {
166  if (active_)
167  {
168  source1Ptr_->write();
169  source2Ptr_->write();
170 
171  writeTime(file());
172 
173  if (log_) Info << type() << " " << name_ << " output:" << endl;
174 
175  const word& name1 = source1Ptr_->name();
176  const word& name2 = source2Ptr_->name();
177 
178  const wordList entries1 = objectResultEntries(name1);
179  const wordList entries2 = objectResultEntries(name2);
180 
181  if (entries1.size() != entries2.size())
182  {
184  << name_ << ": objects must generate the same number of results"
185  << nl
186  << " " << name1 << " objects: " << entries1 << nl
187  << " " << name2 << " objects: " << entries2 << nl
188  << exit(FatalError);
189  }
190 
191  forAll(entries1, i)
192  {
193  const word& entry1(entries1[i]);
194  const word& entry2(entries2[i]);
195  const word type1 = objectResultType(name1, entry1);
196  const word type2 = objectResultType(name2, entry2);
197 
198  if (type1 != type2)
199  {
201  << name_
202  << ": input values for operation must be of the same type"
203  << nl
204  << " " << entry1 << ": " << type1 << nl
205  << " " << entry2 << ": " << type2 << nl
206  << exit(FatalError);
207  }
208 
209  bool found = false;
210 
211  apply<scalar>(type1, name1, name2, entry1, entry2, found);
212  apply<vector>(type1, name1, name2, entry1, entry2, found);
213  apply<sphericalTensor>(type1, name1, name2, entry1, entry2, found);
214  apply<symmTensor>(type1, name1, name2, entry1, entry2, found);
215  apply<tensor>(type1, name1, name2, entry1, entry2, found);
216 
217  if (log_ && !found)
218  {
219  Info<< "Operation between "
220  << name1 << " with result " << entry1 << " and "
221  << name2 << " with result " << entry2 << " not applied"
222  << endl;
223  }
224  }
225 
226  if (log_)
227  {
228  if (entries1.empty())
229  {
230  Info<< " none";
231  }
232  Info<< endl;
233  }
234 
235  file()<< endl;
236  }
237 }
238 
239 
241 {
242  // Do nothing
243 }
244 
245 
247 {
248  // Do nothing
249 }
250 
251 
253 {
254  // Do nothing
255 }
256 
257 
259 {
260  // Do nothing
261 }
262 
263 
264 // ************************************************************************* //
volFields.H
Foam::Switch
A simple wrapper around bool so that it can be read as a word: true/false, on/off,...
Definition: Switch.H:60
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::functionObjectState
Base class for function objects, adding functionality to read/write state information (data required ...
Definition: functionObjectState.H:54
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
Foam::findIndex
label findIndex(const ListType &, typename ListType::const_reference, const label start=0)
Find first occurence of given element and return index,.
Foam::DynamicList< word >
Foam::read
bool read(const char *, int32_t &)
Definition: int32IO.C:87
Foam::functionObjectFile::read
void read(const dictionary &dict)
Read.
Definition: functionObjectFile.C:178
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
surfaceFields.H
Foam::surfaceFields.
Foam::polyMesh
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:74
Foam::objectRegistry
Registry of regIOobjects.
Definition: objectRegistry.H:50
Foam::fieldValues::fieldValueDelta::execute
virtual void execute()
Execute.
Definition: fieldValueDelta.C:164
Foam::label
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
Foam::fieldValues::fieldValueDelta::source1Ptr_
autoPtr< fieldValue > source1Ptr_
Field value source object 1.
Definition: fieldValueDelta.H:166
Foam::fieldValues::fieldValueDelta::~fieldValueDelta
virtual ~fieldValueDelta()
Destructor.
Definition: fieldValueDelta.C:117
Foam::nl
static const char nl
Definition: Ostream.H:260
Foam::Info
messageStream Info
Foam::fieldValues::fieldValueDelta::updateMesh
virtual void updateMesh(const mapPolyMesh &)
Update mesh.
Definition: fieldValueDelta.C:252
Foam::fieldValues::fieldValueDelta::timeSet
virtual void timeSet()
Called when time was set at the end of the Time::operator++.
Definition: fieldValueDelta.C:246
Foam::fieldValues::fieldValueDelta::writeFileHeader
virtual void writeFileHeader(Ostream &os) const
Output file header information.
Definition: fieldValueDelta.C:59
Foam::fieldValues::fieldValueDelta::operation_
operationType operation_
Operation to apply to values.
Definition: fieldValueDelta.H:163
fieldValueDelta.H
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:137
Foam::fieldValues::fieldValueDelta::read
virtual void read(const dictionary &)
Read from dictionary.
Definition: fieldValueDelta.C:123
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::DynamicList::append
DynamicList< T, SizeInc, SizeMult, SizeDiv > & append(const T &)
Append an element at the end of the list.
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
Foam::fieldValues::fieldValueDelta::movePoints
virtual void movePoints(const polyMesh &)
Move points.
Definition: fieldValueDelta.C:258
found
bool found
Definition: TABSMDCalcMethod2.H:32
Foam::functionObjectFile::writeHeaderValue
void writeHeaderValue(Ostream &os, const string &property, const Type &value) const
Write a (commented) header property and value pair.
Definition: functionObjectFileTemplates.C:32
Foam::fieldValue::New
static autoPtr< fieldValue > New(const word &name, const objectRegistry &obr, const dictionary &dict, const bool loadFromFiles=false, const bool output=true)
Return a reference to the selected fieldValue.
Definition: fieldValueNew.C:31
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:318
Foam::fieldValues::fieldValueDelta::source2Ptr_
autoPtr< fieldValue > source2Ptr_
Field value source object 2.
Definition: fieldValueDelta.H:169
Foam::tab
static const char tab
Definition: Ostream.H:259
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
Foam::functionObjectFile
Base class for output file data handling.
Definition: functionObjectFile.H:57
Foam::mapPolyMesh
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
Definition: mapPolyMesh.H:158
ListOps.H
Various functions to operate on Lists.
Foam::List::size
void size(const label)
Override size to be inconsistent with allocated storage.
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:53
Foam::type
fileName::Type type(const fileName &)
Return the file type: DIRECTORY or FILE.
Definition: POSIX.C:588
Foam::fieldValues::fieldValueDelta::fieldValueDelta
fieldValueDelta(const word &name, const objectRegistry &obr, const dictionary &dict, const bool loadFromFiles=false)
Construct from components.
Definition: fieldValueDelta.C:91
Foam::functionObjectFile::writeCommented
void writeCommented(Ostream &os, const string &str) const
Write a commented string to stream.
Definition: functionObjectFile.C:219
Foam::name
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
Foam::fieldValues::defineTypeNameAndDebug
defineTypeNameAndDebug(cellSource, 0)
Foam::fieldValues::fieldValueDelta::operationTypeNames_
static const NamedEnum< operationType, 5 > operationTypeNames_
Operation type names.
Definition: fieldValueDelta.H:146
Foam::NamedEnum
Initialise the NamedEnum HashTable from the static list of names.
Definition: NamedEnum.H:52
Foam::fieldValues::fieldValueDelta::end
virtual void end()
Execute the at the final time-loop, currently does nothing.
Definition: fieldValueDelta.C:240
Foam::fieldValues::fieldValueDelta::write
virtual void write()
Calculate and write.
Definition: fieldValueDelta.C:158