Skip to content

Commit

Permalink
Array variables in DERIVATIVE block. (#1126)
Browse files Browse the repository at this point in the history
* Array variables in DERIVATIVE block.

* Check RANGE array variables.
  • Loading branch information
1uc authored Jan 8, 2024
1 parent 8821cdb commit 58bb7fd
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 4 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 @@ -468,9 +468,16 @@ void CodegenNeuronCppVisitor::print_make_instance() const {
const auto codegen_float_variables_size = codegen_float_variables.size();
for (int i = 0; i < codegen_float_variables_size; ++i) {
const auto& float_var = codegen_float_variables[i];
printer->fmt_line("&_ml.template fpfield<{}>(0){}",
i,
i < codegen_float_variables_size - 1 ? "," : "");
if (float_var->is_array()) {
printer->fmt_line("_ml.template data_array<{}, {}>(0){}",
i,
float_var->get_length(),
i < codegen_float_variables_size - 1 ? "," : "");
} else {
printer->fmt_line("&_ml.template fpfield<{}>(0){}",
i,
i < codegen_float_variables_size - 1 ? "," : "");
}
}
printer->pop_block(";");
printer->pop_block();
Expand Down
2 changes: 1 addition & 1 deletion test/usecases/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
set(NMODL_USECASE_DIRS cnexp_scalar)
set(NMODL_USECASE_DIRS cnexp_scalar cnexp_array)

foreach(usecase ${NMODL_USECASE_DIRS})
add_test(NAME usecase_${usecase}
Expand Down
30 changes: 30 additions & 0 deletions test/usecases/cnexp_array/leonhard.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
NEURON {
SUFFIX leonhard
RANGE z
}

ASSIGNED {
z[3]
}

STATE {
x
y[2]
}

INITIAL {
x = 42.0
y[0] = 0.1
y[1] = -1.0
z[0] = 0.7
z[1] = 0.8
z[2] = 0.9
}

BREAKPOINT {
SOLVE dX METHOD cnexp
}

DERIVATIVE dX {
x' = (y[0] + y[1])*(z[0]*z[1]*z[2])*x
}
27 changes: 27 additions & 0 deletions test/usecases/cnexp_array/simulate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import numpy as np

from neuron import h, gui
from neuron.units import ms

nseg = 1

s = h.Section()
s.insert("leonhard")
s.nseg = nseg

x_hoc = h.Vector().record(s(0.5)._ref_x_leonhard)
t_hoc = h.Vector().record(h._ref_t)

h.stdinit()
h.tstop = 5.0 * ms
h.run()

x = np.array(x_hoc.as_numpy())
t = np.array(t_hoc.as_numpy())

rate = (0.1 - 1.0) * (0.7 * 0.8 * 0.9)
x_exact = 42.0 * np.exp(rate*t)
rel_err = np.abs(x - x_exact) / x_exact

assert np.all(rel_err < 1e-12)
print("leonhard: success")

0 comments on commit 58bb7fd

Please sign in to comment.