Skip to content

Commit

Permalink
Lint code and add JuliaFormatter GH action
Browse files Browse the repository at this point in the history
  • Loading branch information
michel2323 committed Apr 17, 2024
1 parent 5eeaeb7 commit 6f51478
Show file tree
Hide file tree
Showing 19 changed files with 660 additions and 475 deletions.
Empty file added .github/workflows/format.yml
Empty file.
8 changes: 4 additions & 4 deletions docs/make.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Pkg

checkpointingspec = PackageSpec(path=joinpath(dirname(@__FILE__), ".."))
checkpointingspec = PackageSpec(path = joinpath(dirname(@__FILE__), ".."))
Pkg.develop(checkpointingspec)

# when first running instantiate
Expand All @@ -13,7 +13,7 @@ makedocs(
sitename = "Checkpointing.jl",
format = Documenter.HTML(
prettyurls = Base.get(ENV, "CI", nothing) == "true",
mathengine = Documenter.KaTeX()
mathengine = Documenter.KaTeX(),
),
modules = [Checkpointing],
repo = "https://github.com/Argonne-National-Laboratory/Checkpointing.jl/blob/{commit}{path}#{line}",
Expand All @@ -25,7 +25,7 @@ makedocs(
"Rules" => "rules.md",
"Storage" => "storage.md",
"API" => "lib/checkpointing.md",
]
],
)

deploydocs(
Expand All @@ -34,4 +34,4 @@ deploydocs(
devbranch = "main",
devurl = "main",
push_preview = true,
)
)
59 changes: 38 additions & 21 deletions examples/box_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,44 @@ const blength = [5000.0e5; 1000.0e5; 5000.0e5] ## north-south size of boxes, c

const bdepth = [1.0e5; 5.0e5; 4.0e5] ## depth of boxes, centimeters

const delta = bdepth[1]/(bdepth[1] + bdepth[3]) ## constant ratio of two depths
const delta = bdepth[1] / (bdepth[1] + bdepth[3]) ## constant ratio of two depths

const bwidth = 4000.0*1e5 ## box width, centimeters
const bwidth = 4000.0 * 1e5 ## box width, centimeters

# box areas
const barea = [blength[1]*bwidth;
blength[2]*bwidth;
blength[3]*bwidth]
const barea = [
blength[1] * bwidth
blength[2] * bwidth
blength[3] * bwidth
]

# box volumes
const bvol = [barea[1]*bdepth[1];
barea[2]*bdepth[2];
barea[3]*bdepth[3]]
const bvol = [
barea[1] * bdepth[1]
barea[2] * bdepth[2]
barea[3] * bdepth[3]
]

# parameters that are used to ensure units are in CGS (cent-gram-sec)

const hundred = 100.0
const thousand = 1000.0
const day = 3600.0*24.0
const year = day*365.0
const day = 3600.0 * 24.0
const year = day * 365.0
const Sv = 1e12 ## one Sverdrup (a unit of ocean transport), 1e6 meters^3/second

# parameters that appear in box model equations
const u0 = 16.0*Sv/0.0004
const u0 = 16.0 * Sv / 0.0004
const alpha = 1668e-7
const beta = 0.7811e-3

const gamma = 1/(300*day)
const gamma = 1 / (300 * day)

# robert filter coefficient for the smoother part of the timestep
const robert_filter_coeff = 0.25

# freshwater forcing
const FW = [(hundred/year) * 35.0 * barea[1]; -(hundred/year) * 35.0 * barea[1]]
const FW = [(hundred / year) * 35.0 * barea[1]; -(hundred / year) * 35.0 * barea[1]]

# restoring atmospheric temperatures
const Tstar = [22.0; 0.0]
Expand All @@ -47,7 +51,7 @@ const Sstar = [36.0; 34.0];

function U_func(dens)

U = u0*(dens[2] - (delta * dens[1] + (1 - delta)*dens[3]))
U = u0 * (dens[2] - (delta * dens[1] + (1 - delta) * dens[3]))
return U

end
Expand Down Expand Up @@ -123,9 +127,10 @@ end
function forward_func_4_AD(in_now, in_old, out_old, out_now)
rho_now = rho_func(in_now) ## compute density
u_now = U_func(rho_now) ## compute transport
in_new = timestep_func(in_now, in_old, u_now, 10*day) ## compute new state values
in_new = timestep_func(in_now, in_old, u_now, 10 * day) ## compute new state values
for j = 1:6
in_now[j] = in_now[j] + robert_filter_coeff * (in_new[j] - 2.0 * in_now[j] + in_old[j])
in_now[j] =
in_now[j] + robert_filter_coeff * (in_new[j] - 2.0 * in_now[j] + in_old[j])
end
out_old[:] = in_now
out_now[:] = in_new
Expand All @@ -138,7 +143,7 @@ function advance(box::Box)
end

function timestepper_for(box::Box, scheme::Scheme, tsteps::Int)
@checkpoint_struct scheme box for i in 1:tsteps
@checkpoint_struct scheme box for i = 1:tsteps
advance(box)
box.in_now[:] = box.out_old
box.in_old[:] = box.out_now
Expand All @@ -157,7 +162,13 @@ function box_for(scheme::Scheme, tsteps::Int, ::EnzymeTool)
dbox = Box(zeros(6), zeros(6), zeros(6), zeros(6), 0)

# Compute gradient
autodiff(Enzyme.ReverseWithPrimal, timestepper_for, Duplicated(box, dbox), Const(scheme), Const(tsteps))
autodiff(
Enzyme.ReverseWithPrimal,
timestepper_for,
Duplicated(box, dbox),
Const(scheme),
Const(tsteps),
)
return box.out_now[1], dbox.in_old
end

Expand All @@ -175,12 +186,12 @@ end


function timestepper_while(box::Box, scheme::Scheme, tsteps::Int)
box.i=1
box.i = 1
@checkpoint_struct scheme box while box.i <= tsteps
advance(box)
box.in_now[:] = box.out_old
box.in_old[:] = box.out_now
box.i = box.i+1
box.i = box.i + 1
nothing
end
return box.out_now[1]
Expand All @@ -196,7 +207,13 @@ function box_while(scheme::Scheme, tsteps::Int, ::EnzymeTool)
dbox = Box(zeros(6), zeros(6), zeros(6), zeros(6), 0)

# Compute gradient
autodiff(Enzyme.ReverseWithPrimal, timestepper_while, Duplicated(box, dbox), Const(scheme), Const(tsteps))
autodiff(
Enzyme.ReverseWithPrimal,
timestepper_while,
Duplicated(box, dbox),
Const(scheme),
Const(tsteps),
)
return box.out_now[1], dbox.in_old
end

Expand Down
48 changes: 30 additions & 18 deletions examples/heat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ function advance(heat::Heat)
last = heat.Tlast
λ = heat.λ
n = heat.n
for i in 2:(n-1)
next[i] = last[i] + λ*(last[i-1]-2*last[i]+last[i+1])
for i = 2:(n-1)
next[i] = last[i] + λ * (last[i-1] - 2 * last[i] + last[i+1])
end
return nothing
end


function sumheat_for(heat::Heat, chkpscheme::Scheme, tsteps::Int64)
# AD: Create shadow copy for derivatives
@checkpoint_struct chkpscheme heat for i in 1:tsteps
# checkpoint_struct_for(advance, heat)
@checkpoint_struct chkpscheme heat for i = 1:tsteps
# checkpoint_struct_for(advance, heat)
heat.Tlast .= heat.Tnext
advance(heat)
end
Expand All @@ -45,8 +45,8 @@ end

function heat_for(scheme::Scheme, tsteps::Int, ::EnzymeTool)
n = 100
Δx=0.1
Δt=0.001
Δx = 0.1
Δt = 0.001
# Select μ such that λ ≤ 0.5 for stability with μ = (λ*Δt)/Δx^2
λ = 0.5

Expand All @@ -56,27 +56,33 @@ function heat_for(scheme::Scheme, tsteps::Int, ::EnzymeTool)
dheat = Heat(zeros(n), zeros(n), n, λ, tsteps)

# Boundary conditions
heat.Tnext[1] = 20.0
heat.Tnext[1] = 20.0
heat.Tnext[end] = 0

# Compute gradient
autodiff(Enzyme.ReverseWithPrimal, sumheat_for, Duplicated(heat, dheat), Const(scheme), Const(tsteps))
autodiff(
Enzyme.ReverseWithPrimal,
sumheat_for,
Duplicated(heat, dheat),
Const(scheme),
Const(tsteps),
)

return heat.Tnext, dheat.Tnext[2:end-1]
end

function heat_for(scheme::Scheme, tsteps::Int, ::ZygoteTool)
n = 100
Δx=0.1
Δt=0.001
Δx = 0.1
Δt = 0.001
# Select μ such that λ ≤ 0.5 for stability with μ = (λ*Δt)/Δx^2
λ = 0.5

# Create object from struct. tsteps is not needed for a for-loop
heat = Heat(zeros(n), zeros(n), n, λ, tsteps)

# Boundary conditions
heat.Tnext[1] = 20.0
heat.Tnext[1] = 20.0
heat.Tnext[end] = 0

# Compute gradient
Expand All @@ -87,8 +93,8 @@ end

function heat_while(scheme::Scheme, tsteps::Int, ::EnzymeTool)
n = 100
Δx=0.1
Δt=0.001
Δx = 0.1
Δt = 0.001
# Select μ such that λ ≤ 0.5 for stability with μ = (λ*Δt)/Δx^2
λ = 0.5

Expand All @@ -98,27 +104,33 @@ function heat_while(scheme::Scheme, tsteps::Int, ::EnzymeTool)
dheat = Heat(zeros(n), zeros(n), n, λ, tsteps)

# Boundary conditions
heat.Tnext[1] = 20.0
heat.Tnext[1] = 20.0
heat.Tnext[end] = 0

# Compute gradient
autodiff(Enzyme.ReverseWithPrimal, sumheat_while, Duplicated(heat, dheat), Const(scheme), Const(tsteps))
autodiff(
Enzyme.ReverseWithPrimal,
sumheat_while,
Duplicated(heat, dheat),
Const(scheme),
Const(tsteps),
)

return heat.Tnext, dheat.Tnext[2:end-1]
end

function heat_while(scheme::Scheme, tsteps::Int, ::ZygoteTool)
n = 100
Δx=0.1
Δt=0.001
Δx = 0.1
Δt = 0.001
# Select μ such that λ ≤ 0.5 for stability with μ = (λ*Δt)/Δx^2
λ = 0.5

# Create object from struct
heat = Heat(zeros(n), zeros(n), n, λ, 1)

# Boundary conditions
heat.Tnext[1] = 20.0
heat.Tnext[1] = 20.0
heat.Tnext[end] = 0

# Compute gradient
Expand Down
Loading

0 comments on commit 6f51478

Please sign in to comment.