Skip to content

Commit

Permalink
Add test.
Browse files Browse the repository at this point in the history
  • Loading branch information
1uc committed Sep 20, 2024
1 parent 29e6b6c commit 7b9225c
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
30 changes: 30 additions & 0 deletions test/usecases/solve/finite_difference.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
NEURON {
SUFFIX finite_difference
GLOBAL a
THREADSAFE
}

ASSIGNED {
a
}

STATE {
x
}

INITIAL {
x = 42.0
a = 0.1
}

BREAKPOINT {
SOLVE dX METHOD derivimplicit
}

DERIVATIVE dX {
x' = -f(x)
}

FUNCTION f(x) {
f = a*x
}
30 changes: 30 additions & 0 deletions test/usecases/solve/test_finite_difference.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import numpy as np
from neuron import h, gui
from neuron.units import ms


def test_finite_difference():
nseg = 1

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

x_hoc = h.Vector().record(getattr(s(0.5), f"_ref_x_finite_difference"))
t_hoc = h.Vector().record(h._ref_t)

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

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

a = h.a_finite_difference
x_exact = 42.0 * np.exp(-a * t)
np.testing.assert_allclose(x, x_exact, rtol=1e-4)


if __name__ == "__main__":
test_finite_difference()

0 comments on commit 7b9225c

Please sign in to comment.