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( Integer,  Fixed );
00077 
00078         PROPERTYSLOT_NO_LOAD_SAVE( Real, Velocity,
00079                                    NOMETHOD,
00080                                    &Variable::getVelocity );
00081 
00082         PROPERTYSLOT_LOAD_SAVE( Real, MolarConc,
00083                                 &Variable::setMolarConc,
00084                                 &Variable::getMolarConc,
00085                                 &Variable::loadMolarConc,
00086                                 NOMETHOD );
00087         //      PROPERTYSLOT_NO_LOAD_SAVE( Real, MolarConc,
00088         //                                 &Variable::setMolarConc,
00089         //                                 &Variable::getMolarConc );
00090 
00091         PROPERTYSLOT_NO_LOAD_SAVE( Real, NumberConc,
00092                                    &Variable::setNumberConc,
00093                                    &Variable::getNumberConc );
00094       }
00095 
00096 
00097     Variable();
00098     virtual ~Variable();
00099 
00100     virtual const EntityType getEntityType() const
00101     {
00102       return EntityType( EntityType::VARIABLE );
00103     }
00104 
00105 
00106     /**
00107        Initializes this variable. 
00108     */
00109 
00110     virtual void initialize();
00111 
00112 
00113     /**
00114        Clear theVelocity by zero.
00115     */
00116 
00117     virtual const bool isIntegrationNeeded() const
00118     {
00119       return ! theInterpolantVector.empty();
00120     }
00121 
00122     /** 
00123         Integrate.
00124     */
00125 
00126     virtual void integrate( RealParam aTime )
00127     {
00128       if( isFixed() == false ) 
00129         {
00130           updateValue( aTime );
00131         }
00132       else 
00133         {
00134           theLastTime = aTime;
00135         }
00136     }
00137 
00138 
00139     /**
00140 
00141     This method is used internally by DifferentialStepper.
00142 
00143     @internal
00144     */
00145 
00146     void interIntegrate( RealParam aCurrentTime )
00147       {
00148         const Real anInterval( aCurrentTime - theLastTime );
00149         
00150         if ( anInterval > 0.0 )
00151           {
00152             Real aVelocitySum( calculateDifferenceSum( aCurrentTime,
00153                                                        anInterval ) );
00154             loadValue( getValue() + aVelocitySum );
00155           }
00156       }
00157 
00158 
00159     /**
00160        This simply sets the value of this Variable if getFixed() is false.
00161 
00162        @see getFixed()
00163     */
00164 
00165     virtual SET_METHOD( Real, Value )
00166     { 
00167       if ( !isFixed() ) 
00168         {
00169           loadValue( value ); 
00170         }
00171     }
00172 
00173 
00174     // Currently this is non-virtual, but will be changed to a 
00175     // virtual function, perhaps in version 3.3.
00176     // virtual
00177     GET_METHOD( Real, Value )
00178     { 
00179       return saveValue();
00180     }
00181 
00182     void addValue( RealParam aValue )
00183     {
00184       setValue( getValue() + aValue );
00185     }
00186 
00187     void loadValue( RealParam aValue )
00188     {
00189       theValue = aValue;
00190     }
00191 
00192     const Real saveValue() const
00193     {
00194       return theValue;
00195     }
00196 
00197     /**
00198        @return current velocity value in (number of molecules)/(step)
00199     */
00200 
00201     GET_METHOD( Real, Velocity )
00202     {
00203       Real aVelocitySum( 0.0 );
00204       FOR_ALL( InterpolantVector, theInterpolantVector )
00205         {
00206           InterpolantPtr const anInterpolantPtr( *i );
00207           aVelocitySum += anInterpolantPtr->getVelocity( theLastTime );
00208         }
00209 
00210       return aVelocitySum;
00211     }
00212 
00213     /**
00214 
00215     A wrapper to set Fixed property by a bool value.
00216 
00217     */
00218 
00219     void setFixed( const bool aValue )
00220     {
00221       theFixed = aValue;
00222     }
00223 
00224     /**
00225        @return true if the Variable is fixed or false if not.
00226     */
00227 
00228     const bool isFixed() const
00229     {
00230       return theFixed;
00231     }
00232 
00233 
00234     // wrappers to expose is/setFixed as PropertySlots 
00235     SET_METHOD( Integer, Fixed )
00236     { 
00237       theFixed = static_cast<bool>( value );
00238     }
00239 
00240     GET_METHOD( Integer, Fixed )
00241     { 
00242       return theFixed;
00243     }
00244 
00245 
00246     /**
00247        Returns the molar concentration of this Variable.
00248 
00249        @return Concentration in M [mol/L].
00250     */
00251 
00252     GET_METHOD( Real, MolarConc )
00253     {
00254       // N_A_R = 1.0 / N_A
00255       return getNumberConc() * N_A_R;
00256     }
00257 
00258     /**
00259        Set the molar concentration of this Variable.
00260 
00261        @param value Concentration in M [mol/L].
00262     */
00263 
00264     SET_METHOD( Real, MolarConc )
00265     {
00266       setNumberConc( value * N_A );
00267     }
00268 
00269     /**
00270        Load the molar concentration of this Variable.
00271 
00272        This method uses loadNumberConc() instead of setNumberConc().
00273 
00274        @see setNumberConc()
00275     */
00276 
00277     LOAD_METHOD( Real, MolarConc )
00278     {
00279       loadNumberConc( value * N_A );
00280     }
00281 
00282     /**
00283        Returns the number concentration of this Variable.
00284 
00285        Unlike getMolarConc, this method just returns value / size.
00286 
00287        @return Concentration in [number/L].
00288     */
00289 
00290     GET_METHOD( Real, NumberConc )
00291     {
00292       return getValue() / getSizeOfSuperSystem();
00293 
00294       // This uses getSizeOfSuperSystem() private method instead of
00295       // getSuperSystem()->getSize() because otherwise it is 
00296       // impossible to inline this.
00297     }
00298 
00299     /**
00300        Set the number concentration of this Variable.
00301 
00302        @param value concentration in [number/L].
00303     */
00304 
00305     SET_METHOD( Real, NumberConc )
00306     {
00307       setValue( value * getSizeOfSuperSystem() );
00308 
00309       // This uses getSizeOfSuperSystem() private method instead of
00310       // getSuperSystem()->getSize() because otherwise it is 
00311       // impossible to inline this.
00312     }
00313 
00314 
00315     /**
00316        Load the number concentration of this Variable.
00317 
00318        This method can be called before the SIZE Variable of 
00319        the supersystem of this Variable is configured in
00320        Model::initialize().
00321 
00322        Thus this method gets the value of the SIZE Variable
00323        without relying on the System::getSizeVariable() method
00324        of the supersystem.
00325 
00326        @see loadMolarConc()
00327        @see System::getSizeVariable()
00328        @see System::configureSizeVariable()
00329        @see System::findSizeVariable()
00330     */
00331 
00332     LOAD_METHOD( Real, NumberConc );
00333 
00334     void registerInterpolant( InterpolantPtr const anInterpolant );
00335     //    void removeInterpolant( InterpolantPtr const anInterpolant );
00336 
00337 
00338   protected:
00339 
00340     const Real calculateDifferenceSum( RealParam aCurrentTime, 
00341                                        RealParam anInterval ) const
00342     {
00343       Real aVelocitySum( 0.0 );
00344       FOR_ALL( InterpolantVector, theInterpolantVector )
00345         {
00346           InterpolantPtr const anInterpolantPtr( *i );
00347           aVelocitySum += anInterpolantPtr->getDifference( aCurrentTime,
00348                                                            anInterval );
00349         }
00350 
00351       return aVelocitySum;
00352     }
00353 
00354 
00355     void updateValue( RealParam aCurrentTime )
00356     {
00357       const Real anInterval( aCurrentTime - theLastTime );
00358 
00359       if( anInterval == 0.0 )
00360         {
00361           return;
00362         }
00363 
00364       const Real aVelocitySum( calculateDifferenceSum( aCurrentTime, 
00365                                                        anInterval ) );
00366       loadValue( getValue() + aVelocitySum );
00367 
00368       theLastTime = aCurrentTime;
00369     }
00370 
00371 
00372 
00373     void clearInterpolantVector();
00374 
00375   private:
00376 
00377     const Real getSizeOfSuperSystem() const
00378       {
00379         return getSuperSystem()->getSizeVariable()->getValue();
00380       }
00381 
00382   protected:
00383 
00384     Real theValue;
00385 
00386     Real theLastTime;
00387 
00388     InterpolantVector theInterpolantVector;
00389 
00390     bool theFixed;
00391   };
00392 
00393 
00394   /*@}*/
00395 
00396 } // namespace libecs
00397 
00398 
00399 #endif /* __VARIABLE_HPP */
00400 
00401 /*
00402   Do not modify
00403   $Author: shafi $
00404   $Revision: 2529 $
00405   $Date: 2005-11-19 10:36:40 +0100 (Sat, 19 Nov 2005) $
00406   $Locker$
00407 */
00408 
00409 
00410 

Generated on Fri Apr 28 10:38:30 2006 for E-CELL C++ libraries (libecs and libemc) 3.1.105 by  doxygen 1.4.6