removed redundant code; added docs; adjused structure

pull/2/head
Ruben Seitz 2025-05-31 12:29:23 +02:00
parent 5abce884d1
commit f4ca3fb591
9 changed files with 121 additions and 65 deletions

View File

@ -1,8 +1,8 @@
# This file is machine-generated - editing it directly is not advised
julia_version = "1.11.4"
julia_version = "1.11.5"
manifest_format = "2.0"
project_hash = "0d147eaf78630b05e95b5317275a880443440272"
project_hash = "ede42d5c0ee6ced5b877b9fea99444d4d8bea863"
[[deps.ADTypes]]
git-tree-sha1 = "e2478490447631aedba0823d4d7a80b2cc8cdb32"
@ -15,6 +15,11 @@ weakdeps = ["ChainRulesCore", "ConstructionBase", "EnzymeCore"]
ADTypesConstructionBaseExt = "ConstructionBase"
ADTypesEnzymeCoreExt = "EnzymeCore"
[[deps.ANSIColoredPrinters]]
git-tree-sha1 = "574baf8110975760d391c710b6341da1afa48d8c"
uuid = "a4c015fc-c6ff-483c-b24f-f7ea428134e9"
version = "0.0.1"
[[deps.AbstractFFTs]]
deps = ["LinearAlgebra"]
git-tree-sha1 = "d92ad398961a3ed262d8bf04a1a2b8340f915fef"
@ -300,6 +305,12 @@ git-tree-sha1 = "05ba0d07cd4fd8b7a39541e31a7b0254704ea581"
uuid = "fb6a15b2-703c-40df-9091-08a04967cfa9"
version = "0.1.13"
[[deps.CodecZlib]]
deps = ["TranscodingStreams", "Zlib_jll"]
git-tree-sha1 = "962834c22b66e32aa10f7611c08c8ca4e20749a9"
uuid = "944b1d66-785c-5afd-91f1-9de20f533193"
version = "0.7.8"
[[deps.ColorBrewer]]
deps = ["Colors", "JSON"]
git-tree-sha1 = "e771a63cc8b539eca78c85b0cabd9233d6c8f06f"
@ -610,6 +621,12 @@ git-tree-sha1 = "e7b7e6f178525d17c720ab9c081e4ef04429f860"
uuid = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
version = "0.9.4"
[[deps.Documenter]]
deps = ["ANSIColoredPrinters", "AbstractTrees", "Base64", "CodecZlib", "Dates", "DocStringExtensions", "Downloads", "Git", "IOCapture", "InteractiveUtils", "JSON", "Logging", "Markdown", "MarkdownAST", "Pkg", "PrecompileTools", "REPL", "RegistryInstances", "SHA", "TOML", "Test", "Unicode"]
git-tree-sha1 = "6c182d0bd94142d7cbc3ae8a1e74668f15d0dd65"
uuid = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
version = "1.11.4"
[[deps.Downloads]]
deps = ["ArgTools", "FileWatching", "LibCURL", "NetworkOptions"]
uuid = "f43a241f-c20a-4ad4-852c-f6b1247861c6"
@ -935,6 +952,18 @@ git-tree-sha1 = "6570366d757b50fabae9f4315ad74d2e40c0560a"
uuid = "59f7168a-df46-5410-90c8-f2779963d0ec"
version = "5.2.3+0"
[[deps.Git]]
deps = ["Git_jll", "JLLWrappers", "OpenSSH_jll"]
git-tree-sha1 = "2230a9cc32394b11a3b3aa807a382e3bbab1198c"
uuid = "d7ba0133-e1db-5d97-8f8c-041e4b3a1eb2"
version = "1.4.0"
[[deps.Git_jll]]
deps = ["Artifacts", "Expat_jll", "JLLWrappers", "LibCURL_jll", "Libdl", "Libiconv_jll", "OpenSSL_jll", "PCRE2_jll", "Zlib_jll"]
git-tree-sha1 = "2f6d6f7e6d6de361865d4394b802c02fc944fc7c"
uuid = "f8c6e375-362e-5223-8a59-34ff63f689eb"
version = "2.49.0+0"
[[deps.Glib_jll]]
deps = ["Artifacts", "Gettext_jll", "JLLWrappers", "Libdl", "Libffi_jll", "Libiconv_jll", "Libmount_jll", "PCRE2_jll", "Zlib_jll"]
git-tree-sha1 = "b0036b392358c80d2d2124746c2bf3d48d457938"
@ -976,6 +1005,12 @@ git-tree-sha1 = "68c173f4f449de5b438ee67ed0c9c748dc31a2ec"
uuid = "34004b35-14d8-5ef3-9330-4cdb6864b03a"
version = "0.3.28"
[[deps.IOCapture]]
deps = ["Logging", "Random"]
git-tree-sha1 = "b6d6bfdd7ce25b0f9b2f6b3dd56b2673a66c8770"
uuid = "b5f81e59-6552-4d32-b1f0-c071b021bf89"
version = "0.2.5"
[[deps.IfElse]]
git-tree-sha1 = "debdd00ffef04665ccbb3e150747a77560e8fad1"
uuid = "615f187c-cbe4-4ef1-ba3b-2fcf58d6d173"
@ -1189,6 +1224,11 @@ git-tree-sha1 = "a9eaadb366f5493a5654e843864c13d8b107548c"
uuid = "10f19ff3-798f-405d-979b-55457f8fc047"
version = "0.1.17"
[[deps.LazilyInitializedFields]]
git-tree-sha1 = "0f2da712350b020bc3957f269c9caad516383ee0"
uuid = "0e77f7df-68c5-4e49-93ce-4cd80f5598bf"
version = "1.3.0"
[[deps.LazyArrays]]
deps = ["ArrayLayouts", "FillArrays", "LinearAlgebra", "MacroTools", "SparseArrays"]
git-tree-sha1 = "866ce84b15e54d758c11946aacd4e5df0e60b7a3"
@ -1409,6 +1449,12 @@ deps = ["Base64"]
uuid = "d6f4376e-aef5-505a-96c1-9c027394607a"
version = "1.11.0"
[[deps.MarkdownAST]]
deps = ["AbstractTrees", "Markdown"]
git-tree-sha1 = "465a70f0fc7d443a00dcdc3267a497397b8a3899"
uuid = "d0879d2d-cac2-40c8-9cee-1863dc0c7391"
version = "0.1.2"
[[deps.MathTeXEngine]]
deps = ["AbstractTrees", "Automa", "DataStructures", "FreeTypeAbstraction", "GeometryBasics", "LaTeXStrings", "REPL", "RelocatableFolders", "UnicodeFun"]
git-tree-sha1 = "f5a6805fb46c0285991009b526ec6fae43c6dec2"
@ -1631,7 +1677,13 @@ version = "3.2.4+0"
[[deps.OpenLibm_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "05823500-19ac-5b8b-9628-191a04bc5112"
version = "0.8.1+4"
version = "0.8.5+0"
[[deps.OpenSSH_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "OpenSSL_jll", "Zlib_jll"]
git-tree-sha1 = "cb7acd5d10aff809b4d0191dfe1956c2edf35800"
uuid = "9bd350c2-7e96-507f-8002-3f2e150b4e1b"
version = "10.0.1+0"
[[deps.OpenSSL_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl"]
@ -2109,6 +2161,12 @@ git-tree-sha1 = "45e428421666073eab6f2da5c9d310d99bb12f9b"
uuid = "189a3867-3050-52da-a836-e630ba90ab69"
version = "1.2.2"
[[deps.RegistryInstances]]
deps = ["LazilyInitializedFields", "Pkg", "TOML", "Tar"]
git-tree-sha1 = "ffd19052caf598b8653b99404058fce14828be51"
uuid = "2792f1a3-b283-48e8-9a74-f99dce5104f3"
version = "0.1.0"
[[deps.RelocatableFolders]]
deps = ["SHA", "Scratch"]
git-tree-sha1 = "ffdaf70d81cf6ff22c2b6e733c900c3321cab864"

View File

@ -1,4 +1,5 @@
[deps]
DifferentialEquations = "0c46a032-eb83-5123-abaf-570d42b7fbaa"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
GLMakie = "e9467ef8-e4e7-5192-8a1a-b1aee30e663a"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

View File

@ -1,14 +0,0 @@
function fhn!(du, u, p::FHNParams, t)
N, dx = p.N, p.dx
u_mat = reshape(u[1:N^2], N, N)
v_mat = reshape(u[N^2+1:end], N, N)
Δu = laplacian(u_mat) / dx^2
Δv = laplacian(v_mat) / dx^2
fu = p.Du * Δu .+ u_mat .- u_mat .^ 3 ./ 3 .- v_mat
fv = p.Dv * Δv .+ p.ε * (u_mat .+ p.a .- p.b .* v_mat)
du .= vcat(vec(fu), vec(fv))
end

View File

@ -2,9 +2,7 @@
module AnimalFurFHN
include("constants.jl")
include("initial_conditions.jl")
include("laplacian.jl")
include("model_fhn.jl")
include("solver.jl")
export run_simulation # Make sure this is here!

View File

@ -1,9 +1,15 @@
module Constants
struct FHNParams
N::Int
dx::Float64
dx::Float64 # grid spacing
Du::Float64
Dv::Float64
ε::Float64
ϵ::Float64
a::Float64
b::Float64
end
export FHNParams
end # module Constants

View File

@ -1,8 +0,0 @@
import Random
function initial_conditions(N)
Random.seed!(1234)
u = 0.1 .+ 0.01 .* rand(N, N)
v = 0.1 .+ 0.01 .* rand(N, N)
return vec(u), vec(v)
end

View File

@ -1,4 +1,19 @@
"""
laplacian(U::Matrix{Float64}, N::Int, h::Float64)
Computes the discrete 2D Laplacian of a matrix `U` using a 5-point stencil
and circular boundary conditions.
# Arguments
- `U::Matrix{Float64}`: The input 2D matrix representing the field or image.
- `N::Int`: Integer
- `h::Float64`: The spatial step size or grid spacing between points in the discretization.
# Returns
- `Vector{Float64}`: A flattened (vectorized) representation of the approximated Laplacian values for each element in `U`. The boundary conditions are handled circularly.
"""
function laplacian(U::Matrix{Float64}, N::Int, h::Float64)
# shifts matrices and sums them up
padded = circshift(U, (-1, 0)) .+ circshift(U, (1, 0)) .+
circshift(U, (0, -1)) .+ circshift(U, (0, 1)) .- 4 .* U
return vec(padded) ./ h^2

View File

@ -1,14 +0,0 @@
function fhn!(du, u, p::FHNParams, t)
N, dx = p.N, p.dx
u_mat = reshape(u[1:N^2], N, N)
v_mat = reshape(u[N^2+1:end], N, N)
Δu = laplacian(u_mat) / dx^2
Δv = laplacian(v_mat) / dx^2
fu = p.Du * Δu .+ u_mat .- u_mat .^ 3 ./ 3 .- v_mat
fv = p.Dv * Δv .+ p.ε * (u_mat .+ p.a .- p.b .* v_mat)
du .= vcat(vec(fu), vec(fv))
end

View File

@ -1,34 +1,48 @@
using DifferentialEquations
using Random
using .Constants
"""
fhn(du, u, p:FHNParams, t:)
Implements the spatial dynamics of FitzHugh-Nagumo (fhn). Designed to be
within a larger numerical solver of partial differential equations.
# Arguments
- `du`: output argument which stores the calculated derivatives
- `u`: input vector containing the current state of the system at time t
- `p`: holds all the fixed parameters of the FHN model
- `t`: current time
# Returns
- `du`: calculated derivatives put back into the du array
"""
function fhn!(du, u, p::FHNParams, t = 0)
u_mat = reshape(u[1:N^2], N, N) # activation variable
v_mat = reshape(u[N^2+1:end], N, N) # deactivation variable
Δu = reshape(laplacian(u_mat, N, p.dx), N, N)
Δv = reshape(laplacian(v_mat, N, p.dx), N, N)
fu = p.Du * Δu .+ u_mat .- u_mat .^ 3 ./ 3 .- v_mat
fv = p.Dv * Δv .+ p.ϵ * (u_mat .+ p.a .- p.b .* v_mat)
du .= vcat(vec(fu), vec(fv))
end
function run_simulation(N::Int, tspan::Tuple{Float64,Float64})
# Turing-spot parameters
a = 0.1
b = 0.5
ϵ = 0.01
Du = 1e-5
Dv = 1e-3
dx = 1.0 # grid spacing
p = FHNParams(N, 1.0, 1e-5, 1e-3, 0.01, 0.1, 0.5)
# Initial conditions (random noise)
u0, v0 = initial_conditions(N)
Random.seed!(1234)
# Create two vectors with length N*N with numbers between 0.1 and 0.11
u0 = vec(0.1 .+ 0.01 .* rand(N, N))
v0 = vec(0.1 .+ 0.01 .* rand(N, N))
y0 = vcat(u0, v0)
function f!(du, u, p, t)
u_mat = reshape(u[1:N^2], N, N)
v_mat = reshape(u[N^2+1:end], N, N)
Δu = reshape(laplacian(u_mat, N, dx), N, N)
Δv = reshape(laplacian(v_mat, N, dx), N, N)
fu = Du * Δu .+ u_mat .- u_mat .^ 3 ./ 3 .- v_mat
fv = Dv * Δv .+ ϵ * (u_mat .+ a .- b .* v_mat)
du .= vcat(vec(fu), vec(fv))
end
prob = ODEProblem(f!, y0, tspan)
prob = ODEProblem(fhn!, y0, tspan, p)
sol = solve(prob, BS3(), saveat=1.0) # You can try `Rosenbrock23()` too
return sol