using OrdinaryDiffEqSSPRK
using Trixi

###############################################################################
# semidiscretization of the compressible Euler equations

equations = CompressibleEulerEquations2D(1.4)

"""
    initial_condition_double_mach_reflection(x, t, equations::CompressibleEulerEquations2D)

Compressible Euler setup for a double Mach reflection problem.
Involves strong shock interactions as well as steady / unsteady flow structures.
Also exercises special boundary conditions along the bottom of the domain that is a mixture of
Dirichlet and slip wall.
See Section IV c on the paper below for details.

- Paul Woodward and Phillip Colella (1984)
  The Numerical Simulation of Two-Dimensional Fluid Flows with Strong Shocks.
  [DOI: 10.1016/0021-9991(84)90142-6](https://doi.org/10.1016/0021-9991(84)90142-6)
"""
@inline function initial_condition_double_mach_reflection(x, t,
                                                          equations::CompressibleEulerEquations2D)
    if x[1] < 1 / 6 + (x[2] + 20 * t) / sqrt(3)
        phi = pi / 6
        sin_phi, cos_phi = sincos(phi)

        rho = 8.0
        v1 = 8.25 * cos_phi
        v2 = -8.25 * sin_phi
        p = 116.5
    else
        rho = 1.4
        v1 = 0.0
        v2 = 0.0
        p = 1.0
    end

    prim = SVector(rho, v1, v2, p)
    return prim2cons(prim, equations)
end

initial_condition = initial_condition_double_mach_reflection

boundary_condition_inflow = BoundaryConditionDirichlet(initial_condition_double_mach_reflection)

# Supersonic outflow boundary condition. Solution is taken entirely from the internal state.
# See `examples/p4est_2d_dgsem/elixir_euler_forward_step_amr.jl` for complete documentation.
@inline function boundary_condition_outflow(u_inner, normal_direction::AbstractVector, x, t,
                                            surface_flux_function,
                                            equations::CompressibleEulerEquations2D)
    # NOTE: Only for the supersonic outflow is this strategy valid
    # Calculate the boundary flux entirely from the internal solution state
    return flux(u_inner, normal_direction, equations)
end

# Special mixed boundary condition type for the :Bottom of the domain.
# It is Dirichlet when x < 1/6 and a slip wall when x >= 1/6
@inline function boundary_condition_mixed_dirichlet_wall(u_inner,
                                                         normal_direction::AbstractVector,
                                                         x, t, surface_flux_function,
                                                         equations::CompressibleEulerEquations2D)
    if x[1] < 1 / 6
        # From the BoundaryConditionDirichlet
        # get the external value of the solution
        u_boundary = initial_condition_double_mach_reflection(x, t, equations)
        # Calculate boundary flux
        flux = surface_flux_function(u_inner, u_boundary, normal_direction, equations)
    else # x[1] >= 1 / 6
        # Use the free slip wall BC otherwise
        flux = boundary_condition_slip_wall(u_inner, normal_direction, x, t,
                                            surface_flux_function, equations)
    end

    return flux
end

boundary_conditions = (; Bottom = boundary_condition_mixed_dirichlet_wall,
                       Top = boundary_condition_inflow,
                       Right = boundary_condition_outflow,
                       Left = boundary_condition_inflow)

volume_flux = flux_ranocha
surface_flux = flux_lax_friedrichs

volume_integral_wf = VolumeIntegralWeakForm()
volume_integral_fluxdiff = VolumeIntegralFluxDifferencing(volume_flux)

# This indicator ensures entropy-stability even with usage of the weak form volume integral by
# checking the entropy production of the weak form volume term after (i.e., a-posteriori) computation
indicator_a_posteriori = IndicatorEntropyChange(maximum_entropy_increase = 0.0)

volume_integral_adaptive = VolumeIntegralAdaptive(indicator = indicator_a_posteriori,
                                                  volume_integral_default = volume_integral_wf,
                                                  volume_integral_stabilized = volume_integral_fluxdiff)

polydeg = 4
basis = LobattoLegendreBasis(polydeg)
volume_integral_blend_low_order = VolumeIntegralPureLGLFiniteVolumeO2(basis;
                                                                      volume_flux_fv = surface_flux,
                                                                      reconstruction_mode = reconstruction_O2_inner,
                                                                      slope_limiter = minmod)

# This volume integral applies blended DG-FV shock capturing on the elements based on the shock indicator
shock_indicator = IndicatorHennemannGassner(equations, basis,
                                            alpha_max = 0.5, alpha_min = 5e-2,
                                            alpha_smooth = true,
                                            variable = density_pressure)

volume_integral = VolumeIntegralShockCapturingHGType(shock_indicator;
                                                     volume_integral_default = volume_integral_adaptive,
                                                     volume_integral_blend_high_order = volume_integral_fluxdiff,
                                                     volume_integral_blend_low_order = volume_integral_blend_low_order)

solver = DGSEM(polydeg = polydeg, surface_flux = surface_flux,
               volume_integral = volume_integral)

# Get the unstructured quad mesh from a file (downloads the file if not available locally)
mesh_file = Trixi.download("https://gist.githubusercontent.com/andrewwinters5000/a0806ef0d03cf5ea221af523167b6e32/raw/61ed0eb017eb432d996ed119a52fb041fe363e8c/abaqus_double_mach.inp",
                           joinpath(@__DIR__, "abaqus_double_mach.inp"))

mesh = P4estMesh{2}(mesh_file)

semi = SemidiscretizationHyperbolic(mesh, equations, initial_condition, solver;
                                    boundary_conditions = boundary_conditions)

###############################################################################
# ODE solvers, callbacks etc.

tspan = (0.0, 0.2)
ode = semidiscretize(semi, tspan)

summary_callback = SummaryCallback()

analysis_interval = 200
analysis_callback = AnalysisCallback(semi, interval = analysis_interval,
                                     analysis_errors = Symbol[],
                                     extra_analysis_integrals = (entropy,))

alive_callback = AliveCallback(analysis_interval = analysis_interval)

save_interval = 500
save_solution = SaveSolutionCallback(interval = save_interval,
                                     save_initial_solution = true,
                                     save_final_solution = true,
                                     solution_variables = cons2prim)

amr_indicator = IndicatorLöhner(semi, variable = Trixi.density)

amr_controller = ControllerThreeLevel(semi, amr_indicator,
                                      base_level = 0,
                                      med_level = 3, med_threshold = 0.05,
                                      max_level = 6, max_threshold = 0.1)

amr_callback = AMRCallback(semi, amr_controller,
                           interval = 2,
                           adapt_initial_condition = true,
                           adapt_initial_condition_only_refine = true)

callbacks = CallbackSet(summary_callback,
                        analysis_callback, alive_callback,
                        save_solution, amr_callback)

# positivity limiter necessary for this example with strong shocks
stage_limiter! = PositivityPreservingLimiterZhangShu(thresholds = (5.0e-6, 5.0e-6),
                                                     variables = (Trixi.density, pressure))

###############################################################################
# run the simulation
sol = solve(ode, SSPRK43(; stage_limiter! = stage_limiter!, thread = Trixi.Threaded());
            dt = 5e-7, # Reducing initial timestep allows AMR interval of 2 instead of 1
            adaptive = true, ode_default_options()..., callback = callbacks);
