Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
trixi-framework
GitHub Repository: trixi-framework/Trixi.jl
Path: blob/main/examples/structured_2d_dgsem/elixir_advection_meshview.jl
2055 views
1
using OrdinaryDiffEqLowStorageRK
2
using Trixi
3
4
###############################################################################
5
# Coupled semidiscretization of two linear advection systems using converter functions
6
# and mesh views for the semidiscretizations. First we define a parent mesh
7
# for the entire physical domain, then we define the two mesh views on this parent.
8
#
9
# In this elixir, we have a square domain that is divided into left and right subdomains.
10
# On each half of the domain, a completely independent `SemidiscretizationHyperbolic`
11
# is created for the linear advection equations. The two systems are coupled in the
12
# x-direction.
13
# For a high-level overview, see also the figure below:
14
#
15
# (-1, 1) ( 1, 1)
16
# ┌────────────────────┬────────────────────┐
17
# │ ↑ periodic ↑ │ ↑ periodic ↑ │
18
# │ │ │
19
# │ ========= │ ========= │
20
# │ system #1 │ system #2 │
21
# │ ========= │ ========= │
22
# │ │ │
23
# │<-- coupled │<-- coupled │
24
# │ coupled -->│ coupled -->│
25
# │ │ │
26
# │ ↓ periodic ↓ │ ↓ periodic ↓ │
27
# └────────────────────┴────────────────────┘
28
# (-1, -1) ( 1, -1)
29
30
advection_velocity = (0.2, -0.7)
31
equations = LinearScalarAdvectionEquation2D(advection_velocity)
32
33
# Create DG solver with polynomial degree = 3
34
solver = DGSEM(polydeg = 3, surface_flux = flux_lax_friedrichs)
35
36
# Domain size of the parent mesh.
37
coordinates_min = (-1.0, -1.0)
38
coordinates_max = (1.0, 1.0)
39
40
# Cell dimensions of the parent mesh.
41
cells_per_dimension = (16, 16)
42
43
# Create parent mesh with 16 x 16 elements
44
parent_mesh = StructuredMesh(cells_per_dimension, coordinates_min, coordinates_max)
45
46
# Create the two mesh views, each of which takes half of the parent mesh.
47
mesh1 = StructuredMeshView(parent_mesh; indices_min = (1, 1), indices_max = (8, 16))
48
mesh2 = StructuredMeshView(parent_mesh; indices_min = (9, 1), indices_max = (16, 16))
49
50
# The coupling function is simply the identity, as we are dealing with two identical systems.
51
coupling_function = (x, u, equations_other, equations_own) -> u
52
53
# Define the coupled boundary conditions
54
# The indices (:end, :i_forward) and (:begin, :i_forward) denote the interface indexing.
55
# For a system with coupling in x and y see examples/structured_2d_dgsem/elixir_advection_coupled.jl.
56
boundary_conditions1 = (
57
# Connect left boundary with right boundary of left mesh
58
x_neg = BoundaryConditionCoupled(2, (:end, :i_forward), Float64,
59
coupling_function),
60
x_pos = BoundaryConditionCoupled(2, (:begin, :i_forward), Float64,
61
coupling_function),
62
y_neg = boundary_condition_periodic,
63
y_pos = boundary_condition_periodic)
64
boundary_conditions2 = (
65
# Connect left boundary with right boundary of left mesh
66
x_neg = BoundaryConditionCoupled(1, (:end, :i_forward), Float64,
67
coupling_function),
68
x_pos = BoundaryConditionCoupled(1, (:begin, :i_forward), Float64,
69
coupling_function),
70
y_neg = boundary_condition_periodic,
71
y_pos = boundary_condition_periodic)
72
73
# A semidiscretization collects data structures and functions for the spatial discretization
74
semi1 = SemidiscretizationHyperbolic(mesh1, equations, initial_condition_convergence_test,
75
solver,
76
boundary_conditions = boundary_conditions1)
77
semi2 = SemidiscretizationHyperbolic(mesh2, equations, initial_condition_convergence_test,
78
solver,
79
boundary_conditions = boundary_conditions2)
80
semi = SemidiscretizationCoupled(semi1, semi2)
81
82
###############################################################################
83
# ODE solvers, callbacks etc.
84
85
# Create ODE problem with time span from 0.0 to 1.0
86
ode = semidiscretize(semi, (0.0, 1.0))
87
88
# At the beginning of the main loop, the SummaryCallback prints a summary of the simulation setup
89
# and resets the timers
90
summary_callback = SummaryCallback()
91
92
# The AnalysisCallback allows to analyse the solution in regular intervals and prints the results
93
analysis_callback1 = AnalysisCallback(semi1, interval = 100)
94
analysis_callback2 = AnalysisCallback(semi2, interval = 100)
95
analysis_callback = AnalysisCallbackCoupled(semi, analysis_callback1, analysis_callback2)
96
97
analysis_interval = 100
98
alive_callback = AliveCallback(analysis_interval = analysis_interval)
99
100
# The SaveSolutionCallback allows to save the solution to a file in regular intervals
101
save_solution = SaveSolutionCallback(interval = 100,
102
save_initial_solution = true,
103
save_final_solution = true,
104
solution_variables = cons2prim)
105
106
# The StepsizeCallback handles the re-calculation of the maximum Δt after each time step
107
stepsize_callback = StepsizeCallback(cfl = 1.6)
108
109
# Create a CallbackSet to collect all callbacks such that they can be passed to the ODE solver
110
callbacks = CallbackSet(summary_callback, analysis_callback, save_solution,
111
stepsize_callback)
112
113
###############################################################################
114
# run the simulation
115
116
# OrdinaryDiffEq's `solve` method evolves the solution in time and executes the passed callbacks
117
sol = solve(ode, CarpenterKennedy2N54(williamson_condition = false);
118
dt = 5.0e-2, # solve needs some value here but it will be overwritten by the stepsize_callback
119
ode_default_options()..., callback = callbacks);
120
121