MOOSE - Multiscale Object Oriented Simulation Environment
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
VectorTable.cpp
Go to the documentation of this file.
1 /**********************************************************************
2 ** This program is part of 'MOOSE', the
3 ** Messaging Object Oriented Simulation Environment.
4 ** Copyright (C) 2003-2011 Upinder S. Bhalla. and NCBS
5 ** It is made available under the terms of the
6 ** GNU Lesser General Public License version 2.1
7 ** See the file COPYING.LIB for the full notice.
8 **********************************************************************/
9 #include "header.h"
10 #include "VectorTable.h"
11 
12 #define INIT_XMIN 0
13 #define INIT_XMAX 0
14 #define INIT_XDIV 0
15 
16 using namespace std;
17 
19 {
20  //Field information.
21  static ValueFinfo< VectorTable, unsigned int > xdivs("xdivs",
22  "Number of divisions.",
25  );
26 
27  static ValueFinfo< VectorTable, double > xmin("xmin",
28  "Minimum value in table.",
31  );
32 
33  static ValueFinfo< VectorTable, double > xmax("xmax",
34  "Maximum value in table.",
37  );
38 
39  static ReadOnlyValueFinfo< VectorTable, double > invdx("invdx",
40  "Maximum value in table.",
42  );
43 
44  static ValueFinfo< VectorTable, vector< double > > table("table",
45  "The lookup table.",
48  );
49 
51  lookupvalue(
52  "lookupvalue",
53  "Lookup function that performs interpolation to return a value.",
55  );
56 
58  lookupindex(
59  "lookupindex",
60  "Lookup function that returns value by index.",
62  );
63 
64  static Finfo* vectorTableFinfos[] =
65  {
66  &xdivs,
67  &xmin,
68  &xmax,
69  &invdx,
70  &table,
71  &lookupvalue,
72  &lookupindex
73  };
74 
75  static string doc[] =
76  {
77  "Name", "VectorTable",
78  "Author", "Vishaka Datta S, 2011, NCBS",
79  "Description", "This is a minimal 1D equivalent of the Interpol2D class. "
80  "Provides simple functions for getting and setting up the table, along "
81  "with a lookup function. This class is to be used while supplying lookup "
82  "tables to the MarkovChannel class, in cases where the transition rate "
83  "varies with either membrane voltage or ligand concentration."
84  };
85 
86  static Dinfo< VectorTable > dinfo;
87  static Cinfo VectorTableCinfo(
88  "VectorTable",
90  vectorTableFinfos,
91  sizeof( vectorTableFinfos )/sizeof( Finfo* ),
92  &dinfo,
93  doc,
94  sizeof( doc ) / sizeof( string )
95  );
96 
97  return &VectorTableCinfo;
98 }
99 
101 
103  invDx_(-1), table_(0)
104 { ; }
105 
106 //Implementation identical to that of HHGate::lookupTable.
107 double VectorTable::lookupByValue( double x ) const
108 {
109  if ( table_.size() == 1 )
110  return table_[0];
111 
112  if ( x < xMin_ || doubleEq( x, xMin_ ) )
113  return table_[0];
114  if ( x > xMax_ || doubleEq( x, xMax_ ) )
115  return table_.back();
116 
117  unsigned int index = static_cast< unsigned int>( ( x - xMin_ ) * invDx_ );
118  double frac = ( x - xMin_ - index / invDx_ ) * invDx_;
119  return table_[ index ] * ( 1 - frac ) + table_[ index + 1 ] * frac;
120 }
121 
122 double VectorTable::lookupByIndex( unsigned int index ) const
123 {
124  if ( tableIsEmpty() )
125  return 0;
126 
127  /* NOTE: Why commented out?
128  * index is unsigned int, can't be less than zero.
129  */
130 #if 0
131  //Applying similar wrapping as is done in lookupByValue.
132  if ( index < 0 )
133  index = 0;
134 #endif
135 
136  if ( index >= table_.size() )
137  index = table_.size() - 1;
138 
139  return table_[index];
140 }
141 
142 vector< double > VectorTable::getTable() const
143 {
144  if ( table_.size() == 0 )
145  {
146  cerr << "VectorTable::getTable : Warning : Table is empty\n";
147  }
148 
149  return table_;
150 }
151 
152 //Function to set up the lookup table.
153 //All parameters except xMin_ and xMax_ can be set based on the table that is
154 //passed in.
155 void VectorTable::setTable( vector< double > table )
156 {
157  if ( table.size() > 1 && xMin_ == xMax_ )
158  {
159  cerr << "VectorTable::setTable : Error : xmin and xmax cannot be the same when there are more than "
160  "two entries in the table!\n";
161  return;
162  }
163 
164  if ( table.empty() )
165  {
166  cerr << "VectorTable::setTable : Error : Cannot set with empty table!\n";
167  return;
168  }
169 
170  table_ = table;
171  xDivs_ = table.size() - 1;
172 
173  //This is in case the lookup table has only one entry, in which case,
174  //the transition rate being considered is assumed to be constant.
175  if ( table.size() > 1 )
176  invDx_ = xDivs_ / ( xMax_ - xMin_ );
177  else
178  invDx_ = 0;
179 }
180 
181 unsigned int VectorTable::getDiv() const
182 {
183  return xDivs_;
184 }
185 
186 void VectorTable::setDiv( unsigned int xDivs )
187 {
188  xDivs_ = xDivs;
189 }
190 
191 double VectorTable::getMin() const
192 {
193  return xMin_;
194 }
195 
196 void VectorTable::setMin( double xMin )
197 {
198  xMin_ = xMin;
199 }
200 
201 double VectorTable::getMax() const
202 {
203  return xMax_;
204 }
205 
206 void VectorTable::setMax( double xMax )
207 {
208  xMax_ = xMax;
209 }
210 
211 double VectorTable::getInvDx() const
212 {
213  return invDx_;
214 }
215 
217 {
218  return table_.empty();
219 }
220 
221 istream& operator>>( istream& in, VectorTable& vecTable )
222 {
223  in >> vecTable.xDivs_;
224  in >> vecTable.xMin_;
225  in >> vecTable.xMax_;
226  in >> vecTable.invDx_;
227 
228  for ( unsigned int i = 0; i < vecTable.table_.size(); ++i )
229  in >> vecTable.table_[i];
230 
231  return in;
232 }
233 
234 #ifdef DO_UNIT_TESTS
235 void testVectorTable()
236 {
237  VectorTable unInitedTable;
238 
239  vector< double > data;
240 
241  double arr[11] = {0.0, 0.23, 0.41, 0.46, 0.42, 0.52, 0.49, 0.43, 0.38, 0.43, 0.44};
242 
243  data.reserve( 11 );
244  //Filling up user table.
245  for ( int i = 0; i < 11; i++ )
246  data.push_back( arr[i] );
247 
248  unInitedTable.setMin( -0.5 );
249  unInitedTable.setMax( 0.5 );
250  unInitedTable.setTable( data );
251 
252  assert( doubleEq(unInitedTable.getInvDx(), 10 ) );
253 
254  assert( doubleEq(unInitedTable.lookupByValue( 0.0 ), 0.52 ) );
255  assert( doubleEq(unInitedTable.lookupByValue( 0.1 ), 0.49 ) );
256  assert( doubleEq(unInitedTable.lookupByValue( 0.15 ), 0.46 ) );
257  assert( doubleEq(unInitedTable.lookupByValue( 0.2 ), 0.43 ) );
258  assert( doubleEq(unInitedTable.lookupByValue( 0.25 ), 0.405 ) );
259  assert( doubleEq(unInitedTable.lookupByValue( 0.3 ), 0.38 ) );
260  assert( doubleEq(unInitedTable.lookupByValue( 0.35 ), 0.405 ) );
261  assert( doubleEq(unInitedTable.lookupByValue( 0.4 ), 0.43 ) );
262  assert( doubleEq(unInitedTable.lookupByValue( 0.45 ), 0.435 ) );
263  assert( doubleEq(unInitedTable.lookupByValue( 0.5 ), 0.44 ) );
264 
265  cout << "." << flush;
266 }
267 #endif
double lookupByValue(double) const
double getInvDx() const
double getMin() const
vector< double > getTable() const
bool tableIsEmpty() const
Definition: Dinfo.h:60
void setMin(double)
void setTable(vector< double >)
#define INIT_XMAX
Definition: VectorTable.cpp:13
static const Cinfo * vectorTableCinfo
void testVectorTable()
unsigned int xDivs_
Definition: VectorTable.h:50
#define INIT_XMIN
Definition: VectorTable.cpp:12
bool doubleEq(double x, double y)
Definition: doubleEq.cpp:16
vector< double > table_
Definition: VectorTable.h:55
double xMin_
Definition: VectorTable.h:51
void setMax(double)
unsigned int getDiv() const
istream & operator>>(istream &in, VectorTable &vecTable)
#define INIT_XDIV
Definition: VectorTable.cpp:14
double xMax_
Definition: VectorTable.h:52
double invDx_
Definition: VectorTable.h:53
static const Cinfo * initCinfo()
Definition: Neutral.cpp:16
static const Cinfo * initCinfo()
Definition: VectorTable.cpp:18
void setDiv(unsigned int)
Definition: Cinfo.h:18
double getMax() const
double lookupByIndex(unsigned int) const
Definition: Finfo.h:12