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

Commit 662a107

Browse files
Make README more informative.
1 parent 3974bc9 commit 662a107

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

README.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,78 @@
1111
[![DiffEqApproxFun](http://pkg.julialang.org/badges/DiffEqApproxFun_0.6.svg)](http://pkg.julialang.org/?pkg=DiffEqApproxFun)
1212

1313
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).
14+
15+
## Usage Examples
16+
17+
### Indirect ApproxFuns
18+
19+
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.
20+
21+
To define such a problem, we first need to define our inital condition as a `Fun` type:
22+
23+
```julia
24+
using DiffEqBase, OrdinaryDiffEq, Sundials, ApproxFun, DiffEqApproxFun
25+
S=Fourier()
26+
u0=Fun->cos(cos-0.1))-cos(cos(0-0.1)),S)
27+
```
28+
29+
Now let's define our ODE which takes in `Fun` types and spits out new `Fun`s:
30+
31+
```julia
32+
c=Fun(cos,S)
33+
ode_prob = ODEProblem((t,u)->u''+(c+1)*u',u0,(0.,1.))
34+
```
35+
36+
To turn this into an indirect ApproxFun problem, we use the `ApproxFunProblem` wrapper:
37+
38+
```julia
39+
prob = ApproxFunProblem(ode_prob)
40+
```
41+
42+
We can now solve this using any solver:
43+
44+
```julia
45+
sol=solve(prob,Tsit5()) # OrdinaryDiffEq.jl
46+
sol=solve(prob,CVODE_BDF()) # Sundials.jl
47+
sol=solve(prob,radau()) # ODEInterfaceDiffEq.jl
48+
```
49+
50+
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)`.
51+
52+
### Adding Boundary Conditions
53+
54+
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:
55+
56+
```julia
57+
function bc(t,u)
58+
C=eye(S)[3:end,:]
59+
tmp = [Evaluation(0);
60+
Evaluation(2π);
61+
C]\[0.;0.;u]
62+
end
63+
```
64+
65+
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.
66+
67+
```julia
68+
boundary_projection = BoundaryCallback(bc)
69+
sol=solve(prob,Tsit5(),callback=boundary_projection)
70+
```
71+
72+
The resulting solution satisfies the boundary conditions.
73+
74+
### Plotting
75+
76+
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:
77+
78+
```julia
79+
plot(sol[1],labels="t=0.0")
80+
plot!(sol(0.5),labels="t=0.5")
81+
plot!(sol[end],labels="t=1.0")
82+
```
83+
84+
![Plot Example](https://user-images.githubusercontent.com/1814174/29637284-8695de9a-8808-11e7-9f01-63809f72b4a9.png)
85+
86+
## Direct ApproxFuns
87+
88+
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.

0 commit comments

Comments
 (0)