# Spiking Neural Model

**URL:** <https://discourse.julialang.org/t/spiking-neural-model/135029>\
**Category:** Modelling & Simulations\
**Tags:** question, diffeqcallbacks\
**Created:** [January 13, 2026, 2:33pm UTC](https://discourse.julialang.org/t/spiking-neural-model/135029 "2026-01-13T14:33:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tbzcn](https://avatars.discourse-cdn.com/v4/letter/t/eb9ed0/32.png) [@tbzcn](https://discourse.julialang.org/u/tbzcn)\
**Post date:** [January 13, 2026, 2:33pm UTC](https://discourse.julialang.org/t/spiking-neural-model/135029/1 "2026-01-13T14:33:09Z")

</div>

Hi,

I am new to Julia and my aim is to create a network of spiking neurons. I use simple leaky integrate and fire (LIF) model for now with ODEProblem solver. Whenever the neuron (u) reaches a threshold value, it should enter a refractory period of some fixed ms, where du would be 0 and u would be kept in a reset value. Also the time points at which u reaches the threshold must be recorded as a vector for each neuron. However I couldn`t really understand how I can implement this, I use callbacks for thresholds passing condition, I am not sure how to use callbacks for setting refractory time. In python I would use a counter inside the function but since ODE handles the time itself I am confused how to intervene with it.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [January 21, 2026, 5:01am UTC](https://discourse.julialang.org/t/spiking-neural-model/135029/2 "2026-01-21T05:01:49Z")

</div>

What did you try? What not just add a counter inside the function as you describe?

```julia-auto
using DifferentialEquations

τ_m = 20.0
V_rest = -70.0
V_thresh = -55.0
V_reset = -75.0
τ_ref = 5.0
I_ext = 20.0

u0 = [V_rest]
spike_times = Float64[]
in_refractory = Ref(false)
refractory_end = Ref(0.0)

function lif!(du, u, p, t)
    V = u[1]
    if in_refractory[]
        du[1] = 0.0
    else
      du[1] = (V_rest - V + I_ext) / τ_m
    end
end

condition(u, t, integrator) = in_refractory[] ? -1.0 : u[1] - V_thresh

function affect!(integrator)
    push!(spike_times, integrator.t)
    integrator.u[1] = V_reset
    in_refractory[] = true
    refractory_end[] = integrator.t + τ_ref
    add_tstop!(integrator, refractory_end[])
end

spike_cb = ContinuousCallback(condition, affect!, nothing)

function refractory_condition(u, t, integrator)
    in_refractory[] && t >= refractory_end[]
end

function refractory_affect!(integrator)
    in_refractory[] = false
end

refractory_cb = DiscreteCallback(refractory_condition, refractory_affect!)

cb = CallbackSet(spike_cb, refractory_cb)

prob = ODEProblem(lif!, u0, (0.0, 200.0))
sol = solve(prob, Tsit5(), callback=cb)

println("Spike times: ", spike_times)

```
