|
| 1 | +# # Using the an implicit solver based on Ariadne with Trixi.jl |
| 2 | + |
| 3 | +using Trixi |
| 4 | +using Theseus |
| 5 | +using CairoMakie |
| 6 | +using LinearAlgebra |
| 7 | +import Ariadne: JacobianOperator |
| 8 | + |
| 9 | + |
| 10 | +# Notes: |
| 11 | +# Must disable both Polyester and LoopVectorization for Enzyme to be able to differentiate Trixi.jl |
| 12 | +# Using https://github.com/trixi-framework/Trixi.jl/pull/2295 |
| 13 | +# |
| 14 | +# LocalPreferences.jl |
| 15 | +# ```toml |
| 16 | +# [Trixi] |
| 17 | +# loop_vectorization = false |
| 18 | +# backend = "static" |
| 19 | +# ``` |
| 20 | + |
| 21 | +@assert Trixi._PREFERENCE_THREADING !== :polyester |
| 22 | +@assert !Trixi._PREFERENCE_LOOPVECTORIZATION |
| 23 | + |
| 24 | +# ## Load Trixi Example |
| 25 | +trixi_include(@__MODULE__, joinpath(examples_dir(), "tree_2d_dgsem", "elixir_advection_basic.jl"), sol = nothing); |
| 26 | + |
| 27 | +u = copy(ode.u0) |
| 28 | +du = zero(ode.u0) |
| 29 | +res = zero(ode.u0) |
| 30 | + |
| 31 | +F! = Theseus.nonlinear_problem(Theseus.ImplicitEuler(), ode.f) |
| 32 | +J = JacobianOperator(F!, res, u, (ode.u0, 1.0, du, ode.p, 0.0, (), 1)) |
| 33 | + |
| 34 | +out = zero(u) |
| 35 | +v = zero(u) |
| 36 | + |
| 37 | +## precompile |
| 38 | +mul!(u, J, v) |
| 39 | +F!(res, u, (ode.u0, 1.0, du, ode.p, 0.0, (), 1)) |
| 40 | + |
| 41 | +@time mul!(u, J, v) |
| 42 | +@time F!(res, u, (ode.u0, 1.0, du, ode.p, 0.0, (), 1)) |
| 43 | + |
| 44 | +# Cost of time(mul!) ≈ 2 * time(F!) |
| 45 | + |
| 46 | +# ### Solve using ODE interface |
| 47 | + |
| 48 | +sol_trbdf2 = solve( |
| 49 | + ode, Theseus.TRBDF2(); |
| 50 | + dt = 1.0, # solve needs some value here but it will be overwritten by the stepsize_callback |
| 51 | + ode_default_options()..., callback = callbacks, |
| 52 | + ## verbose=1, |
| 53 | + krylov_algo = :gmres, |
| 54 | + ## krylov_kwargs=(;verbose=1) |
| 55 | +); |
| 56 | + |
| 57 | +# #### Plot the solution |
| 58 | + |
| 59 | +# We have to manually convert the sol since Theseus has it's own leightweight solution type. |
| 60 | + |
| 61 | +plot(Trixi.PlotData2DTriangulated(sol_trbdf2.u[end], sol_trbdf2.prob.p)) |
| 62 | + |
| 63 | +# ### Solve using OrdinaryDiffEqSDIRKSDIRK |
| 64 | + |
| 65 | +import OrdinaryDiffEqSDIRK |
| 66 | +import DifferentiationInterface: AutoFiniteDiff |
| 67 | +sol_sdrik = solve( |
| 68 | + ode, OrdinaryDiffEqSDIRK.TRBDF2(autodiff = AutoFiniteDiff()); |
| 69 | + dt = 1.0, # solve needs some value here but it will be overwritten by the stepsize_callback |
| 70 | + ode_default_options()..., callback = callbacks, |
| 71 | + adaptive = false |
| 72 | +); |
| 73 | + |
| 74 | +# #### Plot the solution |
| 75 | + |
| 76 | +plot(Trixi.PlotData2DTriangulated(sol_sdrik.u[end], sol_sdrik.prob.p)) |
| 77 | + |
| 78 | +# ## Increase CFL numbers |
| 79 | + |
| 80 | +trixi_include(@__MODULE__, joinpath(examples_dir(), "tree_2d_dgsem", "elixir_advection_basic.jl"), cfl = 10, sol = nothing); |
| 81 | + |
| 82 | +sol = solve( |
| 83 | + ode, Theseus.ImplicitEuler(); |
| 84 | + dt = 1.0, # solve needs some value here but it will be overwritten by the stepsize_callback |
| 85 | + ode_default_options()..., callback = callbacks, |
| 86 | + ## verbose=1, |
| 87 | + krylov_algo = :gmres, |
| 88 | + ## krylov_kwargs=(;verbose=1) |
| 89 | +); |
| 90 | + |
| 91 | +@show callbacks.discrete_callbacks[4] |
| 92 | + |
| 93 | +# ### Plot the solution |
| 94 | + |
| 95 | +plot(Trixi.PlotData2DTriangulated(sol.u[end], sol.prob.p)) |
0 commit comments