Skip to content

Commit f0364a7

Browse files
authored
Merge pull request #304 from FourierFlows/ncc/add-doctests
Add some doctests
2 parents 9806a37 + 27e9e92 commit f0364a7

File tree

5 files changed

+80
-6
lines changed

5 files changed

+80
-6
lines changed

docs/src/diagnostics.md

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ functionality.
66
```@meta
77
DocTestSetup = quote
88
using FourierFlows
9-
using LinearAlgebra: mul!
9+
using LinearAlgebra: mul!, ldiv!
1010
nx, Lx = 32, 2.0
1111
grid = OneDGrid(nx, Lx)
1212
struct Params <: AbstractParams
@@ -28,6 +28,10 @@ DocTestSetup = quote
2828
prob = FourierFlows.Problem(equation, stepper, dt, grid, vars, params)
2929
u0 = @. cos(π * grid.x)
3030
mul!(prob.sol, grid.rfftplan, u0)
31+
function energy(prob)
32+
ldiv!(prob.vars.u, grid.rfftplan, prob.sol)
33+
return sum(prob.vars.u.^2) * prob.grid.dx
34+
end
3135
end
3236
```
3337

@@ -36,7 +40,6 @@ using FourierFlows, Plots
3640
3741
using LinearAlgebra: mul!
3842
39-
Plots.scalefontsizes(1.25)
4043
Plots.default(lw=3)
4144
4245
nx, Lx = 32, 2.0
@@ -101,8 +104,22 @@ and then we create a [`Diagnostic`](@ref) using the
101104
[`Diagnostic`](@ref Diagnostic(calc, prob; freq=1, nsteps=100, ndata=ceil(Int, (nsteps+1)/freq)))
102105
constructor. Say we want to save energy every 2 time-steps, then:
103106

104-
```@example 3
107+
```jldoctest; output = false
105108
E = Diagnostic(energy, prob, freq=2, nsteps=200)
109+
110+
# output
111+
112+
Diagnostic
113+
├─── calc: energy
114+
├─── prob: FourierFlows.Problem{DataType, Vector{ComplexF64}, Float64, Vector{Float64}}
115+
├─── data: 101-element Vector{Float64}
116+
├────── t: 101-element Vector{Float64}
117+
├── steps: 101-element Vector{Int64}
118+
├─── freq: 2
119+
└────── i: 1
120+
```
121+
```@example 3
122+
E = Diagnostic(energy, prob, freq=2, nsteps=200) #hide
106123
```
107124

108125
Now, when we step forward the problem we provide the diagnostic as the second positional

docs/src/output.md

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,42 @@ To save output we use [`Output`](@ref). Let's see how we can use the example dev
44
in [Problem](@ref problem_docs) and [Diagnostics](@ref diagnostics_docs) sections to
55
demonstrate how we can save some output to disk and then load it.
66

7+
```@meta
8+
DocTestSetup = quote
9+
using FourierFlows
10+
using LinearAlgebra: mul!, ldiv!
11+
nx, Lx = 32, 2.0
12+
grid = OneDGrid(nx, Lx)
13+
struct Params <: AbstractParams
14+
α :: Float64
15+
end
16+
params = Params(0.1)
17+
struct Vars <: AbstractVars
18+
u :: Array{Float64,1}
19+
uh :: Array{Complex{Float64}, 1}
20+
end
21+
vars = Vars(zeros(Float64, (grid.nx,)), zeros(Complex{Float64}, (grid.nkr,)))
22+
L = - params.α * ones(grid.nkr)
23+
function calcN!(N, sol, t, clock, vars, params, grid)
24+
@. N = 0
25+
return nothing
26+
end
27+
equation = FourierFlows.Equation(L, calcN!, grid)
28+
stepper, dt = "ForwardEuler", 0.02
29+
prob = FourierFlows.Problem(equation, stepper, dt, grid, vars, params)
30+
u0 = @. cos(π * grid.x)
31+
mul!(prob.sol, grid.rfftplan, u0)
32+
function energy(prob)
33+
ldiv!(prob.vars.u, grid.rfftplan, prob.sol)
34+
return sum(prob.vars.u.^2) * prob.grid.dx
35+
end
36+
E = Diagnostic(energy, prob, freq=2, nsteps=200)
37+
filepath = "."
38+
filename = joinpath(filepath, "simplestpde.jld2")
39+
get_uh(prob) = prob.sol
40+
end
41+
```
42+
743
```@setup 4
844
using FourierFlows, Plots
945
@@ -68,10 +104,23 @@ that we want to output and a function what takes `prob` as its argument and
68104
returns the corresponding value of that field. For this example, let's save
69105
the energy `E` and the state vector `sol`.
70106

71-
```@example 4
107+
```jldoctest; output = false, filter = r"path:.*"
72108
get_uh(prob) = prob.sol
73109
74110
out = Output(prob, filename, (:uh, get_uh), (:E, energy))
111+
112+
# output
113+
114+
Output
115+
├──── prob: FourierFlows.Problem{DataType, Vector{ComplexF64}, Float64, Vector{Float64}}
116+
├──── path: ./simplestpde.jld2
117+
└── fields: Dict{Symbol, Function}(:uh => get_uh, :E => energy)
118+
```
119+
120+
```@example 4
121+
get_uh(prob) = prob.sol #hide
122+
123+
out = Output(prob, filename, (:uh, get_uh), (:E, energy)) #hide
75124
```
76125

77126
Note that we haven't saved anything to disk yet!

src/diagnostics.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,3 @@ show(io::IO, d::Diagnostic{T, N}) where {T, N} =
124124
" ├── steps: ", summary(d.steps), '\n',
125125
" ├─── freq: ", d.freq, '\n',
126126
" └────── i: ", d.i)
127-

src/output.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,9 @@ function savediagnostic(diag, diagname, filename)
158158

159159
return nothing
160160
end
161+
162+
show(io::IO, out::Output) =
163+
print(io, "Output\n",
164+
" ├──── prob: ", summary(out.prob), '\n',
165+
" ├──── path: ", out.path, '\n',
166+
" └── fields: ", out.fields)

test/runtests.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ for dev in devices
267267

268268
g = OneDGrid(dev, nx, Lx)
269269
σ = 0.5
270-
f1 = @. exp(-g.x^2/(2σ^2))
270+
f1 = @. exp(-g.x^2 / 2σ^2)
271271
@test test_parsevalsum2(f1, g; realvalued=true) # Real valued f with rfft
272272
@test test_parsevalsum2(f1, g; realvalued=false) # Real valued f with fft
273273

@@ -355,16 +355,19 @@ for dev in devices
355355

356356
get_sol(prob) = prob.sol
357357
diag = Diagnostic(get_sol, prob)
358+
out = Output(prob, "output.jld2")
358359

359360
@test repr(prob1.params) == "Parameters\n ├───── parameter: κ1 -> Float64\n ├───── parameter: κ2 -> Float64\n └───── parameter: func -> Function\n"
360361
@test repr(prob2.params) == "Parameters\n ├───── parameter: κ1 -> Float64\n ├───── parameter: func -> Function\n └───── parameter: κ2 -> Float64\n"
361362

362363
if dev == CPU()
363364
@test repr(prob.vars) == "Variables\n ├───── variable: c -> 128-element Vector{Float64}\n ├───── variable: cx -> 128-element Vector{Float64}\n ├───── variable: ch -> 65-element Vector{ComplexF64}\n └───── variable: cxh -> 65-element Vector{ComplexF64}\n"
364365
@test repr(diag) == "Diagnostic\n ├─── calc: get_sol\n ├─── prob: FourierFlows.Problem{DataType, Vector{ComplexF64}, Float64, Vector{Int64}}\n ├─── data: 101-element Vector{Vector{ComplexF64}}\n ├────── t: 101-element Vector{Float64}\n ├── steps: 101-element Vector{Int64}\n ├─── freq: 1\n └────── i: 1"
366+
@test repr(out) == "Output\n ├──── prob: FourierFlows.Problem{DataType, Vector{ComplexF64}, Float64, Vector{Int64}}\n ├──── path: output.jld2\n └── fields: Dict{Symbol, Function}()"
365367
else
366368
@test repr(prob.vars) == "Variables\n ├───── variable: c -> 128-element " * string(ArrayType(dev)) * "{Float64, 1, CUDA.Mem.DeviceBuffer}\n ├───── variable: cx -> 128-element " * string(ArrayType(dev)) * "{Float64, 1, CUDA.Mem.DeviceBuffer}\n ├───── variable: ch -> 65-element " * string(ArrayType(dev)) * "{ComplexF64, 1, CUDA.Mem.DeviceBuffer}\n └───── variable: cxh -> 65-element " * string(ArrayType(dev)) * "{ComplexF64, 1, CUDA.Mem.DeviceBuffer}\n"
367369
@test repr(diag) == "Diagnostic\n ├─── calc: get_sol\n ├─── prob: FourierFlows.Problem{DataType, " * string(ArrayType(dev)) * "{ComplexF64, 1, CUDA.Mem.DeviceBuffer}, Float64, " * string(ArrayType(dev)) * "{Int64, 1, CUDA.Mem.DeviceBuffer}}\n ├─── data: 101-element Vector{" * string(ArrayType(dev)) * "{ComplexF64, 1, CUDA.Mem.DeviceBuffer}}\n ├────── t: 101-element Vector{Float64}\n ├── steps: 101-element Vector{Int64}\n ├─── freq: 1\n └────── i: 1"
370+
@test repr(out) == "Output\n ├──── prob: FourierFlows.Problem{DataType, CuArray{ComplexF64, 1, CUDA.Mem.DeviceBuffer}, Float64, CuArray{Int64, 1, CUDA.Mem.DeviceBuffer}}\n ├──── path: output.jld2\n └── fields: Dict{Symbol, Function}()"
368371
end
369372
@test repr(prob.eqn) == "Equation\n ├──────── linear coefficients: L\n │ ├───type: Int64\n │ └───size: (65,)\n ├───────────── nonlinear term: calcN!()\n └─── type of state vector sol: ComplexF64"
370373
@test repr(prob.clock) == "Clock\n ├─── timestep dt: 0.01\n ├────────── step: 0\n └──────── time t: 0.0"

0 commit comments

Comments
 (0)