Xyce  6.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
N_DEV_Neuron.h
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 // Copyright Notice
3 //
4 // Copyright 2002 Sandia Corporation. Under the terms
5 // of Contract DE-AC04-94AL85000 with Sandia Corporation, the U.S.
6 // Government retains certain rights in this software.
7 //
8 // Xyce(TM) Parallel Electrical Simulator
9 // Copyright (C) 2002-2011 Sandia Corporation
10 //
11 // This program is free software: you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation, either version 3 of the License, or
14 // (at your option) any later version.
15 //
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program. If not, see <http://www.gnu.org/licenses/>.
23 //-----------------------------------------------------------------------------
24 
25 //-----------------------------------------------------------------------------
26 // Filename : $RCSfile: N_DEV_Neuron.h,v $
27 //
28 // Purpose : Neuron classes.
29 //
30 // Special Notes :
31 //
32 // Creator : Richard Schiek, SNL, Electrical and Microsystem Modeling
33 //
34 // Creation Date : 01/02/08
35 //
36 // Revision Information:
37 // ---------------------
38 //
39 // Revision Number: $Revision: 1.38 $
40 //
41 // Revision Date : $Date: 2014/05/21 18:25:50 $
42 //
43 // Current Owner : $Author: dgbaur $
44 //-----------------------------------------------------------------------------
45 
46 #ifndef Xyce_N_DEV_Neuron_h
47 #define Xyce_N_DEV_Neuron_h
48 
49 #include <N_DEV_Configuration.h>
50 #include <Sacado.hpp>
51 
52 // ---------- Xyce Includes ----------
53 #include <N_DEV_DeviceMaster.h>
54 #include <N_DEV_DeviceBlock.h>
55 #include <N_DEV_DeviceInstance.h>
56 #include <N_DEV_DeviceModel.h>
57 
58 #ifdef HAVE_MATH_H
59 #include <math.h>
60 #endif
61 
62 namespace Xyce {
63 namespace Device {
64 namespace Neuron {
65 
66 class Model;
67 class Instance;
68 
69 struct Traits : public DeviceTraits<Model, Instance>
70 {
71  static const char *name() {return "Neuron";}
72  static const char *deviceTypeName() {return "YNEURON level 1";}
73  static int numNodes() {return 2;}
74  static bool modelRequired() {return true;}
75  static bool isLinearDevice() {return true;}
76 
77  static Device *factory(const Configuration &configuration, const FactoryBlock &factory_block);
78  static void loadModelParameters(ParametricData<Model> &model_parameters);
79  static void loadInstanceParameters(ParametricData<Instance> &instance_parameters);
80 };
81 
82 //-----------------------------------------------------------------------------
83 // Class : Instance
84 // Purpose : This is class refers to a single instance of the
85 // Neuron device. It has two nodes associated with it, a
86 // positive and a negative node. See the NeuronInstance
87 // class for a more detailed explanation.
88 // Special Notes :
89 // Creator : Richard Schiek, SNL, Electrical and Microsystem Modeling
90 // Creation Date : 01/02/08
91 //-----------------------------------------------------------------------------
92 class Instance : public DeviceInstance
93 {
94  friend class ParametricData<Instance>;
95  friend class Model;
96  friend class Traits;friend class Master;
97 
98 public:
99  static std::vector< std::vector<int> > jacStamp;
100 
101  Instance(
102  const Configuration & configuration,
103  const InstanceBlock & IB,
104  Model & Miter,
105  const FactoryBlock & factory_block);
106 
107 
108  ~Instance();
109 
110 private:
111  Instance(const Instance &);
112  Instance &operator=(const Instance &);
113 
114 public:
115  void registerLIDs( const std::vector<int> & intLIDVecRef,
116  const std::vector<int> & extLIDVecRef );
117  void registerStateLIDs( const std::vector<int> & staLIDVecRef );
118 
119  std::map<int,std::string> & getIntNameMap ();
120  bool loadDeviceMask();
121  const std::vector< std::vector<int> > & jacobianStamp() const;
122  void registerJacLIDs( const std::vector< std::vector<int> > & jacLIDVec );
123 
124  bool processParams ();
125  bool updateTemperature(const double & temp_tmp);
126 
127  bool updateIntermediateVars ();
128  bool updatePrimaryState ();
129  bool updateSecondaryState ();
130  bool setIC ();
131 
132  void varTypes( std::vector<char> & varTypeVec );
133 
134  // load functions, residual:
135  bool loadDAEQVector ();
136  bool loadDAEFVector ();
137 
138  void auxDAECalculations ();
139 
140  // load functions, Jacobian:
141  bool loadDAEdQdx ();
142  bool loadDAEdFdx ();
143 
144 private:
145  // These functions represent the equations that need to be solved
146  // for this device. Since Xyce loads an F and Q contribution, the
147  // equations are broken up into their F and Q components. Thus there
148  // is a kcl1EquF() and kcl1EquQ(). Automatic differentiation will
149  // be used to generate all the derivatives of these equations for the
150  // dF/dX and dQ/dX loads
151 
152  // first we list some utility functions for calculating coefficients.
153  // alpha and beta equations are taken from Koch.
154  // They're generally functions of membrane voltage; here, the membrane voltage is
155  // the difference between Vn1 and Vn2.
156  // Also, in the Koch formulation, the voltage is relative to the resting potential;
157  // equations adjusted here to accommodate a nonzero resting potential vRest.
158  // These functions expect V to be in milli-volts and then return values that
159  // are in 1/ms. Thus the extra factor's of 1000 here and there
160  //
161  // potassium current, functions for activator equation
162  template <typename ScalarT>
163  static ScalarT alphaN( const ScalarT & Vn1, const ScalarT & Vn2, const ScalarT & Vrest)
164  {
165  ScalarT vDiff = 1000.0 * (Vn1 - Vn2 - Vrest); // convert voltage to milli-volts
166  // and shift to account for nonzero Vrest
167  ScalarT r;
168  if ((vDiff > 9.99) && (vDiff < 10.01) )
169  {
170  r = 1.0/(10.0 * ( std::exp( (10.0 - vDiff)/10.0 )));
171  }
172  else
173  {
174  r = (10.0 - vDiff) /
175  (100.0 * ( std::exp( (10.0 - vDiff)/10.0 ) - 1.0 ));
176  }
177  r *= 1000.0; // change from 1/ms to 1/s
178  return r;
179  }
180 
181  template <typename ScalarT>
182  static ScalarT betaN( const ScalarT & Vn1, const ScalarT & Vn2, const ScalarT & Vrest)
183  {
184  ScalarT vDiff = 1000.0 * (Vn1 - Vn2 - Vrest);
185  ScalarT r = 0.125 * std::exp( -vDiff/80.0 );
186  r *= 1000.0; // change from 1/ms to 1/s
187  return r;
188  }
189 
190  // sodium current, functions for activator equation
191  template <typename ScalarT>
192  static ScalarT alphaM( const ScalarT & Vn1, const ScalarT & Vn2, const ScalarT & Vrest)
193  {
194  ScalarT vDiff = 1000.0 * (Vn1 - Vn2 - Vrest);
195  ScalarT r;
196  if ((vDiff > 24.99) && (vDiff < 25.01) )
197  {
198  r = (1.0) /
199  (( std::exp( (25.0 - vDiff)/10.0 )));
200  }
201  else
202  {
203  r = (25.0 - vDiff) /
204  (10.0 * ( std::exp( (25.0 - vDiff)/10.0 ) - 1.0 ));
205  }
206  r *= 1000.0; // change from 1/ms to 1/s
207  return r;
208  }
209 
210  template <typename ScalarT>
211  static ScalarT betaM( const ScalarT & Vn1, const ScalarT & Vn2, const ScalarT & Vrest)
212  {
213  ScalarT vDiff = 1000.0 * (Vn1 - Vn2 - Vrest);
214  ScalarT r = 4.0 * std::exp( -vDiff/18.0 );
215  r *= 1000.0; // change from 1/ms to 1/s
216 
217  return r;
218  }
219 
220  template <typename ScalarT>
221  static ScalarT alphaH( const ScalarT & Vn1, const ScalarT & Vn2, const ScalarT & Vrest)
222  {
223  ScalarT vDiff = 1000.0 * (Vn1 - Vn2 - Vrest);
224  ScalarT r = 0.07 * std::exp( -vDiff/20.0 );
225  r *= 1000.0; // change from 1/ms to 1/s
226  return r;
227  }
228 
229  template <typename ScalarT>
230  static ScalarT betaH( const ScalarT & Vn1, const ScalarT & Vn2, const ScalarT & Vrest)
231  {
232  ScalarT vDiff = 1000.0 * (Vn1 - Vn2 - Vrest);
233  ScalarT r = 1.0 / ( std::exp( (30.0 - vDiff)/10.0 ) + 1.0 );
234  r *= 1000.0; // change from 1/ms to 1/s
235  return r;
236  }
237 
238  // now the device equations
239  // KCL equation 1
240  template <typename ScalarT>
241  static ScalarT kcl1EquF( const ScalarT& Vn1, const ScalarT& Vn2, const ScalarT& n, const ScalarT& m, const ScalarT& h,
242  const ScalarT& memG, const ScalarT& leakE, const ScalarT& Kg, const ScalarT& Ke, const ScalarT& NaG, const ScalarT& NaE )
243  {
244  ScalarT powN = n * n * n * n;
245  ScalarT powM = m * m * m;
246  ScalarT r = memG * (Vn1 - Vn2 - leakE) + Kg * powN * (Vn1 - Vn2 - Ke ) + NaG * powM * h * (Vn1 - Vn2 - NaE );
247  return r;
248  }
249 
250  template <typename ScalarT>
251  static ScalarT kcl1EquQ( const ScalarT& Vn1, const ScalarT& Vn2, const ScalarT& memC )
252  {
253  ScalarT r = memC * (Vn1 - Vn2);
254  return r;
255  }
256 
257  // KCL equation 2 -- -1 * equation 1 because of device symmetry
258  template <typename ScalarT>
259  static ScalarT kcl2EquF( const ScalarT& Vn1, const ScalarT& Vn2, const ScalarT& n, const ScalarT& m, const ScalarT& h,
260  const ScalarT& memG, const ScalarT& leakE, const ScalarT& Kg, const ScalarT& Ke, const ScalarT& NaG, const ScalarT& NaE )
261  {
262  ScalarT powN = n * n * n * n;
263  ScalarT powM = m * m * m;
264  ScalarT r = -1.0*(memG * (Vn1 - Vn2 - leakE) + Kg * powN * (Vn1 - Vn2 - Ke ) + NaG * powM * h * (Vn1 - Vn2 - NaE ));
265  return r;
266  }
267 
268  template <typename ScalarT>
269  static ScalarT kcl2EquQ( const ScalarT& Vn1, const ScalarT& Vn2, const ScalarT& memC )
270  {
271  ScalarT r = -1.0 * memC * (Vn1 - Vn2);
272  return r;
273  }
274 
275  // n conservation equation
276  template <typename ScalarT>
277  static ScalarT nEquF( const ScalarT& Vn1, const ScalarT& Vn2, const ScalarT& n, const ScalarT& Vrest )
278  {
279  ScalarT r = alphaN<ScalarT>( Vn1, Vn2, Vrest ) * (1.0 - n ) - betaN<ScalarT>( Vn1, Vn2, Vrest ) * n;
280  return r;
281  }
282 
283  template <typename ScalarT>
284  static ScalarT nEquQ( const ScalarT& n )
285  {
286  ScalarT r = -n;
287  return r;
288  }
289 
290  // m conservation equation
291  template <typename ScalarT>
292  static ScalarT mEquF( const ScalarT& Vn1, const ScalarT& Vn2, const ScalarT& m, const ScalarT& Vrest )
293  {
294  ScalarT r = alphaM<ScalarT>( Vn1, Vn2, Vrest ) * (1.0 - m ) - betaM<ScalarT>( Vn1, Vn2, Vrest ) * m;
295  return r;
296  }
297 
298  template <typename ScalarT>
299  static ScalarT mEquQ( const ScalarT& m )
300  {
301  ScalarT r = -m;
302  return r;
303  }
304 
305  // h conservation equation
306  template <typename ScalarT>
307  static ScalarT hEquF( const ScalarT& Vn1, const ScalarT& Vn2, const ScalarT& h, const ScalarT& Vrest )
308  {
309  ScalarT r = alphaH<ScalarT>( Vn1, Vn2, Vrest ) * (1.0 - h ) - betaH<ScalarT>( Vn1, Vn2, Vrest ) * h;
310  return r;
311  }
312 
313  template <typename ScalarT>
314  static ScalarT hEquQ( const ScalarT& h )
315  {
316  ScalarT r = -h;
317  return r;
318  }
319 
320 public:
321  // iterator reference to the Neuron model which owns this instance.
322  // Getters and setters
324  {
325  return model_;
326  }
327 
328 private:
329 
330  Model & model_; //< Owning model
331 
332  // derrived quantities computed in updateIntermediateVars
333  // and used in the load functions
344 
345  // state variables
348 
349  // local state indices (offsets)
352 
353  // local solution indices (offsets)
354  int li_Pos; // local index to positive node on this device
355  int li_Neg; // local index to negative node on this device
356  int li_nPro; // local index to n promoter value (Na current)
357  int li_mPro; // local index to m promoter value (K current)
358  int li_hPro; // local index to h promoter value (K current)
359 
360  // Matrix equation index variables:
361 
362  // Offset variables corresponding to the above declared indices.
368 
374 
378 
382 
386 };
387 
388 //-----------------------------------------------------------------------------
389 // Class : Model
390 // Purpose :
391 // Special Notes :
392 // Creator : Richard Schiek, SNL, Electrical and Microsystem Modeling
393 // Creation Date : 01/02/08
394 //-----------------------------------------------------------------------------
395 class Model : public DeviceModel
396 {
397  typedef std::vector<Instance *> InstanceVector;
398 
399  friend class ParametricData<Model>;
400  friend class Instance;
401  friend class Traits;friend class Master;
402 
403 public:
404  Model(
405  const Configuration & configuration,
406  const ModelBlock & MB,
407  const FactoryBlock & factory_block);
408  ~Model();
409 
410 private:
411  Model();
412  Model(const Model &);
413  Model &operator=(const Model &);
414 
415 public:
416  virtual void forEachInstance(DeviceInstanceOp &op) const /* override */;
417 
418  virtual std::ostream &printOutInstances(std::ostream &os) const;
419 
420  bool processParams ();
421  bool processInstanceParams ();
422 
423 private:
424 
425  // parameter variables
426  double cMem; // membrane capacitance
427  double gMem; // membrane conductance of leak current
428  double eLeak; // reversal potential of leak current
429  double eNa; // sodium reversal potential
430  double gNa; // sodium base conductance
431  double eK; // potassium reversal potential
432  double gK; // potassium base conductance
433  double vRest; // resting potential
434 
435  // flags that parameters were given
436  bool cMemGiven;
437  bool gMemGiven;
439  bool eNaGiven;
440  bool gNaGiven;
441  bool eKGiven;
442  bool gKGiven;
444 
445 
446 public:
447  void addInstance(Instance *instance)
448  {
449  instanceContainer.push_back(instance);
450  }
451 
452 private:
453  std::vector<Instance*> instanceContainer;
454 };
455 
456 
457 //-----------------------------------------------------------------------------
458 // Class : Master
459 // Purpose :
460 // Special Notes :
461 // Creator : Richard Schiek, SNL, Parallel Computational Sciences
462 // Creation Date : 06/01/12
463 //-----------------------------------------------------------------------------
464 class Master : public DeviceMaster<Traits>
465 {
466  friend class Neuron::Instance;
467  friend class Neuron::Model;
468 
469 public:
471  const Configuration & configuration,
472  const FactoryBlock & factory_block,
473  const SolverState & ss1,
474  const DeviceOptions & do1)
475  : DeviceMaster<Traits>(configuration, factory_block, ss1, do1)
476  {}
477 
478  virtual bool updateState (double * solVec, double * staVec, double * stoVec);
479 };
480 
481 void registerDevice();
482 
483 } // namespace Neuron
484 } // namespace Device
485 } // namespace Xyce
486 
490 
491 #endif