Skip to content

Commit

Permalink
Merge pull request #22 from ranocha/patch-1
Browse files Browse the repository at this point in the history
Possible solution of the Julia optimization task
  • Loading branch information
behinger authored Oct 20, 2023
2 parents 37f4033 + 66af620 commit 7d98603
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
3 changes: 3 additions & 0 deletions material/5_fri/optimizing_julia/optimizing_julia.jl
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ using BenchmarkTools
@profview simulate()

# Task: Optimize the code!
#
# You can find one improved version in the file
# [`optimizing_julia_solution.jl`](optimizing_julia_solution.jl).


# ## Introduction to generic Julia code and AD
Expand Down
3 changes: 3 additions & 0 deletions material/5_fri/optimizing_julia/optimizing_julia.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ PProf.jl (`pprof()` after creating a profile via `@profview`).

Task: Optimize the code!

You can find one improved version in the file
[`optimizing_julia_solution.jl`](optimizing_julia_solution.jl).

## Introduction to generic Julia code and AD

One of the strengths of Julia is that you can use quite a few
Expand Down
86 changes: 86 additions & 0 deletions material/5_fri/optimizing_julia/optimizing_julia_solution.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# # Optimizing Julia code: possible solution

# First, we install all required packages
import Pkg
Pkg.activate(@__DIR__)
Pkg.instantiate()

# The markdown file is created from the source code using
# [Literate.jl](https://github.com/fredrikekre/Literate.jl).
# You can create the markdown file via

using Literate
Literate.markdown(joinpath(@__DIR__, "optimizing_julia_solution.jl");
flavor = Literate.CommonMarkFlavor())

# First, we generate initial data and store it in a file.
using HDF5
x = range(-1.0, 1.0, length = 1000)
dx = step(x)
h5open("initial_data.h5", "w") do io
u0 = sinpi.(x)
write_dataset(io, "x", collect(x))
write_dataset(io, "u0", u0)
end

function simulate(; kwargs...)
x, u0 = h5open("initial_data.h5", "r") do io
read_dataset(io, "x"), read_dataset(io, "u0")
end

u = copy(u0)
t = simulate!(u; kwargs...)

return t, x, u, u0
end

function simulate!(u; t_end, dt, dx)
du = similar(u)
t = zero(t_end)
while t < t_end
rhs!(du, u, dx)
# u = u + dt * du
# u .= u .+ dt .* du
@. u = u + dt * du
t = t + dt
end

return t
end

function rhs!(du, u, dx)
inv_dx = inv(dx)

let i = firstindex(u)
im1 = lastindex(u)
# du[i] = -(u[i] - u[im1]) / dx
du[i] = -inv_dx * (u[i] - u[im1])
end

for i in (firstindex(u) + 1):lastindex(u)
im1 = i - 1
# du[i] = -(u[i] - u[im1]) / dx
du[i] = -inv_dx * (u[i] - u[im1])
end
return nothing
end

# Now, we can define our parameters, run a simulation,
# and plot the results.
using Plots, LaTeXStrings

t_end = 2.5
dt = 0.9 * dx
t, x, u, u0 = simulate(; t_end, dt, dx)
plot(x, u0; label = L"u_0", xguide = L"x")
plot!(x, u; label = L"u")


using BenchmarkTools
@benchmark simulate(; t_end, dt, dx)

let u = h5open("initial_data.h5", "r") do io
read_dataset(io, "u0")
end
@benchmark simulate!(u; t_end, dt, dx)
end

0 comments on commit 7d98603

Please sign in to comment.