00001 /********************************************************************** 00002 ** This program is part of 'MOOSE', the 00003 ** Messaging Object Oriented Simulation Environment. 00004 ** Copyright (C) 2003-2011 Upinder S. Bhalla. and NCBS 00005 ** It is made available under the terms of the 00006 ** GNU Lesser General Public License version 2.1 00007 ** See the file COPYING.LIB for the full notice. 00008 **********************************************************************/ 00009 00010 #ifndef _MARKOVRATETABLE_H 00011 #define _MARKOVRATETABLE_H 00012 00014 //Class : MarkovRateTable 00015 //Author : Vishaka Datta S, 2011, NCBS 00016 // 00017 //Presents a unified interface to deal with transition rates that are dependent 00018 //on one or two parameters, or are constant. 00019 //Ther Interpol2D class is used for carrying out 2-D rate lookups. The one 00020 //parameter lookup functions are based on the lookup tables used in the HHGate 00021 //class. 00022 // 00023 //The class consists of a table of pointers to the VectorTable class and the 00024 //Interpol2D class. Since these classes are user-defined and possess 00025 //constructors, destructors, etc., using a union is not possible (but would've 00026 //been more efficient). 00027 // 00028 //For a given rate entry (i,j) either one of these pointers may be set, but not 00029 //both. If both pointers are null, the rate is assumed to be zero i.e. there 00030 //is no transition from state i to state j. 00031 // 00032 //In case a rate is constant, it is stored in a VectorTable class whose lookup 00033 //table is of size 1. 00034 // 00036 00037 template <class T> 00038 vector< vector< T > > resize( vector< vector< T > >table, unsigned int n, T init ) 00039 { 00040 table.resize( n ); 00041 00042 for ( unsigned int i = 0; i < n; ++i ) 00043 table[i].resize( n, init ); 00044 00045 return table; 00046 } 00047 00048 class MarkovRateTable { 00049 public : 00050 MarkovRateTable(); 00051 00052 ~MarkovRateTable(); 00053 00054 //One parameter rate table set and get functions. 00055 VectorTable* getVtChildTable( unsigned int, unsigned int ) const; 00056 void innerSetVtChildTable( unsigned int, unsigned int, VectorTable, unsigned int ); 00057 //Two parameter rate table set and get functions. 00058 Interpol2D* getInt2dChildTable( unsigned int, unsigned int ) const; 00059 void innerSetInt2dChildTable( unsigned int, unsigned int, Interpol2D ); 00060 00061 //Lookup functions. 00062 double lookup1dValue( unsigned int, unsigned int, double ); 00063 double lookup1dIndex( unsigned int, unsigned int, unsigned int ); 00064 double lookup2dValue( unsigned int, unsigned int, double, double ); 00065 double lookup2dIndex( unsigned int, unsigned int, unsigned int, unsigned int ); 00066 00067 //Rate update and initialization functions. 00068 void updateRates(); 00069 void initConstantRates(); 00070 00071 vector< unsigned int > getListOf1dRates(); 00072 vector< unsigned int > getListOf2dRates(); 00073 vector< unsigned int > getListOfLigandRates(); 00074 vector< unsigned int > getListOfVoltageRates(); 00075 vector< unsigned int > getListOfConstantRates(); 00076 00077 //Overloading the >> operator. This is done so that VectorTable and 00078 //Interpol2D classes can be used to declare OpFuncs. 00079 friend istream& operator>>( istream&, MarkovRateTable& ); 00080 00082 //*ValueFinfo related functions. 00084 vector< vector< double > > getQ() const; 00085 unsigned int getSize() const; 00086 00087 double getVm( ) const; 00088 void setVm( double ); 00089 00090 double getLigandConc( ) const; 00091 void setLigandConc( double ); 00092 00094 //Helper functions 00096 00097 //Returns true if the (i,j)'th rate is zero i.e. no transitions between states 00098 //i and j. When this is the case, both the (i,j)'th pointers in vtTables_ 00099 // and int2dTables_ are not set i.e. both are zero. 00100 bool isRateZero( unsigned int, unsigned int ) const ; 00101 00102 //Returns true if the (i,j)'th rate is a constant. This is true when the 00103 //vtTables_[i][j] pointer points to a 1D lookup table of size 1. 00104 bool isRateConstant( unsigned int, unsigned int ) const ; 00105 00106 //Returns true if the (i,j)'th rate is varies with only one parameter. 00107 bool isRate1d( unsigned int, unsigned int ) const ; 00108 00109 //Returns true if the (i,j)'th rate is dependent on ligand concentration and 00110 //not membrane voltage, and is set with a 1D lookup table. 00111 bool isRateLigandDep( unsigned int, unsigned int) const; 00112 00113 //Returns true if the (i,j)'th rate varies with two parameters. 00114 bool isRate2d( unsigned int, unsigned int ) const ; 00115 00116 //Returns true if a table is being accessed at an invalid address. 00117 bool areIndicesOutOfBounds( unsigned int, unsigned int ) const ; 00118 00120 //These functions return general information about the whole rate table 00121 //itself. Useful for the MarkovSolver class. 00123 00124 //Returns true if all the rates in the transition matrix are constant. 00125 bool areAllRatesConstant(); 00126 00127 //Returns true if at least one of the rates are 1D i.e. vary with only 00128 //one parameter. 00129 bool areAnyRates1d(); 00130 bool areAllRates1d(); 00131 00132 //Returns true if at least one of the rates are 2D i.e. vary with two 00133 //parameters. 00134 bool areAnyRates2d(); 00135 bool areAllRates2d(); 00136 00137 //Returns true if at least one of the rates are ligand dependent. 00138 bool areAnyRatesLigandDep(); 00139 bool areAllRatesLigandDep(); 00140 00141 //Returns true if at least one of the rates are voltage dependent. 00142 bool areAnyRatesVoltageDep(); 00143 bool areAllRatesVoltageDep(); 00144 00145 static const Cinfo* initCinfo(); 00146 00148 //MsgDest functions. 00150 void process( const Eref&, ProcPtr ); 00151 void reinit( const Eref&, ProcPtr ); 00152 00153 //Initialization of object. 00154 void init( unsigned int ); 00155 00156 //Handling incoming messages containing voltage and concentration information. 00157 void handleVm( double ); 00158 void handleLigandConc( double ); 00159 00160 //Setting rate tables for 1-D and 2-D rate transitions. 00161 void setVtChildTable( unsigned int, unsigned int, Id, unsigned int ); 00162 void setInt2dChildTable( unsigned int, unsigned int, Id ); 00163 00164 //Setting a constant rate. 00165 void setConstantRate( unsigned int, unsigned int, double ); 00166 00167 private : 00168 //Family of 1D lookup table pointers. Each entry is a pointer to a 1-D 00169 //lookup table (VectorTable type) that contains the values of the rates of 00170 //transition at a given voltage or ligand concentration. 00171 vector< vector< VectorTable* > > vtTables_; 00172 00173 //Family of 2D lookup table pointers. Each entry is a pointer to a 2-D 00174 //lookup table of type Interpol2D. These lookup tables deal with rates that 00175 //vary with voltage and ligand concentration. 00176 vector< vector< Interpol2D* > > int2dTables_; 00177 00178 //In the case of 1-D lookup tables, this matrix determines which rates are 00179 //ligand dependent. When the (i,j)'th value is "true", the ligand 00180 //concentration is used instead of voltage for rate lookups. 00181 vector< vector< unsigned int > > useLigandConc_; 00182 00183 //Maintains a list of the indices of 1D,2D,constant rates, voltage and 00184 //ligand dependent rates. Makes updating rates a whole lot easier and 00185 //more elegant than blindly scanning through the entire rate matrix 00186 //each time. 00187 vector< unsigned int > listOf1dRates_; 00188 vector< unsigned int > listOf2dRates_; 00189 vector< unsigned int > listOfConstantRates_; 00190 vector< unsigned int > listOfLigandRates_; 00191 vector< unsigned int > listOfVoltageRates_; 00192 00193 //The instantaneous rate matrix. 00194 vector< vector< double > > Q_; 00195 00196 //Membrane voltage and ligand concentration. Needed for rate lookups. 00197 //Admittedly, this is a bit of a deviation from the usual scheme where 00198 //channels are the only destinations for Vm and conc. 00199 double Vm_; 00200 double ligandConc_; 00201 00202 //Time step of simulation. The rates are specified in sec^(-1). All the rates 00203 //have to be proportionally scaled for an arbitrary time step. 00204 //Is not a MOOSE field, and does not need an explicit setup call either. 00205 //It is set during the call to reinit. 00206 00207 unsigned int size_; 00208 00209 //Check to see if the table has been initialized. 00210 bool isInitialized() const; 00211 }; 00212 #endif