Using the an implicit solver based on Ariadne with Trixi.jl

using Trixi
using Theseus
using CairoMakie
using LinearAlgebra
import Ariadne: JacobianOperator

Notes: Must disable both Polyester and LoopVectorization for Enzyme to be able to differentiate Trixi.jl

LocalPreferences.jl

[Trixi]
loop_vectorization = false
backend = "static"
@assert Trixi._PREFERENCE_THREADING !== :polyester
@assert !Trixi._PREFERENCE_LOOPVECTORIZATION

Load Trixi Example

trixi_include(@__MODULE__, joinpath(examples_dir(), "tree_2d_dgsem", "elixir_advection_basic.jl"), sol = nothing);

u = copy(ode.u0)
du = zero(ode.u0)
res = zero(ode.u0)

F! = Theseus.nonlinear_problem(Theseus.ImplicitEuler(), ode.f)
J = JacobianOperator(F!, res, u, (ode.u0, 1.0, du, ode.p, 0.0, (), 1))

out = zero(u)
v = zero(u)

# precompile
mul!(u, J, v)
F!(res, u, (ode.u0, 1.0, du, ode.p, 0.0, (), 1))

@time mul!(u, J, v)
@time F!(res, u, (ode.u0, 1.0, du, ode.p, 0.0, (), 1))
[ Info: You just called `trixi_include`. Julia may now compile the code, please be patient.
  0.000190 seconds (79 allocations: 7.844 KiB)
  0.000137 seconds (7 allocations: 352 bytes)

Cost of time(mul!) ≈ 2 * time(F!)

Solve using ODE interface

sol_trbdf2 = solve(
    ode, Theseus.TRBDF2();
    dt = 1.0, # solve needs some value here but it will be overwritten by the stepsize_callback
    ode_default_options()..., callback = callbacks,
    # verbose=1,
    krylov_algo = :gmres,
    # krylov_kwargs=(;verbose=1)
);

████████╗██████╗ ██╗██╗  ██╗██╗
╚══██╔══╝██╔══██╗██║╚██╗██╔╝██║
   ██║   ██████╔╝██║ ╚███╔╝ ██║
   ██║   ██╔══██╗██║ ██╔██╗ ██║
   ██║   ██║  ██║██║██╔╝ ██╗██║
   ╚═╝   ╚═╝  ╚═╝╚═╝╚═╝  ╚═╝╚═╝

┌──────────────────────────────────────────────────────────────────────────────────────────────────┐
│ SemidiscretizationHyperbolic                                                                     │
│ ════════════════════════════                                                                     │
│ #spatial dimensions: ……………………………………… 2                                                           │
│ mesh: ……………………………………………………………………………… TreeMesh{2, Trixi.SerialTree{2, Float64}} with length 341   │
│ equations: ………………………………………………………………… LinearScalarAdvectionEquation2D                             │
│ initial condition: …………………………………………… initial_condition_convergence_test                          │
│ boundary conditions: ……………………………………… Trixi.BoundaryConditionPeriodic                             │
│ source terms: ………………………………………………………… nothing                                                     │
│ solver: ………………………………………………………………………… DG                                                          │
│ total #DOFs per field: ………………………………… 4096                                                        │
└──────────────────────────────────────────────────────────────────────────────────────────────────┘

┌──────────────────────────────────────────────────────────────────────────────────────────────────┐
│ TreeMesh{2, Trixi.SerialTree{2, Float64}}                                                        │
│ ═════════════════════════════════════════                                                        │
│ center: ………………………………………………………………………… [0.0, 0.0]                                                  │
│ length: ………………………………………………………………………… 2.0                                                         │
│ periodicity: …………………………………………………………… (true, true)                                                │
│ current #cells: …………………………………………………… 341                                                         │
│ #leaf-cells: …………………………………………………………… 256                                                         │
│ current capacity: ……………………………………………… 341                                                         │
└──────────────────────────────────────────────────────────────────────────────────────────────────┘

┌──────────────────────────────────────────────────────────────────────────────────────────────────┐
│ LinearScalarAdvectionEquation2D                                                                  │
│ ═══════════════════════════════                                                                  │
│ #variables: ……………………………………………………………… 1                                                           │
│ │ variable 1: ………………………………………………………… scalar                                                      │
└──────────────────────────────────────────────────────────────────────────────────────────────────┘

┌──────────────────────────────────────────────────────────────────────────────────────────────────┐
│ DG{Float64}                                                                                      │
│ ═══════════                                                                                      │
│ basis: …………………………………………………………………………… LobattoLegendreBasis{Float64}(polydeg=3)                    │
│ mortar: ………………………………………………………………………… LobattoLegendreMortarL2{Float64}(polydeg=3)                 │
│ surface integral: ……………………………………………… SurfaceIntegralWeakForm                                     │
│ │ surface flux: …………………………………………………… FluxLaxFriedrichs(max_abs_speed)                            │
│ volume integral: ………………………………………………… VolumeIntegralWeakForm                                      │
└──────────────────────────────────────────────────────────────────────────────────────────────────┘

┌──────────────────────────────────────────────────────────────────────────────────────────────────┐
│ AnalysisCallback                                                                                 │
│ ════════════════                                                                                 │
│ interval: …………………………………………………………………… 100                                                         │
│ analyzer: …………………………………………………………………… LobattoLegendreAnalyzer{Float64}(polydeg=6)                 │
│ │ error 1: ………………………………………………………………… l2_error                                                    │
│ │ error 2: ………………………………………………………………… linf_error                                                  │
│ │ integral 1: ………………………………………………………… entropy_timederivative                                      │
│ save analysis to file: ………………………………… no                                                          │
└──────────────────────────────────────────────────────────────────────────────────────────────────┘

┌──────────────────────────────────────────────────────────────────────────────────────────────────┐
│ SaveSolutionCallback                                                                             │
│ ════════════════════                                                                             │
│ interval: …………………………………………………………………… 100                                                         │
│ solution variables: ………………………………………… cons2prim                                                   │
│ save initial solution: ………………………………… yes                                                         │
│ save final solution: ……………………………………… yes                                                         │
│ output directory: ……………………………………………… /home/runner/work/Ariadne.jl/…e.jl/docs/build/generated/out │
└──────────────────────────────────────────────────────────────────────────────────────────────────┘

┌──────────────────────────────────────────────────────────────────────────────────────────────────┐
│ StepsizeCallback                                                                                 │
│ ════════════════                                                                                 │
│ CFL Hyperbolic: …………………………………………………… Returns{Float64}(1.6)                                       │
│ CFL Parabolic: ……………………………………………………… Returns{Float64}(0.0)                                       │
│ Interval: …………………………………………………………………… 1                                                           │
└──────────────────────────────────────────────────────────────────────────────────────────────────┘

┌──────────────────────────────────────────────────────────────────────────────────────────────────┐
│ Time integration                                                                                 │
│ ════════════════                                                                                 │
│ Start time: ……………………………………………………………… 0.0                                                         │
│ Final time: ……………………………………………………………… 1.0                                                         │
│ time integrator: ………………………………………………… TRBDF2                                                      │
│ adaptive: …………………………………………………………………… false                                                       │
└──────────────────────────────────────────────────────────────────────────────────────────────────┘
┌──────────────────────────────────────────────────────────────────────────────────────────────────┐
│ Environment information                                                                          │
│ ═══════════════════════                                                                          │
│ #threads: …………………………………………………………………… 1                                                           │
│ threading backend: …………………………………………… static                                                      │
│ LoopVectorization: …………………………………………… disabled                                                    │
└──────────────────────────────────────────────────────────────────────────────────────────────────┘

────────────────────────────────────────────────────────────────────────────────────────────────────
 Simulation running 'LinearScalarAdvectionEquation2D' with DGSEM(polydeg=3)
────────────────────────────────────────────────────────────────────────────────────────────────────
 #timesteps:                  0                run time:       1.12762964e+00 s
 Δt:             1.00000000e+00                └── GC time:    0.00000000e+00 s (0.000%)
 sim. time:      0.00000000e+00 (0.000%)       time/DOF/rhs!:  2.65126343e-08 s
                                               PID:                   Inf s
 #DOFs per field:          4096                alloc'd memory:        580.559 MiB
 #elements:                 256                device memory:           0.000 MiB

 Variable:       scalar
 L2 error:       3.94120825e-06
 Linf error:     1.23670425e-05
 ∑∂S/∂U ⋅ Uₜ :  -2.19124035e-16
────────────────────────────────────────────────────────────────────────────────────────────────────


────────────────────────────────────────────────────────────────────────────────────────────────────
 Simulation running 'LinearScalarAdvectionEquation2D' with DGSEM(polydeg=3)
────────────────────────────────────────────────────────────────────────────────────────────────────
 #timesteps:                 18                run time:       3.64159017e+01 s
 Δt:             5.55555556e-02                └── GC time:    3.60155723e-01 s (0.989%)
 sim. time:      1.00000000e+00 (100.000%)     time/DOF/rhs!:  2.08566259e-08 s
                                               PID:            1.60652188e-05 s
 #DOFs per field:          4096                alloc'd memory:        438.231 MiB
 #elements:                 256                device memory:           0.000 MiB

 Variable:       scalar
 L2 error:       1.71224434e-04
 Linf error:     2.52822142e-04
 ∑∂S/∂U ⋅ Uₜ :  -1.97551223e-09
────────────────────────────────────────────────────────────────────────────────────────────────────

────────────────────────────────────────────────────────────────────────────────────
             Trixi.jl                      Time                    Allocations
                                  ───────────────────────   ────────────────────────
        Tot / % measured:              36.7s /   4.4%           1.81GiB /   1.9%

Section                   ncalls     time    %tot     avg     alloc    %tot      avg
────────────────────────────────────────────────────────────────────────────────────
I/O                            3    1.36s   83.9%   455ms   16.6MiB   47.1%  5.54MiB
  ~I/O~                        3    1.18s   72.5%   393ms   12.6MiB   35.7%  4.20MiB
  save solution                2    180ms   11.1%  90.0ms   3.41MiB    9.7%  1.71MiB
  get element variables        2   6.34ms    0.4%  3.17ms    620KiB    1.7%   310KiB
  get node variables           2    952ns    0.0%   476ns     0.00B    0.0%    0.00B
  save mesh                    2    482ns    0.0%   241ns     0.00B    0.0%    0.00B
analyze solution               2    216ms   13.3%   108ms   18.6MiB   52.9%  9.31MiB
rhs!                         533   45.4ms    2.8%  85.3μs   4.78KiB    0.0%    9.19B
  volume integral            533   30.4ms    1.9%  57.0μs     0.00B    0.0%    0.00B
  interface flux             533   4.48ms    0.3%  8.40μs     0.00B    0.0%    0.00B
  prolong2interfaces         533   3.34ms    0.2%  6.26μs     0.00B    0.0%    0.00B
  surface integral           533   3.26ms    0.2%  6.12μs     0.00B    0.0%    0.00B
  Jacobian                   533   2.76ms    0.2%  5.18μs     0.00B    0.0%    0.00B
  reset ∂u/∂t                533    550μs    0.0%  1.03μs     0.00B    0.0%    0.00B
  ~rhs!~                     533    544μs    0.0%  1.02μs   4.78KiB    0.0%    9.19B
  prolong2mortars            533   44.7μs    0.0%  83.8ns     0.00B    0.0%    0.00B
  prolong2boundaries         533   31.9μs    0.0%  59.8ns     0.00B    0.0%    0.00B
  mortar flux                533   25.5μs    0.0%  47.9ns     0.00B    0.0%    0.00B
  boundary flux              533   15.8μs    0.0%  29.7ns     0.00B    0.0%    0.00B
  source terms               533   15.8μs    0.0%  29.6ns     0.00B    0.0%    0.00B
calculate dt                  19   15.0μs    0.0%   790ns   2.67KiB    0.0%     144B
────────────────────────────────────────────────────────────────────────────────────

Plot the solution

We have to manually convert the sol since Theseus has it's own lightweight solution type.

plot(Trixi.PlotData2DTriangulated(sol_trbdf2.u[end], sol_trbdf2.prob.p))

Solve using OrdinaryDiffEqSDIRK

import OrdinaryDiffEqSDIRK
import DifferentiationInterface: AutoFiniteDiff
sol_sdrik = solve(
    ode, OrdinaryDiffEqSDIRK.TRBDF2(autodiff = AutoFiniteDiff());
    dt = 1.0, # solve needs some value here but it will be overwritten by the stepsize_callback
    ode_default_options()..., callback = callbacks,
    adaptive = false
);

████████╗██████╗ ██╗██╗  ██╗██╗
╚══██╔══╝██╔══██╗██║╚██╗██╔╝██║
   ██║   ██████╔╝██║ ╚███╔╝ ██║
   ██║   ██╔══██╗██║ ██╔██╗ ██║
   ██║   ██║  ██║██║██╔╝ ██╗██║
   ╚═╝   ╚═╝  ╚═╝╚═╝╚═╝  ╚═╝╚═╝

┌──────────────────────────────────────────────────────────────────────────────────────────────────┐
│ SemidiscretizationHyperbolic                                                                     │
│ ════════════════════════════                                                                     │
│ #spatial dimensions: ……………………………………… 2                                                           │
│ mesh: ……………………………………………………………………………… TreeMesh{2, Trixi.SerialTree{2, Float64}} with length 341   │
│ equations: ………………………………………………………………… LinearScalarAdvectionEquation2D                             │
│ initial condition: …………………………………………… initial_condition_convergence_test                          │
│ boundary conditions: ……………………………………… Trixi.BoundaryConditionPeriodic                             │
│ source terms: ………………………………………………………… nothing                                                     │
│ solver: ………………………………………………………………………… DG                                                          │
│ total #DOFs per field: ………………………………… 4096                                                        │
└──────────────────────────────────────────────────────────────────────────────────────────────────┘

┌──────────────────────────────────────────────────────────────────────────────────────────────────┐
│ TreeMesh{2, Trixi.SerialTree{2, Float64}}                                                        │
│ ═════════════════════════════════════════                                                        │
│ center: ………………………………………………………………………… [0.0, 0.0]                                                  │
│ length: ………………………………………………………………………… 2.0                                                         │
│ periodicity: …………………………………………………………… (true, true)                                                │
│ current #cells: …………………………………………………… 341                                                         │
│ #leaf-cells: …………………………………………………………… 256                                                         │
│ current capacity: ……………………………………………… 341                                                         │
└──────────────────────────────────────────────────────────────────────────────────────────────────┘

┌──────────────────────────────────────────────────────────────────────────────────────────────────┐
│ LinearScalarAdvectionEquation2D                                                                  │
│ ═══════════════════════════════                                                                  │
│ #variables: ……………………………………………………………… 1                                                           │
│ │ variable 1: ………………………………………………………… scalar                                                      │
└──────────────────────────────────────────────────────────────────────────────────────────────────┘

┌──────────────────────────────────────────────────────────────────────────────────────────────────┐
│ DG{Float64}                                                                                      │
│ ═══════════                                                                                      │
│ basis: …………………………………………………………………………… LobattoLegendreBasis{Float64}(polydeg=3)                    │
│ mortar: ………………………………………………………………………… LobattoLegendreMortarL2{Float64}(polydeg=3)                 │
│ surface integral: ……………………………………………… SurfaceIntegralWeakForm                                     │
│ │ surface flux: …………………………………………………… FluxLaxFriedrichs(max_abs_speed)                            │
│ volume integral: ………………………………………………… VolumeIntegralWeakForm                                      │
└──────────────────────────────────────────────────────────────────────────────────────────────────┘

┌──────────────────────────────────────────────────────────────────────────────────────────────────┐
│ AnalysisCallback                                                                                 │
│ ════════════════                                                                                 │
│ interval: …………………………………………………………………… 100                                                         │
│ analyzer: …………………………………………………………………… LobattoLegendreAnalyzer{Float64}(polydeg=6)                 │
│ │ error 1: ………………………………………………………………… l2_error                                                    │
│ │ error 2: ………………………………………………………………… linf_error                                                  │
│ │ integral 1: ………………………………………………………… entropy_timederivative                                      │
│ save analysis to file: ………………………………… no                                                          │
└──────────────────────────────────────────────────────────────────────────────────────────────────┘

┌──────────────────────────────────────────────────────────────────────────────────────────────────┐
│ SaveSolutionCallback                                                                             │
│ ════════════════════                                                                             │
│ interval: …………………………………………………………………… 100                                                         │
│ solution variables: ………………………………………… cons2prim                                                   │
│ save initial solution: ………………………………… yes                                                         │
│ save final solution: ……………………………………… yes                                                         │
│ output directory: ……………………………………………… /home/runner/work/Ariadne.jl/…e.jl/docs/build/generated/out │
└──────────────────────────────────────────────────────────────────────────────────────────────────┘

┌──────────────────────────────────────────────────────────────────────────────────────────────────┐
│ StepsizeCallback                                                                                 │
│ ════════════════                                                                                 │
│ CFL Hyperbolic: …………………………………………………… Returns{Float64}(1.6)                                       │
│ CFL Parabolic: ……………………………………………………… Returns{Float64}(0.0)                                       │
│ Interval: …………………………………………………………………… 1                                                           │
└──────────────────────────────────────────────────────────────────────────────────────────────────┘

┌──────────────────────────────────────────────────────────────────────────────────────────────────┐
│ Time integration                                                                                 │
│ ════════════════                                                                                 │
│ Start time: ……………………………………………………………… 0.0                                                         │
│ Final time: ……………………………………………………………… 1.0                                                         │
│ time integrator: ………………………………………………… TRBDF2                                                      │
│ adaptive: …………………………………………………………………… false                                                       │
└──────────────────────────────────────────────────────────────────────────────────────────────────┘
┌──────────────────────────────────────────────────────────────────────────────────────────────────┐
│ Environment information                                                                          │
│ ═══════════════════════                                                                          │
│ #threads: …………………………………………………………………… 1                                                           │
│ threading backend: …………………………………………… static                                                      │
│ LoopVectorization: …………………………………………… disabled                                                    │
└──────────────────────────────────────────────────────────────────────────────────────────────────┘

────────────────────────────────────────────────────────────────────────────────────────────────────
 Simulation running 'LinearScalarAdvectionEquation2D' with DGSEM(polydeg=3)
────────────────────────────────────────────────────────────────────────────────────────────────────
 #timesteps:                  0                run time:       4.61000000e-07 s
 Δt:             1.00000000e+00                └── GC time:    0.00000000e+00 s (0.000%)
 sim. time:      0.00000000e+00 (0.000%)       time/DOF/rhs!:  2.33127441e-08 s
                                               PID:                   Inf s
 #DOFs per field:          4096                alloc'd memory:        668.224 MiB
 #elements:                 256                device memory:           0.000 MiB

 Variable:       scalar
 L2 error:       3.94120825e-06
 Linf error:     1.23670425e-05
 ∑∂S/∂U ⋅ Uₜ :  -2.19124035e-16
────────────────────────────────────────────────────────────────────────────────────────────────────


────────────────────────────────────────────────────────────────────────────────────────────────────
 Simulation running 'LinearScalarAdvectionEquation2D' with DGSEM(polydeg=3)
────────────────────────────────────────────────────────────────────────────────────────────────────
 #timesteps:                 18                run time:       3.79284220e+01 s
 Δt:             5.55555556e-02                └── GC time:    8.49217295e-01 s (2.239%)
 sim. time:      1.00000000e+00 (100.000%)     time/DOF/rhs!:  2.34196013e-08 s
                                               PID:            6.27626220e-08 s
 #DOFs per field:          4096                alloc'd memory:       1025.106 MiB
 #elements:                 256                device memory:           0.000 MiB

 Variable:       scalar
 L2 error:       1.71161806e-04
 Linf error:     2.52798215e-04
 ∑∂S/∂U ⋅ Uₜ :  -1.97752414e-09
────────────────────────────────────────────────────────────────────────────────────────────────────

────────────────────────────────────────────────────────────────────────────────────
             Trixi.jl                      Time                    Allocations
                                  ───────────────────────   ────────────────────────
        Tot / % measured:              37.9s /  37.3%           4.67GiB /   0.1%

Section                   ncalls     time    %tot     avg     alloc    %tot      avg
────────────────────────────────────────────────────────────────────────────────────
rhs!                        148k    14.1s  100.0%  95.8μs   4.78KiB    0.1%    0.03B
  volume integral           148k    9.69s   68.5%  65.7μs     0.00B    0.0%    0.00B
  interface flux            148k    1.17s    8.3%  7.96μs     0.00B    0.0%    0.00B
  Jacobian                  148k    1.02s    7.2%  6.90μs     0.00B    0.0%    0.00B
  surface integral          148k    1.01s    7.2%  6.87μs     0.00B    0.0%    0.00B
  prolong2interfaces        148k    998ms    7.1%  6.77μs     0.00B    0.0%    0.00B
  ~rhs!~                    148k    125ms    0.9%   845ns   4.78KiB    0.1%    0.03B
  reset ∂u/∂t               148k   91.0ms    0.6%   617ns     0.00B    0.0%    0.00B
  prolong2boundaries        148k   6.92ms    0.0%  46.9ns     0.00B    0.0%    0.00B
  prolong2mortars           148k   5.87ms    0.0%  39.8ns     0.00B    0.0%    0.00B
  mortar flux               148k   5.59ms    0.0%  37.9ns     0.00B    0.0%    0.00B
  boundary flux             148k   4.38ms    0.0%  29.7ns     0.00B    0.0%    0.00B
  source terms              148k   4.32ms    0.0%  29.3ns     0.00B    0.0%    0.00B
I/O                            3   2.26ms    0.0%   753μs    203KiB    3.9%  67.8KiB
  save solution                2   2.25ms    0.0%  1.13ms    200KiB    3.9%   100KiB
  ~I/O~                        3   4.83μs    0.0%  1.61μs   1.70KiB    0.0%     581B
  get element variables        2   1.41μs    0.0%   706ns   1.41KiB    0.0%     720B
  get node variables           2   1.18μs    0.0%   591ns     0.00B    0.0%    0.00B
  save mesh                    2    170ns    0.0%  85.0ns     0.00B    0.0%    0.00B
analyze solution               2   1.74ms    0.0%   869μs   4.87MiB   95.9%  2.44MiB
calculate dt                  19   64.4μs    0.0%  3.39μs   2.67KiB    0.1%     144B
────────────────────────────────────────────────────────────────────────────────────

Plot the solution

plot(Trixi.PlotData2DTriangulated(sol_sdrik.u[end], sol_sdrik.prob.p))

Increase CFL numbers

trixi_include(@__MODULE__, joinpath(examples_dir(), "tree_2d_dgsem", "elixir_advection_basic.jl"), cfl = 10, sol = nothing);

sol = solve(
    ode, Theseus.ImplicitEuler();
    dt = 1.0, # solve needs some value here but it will be overwritten by the stepsize_callback
    ode_default_options()..., callback = callbacks,
    # verbose=1,
    krylov_algo = :gmres,
    # krylov_kwargs=(;verbose=1)
);

@show callbacks.discrete_callbacks[4]
┌──────────────────────────────────────────────────────────────────────────────────────────────────┐
│ StepsizeCallback                                                                                 │
│ ════════════════                                                                                 │
│ CFL Hyperbolic: …………………………………………………… Returns{Int64}(10)                                          │
│ CFL Parabolic: ……………………………………………………… Returns{Float64}(0.0)                                       │
│ Interval: …………………………………………………………………… 1                                                           │
└──────────────────────────────────────────────────────────────────────────────────────────────────┘

Plot the solution

plot(Trixi.PlotData2DTriangulated(sol.u[end], sol.prob.p))

This page was generated using Literate.jl.