Path: blob/main/examples/tree_1d_dgsem/elixir_advection_diffusion.jl
5586 views
using OrdinaryDiffEqSDIRK, ADTypes1using Trixi2import LinearSolve: LUFactorization34###############################################################################5# semidiscretization of the linear advection diffusion equation67advection_velocity = 0.18equations = LinearScalarAdvectionEquation1D(advection_velocity)9diffusivity() = 0.110equations_parabolic = LaplaceDiffusion1D(diffusivity(), equations)1112# Create DG solver with polynomial degree = 3 and (local) Lax-Friedrichs/Rusanov flux as surface flux13solver = DGSEM(polydeg = 3, surface_flux = flux_lax_friedrichs)1415coordinates_min = -convert(Float64, pi) # minimum coordinate16coordinates_max = convert(Float64, pi) # maximum coordinate1718# Create a uniformly refined mesh with periodic boundaries19mesh = TreeMesh(coordinates_min, coordinates_max,20initial_refinement_level = 4,21n_cells_max = 30_000, # set maximum capacity of tree data structure22periodicity = true)2324function x_trans_periodic(x, domain_length = SVector(oftype(x[1], 2 * pi)),25center = SVector(oftype(x[1], 0)))26x_normalized = x .- center27x_shifted = x_normalized .% domain_length28x_offset = ((x_shifted .< -0.5f0 * domain_length) -29(x_shifted .> 0.5f0 * domain_length)) .*30domain_length31return center + x_shifted + x_offset32end3334# Define initial condition35function initial_condition_diffusive_convergence_test(x, t,36equation::LinearScalarAdvectionEquation1D)37# Store translated coordinate for easy use of exact solution38x_trans = x_trans_periodic(x - equation.advection_velocity * t)3940nu = diffusivity()41c = 042A = 143omega = 144scalar = c + A * sin(omega * sum(x_trans)) * exp(-nu * omega^2 * t)45return SVector(scalar)46end47initial_condition = initial_condition_diffusive_convergence_test4849# define periodic boundary conditions everywhere50boundary_conditions = boundary_condition_periodic51boundary_conditions_parabolic = boundary_condition_periodic5253# A semidiscretization collects data structures and functions for the spatial discretization54semi = SemidiscretizationHyperbolicParabolic(mesh, (equations, equations_parabolic),55initial_condition,56solver;57boundary_conditions = (boundary_conditions,58boundary_conditions_parabolic))5960###############################################################################61# ODE solvers, callbacks etc.6263# Create ODE problem with time span from 0.0 to 1.064tspan = (0.0, 1.0)65ode = semidiscretize(semi, tspan)6667# At the beginning of the main loop, the SummaryCallback prints a summary of the simulation setup68# and resets the timers69summary_callback = SummaryCallback()7071# The AnalysisCallback allows to analyse the solution in regular intervals and prints the results72analysis_callback = AnalysisCallback(semi, interval = 100)7374# The AliveCallback prints short status information in regular intervals75alive_callback = AliveCallback(analysis_interval = 100)7677# The SaveRestartCallback allows to save a file from which a Trixi.jl simulation can be restarted78save_restart = SaveRestartCallback(interval = 100,79save_final_restart = true)8081# Create a CallbackSet to collect all callbacks such that they can be passed to the ODE solver82callbacks = CallbackSet(summary_callback, analysis_callback, alive_callback, save_restart)8384###############################################################################85# run the simulation8687# OrdinaryDiffEq's `solve` method evolves the solution in time and executes the passed callbacks88time_int_tol = 1.0e-1089time_abs_tol = 1.0e-1090# This is an IMEX SDIRK method91ode_alg = KenCarp4(autodiff = AutoFiniteDiff(), linsolve = LUFactorization())92sol = solve(ode, ode_alg;93abstol = time_abs_tol, reltol = time_int_tol,94ode_default_options()..., callback = callbacks)959697