Skip to content
Snippets Groups Projects
Commit 871adee3 authored by Lars Stietz's avatar Lars Stietz
Browse files

Merge branch '8-simulation-update' into 'main'

Resolve "Simulation update"

Closes #8

See merge request !11
parents 8bc1bf2e 92db0a37
No related branches found
No related tags found
1 merge request!11Resolve "Simulation update"
Showing
with 70 additions and 60 deletions
plots/simulation_waterchannel_nx=100_dt=1e-3/sensor_2_observation.png

39 KiB

plots/simulation_waterchannel_nx=100_dt=1e-3/sensor_3_observation.png

36.9 KiB

plots/simulation_waterchannel_nx=100_dt=1e-3/sensor_4_observation.png

34.1 KiB

plots/simulation_waterchannel_nx=100_dt=1e-3/sensor_observation_waterchannel.png

37.1 KiB

plots/simulation_waterchannel_nx=100_dt=1e-3/simulation_h_heatmap_waterchannel.png

44.8 KiB

plots/simulation_waterchannel_nx=100_dt=1e-3/simulation_h_surface_waterchannel.png

59 KiB

plots/simulation_waterchannel_nx=100_dt=5e-5/sensor_2_observation.png

38.8 KiB

plots/simulation_waterchannel_nx=100_dt=5e-5/sensor_3_observation.png

36.4 KiB

plots/simulation_waterchannel_nx=100_dt=5e-5/sensor_4_observation.png

34.7 KiB

plots/simulation_waterchannel_nx=100_dt=5e-5/sensor_observation_waterchannel.png

37.1 KiB

plots/simulation_waterchannel_nx=100_dt=5e-5/simulation_h_heatmap_waterchannel.png

44.8 KiB

plots/simulation_waterchannel_nx=100_dt=5e-5/simulation_h_surface_waterchannel.png

64.9 KiB

plots/simulation_waterchannel_nx=68_dt=1e-3/sensor_2_observation.png

39.6 KiB

plots/simulation_waterchannel_nx=68_dt=1e-3/sensor_3_observation.png

37.8 KiB

plots/simulation_waterchannel_nx=68_dt=1e-3/sensor_4_observation.png

34.7 KiB

plots/simulation_waterchannel_nx=68_dt=1e-3/sensor_observation_waterchannel.png

39 KiB

plots/simulation_waterchannel_nx=68_dt=1e-3/simulation_h_heatmap_waterchannel.png

44.6 KiB

plots/simulation_waterchannel_nx=68_dt=1e-3/simulation_h_surface_waterchannel.png

69.2 KiB

......@@ -4,10 +4,13 @@ Pkg.activate(".")
using Turing
using PyCall
using Plots
using LinearAlgebra
using Optim
PyCall.python
# Import the SWESolver class from the utils module
@pyimport swe_wrapper as swe
@pyimport swe_wrapper.utils as utils
using HDF5 # Has to imported after my module otherwise pycall cannot find swe_wrapper
struct observation_data
t::Array{Float64}
......@@ -28,73 +31,80 @@ function load_observation_data(file_path::String)
end
file_path = "./data/toy_measurement/simulation_data.h5"
measurement = "./experiment_data/Data_Sensors_orig/With_Bathymetry/Heat1.txt"
mean_bc = "./experiment_data/Data_Sensors_orig/With_Bathymetry/mean_bc.txt"
observation = load_observation_data(file_path)
xbounds = (0., 10.)
nx = 64
tend = 10
timestep = 1e-3
dataObj = utils.data(measurement)
# lbc1 = swe.leftbc(measurement)
# lbc2 = swe.leftbc(mean_bc)
bathy = swe.rampFunc
# gaussian = swe.gaussian_bathymetry
# println(norm(lbc1.f(observation.x) - lbc2.f(observation.x)))
# cost(x) = sum((bathy(observation.x) - gaussian(observation.x, x)).^2)
# res = optimize(cost, [4., 0.1])
# println(Optim.minimizer(res))
# println(Optim.minimum(res))
# plot(observation.x, observation.b)
# plot!(observation.x, gaussian(observation.x, (4., 0.1)))
# display(plot!(observation.x, bathy(observation.x)))
# println(norm(gaussian(observation.x, (4., 0.15)) - bathy(observation.x)))
xbounds = (1.5, 15.)
nx = 100
total_t = 10
tstart = 32.
timestep = 5e-5
timestepstr = "5e-5"
g = 9.81
kappa = 0.2
dealias = 3/2
bathy_peak = (5,1) #rand(MvNormal(zeros(130), 1),1)
#b = observation.b[1:2:end-2]
b = zeros(64)
b[1] = rand(Normal(0,0.01),1)[1]
for i in 2:64
b[i] = rand(Normal(b[i-1],0.01),1)[1]
end
function create_exponential_decay_matrix(n, decay_factor)
A = zeros(n, n)
for i in 1:n
for j in 1:n
A[i, j] = exp(-1/decay_factor * abs(i - j))
end
end
return A
end
problemtype = "waterchannel"
# Create SWE solver and calculate the solution
solver = swe.SWESolver(xbounds, timestep, nx, total_t, tstart=tstart, g=g, kappa=kappa, dealias=dealias, problemtype=problemtype)
x = solver.domain.x
b = bathy(x)
# Use the solver to simulate the shallow water equation with the sampled peak
@time sensor_sim, t, simulation_h, _ = solver.solve(b)
H = simulation_h .+ b'
t = t .+ 32
# Create laplacian operator matrix with dirichlet boundary conditions
function create_laplace_matrix_1d(N)
# N ist die Anzahl der Gitterpunkte
L = zeros(Float64, N, N) # Erstellen einer N×N Matrix
sensor_obs = hcat(dataObj.f[1](t), dataObj.f[2](t), dataObj.f[3](t)).+ 0.3
# Füllen der Matrix mit den Werten für den Laplace-Operator
for i in 1:N
if i > 1
L[i, i - 1] = 1 # Nachbar links
end
L[i, i] = -2 # Zentraler Punkt
if i < N
L[i, i + 1] = 1 # Nachbar rechts
end
error = (sensor_sim .- sensor_obs)
println(size(error))
rel_l2_error = [norm(error[:,i])./ norm(sensor_obs[:,i]) for i in 1:size(error,2)]
println("Relative Error: ", rel_l2_error)
plot_path = "plots/simulation_$(problemtype)_nx=$(nx)_dt=$(timestepstr)"
println("Save figure in $(plot_path)? (y/o/[n]): ")
s = readline()
if s == "y" || s == "o"
if s == "o"
println("Enter path: ")
plot_path = readline()
end
return 1/N.*L
mkpath(plot_path)
cd(plot_path)
plot(x,t, H, st=:surface, colorbar_title="h [m]")
xlabel!("x [m]")
ylabel!("t [s]")
zlabel!("h [m]")
savefig("simulation_h_surface_$(problemtype).png")
plot(x,t, H, st=:heatmap, colorbar_title="h [m]")
xlabel!("x [m]")
ylabel!("t [s]")
savefig("simulation_h_heatmap_$(problemtype).png")
for i in 1:3
plot(t, sensor_obs[:,i], label="sensor $(i+1)")
plot!(t, sensor_sim[:,i], label="sensor $(i+1) sim")
title!("Relative L2-Error: $(rel_l2_error[i])")
savefig("sensor_$(i+1)_observation.png")
end
plot(t .+ 32, sensor_sim, label=hcat(["sensor $(i)" for i in 2:4]...), xticks=32:2:42, yticks=([0.294:0.002:0.306;],[0.294:0.002:0.306;]))
xlabel!("t [s]")
ylabel!("h [m]")
savefig("sensor_observation_$(problemtype).png")
end
decay_factor = 100 # Decay factor for off-diagonals
A = create_exponential_decay_matrix(nx, decay_factor)
b = rand(MvNormal(zeros(64), A), 1)
# smoothed prior
L = create_laplace_matrix_1d(nx)
gamma = 0.1
w = rand(MvNormal(zeros(nx), 1), 1)
b = L\(gamma.*w)
b = 0.2.* b/maximum(b)
plot(b)
savefig("b_rand_smoothed.png")
# Create SWE solver and calculate the solution
solver = swe.SWESolver(xbounds, timestep, nx, tend, g, kappa, dealias)
solver.set_initial_conditions()
c = 1.2e-3 + minimum(solver.initial_conditions.H)
println(minimum(solver.initial_conditions.H))
b=ones(64)*c
solver.set_initial_conditions()
println(minimum(solver.initial_conditions.H-b))
# Use the solver to simulate the shallow water equation with the sampled peak
@time simulation_h, _, t = solver.solve(b)
println(t[end])
\ No newline at end of file
simulation_h_heatmap_periodic.png

46.7 KiB

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment