Skip to content

Commit c46997c

Browse files
authored
split test and doc code (#817)
1 parent 7c4bcf2 commit c46997c

File tree

12 files changed

+180
-157
lines changed

12 files changed

+180
-157
lines changed

Project.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ DomainSets = "0.3, 0.4, 0.5"
3030
DualNumbers = "0.6.2"
3131
FFTW = "1"
3232
FastTransforms = "0.13, 0.14"
33-
Plots = "1"
3433
RecipesBase = "1.0"
3534
Reexport = "1.0"
3635
SpecialFunctions = "1.1, 2"
@@ -39,9 +38,8 @@ julia = "1.6"
3938
[extras]
4039
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
4140
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
42-
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
4341
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
4442
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
4543

4644
[targets]
47-
test = ["Aqua", "Documenter", "Plots", "Random", "Test"]
45+
test = ["Aqua", "Documenter", "Random", "Test"]

docs/make.jl

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,57 @@ using Literate
33

44
# Generate examples using Literate
55
# See https://github.com/fredrikekre/Literate.jl/blob/master/docs/make.jl
6-
example_dir = joinpath(@__DIR__, "..", "examples")
7-
output_dir = joinpath(@__DIR__, "src/generated")
6+
const example_dir = joinpath(@__DIR__, "..", "examples")
7+
const output_dir = joinpath(@__DIR__, "src/generated")
88

9-
for example in ["ODE.jl", "PDE.jl", "Sampling.jl", "Periodic.jl",
10-
"Eigenvalue.jl", "NonlinearBVP.jl"]
9+
function replace_includes(str, included)
10+
for ex in included
11+
content = read(joinpath(example_dir, ex), String)
12+
str = replace(str, "include(\"$(ex)\")" => content)
13+
end
14+
return str
15+
end
16+
17+
for (example, included) in [
18+
("ODE.jl", ["ODE_BVP.jl", "ODE_increaseprec.jl"]),
19+
("PDE.jl", ["PDE1.jl"]),
20+
("Sampling.jl", String[]),
21+
("Periodic.jl", ["Periodic1.jl"]),
22+
("Eigenvalue.jl", String[]),
23+
("NonlinearBVP.jl", ["NonlinearBVP1.jl"])
24+
]
1125
filename = joinpath(example_dir, example)
12-
Literate.markdown(filename, output_dir, documenter=true)
26+
Literate.markdown(filename, output_dir, documenter=true,
27+
preprocess = str -> replace_includes(str, included))
1328
end
1429

1530
makedocs(
16-
doctest = false,
17-
clean = true,
18-
format = Documenter.HTML(),
19-
sitename = "ApproxFun.jl",
20-
authors = "Sheehan Olver",
21-
pages = Any[
22-
"Home" => "index.md",
23-
"Usage" => Any[
24-
"Domains" => "usage/domains.md",
25-
"Spaces" => "usage/spaces.md",
26-
"Constructors" => "usage/constructors.md",
27-
"Operators" => "usage/operators.md",
28-
"Linear Equations" => "usage/equations.md"
29-
],
30-
"Examples" => [
31-
"generated/ODE.md",
32-
"generated/PDE.md",
33-
"generated/Sampling.md",
34-
"generated/Periodic.md",
35-
"generated/Eigenvalue.md",
36-
"generated/NonlinearBVP.md",
37-
],
38-
"Frequently Asked Questions" => "faq.md",
39-
"Library" => "library.md"
40-
]
41-
)
31+
doctest = false,
32+
clean = true,
33+
format = Documenter.HTML(),
34+
sitename = "ApproxFun.jl",
35+
authors = "Sheehan Olver",
36+
pages = Any[
37+
"Home" => "index.md",
38+
"Usage" => Any[
39+
"Domains" => "usage/domains.md",
40+
"Spaces" => "usage/spaces.md",
41+
"Constructors" => "usage/constructors.md",
42+
"Operators" => "usage/operators.md",
43+
"Linear Equations" => "usage/equations.md"
44+
],
45+
"Examples" => [
46+
"generated/ODE.md",
47+
"generated/PDE.md",
48+
"generated/Sampling.md",
49+
"generated/Periodic.md",
50+
"generated/Eigenvalue.md",
51+
"generated/NonlinearBVP.md",
52+
],
53+
"Frequently Asked Questions" => "faq.md",
54+
"Library" => "library.md"
55+
]
56+
)
4257

4358

4459
deploydocs(

examples/NonlinearBVP.jl

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,12 @@
1010
# ```math
1111
# Nu = [u(-1)-1,\,u(1)+0.5,\,Du] = [0,0,0].
1212
# ```
13-
14-
using ApproxFun
15-
using LinearAlgebra
16-
17-
# Define the vector that collates the differential equation and
18-
# the boundary conditions
19-
N(u, x = Fun()) = [u(-1.)-1., u(1.)+0.5, 0.001u'' + 6(1-x^2)u' + u^2 - 1];
20-
21-
# Solve the equation using Newton iteration
22-
function nbvpsolver()
23-
x = Fun()
24-
u0 = 0 * x # starting value
25-
26-
newton(N, u0)
27-
end
28-
29-
u = nbvpsolver();
13+
include("NonlinearBVP1.jl")
3014

3115
# We plot the solution
3216
import Plots
3317
Plots.plot(u; title = "Solution", xlabel="x", ylabel="u(x)", legend=false)
3418

35-
#src # We verify that the solution satisfies the differential equation and the boundary conditions
36-
using Test #src
37-
@test norm(N(u)) 1000eps() #src
38-
3919
# ## System of nonlinear differential equations
4020
# One can also solve a system of nonlinear ODEs with potentially nonlinear boundary conditions:
4121
N(u1, u2) = [u1'(0) - 0.5*u1(0)*u2(0);

examples/NonlinearBVP1.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using ApproxFun
2+
using LinearAlgebra
3+
4+
# Define the vector that collates the differential equation and
5+
# the boundary conditions
6+
N(u, x = Fun()) = [u(-1.)-1., u(1.)+0.5, 0.001u'' + 6(1-x^2)u' + u^2 - 1];
7+
8+
# Solve the equation using Newton iteration
9+
function nbvpsolver()
10+
x = Fun()
11+
u0 = 0 * x # starting value
12+
13+
newton(N, u0)
14+
end
15+
16+
u = nbvpsolver();
17+
18+
#src # We verify that the solution satisfies the differential equation and the boundary conditions
19+
using Test #src
20+
@test norm(N(u)) 1000eps() #src

examples/ODE.jl

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,47 +11,18 @@
1111

1212
using ApproxFun
1313

14-
# Construct the domain
15-
a, b = -20, 10
16-
d = a..b;
17-
18-
# We construct the Airy differential operator ``L = d^2/dx^2 - x``:
19-
x = Fun(d);
20-
D = Derivative(d);
21-
L = D^2 - x;
22-
23-
# We impose boundary conditions by setting the function at
24-
# the boundaries equal to the values of the Airy function at these points.
25-
# First, we construct the Dirichlet boundary condition operator,
26-
# that evaluates the function at the boundaries
27-
B = Dirichlet(d);
28-
29-
# The right hand side for the boundary condition is obtained by evaluating the Airy function
30-
# We use `airyai` from `SpecialFunctions.jl` for this
31-
using SpecialFunctions
32-
B_vals = [airyai(a), airyai(b)];
33-
34-
# The main step, where we solve the differential equation
35-
u = [B; L] \ [B_vals, 0];
14+
include("ODE_BVP.jl")
3615

3716
# We plot the solution
3817
import Plots
3918
Plots.plot(u, xlabel="x", ylabel="u(x)", legend=false)
4019

41-
#src # Finally, we compare the result with the expected solution (which is an Airy function)
42-
using Test #src
43-
@test (u(0), airyai(0); atol=10000eps()) #src
4420

4521
# ## Increasing Precision
4622

4723
# Solving differential equations with high precision types is available.
4824
# The following calculates ``e`` to 300 digits by solving the ODE ``u^\prime = u``:
4925

50-
u = setprecision(1000) do
51-
d = BigFloat(0)..BigFloat(1)
52-
D = Derivative(d)
53-
[ldirichlet(); D-1] \ [1; 0]
54-
end
55-
Plots.plot(u; legend=false, xlabel="x", ylabel="u(x)")
26+
include("ODE_increaseprec.jl")
5627

57-
@test u(1) exp(BigFloat(1)) #src
28+
Plots.plot(u; legend=false, xlabel="x", ylabel="u(x)")

examples/ODE_BVP.jl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using ApproxFun #src
2+
# Construct the domain
3+
a, b = -20, 10
4+
d = a..b;
5+
6+
# We construct the Airy differential operator ``L = d^2/dx^2 - x``:
7+
x = Fun(d);
8+
D = Derivative(d);
9+
L = D^2 - x;
10+
11+
# We impose boundary conditions by setting the function at
12+
# the boundaries equal to the values of the Airy function at these points.
13+
# First, we construct the Dirichlet boundary condition operator,
14+
# that evaluates the function at the boundaries
15+
B = Dirichlet(d);
16+
17+
# The right hand side for the boundary condition is obtained by evaluating the Airy function
18+
# We use `airyai` from `SpecialFunctions.jl` for this
19+
using SpecialFunctions
20+
B_vals = [airyai(a), airyai(b)];
21+
22+
# The main step, where we solve the differential equation
23+
u = [B; L] \ [B_vals, 0];
24+
25+
#src we compare the result with the expected solution (which is an Airy function)
26+
using Test #src
27+
@test (u(0), airyai(0); atol=10000eps()) #src

examples/ODE_increaseprec.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using ApproxFun #src
2+
u = setprecision(1000) do
3+
d = BigFloat(0)..BigFloat(1)
4+
D = Derivative(d)
5+
[ldirichlet(); D-1] \ [1; 0]
6+
end
7+
using Test #src
8+
@test u(1) exp(BigFloat(1)) #src

examples/PDE.jl

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,8 @@
77
# on the rectangle `-1..1 × -1..1`,
88
# subject to the condition that ``y=1`` on the boundary.
99

10-
using ApproxFun
11-
using LinearAlgebra
12-
13-
# The rectangular domain may be expressed as a tensor product of `ChebyshevInterval`s
14-
d = ChebyshevInterval()^2;
15-
16-
# We construct the operator that collates the differential operator and boundary conditions
17-
L = [Dirichlet(d); Laplacian()+100I];
18-
19-
# We compute the QR decomposition of the operator to speed up the solution
20-
Q = qr(L);
21-
ApproxFun.resizedata!(Q,:,4000);
22-
23-
# The boundary condition is a function that is equal to one on each edge
24-
boundary_cond = ones((d));
25-
26-
# Solve the differential equation.
27-
# This function has weak singularities at the corner,
28-
# so we specify a lower tolerance to avoid resolving these singularities completely
29-
u = \(Q, [boundary_cond; 0.]; tolerance=1E-7);
10+
include("PDE1.jl")
3011

3112
# Plot the solution
3213
import Plots
3314
Plots.plot(u; st=:surface, legend=false)
34-
35-
#src # We verify that the solution satisfies the boundary condition
36-
using Test #src
37-
@test u(0.1,1.) 1.0 #src
38-
39-
#src # We validate the solution at an internal point
40-
@test (u(0.1,0.2), -0.02768276827514463; atol=1E-8) #src

examples/PDE1.jl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using ApproxFun
2+
using LinearAlgebra
3+
4+
# The rectangular domain may be expressed as a tensor product of `ChebyshevInterval`s
5+
d = ChebyshevInterval()^2;
6+
7+
# We construct the operator that collates the differential operator and boundary conditions
8+
L = [Dirichlet(d); Laplacian()+100I];
9+
10+
# We compute the QR decomposition of the operator to speed up the solution
11+
Q = qr(L);
12+
ApproxFun.resizedata!(Q,:,4000);
13+
14+
# The boundary condition is a function that is equal to one on each edge
15+
boundary_cond = ones((d));
16+
17+
# Solve the differential equation.
18+
# This function has weak singularities at the corner,
19+
# so we specify a lower tolerance to avoid resolving these singularities completely
20+
u = \(Q, [boundary_cond; 0.]; tolerance=1E-7);
21+
22+
#src # We verify that the solution satisfies the boundary condition
23+
using Test #src
24+
@test u(0.1,1.) 1.0 #src
25+
26+
#src # We validate the solution at an internal point
27+
@test (u(0.1,0.2), -0.02768276827514463; atol=1E-8) #src

examples/Periodic.jl

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,5 @@
11
# # Periodic Domains
2-
using ApproxFun
3-
using LinearAlgebra
4-
5-
# We may approximate a periodic function in `Fourier` space
6-
f = Fun(cos, Fourier(-π..π))
7-
norm(differentiate(f) + Fun(sin, Fourier(-π..π))) < 100eps()
8-
9-
# ## Boundary Value Problems
10-
11-
# Due to the periodicity, Fourier representations allow for the asymptotic
12-
# savings of 2/π in the number of coefficients that need
13-
# to be stored compared with a Chebyshev representation.
14-
15-
# ODEs can also be solved when the solution is periodic.
16-
17-
# In this example, we solve
18-
# ```math
19-
# \frac{dy}{dt} + (1 + \sin(\cos(2t)))y = \exp(\sin(10t))
20-
# ```
21-
# subject to periodic boundary conditions on the domain ``[-\pi,\,\pi]``.
22-
23-
# Solve the differential equation in Chebyshev space
24-
s = Chebyshev(-π..π)
25-
a = Fun(t-> 1+sin(cos(2t)),s)
26-
L = Derivative() + a
27-
f = Fun(t->exp(sin(10t)),s)
28-
B = periodic(s,0)
29-
uChebyshev = [B;L]\[0.,f]
30-
ncoefficients(uChebyshev)
31-
32-
# Solve the differential equation in Fourier space
33-
s = Fourier(-π..π)
34-
a = Fun(t-> 1+sin(cos(2t)),s)
35-
L = Derivative() + a
36-
f = Fun(t->exp(sin(10t)),s)
37-
uFourier = L\f
38-
ncoefficients(uFourier)
2+
include("Periodic1.jl")
393

404
# We note that the number of coefficients in the Fourier space is lower than that
415
# in the Chebyshev space by a factor of approximately π/2
@@ -45,5 +9,3 @@ ncoefficients(uFourier)/ncoefficients(uChebyshev),2/π
459
import Plots
4610
Plots.plot(uFourier, xlims=(-pi, pi), legend=false)
4711

48-
using Test #src
49-
@test uChebyshev(0.) uFourier(0.) #src

0 commit comments

Comments
 (0)