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 
76 void Foam::fieldValues::cellSource::setCellZoneCells()
77 {
78  switch (source_)
79  {
80  case stCellZone:
81  {
82  dict().lookup("sourceName") >> sourceName_;
83 
84  label zoneId = mesh().cellZones().findZoneID(sourceName_);
85 
86  if (zoneId < 0)
87  {
88  FatalErrorIn("cellSource::cellSource::setCellZoneCells()")
89  << "Unknown cell zone name: " << sourceName_
90  << ". Valid cell zones are: " << mesh().cellZones().names()
91  << nl << exit(FatalError);
92  }
93 
94  cellId_ = mesh().cellZones()[zoneId];
95  nCells_ = returnReduce(cellId_.size(), sumOp<label>());
96  break;
97  }
98 
99  case stAll:
100  {
101  cellId_ = identity(mesh().nCells());
102  nCells_ = returnReduce(cellId_.size(), sumOp<label>());
103  break;
104  }
105 
106  default:
107  {
108  FatalErrorIn("cellSource::setCellZoneCells()")
109  << "Unknown source type. Valid source types are:"
110  << sourceTypeNames_ << nl << exit(FatalError);
111  }
112  }
113 
114  if (debug)
115  {
116  Pout<< "Selected source size = " << cellId_.size() << endl;
117  }
118 }
119 
120 
121 Foam::scalar Foam::fieldValues::cellSource::volume() const
122 {
123  return gSum(filterField(mesh().V()));
124 }
125 
126 
127 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
128 
130 {
131  setCellZoneCells();
132 
133  if (nCells_ == 0)
134  {
135  WarningIn
136  (
137  "Foam::fieldValues::cellSource::initialise(const dictionary&)"
138  ) << type() << " " << name_ << ": "
139  << sourceTypeNames_[source_] << "(" << sourceName_ << "):" << nl
140  << " Source has no cells - deactivating" << endl;
141 
142  active_ = false;
143  return;
144  }
145 
146  volume_ = volume();
147 
148  Info<< type() << " " << name_ << ":"
149  << sourceTypeNames_[source_] << "(" << sourceName_ << "):" << nl
150  << " total cells = " << nCells_ << nl
151  << " total volume = " << volume_
152  << nl << endl;
153 
154  if (dict.readIfPresent("weightField", weightFieldName_))
155  {
156  Info<< " weight field = " << weightFieldName_;
157  }
158 
159  Info<< nl << endl;
160 }
161 
162 
164 {
165  writeCommented(file(), "Source : ");
166  file() << sourceTypeNames_[source_] << " " << sourceName_ << endl;
167  writeCommented(file(), "Cells : ");
168  file() << nCells_ << endl;
169  writeCommented(file(), "Volume : ");
170  file() << volume_ << endl;
171 
172  writeCommented(file(), "Time");
173  if (writeVolume_)
174  {
175  file() << tab << "Volume";
176  }
177 
178  forAll(fields_, i)
179  {
180  file()
181  << tab << operationTypeNames_[operation_]
182  << "(" << fields_[i] << ")";
183  }
184 
185  file() << endl;
186 }
187 
188 
189 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
190 
192 (
193  const word& name,
194  const objectRegistry& obr,
195  const dictionary& dict,
196  const bool loadFromFiles
197 )
198 :
199  fieldValue(name, obr, dict, typeName, loadFromFiles),
200  source_(sourceTypeNames_.read(dict.lookup("source"))),
201  operation_(operationTypeNames_.read(dict.lookup("operation"))),
202  nCells_(0),
203  cellId_(),
204  weightFieldName_("none"),
205  writeVolume_(dict.lookupOrDefault("writeVolume", false))
206 {
207  read(dict);
208 }
209 
210 
211 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
212 
214 {}
215 
216 
217 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
218 
220 {
221  fieldValue::read(dict);
222 
223  if (active_)
224  {
225  // no additional info to read
226  initialise(dict);
227  }
228 }
229 
230 
232 {
234 
235  if (active_)
236  {
237  if (Pstream::master())
238  {
239  file() << obr_.time().value();
240  }
241 
242  if (writeVolume_)
243  {
244  volume_ = volume();
245  if (Pstream::master())
246  {
247  file() << tab << volume_;
248  }
249  if (log_) Info<< " total volume = " << volume_ << endl;
250  }
251 
252  forAll(fields_, i)
253  {
254  const word& fieldName = fields_[i];
255  bool processed = false;
256 
257  processed = processed || writeValues<scalar>(fieldName);
258  processed = processed || writeValues<vector>(fieldName);
259  processed = processed || writeValues<sphericalTensor>(fieldName);
260  processed = processed || writeValues<symmTensor>(fieldName);
261  processed = processed || writeValues<tensor>(fieldName);
262 
263  if (!processed)
264  {
265  WarningIn("void Foam::fieldValues::cellSource::write()")
266  << "Requested field " << fieldName
267  << " not found in database and not processed"
268  << endl;
269  }
270  }
271 
272  if (Pstream::master())
273  {
274  file()<< endl;
275  }
276 
277  if (log_) Info<< endl;
278  }
279 }
280 
281 
282 // ************************************************************************* //
cellSource(const word &name, const objectRegistry &obr, const dictionary &dict, const bool loadFromFiles=false)
Construct from components.
Definition: cellSource.C:192
T returnReduce(const T &Value, const BinaryOp &bop, const int tag=Pstream::msgType(), const label comm=UPstream::worldComm)
virtual void write()
Calculate and write.
Definition: cellSource.C:231
A class for handling words, derived from string.
Definition: word.H:59
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
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
Base class for field value -based function objects.
Definition: fieldValue.H:62
messageStream Info
dynamicFvMesh & mesh
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:137
virtual void write()
Write to screen/file.
Definition: fieldValue.C:55
Namespace for OpenFOAM.
virtual void read(const dictionary &dict)
Read from dictionary.
Definition: fieldValue.C:42
Type gSum(const FieldField< Field, Type > &f)
dictionary dict
static const char nl
Definition: Ostream.H:260
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
#define WarningIn(functionName)
Report a warning using Foam::Warning.
static const NamedEnum< sourceType, 2 > sourceTypeNames_
Source type names.
Definition: cellSource.H:242
#define forAll(list, i)
Definition: UList.H:421
void initialise(const dictionary &dict)
Initialise, e.g. cell addressing.
Definition: cellSource.C:129
Macros for easy insertion into run-time selection tables.
virtual ~cellSource()
Destructor.
Definition: cellSource.C:213
labelList identity(const label len)
Create identity map (map[i] == i) of given length.
Definition: ListOps.C:104
virtual void read(const dictionary &)
Read from dictionary.
Definition: cellSource.C:219
fileName::Type type(const fileName &)
Return the file type: DIRECTORY or FILE.
Definition: POSIX.C:589
Initialise the NamedEnum HashTable from the static list of names.
Definition: NamedEnum.H:52
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:314
addToRunTimeSelectionTable(fieldValue, cellSource, dictionary)
static const char tab
Definition: Ostream.H:259
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:452
error FatalError
virtual void writeFileHeader(const label i)
Output file header information.
Definition: cellSource.C:163
T lookupOrDefault(const word &, const T &, bool recursive=false, bool patternMatch=true) const
Find and return a T,.
Registry of regIOobjects.
defineTypeNameAndDebug(cellSource, 0)
bool readIfPresent(const word &, T &, bool recursive=false, bool patternMatch=true) const
Find an entry if present, and assign to T.
This function object provides a &#39;cell source&#39; variant of the fieldValues function object...
Definition: cellSource.H:225
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:398
bool read(const char *, int32_t &)
Definition: int32IO.C:87
static const NamedEnum< operationType, 11 > operationTypeNames_
Operation type names.
Definition: cellSource.H:262
prefixOSstream Pout(cout,"Pout")
Definition: IOstreams.H:53