From 8a7bb609459cf865cf462594f58a4772136bdb1c Mon Sep 17 00:00:00 2001 From: Ioannis Magkanaris Date: Thu, 24 Feb 2022 18:59:15 +0100 Subject: [PATCH 1/5] Read twice the global data to fill the global variables of coreneuron and the mod files --- coreneuron/apps/main1.cpp | 49 +++++++++++++++++++++++++++-------- coreneuron/io/global_vars.cpp | 1 + coreneuron/mechanism/eion.cpp | 3 +++ 3 files changed, 42 insertions(+), 11 deletions(-) diff --git a/coreneuron/apps/main1.cpp b/coreneuron/apps/main1.cpp index 98e9ed79f..ef02f96f9 100644 --- a/coreneuron/apps/main1.cpp +++ b/coreneuron/apps/main1.cpp @@ -185,13 +185,6 @@ void nrn_init_and_load_data(int argc, report_mem_usage("After MPI_Init"); } - // initialise default coreneuron parameters - initnrn(); - - // set global variables - // precedence is: set by user, globals.dat, 34.0 - celsius = corenrn_param.celsius; - #if CORENEURON_ENABLE_GPU if (!corenrn_param.gpu && corenrn_param.cell_interleave_permute == 2) { fprintf(stderr, @@ -218,9 +211,6 @@ void nrn_init_and_load_data(int argc, // full path of files.dat file std::string filesdat(corenrn_param.datpath + "/" + corenrn_param.filesdat); - // read the global variable names and set their values from globals.dat - set_globals(corenrn_param.datpath.c_str(), (corenrn_param.seed >= 0), corenrn_param.seed); - // set global variables for start time, timestep and temperature if (!corenrn_embedded) { t = checkPoints.restore_time(); @@ -236,6 +226,8 @@ void nrn_init_and_load_data(int argc, rev_dt = (int) (1. / dt); + printf("CoreNEURON Global Vars before editing: second order=%d t=%g dt=%g rev_dt=%d celsius=%lf\n", secondorder, t, dt, rev_dt, celsius); + if (corenrn_param.celsius != -1000.) { // command line arg highest precedence celsius = corenrn_param.celsius; } else if (celsius == -1000.) { // not on command line and no celsius in globals.dat @@ -247,6 +239,8 @@ void nrn_init_and_load_data(int argc, // for ispc backend ispc_celsius = celsius; + printf("CoreNEURON Global Vars after editing: second order=%d t=%g dt=%g rev_dt=%d celsius=%lf\n", secondorder, t, dt, rev_dt, celsius); + // create net_cvode instance mk_netcvode(); @@ -514,9 +508,36 @@ extern "C" void mk_mech_init(int argc, char** argv) { out.close(); } - // reads mechanism information from bbcore_mech.dat + // Not working + // initialise default coreneuron parameters + initnrn(); + printf("CoreNEURON Global Vars after initnrn(): second order=%d t=%g dt=%g rev_dt=%d celsius=%lf\n", secondorder, t, dt, rev_dt, celsius); + // set global variables + // precedence is: set by user, globals.dat, 34.0 + celsius = corenrn_param.celsius; + printf("CoreNEURON Global Vars after corenrn_param: second order=%d t=%g dt=%g rev_dt=%d celsius=%lf\n", secondorder, t, dt, rev_dt, celsius); + + // read the global variable names and set their values from globals.dat + set_globals(corenrn_param.datpath.c_str(), (corenrn_param.seed >= 0), corenrn_param.seed); + printf("CoreNEURON Global Vars after set_globals(): second order=%d t=%g dt=%g rev_dt=%d celsius=%lf\n", secondorder, t, dt, rev_dt, celsius); + + // reads mechanism information from bbcore_mech.dat mk_mech((corenrn_param.datpath).c_str()); + + // // Not working + // // initialise default coreneuron parameters + // initnrn(); + // printf("CoreNEURON Global Vars after initnrn(): second order=%d t=%g dt=%g rev_dt=%d celsius=%lf\n", secondorder, t, dt, rev_dt, celsius); + + // // set global variables + // // precedence is: set by user, globals.dat, 34.0 + // celsius = corenrn_param.celsius; + // printf("CoreNEURON Global Vars after corenrn_param: second order=%d t=%g dt=%g rev_dt=%d celsius=%lf\n", secondorder, t, dt, rev_dt, celsius); + + // // read the global variable names and set their values from globals.dat + // set_globals(corenrn_param.datpath.c_str(), (corenrn_param.seed >= 0), corenrn_param.seed); + // printf("CoreNEURON Global Vars after set_globals(): second order=%d t=%g dt=%g rev_dt=%d celsius=%lf\n", secondorder, t, dt, rev_dt, celsius); } extern "C" int run_solve_core(int argc, char** argv) { @@ -527,6 +548,12 @@ extern "C" int run_solve_core(int argc, char** argv) { std::vector> spikes_population_name_offset; bool reports_needs_finalize = false; + + // read the global variable names and set their values from globals.dat + set_globals(corenrn_param.datpath.c_str(), (corenrn_param.seed >= 0), corenrn_param.seed); + printf("CoreNEURON Global Vars after set_globals(): second order=%d t=%g dt=%g rev_dt=%d celsius=%lf\n", secondorder, t, dt, rev_dt, celsius); + + if (!corenrn_param.is_quiet()) { report_mem_usage("After mk_mech"); } diff --git a/coreneuron/io/global_vars.cpp b/coreneuron/io/global_vars.cpp index 128a1cdb9..bae73fca8 100644 --- a/coreneuron/io/global_vars.cpp +++ b/coreneuron/io/global_vars.cpp @@ -62,6 +62,7 @@ void set_globals(const char* path, bool cli_global_seed, int cli_global_seed_val // will be processed before exit from loop. if (val) { N2V::iterator it = n2v->find(name); + printf("Passing from NEURON %s %d %g\n", name, size, *val); if (it != n2v->end()) { if (size == 0) { nrn_assert(it->second.first == 0); diff --git a/coreneuron/mechanism/eion.cpp b/coreneuron/mechanism/eion.cpp index 8bb167cf7..9aa95b5bd 100644 --- a/coreneuron/mechanism/eion.cpp +++ b/coreneuron/mechanism/eion.cpp @@ -109,16 +109,19 @@ void ion_reg(const char* name, double valence) { sprintf(buf[1], "%so0_%s", name, buf[0]); if (strcmp("na", name) == 0) { na_ion = mechtype; + printf("Setting global_conci(na) to %lf and global_conco(na) to %lf\n", DEF_nai, DEF_nao); global_conci(mechtype) = DEF_nai; global_conco(mechtype) = DEF_nao; global_charge(mechtype) = 1.; } else if (strcmp("k", name) == 0) { k_ion = mechtype; + printf("Setting global_conci(k) to %lf and global_conco(k) to %lf\n",DEF_ki, DEF_ko); global_conci(mechtype) = DEF_ki; global_conco(mechtype) = DEF_ko; global_charge(mechtype) = 1.; } else if (strcmp("ca", name) == 0) { ca_ion = mechtype; + printf("Setting global_conci(ca) to %lf and global_conco(ca) to %lf\n", DEF_cai, DEF_cao); global_conci(mechtype) = DEF_cai; global_conco(mechtype) = DEF_cao; global_charge(mechtype) = 2.; From 8c9a8db4cfde52da9ac54ec100d464f6cc3cc2c4 Mon Sep 17 00:00:00 2001 From: Ioannis Magkanaris Date: Thu, 24 Feb 2022 19:02:55 +0100 Subject: [PATCH 2/5] Added changes in eion.cpp and global_vars.cpp --- coreneuron/io/global_vars.cpp | 6 +++++- coreneuron/mechanism/eion.cpp | 36 ++++++++++++++++++++++++----------- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/coreneuron/io/global_vars.cpp b/coreneuron/io/global_vars.cpp index bae73fca8..7b40b8766 100644 --- a/coreneuron/io/global_vars.cpp +++ b/coreneuron/io/global_vars.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include "coreneuron/utils/randoms/nrnran123.h" #include "coreneuron/nrnconf.h" @@ -28,6 +29,8 @@ using N2V = std::map; static N2V* n2v; +extern std::unordered_map nrn_ion_init; + void hoc_register_var(DoubScal* ds, DoubVec* dv, VoidFunc*) { if (!n2v) { n2v = new N2V(); @@ -62,7 +65,6 @@ void set_globals(const char* path, bool cli_global_seed, int cli_global_seed_val // will be processed before exit from loop. if (val) { N2V::iterator it = n2v->find(name); - printf("Passing from NEURON %s %d %g\n", name, size, *val); if (it != n2v->end()) { if (size == 0) { nrn_assert(it->second.first == 0); @@ -114,6 +116,8 @@ void set_globals(const char* path, bool cli_global_seed, int cli_global_seed_val if (it != n2v->end()) { nrn_assert(it->second.first == 0); *(it->second.second) = val; + } else if (static_cast(name).find("ion") != std::string::npos) { + nrn_ion_init[name] = val; } } else if (sscanf(line, "%[^[][%d]\n", name, &n) == 2) { if (strcmp(name, "0") == 0) { diff --git a/coreneuron/mechanism/eion.cpp b/coreneuron/mechanism/eion.cpp index 9aa95b5bd..254223268 100644 --- a/coreneuron/mechanism/eion.cpp +++ b/coreneuron/mechanism/eion.cpp @@ -60,6 +60,15 @@ double nrn_ion_charge(int type) { return global_charge(type); } +std::unordered_map nrn_ion_init = { + {"nai0_na_ion", DEF_nai}, + {"nao0_na_ion", DEF_nao}, + {"ki0_k_ion", DEF_ki}, + {"ko0_k_ion", DEF_ko}, + {"cai0_ca_ion", DEF_cai}, + {"cao0_ca_ion", DEF_cao}, +}; + void ion_reg(const char* name, double valence) { char buf[7][50]; #define VAL_SENTINAL -10000. @@ -109,25 +118,30 @@ void ion_reg(const char* name, double valence) { sprintf(buf[1], "%so0_%s", name, buf[0]); if (strcmp("na", name) == 0) { na_ion = mechtype; - printf("Setting global_conci(na) to %lf and global_conco(na) to %lf\n", DEF_nai, DEF_nao); - global_conci(mechtype) = DEF_nai; - global_conco(mechtype) = DEF_nao; + global_conci(mechtype) = nrn_ion_init["nai0_na_ion"]; + global_conco(mechtype) = nrn_ion_init["nao0_na_ion"]; global_charge(mechtype) = 1.; } else if (strcmp("k", name) == 0) { k_ion = mechtype; - printf("Setting global_conci(k) to %lf and global_conco(k) to %lf\n",DEF_ki, DEF_ko); - global_conci(mechtype) = DEF_ki; - global_conco(mechtype) = DEF_ko; + global_conci(mechtype) = nrn_ion_init["ki0_k_ion"]; + global_conco(mechtype) = nrn_ion_init["ko0_k_ion"]; global_charge(mechtype) = 1.; } else if (strcmp("ca", name) == 0) { ca_ion = mechtype; - printf("Setting global_conci(ca) to %lf and global_conco(ca) to %lf\n", DEF_cai, DEF_cao); - global_conci(mechtype) = DEF_cai; - global_conco(mechtype) = DEF_cao; + global_conci(mechtype) = nrn_ion_init["cai0_ca_ion"]; + global_conco(mechtype) = nrn_ion_init["cao0_ca_ion"]; global_charge(mechtype) = 2.; } else { - global_conci(mechtype) = DEF_ioni; - global_conco(mechtype) = DEF_iono; + if (nrn_ion_init[static_cast(buf[0])]) { + global_conci(mechtype) = nrn_ion_init[static_cast(buf[0])]; + } else { + global_conci(mechtype) = DEF_ioni; + } + if (nrn_ion_init[static_cast(buf[1])]) { + global_conco(mechtype) = nrn_ion_init[static_cast(buf[1])]; + } else { + global_conco(mechtype) = DEF_iono; + } global_charge(mechtype) = VAL_SENTINAL; } } From c3ec12daa78b17bb007a5acb68db5079b12e58ae Mon Sep 17 00:00:00 2001 From: Ioannis Magkanaris Date: Fri, 25 Feb 2022 12:35:06 +0100 Subject: [PATCH 3/5] Fixed initialization of global variables --- coreneuron/apps/main1.cpp | 24 ++++-------------------- coreneuron/io/global_vars.cpp | 6 +++++- 2 files changed, 9 insertions(+), 21 deletions(-) diff --git a/coreneuron/apps/main1.cpp b/coreneuron/apps/main1.cpp index ef02f96f9..9e3f0e348 100644 --- a/coreneuron/apps/main1.cpp +++ b/coreneuron/apps/main1.cpp @@ -508,36 +508,21 @@ extern "C" void mk_mech_init(int argc, char** argv) { out.close(); } - // Not working // initialise default coreneuron parameters initnrn(); - printf("CoreNEURON Global Vars after initnrn(): second order=%d t=%g dt=%g rev_dt=%d celsius=%lf\n", secondorder, t, dt, rev_dt, celsius); // set global variables // precedence is: set by user, globals.dat, 34.0 celsius = corenrn_param.celsius; - printf("CoreNEURON Global Vars after corenrn_param: second order=%d t=%g dt=%g rev_dt=%d celsius=%lf\n", secondorder, t, dt, rev_dt, celsius); // read the global variable names and set their values from globals.dat + // some global variables need to be read before the ion_reg to initialize + // properly the in-built ion mechanisms set_globals(corenrn_param.datpath.c_str(), (corenrn_param.seed >= 0), corenrn_param.seed); - printf("CoreNEURON Global Vars after set_globals(): second order=%d t=%g dt=%g rev_dt=%d celsius=%lf\n", secondorder, t, dt, rev_dt, celsius); // reads mechanism information from bbcore_mech.dat mk_mech((corenrn_param.datpath).c_str()); - // // Not working - // // initialise default coreneuron parameters - // initnrn(); - // printf("CoreNEURON Global Vars after initnrn(): second order=%d t=%g dt=%g rev_dt=%d celsius=%lf\n", secondorder, t, dt, rev_dt, celsius); - - // // set global variables - // // precedence is: set by user, globals.dat, 34.0 - // celsius = corenrn_param.celsius; - // printf("CoreNEURON Global Vars after corenrn_param: second order=%d t=%g dt=%g rev_dt=%d celsius=%lf\n", secondorder, t, dt, rev_dt, celsius); - - // // read the global variable names and set their values from globals.dat - // set_globals(corenrn_param.datpath.c_str(), (corenrn_param.seed >= 0), corenrn_param.seed); - // printf("CoreNEURON Global Vars after set_globals(): second order=%d t=%g dt=%g rev_dt=%d celsius=%lf\n", secondorder, t, dt, rev_dt, celsius); } extern "C" int run_solve_core(int argc, char** argv) { @@ -549,10 +534,9 @@ extern "C" int run_solve_core(int argc, char** argv) { bool reports_needs_finalize = false; - // read the global variable names and set their values from globals.dat + // read agin the global variables to set the global variables defined by + // the mod files' mechanisms set_globals(corenrn_param.datpath.c_str(), (corenrn_param.seed >= 0), corenrn_param.seed); - printf("CoreNEURON Global Vars after set_globals(): second order=%d t=%g dt=%g rev_dt=%d celsius=%lf\n", secondorder, t, dt, rev_dt, celsius); - if (!corenrn_param.is_quiet()) { report_mem_usage("After mk_mech"); diff --git a/coreneuron/io/global_vars.cpp b/coreneuron/io/global_vars.cpp index 7b40b8766..3976d6831 100644 --- a/coreneuron/io/global_vars.cpp +++ b/coreneuron/io/global_vars.cpp @@ -77,6 +77,9 @@ void set_globals(const char* path, bool cli_global_seed, int cli_global_seed_val } } } + if (static_cast(name).find("ion") != std::string::npos) { + nrn_ion_init[name] = *val; + } delete[] val; val = nullptr; } @@ -116,7 +119,8 @@ void set_globals(const char* path, bool cli_global_seed, int cli_global_seed_val if (it != n2v->end()) { nrn_assert(it->second.first == 0); *(it->second.second) = val; - } else if (static_cast(name).find("ion") != std::string::npos) { + } + if (static_cast(name).find("ion") != std::string::npos) { nrn_ion_init[name] = val; } } else if (sscanf(line, "%[^[][%d]\n", name, &n) == 2) { From a6d9da0d795100425ba992465d944d64d02ec8bf Mon Sep 17 00:00:00 2001 From: Ioannis Magkanaris Date: Fri, 25 Feb 2022 12:36:34 +0100 Subject: [PATCH 4/5] Small cleanup --- coreneuron/apps/main1.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/coreneuron/apps/main1.cpp b/coreneuron/apps/main1.cpp index 9e3f0e348..dba43a7e3 100644 --- a/coreneuron/apps/main1.cpp +++ b/coreneuron/apps/main1.cpp @@ -226,8 +226,6 @@ void nrn_init_and_load_data(int argc, rev_dt = (int) (1. / dt); - printf("CoreNEURON Global Vars before editing: second order=%d t=%g dt=%g rev_dt=%d celsius=%lf\n", secondorder, t, dt, rev_dt, celsius); - if (corenrn_param.celsius != -1000.) { // command line arg highest precedence celsius = corenrn_param.celsius; } else if (celsius == -1000.) { // not on command line and no celsius in globals.dat @@ -239,8 +237,6 @@ void nrn_init_and_load_data(int argc, // for ispc backend ispc_celsius = celsius; - printf("CoreNEURON Global Vars after editing: second order=%d t=%g dt=%g rev_dt=%d celsius=%lf\n", secondorder, t, dt, rev_dt, celsius); - // create net_cvode instance mk_netcvode(); From 36309e551474dbf56e530e7af57992b93165cb75 Mon Sep 17 00:00:00 2001 From: Ioannis Magkanaris Date: Fri, 25 Feb 2022 12:44:36 +0100 Subject: [PATCH 5/5] Make clang-format happy --- coreneuron/apps/main1.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/coreneuron/apps/main1.cpp b/coreneuron/apps/main1.cpp index dba43a7e3..9d362cb91 100644 --- a/coreneuron/apps/main1.cpp +++ b/coreneuron/apps/main1.cpp @@ -515,10 +515,9 @@ extern "C" void mk_mech_init(int argc, char** argv) { // some global variables need to be read before the ion_reg to initialize // properly the in-built ion mechanisms set_globals(corenrn_param.datpath.c_str(), (corenrn_param.seed >= 0), corenrn_param.seed); - + // reads mechanism information from bbcore_mech.dat mk_mech((corenrn_param.datpath).c_str()); - } extern "C" int run_solve_core(int argc, char** argv) {