FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

TimeActivatedExplicitSource_.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 2010-2010 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software: you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
00023 
00024 \*---------------------------------------------------------------------------*/
00025 
00026 #include "TimeActivatedExplicitSource_.H"
00027 #include <finiteVolume/fvMesh.H>
00028 #include <finiteVolume/volFields.H>
00029 
00030 // * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //
00031 
00032 template<class Type>
00033 const Foam::wordList Foam::TimeActivatedExplicitSource<Type>::
00034 selectionModeTypeNames_
00035 (
00036     IStringStream("(points cellSet cellZone all)")()
00037 );
00038 
00039 
00040 template<class Type>
00041 const Foam::wordList Foam::TimeActivatedExplicitSource<Type>::
00042 volumeModeTypeNames_
00043 (
00044     IStringStream("(absolute specific)")()
00045 );
00046 
00047 
00048 // * * * * * * * * * * * * Protected Member Functions  * * * * * * * * * * * //
00049 
00050 template<class Type>
00051 typename Foam::TimeActivatedExplicitSource<Type>::selectionModeType
00052 Foam::TimeActivatedExplicitSource<Type>::wordToSelectionModeType
00053 (
00054     const word& smtName
00055 ) const
00056 {
00057     forAll(selectionModeTypeNames_, i)
00058     {
00059         if (smtName == selectionModeTypeNames_[i])
00060         {
00061             return selectionModeType(i);
00062         }
00063     }
00064 
00065     FatalErrorIn
00066     (
00067         "TimeActivatedExplicitSource<Type>::selectionModeType"
00068         "TimeActivatedExplicitSource<Type>::wordToSelectionModeType"
00069         "("
00070             "const word&"
00071         ")"
00072     )   << "Unknown selectionMode type " << smtName
00073         << ". Valid selectionMode types are:" << nl << selectionModeTypeNames_
00074         << exit(FatalError);
00075 
00076     return selectionModeType(0);
00077 }
00078 
00079 
00080 template<class Type>
00081 typename Foam::TimeActivatedExplicitSource<Type>::volumeModeType
00082 Foam::TimeActivatedExplicitSource<Type>::wordToVolumeModeType
00083 (
00084     const word& vmtName
00085 ) const
00086 {
00087     forAll(volumeModeTypeNames_, i)
00088     {
00089         if (vmtName == volumeModeTypeNames_[i])
00090         {
00091             return volumeModeType(i);
00092         }
00093     }
00094 
00095     FatalErrorIn
00096     (
00097         "TimeActivatedExplicitSource<Type>::volumeModeType"
00098         "TimeActivatedExplicitSource<Type>::wordToVolumeModeType(const word&)"
00099     )   << "Unknown volumeMode type " << vmtName
00100         << ". Valid volumeMode types are:" << nl << volumeModeTypeNames_
00101         << exit(FatalError);
00102 
00103     return volumeModeType(0);
00104 }
00105 
00106 
00107 template<class Type>
00108 Foam::word Foam::TimeActivatedExplicitSource<Type>::selectionModeTypeToWord
00109 (
00110     const selectionModeType& smtType
00111 ) const
00112 {
00113     if (smtType > selectionModeTypeNames_.size())
00114     {
00115         return "UNKNOWN";
00116     }
00117     else
00118     {
00119         return selectionModeTypeNames_[smtType];
00120     }
00121 }
00122 
00123 
00124 template<class Type>
00125 Foam::word Foam::TimeActivatedExplicitSource<Type>::volumeModeTypeToWord
00126 (
00127     const volumeModeType& vmtType
00128 ) const
00129 {
00130     if (vmtType > volumeModeTypeNames_.size())
00131     {
00132         return "UNKNOWN";
00133     }
00134     else
00135     {
00136         return volumeModeTypeNames_[vmtType];
00137     }
00138 }
00139 
00140 
00141 template<class Type>
00142 void Foam::TimeActivatedExplicitSource<Type>::setSelection
00143 (
00144     const dictionary& dict
00145 )
00146 {
00147     switch (selectionMode_)
00148     {
00149         case smPoints:
00150         {
00151             dict.lookup("points") >> points_;
00152             break;
00153         }
00154         case smCellSet:
00155         {
00156             dict.lookup("cellSet") >> cellSetName_;
00157             break;
00158         }
00159         case smCellZone:
00160         {
00161             dict.lookup("cellZone") >> cellSetName_;
00162             break;
00163         }
00164         case smAll:
00165         {
00166             break;
00167         }
00168         default:
00169         {
00170             FatalErrorIn
00171             (
00172                 "TimeActivatedExplicitSource::setSelection(const dictionary&)"
00173             )   << "Unknown selectionMode "
00174                 << selectionModeTypeNames_[selectionMode_]
00175                 << ". Valid selectionMode types are" << selectionModeTypeNames_
00176                 << exit(FatalError);
00177         }
00178     }
00179 }
00180 
00181 
00182 template<class Type>
00183 void Foam::TimeActivatedExplicitSource<Type>::setFieldData
00184 (
00185     const dictionary& dict,
00186     const wordList& fieldNames
00187 )
00188 {
00189     dict.lookup("fieldData") >> fieldData_;
00190     labelList localFieldIds(fieldData_.size(), -1);
00191     forAll(fieldNames, i)
00192     {
00193         forAll(fieldData_, j)
00194         {
00195             const word& fdName = fieldData_[j].first();
00196             if (fdName == fieldNames[i])
00197             {
00198                 fieldIds_[i] = j;
00199                 localFieldIds[j] = i;
00200                 break;
00201             }
00202         }
00203     }
00204     forAll(localFieldIds, i)
00205     {
00206         if (localFieldIds[i] < 0)
00207         {
00208             FatalErrorIn
00209             (
00210                 "TimeActivatedExplicitSource<Type>::setFieldData"
00211                 "("
00212                     "const dictionary&, "
00213                     "const wordList&"
00214                 ")"
00215             )   << "Field " << fieldData_[i].first() << " not found in "
00216                 << "field list. Available fields are: " << nl << fieldNames
00217                 << exit(FatalError);
00218         }
00219     }
00220 }
00221 
00222 
00223 template<class Type>
00224 void Foam::TimeActivatedExplicitSource<Type>::setCellSet()
00225 {
00226     Info<< incrIndent << indent << "Source: " << name_ << endl;
00227     switch (selectionMode_)
00228     {
00229         case smPoints:
00230         {
00231             Info<< indent << "- selecting cells using points" << endl;
00232 
00233             labelHashSet selectedCells;
00234 
00235             forAll(points_, i)
00236             {
00237                 label cellI = mesh_.findCell(points_[i]);
00238                 if (cellI >= 0)
00239                 {
00240                     selectedCells.insert(cellI);
00241                 }
00242 
00243                 label globalCellI = returnReduce(cellI, maxOp<label>());
00244                 if (globalCellI < 0)
00245                 {
00246                     WarningIn("TimeActivatedExplicitSource<Type>::setCellIds()")
00247                         << "Unable to find owner cell for point " << points_[i]
00248                         << endl;
00249                 }
00250             }
00251 
00252             cells_ = selectedCells.toc();
00253 
00254             break;
00255         }
00256         case smCellSet:
00257         {
00258             Info<< indent << "- selecting cells using cellSet "
00259                 << cellSetName_ << endl;
00260 
00261             cellSet selectedCells(mesh_, cellSetName_);
00262             cells_ = selectedCells.toc();
00263 
00264             break;
00265         }
00266         case smCellZone:
00267         {
00268             Info<< indent << "- selecting cells using cellZone "
00269                 << cellSetName_ << endl;
00270             label zoneID = mesh_.cellZones().findZoneID(cellSetName_);
00271             if (zoneID == -1)
00272             {
00273                 FatalErrorIn("TimeActivatedExplicitSource<Type>::setCellIds()")
00274                     << "Cannot find cellZone " << cellSetName_ << endl
00275                     << "Valid cellZones are " << mesh_.cellZones().names()
00276                     << exit(FatalError);
00277             }
00278             cells_ = mesh_.cellZones()[zoneID];
00279 
00280             break;
00281         }
00282         case smAll:
00283         {
00284             Info<< indent << "- selecting all cells" << endl;
00285             cells_ = identity(mesh_.nCells());
00286 
00287             break;
00288         }
00289         default:
00290         {
00291             FatalErrorIn("TimeActivatedExplicitSource<Type>::setCellIds()")
00292                 << "Unknown selectionMode "
00293                 << selectionModeTypeNames_[selectionMode_]
00294                 << ". Valid selectionMode types are" << selectionModeTypeNames_
00295                 << exit(FatalError);
00296         }
00297     }
00298 
00299     // Set volume normalisation
00300     if (volumeMode_ == vmAbsolute)
00301     {
00302         V_ = 0.0;
00303         forAll(cells_, i)
00304         {
00305             V_ += mesh_.V()[cells_[i]];
00306         }
00307         reduce(V_, sumOp<scalar>());
00308     }
00309 
00310     Info<< indent << "- selected "
00311         << returnReduce(cells_.size(), sumOp<label>())
00312         << " cell(s) with volume " << V_ << nl << decrIndent << endl;
00313 }
00314 
00315 
00316 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00317 
00318 template<class Type>
00319 Foam::TimeActivatedExplicitSource<Type>::TimeActivatedExplicitSource
00320 (
00321     const word& name,
00322     const dictionary& dict,
00323     const fvMesh& mesh,
00324     const wordList& fieldNames
00325 )
00326 :
00327     name_(name),
00328     mesh_(mesh),
00329     active_(readBool(dict.lookup("active"))),
00330     timeStart_(readScalar(dict.lookup("timeStart"))),
00331     duration_(readScalar(dict.lookup("duration"))),
00332     volumeMode_(wordToVolumeModeType(dict.lookup("volumeMode"))),
00333     selectionMode_(wordToSelectionModeType(dict.lookup("selectionMode"))),
00334     points_(),
00335     cellSetName_("none"),
00336     V_(1.0),
00337     fieldData_(),
00338     fieldIds_(fieldNames.size(), -1)
00339 {
00340     setSelection(dict);
00341 
00342     if (fieldNames.size() == 1)
00343     {
00344         fieldData_.setSize(1);
00345         fieldData_[0].first() = fieldNames[0];
00346         dict.lookup("fieldData") >> fieldData_[0].second();
00347         fieldIds_[0] = 0;
00348     }
00349     else
00350     {
00351         setFieldData(dict, fieldNames);
00352     }
00353 
00354     setCellSet();
00355 }
00356 
00357 
00358 template<class Type>
00359 void Foam::TimeActivatedExplicitSource<Type>::addToField
00360 (
00361     DimensionedField<Type, volMesh>& Su,
00362     const label fieldI
00363 )
00364 {
00365     const label fid = fieldIds_[fieldI];
00366 
00367     if
00368     (
00369         active_
00370      && (fid >= 0)
00371      && (mesh_.time().value() >= timeStart_)
00372      && (mesh_.time().value() <= timeEnd())
00373     )
00374     {
00375         // Update the cell set if the mesh is changing
00376         if (mesh_.changing())
00377         {
00378             setCellSet();
00379         }
00380 
00381         forAll(cells_, i)
00382         {
00383             Su[cells_[i]] = fieldData_[fid].second()/V_;
00384         }
00385     }
00386 }
00387 
00388 
00389 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines