NEURON
kschan.h
Go to the documentation of this file.
1 #ifndef kschan_h
2 #define kschan_h
3 
4 #include <math.h>
5 #include <OS/string.h>
6 #include "nrnoc2iv.h"
7 #include "ivocvect.h"
8 #include "nrnunits_modern.h"
9 
10 #include "spmatrix.h"
11 
12 //extern double dt;
13 extern double celsius;
14 
15 class KSState;
16 class KSChan;
17 class KSSingle;
18 struct NrnThread;
19 
21 public:
23  virtual ~KSChanFunction();
24  virtual int type() { return 0; }
25  virtual double f(double v) { return 1.; }
26  void f(int cnt, double* v, double* val) {
27  int i; for (i=0; i < cnt; ++i) { val[i] = f(v[i]); }
28  }
29  static KSChanFunction* new_function(int type, Vect*, double , double);
30  Vect* gp_; // gate parameters
31  double c(int i) { return gp_->elem(i); }
32  static double Exp(double x) { if (x > 700.) {return exp(700.);
33  } else if (x < -700.) { return exp(-700.);
34  } else { return exp(x); } }
35 };
36 
37 class KSChanConst : public KSChanFunction {
38 public:
39  virtual int type() { return 1; }
40  virtual double f(double v) { return c(0); }
41 };
42 
43 class KSChanExp : public KSChanFunction {
44 public:
45  virtual int type() { return 2; }
46  virtual double f(double v) { return c(0)*Exp(c(1) * (v - c(2))); }
47 };
48 
49 class KSChanLinoid : public KSChanFunction {
50 public:
51  virtual int type() { return 3; }
52  virtual double f(double v) {
53  double x = c(1)*(v - c(2));
54  if (fabs(x) > 1e-6) {
55  return c(0)*x/(1 - Exp(-x));
56  }else{
57  return c(0)*(1 + x/2.);
58  }
59  }
60 };
61 
62 class KSChanSigmoid : public KSChanFunction {
63 public:
64  virtual int type() { return 4; }
65  virtual double f(double v) {
66  return c(0) / (1. + Exp(c(1) * (v - c(2))));
67  }
68 };
69 
70 
71 // e/(kT) e/k=11.604589 from hoc's FARADAY and R values (legacy units)
72 #define _e_over_k _e_over_k_[_nrnunit_use_legacy_]
73 static double _e_over_k_[2] = {_e_over_k_codata2018, 11.604589}; /* K/mV */
74 #define ebykt (_e_over_k/(273.15 + celsius))
75 
76 // from MODELING NEURONAL BIOPHYSICS Lyle J Graham
77 //A Chapter in the Handbook of Brain Theory and Neural Networks, Volume 2
78 //a' = K*exp(z*gam*(v-vhalf)*F/RT)
79 //b' = K*exp(-z*(1-gam)*(v-vhalf)*F/RT)
80 //but tau = 1/(a' + b') + tau0
81 //and inf = a'/(a' + b')
82 // so there is no fast way to get either a,b or inf,tau individually
83 
84 class KSChanBGinf : public KSChanFunction {
85 public:
86  virtual int type() { return 5; }
87  virtual double f(double v) {
88  double x = ebykt*c(2)*(v - c(1));
89  double ap = c(0) * Exp(c(3)*x);
90  double bp = c(0) * Exp((c(3) - 1.)*x);
91  tau = 1/(ap + bp);
92  double inf = ap*tau; tau += c(4);
93  return inf;
94  }
95  double tau; // may avoid duplicate call to KSChanBGtau
96 };
97 
98 class KSChanBGtau : public KSChanFunction {
99 public:
100  virtual int type() { return 6; }
101  virtual double f(double v) {
102  double x = ebykt*c(2)*(v - c(1));
103  double ap = c(0) * Exp(c(3)*x);
104  double bp = c(0) * Exp((c(3) - 1.)*x);
105  double tau = 1/(ap + bp);
106  inf = ap*tau; tau += c(4);
107  return tau;
108  }
109  double inf; // may avoid duplicate call to KSChanBGinf
110 };
111 
112 class KSChanTable : public KSChanFunction {
113 public:
114  KSChanTable(Vect*, double vmin, double vmax);
115  virtual int type() { return 7; }
116  virtual double f(double v);
117  double vmin_, vmax_;
118 private:
119  double dvinv_;
120 };
121 
123 public:
124  KSTransition();
125  virtual ~KSTransition();
126  // vmin, vmax only for type KSChanTable
127  void setf(int direction, int type, Vect* vec, double vmin, double vmax);
128  // only voltage gated for now
129  double alpha(double v) { return type_ == 0 ? f0->f(v) : f0->f(v)/f1->f(v); }
130  double beta(double v) { return type_ == 0 ? f1->f(v) : (1. - f0->f(v))/f1->f(v); }
131  double inf(double v) { return type_ == 1 ? f0->f(v) : f0->f(v)/(f0->f(v) + f1->f(v)); }
132  double tau(double v) { return type_ == 1 ? f1->f(v) : 1./(f0->f(v) + f1->f(v)); }
133  void ab(double v, double& a, double& b);
134  void ab(Vect* v, Vect* a, Vect* b);
135  void inftau(double v, double& inf, double& tau);
136  void inftau(Vect* v, Vect* inf, Vect* tau);
137  // hh tables
138  // easily out of date!!
139  // in anything about f0 or f1 changes then must call hh_table_make;
140  void hh_table_make(double dt, int size=200, double vmin=-100., double vmax=50.);
141  bool usehhtable() { return (size1_ > 0); }
142  void inftau_hh_table(int i, double& inf, double& tau) {
143  inf = inftab_[i];
144  tau = tautab_[i];
145  }
146  void inftau_hh_table(int i, double x, double& inf, double& tau) {
147  inf = inftab_[i] + (inftab_[i+1] - inftab_[i])*x;
148  tau = tautab_[i] + (tautab_[i+1] - tautab_[i])*x;
149  }
150  // the agent style
151  virtual double alpha(Datum*);
152  virtual double beta();
153  void lig2pd(int pdoff);
154 public:
156  int index_; // into trans_ array
157  int src_;
158  int target_;
162  int type_; // 0-ab,voltage gated; 1-inftau voltage gated
163  // 2-ligand outside; 3-ligand inside
167 private:
168  // for hh tables.
169  double* inftab_;
170  double* tautab_;
171  int size1_;
172 };
173 
175 public:
176  KSGateComplex();
177  virtual ~KSGateComplex();
178  double conductance(double* state, KSState* st);
179 public:
182  int index_; // into gc_ array
183  int sindex_; // starting state index for this complex
184  int nstate_; // number of states
185  int power_; // eg. n^4, or m^3
186 };
187 
188 class KSIv {
189 public:
190  // this one for ionic ohmic and nernst.
191  virtual double cur(double g, double* p, Datum* pd, double v);
192  virtual double jacob(double* p, Datum* pd, double v);
193 };
194 class KSIvghk : public KSIv {
195 public:
196  // this one for ionic Goldman-Hodgkin-Katz
197  virtual double cur(double g, double* p, Datum* pd, double v);
198  virtual double jacob(double* p, Datum* pd, double v);
199  double z;
200 };
201 class KSIvNonSpec : public KSIv{
202  // this one for non-specific ohmic. There will be a PARAMETER e_suffix at p[1]
203  virtual double cur(double g, double* p, Datum* pd, double v);
204  virtual double jacob(double* p, Datum* pd, double v);
205 };
206 
207 class KSPPIv : public KSIv {
208 public:
209  // this one for POINT_PROCESS ionic ohmic and nernst.
210  virtual double cur(double g, double* p, Datum* pd, double v);
211  virtual double jacob(double* p, Datum* pd, double v);
212  int ppoff_;
213 };
214 class KSPPIvghk : public KSPPIv {
215 public:
216  // this one for POINT_PROCESS ionic Goldman-Hodgkin-Katz
217  virtual double cur(double g, double* p, Datum* pd, double v);
218  virtual double jacob(double* p, Datum* pd, double v);
219  double z;
220 };
221 class KSPPIvNonSpec : public KSPPIv{
222  // this one for POINT_PROCESS non-specific ohmic. There will be a PARAMETER e_suffix at p[1]
223  virtual double cur(double g, double* p, Datum* pd, double v);
224  virtual double jacob(double* p, Datum* pd, double v);
225 };
226 
227 class KSState {
228 public:
229  KSState();
230  virtual ~KSState();
231  const char* string() { return name_.string(); }
232  double f_; // normalized conductance
234  int index_; // into state_ array
237 };
238 
239 class KSChan {
240 public:
241  KSChan(Object*, bool is_point = false);
242  virtual ~KSChan();
243  virtual void alloc(Prop*);
244  virtual void init(int, Node**, double**, Datum**, NrnThread*);
245  virtual void cur(int, Node**, double**, Datum**);
246  virtual void jacob(int, Node**, double**, Datum**);
247  virtual void state(int, Node**, double**, Datum**, NrnThread*);
248 #if CACHEVEC != 0
249  virtual void cur(int, int *, double**, Datum**, NrnThread*);
250  virtual void jacob(int, int *, double**, Datum**, NrnThread*);
251  virtual void state(int, int *, Node**, double**, Datum**, NrnThread*);
252 #endif /* CACHEVEC */
253  void add_channel(const char**);
254  //for cvode
255  virtual int count();
256  virtual void map(int, double**, double**, double*, Datum*, double*);
257  virtual void spec(int, Node**, double**, Datum**);
258  virtual void matsol(int, Node**, double**, Datum**, NrnThread*);
259  virtual void cv_sc_update(int, Node**, double**, Datum**, NrnThread*);
260  double conductance(double gmax, double* state);
261 public:
262  // hoc accessibilty
263  int state(const char* name);
264  const char* state(int index);
265  int trans_index(const char* src, const char* target); // index of the transition
266  int trans_index(int src, int target); // index of the transition
267  int gate_index(int state_index); // index of the gate
268  bool is_point() { return is_point_; }
269  bool is_single() { return is_single_; }
270  void set_single(bool, bool update = true);
271  void destroy_pnt(Point_process*);
272  int nsingle(Point_process*);
273  void nsingle(Point_process*, int);
274  double alpha(double v, int, int) {return 0.;}
275  double beta(double v, int, int) {return 0.;}
276  void setstructure(Vect*);
277  void setname(const char*);
278  void setsname(int, const char*);
279  void setion(const char*);
280  void setligand(int i, const char*);
281  void settype(KSTransition*, int type, const char*);
282  void setivrelation(int);
283  // hoc incremental management
284  KSState* add_hhstate(const char*);
285  KSState* add_ksstate(int igate, const char*);
286  void remove_state(int);
287  // these are only for kinetic scheme transitions since an hh
288  // always has one and only one transition.
289  KSTransition* add_transition(int src, int target, const char* ligand);
290  void remove_transition(int);
291  void setcond();
292  void power(KSGateComplex*, int);
293  void usetable(bool, int size, double vmin, double vmax);
294  void usetable(bool);
295  int usetable(double* vmin, double* vmax);// get info
296  bool usetable() { return usetable_; }
297  void check_table_thread(NrnThread*);
298 private:
299  void free1();
300  void build();
301  void setupmat();
302  void fillmat(double v, Datum* pd);
303  void mat_dt(double dt, double* p);
304  void solvemat(double*);
305  void mulmat(double*, double*);
306  void ion_consist();
307  void ligand_consist(int, int, Prop*, Node*);
308  Prop* needion(Symbol*, Node*, Prop*);
309  void state_consist(int shift = 0);
310  void sname_install();
311  Symbol* looksym(const char*, Symbol* tmplt = NULL);
312  Symbol* installsym(const char*, int, Symbol* tmplt = NULL);
313  void freesym(Symbol*, Symbol* tmplt = NULL);
314  Symbol** newppsym(int);
315  void delete_schan_node_data();
316  void alloc_schan_node_data();
317  void update_prop(); // can add and remove Nsingle and SingleNodeData
318 
319  KSState* state_insert(int i, const char* name, double frac);
320  void state_remove(int i);
321  KSGateComplex* gate_insert(int ig, int is, int power);
322  void gate_remove(int i);
323  KSTransition* trans_insert(int i, int src, int target);
324  void trans_remove(int i);
325  void check_struct();
329  bool is_point_;
331  // for point process
334 public:
335  CopyString name_; // name of channel
336  CopyString ion_; // name of ion , "" means non-specific
337  double gmax_deflt_;
338  double erev_deflt_;
341  int ngate_; // number of gating complexes
342  int ntrans_; // total number of transitions
343  int ivkstrans_; // index of beginning of voltage sensitive ks transitions
344  int iligtrans_; // index of beginning of ligand sensitive ks transitions
345  // 0 to ivkstrans_ - 1 are the hh transitions
346  int nhhstate_; // total number of hh states, does not include ks states
347  int nksstate_; // total number of kinetic scheme states.
348  int nstate_; // total number of all states, nhhstate_ to nstate_ - 1
349  // are kinetic scheme states. The nhhstate_ are first
350  // and the nhhstate_ = ivkstrans_
351  KSState* state_; // the state names
353  KSTransition* trans_; // array of transitions
354  Symbol* ion_sym_; // if NULL then non-specific and e_suffix is a parameter
355  int nligand_;
359 private:
361  Symbol* mechsym_; // the top level symbol (insert sym or new sym)
362  Symbol* rlsym_; // symbol with the range list (= mechsym_ when density)
363  char* mat_;
364  double** elms_;
365  double** diag_;
366  int dsize_; // size of prop->dparam
367  int psize_; // size of prop->param
368  int soffset_; //STATE begins here in the p array.
369  int gmaxoffset_; // gmax is here in the p array, normally 0 but if
370  // there is an Nsingle then it is 1.
371  int ppoff_; //2 or 3 for point process since area and Point_process* are
372  // first two elements and there may be a KSSingleNodeData*
373  // for hh rate tables
374  double vmin_, vmax_, dvinv_, dtsav_;
376  bool usetable_;
377 };
378 
379 #endif
bool usetable()
Definition: kschan.h:296
static KSChanFunction * new_function(int type, Vect *, double, double)
Definition: kschan.cpp:2930
int nstate_
Definition: kschan.h:184
KSSingle * single_
Definition: kschan.h:358
virtual int type()
Definition: kschan.h:51
virtual double f(double v)
Definition: kschan.h:101
KSTransition * trans_
Definition: kschan.h:353
static void * vmin(NrnThread *nt)
int nligand_
Definition: kschan.h:355
static double Exp(double x)
Definition: kschan.h:32
int src_
Definition: kschan.h:157
virtual double f(double v)
Definition: kschan.h:87
int gate_size_
Definition: kschan.h:327
int mechtype_
Definition: kschan.h:333
#define Vect
Definition: ivocvect.h:14
int type_
Definition: kschan.h:162
double beta(double v)
Definition: kschan.h:130
double gmax_deflt_
Definition: kschan.h:337
int index_
Definition: kschan.h:182
double * inftab_
Definition: kschan.h:169
#define g
Definition: passive0.cpp:23
double inf(double v)
Definition: kschan.h:131
double erev_deflt_
Definition: kschan.h:338
KSChan * ks_
Definition: kschan.h:181
int gmaxoffset_
Definition: kschan.h:369
#define ebykt
Definition: kschan.h:74
size_t p
Represent main neuron object computed by single thread.
Definition: multicore.h:58
virtual int type()
Definition: kschan.h:100
virtual ~KSChanFunction()
Definition: kschan.cpp:2924
double ** elms_
Definition: kschan.h:364
int index_
Definition: kschan.h:156
virtual double f(double v)
Definition: kschan.h:46
virtual double f(double v)
Definition: kschan.h:52
static void update(NrnThread *)
Definition: fadvance.cpp:570
#define v
Definition: md1redef.h:4
virtual int type()
Definition: kschan.h:115
KSState * state_
Definition: kschan.h:351
char * mat_
Definition: kschan.h:363
double c(int i)
Definition: kschan.h:31
double inf
Definition: kschan.h:109
int ngate_
Definition: kschan.h:341
virtual int type()
Definition: kschan.h:39
#define cur
Definition: eion.cpp:338
#define e
Definition: passive0.cpp:24
void inftau_hh_table(int i, double x, double &inf, double &tau)
Definition: kschan.h:146
Object * obj_
Definition: kschan.h:155
int size1_
Definition: kschan.h:171
double z
Definition: kschan.h:199
void init()
Definition: init.cpp:169
double alpha(double v)
Definition: kschan.h:129
double dt
Definition: init.cpp:123
double ** diag_
Definition: kschan.h:365
static double map(void *v)
Definition: mlinedit.cpp:46
double tau
Definition: kschan.h:95
int ligand_index_
Definition: kschan.h:164
bool usehhtable()
Definition: kschan.h:141
const char * string()
Definition: kschan.h:231
virtual double f(double v)
Definition: kschan.h:65
int cond_model_
Definition: kschan.h:339
Symbol * mechsym_
Definition: kschan.h:361
double z
Definition: kschan.h:219
int ppoff_
Definition: kschan.h:371
int val
Definition: dll.cpp:167
Object * obj_
Definition: kschan.h:180
double vmin_
Definition: kschan.h:374
virtual double f(double v)
Definition: kschan.h:25
#define _e_over_k_codata2018
int power_
Definition: kschan.h:185
Object * obj_
Definition: kschan.h:357
virtual int type()
Definition: kschan.h:45
#define power
Definition: redef.h:107
#define cnt
Definition: spt2queue.cpp:19
CopyString name_
Definition: kschan.h:233
Object * obj_
Definition: kschan.h:236
exp
Definition: extdef.h:3
#define alpha
Definition: bkpfacto.c:43
Definition: model.h:57
double * tautab_
Definition: kschan.h:170
double alpha(double v, int, int)
Definition: kschan.h:274
Definition: section.h:213
char * name
Definition: init.cpp:16
int index_
Definition: kschan.h:234
double f_
Definition: kschan.h:232
int cvode_ieq_
Definition: kschan.h:360
void f(int cnt, double *v, double *val)
Definition: kschan.h:26
int nhhstate_
Definition: kschan.h:346
#define matsol
Definition: lineq.h:7
Symbol ** ligands_
Definition: kschan.h:356
int pointtype_
Definition: kschan.h:332
static double _e_over_k_[2]
Definition: kschan.h:73
int hh_tab_size_
Definition: kschan.h:375
KSChanFunction * f0
Definition: kschan.h:160
Symbol * rlsym_
Definition: kschan.h:362
Definition: kschan.h:207
KSGateComplex * gc_
Definition: kschan.h:352
int ntrans_
Definition: kschan.h:342
double vmin_
Definition: kschan.h:117
int nstate_
Definition: kschan.h:348
KSIv * iv_relation_
Definition: kschan.h:340
int nksstate_
Definition: kschan.h:347
Definition: hocdec.h:226
void inftau_hh_table(int i, double &inf, double &tau)
Definition: kschan.h:142
int stoichiom_
Definition: kschan.h:166
KSChan * ks_
Definition: kschan.h:159
bool is_single()
Definition: kschan.h:269
#define i
Definition: md1redef.h:12
int ivkstrans_
Definition: kschan.h:343
int pd_index_
Definition: kschan.h:165
int state_size_
Definition: kschan.h:326
fabs
Definition: extdef.h:3
int ppoff_
Definition: kschan.h:212
KSChanFunction * f1
Definition: kschan.h:161
bool usetable_
Definition: kschan.h:376
bool is_point_
Definition: kschan.h:329
int dsize_
Definition: kschan.h:366
double tau(double v)
Definition: kschan.h:132
bool is_point()
Definition: kschan.h:268
bool is_single_
Definition: kschan.h:330
virtual double f(double v)
Definition: kschan.h:40
KSChan * ks_
Definition: kschan.h:235
Symbol * ion_sym_
Definition: kschan.h:354
CopyString ion_
Definition: kschan.h:336
int soffset_
Definition: kschan.h:368
int trans_size_
Definition: kschan.h:328
int psize_
Definition: kschan.h:367
CopyString name_
Definition: kschan.h:335
Definition: kschan.h:239
Definition: section.h:132
int target_
Definition: kschan.h:158
virtual int type()
Definition: kschan.h:86
Definition: kschan.h:188
Definition: hocdec.h:176
int iligtrans_
Definition: kschan.h:344
double celsius
Definition: init.cpp:123
return NULL
Definition: cabcode.cpp:461
double dvinv_
Definition: kschan.h:119
virtual int type()
Definition: kschan.h:24
short index
Definition: cabvars.h:11
int sindex_
Definition: kschan.h:183
double beta(double v, int, int)
Definition: kschan.h:275
Vect * gp_
Definition: kschan.h:30
virtual int type()
Definition: kschan.h:64