Skip to content

Commit

Permalink
Publish solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
utkinis committed Oct 8, 2024
1 parent 3da5b17 commit 2f370c9
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 84 deletions.
4 changes: 2 additions & 2 deletions slide-notebooks/deploy_notebooks.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Literate
## include Literate scripts starting with following 3 letters in the deploy
incl = "l2_"
incl = "l3_"
## Set `sol=true` to produce output with solutions contained and hints stripts. Otherwise the other way around.
sol = false
sol = true
##

function replace_string(str)
Expand Down
79 changes: 54 additions & 25 deletions slide-notebooks/notebooks/l1_3-julia-intro.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -510,8 +510,8 @@
"cell_type": "code",
"source": [
"a = [2, 3]\n",
"b = ...\n",
"[ ; ]"
"b = [4, 5]\n",
"[a ; b]"
],
"metadata": {},
"execution_count": null
Expand All @@ -527,7 +527,8 @@
"outputs": [],
"cell_type": "code",
"source": [
"#"
"push!(b, 1)\n",
"push!(b, 3, 4)"
],
"metadata": {},
"execution_count": null
Expand All @@ -550,7 +551,7 @@
"outputs": [],
"cell_type": "code",
"source": [
"[ ; ]"
"[1:10; [4,5]]"
],
"metadata": {},
"execution_count": null
Expand All @@ -566,7 +567,7 @@
"outputs": [],
"cell_type": "code",
"source": [
"#"
"a = rand(3,3)"
],
"metadata": {},
"execution_count": null
Expand All @@ -589,7 +590,7 @@
"outputs": [],
"cell_type": "code",
"source": [
"a[ ... ], a[ ... ]"
"a[1,2], a[2,1]"
],
"metadata": {},
"execution_count": null
Expand All @@ -605,7 +606,7 @@
"outputs": [],
"cell_type": "code",
"source": [
"#"
"[ a[1,2], a[2,1] ]"
],
"metadata": {},
"execution_count": null
Expand Down Expand Up @@ -639,8 +640,8 @@
"outputs": [],
"cell_type": "code",
"source": [
"a[...]\n",
"a[..., ...]"
"a[end]\n",
"a[end, end]"
],
"metadata": {},
"execution_count": null
Expand All @@ -663,7 +664,7 @@
"outputs": [],
"cell_type": "code",
"source": [
"a[... , ...]"
"a[end, 1:end]"
],
"metadata": {},
"execution_count": null
Expand All @@ -679,7 +680,7 @@
"outputs": [],
"cell_type": "code",
"source": [
"a[ ]"
"a[1:2, 1:2]"
],
"metadata": {},
"execution_count": null
Expand Down Expand Up @@ -803,7 +804,7 @@
"outputs": [],
"cell_type": "code",
"source": [
"@assert ..."
"@assert a[1] == 99"
],
"metadata": {},
"execution_count": null
Expand Down Expand Up @@ -862,7 +863,10 @@
"outputs": [],
"cell_type": "code",
"source": [
"#"
"a = Int[]\n",
"push!(a, 1) ## works\n",
"push!(a, 1.0) ## works\n",
"push!(a, 1.5) ## errors as 1.5 cannot be converted to an Int"
],
"metadata": {},
"execution_count": null
Expand All @@ -879,7 +883,9 @@
"outputs": [],
"cell_type": "code",
"source": [
"#"
"a = []\n",
"push!(a, 5)\n",
"push!(a, \"a\")"
],
"metadata": {},
"execution_count": null
Expand All @@ -895,7 +901,7 @@
"outputs": [],
"cell_type": "code",
"source": [
"#"
"[1][1] = 1.5 ## errors"
],
"metadata": {},
"execution_count": null
Expand All @@ -919,7 +925,7 @@
"outputs": [],
"cell_type": "code",
"source": [
"#"
"a = Array{Any}(undef, 3, 3)"
],
"metadata": {},
"execution_count": null
Expand All @@ -935,7 +941,7 @@
"outputs": [],
"cell_type": "code",
"source": [
"#"
"size(a)"
],
"metadata": {},
"execution_count": null
Expand Down Expand Up @@ -1003,7 +1009,14 @@
"outputs": [],
"cell_type": "code",
"source": [
"#"
"a = \"Where are the flowers\"\n",
"if startswith(a, \"Wh\")\n",
" b = \"Likely a question\"\n",
"elseif startswith(a, \"The\")\n",
" b = \"Likely a noun\"\n",
"else\n",
" b = \"no idea\"\n",
"end"
],
"metadata": {},
"execution_count": null
Expand Down Expand Up @@ -1035,7 +1048,7 @@
"outputs": [],
"cell_type": "code",
"source": [
"#"
"a > 5 ? \"really big\" : \"not so big\""
],
"metadata": {},
"execution_count": null
Expand Down Expand Up @@ -1067,6 +1080,14 @@
],
"metadata": {}
},
{
"cell_type": "markdown",
"source": [
"If `a < 0` evaluates to `true` then the bit after the `&&` is evaluated too,\n",
"i.e. an error is thrown. Otherwise, only `a < 0` is evaluated and no error is thrown."
],
"metadata": {}
},
{
"cell_type": "markdown",
"source": [
Expand Down Expand Up @@ -1147,7 +1168,13 @@
"outputs": [],
"cell_type": "code",
"source": [
"#"
"function fn(a, b)\n",
" if a> b\n",
" return a\n",
" else\n",
" return b\n",
" end\n",
"end"
],
"metadata": {},
"execution_count": null
Expand All @@ -1173,7 +1200,8 @@
"outputs": [],
"cell_type": "code",
"source": [
"#"
"mymap(fn, a) = [fn(aa) for aa in a]\n",
"mymap(sin, 1:10)"
],
"metadata": {},
"execution_count": null
Expand All @@ -1199,7 +1227,7 @@
"outputs": [],
"cell_type": "code",
"source": [
"#"
"sin.(1:10)"
],
"metadata": {},
"execution_count": null
Expand All @@ -1216,7 +1244,7 @@
"outputs": [],
"cell_type": "code",
"source": [
"#"
"(1:10) .+ (1:10)'"
],
"metadata": {},
"execution_count": null
Expand All @@ -1240,7 +1268,8 @@
"outputs": [],
"cell_type": "code",
"source": [
"#"
"x,y = 0:0.1:pi, -pi:0.1:pi\n",
"sin.(x) .+ cos.(y')"
],
"metadata": {},
"execution_count": null
Expand Down Expand Up @@ -1268,7 +1297,7 @@
"outputs": [],
"cell_type": "code",
"source": [
"#"
"map(x -> sin(x) + cos(x), 1:10)"
],
"metadata": {},
"execution_count": null
Expand Down
21 changes: 11 additions & 10 deletions slide-notebooks/notebooks/l2_1-physics.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@
"```julia\n",
"# array initialisation\n",
"C = @. 0.5cos(9π*xc/lx)+0.5; C_i = copy(C)\n",
"qx = zeros(Float64, nx) # won't work\n",
"qx = zeros(Float64, nx-1)\n",
"```"
],
"metadata": {
Expand Down Expand Up @@ -341,7 +341,7 @@
"xc = LinRange(dx/2,lx-dx/2,nx)\n",
"# array initialisation\n",
"C = @. 0.5cos(9π*xc/lx)+0.5; C_i = copy(C)\n",
"qx = zeros(Float64, nx) # won't work"
"qx = zeros(Float64, nx-1)"
],
"metadata": {},
"execution_count": null
Expand All @@ -364,8 +364,8 @@
"source": [
"# time loop\n",
"for it = 1:nt\n",
" #qx .= # add solution\n",
" #C[2:end-1] .-= # add solution\n",
" qx .= .-dc.*diff(C )./dx\n",
" C[2:end-1] .-= dt.*diff(qx)./dx\n",
" # visualisation\n",
"end"
],
Expand Down Expand Up @@ -698,7 +698,7 @@
"ρ,β = 1.0,1.0\n",
"\n",
"# array initialisation\n",
"#Pr = exp.(...)\n",
"Pr = @. exp(-(xc - lx/4)^2)\n",
"```"
],
"metadata": {
Expand Down Expand Up @@ -728,8 +728,8 @@
"Should be modified to account for pressure `Pr` instead of concentration `C`, the velocity update (`Vx`) added, and the coefficients modified:\n",
"\n",
"```julia\n",
"Vx .-= ...\n",
"Pr[2:end-1] .-= ...\n",
"Vx .-= dt./ρ.*diff(Pr)./dx\n",
"Pr[2:end-1] .-= dt./β.*diff(Vx)./dx\n",
"```"
],
"metadata": {
Expand Down Expand Up @@ -876,7 +876,7 @@
"In the `# array initialisation` section, initialise the quantity `C` as a Gaussian profile of amplitude 1, standard deviation 1, with centre located at $c = 0.4 l_x$.\n",
"\n",
"```julia\n",
"C = exp.( ... )\n",
"C = @. exp(-(xc - lx/4)^2)\n",
"```"
],
"metadata": {
Expand Down Expand Up @@ -970,7 +970,7 @@
"nt = nx\n",
"xc = LinRange(dx/2,lx-dx/2,nx)\n",
"# array initialisation\n",
"#C = @. exp(...); C_i = copy(C)"
"C = @. exp(-(xc-lx/4)^2); C_i = copy(C)"
],
"metadata": {},
"execution_count": null
Expand All @@ -993,7 +993,8 @@
"source": [
"# time loop\n",
"@gif for it = 1:nt\n",
" #C .-= ...\n",
" C[2:end ] .-= dt.*max(vx,0.0).*diff(C)./dx\n",
" C[1:end-1] .-= dt.*min(vx,0.0).*diff(C)./dx\n",
" (it % (nt÷2) == 0) && (vx = -vx) # change the direction of wave propagation\n",
" plot(xc,[C_i,C];xlims=(0,lx), ylims=(-0.1,1.1),\n",
" xlabel=\"lx\", ylabel=\"Concentration\",\n",
Expand Down
11 changes: 6 additions & 5 deletions slide-notebooks/notebooks/l3_1-physics.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@
"\n",
"```julia\n",
"for it = 1:nt\n",
" #qx .-= ...\n",
" qx .-= dt./(ρ*dc + dt).*(qx + dc.*diff(C)./dx)\n",
" C[2:end-1] .-= dt.*diff(qx)./dx\n",
" ...\n",
"end\n",
Expand Down Expand Up @@ -838,7 +838,7 @@
"iter = 1; err = 2ϵtol; iter_evo = Float64[]; err_evo = Float64[]\n",
"while err >= ϵtol && iter <= maxiter\n",
" ...\n",
" # C[2:end-1] .-= ...\n",
" C[2:end-1] .-= dτ./(1 + dτ/ξ) .*((C[2:end-1] .- C_eq)./ξ .+ diff(qx)./dx)\n",
" ...\n",
"end\n",
"```"
Expand Down Expand Up @@ -978,9 +978,10 @@
"\n",
"```julia\n",
"while err >= ϵtol && iter <= maxiter\n",
" # qx .-= ...\n",
" # qy .-= ...\n",
" # C[2:end-1,2:end-1] .-= ...\n",
" qx .-= dτ./(ρ + dτ/dc).*(qx./dc .+ diff(C,dims=1)./dx)\n",
" qy .-= dτ./(ρ + dτ/dc).*(qy./dc .+ diff(C,dims=2)./dy)\n",
" C[2:end-1,2:end-1] .-= dτ./(1 + dτ/ξ) .*((C[2:end-1,2:end-1] .- C_eq)./ξ .+ diff(qx[:,2:end-1],dims=1)./dx .+\n",
" diff(qy[2:end-1,:],dims=2)./dy)\n",
" ...\n",
"end\n",
"```"
Expand Down
Loading

0 comments on commit 2f370c9

Please sign in to comment.