From 884e87383cf50fa2acaf988462a635a3e4890b59 Mon Sep 17 00:00:00 2001 From: 2211567 Date: Sat, 14 Jun 2025 14:36:31 +0200 Subject: [PATCH] compatibility of new UI with FHN implemented --- scripts/main.jl | 31 ++++++++++++++++++++--- scripts/run_simulation.jl | 13 ---------- src/fhn_solver.jl | 46 ++++++++++++++++++++------------- src/gray_scott_solver.jl | 2 ++ src/visualization.jl | 53 +++++++++++++-------------------------- 5 files changed, 76 insertions(+), 69 deletions(-) delete mode 100644 scripts/run_simulation.jl diff --git a/scripts/main.jl b/scripts/main.jl index 69ce53f..a95c179 100644 --- a/scripts/main.jl +++ b/scripts/main.jl @@ -1,5 +1,7 @@ +include("../src/utils/constants.jl") +include("../src/fhn_solver.jl") +#include("../src/gray_scott_solver.jl") include("../src/visualization.jl") -include("../src/gray_scott_solver.jl") using Observables using GLMakie @@ -7,6 +9,8 @@ using GLMakie using .Constants using .Visualization +""" +# GSParams N = 128 dx = 1.0 Du, Dv = Observable(0.16), Observable(0.08) @@ -18,11 +22,32 @@ param_observables = ( F=F, k=k, ) +""" -params_obs = Observable(GSParams(N, dx, Du[], Dv[], F[], k[])) +# FHNParams +N = 128 +dx = 1.0 +Du, Dv = Observable(0.016), Observable(0.1) +ϵ, a, b = Observable(0.1), Observable(0.5), Observable(0.9) +param_observables = ( + Du=Du, + Dv=Dv, + ϵ=ϵ, + a=a, + b=b +) +#params_obs = Observable(GSParams(N, dx, Du[], Dv[], F[], k[])) +params_obs = Observable(Constants.FHNParams(N=N, dx=dx, Du=Du[], Dv=Dv[], ϵ=ϵ[], a=a[], b=b[])) + +""" lift(Du, Dv, F, k) do u, v, f, ki - params_obs[] = GSParams(N, dx, u, v, f, ki) + params_obs[] = GSParams(N, dx, u, v, f, ki) +end +""" + +lift(Du, Dv, ϵ, a, b) do d_u, d_v, eps, aa, bb + params_obs[] = Constants.FHNParams(N=N, dx=dx, Du=d_u, Dv=d_v, ϵ=eps, a=aa, b=bb) end U = ones(N, N) diff --git a/scripts/run_simulation.jl b/scripts/run_simulation.jl deleted file mode 100644 index 3a5a5e5..0000000 --- a/scripts/run_simulation.jl +++ /dev/null @@ -1,13 +0,0 @@ -include("../src/AnimalFurFHN.jl") # this loads the module code -using .AnimalFurFHN -# include optional visualizer only if needed: -include("../src/visualization.jl") -using .Visualization - -N = 128 -tspan = (0.0, 1000.0) - -sol = AnimalFurFHN.run_simulation(tspan, N) - - -Visualization.step_through_solution(sol, N) \ No newline at end of file diff --git a/src/fhn_solver.jl b/src/fhn_solver.jl index 41e2524..4118c01 100644 --- a/src/fhn_solver.jl +++ b/src/fhn_solver.jl @@ -1,7 +1,11 @@ +include("utils/laplacian.jl") + using DifferentialEquations using Random +using Observables + +using ..Constants using .Laplacian -using .Constants """ fhn(du, u, p:FHNParams, t:) @@ -18,7 +22,7 @@ using .Constants # Returns - `du`: calculated derivatives put back into the du array """ -function fhn!(du, u, p::FHNParams, t=0) +function fhn!(du, u, p::FHNParams, t) u_mat = reshape(u[1:p.N^2], p.N, p.N) # activation variable v_mat = reshape(u[p.N^2+1:end], p.N, p.N) # deactivation variable @@ -31,25 +35,31 @@ function fhn!(du, u, p::FHNParams, t=0) du .= vcat(vec(fu), vec(fv)) end -""" - run_simulation(tspan::Tuple{Float64,Float64}, N::Int) +function step!(U, V, params_obs::Observable; dx=1) + p = params_obs[] - solving the ODE and modelling it after FHN + # Flatten initial condition (activation u, recovery v) + u0 = vec(U) + v0 = vec(V) + u_init = vcat(u0, v0) - # Arguments - - `tspan`: tuple of two Float64's representing start and end times for simulation - - `N`: size of the N×N grid + # 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, Tsit5(); dt=0.1, save_start=false, saveat=10.0) - # Returns - - `sol`: solved differential equation (ODE) -""" -function run_simulation(tspan::Tuple{Float64,Float64}, N::Int) - # Initial conditions - # params, y0 = zebra_conditions(N) # tspan of ~3500 enough - params, y0 = zebra_conditions(N) + # 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) - prob = ODEProblem(fhn!, y0, tspan, params) - sol = solve(prob, BS3(), saveat=50.0) # You can try `Rosenbrock23()` too + # Update matrices in-place + U .= u_new + V .= v_new - return sol + return U +end + +function multi_step!(state, n_steps, heat_obs::Observable, params_obs::Observable; dx=1) + for _ in 1:n_steps + heat_obs[] = step!(state[1], state[2], params_obs; dx=1) + end end diff --git a/src/gray_scott_solver.jl b/src/gray_scott_solver.jl index 2c213f0..eeb0ed8 100644 --- a/src/gray_scott_solver.jl +++ b/src/gray_scott_solver.jl @@ -1,6 +1,8 @@ include("utils/constants.jl") include("utils/laplacian.jl") + using Observables + using .Constants using .Laplacian diff --git a/src/visualization.jl b/src/visualization.jl index e50ceac..792a87f 100644 --- a/src/visualization.jl +++ b/src/visualization.jl @@ -1,46 +1,29 @@ module Visualization -include("gray_scott_solver.jl") +#include("gray_scott_solver.jl") +include("fhn_solver.jl") using GLMakie, Observables, Makie -""" - step_through_solution(sol::SolutionType, N::Int) - - Function for visualization for the output of run_simulation - - # Arguments - - `sol`: computed differential equation by run_simulation - - `N`: size of the N×N grid - - # Returns - - ``: Displays created figure -""" -function step_through_solution(sol, N::Int) - fig = Figure(resolution=(600, 600)) - ax = Axis(fig[1, 1]) - slider = Slider(fig[2, 1], range=1:length(sol), startvalue=1) - - # Initialize heatmap with first time step - u0 = reshape(sol[1][1:N^2], N, N) - heat_obs = Observable(u0) - hmap = heatmap!(ax, heat_obs, colormap=:RdGy) - - # Update heatmap on slider movement - on(slider.value) do i - u = reshape(sol[i][1:N^2], N, N) - heat_obs[] = u - end - - - display(fig) -end - function coord_to_index(x, y, N) ix = clamp(round(Int, x), 1, N) iy = clamp(round(Int, y), 1, N) return ix, iy end +""" + reset(U, V, heat_obs) + + Resets heatmap to original state by replacing each cell. + Currently only places a square in the center + + # Arguments + - `U`: Matrix filled with ones + - `V`: Empty matrix filled with zeros + - `heat_obs`: Heatmap observable + + # Returns + - ``: resetted heatmap observable +""" function reset!(U, V, heat_obs) U .= 1.0 V .= 0.0 @@ -77,7 +60,7 @@ function build_ui(U, V, param_obs_map::NamedTuple, params_obs, heat_obs) stepsize = Observable(30) spoint = select_point(ax.scene) - # # Controls + # Controls fig[2, 1] = buttongrid = GridLayout(ax.scene, tellwidth=false) btn_step = Button(buttongrid[1, 1], width=50, label="Step") btn_start = Button(buttongrid[1, 2], width=50, label=run_label) @@ -162,5 +145,5 @@ function build_ui(U, V, param_obs_map::NamedTuple, params_obs, heat_obs) return fig end -export step_through_solution, build_ui +export build_ui end