cellSource.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) 2011-2015 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 "cellSource.H"
27 #include "fvMesh.H"
28 #include "volFields.H"
30 
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
32 
33 namespace Foam
34 {
35  template<>
37  {
38  "cellZone",
39  "all"
40  };
41 
42 
43  template<>
45  {
46  "none",
47  "sum",
48  "sumMag",
49  "average",
50  "weightedAverage",
51  "volAverage",
52  "weightedVolAverage",
53  "volIntegrate",
54  "min",
55  "max",
56  "CoV"
57  };
58 
59  namespace fieldValues
60  {
63  }
64 }
65 
66 
69 
72 
73 
74 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
75 
77 {
78  switch (source_)
79  {
80  case stCellZone:
81  {
83 
84  if (zoneId < 0)
85  {
87  << "Unknown cell zone name: " << sourceName_
88  << ". Valid cell zones are: " << mesh().cellZones().names()
89  << nl << exit(FatalError);
90  }
91 
92  cellId_ = mesh().cellZones()[zoneId];
94  break;
95  }
96 
97  case stAll:
98  {
99  cellId_ = identity(mesh().nCells());
101  break;
102  }
103 
104  default:
105  {
107  << "Unknown source type. Valid source types are:"
109  }
110  }
111 
112  if (debug)
113  {
114  Pout<< "Selected source size = " << cellId_.size() << endl;
115  }
116 }
117 
118 
120 {
121  return gSum(filterField(mesh().V()));
122 }
123 
124 
125 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
126 
128 {
129  setCellZoneCells();
130 
131  if (nCells_ == 0)
132  {
134  << type() << " " << name_ << ": "
135  << sourceTypeNames_[source_] << "(" << sourceName_ << "):" << nl
136  << " Source has no cells - deactivating" << endl;
137 
138  active_ = false;
139  return;
140  }
141 
142  volume_ = volume();
143 
144  if (log_) Info
145  << type() << " " << name_ << ":"
146  << sourceTypeNames_[source_] << "(" << sourceName_ << "):" << nl
147  << " total cells = " << nCells_ << nl
148  << " total volume = " << volume_
149  << nl << endl;
150 
151  if (dict.readIfPresent("weightField", weightFieldName_))
152  {
153  if (log_) Info << " weight field = " << weightFieldName_;
154  }
155 
156  if (log_) Info << nl << endl;
157 }
158 
159 
161 {
162  writeHeaderValue(os, "Source", sourceTypeNames_[source_]);
163  writeHeaderValue(os, "Name", sourceName_);
164  writeHeaderValue(os, "Cells", nCells_);
165  writeHeaderValue(os, "Volume", volume_);
166  writeHeaderValue(os, "Scale factor", scaleFactor_);
167 
168 
169  writeCommented(os, "Time");
170  if (writeVolume_)
171  {
172  os << tab << "Volume";
173  }
174 
175  forAll(fields_, i)
176  {
177  os << tab << operationTypeNames_[operation_]
178  << "(" << fields_[i] << ")";
179  }
180 
181  os << endl;
182 }
183 
184 
185 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
186 
188 (
189  const word& name,
190  const objectRegistry& obr,
191  const dictionary& dict,
192  const bool loadFromFiles
193 )
194 :
195  fieldValue(name, obr, dict, typeName, loadFromFiles),
196  source_(sourceTypeNames_.read(dict.lookup("source"))),
197  operation_(operationTypeNames_.read(dict.lookup("operation"))),
198  nCells_(0),
199  cellId_(),
200  weightFieldName_("none"),
201  writeVolume_(dict.lookupOrDefault("writeVolume", false))
202 {
203  if (active_)
204  {
205  read(dict);
206  writeFileHeader(file());
207  }
208 }
209 
210 
211 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
212 
214 {}
215 
216 
217 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
218 
220 {
221  if (active_)
222  {
224 
225  // No additional info to read
226  initialise(dict);
227  }
228 }
229 
230 
232 {
234 
235  if (active_)
236  {
237  writeTime(file());
238 
239  // Construct weight field. Note: zero size indicates unweighted
240  scalarField weightField;
241  if (weightFieldName_ != "none")
242  {
243  weightField = setFieldValues<scalar>(weightFieldName_, true);
244  }
245 
246  if (writeVolume_)
247  {
248  volume_ = volume();
249  file() << tab << volume_;
250  if (log_) Info<< " total volume = " << volume_ << endl;
251  }
252 
253  forAll(fields_, i)
254  {
255  const word& fieldName = fields_[i];
256  bool ok = false;
257 
258  ok = ok || writeValues<scalar>(fieldName, weightField);
259  ok = ok || writeValues<vector>(fieldName, weightField);
260  ok = ok || writeValues<sphericalTensor>(fieldName, weightField);
261  ok = ok || writeValues<symmTensor>(fieldName, weightField);
262  ok = ok || writeValues<tensor>(fieldName, weightField);
263 
264  if (!ok)
265  {
267  << "Requested field " << fieldName
268  << " not found in database and not processed"
269  << endl;
270  }
271  }
272 
273  file()<< endl;
274 
275  if (log_) Info<< endl;
276  }
277 }
278 
279 
280 // ************************************************************************* //
Foam::fieldValues::cellSource::nCells_
label nCells_
Global number of cells.
Definition: cellSource.H:290
volFields.H
Foam::fieldValues::cellSource::~cellSource
virtual ~cellSource()
Destructor.
Definition: cellSource.C:213
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::returnReduce
T returnReduce(const T &Value, const BinaryOp &bop, const int tag=Pstream::msgType(), const label comm=UPstream::worldComm)
Definition: PstreamReduceOps.H:86
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
Foam::fieldValues::cellSource::write
virtual void write()
Calculate and write.
Definition: cellSource.C:231
Foam::fieldValues::cellSource::operationTypeNames_
static const NamedEnum< operationType, 11 > operationTypeNames_
Operation type names.
Definition: cellSource.H:262
Foam::fieldValues::cellSource::initialise
void initialise(const dictionary &dict)
Initialise, e.g. cell addressing.
Definition: cellSource.C:127
Foam::read
bool read(const char *, int32_t &)
Definition: int32IO.C:87
Foam::fieldValues::cellSource::stCellZone
@ stCellZone
Definition: cellSource.H:237
Foam::fieldValues::cellSource::volume
scalar volume() const
Calculate and return volume of the cellSource: sum(V)
Definition: cellSource.C:119
Foam::fieldValue::sourceName_
word sourceName_
Name of source object.
Definition: fieldValue.H:83
Foam::polyMesh::cellZones
const cellZoneMesh & cellZones() const
Return cell zone mesh.
Definition: polyMesh.H:469
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::fieldValues::cellSource::sourceTypeNames_
static const NamedEnum< sourceType, 2 > sourceTypeNames_
Source type names.
Definition: cellSource.H:242
Foam::gSum
Type gSum(const FieldField< Field, Type > &f)
Definition: FieldFieldFunctions.C:564
Foam::objectRegistry
Registry of regIOobjects.
Definition: objectRegistry.H:50
Foam::fieldValues::cellSource::source_
sourceType source_
Source type.
Definition: cellSource.H:284
Foam::fieldValue::read
virtual void read(const dictionary &dict)
Read from dictionary.
Definition: fieldValue.C:42
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::Field
Pre-declare SubField and related Field type.
Definition: Field.H:57
Foam::nl
static const char nl
Definition: Ostream.H:260
Foam::Info
messageStream Info
Foam::fieldValues::cellSource::writeFileHeader
virtual void writeFileHeader(Ostream &os) const
Output file header information.
Definition: cellSource.C:160
Foam::fieldValues::cellSource::cellSource
cellSource(const word &name, const objectRegistry &obr, const dictionary &dict, const bool loadFromFiles=false)
Construct from components.
Definition: cellSource.C:188
Foam::identity
labelList identity(const label len)
Create identity map (map[i] == i) of given length.
Definition: ListOps.C:104
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
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:18
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
fvMesh.H
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::ZoneMesh::findZoneID
label findZoneID(const word &zoneName) const
Find zone index given a name.
Definition: ZoneMesh.C:348
Foam::ZoneMesh::names
wordList names() const
Return a list of zone names.
Definition: ZoneMesh.C:263
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:318
Foam::sumOp
Definition: ops.H:162
Foam::tab
static const char tab
Definition: Ostream.H:259
Foam::Pout
prefixOSstream Pout(cout, "Pout")
Definition: IOstreams.H:53
Foam::fieldValue
Base class for field value-based function objects.
Definition: fieldValue.H:63
Foam::fieldValues::cellSource::stAll
@ stAll
Definition: cellSource.H:238
Foam::fieldValues::cellSource::read
virtual void read(const dictionary &)
Read from dictionary.
Definition: cellSource.C:219
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
cellSource.H
Foam::fieldValue::write
virtual void write()
Write to screen/file.
Definition: fieldValue.C:58
Foam::type
fileName::Type type(const fileName &)
Return the file type: DIRECTORY or FILE.
Definition: POSIX.C:588
Foam::fieldValues::addToRunTimeSelectionTable
addToRunTimeSelectionTable(fieldValue, cellSource, dictionary)
Foam::fieldValue::mesh
const fvMesh & mesh() const
Helper function to return the reference to the mesh.
Definition: fieldValueI.H:79
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:259
Foam::name
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
Foam::fieldValues::defineTypeNameAndDebug
defineTypeNameAndDebug(cellSource, 0)
Foam::NamedEnum
Initialise the NamedEnum HashTable from the static list of names.
Definition: NamedEnum.H:52
Foam::fieldValues::cellSource
This function object provides a 'cell source' variant of the fieldValues function object....
Definition: cellSource.H:225
Foam::fieldValues::cellSource::setCellZoneCells
void setCellZoneCells()
Set cells to evaluate based on a cell zone.
Definition: cellSource.C:76
Foam::fieldValues::cellSource::cellId_
labelList cellId_
Local list of cell IDs.
Definition: cellSource.H:293