Variable.hpp

Go to the documentation of this file.
00001 //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
00002 //
00003 //        This file is part of E-Cell Simulation Environment package
00004 //
00005 //                Copyright (C) 1996-2002 Keio University
00006 //
00007 //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
00008 //
00009 //
00010 // E-Cell is free software; you can redistribute it and/or
00011 // modify it under the terms of the GNU General Public
00012 // License as published by the Free Software Foundation; either
00013 // version 2 of the License, or (at your option) any later version.
00014 // 
00015 // E-Cell is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00018 // See the GNU General Public License for more details.
00019 // 
00020 // You should have received a copy of the GNU General Public
00021 // License along with E-Cell -- see the file COPYING.
00022 // If not, write to the Free Software Foundation, Inc.,
00023 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00024 // 
00025 //END_HEADER
00026 //
00027 // written by Koichi Takahashi <shafi@e-cell.org>,
00028 // E-Cell Project.
00029 //
00030 
00031 #ifndef __VARIABLE_HPP
00032 #define __VARIABLE_HPP
00033 
00034 #include <utility>
00035 
00036 #include "libecs.hpp"
00037 #include "Entity.hpp"
00038 #include "Interpolant.hpp"
00039 #include "System.hpp"
00040 
00041 
00042 namespace libecs
00043 {
00044 
00045   /** @addtogroup entities
00046    *@{
00047    */
00048 
00049   /** @file */
00050 
00051 
00052   /**
00053      Variable class represents state variables in the simulation model, such as
00054      amounts of molecular species in a compartment.
00055 
00056   */
00057 
00058   LIBECS_DM_CLASS( Variable, Entity )
00059   {
00060 
00061   public:
00062 
00063     LIBECS_DM_BASECLASS( Variable );
00064 
00065     LIBECS_DM_OBJECT( Variable, Variable )
00066       {
00067         INHERIT_PROPERTIES( Entity );
00068         
00069         PROPERTYSLOT_LOAD_SAVE( Real, Value,
00070                                 &Variable::setValue,
00071                                 &Variable::getValue,
00072                                 &Variable::loadValue,
00073                                 &Variable::saveValue );
00074 
00075 
00076         PROPERTYSLOT_SET_GET( Real,  DiffusionCoeff );
00077 
00078         PROPERTYSLOT_SET_GET( Integer,  Fixed );
00079 
00080         PROPERTYSLOT_NO_LOAD_SAVE( Real, Velocity,
00081                                    NOMETHOD,
00082                                    &Variable::getVelocity );
00083 
00084         PROPERTYSLOT_LOAD_SAVE( Real, MolarConc,
00085                                 &Variable::setMolarConc,
00086                                 &Variable::getMolarConc,
00087                                 &Variable::loadMolarConc,
00088                                 NOMETHOD );
00089         //      PROPERTYSLOT_NO_LOAD_SAVE( Real, MolarConc,
00090         //                                 &Variable::setMolarConc,
00091         //                                 &Variable::getMolarConc );
00092 
00093         PROPERTYSLOT_NO_LOAD_SAVE( Real, NumberConc,
00094                                    &Variable::setNumberConc,
00095                                    &Variable::getNumberConc );
00096       }
00097 
00098 
00099     Variable();
00100     virtual ~Variable();
00101 
00102     virtual const EntityType getEntityType() const
00103     {
00104       return EntityType( EntityType::VARIABLE );
00105     }
00106 
00107 
00108     /**
00109        Initializes this variable. 
00110     */
00111 
00112     virtual void initialize();
00113 
00114 
00115     /**
00116        Clear theVelocity by zero.
00117     */
00118 
00119     virtual const bool isIntegrationNeeded() const
00120     {
00121       return ! theInterpolantVector.empty();
00122     }
00123 
00124     /** 
00125         Integrate.
00126     */
00127 
00128     virtual void integrate( RealParam aTime )
00129     {
00130       if( isFixed() == false ) 
00131         {
00132           updateValue( aTime );
00133         }
00134       else 
00135         {
00136           theLastTime = aTime;
00137         }
00138     }
00139 
00140 
00141     /**
00142 
00143     This method is used internally by DifferentialStepper.
00144 
00145     @internal
00146     */
00147 
00148     void interIntegrate( RealParam aCurrentTime )
00149       {
00150         const Real anInterval( aCurrentTime - theLastTime );
00151         
00152         if ( anInterval > 0.0 )
00153           {
00154             Real aVelocitySum( calculateDifferenceSum( aCurrentTime,
00155                                                        anInterval ) );
00156             loadValue( getValue() + aVelocitySum );
00157           }
00158       }
00159 
00160 
00161     /**
00162        This simply sets the value of this Variable if getFixed() is false.
00163 
00164        @see getFixed()
00165     */
00166 
00167     virtual SET_METHOD( Real, Value )
00168     { 
00169       if ( !isFixed() ) 
00170         {
00171           loadValue( value ); 
00172         }
00173     }
00174 
00175 
00176     // Currently this is non-virtual, but will be changed to a 
00177     // virtual function, perhaps in version 3.3.
00178     // virtual
00179     GET_METHOD( Real, Value )
00180     { 
00181       return saveValue();
00182     }
00183 
00184     void addValue( RealParam aValue )
00185     {
00186       setValue( getValue() + aValue );
00187     }
00188 
00189     void loadValue( RealParam aValue )
00190     {
00191       theValue = aValue;
00192     }
00193 
00194     const Real saveValue() const
00195     {
00196       return theValue;
00197     }
00198 
00199     /**
00200        @return current velocity value in (number of molecules)/(step)
00201     */
00202 
00203     GET_METHOD( Real, Velocity )
00204     {
00205       Real aVelocitySum( 0.0 );
00206       FOR_ALL( InterpolantVector, theInterpolantVector )
00207         {
00208           InterpolantPtr const anInterpolantPtr( *i );
00209           aVelocitySum += anInterpolantPtr->getVelocity( theLastTime );
00210         }
00211 
00212       return aVelocitySum;
00213     }
00214 
00215     /**
00216 
00217     A wrapper to set Fixed property by a bool value.
00218 
00219     */
00220 
00221     void setFixed( const bool aValue )
00222     {
00223       theFixed = aValue;
00224     }
00225 
00226     /**
00227        @return true if the Variable is fixed or false if not.
00228     */
00229 
00230     const bool isFixed() const
00231     {
00232       return theFixed;
00233     }
00234 
00235 
00236     // wrappers to expose is/setFixed as PropertySlots 
00237     SET_METHOD( Integer, Fixed )
00238     { 
00239       theFixed = static_cast<bool>( value );
00240     }
00241 
00242     GET_METHOD( Integer, Fixed )
00243     { 
00244       return theFixed;
00245     }
00246 
00247     SET_METHOD( Real, DiffusionCoeff )
00248     { 
00249       theDiffusionCoeff = value;
00250     }
00251 
00252     GET_METHOD( Real, DiffusionCoeff )
00253     { 
00254       return theDiffusionCoeff;
00255     }
00256 
00257     /**
00258        Returns the molar concentration of this Variable.
00259 
00260        @return Concentration in M [mol/L].
00261     */
00262 
00263     GET_METHOD( Real, MolarConc )
00264     {
00265       // N_A_R = 1.0 / N_A
00266       return getNumberConc() * N_A_R;
00267     }
00268 
00269     /**
00270        Set the molar concentration of this Variable.
00271 
00272        @param value Concentration in M [mol/L].
00273     */
00274 
00275     SET_METHOD( Real, MolarConc )
00276     {
00277       setNumberConc( value * N_A );
00278     }
00279 
00280     /**
00281        Load the molar concentration of this Variable.
00282 
00283        This method uses loadNumberConc() instead of setNumberConc().
00284 
00285        @see setNumberConc()
00286     */
00287 
00288     LOAD_METHOD( Real, MolarConc )
00289     {
00290       loadNumberConc( value * N_A );
00291     }
00292 
00293     /**
00294        Returns the number concentration of this Variable.
00295 
00296        Unlike getMolarConc, this method just returns value / size.
00297 
00298        @return Concentration in [number/L].
00299     */
00300 
00301     GET_METHOD( Real, NumberConc )
00302     {
00303       return getValue() / getSizeOfSuperSystem();
00304 
00305       // This uses getSizeOfSuperSystem() private method instead of
00306       // getSuperSystem()->getSize() because otherwise it is 
00307       // impossible to inline this.
00308     }
00309 
00310     /**
00311        Set the number concentration of this Variable.
00312 
00313        @param value concentration in [number/L].
00314     */
00315 
00316     SET_METHOD( Real, NumberConc )
00317     {
00318       setValue( value * getSizeOfSuperSystem() );
00319 
00320       // This uses getSizeOfSuperSystem() private method instead of
00321       // getSuperSystem()->getSize() because otherwise it is 
00322       // impossible to inline this.
00323     }
00324 
00325 
00326     /**
00327        Load the number concentration of this Variable.
00328 
00329        This method can be called before the SIZE Variable of 
00330        the supersystem of this Variable is configured in
00331        Model::initialize().
00332 
00333        Thus this method gets the value of the SIZE Variable
00334        without relying on the System::getSizeVariable() method
00335        of the supersystem.
00336 
00337        @see loadMolarConc()
00338        @see System::getSizeVariable()
00339        @see System::configureSizeVariable()
00340        @see System::findSizeVariable()
00341     */
00342 
00343     LOAD_METHOD( Real, NumberConc );
00344 
00345     void registerInterpolant( InterpolantPtr const anInterpolant );
00346     //    void removeInterpolant( InterpolantPtr const anInterpolant );
00347 
00348 
00349   protected:
00350 
00351     const Real calculateDifferenceSum( RealParam aCurrentTime, 
00352                                        RealParam anInterval ) const
00353     {
00354       Real aVelocitySum( 0.0 );
00355       FOR_ALL( InterpolantVector, theInterpolantVector )
00356         {
00357           InterpolantPtr const anInterpolantPtr( *i );
00358           aVelocitySum += anInterpolantPtr->getDifference( aCurrentTime,
00359                                                            anInterval );
00360         }
00361 
00362       return aVelocitySum;
00363     }
00364 
00365 
00366     void updateValue( RealParam aCurrentTime )
00367     {
00368       const Real anInterval( aCurrentTime - theLastTime );
00369 
00370       if( anInterval == 0.0 )
00371         {
00372           return;
00373         }
00374 
00375       const Real aVelocitySum( calculateDifferenceSum( aCurrentTime, 
00376                                                        anInterval ) );
00377       loadValue( getValue() + aVelocitySum );
00378 
00379       theLastTime = aCurrentTime;
00380     }
00381 
00382 
00383 
00384     void clearInterpolantVector();
00385 
00386   private:
00387 
00388     const Real getSizeOfSuperSystem() const
00389       {
00390         return getSuperSystem()->getSizeVariable()->getValue();
00391       }
00392 
00393   protected:
00394 
00395     Real theValue;
00396 
00397     Real theLastTime;
00398 
00399     Real theDiffusionCoeff;
00400 
00401     InterpolantVector theInterpolantVector;
00402 
00403     bool theFixed;
00404   };
00405 
00406 
00407   /*@}*/
00408 
00409 } // namespace libecs
00410 
00411 
00412 #endif /* __VARIABLE_HPP */
00413 
00414 /*
00415   Do not modify
00416   $Author: satyanandavel $
00417   $Revision: 2596 $
00418   $Date: 2006-07-08 11:18:19 +0200 (Sat, 08 Jul 2006) $
00419   $Locker$
00420 */
00421 
00422 
00423 

Generated on Fri Aug 31 18:32:07 2007 for E-CELL C++ libraries (libecs and libemc) 3.1.105 by  doxygen 1.5.3