add explicit fhn solving

pull/6/head
Nikola Sebastian Munder 2025-06-17 17:49:33 +02:00
parent 020da7c07a
commit 53383487dc
1 changed files with 69 additions and 31 deletions

View File

@ -24,48 +24,86 @@ using .Laplacian
# Returns # Returns
- `du`: calculated derivatives put back into the du array - `du`: calculated derivatives put back into the du array
""" """
function fhn!(du, u, p::CombinedPDEParams, t) # function fhn!(du, u, p::CombinedPDEParams, t)
N = p.N # N = p.N
u_mat = reshape(u[1:N^2], N, N) # u_mat = reshape(u[1:N^2], N, N)
v_mat = reshape(u[N^2+1:end], N, N) # v_mat = reshape(u[N^2+1:end], N, N)
Δu = laplacian(u_mat, p.dx) # Δu = laplacian(u_mat, p.dx)
Δv = laplacian(v_mat, p.dx) # Δv = laplacian(v_mat, p.dx)
u_in = u_mat[2:end-1, 2:end-1] # u_in = u_mat[2:end-1, 2:end-1]
v_in = v_mat[2:end-1, 2:end-1] # v_in = v_mat[2:end-1, 2:end-1]
fu = p.Du * Δu .+ u_in .- u_in .^ 3 ./ 3 .- v_in # fu = p.Du * Δu .+ u_in .- u_in .^ 3 ./ 3 .- v_in
fv = p.Dv * Δv .+ p.ϵ * (u_in .+ p.a .- p.b .* v_in) # fv = p.Dv * Δv .+ p.ϵ * (u_in .+ p.a .- p.b .* v_in)
# Construct output with zero boundary (padding) # # Construct output with zero boundary (padding)
fu_full = zeros(N, N) # fu_full = zeros(N, N)
fv_full = zeros(N, N) # fv_full = zeros(N, N)
fu_full[2:end-1, 2:end-1] .= fu # fu_full[2:end-1, 2:end-1] .= fu
fv_full[2:end-1, 2:end-1] .= fv # fv_full[2:end-1, 2:end-1] .= fv
du .= vcat(vec(fu_full), vec(fv_full)) # du .= vcat(vec(fu_full), vec(fv_full))
end # end
function step_fhn!(U, V, params_obs::Observable; dx=1) # function step_fhn!(U, V, params_obs::Observable; dx=1)
# p = params_obs[]
# # Flatten initial condition (activation u, recovery v)
# u0 = vec(U)
# v0 = vec(V)
# u_init = vcat(u0, v0)
# # Define one integration step using your fhn! function
# prob = ODEProblem((du, u, p, t) -> fhn!(du, u, p, t), u_init, (0.0, 0.1), p)
# sol = solve(prob, BS3(); save_start=false, saveat=10.0)
# # Extract solution and reshape
# u_new = reshape(sol[end][1:p.N^2], p.N, p.N)
# v_new = reshape(sol[end][p.N^2+1:end], p.N, p.N)
# # Update matrices in-place
# U .= u_new
# V .= v_new
# # Apply periodic boundary conditions
# U[1, :] .= U[end-1, :]
# U[end, :] .= U[2, :]
# U[:, 1] .= U[:, end-1]
# U[:, end] .= U[:, 2]
# V[1, :] .= V[end-1, :]
# V[end, :] .= V[2, :]
# V[:, 1] .= V[:, end-1]
# V[:, end] .= V[:, 2]
# return U
# end
function step_fhn!(U, V, params_obs::Observable; dx=1, dt=0.01)
p = params_obs[] p = params_obs[]
# Flatten initial condition (activation u, recovery v) N = p.N
u0 = vec(U)
v0 = vec(V)
u_init = vcat(u0, v0)
# Define one integration step using your fhn! function # Compute Laplacians on the interior
prob = ODEProblem((du, u, p, t) -> fhn!(du, u, p, t), u_init, (0.0, 0.1), p) Δu = laplacian(U, dx)
sol = solve(prob, BS3(); save_start=false, saveat=10.0) Δv = laplacian(V, dx)
# Extract solution and reshape # Extract interior
u_new = reshape(sol[end][1:p.N^2], p.N, p.N) u_in = U[2:end-1, 2:end-1]
v_new = reshape(sol[end][p.N^2+1:end], p.N, p.N) v_in = V[2:end-1, 2:end-1]
# Update matrices in-place # Compute update using FHN reaction-diffusion terms
U .= u_new fu = p.Du .* Δu .+ u_in .- u_in .^ 3 ./ 3 .- v_in
V .= v_new fv = p.Dv .* Δv .+ p.ϵ .* (u_in .+ p.a .- p.b .* v_in)
# Euler update
u_new = u_in .+ dt .* fu
v_new = v_in .+ dt .* fv
# Write back interior updates
U[2:end-1, 2:end-1] .= u_new
V[2:end-1, 2:end-1] .= v_new
# Apply periodic boundary conditions # Apply periodic boundary conditions
U[1, :] .= U[end-1, :] U[1, :] .= U[end-1, :]