FreeFOAM The Cross-Platform CFD Toolkit
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) 2009-2010 OpenCFD Ltd.
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 <finiteVolume/fvMesh.H>
28 #include <finiteVolume/volFields.H>
29 #include <OpenFOAM/IOList.H>
30 
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
32 
33 namespace Foam
34 {
35  namespace fieldValues
36  {
38  }
39 
40  template<>
42  names[] = {"cellZone", "all"};
43 
44  const NamedEnum<fieldValues::cellSource::sourceType, 2>
46 
47  template<>
48  const char* NamedEnum<fieldValues::cellSource::operationType, 7>::
49  names[] =
50  {
51  "none", "sum", "volAverage",
52  "volIntegrate", "weightedAverage", "min", "max"
53  };
54 
55  const NamedEnum<fieldValues::cellSource::operationType, 7>
57 
58 }
59 
60 
61 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
62 
63 void Foam::fieldValues::cellSource::setCellZoneCells()
64 {
65  switch (source_)
66  {
67  case stCellZone:
68  {
69  label zoneId = mesh().cellZones().findZoneID(sourceName_);
70 
71  if (zoneId < 0)
72  {
73  FatalErrorIn("cellSource::cellSource::setCellZoneCells()")
74  << "Unknown cell zone name: " << sourceName_
75  << ". Valid cell zones are: " << mesh().cellZones().names()
76  << nl << exit(FatalError);
77  }
78 
79  cellId_ = mesh().cellZones()[zoneId];
80  nCells_ = returnReduce(cellId_.size(), sumOp<label>());
81  break;
82  }
83 
84  case stAll:
85  {
86  cellId_ = identity(mesh().nCells());
87  nCells_ = returnReduce(cellId_.size(), sumOp<label>());
88  break;
89  }
90 
91  default:
92  {
93  FatalErrorIn("cellSource::setCellZoneCells()")
94  << "Unknown source type. Valid source types are:"
96  }
97  }
98 
99  if (debug)
100  {
101  Pout<< "Selected source size = " << cellId_.size() << endl;
102  }
103 }
104 
105 
106 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
107 
109 {
110  setCellZoneCells();
111 
112  Info<< type() << " " << name_ << ":" << nl
113  << " total cells = " << nCells_ << nl
114  << " total volume = " << gSum(filterField(mesh().V()))
115  << nl << endl;
116 
117  if (operation_ == opWeightedAverage)
118  {
119  dict.lookup("weightField") >> weightFieldName_;
120  if
121  (
122  obr().foundObject<volScalarField>(weightFieldName_)
123  )
124  {
125  Info<< " weight field = " << weightFieldName_;
126  }
127  else
128  {
129  FatalErrorIn("cellSource::initialise()")
130  << type() << " " << name_ << ": "
131  << sourceTypeNames_[source_] << "(" << sourceName_ << "):"
132  << nl << " Weight field " << weightFieldName_
133  << " must be a " << volScalarField::typeName
134  << nl << exit(FatalError);
135  }
136  }
137 
138  Info<< nl << endl;
139 }
140 
141 
143 {
144  if (outputFilePtr_.valid())
145  {
146  outputFilePtr_()
147  << "# Source : " << sourceTypeNames_[source_] << " "
148  << sourceName_ << nl << "# Cells : " << nCells_ << nl
149  << "# Time" << tab << "sum(V)";
150 
151  forAll(fields_, i)
152  {
153  outputFilePtr_()
154  << tab << operationTypeNames_[operation_]
155  << "(" << fields_[i] << ")";
156  }
157 
158  outputFilePtr_() << endl;
159  }
160 }
161 
162 
163 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
164 
166 (
167  const word& name,
168  const objectRegistry& obr,
169  const dictionary& dict,
170  const bool loadFromFiles
171 )
172 :
173  fieldValue(name, obr, dict, loadFromFiles),
174  source_(sourceTypeNames_.read(dict.lookup("source"))),
175  operation_(operationTypeNames_.read(dict.lookup("operation"))),
176  nCells_(0),
177  cellId_()
178 {
179  read(dict);
180 }
181 
182 
183 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
184 
186 {}
187 
188 
189 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
190 
192 {
193  fieldValue::read(dict);
194 
195  if (active_)
196  {
197  // no additional info to read
198  initialise(dict);
199  }
200 }
201 
202 
204 {
206 
207  if (active_)
208  {
209  if (Pstream::master())
210  {
211  outputFilePtr_()
212  << obr_.time().value() << tab
213  << sum(filterField(mesh().V()));
214  }
215 
216  forAll(fields_, i)
217  {
218  writeValues<scalar>(fields_[i]);
219  writeValues<vector>(fields_[i]);
220  writeValues<sphericalTensor>(fields_[i]);
221  writeValues<symmTensor>(fields_[i]);
222  writeValues<tensor>(fields_[i]);
223  }
224 
225  if (Pstream::master())
226  {
227  outputFilePtr_()<< endl;
228  }
229 
230  if (log_)
231  {
232  Info<< endl;
233  }
234  }
235 }
236 
237 
238 // ************************ vim: set sw=4 sts=4 et: ************************ //
239