15 #include "../builtins/Interpol2D.h"
23 "Sends updated state to the MarkovChannel class."
34 "Handles incoming message containing voltage information.",
38 static Finfo* channelShared[] =
44 "This message couples the MarkovSolverBase to the Compartment. The "
45 "compartment needs Vm in order to look up the correct matrix "
46 "exponential for computing the state.",
47 channelShared,
sizeof( channelShared ) /
sizeof(
Finfo* )
55 "Handles process call",
59 "Handles reinit call",
62 static Finfo* processShared[] =
68 "This is a shared message to receive Process message from the"
69 "scheduler. The first entry is a MsgDest for the Process "
70 "operation. It has a single argument, ProcInfo, which "
71 "holds lots of information about current time, thread, dt and"
72 "so on. The second entry is a MsgDest for the Reinit "
73 "operation. It also uses ProcInfo.",
74 processShared,
sizeof( processShared ) /
sizeof(
Finfo* )
78 "Handles incoming message containing ligand concentration.",
83 "Setups the table of matrix exponentials associated with the"
93 "Instantaneous rate matrix.",
98 "Current state of the channel.",
103 "Initial state of the channel.",
109 "Minimum value for x axis of lookup table",
114 "Maximum value for x axis of lookup table",
119 "# of divisions on x axis of lookup table",
124 "Reciprocal of increment on x axis of lookup table",
128 "Minimum value for y axis of lookup table",
133 "Maximum value for y axis of lookup table",
138 "# of divisions on y axis of lookup table",
143 "Reciprocal of increment on y axis of lookup table",
147 static Finfo* markovSolverFinfos[] =
167 static string doc[] =
169 "Name",
"MarkovSolverBase",
170 "Author",
"Vishaka Datta S, 2011, NCBS",
171 "Description",
"Solver for Markov Channel."
178 sizeof( markovSolverFinfos ) /
sizeof(
Finfo* ),
181 sizeof(doc) /
sizeof(
string)
190 expMats2d_(0), xMin_(DBL_MAX), xMax_(DBL_MIN), xDivs_(0u),
191 yMin_(DBL_MAX), yMax_(DBL_MIN), yDivs_(0u), size_(0u), Vm_(0),
192 ligandConc_(0), dt_(0)
214 for(
unsigned int i = 0; i < n; ++i )
216 for (
unsigned int j = 0; j <
expMats2d_[i].size(); ++j )
319 bool isEndOfX =
false;
320 bool isEndOfY =
false;
322 unsigned int xIndex =
324 unsigned int yIndex =
329 double xF = xv - xIndex;
330 double yF = yv - yIndex;
331 double xFyF = xF * yF;
333 ( xIndex ==
xDivs_ ) ? isEndOfX =
true : isEndOfX =
false;
334 ( yIndex ==
yDivs_ ) ? isEndOfY =
true : isEndOfY =
false;
336 vector< vector< Matrix* > >::const_iterator iExpQ0 =
338 vector< Matrix* >::const_iterator iExpQ00 = iExpQ0->begin() + yIndex;
339 vector< Matrix* >::const_iterator iExpQ10;
341 Matrix* expQ00 = *iExpQ00;
348 Vector *state00 =
nullptr;
349 Vector *state01 =
nullptr;
350 Vector *state10 =
nullptr;
351 Vector *state11 =
nullptr;
354 Vector *state00 = NULL, *state01 = NULL
355 , *state10 = NULL, *state11 = NULL
367 expQ01 = *(iExpQ00 + 1);
375 iExpQ10 = ( iExpQ0 + 1 )->begin() + yIndex;
385 expQ01 = *( iExpQ00 + 1 );
386 expQ11 = *( iExpQ10 + 1 );
394 ( 1 - xF - yF + xFyF ),
398 temp2 =
vecVecScalAdd( state01, state11, ( yF - xFyF ), xFyF );
430 else if ( x >
xMax_ )
433 unsigned int xIndex =
static_cast< unsigned int >( ( x -
xMin_) *
invDx_ );
435 double xv = ( x -
xMin_ ) * invDx_;
436 double xF = xv - xIndex;
438 vector< Matrix* >::const_iterator iExpQ =
441 Vector *state0, *state1, *result;
466 bool useBilinear =
false;
497 vector< unsigned int > rateIndices,
500 unsigned int yIndex )
502 unsigned int n = rateIndices.size(), i, j;
504 for (
unsigned int k = 0; k < n; ++k )
506 i = ( ( rateIndices[k] / 10 ) % 10 ) - 1;
507 j = ( rateIndices[k] % 10 ) - 1;
509 (*Q_)[i][i] += (*Q_)[i][j];
511 if ( rateType.compare(
"2D") == 0 )
513 else if ( rateType.compare(
"1D") == 0 )
515 else if ( rateType.compare(
"constant") == 0 )
520 (*Q_)[i][i] -= (*Q_)[i][j];
531 vector< unsigned int > listOfConstantRates =
548 for (
unsigned int xIndex = 0; xIndex <
xDivs_ + 1; ++xIndex )
551 for(
unsigned int yIndex = 0; yIndex <
yDivs_ + 1; ++yIndex )
570 vector< unsigned int > listOfLigandRates =
573 for (
unsigned int xIndex = 0; xIndex <
xDivs_ + 1; ++xIndex )
583 vector< unsigned int > listOfVoltageRates =
586 for (
unsigned int xIndex = 0; xIndex <
xDivs_ + 1; ++xIndex )
613 cerr <<
"MarkovSolverBase::reinit : Initial state has not been "
657 for(
unsigned int i = 0; i <
xDivs_ + 1; ++i )
701 vector< unsigned int > listOfLigandRates
703 vector< unsigned int > listOfVoltageRates
707 double yMax = DBL_MIN, yMin = DBL_MAX;
708 unsigned int yDivs = 0u;
709 unsigned int divs, i, j;
711 for(
unsigned int k = 0; k < listOfLigandRates.size(); ++k )
713 i = ( ( listOfLigandRates[k] / 10 ) % 10 ) - 1;
714 j = ( listOfLigandRates[k] % 10 ) - 1;
735 invDx_ = yDivs / ( yMax - yMin );
742 invDy_ = yDivs / ( yMax - yMin );
745 for(
unsigned int k = 0; k < listOfVoltageRates.size(); ++k )
747 i = ( ( listOfVoltageRates[k] / 10 ) % 10 ) - 1;
748 j = ( listOfVoltageRates[k] % 10 ) - 1;
768 unsigned int divs, i, j;
770 for(
unsigned int k = 0; k < listOf2dRates.size(); ++k )
772 i = ( ( listOf2dRates[k] / 10 ) % 10 ) - 1;
773 j = ( listOf2dRates[k] % 10 ) - 1;
806 void MarkovSolverBase::setVm(
double Vm )
811 void MarkovSolverBase::setLigandConc(
double ligandConc )
816 void setupInterpol2D(
Interpol2D* table,
unsigned int xDivs,
double xMin,
817 double xMax,
unsigned int yDivs,
double yMin,
double yMax )
827 void setupVectorTable(
VectorTable* table,
unsigned int xDivs,
double xMin,
841 void testMarkovSolverBase()
852 vector< Id > rateTableIds;
853 vector< Id > solverBaseIds;
855 vector< Element* > rateTableElements;
856 vector< Eref* > rateTableErefs;
858 vector< Element* > solverBaseElements;
859 vector< Eref* > solverBaseErefs;
861 vector< MarkovRateTable* > rateTables;
862 vector< MarkovSolverBase* > solverBases;
864 vector< unsigned int > single;
867 unsigned int numCopies = 4;
869 for (
unsigned int i = 0; i < numCopies; ++i )
872 str = string(
"ratetable") +
static_cast< char >( 65 + i );
873 rateTableElements.push_back(
new Element( rateTableIds[i], rateTableCinfo,
875 rateTableErefs.push_back(
new Eref( rateTableElements[i], 0 ) );
876 rateTables.push_back( reinterpret_cast< MarkovRateTable* >(
877 rateTableErefs[i]->data() ) );
880 str = string(
"solverbase") +
static_cast< char >( 65 + i );
881 solverBaseElements.push_back(
new Element( solverBaseIds[i], solverBaseCinfo,
883 solverBaseErefs.push_back(
new Eref( solverBaseElements[i], 0 ) );
884 solverBases.push_back( reinterpret_cast< MarkovSolverBase* >(
885 solverBaseErefs[i]->data() ) );
888 Element *eInt2d =
new Element( int2dId, interpol2dCinfo,
"int2d", single, 1 );
889 Element *eVecTable =
new Element( vecTableId, vectorTableCinfo,
"vecTable",
895 Eref int2dEref( eInt2d, 0 );
896 int2dTable =
reinterpret_cast< Interpol2D*
>( int2dEref.data() );
898 Eref vecTableEref( eVecTable, 0 );
899 vecTable =
reinterpret_cast< VectorTable*
>( vecTableEref.data() );
912 rateTables[0]->init( 3 );
914 setupInterpol2D( int2dTable, 201, -0.05, 0.10, 151, 1e-9, 50e-9 );
915 rateTables[0]->setInt2dChildTable( 1, 2, int2dId );
917 setupInterpol2D( int2dTable, 175, -0.02, 0.19, 151, 3e-9, 75e-9 );
918 rateTables[0]->setInt2dChildTable( 2, 3, int2dId );
920 solverBases[0]->init( rateTableIds[0], 0.1 );
922 assert( solverBases[0]->getXdivs() == 201 );
923 assert(
doubleEq( solverBases[0]->getXmin(), -0.05 ) );
924 assert(
doubleEq( solverBases[0]->getXmax(), 0.19 ) );
925 assert( solverBases[0]->getYdivs() == 151 );
928 rateTables[1]->init( 5 );
935 setupInterpol2D( int2dTable, 250, -0.10, 0.75, 101, 10e-9, 25e-8 );
936 rateTables[1]->setInt2dChildTable( 1, 2, int2dId );
938 setupInterpol2D( int2dTable, 275, -0.05, 0.55, 141, 5e-9, 40e-7 );
939 rateTables[1]->setInt2dChildTable( 1, 3, int2dId );
942 setupVectorTable( vecTable, 145, -0.17, 0.73 );
943 rateTables[1]->setVtChildTable( 2, 1, vecTableId, 0 );
946 setupVectorTable( vecTable, 375, 7e-9, 75e-7 );
947 rateTables[1]->setVtChildTable( 3, 1, vecTableId, 1 );
949 solverBases[1]->init( rateTableIds[1], 0.1 );
951 assert( solverBases[1]->getXdivs() == 275 );
952 assert( solverBases[1]->getYdivs() == 375 );
953 assert(
doubleEq( solverBases[1]->getXmin(), -0.17 ) );
954 assert(
doubleEq( solverBases[1]->getXmax(), 0.75 ) );
955 assert(
doubleEq( solverBases[1]->getYmin(), 5e-9 ) );
956 assert(
doubleEq( solverBases[1]->getYmax(), 75e-7 ) );
963 rateTables[2]->init( 5 );
965 setupVectorTable( vecTable, 155, 7e-9, 50e-9 );
966 rateTables[2]->setVtChildTable( 1, 2, vecTableId, 1 );
968 setupVectorTable( vecTable, 190, 4e-9, 35e-9 );
969 rateTables[2]->setVtChildTable( 3, 5, vecTableId, 1 );
971 setupVectorTable( vecTable, 120, 7e-9, 90e-9 );
972 rateTables[2]->setVtChildTable( 2, 4, vecTableId, 1 );
974 setupVectorTable( vecTable, 250, 10e-9, 100e-9 );
975 rateTables[2]->setVtChildTable( 4, 1, vecTableId, 1 );
977 solverBases[2]->init( rateTableIds[2], 0.1 );
979 assert(
doubleEq( 1e-308 * solverBases[2]->getYmin(), 1e-308 * DBL_MAX ) );
980 assert(
doubleEq( 1e308 * solverBases[2]->getYmax(), 1e308 * DBL_MIN ) );
981 assert( solverBases[2]->getYdivs() == 0u );
983 assert(
doubleEq( solverBases[2]->getXmin(), 4e-9 ) );
984 assert(
doubleEq( solverBases[2]->getXmax(), 100e-9 ) );
985 assert( solverBases[2]->getXdivs() == 250 );
992 rateTables[3]->init( 7 );
994 setupVectorTable( vecTable, 100, -0.05, 0.1 );
995 rateTables[3]->setVtChildTable( 3, 6, vecTableId, 1 );
997 setupVectorTable( vecTable, 190, -0.15, 0.2 );
998 rateTables[3]->setVtChildTable( 5, 6, vecTableId, 1 );
1000 setupVectorTable( vecTable, 140, -0.2, 0.1 );
1001 rateTables[3]->setVtChildTable( 1, 4, vecTableId, 1 );
1003 solverBases[3]->init( rateTableIds[3], 0.1 );
1005 assert(
doubleEq( 1e-308 * solverBases[3]->getYmin(), 1e-308 * DBL_MAX ) );
1006 assert(
doubleEq( 1e308 * solverBases[3]->getYmax(), 1e308 * DBL_MIN ) );
1007 assert( solverBases[3]->getYdivs() == 0u );
1009 assert(
doubleEq( solverBases[3]->getXmin(), -0.2 ) );
1010 assert(
doubleEq( solverBases[3]->getXmax(), 0.2 ) );
1011 assert( solverBases[3]->getXdivs() == 190 );
1013 for (
unsigned int i = 0; i < numCopies; ++i )
1015 rateTableIds[i].destroy();
1016 solverBaseIds[i].destroy();
1017 delete rateTableErefs[i];
1018 delete solverBaseErefs[i];
1024 cout <<
"." << flush;
vector< unsigned int > getListOf2dRates()
Interpol2D * getInt2dChildTable(unsigned int, unsigned int) const
void setXmin(double value)
void setYdivs(unsigned int value)
bool areAllRatesVoltageDep()
bool areAllRatesLigandDep()
void setInitialState(Vector)
bool areAnyRatesLigandDep()
VectorTable * getVtChildTable(unsigned int, unsigned int) const
virtual Matrix * computeMatrixExponential()
void setYdivs(unsigned int)
vector< unsigned int > getListOfConstantRates()
void setXdivs(unsigned int value)
MarkovRateTable * rateTable_
unsigned int getYdivs() const
static const Cinfo * vectorTableCinfo
Vector * vecVecScalAdd(const Vector *v1, const Vector *v2, double alpha, double beta)
unsigned int getXdivs() const
static const Cinfo * initCinfo()
Vector * vecMatMul(const Vector *v, Matrix *A)
bool doubleEq(double x, double y)
void setXdivs(unsigned int)
vector< unsigned int > getListOf1dRates()
vector< Matrix * > expMats1d_
void innerFillupTable(vector< unsigned int >, string, unsigned int, unsigned int)
Vector getInitialState() const
unsigned int getXdivs() const
bool areAnyRatesVoltageDep()
static const Cinfo * initCinfo()
vector< unsigned int > getListOfVoltageRates()
unsigned int getDiv() const
Vector * linearInterpolate() const
unsigned int getYdivs() const
void setYmax(double value)
static const Cinfo * initCinfo()
vector< vector< Matrix * > > expMats2d_
vector< vector< T > > resize(vector< vector< T > >table, unsigned int n, T init)
bool areAllRatesConstant()
vector< vector< double > > Matrix
Vector * bilinearInterpolate() const
unsigned int getSize() const
void handleLigandConc(double)
void reinit(const Eref &, ProcPtr)
static const Cinfo * initCinfo()
static const Cinfo * initCinfo()
void setYmin(double value)
vector< unsigned int > getListOfLigandRates()
double lookup2dIndex(unsigned int, unsigned int, unsigned int, unsigned int)
void setDiv(unsigned int)
SrcFinfo1< Vector > * stateOut()
Matrix * matAlloc(unsigned int n)
void setXmax(double value)
static const Cinfo * markovSolverBaseCinfo
void process(const Eref &, ProcPtr)
double lookup1dValue(unsigned int, unsigned int, double)
double lookup1dIndex(unsigned int, unsigned int, unsigned int)
virtual ~MarkovSolverBase()