OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

forces.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) 1991-2009 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 the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     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, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 #include "forces.H"
00028 #include "volFields.H"
00029 #include "dictionary.H"
00030 #include "Time.H"
00031 
00032 #include "incompressible/singlePhaseTransportModel/singlePhaseTransportModel.H"
00033 #include "incompressible/RAS/RASModel/RASModel.H"
00034 #include "incompressible/LES/LESModel/LESModel.H"
00035 
00036 #include "basicThermo.H"
00037 #include "compressible/RAS/RASModel/RASModel.H"
00038 #include "compressible/LES/LESModel/LESModel.H"
00039 
00040 
00041 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00042 
00043 namespace Foam
00044 {
00045     defineTypeNameAndDebug(forces, 0);
00046 }
00047 
00048 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00049 
00050 Foam::tmp<Foam::volSymmTensorField> Foam::forces::devRhoReff() const
00051 {
00052     if (obr_.foundObject<compressible::RASModel>("RASProperties"))
00053     {
00054         const compressible::RASModel& ras
00055             = obr_.lookupObject<compressible::RASModel>("RASProperties");
00056 
00057         return ras.devRhoReff();
00058     }
00059     else if (obr_.foundObject<incompressible::RASModel>("RASProperties"))
00060     {
00061         const incompressible::RASModel& ras
00062             = obr_.lookupObject<incompressible::RASModel>("RASProperties");
00063 
00064         return rhoRef_*ras.devReff();
00065     }
00066     else if (obr_.foundObject<compressible::LESModel>("LESProperties"))
00067     {
00068         const compressible::LESModel& les =
00069         obr_.lookupObject<compressible::LESModel>("LESProperties");
00070 
00071         return les.devRhoBeff();
00072     }
00073     else if (obr_.foundObject<incompressible::LESModel>("LESProperties"))
00074     {
00075         const incompressible::LESModel& les
00076             = obr_.lookupObject<incompressible::LESModel>("LESProperties");
00077 
00078         return rhoRef_*les.devBeff();
00079     }
00080     else if (obr_.foundObject<basicThermo>("thermophysicalProperties"))
00081     {
00082         const basicThermo& thermo =
00083              obr_.lookupObject<basicThermo>("thermophysicalProperties");
00084 
00085         const volVectorField& U = obr_.lookupObject<volVectorField>(UName_);
00086 
00087         return -thermo.mu()*dev(twoSymm(fvc::grad(U)));
00088     }
00089     else if
00090     (
00091         obr_.foundObject<singlePhaseTransportModel>("transportProperties")
00092     )
00093     {
00094         const singlePhaseTransportModel& laminarT =
00095             obr_.lookupObject<singlePhaseTransportModel>
00096             ("transportProperties");
00097 
00098         const volVectorField& U = obr_.lookupObject<volVectorField>(UName_);
00099 
00100         return -rhoRef_*laminarT.nu()*dev(twoSymm(fvc::grad(U)));
00101     }
00102     else if (obr_.foundObject<dictionary>("transportProperties"))
00103     {
00104         const dictionary& transportProperties =
00105              obr_.lookupObject<dictionary>("transportProperties");
00106 
00107         dimensionedScalar nu(transportProperties.lookup("nu"));
00108 
00109         const volVectorField& U = obr_.lookupObject<volVectorField>(UName_);
00110 
00111         return -rhoRef_*nu*dev(twoSymm(fvc::grad(U)));
00112     }
00113     else
00114     {
00115         FatalErrorIn("forces::devRhoReff()")
00116             << "No valid model for viscous stress calculation."
00117             << exit(FatalError);
00118 
00119         return volSymmTensorField::null();
00120     }
00121 }
00122 
00123 
00124 Foam::scalar Foam::forces::rho(const volScalarField& p) const
00125 {
00126     if (p.dimensions() == dimPressure)
00127     {
00128         return 1.0;
00129     }
00130     else
00131     {
00132         return rhoRef_;
00133     }
00134 }
00135 
00136 
00137 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00138 
00139 Foam::forces::forces
00140 (
00141     const word& name,
00142     const objectRegistry& obr,
00143     const dictionary& dict,
00144     const bool loadFromFiles
00145 )
00146 :
00147     name_(name),
00148     obr_(obr),
00149     active_(true),
00150     log_(false),
00151     patchSet_(),
00152     pName_(""),
00153     UName_(""),
00154     directForceDensity_(false),
00155     fDName_(""),
00156     rhoRef_(0),
00157     CofR_(vector::zero),
00158     forcesFilePtr_(NULL)
00159 {
00160     // Check if the available mesh is an fvMesh otherise deactivate
00161     if (!isA<fvMesh>(obr_))
00162     {
00163         active_ = false;
00164         WarningIn
00165         (
00166             "Foam::forces::forces"
00167             "("
00168                 "const word&, "
00169                 "const objectRegistry&, "
00170                 "const dictionary&, "
00171                 "const bool"
00172             ")"
00173         )   << "No fvMesh available, deactivating."
00174             << endl;
00175     }
00176 
00177     read(dict);
00178 }
00179 
00180 
00181 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00182 
00183 Foam::forces::~forces()
00184 {}
00185 
00186 
00187 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00188 
00189 void Foam::forces::read(const dictionary& dict)
00190 {
00191     if (active_)
00192     {
00193         log_ = dict.lookupOrDefault<Switch>("log", false);
00194 
00195         const fvMesh& mesh = refCast<const fvMesh>(obr_);
00196 
00197         patchSet_ =
00198             mesh.boundaryMesh().patchSet(wordList(dict.lookup("patches")));
00199 
00200         dict.readIfPresent("directForceDensity", directForceDensity_);
00201 
00202         if (directForceDensity_)
00203         {
00204             // Optional entry for fDName
00205             fDName_ = dict.lookupOrDefault<word>("fDName", "fD");
00206 
00207             // Check whether fDName exists, if not deactivate forces
00208             if
00209             (
00210                 !obr_.foundObject<volVectorField>(fDName_)
00211             )
00212             {
00213                 active_ = false;
00214                 WarningIn("void forces::read(const dictionary& dict)")
00215                 << "Could not find " << fDName_ << " in database." << nl
00216                     << "    De-activating forces."
00217                     << endl;
00218             }
00219         }
00220         else
00221         {
00222             // Optional entries U and p
00223             pName_ = dict.lookupOrDefault<word>("pName", "p");
00224             UName_ = dict.lookupOrDefault<word>("UName", "U");
00225 
00226             // Check whether UName and pName exists, if not deactivate forces
00227             if
00228             (
00229                 !obr_.foundObject<volVectorField>(UName_)
00230              || !obr_.foundObject<volScalarField>(pName_)
00231             )
00232             {
00233                 active_ = false;
00234                 WarningIn("void forces::read(const dictionary& dict)")
00235                 << "Could not find " << UName_ << " or "
00236                     << pName_ << " in database." << nl
00237                     << "    De-activating forces."
00238                     << endl;
00239             }
00240 
00241             // Reference density needed for incompressible calculations
00242             rhoRef_ = readScalar(dict.lookup("rhoInf"));
00243         }
00244 
00245         // Centre of rotation for moment calculations
00246         CofR_ = dict.lookup("CofR");
00247     }
00248 }
00249 
00250 
00251 void Foam::forces::makeFile()
00252 {
00253     // Create the forces file if not already created
00254     if (forcesFilePtr_.empty())
00255     {
00256         if (debug)
00257         {
00258             Info<< "Creating forces file." << endl;
00259         }
00260 
00261         // File update
00262         if (Pstream::master())
00263         {
00264             fileName forcesDir;
00265             if (Pstream::parRun())
00266             {
00267                 // Put in undecomposed case (Note: gives problems for
00268                 // distributed data running)
00269                 forcesDir =
00270                 obr_.time().path()/".."/name_/obr_.time().timeName();
00271             }
00272             else
00273             {
00274                 forcesDir = obr_.time().path()/name_/obr_.time().timeName();
00275             }
00276 
00277             // Create directory if does not exist.
00278             mkDir(forcesDir);
00279 
00280             // Open new file at start up
00281             forcesFilePtr_.reset(new OFstream(forcesDir/(type() + ".dat")));
00282 
00283             // Add headers to output data
00284             writeFileHeader();
00285         }
00286     }
00287 }
00288 
00289 
00290 void Foam::forces::writeFileHeader()
00291 {
00292     if (forcesFilePtr_.valid())
00293     {
00294         forcesFilePtr_()
00295             << "# Time" << tab
00296             << "forces(pressure, viscous) moment(pressure, viscous)"
00297             << endl;
00298     }
00299 }
00300 
00301 
00302 void Foam::forces::execute()
00303 {
00304     // Do nothing - only valid on write
00305 }
00306 
00307 
00308 void Foam::forces::end()
00309 {
00310     // Do nothing - only valid on write
00311 }
00312 
00313 
00314 void Foam::forces::write()
00315 {
00316     if (active_)
00317     {
00318         // Create the forces file if not already created
00319         makeFile();
00320 
00321         forcesMoments fm = calcForcesMoment();
00322 
00323         if (Pstream::master())
00324         {
00325             forcesFilePtr_() << obr_.time().value() << tab << fm << endl;
00326 
00327             if (log_)
00328             {
00329                 Info<< "forces output:" << nl
00330                     << "    forces(pressure, viscous)" << fm.first() << nl
00331                     << "    moment(pressure, viscous)" << fm.second() << nl
00332                     << endl;
00333             }
00334         }
00335     }
00336 }
00337 
00338 
00339 Foam::forces::forcesMoments Foam::forces::calcForcesMoment() const
00340 {
00341     forcesMoments fm
00342     (
00343         pressureViscous(vector::zero, vector::zero),
00344         pressureViscous(vector::zero, vector::zero)
00345     );
00346 
00347     if (directForceDensity_)
00348     {
00349         const volVectorField& fD = obr_.lookupObject<volVectorField>(fDName_);
00350 
00351         const fvMesh& mesh = fD.mesh();
00352 
00353         const surfaceVectorField::GeometricBoundaryField& Sfb =
00354             mesh.Sf().boundaryField();
00355 
00356         forAllConstIter(labelHashSet, patchSet_, iter)
00357         {
00358             label patchi = iter.key();
00359 
00360             vectorField Md = mesh.C().boundaryField()[patchi] - CofR_;
00361 
00362             scalarField sA = mag(Sfb[patchi]);
00363 
00364             // Normal force = surfaceUnitNormal * (surfaceNormal & forceDensity)
00365             vectorField fN =
00366                 Sfb[patchi]/sA
00367                *(
00368                     Sfb[patchi] & fD.boundaryField()[patchi]
00369                 );
00370 
00371             fm.first().first() += sum(fN);
00372             fm.second().first() += sum(Md ^ fN);
00373 
00374             // Tangential force (total force minus normal fN)
00375             vectorField fT = sA*fD.boundaryField()[patchi] - fN;
00376 
00377             fm.first().second() += sum(fT);
00378             fm.second().second() += sum(Md ^ fT);
00379         }
00380     }
00381     else
00382     {
00383         const volVectorField& U = obr_.lookupObject<volVectorField>(UName_);
00384         const volScalarField& p = obr_.lookupObject<volScalarField>(pName_);
00385 
00386         const fvMesh& mesh = U.mesh();
00387 
00388         const surfaceVectorField::GeometricBoundaryField& Sfb =
00389             mesh.Sf().boundaryField();
00390 
00391         tmp<volSymmTensorField> tdevRhoReff = devRhoReff();
00392         const volSymmTensorField::GeometricBoundaryField& devRhoReffb
00393             = tdevRhoReff().boundaryField();
00394 
00395         forAllConstIter(labelHashSet, patchSet_, iter)
00396         {
00397             label patchi = iter.key();
00398 
00399             vectorField Md = mesh.C().boundaryField()[patchi] - CofR_;
00400 
00401             vectorField pf = Sfb[patchi]*p.boundaryField()[patchi];
00402 
00403             fm.first().first() += rho(p)*sum(pf);
00404             fm.second().first() += rho(p)*sum(Md ^ pf);
00405 
00406             vectorField vf = Sfb[patchi] & devRhoReffb[patchi];
00407 
00408             fm.first().second() += sum(vf);
00409             fm.second().second() += sum(Md ^ vf);
00410         }
00411     }
00412 
00413     reduce(fm, sumOp());
00414 
00415     return fm;
00416 }
00417 
00418 
00419 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd