Skip to content

Commit

Permalink
Refactor slist and dlist according to NEURON (#1110)
Browse files Browse the repository at this point in the history
* Added test for `_initlists()`
  • Loading branch information
iomaganaris authored Dec 7, 2023
1 parent 7265ec9 commit 85b0dfc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
13 changes: 10 additions & 3 deletions src/codegen/codegen_neuron_cpp_visitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,11 @@ void CodegenNeuronCppVisitor::print_neuron_includes() {
}


void CodegenNeuronCppVisitor::print_sdlists_init(bool print_initializers) {
void CodegenNeuronCppVisitor::print_sdlists_init([[maybe_unused]] bool print_initializers) {
/// _initlists() should only be called once by the mechanism registration function
/// (_<mod_file>_reg())
printer->add_newline(2);
printer->push_block("static void _initlists()");
for (auto i = 0; i < info.prime_variables_by_order.size(); ++i) {
const auto& prime_var = info.prime_variables_by_order[i];
/// TODO: Something similar needs to happen for slist/dlist2 but I don't know their usage at
Expand Down Expand Up @@ -319,6 +323,7 @@ void CodegenNeuronCppVisitor::print_sdlists_init(bool print_initializers) {
position_of_float_var(prime_var_deriv_name));
}
}
printer->pop_block();
}


Expand Down Expand Up @@ -396,7 +401,7 @@ void CodegenNeuronCppVisitor::print_mechanism_register() {
printer->add_newline(2);
printer->add_line("/** register channel with the simulator */");
printer->fmt_push_block("extern \"C\" void _{}_reg()", info.mod_file);
print_sdlists_init(true);
printer->add_line("_initlists();");
printer->add_newline();

const auto compute_functions_parameters =
Expand Down Expand Up @@ -449,7 +454,8 @@ void CodegenNeuronCppVisitor::print_mechanism_register() {
}


void CodegenNeuronCppVisitor::print_mechanism_range_var_structure(bool print_initializers) {
void CodegenNeuronCppVisitor::print_mechanism_range_var_structure(
[[maybe_unused]] bool print_initializers) {
printer->add_newline(2);
printer->add_line("/* NEURON RANGE variables macro definitions */");
for (auto i = 0; i < codegen_float_variables.size(); ++i) {
Expand Down Expand Up @@ -731,6 +737,7 @@ void CodegenNeuronCppVisitor::print_codegen_routines() {
print_nrn_alloc();
print_global_variables_for_hoc();
print_compute_functions(); // only nrn_cur and nrn_state
print_sdlists_init(true);
print_mechanism_register();
print_namespace_end();
codegen = false;
Expand Down
14 changes: 10 additions & 4 deletions test/unit/codegen/codegen_neuron_cpp_visitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,13 +227,19 @@ void _nrn_mechanism_register_data_fields(Args&&... args) {
REQUIRE_THAT(generated,
ContainsSubstring(reindent_and_trim_text(expected_placeholder_nrn_state)));
}
THEN("Initialization function for slist/dlist is printed correctly") {
std::string expected_initlists_s_var = "_slist1[0] = {4, 0};";
std::string expected_initlists_Ds_var = "_dlist1[0] = {7, 0};";

REQUIRE_THAT(generated,
ContainsSubstring(reindent_and_trim_text(expected_initlists_s_var)));
REQUIRE_THAT(generated,
ContainsSubstring(reindent_and_trim_text(expected_initlists_Ds_var)));
}
THEN("Placeholder registration function is printed") {
std::string expected_placeholder_reg = R"CODE(/** register channel with the simulator */
extern "C" void __test_reg() {
/* s */
_slist1[0] = {4, 0};
/* Ds */
_dlist1[0] = {7, 0};
_initlists();
register_mech(mechanism_info, nrn_alloc_pas_test, nrn_cur_pas_test, nrn_jacob_pas_test, nrn_state_pas_test, nrn_init_pas_test, hoc_nrnpointerindex, 1);
Expand Down

0 comments on commit 85b0dfc

Please sign in to comment.