Skip to content
This repository was archived by the owner on Jun 24, 2022. It is now read-only.

Commit 70f7833

Browse files
Merge remote-tracking branch 'origin/master'
2 parents c753ff9 + a74e25a commit 70f7833

File tree

4 files changed

+85
-5
lines changed

4 files changed

+85
-5
lines changed

README.md

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,84 @@
77
[![Coverage Status](https://coveralls.io/repos/JuliaDiffEq/DiffEqApproxFun.jl/badge.svg?branch=master&service=github)](https://coveralls.io/github/JuliaDiffEq/DiffEqApproxFun.jl?branch=master)
88
[![codecov.io](http://codecov.io/github/JuliaDiffEq/DiffEqApproxFun.jl/coverage.svg?branch=master)](http://codecov.io/github/JuliaDiffEq/DiffEqApproxFun.jl?branch=master)
99

10-
[![DiffEqApproxFun](http://pkg.julialang.org/badges/DiffEqApproxFun_0.5.svg)](http://pkg.julialang.org/?pkg=DiffEqApproxFun)
1110
[![DiffEqApproxFun](http://pkg.julialang.org/badges/DiffEqApproxFun_0.6.svg)](http://pkg.julialang.org/?pkg=DiffEqApproxFun)
1211

1312
DiffEqApproxFun.jl is a component package in the DifferentialEquations ecosystem. It holds the components for solving differential equations using spectral methods defined by types from ApproxFun.jl. Users interested in using this functionality should check out [DifferentialEquations.jl](https://github.com/JuliaDiffEq/DifferentialEquations.jl).
13+
14+
## Usage Examples
15+
16+
### Indirect ApproxFuns
17+
18+
The indirect ApproxFun interface allows one to define an `ODEProblem` direction from ApproxFun expressions. It will automatically convert this into a spectral ODE problem where the vector is the spectral coefficients. The pro for this method is that it can be used with any ODE solver on the common interface such as Sundials. However, this method uses a constant number of coefficients and truncates the expansions given by ApproxFun to always match this size and so it's a little bit wasteful. But this presents itself as one of the easiest ways to solve a spectral discretization of a PDE.
19+
20+
To define such a problem, we first need to define our inital condition as a `Fun` type:
21+
22+
```julia
23+
using DiffEqApproxFun
24+
S=Fourier()
25+
u0=Fun->cos(cos-0.1))-cos(cos(0-0.1)),S)
26+
```
27+
28+
Now let's define our ODE which takes in `Fun` types and spits out new `Fun`s:
29+
30+
```julia
31+
c=Fun(cos,S)
32+
ode_prob = ODEProblem((t,u)->u''+(c+1)*u',u0,(0.,1.))
33+
```
34+
35+
To turn this into an indirect ApproxFun problem, we use the `ApproxFunProblem` wrapper:
36+
37+
```julia
38+
prob = ApproxFunProblem(ode_prob)
39+
```
40+
41+
We can now solve this using any solver:
42+
43+
```julia
44+
using OrdinaryDiffEq
45+
sol=solve(prob,Tsit5()) # OrdinaryDiffEq.jl
46+
using Sundials
47+
sol=solve(prob,CVODE_BDF()) # Sundials.jl
48+
using ODEInterfaceDiffEq
49+
sol=solve(prob,radau()) # ODEInterfaceDiffEq.jl
50+
```
51+
52+
The solution interface works on this output, so to grab the solution at the 5th timepoint we do `sol[5]` for `sol.t[5]`. Each solution is a `Fun` type, which we can evaluate. Thus we can get the value at `x=0.2` at time `sol.t[5]` via `sol[5](0.2)`. The interpolations generate their own Fun types, so the value of `x=0.2` at time `t=0.5` is calculated via `sol(0.5,0.2)`.
53+
54+
### Adding Boundary Conditions
55+
56+
Normally you will want to solve a PDE with non-trivial boundary conditions. If possible you should make your `Fun` discretization match the BCs. For example, this is done automatically when the BCs are periodic and the discretization is periodic. But this isn't always possible, in which case you will need to define a `bc` function which projects the solution at each step to satisfy the boundary conditions. For example, we can have Dirichlet conditions s.t. the solution must be zero at the endpoints of our interval in the problem above via:
57+
58+
```julia
59+
function bc(t,u)
60+
C=eye(S)[3:end,:]
61+
tmp = [Evaluation(0);
62+
Evaluation(2π);
63+
C]\[0.;0.;u]
64+
end
65+
```
66+
67+
Then we can generate a callback via `BoundaryCallback` and have the solver utilize this callback. This requires a common interface solver which is compatible with the callback interface.
68+
69+
```julia
70+
boundary_projection = BoundaryCallback(bc)
71+
sol=solve(prob,Tsit5(),callback=boundary_projection)
72+
```
73+
74+
The resulting solution satisfies the boundary conditions.
75+
76+
### Plotting
77+
78+
Note that `sol(0.5)` returns the interpolated `Fun` type at time `t=0.5`, and `Fun` types have their own plotting recipes. Thus you can plot the solution at snippets of time directly like `plot(sol(0.5))`. Example:
79+
80+
```julia
81+
plot(sol[1],labels="t=0.0")
82+
plot!(sol(0.5),labels="t=0.5")
83+
plot!(sol[end],labels="t=1.0")
84+
```
85+
86+
![Plot Example](https://user-images.githubusercontent.com/1814174/29637284-8695de9a-8808-11e7-9f01-63809f72b4a9.png)
87+
88+
## Direct ApproxFuns
89+
90+
The direct ApproxFun interface is simply using a `Fun` as an initial condition and a function on `Fun` types as the function in a standard `ODEProblem`. The `*DiffEq` solvers like OrdinaryDiffEq.jl will directly handle this as an adaptive-space spectral discretization of the PDE. However, care has to be taken since this can cause the number of coefficients to grow rapidly. One may need to use an L-stable integrator and change the linear solver which is used. This is still in development.

REQUIRE

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
julia 0.5
1+
julia 0.6
22
DiffEqBase
33
ApproxFun
4+
Reexport

src/DiffEqApproxFun.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ __precompile__()
22

33
module DiffEqApproxFun
44

5-
using ApproxFun, DiffEqBase
5+
using Reexport
6+
@reexport using ApproxFun
7+
@reexport using DiffEqBase
68

79
include("problem_solution_types.jl")
810
include("boundary_handling.jl")

src/problem_solution_types.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
immutable ApproxFunProblem{P,S} <: DEProblem
1+
struct ApproxFunProblem{P,S} <: DEProblem
22
prob::P
33
space::S
44
end
@@ -9,7 +9,7 @@ function ApproxFunProblem(prob;n = ncoefficients(prob.u0))
99
return ApproxFunProblem(_prob,sp)
1010
end
1111

12-
immutable ApproxFunSolution{SOL,S}
12+
struct ApproxFunSolution{SOL,S}
1313
sol::SOL
1414
space::S
1515
end

0 commit comments

Comments
 (0)