|
| 1 | +using LibTrixi |
| 2 | +using OrdinaryDiffEq |
| 3 | +using Trixi |
| 4 | + |
| 5 | +# The function to create the simulation state needs to be named `init_simstate` |
| 6 | +function init_simstate() |
| 7 | + |
| 8 | + ############################################################################### |
| 9 | + # initial condition: density wave + tracer blob |
| 10 | + |
| 11 | + function initial_condition_wave_blob(x, t, |
| 12 | + equations::PassiveTracerEquations) |
| 13 | + # Initial condition for flow equations: density wave |
| 14 | + v1 = 0.1 |
| 15 | + v2 = 0.2 |
| 16 | + rho = 1 + 0.5 * sinpi(2 * (x[1] + x[2] - t * (v1 + v2))) |
| 17 | + rho_v1 = rho * v1 |
| 18 | + rho_v2 = rho * v2 |
| 19 | + p = 20 |
| 20 | + rho_e = p / (equations.flow_equations.gamma - 1) + 0.5 * rho * (v1^2 + v2^2) |
| 21 | + |
| 22 | + # Initial condition for tracers: blob in fraction of density |
| 23 | + tracer = 0.2 * exp(-20 * (x[1] + 0.45)^2 - 10 * (x[2] - 0.15)^2) |
| 24 | + |
| 25 | + return SVector(rho, rho_v1, rho_v2, rho_e, rho * tracer) |
| 26 | + end |
| 27 | + |
| 28 | + ############################################################################### |
| 29 | + # semidiscretization of the compressible Euler equations |
| 30 | + |
| 31 | + gamma = 1.4 |
| 32 | + flow_equations = CompressibleEulerEquations2D(gamma) |
| 33 | + equations = PassiveTracerEquations(flow_equations, n_tracers = 1) |
| 34 | + |
| 35 | + # Create DG solver with polynomial degree = 5, Ranocha flux, and derived tracer flux |
| 36 | + solver = DGSEM(polydeg = 5, surface_flux = FluxTracerEquationsCentral(flux_ranocha)) |
| 37 | + |
| 38 | + coordinates_min = (-1.0, -1.0) # minimum coordinates (min(x), min(y)) |
| 39 | + coordinates_max = ( 1.0, 1.0) # maximum coordinates (max(x), max(y)) |
| 40 | + trees_per_dimension = (4, 4) # initial resolution (without refinement) |
| 41 | + mesh = T8codeMesh(trees_per_dimension, polydeg = 1, |
| 42 | + coordinates_min = coordinates_min, coordinates_max = coordinates_max, |
| 43 | + initial_refinement_level = 1) |
| 44 | + |
| 45 | + # Create spatial discretization |
| 46 | + semi = SemidiscretizationHyperbolic(mesh, equations, initial_condition_wave_blob, |
| 47 | + solver) |
| 48 | + |
| 49 | + ############################################################################### |
| 50 | + # ODE solvers, callbacks etc. |
| 51 | + |
| 52 | + # Create ODE problem with time span from 0.0 to 2.0 |
| 53 | + ode = semidiscretize(semi, (0.0, 2.0)); |
| 54 | + |
| 55 | + # SummaryCallback prints a summary of the simulation setup and recorded performance data |
| 56 | + summary_callback = SummaryCallback() |
| 57 | + |
| 58 | + # AnalysisCallback analyses the solution at regular intervals and prints the results |
| 59 | + analysis_interval = 100 |
| 60 | + analysis_callback = AnalysisCallback(semi, interval=analysis_interval) |
| 61 | + |
| 62 | + # AliveCallback prints a one line summary at regular intervals |
| 63 | + alive_callback = AliveCallback(analysis_interval=analysis_interval) |
| 64 | + |
| 65 | + # StepsizeCallback handles the re-calculation of the maximum Δt after each time step |
| 66 | + stepsize_callback = StepsizeCallback(cfl = 1.0) |
| 67 | + |
| 68 | + # AMRCallback triggers adaptive mesh refinement |
| 69 | + @inline function first_tracer(u, equations::PassiveTracerEquations) |
| 70 | + return Trixi.tracers(u, equations)[1] |
| 71 | + end |
| 72 | + amr_controller = ControllerThreeLevel(semi, IndicatorMax(semi, variable=first_tracer), |
| 73 | + base_level=1, |
| 74 | + med_level=2, med_threshold=0.05, |
| 75 | + max_level=2, max_threshold=0.05) |
| 76 | + amr_callback = AMRCallback(semi, amr_controller, |
| 77 | + interval=50, |
| 78 | + adapt_initial_condition=true, |
| 79 | + adapt_initial_condition_only_refine=true) |
| 80 | + |
| 81 | + # SaveSolutionCallback writes the solution at regular intervals |
| 82 | + save_solution = SaveSolutionCallback(interval=50, |
| 83 | + save_initial_solution=true, |
| 84 | + save_final_solution=true, |
| 85 | + solution_variables = cons2prim) |
| 86 | + |
| 87 | + # CallbackSet collects all callbacks |
| 88 | + callbacks = CallbackSet(summary_callback, |
| 89 | + analysis_callback, |
| 90 | + alive_callback, |
| 91 | + amr_callback, |
| 92 | + save_solution, |
| 93 | + stepsize_callback) |
| 94 | + |
| 95 | + ############################################################################### |
| 96 | + # create the time integrator |
| 97 | + |
| 98 | + # OrdinaryDiffEq's `integrator` |
| 99 | + integrator = init(ode, CarpenterKennedy2N54(williamson_condition=false), |
| 100 | + dt=1.0, # solve needs some value here but it will be overwritten by the stepsize_callback |
| 101 | + save_everystep=false, callback=callbacks); |
| 102 | + |
| 103 | + ############################################################################### |
| 104 | + # Create simulation state |
| 105 | + |
| 106 | + simstate = SimulationState(semi, integrator) |
| 107 | + |
| 108 | + return simstate |
| 109 | +end |
0 commit comments