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

pairPotential.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) 2008-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 "pairPotential.H"
00028 #include "energyScalingFunction.H"
00029 
00030 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00031 
00032 namespace Foam
00033 {
00034     defineTypeNameAndDebug(pairPotential, 0);
00035     defineRunTimeSelectionTable(pairPotential, dictionary);
00036 }
00037 
00038 
00039 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00040 
00041 
00042 void Foam::pairPotential::scaleEnergy(scalar& e, const scalar r) const
00043 {
00044     if (!esfPtr_)
00045     {
00046         esfPtr_ = energyScalingFunction::New
00047         (
00048             name_, pairPotentialProperties_, *this
00049         ).ptr();
00050     }
00051 
00052     esfPtr_->scaleEnergy(e, r);
00053 }
00054 
00055 
00056 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00057 
00058 Foam::pairPotential::pairPotential
00059 (
00060     const word& name,
00061     const dictionary& pairPotentialProperties
00062 )
00063 :
00064     name_(name),
00065     pairPotentialProperties_(pairPotentialProperties),
00066     rCut_(readScalar(pairPotentialProperties_.lookup("rCut"))),
00067     rCutSqr_(rCut_*rCut_),
00068     rMin_(readScalar(pairPotentialProperties_.lookup("rMin"))),
00069     dr_(readScalar(pairPotentialProperties_.lookup("dr"))),
00070     forceLookup_(0),
00071     energyLookup_(0),
00072     esfPtr_(NULL),
00073     writeTables_(Switch(pairPotentialProperties_.lookup("writeTables")))
00074 {}
00075 
00076 
00077 // * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * * //
00078 
00079 void Foam::pairPotential::setLookupTables()
00080 {
00081     label N = label((rCut_ - rMin_)/dr_) + 1;
00082 
00083     forceLookup_.setSize(N);
00084 
00085     energyLookup_.setSize(N);
00086 
00087     forAll(forceLookup_, k)
00088     {
00089         energyLookup_[k] = scaledEnergy(k*dr_ + rMin_);
00090 
00091         forceLookup_[k] = -energyDerivative((k*dr_ + rMin_), true);
00092     }
00093 }
00094 
00095 
00096 Foam::scalar Foam::pairPotential::force(const scalar r) const
00097 {
00098     scalar k_rIJ = (r - rMin_)/dr_;
00099 
00100     label k = label(k_rIJ);
00101 
00102     if (k < 0)
00103     {
00104         FatalErrorIn("pairPotential.C") << nl
00105             << "r less than rMin in pair potential " << name_ << nl
00106             << abort(FatalError);
00107     }
00108 
00109     scalar f =
00110         (k_rIJ - k)*forceLookup_[k+1]
00111       + (k + 1 - k_rIJ)*forceLookup_[k];
00112 
00113     return f;
00114 }
00115 
00116 
00117 Foam::List< Foam::Pair< Foam::scalar > >
00118 Foam::pairPotential::forceTable() const
00119 {
00120     List<Pair<scalar> > forceTab(forceLookup_.size());
00121 
00122     forAll(forceLookup_,k)
00123     {
00124         forceTab[k].first() = rMin_ + k*dr_;
00125 
00126         forceTab[k].second() = forceLookup_[k];
00127     }
00128 
00129     return forceTab;
00130 }
00131 
00132 
00133 Foam::scalar Foam::pairPotential::energy(const scalar r) const
00134 {
00135     scalar k_rIJ = (r - rMin_)/dr_;
00136 
00137     label k = label(k_rIJ);
00138 
00139     if (k < 0)
00140     {
00141         FatalErrorIn("pairPotential.C") << nl
00142             << "r less than rMin in pair potential " << name_ << nl
00143             << abort(FatalError);
00144     }
00145 
00146     scalar e =
00147         (k_rIJ - k)*energyLookup_[k+1]
00148       + (k + 1 - k_rIJ)*energyLookup_[k];
00149 
00150     return e;
00151 }
00152 
00153 
00154 Foam::List< Foam::Pair< Foam::scalar > >
00155     Foam::pairPotential::energyTable() const
00156 {
00157     List<Pair<scalar> > energyTab(energyLookup_.size());
00158 
00159     forAll(energyLookup_,k)
00160     {
00161         energyTab[k].first() = rMin_ + k*dr_;
00162 
00163         energyTab[k].second() = energyLookup_[k];
00164     }
00165 
00166     return energyTab;
00167 }
00168 
00169 
00170 Foam::scalar Foam::pairPotential::scaledEnergy(const scalar r) const
00171 {
00172     scalar e = unscaledEnergy(r);
00173 
00174     scaleEnergy(e, r);
00175 
00176     return e;
00177 }
00178 
00179 
00180 Foam::scalar Foam::pairPotential::energyDerivative
00181 (
00182     const scalar r,
00183     const bool scaledEnergyDerivative
00184 ) const
00185 {
00186     // Local quadratic fit to energy: E = a0 + a1*r + a2*r^2
00187     // Differentiate to give f = -dE/dr = -a1 - 2*a2*r
00188 
00189     scalar ra = r - dr_;
00190     scalar rf = r;
00191     scalar rb = r + dr_;
00192 
00193     scalar Ea, Ef, Eb;
00194 
00195     if (scaledEnergyDerivative)
00196     {
00197         Ea = scaledEnergy(ra);
00198         Ef = scaledEnergy(rf);
00199         Eb = scaledEnergy(rb);
00200     }
00201     else
00202     {
00203         Ea = unscaledEnergy(ra);
00204         Ef = unscaledEnergy(rf);
00205         Eb = unscaledEnergy(rb);
00206     }
00207 
00208     scalar denominator = (ra - rf)*(ra - rb)*(rf - rb);
00209 
00210     scalar a1 =
00211     (
00212         rb*rb*(Ea - Ef) + ra*ra*(Ef - Eb) + rf*rf*(Eb - Ea)
00213     ) / denominator;
00214 
00215     scalar a2 =
00216     (
00217         rb*(Ef - Ea) + rf*(Ea - Eb) + ra*(Eb - Ef)
00218     ) / denominator;
00219 
00220     return a1 + 2.0*a2*r;
00221 }
00222 
00223 
00224 bool Foam::pairPotential::read(const dictionary& pairPotentialProperties)
00225 {
00226     pairPotentialProperties_ = pairPotentialProperties;
00227 
00228     return true;
00229 }
00230 
00231 
00232 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd