NEURON
cvodeobj.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "nrnmpi.h"
4 #include "nrnneosm.h"
5 //#include "shared/nvector_serial.h"
6 #include "shared/nvector.h"
7 #include "membfunc.h"
8 #include "netcon.h"
9 #include "tqitem.hpp"
10 #include "htlist.h"
11 
12 class NetCvode;
13 class Daspk;
14 class TQueue;
15 typedef std::vector<PreSyn*> PreSynList;
16 struct BAMech;
17 struct NrnThread;
18 class PlayRecord;
19 class STEList;
20 namespace neuron {
21 struct model_sorted_token;
22 }
23 
24 /**
25  * @brief Wrapper for Memb_list in CVode related code.
26  *
27  * This gets used in two ways:
28  * - with ml.size() == 1 and ml[0].nodecount > 1 when the mechanism instances to be processed are
29  * contiguous
30  * - with ml.size() >= 1 and ml[i].nodecount == 1 when non-contiguous instances need to be processed
31  *
32  * generic configurations with ml.size() and ml[i].nodecount both larger than one are only
33  * supported for the local variable time step method.
34  */
35 struct CvMembList {
37  : index{type} {
38  ml.emplace_back(type);
39  }
41  std::vector<Memb_list> ml{};
42  int index{};
43 };
44 
45 struct BAMechList {
46  BAMechList(BAMechList** first);
49  std::vector<Memb_list*> ml;
50  static void destruct(BAMechList** first);
51 };
52 
53 #define CTD(i) ctd_[((nctd_ > 1) ? (i) : 0)]
55  public:
57  virtual ~CvodeThreadData();
59 
60  std::vector<int> no_cap_indices_;
61  std::vector<int> no_cap_child_indices_;
64  CvMembList* cmlext_; // used only by daspk
65  CvMembList* no_cap_memb_; // used only by cvode, point processes in the no cap nodes
69 
70  // Analogous to NrnThread ncell and end to allow similar SoA container
71  // indexing as in fixed step triang and bksub in nrnoc/solve.cpp.
72  // The v, rhs, d, a, b, parent storage containers are assumed to be
73  // sorted for cell contiguity (when lvardt is used) though roots are
74  // all at the beginning of each thread. The following 4 indices are
75  // used to directly access regions of those containers for tree setup
76  // and solving. This eliminates the use of low performance
77  // Node** v_node_, v_parent_ to access the containers.
82 
83  PreSynList* psl_th_; // with a threshold
85  // since scatter/gather are hot loops, don't want to use data_handle
86  // std::vector<neuron::container::data_handle<double>> pv_, pvdot_;
87  std::vector<double*> pv_, pvdot_;
88  int nvoffset_; // beginning of this threads states
89  int nvsize_; // total number of states for this thread
90  int neq_v_; // for daspk, number of voltage states for this thread
91  int nonvint_offset_; // synonym for neq_v_. Beginning of this threads nonvint variables.
92  int nonvint_extra_offset_; // extra states (probably Python). Not scattered or gathered.
93  std::vector<PlayRecord*>* play_;
94  std::vector<PlayRecord*>* record_;
95 };
96 
97 class Cvode {
98  public:
99  Cvode(NetCvode*);
100  Cvode();
101  virtual ~Cvode();
102 
103  virtual int handle_step(neuron::model_sorted_token const&, NetCvode*, double);
104  virtual int init(double t);
105  virtual int advance_tn(neuron::model_sorted_token const&);
106  virtual int interpolate(double t);
107  virtual double tn() {
108  return tn_;
109  } // furthest time of advance
110  virtual double t0() {
111  return t0_;
112  } // beginning of last real step
113  void init_prepare();
114 
115  int solve(); // checks event_flag and init or advance_tn
116  void statistics();
117  double gam();
118  double time() const {
119  return t_;
120  }
121  void free_cvodemem();
122  int order();
123  void maxorder(int), minstep(double), maxstep(double);
124 
125  public:
126  double tn_, t0_, t_;
128  bool can_retreat_; // only true after an integration step
129  // statistics
130  void stat_init();
133 
134  private:
135  void alloc_cvode();
136  void alloc_daspk();
137  int cvode_init(double);
139  int cvode_interpolate(double);
140  int daspk_init(double);
141  int daspk_advance_tn();
142  int daspk_interpolate(double);
143 
144  public:
145  N_Vector nvnew(long);
146  int setup(N_Vector ypred, N_Vector fpred);
147  int solvex_thread(neuron::model_sorted_token const&, double* b, double* y, NrnThread* nt);
148  int solvex_thread_part1(double* b, NrnThread* nt);
150  int solvex_thread_part3(double* b, NrnThread* nt);
152  double t,
153  double* y,
154  double* ydot,
155  NrnThread* nt);
157  double t,
158  double* y,
159  NrnThread* nt);
160  void fun_thread_transfer_part2(neuron::model_sorted_token const&, double* ydot, NrnThread* nt);
161  void fun_thread_ms_part1(double t, double* y, NrnThread* nt);
162  void fun_thread_ms_part2(NrnThread* nt);
163  void fun_thread_ms_part3(NrnThread* nt);
164  void fun_thread_ms_part4(double* ydot, NrnThread* nt);
165  void fun_thread_ms_part34(double* ydot, NrnThread* nt);
166  bool at_time(double, NrnThread*);
167  void set_init_flag();
168  void check_deliver(NrnThread* nt = 0);
169  void evaluate_conditions(NrnThread* nt = 0);
170  void ste_check();
171  void states(double*);
172  void dstates(double*);
173  void error_weights(double*);
174  void acor(double*);
175  void fill(Cvode* standard);
176  // following 7 crucial for local time step recording, also used by global
177  void delete_prl();
178  void record_add(PlayRecord*);
179  void record_continuous();
181  void play_add(PlayRecord*);
182  void play_continuous(double t);
183  void play_continuous_thread(double t, NrnThread*);
185  void do_nonode(neuron::model_sorted_token const&, NrnThread* nt = 0);
186  double* n_vector_data(N_Vector, int);
187 
188  private:
189  void cvode_constructor();
190  bool init_global();
191  void init_eqn();
192  void daspk_init_eqn();
193  void matmeth();
195  void nocap_v_part1(NrnThread*);
196  void nocap_v_part2(NrnThread*);
197  void nocap_v_part3(NrnThread*);
199  void atolvec_alloc(int);
200  double h();
201  N_Vector ewtvec();
202  N_Vector acorvec();
205 
206  public:
207  // daspk
210  int res(double, double*, double*, double*, NrnThread*);
211  int psol(double, double*, double*, double, NrnThread*);
212  void daspk_scatter_y(N_Vector); // daspk solves vi,vx instead of vm,vx
213  void daspk_gather_y(N_Vector);
214  void daspk_scatter_y(double*, int);
215  void daspk_gather_y(double*, int);
216  void scatter_y(neuron::model_sorted_token const&, double*, int);
217  void gather_y(N_Vector);
218  void gather_y(double*, int);
219  void scatter_ydot(double*, int);
220  void gather_ydot(N_Vector);
221  void gather_ydot(double*, int);
222 
223  public:
224  void activate_maxstate(bool);
225  void maxstate(double*);
226  void maxstate(bool, NrnThread* nt = 0);
227  void maxacor(double*);
228 
229  public:
230  void* mem_;
231  N_Vector y_;
232  N_Vector atolnvec_;
233  N_Vector maxstate_;
234  N_Vector maxacor_;
235 
236  public:
238 
239  public:
241  NrnThread* nth_; // for lvardt
242  int nctd_;
243  long int* nthsizes_; // N_Vector_NrnThread uses this copy of ctd_[i].nvsize_
245  int neq_;
248  double tstop_;
250 
251  private:
256  void triang(NrnThread*);
257  void bksub(NrnThread*);
258 
259  private:
260  // segregation of old vectorized information to per cell info
261  friend class NetCvode;
262  bool is_owner(neuron::container::data_handle<double> const&); // for play and record in
263  // local step context.
264  bool local_;
265  void daspk_setup1_tree_matrix(); // unused
266  void daspk_setup2_tree_matrix(); // unused
268 
269  private:
271 #if NRNMPI
272  public:
273  bool use_partrans_;
274  int global_neq_;
275  int opmode_; // 1 advance, 2 interpolate, 3 init; for testing
276 #endif // NRNMPI
277 };
Definition: cvodeobj.h:97
void fun_thread_transfer_part2(neuron::model_sorted_token const &, double *ydot, NrnThread *nt)
Definition: occvode.cpp:764
int init_calls_
Definition: cvodeobj.h:131
void fun_thread_ms_part2(NrnThread *nt)
Definition: occvode.cpp:823
int neq_
Definition: cvodeobj.h:245
void solvemem(neuron::model_sorted_token const &, NrnThread *)
Definition: occvode.cpp:697
void ste_check()
void free_cvodemem()
Definition: cvodeobj.cpp:1031
void do_nonode(neuron::model_sorted_token const &, NrnThread *nt=0)
Definition: occvode.cpp:1005
double next_at_time_
Definition: cvodeobj.h:247
double t_
Definition: cvodeobj.h:126
int mxb_calls_
Definition: cvodeobj.h:132
long int * nthsizes_
Definition: cvodeobj.h:243
void daspk_setup2_tree_matrix()
void maxstep(double)
Definition: cvodeobj.cpp:1019
double tstop_begin_
Definition: cvodeobj.h:249
void alloc_daspk()
Definition: cvodeobj.cpp:1107
void rhs(neuron::model_sorted_token const &, NrnThread *)
Definition: cvtrset.cpp:12
void rhs_memb(neuron::model_sorted_token const &, CvMembList *, NrnThread *)
Definition: cvtrset.cpp:66
void cvode_constructor()
Definition: cvodeobj.cpp:701
int solvex_thread_part2(NrnThread *nt)
Definition: occvode.cpp:677
virtual int handle_step(neuron::model_sorted_token const &, NetCvode *, double)
Definition: netcvode.cpp:2136
void fun_thread_transfer_part1(neuron::model_sorted_token const &, double t, double *y, NrnThread *nt)
Definition: occvode.cpp:737
int advance_calls_
Definition: cvodeobj.h:131
void play_continuous_thread(double t, NrnThread *)
Definition: occvode.cpp:1155
void init_prepare()
Definition: cvodeobj.cpp:865
bool at_time(double, NrnThread *)
Definition: cvodeobj.cpp:745
int nctd_
Definition: cvodeobj.h:242
void states(double *)
Definition: occvode.cpp:1033
N_Vector atolnvec_
Definition: cvodeobj.h:232
void * mem_
Definition: cvodeobj.h:230
int solvex_thread(neuron::model_sorted_token const &, double *b, double *y, NrnThread *nt)
Definition: occvode.cpp:588
int cvode_init(double)
Definition: cvodeobj.cpp:1057
int jac_calls_
Definition: cvodeobj.h:132
void dstates(double *)
Definition: occvode.cpp:1044
int psol(double, double *, double *, double, NrnThread *)
Definition: nrndaspk.cpp:604
bool init_global()
Definition: occvode.cpp:74
void triang(NrnThread *)
Definition: cvtrset.cpp:139
double tstop_end_
Definition: cvodeobj.h:249
TQItem * tqitem_
Definition: cvodeobj.h:267
void record_add(PlayRecord *)
Definition: occvode.cpp:1092
CvodeThreadData * ctd_
Definition: cvodeobj.h:240
void play_add(PlayRecord *)
Definition: occvode.cpp:1133
int prior2init_
Definition: cvodeobj.h:270
void daspk_scatter_y(N_Vector)
Definition: nrndaspk.cpp:391
virtual double tn()
Definition: cvodeobj.h:107
void stat_init()
Definition: cvodeobj.cpp:859
virtual ~Cvode()
Definition: cvodeobj.cpp:835
void gather_y(N_Vector)
Definition: occvode.cpp:523
void daspk_init_eqn()
Definition: occvode.cpp:369
int ts_inits_
Definition: cvodeobj.h:132
int setup(N_Vector ypred, N_Vector fpred)
Definition: occvode.cpp:574
void play_continuous(double t)
Definition: occvode.cpp:1141
void record_continuous()
Definition: occvode.cpp:1101
void statistics()
Definition: cvodeobj.cpp:1407
void fun_thread_ms_part4(double *ydot, NrnThread *nt)
Definition: occvode.cpp:834
N_Vector y_
Definition: cvodeobj.h:231
N_Vector maxstate_
Definition: cvodeobj.h:233
Daspk * daspk_
Definition: cvodeobj.h:209
void nocap_v_part2(NrnThread *)
Definition: occvode.cpp:968
void before_after(neuron::model_sorted_token const &, BAMechList *, NrnThread *)
Definition: occvode.cpp:857
void daspk_setup1_tree_matrix()
void new_no_cap_memb(CvodeThreadData &, NrnThread *)
Definition: occvode.cpp:311
void fun_thread_ms_part34(double *ydot, NrnThread *nt)
Definition: occvode.cpp:826
void fun_thread_ms_part1(double t, double *y, NrnThread *nt)
Definition: occvode.cpp:804
void record_continuous_thread(NrnThread *)
Definition: occvode.cpp:1121
int solvex_thread_part3(double *b, NrnThread *nt)
Definition: occvode.cpp:681
void check_deliver(NrnThread *nt=0)
Definition: netcvode.cpp:5499
int daspk_interpolate(double)
void fun_thread(neuron::model_sorted_token const &, double t, double *y, double *ydot, NrnThread *nt)
Definition: occvode.cpp:715
bool structure_change_
Definition: cvodeobj.h:237
void nocap_v_part3(NrnThread *)
Definition: occvode.cpp:971
void evaluate_conditions(NrnThread *nt=0)
Definition: netcvode.cpp:5470
void fill(Cvode *standard)
bool can_retreat_
Definition: cvodeobj.h:128
NetCvode * ncv_
Definition: cvodeobj.h:244
int interpolate_calls_
Definition: cvodeobj.h:131
void fun_thread_ms_part3(NrnThread *nt)
Definition: occvode.cpp:830
void matmeth()
Definition: cvodeobj.cpp:1441
Cvode()
Definition: cvodeobj.cpp:698
void maxstate(double *)
Definition: cvodeobj.cpp:956
double time() const
Definition: cvodeobj.h:118
int order()
Definition: cvodeobj.cpp:984
N_Vector acorvec()
Definition: cvodeobj.cpp:1399
void maxorder(int)
Definition: cvodeobj.cpp:997
int res(double, double *, double *, double *, NrnThread *)
Definition: nrndaspk.cpp:450
double tstop_
Definition: cvodeobj.h:248
NrnThread * nth_
Definition: cvodeobj.h:241
virtual int init(double t)
Definition: cvodeobj.cpp:1189
void acor(double *)
Definition: occvode.cpp:1066
double * n_vector_data(N_Vector, int)
Definition: occvode.cpp:485
virtual int interpolate(double t)
Definition: cvodeobj.cpp:1222
void bksub(NrnThread *)
Definition: cvtrset.cpp:155
bool local_
Definition: cvodeobj.h:264
void init_eqn()
Definition: occvode.cpp:99
int daspk_advance_tn()
Definition: cvodeobj.cpp:1377
int cvode_advance_tn(neuron::model_sorted_token const &)
Definition: cvodeobj.cpp:1299
N_Vector ewtvec()
Definition: cvodeobj.cpp:1391
double h()
Definition: cvodeobj.cpp:737
void nocap_v_part1(NrnThread *)
Definition: occvode.cpp:932
void activate_maxstate(bool)
Definition: cvodeobj.cpp:899
void delete_prl()
Definition: occvode.cpp:1077
void set_init_flag()
Definition: cvodeobj.cpp:780
void do_ode(neuron::model_sorted_token const &, NrnThread &)
Definition: occvode.cpp:985
double gam()
Definition: cvodeobj.cpp:729
void lhs_memb(neuron::model_sorted_token const &, CvMembList *, NrnThread *)
Definition: cvtrset.cpp:119
bool use_daspk_
Definition: cvodeobj.h:208
void lhs(neuron::model_sorted_token const &, NrnThread *)
Definition: cvtrset.cpp:86
bool is_owner(neuron::container::data_handle< double > const &)
Definition: netcvode.cpp:6379
void nocap_v(neuron::model_sorted_token const &, NrnThread *)
Definition: occvode.cpp:887
void daspk_gather_y(N_Vector)
Definition: nrndaspk.cpp:420
void atolvec_alloc(int)
Definition: cvodeobj.cpp:826
int solvex_thread_part1(double *b, NrnThread *nt)
Definition: occvode.cpp:650
void maxacor(double *)
Definition: cvodeobj.cpp:969
void scatter_y(neuron::model_sorted_token const &, double *, int)
Definition: occvode.cpp:498
double t0_
Definition: cvodeobj.h:126
double tn_
Definition: cvodeobj.h:126
virtual double t0()
Definition: cvodeobj.h:110
void scatter_ydot(double *, int)
Definition: occvode.cpp:541
N_Vector nvnew(long)
Definition: cvodeobj.cpp:788
void gather_ydot(N_Vector)
Definition: occvode.cpp:554
void alloc_cvode()
Definition: cvodeobj.cpp:982
int solve()
Definition: cvodeobj.cpp:1168
int cvode_interpolate(double)
Definition: cvodeobj.cpp:1341
int daspk_init(double)
Definition: cvodeobj.cpp:1103
int f_calls_
Definition: cvodeobj.h:132
N_Vector maxacor_
Definition: cvodeobj.h:234
int event_flag_
Definition: cvodeobj.h:246
bool initialize_
Definition: cvodeobj.h:127
void error_weights(double *)
Definition: occvode.cpp:1055
void minstep(double)
Definition: cvodeobj.cpp:1008
virtual int advance_tn(neuron::model_sorted_token const &)
Definition: cvodeobj.cpp:1117
virtual ~CvodeThreadData()
Definition: netcvode.cpp:1250
int nonvint_extra_offset_
Definition: cvodeobj.h:92
int nonvint_offset_
Definition: cvodeobj.h:91
PreSynList * psl_th_
Definition: cvodeobj.h:83
int vnode_begin_index_
Definition: cvodeobj.h:80
std::vector< double * > pv_
Definition: cvodeobj.h:87
int vnode_end_index_
Definition: cvodeobj.h:81
CvMembList * cv_memb_list_
Definition: cvodeobj.h:62
void delete_memb_list(CvMembList *)
Definition: netcvode.cpp:1326
std::vector< int > no_cap_indices_
Definition: cvodeobj.h:60
CvMembList * no_cap_memb_
Definition: cvodeobj.h:65
BAMechList * before_breakpoint_
Definition: cvodeobj.h:66
CvMembList * cmlext_
Definition: cvodeobj.h:64
BAMechList * after_solve_
Definition: cvodeobj.h:67
int rootnode_begin_index_
Definition: cvodeobj.h:78
std::vector< double * > pvdot_
Definition: cvodeobj.h:87
CvMembList * cmlcap_
Definition: cvodeobj.h:63
std::vector< PlayRecord * > * record_
Definition: cvodeobj.h:94
int rootnode_end_index_
Definition: cvodeobj.h:79
HTList * watch_list_
Definition: cvodeobj.h:84
BAMechList * before_step_
Definition: cvodeobj.h:68
std::vector< PlayRecord * > * play_
Definition: cvodeobj.h:93
std::vector< int > no_cap_child_indices_
Definition: cvodeobj.h:61
Definition: nrndaspk.h:10
Definition: htlist.h:34
std::vector< PreSyn * > PreSynList
Definition: cvodeobj.h:14
In mechanism libraries, cannot use auto const token = nrn_ensure_model_data_are_sorted(); because the...
Definition: tnode.hpp:17
short type
Definition: cabvars.h:10
BAMechList * next
Definition: cvodeobj.h:47
BAMech * bam
Definition: cvodeobj.h:48
std::vector< Memb_list * > ml
Definition: cvodeobj.h:49
static void destruct(BAMechList **first)
Definition: netcvode.cpp:1224
BAMechList(BAMechList **first)
Definition: netcvode.cpp:1212
Wrapper for Memb_list in CVode related code.
Definition: cvodeobj.h:35
std::vector< Memb_list > ml
Definition: cvodeobj.h:41
int index
Definition: cvodeobj.h:42
CvMembList * next
Definition: cvodeobj.h:40
CvMembList(int type)
Definition: cvodeobj.h:36
Represent main neuron object computed by single thread.
Definition: multicore.h:58
Definition: tqitem.hpp:3