@@ -1094,10 +1094,18 @@ int ECS_Grid_node::add_multicompartment_reaction(int nstates, int* indices, int
10941094void ECS_Grid_node::clear_multicompartment_reaction () {
10951095 free (all_reaction_states);
10961096 free (react_offsets);
1097- if (multicompartment_inititalized)
1097+ if (multicompartment_inititalized) {
10981098 free (all_reaction_indices);
1099- else
1099+ #if NRNMPI
1100+ if (nrnmpi_use)
1101+ free (reaction_indices);
1102+ #endif
1103+ } else {
11001104 free (reaction_indices);
1105+ }
1106+ free (induced_currents);
1107+ induced_currents = NULL ;
1108+ local_induced_currents = NULL ;
11011109 all_reaction_indices = NULL ;
11021110 all_reaction_states = NULL ;
11031111 reaction_indices = NULL ;
@@ -1126,12 +1134,11 @@ void ECS_Grid_node::initialize_multicompartment_reaction() {
11261134 break ;
11271135
11281136 if (i != nrnmpi_numprocs) {
1137+ total_reaction_states = 0 ;
11291138 // number of offsets (Reaction) stored in each process
1130- proc_num_reactions = (int *) calloc (nrnmpi_numprocs, sizeof (int ));
11311139 proc_num_reactions[nrnmpi_myid] = react_offset_count;
11321140
11331141 // number of states/indices stored in each process
1134- proc_num_reaction_states = (int *) calloc (nrnmpi_numprocs, sizeof (int ));
11351142 proc_num_reaction_states[nrnmpi_myid] = react_offsets[react_offset_count - 1 ];
11361143 nrnmpi_int_allgather_inplace (proc_num_reactions, 1 );
11371144 nrnmpi_int_allgather_inplace (proc_num_reaction_states, 1 );
@@ -1142,35 +1149,31 @@ void ECS_Grid_node::initialize_multicompartment_reaction() {
11421149 proc_num_reactions[i] = total_reaction_states;
11431150 total_reaction_states += proc_num_reaction_states[i];
11441151 }
1145-
1146- // Move the offsets for each reaction so they reference the
1147- // corresponding indices in the all_reaction_indices array
1148- for (j = 0 ; j < react_offset_count; j++)
1149- react_offsets[j] += start_state;
1152+ free (all_reaction_indices);
1153+ free (all_reaction_states);
11501154
11511155 all_reaction_indices = (int *) malloc (total_reaction_states * sizeof (int ));
11521156 all_reaction_states = (double *) calloc (total_reaction_states, sizeof (double ));
1153-
11541157 memcpy (&all_reaction_indices[start_state],
11551158 reaction_indices,
11561159 proc_num_reaction_states[nrnmpi_myid] * sizeof (int ));
11571160 nrnmpi_int_allgatherv_inplace (all_reaction_indices,
11581161 proc_num_reaction_states,
11591162 proc_num_reactions);
1160- free (reaction_indices);
1161- reaction_indices = NULL ;
1163+
11621164 multicompartment_inititalized = TRUE ;
11631165
11641166 // Handle currents induced by multicompartment reactions.
1167+ int local_induced_current_count = induced_current_count;
11651168 proc_induced_current_count[nrnmpi_myid] = induced_current_count;
11661169 nrnmpi_int_allgather_inplace (proc_induced_current_count, 1 );
1170+
11671171 proc_induced_current_offset[0 ] = 0 ;
11681172 for (i = 1 ; i < nrnmpi_numprocs; i++)
11691173 proc_induced_current_offset[i] = proc_induced_current_offset[i - 1 ] +
11701174 proc_induced_current_count[i - 1 ];
11711175 induced_current_count = proc_induced_current_offset[nrnmpi_numprocs - 1 ] +
11721176 proc_induced_current_count[nrnmpi_numprocs - 1 ];
1173-
11741177 all_scales = (double *) malloc (induced_current_count * sizeof (double ));
11751178 all_indices = (int *) malloc (induced_current_count * sizeof (int ));
11761179 memcpy (&all_scales[proc_induced_current_offset[nrnmpi_myid]],
@@ -1193,16 +1196,18 @@ void ECS_Grid_node::initialize_multicompartment_reaction() {
11931196 free (induced_currents);
11941197 induced_currents_scale = all_scales;
11951198 induced_currents_index = all_indices;
1196- induced_currents = (double *) malloc (induced_current_count * sizeof (double ));
1199+ induced_currents = (double *) calloc (induced_current_count, sizeof (double ));
11971200 local_induced_currents = &induced_currents[proc_induced_current_offset[nrnmpi_myid]];
1201+ // set to local count to avoid accumulation with repeated calls
1202+ induced_current_count = local_induced_current_count;
11981203 }
11991204 } else {
12001205 if (!multicompartment_inititalized) {
12011206 total_reaction_states = react_offsets[react_offset_count - 1 ];
12021207 all_reaction_indices = reaction_indices;
12031208 all_reaction_states = (double *) calloc (total_reaction_states, sizeof (double ));
12041209 multicompartment_inititalized = TRUE ;
1205- induced_currents = (double *) malloc (induced_current_count * sizeof (double ));
1210+ induced_currents = (double *) calloc (induced_current_count, sizeof (double ));
12061211 local_induced_currents = induced_currents;
12071212 }
12081213 }
@@ -1212,7 +1217,7 @@ void ECS_Grid_node::initialize_multicompartment_reaction() {
12121217 all_reaction_indices = reaction_indices;
12131218 all_reaction_states = (double *) calloc (total_reaction_states, sizeof (double ));
12141219 multicompartment_inititalized = TRUE ;
1215- induced_currents = (double *) malloc (induced_current_count * sizeof (double ));
1220+ induced_currents = (double *) calloc (induced_current_count, sizeof (double ));
12161221 local_induced_currents = induced_currents;
12171222 }
12181223#endif
@@ -1237,7 +1242,7 @@ void ECS_Grid_node::do_multicompartment_reactions(double* result) {
12371242 for (i = 0 ; i < total_reaction_states; i++)
12381243 result[all_reaction_indices[i]] += all_reaction_states[i];
12391244 }
1240- memset (all_reaction_states, 0 , total_reaction_states * sizeof (int ));
1245+ memset (all_reaction_states, 0 , total_reaction_states * sizeof (double ));
12411246}
12421247
12431248// TODO: Implement this
@@ -1261,12 +1266,18 @@ ECS_Grid_node::~ECS_Grid_node() {
12611266 free (proc_num_fluxes);
12621267 free (proc_num_reaction_states);
12631268 free (proc_num_reactions);
1269+ free (reaction_indices);
12641270 }
12651271#endif
1272+ free (all_reaction_indices);
1273+ reaction_indices = nullptr ;
1274+ free (react_offsets);
12661275 free (all_currents);
12671276 free (ecs_adi_dir_x);
12681277 free (ecs_adi_dir_y);
12691278 free (ecs_adi_dir_z);
1279+ if (get_alpha == get_alpha_scalar)
1280+ free (alpha);
12701281 if (node_flux_count > 0 ) {
12711282 free (node_flux_idx);
12721283 free (node_flux_scale);
0 commit comments