Path: blob/main/examples/structured_2d_dgsem/elixir_advection_coupled.jl
5586 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 = (;76x_neg = boundary_conditions_x_neg1,77x_pos = boundary_conditions_x_pos1,78y_neg = boundary_conditions_y_neg1,79y_pos = boundary_conditions_y_pos1))8081###########82# system #283###########8485coordinates_min2 = (0.0, 0.0) # minimum coordinates (min(x), min(y))86coordinates_max2 = (1.0, 1.0) # maximum coordinates (max(x), max(y))8788mesh2 = StructuredMesh(cells_per_dimension, coordinates_min2, coordinates_max2,89periodicity = false)9091# Define the coupling functions92coupling_function21 = (x, u, equations_other, equations_own) -> u93coupling_function24 = (x, u, equations_other, equations_own) -> u9495# Define the coupling boundary conditions and the system it is coupled to.96boundary_conditions_x_neg2 = BoundaryConditionCoupled(1, (:end, :i_forward), Float64,97coupling_function21)98boundary_conditions_x_pos2 = BoundaryConditionCoupled(1, (:begin, :i_forward), Float64,99coupling_function21)100boundary_conditions_y_neg2 = BoundaryConditionCoupled(4, (:i_forward, :end), Float64,101coupling_function24)102boundary_conditions_y_pos2 = BoundaryConditionCoupled(4, (:i_forward, :begin), Float64,103coupling_function24)104105# A semidiscretization collects data structures and functions for the spatial discretization106semi2 = SemidiscretizationHyperbolic(mesh2, equations, initial_condition_convergence_test,107solver;108boundary_conditions = (;109x_neg = boundary_conditions_x_neg2,110x_pos = boundary_conditions_x_pos2,111y_neg = boundary_conditions_y_neg2,112y_pos = boundary_conditions_y_pos2))113114###########115# system #3116###########117118coordinates_min3 = (-1.0, -1.0) # minimum coordinates (min(x), min(y))119coordinates_max3 = (0.0, 0.0) # maximum coordinates (max(x), max(y))120121mesh3 = StructuredMesh(cells_per_dimension, coordinates_min3, coordinates_max3,122periodicity = false)123124# Define the coupling functions125coupling_function34 = (x, u, equations_other, equations_own) -> u126coupling_function31 = (x, u, equations_other, equations_own) -> u127128# Define the coupling boundary conditions and the system it is coupled to.129boundary_conditions_x_neg3 = BoundaryConditionCoupled(4, (:end, :i_forward), Float64,130coupling_function34)131boundary_conditions_x_pos3 = BoundaryConditionCoupled(4, (:begin, :i_forward), Float64,132coupling_function34)133boundary_conditions_y_neg3 = BoundaryConditionCoupled(1, (:i_forward, :end), Float64,134coupling_function31)135boundary_conditions_y_pos3 = BoundaryConditionCoupled(1, (:i_forward, :begin), Float64,136coupling_function31)137138# A semidiscretization collects data structures and functions for the spatial discretization139semi3 = SemidiscretizationHyperbolic(mesh3, equations, initial_condition_convergence_test,140solver;141boundary_conditions = (;142x_neg = boundary_conditions_x_neg3,143x_pos = boundary_conditions_x_pos3,144y_neg = boundary_conditions_y_neg3,145y_pos = boundary_conditions_y_pos3))146147###########148# system #4149###########150151coordinates_min4 = (0.0, -1.0) # minimum coordinates (min(x), min(y))152coordinates_max4 = (1.0, 0.0) # maximum coordinates (max(x), max(y))153154mesh4 = StructuredMesh(cells_per_dimension, coordinates_min4, coordinates_max4,155periodicity = false)156157# Define the coupling functions158coupling_function43 = (x, u, equations_other, equations_own) -> u159coupling_function42 = (x, u, equations_other, equations_own) -> u160161# Define the coupling boundary conditions and the system it is coupled to.162boundary_conditions_x_neg4 = BoundaryConditionCoupled(3, (:end, :i_forward), Float64,163coupling_function43)164boundary_conditions_x_pos4 = BoundaryConditionCoupled(3, (:begin, :i_forward), Float64,165coupling_function43)166boundary_conditions_y_neg4 = BoundaryConditionCoupled(2, (:i_forward, :end), Float64,167coupling_function42)168boundary_conditions_y_pos4 = BoundaryConditionCoupled(2, (:i_forward, :begin), Float64,169coupling_function42)170171# A semidiscretization collects data structures and functions for the spatial discretization172semi4 = SemidiscretizationHyperbolic(mesh4, equations, initial_condition_convergence_test,173solver;174boundary_conditions = (;175x_neg = boundary_conditions_x_neg4,176x_pos = boundary_conditions_x_pos4,177y_neg = boundary_conditions_y_neg4,178y_pos = boundary_conditions_y_pos4))179180# Create a semidiscretization that bundles all the semidiscretizations.181semi = SemidiscretizationCoupled(semi1, semi2, semi3, semi4)182183###############################################################################184# ODE solvers, callbacks etc.185186# Create ODE problem with time span from 0.0 to 2.0187ode = semidiscretize(semi, (0.0, 2.0))188189# At the beginning of the main loop, the SummaryCallback prints a summary of the simulation setup190# and resets the timers191summary_callback = SummaryCallback()192193# The AnalysisCallback allows to analyse the solution in regular intervals and prints the results194analysis_callback1 = AnalysisCallback(semi1, interval = 100)195analysis_callback2 = AnalysisCallback(semi2, interval = 100)196analysis_callback3 = AnalysisCallback(semi3, interval = 100)197analysis_callback4 = AnalysisCallback(semi4, interval = 100)198analysis_callback = AnalysisCallbackCoupled(semi, analysis_callback1, analysis_callback2,199analysis_callback3, analysis_callback4)200201# The SaveSolutionCallback allows to save the solution to a file in regular intervals202save_solution = SaveSolutionCallback(interval = 100,203solution_variables = cons2prim)204205# The StepsizeCallback handles the re-calculation of the maximum Δt after each time step206stepsize_callback = StepsizeCallback(cfl = 1.6)207208# Create a CallbackSet to collect all callbacks such that they can be passed to the ODE solver209callbacks = CallbackSet(summary_callback, analysis_callback, save_solution,210stepsize_callback)211212###############################################################################213# run the simulation214215# OrdinaryDiffEq's `solve` method evolves the solution in time and executes the passed callbacks216sol = solve(ode, CarpenterKennedy2N54(williamson_condition = false);217dt = 1.0, # solve needs some value here but it will be overwritten by the stepsize_callback218ode_default_options()..., callback = callbacks);219220221