Skip to content

Commit

Permalink
fix: minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
agdestein committed Nov 4, 2024
1 parent 003f739 commit ab84238
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 11 deletions.
10 changes: 5 additions & 5 deletions docs/src/manual/differentiability.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ IncompressibleNavierStokes is
which means that you can back-propagate gradients through the code.
This comes at a cost however, as intermediate velocity fields need to be stored
in memory for use in the backward pass. For this reason, many of the operators
come in two versions:oa slow differentiable allocating non-mutating variant (e.g.
come in two versions: a slow differentiable allocating non-mutating variant (e.g.
[`divergence`](@ref)) and fast non-differentiable non-allocating mutating
variant (e.g. [`divergence!`](@ref).)

Expand All @@ -26,7 +26,8 @@ make a time stepping loop composed of differentiable operations:

```julia
import IncompressibleNavierStokes as INS
setup = INS.Setup(; x = (0:0.01:1, 0:0.01:1), Re = 500.0)
ax = range(0, 1, 101)
setup = INS.Setup(; x = (ax, ax), Re = 500.0)
psolver = INS.default_psolver(setup)
method = INS.RKMethods.RK44P2()
Δt = 0.01
Expand All @@ -38,16 +39,15 @@ function final_energy(u)
stepper = INS.timestep(method, stepper, Δt)
end
(; u) = stepper
sum(abs2, u[1][Iu[1]]) / 2 + sum(abs2, u[2][Iu[2]]) / 2
sum(abs2, u[Iu[1], 1]) / 2 + sum(abs2, u[Iu[2], 2]) / 2
end

u = INS.random_field(setup)

using Zygote
g, = Zygote.gradient(final_energy, u)

@show size.(u)
@show size.(g)
@show size(u) size(g)
```

Now `g` is the gradient of `final_energy` with respect to the initial conditions
Expand Down
2 changes: 1 addition & 1 deletion examples/Kolmogorov2D.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ ustart = random_field(setup, 0.0; A = 1e-2);
#
# Since the force is steady, it is just stored as a field.

heatmap(setup.bodyforce[1])
heatmap(setup.bodyforce[:, :, 1])

# ## Solve unsteady problem

Expand Down
2 changes: 1 addition & 1 deletion src/initializers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ function random_field(
all(==((PeriodicBC(), PeriodicBC())), boundary_conditions),
"Random field requires periodic boundary conditions."
)
@assert all-> all((0), diff(Δ)), Δ) "Random field requires uniform grid spacing."
@assert all-> all((Δ[1]), Δ), Δ) "Random field requires uniform grid spacing."

Check warning on line 205 in src/initializers.jl

View check run for this annotation

Codecov / codecov/patch

src/initializers.jl#L205

Added line #L205 was not covered by tests
@assert all(iseven, N) "Random field requires even number of volumes."

# Create random velocity field
Expand Down
3 changes: 1 addition & 2 deletions src/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1157,8 +1157,7 @@ function interpolate_u_p!(up, u, setup)
up[I, α] = (u[I-e(α), α] + u[I, α]) / 2
end
for α = 1:D
I0 = first(Ip)
I0 -= oneunit(I0)
I0 = getoffset(Ip)
int!(backend, workgroupsize)(up, u, Val(α), I0; ndrange = Np)
end
up
Expand Down
6 changes: 4 additions & 2 deletions src/processors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,11 @@ function observefield(
interpolate_u_p!(up, u, setup)
# map((u, v, w) -> √sum(u^2 + v^2 + w^2), up...)
if D == 2
@. upnorm = sqrt(up[1]^2 + up[2]^2)
uptuple = eachslice(up; dims = ndims(up))
@. upnorm = sqrt(uptuple[1]^2 + uptuple[2]^2)

Check warning on line 153 in src/processors.jl

View check run for this annotation

Codecov / codecov/patch

src/processors.jl#L152-L153

Added lines #L152 - L153 were not covered by tests
elseif D == 3
@. upnorm = sqrt(up[1]^2 + up[2]^2 + up[3]^2)
uptuple = eachslice(up; dims = ndims(up))
@. upnorm = sqrt(uptuple[1]^2 + uptuple[2]^2 + uptuple[3]^2)

Check warning on line 156 in src/processors.jl

View check run for this annotation

Codecov / codecov/patch

src/processors.jl#L155-L156

Added lines #L155 - L156 were not covered by tests
end
elseif fieldname == :vorticity
apply_bc_u!(u, t, setup)
Expand Down

0 comments on commit ab84238

Please sign in to comment.