Path: blob/main/examples/structured_2d_dgsem/elixir_advection_coupled.jl
2055 views
using OrdinaryDiffEqLowStorageRK1using Trixi23###############################################################################4# Coupled semidiscretization of four linear advection systems using converter functions such that5# they are also coupled across the domain boundaries to generate a periodic system.6#7# In this elixir, we have a square domain that is divided into a upper-left, lower-left,8# upper-right and lower-right quarter. On each quarter9# of the domain, a completely independent SemidiscretizationHyperbolic is created for the10# linear advection equations. The four systems are coupled in the x and y-direction.11# For a high-level overview, see also the figure below:12#13# (-1, 1) ( 1, 1)14# ┌────────────────────┬────────────────────┐15# │ ↑ coupled ↑ │ ↑ coupled ↑ │16# │ │ │17# │ ========= │ ========= │18# │ system #1 │ system #2 │19# │ ========= │ ========= │20# │ │ │21# │<-- coupled │<-- coupled │22# │ coupled -->│ coupled -->│23# │ │ │24# │ ↓ coupled ↓ │ ↓ coupled ↓ │25# ├────────────────────┼────────────────────┤26# │ ↑ coupled ↑ │ ↑ coupled ↑ │27# │ │ │28# │ ========= │ ========= │29# │ system #3 │ system #4 │30# │ ========= │ ========= │31# │ │ │32# │<-- coupled │<-- coupled │33# │ coupled -->│ coupled -->│34# │ │ │35# │ ↓ coupled ↓ │ ↓ coupled ↓ │36# └────────────────────┴────────────────────┘37# (-1, -1) ( 1, -1)3839advection_velocity = (0.2, -0.7)40equations = LinearScalarAdvectionEquation2D(advection_velocity)4142# Create DG solver with polynomial degree = 3 and (local) Lax-Friedrichs/Rusanov flux as surface flux43solver = DGSEM(polydeg = 3, surface_flux = flux_lax_friedrichs)4445# This will be the number of elements for each quarter/semidiscretization.46cells_per_dimension = (8, 8)4748###########49# system #150###########5152coordinates_min1 = (-1.0, 0.0) # minimum coordinates (min(x), min(y))53coordinates_max1 = (0.0, 1.0) # maximum coordinates (max(x), max(y))5455mesh1 = StructuredMesh(cells_per_dimension, coordinates_min1, coordinates_max1,56periodicity = false)5758# Define the coupling functions59coupling_function12 = (x, u, equations_other, equations_own) -> u60coupling_function13 = (x, u, equations_other, equations_own) -> u6162# Define the coupling boundary conditions and the system it is coupled to.63boundary_conditions_x_neg1 = BoundaryConditionCoupled(2, (:end, :i_forward), Float64,64coupling_function12)65boundary_conditions_x_pos1 = BoundaryConditionCoupled(2, (:begin, :i_forward), Float64,66coupling_function12)67boundary_conditions_y_neg1 = BoundaryConditionCoupled(3, (:i_forward, :end), Float64,68coupling_function13)69boundary_conditions_y_pos1 = BoundaryConditionCoupled(3, (:i_forward, :begin), Float64,70coupling_function13)7172# A semidiscretization collects data structures and functions for the spatial discretization73semi1 = SemidiscretizationHyperbolic(mesh1, equations, initial_condition_convergence_test,74solver,75boundary_conditions = (x_neg = boundary_conditions_x_neg1,76x_pos = boundary_conditions_x_pos1,77y_neg = boundary_conditions_y_neg1,78y_pos = boundary_conditions_y_pos1))7980###########81# system #282###########8384coordinates_min2 = (0.0, 0.0) # minimum coordinates (min(x), min(y))85coordinates_max2 = (1.0, 1.0) # maximum coordinates (max(x), max(y))8687mesh2 = StructuredMesh(cells_per_dimension, coordinates_min2, coordinates_max2,88periodicity = false)8990# Define the coupling functions91coupling_function21 = (x, u, equations_other, equations_own) -> u92coupling_function24 = (x, u, equations_other, equations_own) -> u9394# Define the coupling boundary conditions and the system it is coupled to.95boundary_conditions_x_neg2 = BoundaryConditionCoupled(1, (:end, :i_forward), Float64,96coupling_function21)97boundary_conditions_x_pos2 = BoundaryConditionCoupled(1, (:begin, :i_forward), Float64,98coupling_function21)99boundary_conditions_y_neg2 = BoundaryConditionCoupled(4, (:i_forward, :end), Float64,100coupling_function24)101boundary_conditions_y_pos2 = BoundaryConditionCoupled(4, (:i_forward, :begin), Float64,102coupling_function24)103104# A semidiscretization collects data structures and functions for the spatial discretization105semi2 = SemidiscretizationHyperbolic(mesh2, equations, initial_condition_convergence_test,106solver,107boundary_conditions = (x_neg = boundary_conditions_x_neg2,108x_pos = boundary_conditions_x_pos2,109y_neg = boundary_conditions_y_neg2,110y_pos = boundary_conditions_y_pos2))111112###########113# system #3114###########115116coordinates_min3 = (-1.0, -1.0) # minimum coordinates (min(x), min(y))117coordinates_max3 = (0.0, 0.0) # maximum coordinates (max(x), max(y))118119mesh3 = StructuredMesh(cells_per_dimension, coordinates_min3, coordinates_max3,120periodicity = false)121122# Define the coupling functions123coupling_function34 = (x, u, equations_other, equations_own) -> u124coupling_function31 = (x, u, equations_other, equations_own) -> u125126# Define the coupling boundary conditions and the system it is coupled to.127boundary_conditions_x_neg3 = BoundaryConditionCoupled(4, (:end, :i_forward), Float64,128coupling_function34)129boundary_conditions_x_pos3 = BoundaryConditionCoupled(4, (:begin, :i_forward), Float64,130coupling_function34)131boundary_conditions_y_neg3 = BoundaryConditionCoupled(1, (:i_forward, :end), Float64,132coupling_function31)133boundary_conditions_y_pos3 = BoundaryConditionCoupled(1, (:i_forward, :begin), Float64,134coupling_function31)135136# A semidiscretization collects data structures and functions for the spatial discretization137semi3 = SemidiscretizationHyperbolic(mesh3, equations, initial_condition_convergence_test,138solver,139boundary_conditions = (x_neg = boundary_conditions_x_neg3,140x_pos = boundary_conditions_x_pos3,141y_neg = boundary_conditions_y_neg3,142y_pos = boundary_conditions_y_pos3))143144###########145# system #4146###########147148coordinates_min4 = (0.0, -1.0) # minimum coordinates (min(x), min(y))149coordinates_max4 = (1.0, 0.0) # maximum coordinates (max(x), max(y))150151mesh4 = StructuredMesh(cells_per_dimension, coordinates_min4, coordinates_max4,152periodicity = false)153154# Define the coupling functions155coupling_function43 = (x, u, equations_other, equations_own) -> u156coupling_function42 = (x, u, equations_other, equations_own) -> u157158# Define the coupling boundary conditions and the system it is coupled to.159boundary_conditions_x_neg4 = BoundaryConditionCoupled(3, (:end, :i_forward), Float64,160coupling_function43)161boundary_conditions_x_pos4 = BoundaryConditionCoupled(3, (:begin, :i_forward), Float64,162coupling_function43)163boundary_conditions_y_neg4 = BoundaryConditionCoupled(2, (:i_forward, :end), Float64,164coupling_function42)165boundary_conditions_y_pos4 = BoundaryConditionCoupled(2, (:i_forward, :begin), Float64,166coupling_function42)167168# A semidiscretization collects data structures and functions for the spatial discretization169semi4 = SemidiscretizationHyperbolic(mesh4, equations, initial_condition_convergence_test,170solver,171boundary_conditions = (x_neg = boundary_conditions_x_neg4,172x_pos = boundary_conditions_x_pos4,173y_neg = boundary_conditions_y_neg4,174y_pos = boundary_conditions_y_pos4))175176# Create a semidiscretization that bundles all the semidiscretizations.177semi = SemidiscretizationCoupled(semi1, semi2, semi3, semi4)178179###############################################################################180# ODE solvers, callbacks etc.181182# Create ODE problem with time span from 0.0 to 2.0183ode = semidiscretize(semi, (0.0, 2.0))184185# At the beginning of the main loop, the SummaryCallback prints a summary of the simulation setup186# and resets the timers187summary_callback = SummaryCallback()188189# The AnalysisCallback allows to analyse the solution in regular intervals and prints the results190analysis_callback1 = AnalysisCallback(semi1, interval = 100)191analysis_callback2 = AnalysisCallback(semi2, interval = 100)192analysis_callback3 = AnalysisCallback(semi3, interval = 100)193analysis_callback4 = AnalysisCallback(semi4, interval = 100)194analysis_callback = AnalysisCallbackCoupled(semi, analysis_callback1, analysis_callback2,195analysis_callback3, analysis_callback4)196197# The SaveSolutionCallback allows to save the solution to a file in regular intervals198save_solution = SaveSolutionCallback(interval = 100,199solution_variables = cons2prim)200201# The StepsizeCallback handles the re-calculation of the maximum Δt after each time step202stepsize_callback = StepsizeCallback(cfl = 1.6)203204# Create a CallbackSet to collect all callbacks such that they can be passed to the ODE solver205callbacks = CallbackSet(summary_callback, analysis_callback, save_solution,206stepsize_callback)207208###############################################################################209# run the simulation210211# OrdinaryDiffEq's `solve` method evolves the solution in time and executes the passed callbacks212sol = solve(ode, CarpenterKennedy2N54(williamson_condition = false);213dt = 1.0, # solve needs some value here but it will be overwritten by the stepsize_callback214ode_default_options()..., callback = callbacks);215216217