ENH: autoLayerDriver: better layering information message
[OpenFOAM-2.0.x.git] / src / finiteVolume / cfdTools / general / fieldSources / timeActivatedExplicitSource / TimeActivatedExplicitSource.C
blobe9123bee34251cb73adaf22d44bb94c0ab4faeac
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 2011 OpenFOAM Foundation
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
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.
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.
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/>.
24 \*---------------------------------------------------------------------------*/
26 #include "TimeActivatedExplicitSource.H"
27 #include "fvMesh.H"
28 #include "volFields.H"
30 // * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //
32 template<class Type>
33 const Foam::wordList Foam::TimeActivatedExplicitSource<Type>::
34 selectionModeTypeNames_
36     IStringStream("(points cellSet cellZone all)")()
40 template<class Type>
41 const Foam::wordList Foam::TimeActivatedExplicitSource<Type>::
42 volumeModeTypeNames_
44     IStringStream("(absolute specific)")()
48 // * * * * * * * * * * * * Protected Member Functions  * * * * * * * * * * * //
50 template<class Type>
51 typename Foam::TimeActivatedExplicitSource<Type>::selectionModeType
52 Foam::TimeActivatedExplicitSource<Type>::wordToSelectionModeType
54     const word& smtName
55 ) const
57     forAll(selectionModeTypeNames_, i)
58     {
59         if (smtName == selectionModeTypeNames_[i])
60         {
61             return selectionModeType(i);
62         }
63     }
65     FatalErrorIn
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);
76     return selectionModeType(0);
80 template<class Type>
81 typename Foam::TimeActivatedExplicitSource<Type>::volumeModeType
82 Foam::TimeActivatedExplicitSource<Type>::wordToVolumeModeType
84     const word& vmtName
85 ) const
87     forAll(volumeModeTypeNames_, i)
88     {
89         if (vmtName == volumeModeTypeNames_[i])
90         {
91             return volumeModeType(i);
92         }
93     }
95     FatalErrorIn
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);
103     return volumeModeType(0);
107 template<class Type>
108 Foam::word Foam::TimeActivatedExplicitSource<Type>::selectionModeTypeToWord
110     const selectionModeType& smtType
111 ) const
113     if (smtType > selectionModeTypeNames_.size())
114     {
115         return "UNKNOWN";
116     }
117     else
118     {
119         return selectionModeTypeNames_[smtType];
120     }
124 template<class Type>
125 Foam::word Foam::TimeActivatedExplicitSource<Type>::volumeModeTypeToWord
127     const volumeModeType& vmtType
128 ) const
130     if (vmtType > volumeModeTypeNames_.size())
131     {
132         return "UNKNOWN";
133     }
134     else
135     {
136         return volumeModeTypeNames_[vmtType];
137     }
141 template<class Type>
142 void Foam::TimeActivatedExplicitSource<Type>::setSelection
144     const dictionary& dict
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         {
170             FatalErrorIn
171             (
172                 "TimeActivatedExplicitSource::setSelection(const dictionary&)"
173             )   << "Unknown selectionMode "
174                 << selectionModeTypeNames_[selectionMode_]
175                 << ". Valid selectionMode types are" << selectionModeTypeNames_
176                 << exit(FatalError);
177         }
178     }
182 template<class Type>
183 void Foam::TimeActivatedExplicitSource<Type>::setFieldData
185     const dictionary& dict,
186     const wordList& fieldNames
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         {
208             FatalErrorIn
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     }
223 template<class Type>
224 void Foam::TimeActivatedExplicitSource<Type>::setCellSet()
226     Info<< incrIndent << indent << "Source: " << name_ << endl;
227     switch (selectionMode_)
228     {
229         case smPoints:
230         {
231             Info<< indent << "- selecting cells using points" << endl;
233             labelHashSet selectedCells;
235             forAll(points_, i)
236             {
237                 label cellI = mesh_.findCell(points_[i]);
238                 if (cellI >= 0)
239                 {
240                     selectedCells.insert(cellI);
241                 }
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             }
252             cells_ = selectedCells.toc();
254             break;
255         }
256         case smCellSet:
257         {
258             Info<< indent << "- selecting cells using cellSet "
259                 << cellSetName_ << endl;
261             cellSet selectedCells(mesh_, cellSetName_);
262             cells_ = selectedCells.toc();
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];
280             break;
281         }
282         case smAll:
283         {
284             Info<< indent << "- selecting all cells" << endl;
285             cells_ = identity(mesh_.nCells());
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     }
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     }
310     Info<< indent << "- selected "
311         << returnReduce(cells_.size(), sumOp<label>())
312         << " cell(s) with volume " << V_ << nl << decrIndent << endl;
316 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
318 template<class Type>
319 Foam::TimeActivatedExplicitSource<Type>::TimeActivatedExplicitSource
321     const word& name,
322     const dictionary& dict,
323     const fvMesh& mesh,
324     const wordList& fieldNames
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)
340     setSelection(dict);
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     }
354     setCellSet();
358 template<class Type>
359 void Foam::TimeActivatedExplicitSource<Type>::addToField
361     DimensionedField<Type, volMesh>& Su,
362     const label fieldI
365     const label fid = fieldIds_[fieldI];
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         }
381         forAll(cells_, i)
382         {
383             Su[cells_[i]] = fieldData_[fid].second()/V_;
384         }
385     }
389 // ************************************************************************* //