Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix instantiation of array state variables #1081

Merged
merged 5 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/codegen/codegen_cpp_visitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3330,7 +3330,13 @@ void CodegenCppVisitor::print_initial_block(const InitialBlock* node) {
if (!info.is_ionic_conc(name)) {
auto lhs = get_variable_name(name);
auto rhs = get_variable_name(name + "0");
printer->fmt_line("{} = {};", lhs, rhs);
if (var->is_array()) {
for (int i = 0; i < var->get_length(); ++i) {
printer->fmt_line("{}[{}] = {};", lhs, i, rhs);
}
} else {
printer->fmt_line("{} = {};", lhs, rhs);
}
}
}

Expand Down
54 changes: 54 additions & 0 deletions test/unit/codegen/codegen_cpp_visitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1181,3 +1181,57 @@ SCENARIO("Check codegen for MUTEX and PROTECT", "[codegen][mutex_protect]") {
}
}
}


SCENARIO("Array STATE variable", "[codegen][array_state]") {
GIVEN("A mod file containing an array STATE variable") {
std::string const nmodl_text = R"(
DEFINE NANN 4

NEURON {
SUFFIX ca_test
}
STATE {
ca[NANN]
k
}
)";

THEN("nrn_init is printed with proper initialization of the whole array") {
auto const generated = get_cpp_code(nmodl_text);
std::string expected_code_init =
R"(/** initialize channel */
void nrn_init_ca_test(NrnThread* nt, Memb_list* ml, int type) {
int nodecount = ml->nodecount;
int pnodecount = ml->_nodecount_padded;
const int* node_index = ml->nodeindices;
double* data = ml->data;
const double* voltage = nt->_actual_v;
Datum* indexes = ml->pdata;
ThreadDatum* thread = ml->_thread;

setup_instance(nt, ml);
auto* const inst = static_cast<ca_test_Instance*>(ml->instance);

if (_nrn_skip_initmodel == 0) {
#pragma omp simd
#pragma ivdep
for (int id = 0; id < nodecount; id++) {
int node_id = node_index[id];
double v = voltage[node_id];
#if NRN_PRCELLSTATE
inst->v_unused[id] = v;
#endif
(inst->ca+id*4)[0] = inst->global->ca0;
(inst->ca+id*4)[1] = inst->global->ca0;
(inst->ca+id*4)[2] = inst->global->ca0;
(inst->ca+id*4)[3] = inst->global->ca0;
inst->k[id] = inst->global->k0;
}
}
})";

REQUIRE_THAT(generated, ContainsSubstring(expected_code_init));
}
}
}
Loading