FreeFOAM The Cross-Platform CFD Toolkit
TimeActivatedExplicitSource_.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) 2010-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 
27 #include <finiteVolume/fvMesh.H>
28 #include <finiteVolume/volFields.H>
29 
30 // * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //
31 
32 template<class Type>
35 (
36  IStringStream("(points cellSet cellZone all)")()
37 );
38 
39 
40 template<class Type>
43 (
44  IStringStream("(absolute specific)")()
45 );
46 
47 
48 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
49 
50 template<class Type>
53 (
54  const word& smtName
55 ) const
56 {
57  forAll(selectionModeTypeNames_, i)
58  {
59  if (smtName == selectionModeTypeNames_[i])
60  {
61  return selectionModeType(i);
62  }
63  }
64 
66  (
67  "TimeActivatedExplicitSource<Type>::selectionModeType"
68  "TimeActivatedExplicitSource<Type>::wordToSelectionModeType"
69  "("
70  "const word&"
71  ")"
72  ) << "Unknown selectionMode type " << smtName
73  << ". Valid selectionMode types are:" << nl << selectionModeTypeNames_
74  << exit(FatalError);
75 
76  return selectionModeType(0);
77 }
78 
79 
80 template<class Type>
83 (
84  const word& vmtName
85 ) const
86 {
87  forAll(volumeModeTypeNames_, i)
88  {
89  if (vmtName == volumeModeTypeNames_[i])
90  {
91  return volumeModeType(i);
92  }
93  }
94 
96  (
97  "TimeActivatedExplicitSource<Type>::volumeModeType"
98  "TimeActivatedExplicitSource<Type>::wordToVolumeModeType(const word&)"
99  ) << "Unknown volumeMode type " << vmtName
100  << ". Valid volumeMode types are:" << nl << volumeModeTypeNames_
101  << exit(FatalError);
102 
103  return volumeModeType(0);
104 }
105 
106 
107 template<class Type>
109 (
110  const selectionModeType& smtType
111 ) const
112 {
113  if (smtType > selectionModeTypeNames_.size())
114  {
115  return "UNKNOWN";
116  }
117  else
118  {
119  return selectionModeTypeNames_[smtType];
120  }
121 }
122 
123 
124 template<class Type>
126 (
127  const volumeModeType& vmtType
128 ) const
129 {
130  if (vmtType > volumeModeTypeNames_.size())
131  {
132  return "UNKNOWN";
133  }
134  else
135  {
136  return volumeModeTypeNames_[vmtType];
137  }
138 }
139 
140 
141 template<class Type>
143 (
144  const dictionary& dict
145 )
146 {
147  switch (selectionMode_)
148  {
149  case smPoints:
150  {
151  dict.lookup("points") >> points_;
152  break;
153  }
154  case smCellSet:
155  {
156  dict.lookup("cellSet") >> cellSetName_;
157  break;
158  }
159  case smCellZone:
160  {
161  dict.lookup("cellZone") >> cellSetName_;
162  break;
163  }
164  case smAll:
165  {
166  break;
167  }
168  default:
169  {
171  (
172  "TimeActivatedExplicitSource::setSelection(const dictionary&)"
173  ) << "Unknown selectionMode "
174  << selectionModeTypeNames_[selectionMode_]
175  << ". Valid selectionMode types are" << selectionModeTypeNames_
176  << exit(FatalError);
177  }
178  }
179 }
180 
181 
182 template<class Type>
184 (
185  const dictionary& dict,
186  const wordList& fieldNames
187 )
188 {
189  dict.lookup("fieldData") >> fieldData_;
190  labelList localFieldIds(fieldData_.size(), -1);
191  forAll(fieldNames, i)
192  {
193  forAll(fieldData_, j)
194  {
195  const word& fdName = fieldData_[j].first();
196  if (fdName == fieldNames[i])
197  {
198  fieldIds_[i] = j;
199  localFieldIds[j] = i;
200  break;
201  }
202  }
203  }
204  forAll(localFieldIds, i)
205  {
206  if (localFieldIds[i] < 0)
207  {
209  (
210  "TimeActivatedExplicitSource<Type>::setFieldData"
211  "("
212  "const dictionary&, "
213  "const wordList&"
214  ")"
215  ) << "Field " << fieldData_[i].first() << " not found in "
216  << "field list. Available fields are: " << nl << fieldNames
217  << exit(FatalError);
218  }
219  }
220 }
221 
222 
223 template<class Type>
225 {
226  Info<< incrIndent << indent << "Source: " << name_ << endl;
227  switch (selectionMode_)
228  {
229  case smPoints:
230  {
231  Info<< indent << "- selecting cells using points" << endl;
232 
233  labelHashSet selectedCells;
234 
235  forAll(points_, i)
236  {
237  label cellI = mesh_.findCell(points_[i]);
238  if (cellI >= 0)
239  {
240  selectedCells.insert(cellI);
241  }
242 
243  label globalCellI = returnReduce(cellI, maxOp<label>());
244  if (globalCellI < 0)
245  {
246  WarningIn("TimeActivatedExplicitSource<Type>::setCellIds()")
247  << "Unable to find owner cell for point " << points_[i]
248  << endl;
249  }
250  }
251 
252  cells_ = selectedCells.toc();
253 
254  break;
255  }
256  case smCellSet:
257  {
258  Info<< indent << "- selecting cells using cellSet "
259  << cellSetName_ << endl;
260 
261  cellSet selectedCells(mesh_, cellSetName_);
262  cells_ = selectedCells.toc();
263 
264  break;
265  }
266  case smCellZone:
267  {
268  Info<< indent << "- selecting cells using cellZone "
269  << cellSetName_ << endl;
270  label zoneID = mesh_.cellZones().findZoneID(cellSetName_);
271  if (zoneID == -1)
272  {
273  FatalErrorIn("TimeActivatedExplicitSource<Type>::setCellIds()")
274  << "Cannot find cellZone " << cellSetName_ << endl
275  << "Valid cellZones are " << mesh_.cellZones().names()
276  << exit(FatalError);
277  }
278  cells_ = mesh_.cellZones()[zoneID];
279 
280  break;
281  }
282  case smAll:
283  {
284  Info<< indent << "- selecting all cells" << endl;
285  cells_ = identity(mesh_.nCells());
286 
287  break;
288  }
289  default:
290  {
291  FatalErrorIn("TimeActivatedExplicitSource<Type>::setCellIds()")
292  << "Unknown selectionMode "
293  << selectionModeTypeNames_[selectionMode_]
294  << ". Valid selectionMode types are" << selectionModeTypeNames_
295  << exit(FatalError);
296  }
297  }
298 
299  // Set volume normalisation
300  if (volumeMode_ == vmAbsolute)
301  {
302  V_ = 0.0;
303  forAll(cells_, i)
304  {
305  V_ += mesh_.V()[cells_[i]];
306  }
307  reduce(V_, sumOp<scalar>());
308  }
309 
310  Info<< indent << "- selected "
311  << returnReduce(cells_.size(), sumOp<label>())
312  << " cell(s) with volume " << V_ << nl << decrIndent << endl;
313 }
314 
315 
316 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
317 
318 template<class Type>
320 (
321  const word& name,
322  const dictionary& dict,
323  const fvMesh& mesh,
324  const wordList& fieldNames
325 )
326 :
327  name_(name),
328  mesh_(mesh),
329  active_(readBool(dict.lookup("active"))),
330  timeStart_(readScalar(dict.lookup("timeStart"))),
331  duration_(readScalar(dict.lookup("duration"))),
332  volumeMode_(wordToVolumeModeType(dict.lookup("volumeMode"))),
333  selectionMode_(wordToSelectionModeType(dict.lookup("selectionMode"))),
334  points_(),
335  cellSetName_("none"),
336  V_(1.0),
337  fieldData_(),
338  fieldIds_(fieldNames.size(), -1)
339 {
340  setSelection(dict);
341 
342  if (fieldNames.size() == 1)
343  {
344  fieldData_.setSize(1);
345  fieldData_[0].first() = fieldNames[0];
346  dict.lookup("fieldData") >> fieldData_[0].second();
347  fieldIds_[0] = 0;
348  }
349  else
350  {
351  setFieldData(dict, fieldNames);
352  }
353 
354  setCellSet();
355 }
356 
357 
358 template<class Type>
360 (
362  const label fieldI
363 )
364 {
365  const label fid = fieldIds_[fieldI];
366 
367  if
368  (
369  active_
370  && (fid >= 0)
371  && (mesh_.time().value() >= timeStart_)
372  && (mesh_.time().value() <= timeEnd())
373  )
374  {
375  // Update the cell set if the mesh is changing
376  if (mesh_.changing())
377  {
378  setCellSet();
379  }
380 
381  forAll(cells_, i)
382  {
383  Su[cells_[i]] = fieldData_[fid].second()/V_;
384  }
385  }
386 }
387 
388 
389 // ************************ vim: set sw=4 sts=4 et: ************************ //