using OrdinaryDiffEqLowStorageRK
using Trixi

###############################################################################
# semidiscretization of the Burgers' equation with diffusion

equations = InviscidBurgersEquation1D()
diffusivity() = 1e-2
equations_parabolic = LaplaceDiffusion1D(diffusivity(), equations)

solver = DGSEM(polydeg = 4, surface_flux = flux_lax_friedrichs,
               volume_integral = VolumeIntegralFluxDifferencing(flux_ec))

coordinates_min = -0.25
coordinates_max = 0.75

mesh = TreeMesh(coordinates_min, coordinates_max,
                initial_refinement_level = 4,
                periodicity = false)

# This initial condition is a simplification/analogy to the
# Navier Stokes viscous shock (Becker-Morduchow-Libby solution).
function initial_condition_weak_shock_wave(x, t, equations)
    x_shock = -0.0 # initial shock position
    x_translated = (x[1] - x_shock) - t # shock speed is 1
    u = 2 / (1 + exp(x_translated / diffusivity()))

    return SVector(u)
end
initial_condition = initial_condition_weak_shock_wave

boundary_condition = BoundaryConditionDirichlet(initial_condition)
boundary_conditions = boundary_condition
boundary_conditions_parabolic = boundary_condition

semi = SemidiscretizationHyperbolicParabolic(mesh, (equations, equations_parabolic),
                                             initial_condition, solver;
                                             boundary_conditions = (boundary_conditions,
                                                                    boundary_conditions_parabolic))

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

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

summary_callback = SummaryCallback()

analysis_callback = AnalysisCallback(semi, interval = 100)

alive_callback = AliveCallback(analysis_interval = 100)

# Timestep is limited by parabolic CFL
stepsize_callback = StepsizeCallback(cfl = 0.8, cfl_parabolic = 0.15)

callbacks = CallbackSet(summary_callback,
                        analysis_callback, alive_callback,
                        stepsize_callback)

###############################################################################
# run the simulation

sol = solve(ode, RDPK3Sp510();
            adaptive = false,
            dt = 1, # solve needs some value here but it will be overwritten by the stepsize_callback
            ode_default_options()..., callback = callbacks);
