|
7 | 7 | [](https://coveralls.io/github/JuliaDiffEq/DiffEqApproxFun.jl?branch=master) |
8 | 8 | [](http://codecov.io/github/JuliaDiffEq/DiffEqApproxFun.jl?branch=master) |
9 | 9 |
|
10 | | -[](http://pkg.julialang.org/?pkg=DiffEqApproxFun) |
11 | 10 | [](http://pkg.julialang.org/?pkg=DiffEqApproxFun) |
12 | 11 |
|
13 | 12 | 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 | + |
| 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. |
0 commit comments