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_coupled.jl
2055 views
1
using OrdinaryDiffEqLowStorageRK
2
using Trixi
3
4
###############################################################################
5
# Coupled semidiscretization of four linear advection systems using converter functions such that
6
# they are also coupled across the domain boundaries to generate a periodic system.
7
#
8
# In this elixir, we have a square domain that is divided into a upper-left, lower-left,
9
# upper-right and lower-right quarter. On each quarter
10
# of the domain, a completely independent SemidiscretizationHyperbolic is created for the
11
# linear advection equations. The four systems are coupled in the x and y-direction.
12
# For a high-level overview, see also the figure below:
13
#
14
# (-1, 1) ( 1, 1)
15
# ┌────────────────────┬────────────────────┐
16
# │ ↑ coupled ↑ │ ↑ coupled ↑ │
17
# │ │ │
18
# │ ========= │ ========= │
19
# │ system #1 │ system #2 │
20
# │ ========= │ ========= │
21
# │ │ │
22
# │<-- coupled │<-- coupled │
23
# │ coupled -->│ coupled -->│
24
# │ │ │
25
# │ ↓ coupled ↓ │ ↓ coupled ↓ │
26
# ├────────────────────┼────────────────────┤
27
# │ ↑ coupled ↑ │ ↑ coupled ↑ │
28
# │ │ │
29
# │ ========= │ ========= │
30
# │ system #3 │ system #4 │
31
# │ ========= │ ========= │
32
# │ │ │
33
# │<-- coupled │<-- coupled │
34
# │ coupled -->│ coupled -->│
35
# │ │ │
36
# │ ↓ coupled ↓ │ ↓ coupled ↓ │
37
# └────────────────────┴────────────────────┘
38
# (-1, -1) ( 1, -1)
39
40
advection_velocity = (0.2, -0.7)
41
equations = LinearScalarAdvectionEquation2D(advection_velocity)
42
43
# Create DG solver with polynomial degree = 3 and (local) Lax-Friedrichs/Rusanov flux as surface flux
44
solver = DGSEM(polydeg = 3, surface_flux = flux_lax_friedrichs)
45
46
# This will be the number of elements for each quarter/semidiscretization.
47
cells_per_dimension = (8, 8)
48
49
###########
50
# system #1
51
###########
52
53
coordinates_min1 = (-1.0, 0.0) # minimum coordinates (min(x), min(y))
54
coordinates_max1 = (0.0, 1.0) # maximum coordinates (max(x), max(y))
55
56
mesh1 = StructuredMesh(cells_per_dimension, coordinates_min1, coordinates_max1,
57
periodicity = false)
58
59
# Define the coupling functions
60
coupling_function12 = (x, u, equations_other, equations_own) -> u
61
coupling_function13 = (x, u, equations_other, equations_own) -> u
62
63
# Define the coupling boundary conditions and the system it is coupled to.
64
boundary_conditions_x_neg1 = BoundaryConditionCoupled(2, (:end, :i_forward), Float64,
65
coupling_function12)
66
boundary_conditions_x_pos1 = BoundaryConditionCoupled(2, (:begin, :i_forward), Float64,
67
coupling_function12)
68
boundary_conditions_y_neg1 = BoundaryConditionCoupled(3, (:i_forward, :end), Float64,
69
coupling_function13)
70
boundary_conditions_y_pos1 = BoundaryConditionCoupled(3, (:i_forward, :begin), Float64,
71
coupling_function13)
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 = (x_neg = boundary_conditions_x_neg1,
77
x_pos = boundary_conditions_x_pos1,
78
y_neg = boundary_conditions_y_neg1,
79
y_pos = boundary_conditions_y_pos1))
80
81
###########
82
# system #2
83
###########
84
85
coordinates_min2 = (0.0, 0.0) # minimum coordinates (min(x), min(y))
86
coordinates_max2 = (1.0, 1.0) # maximum coordinates (max(x), max(y))
87
88
mesh2 = StructuredMesh(cells_per_dimension, coordinates_min2, coordinates_max2,
89
periodicity = false)
90
91
# Define the coupling functions
92
coupling_function21 = (x, u, equations_other, equations_own) -> u
93
coupling_function24 = (x, u, equations_other, equations_own) -> u
94
95
# Define the coupling boundary conditions and the system it is coupled to.
96
boundary_conditions_x_neg2 = BoundaryConditionCoupled(1, (:end, :i_forward), Float64,
97
coupling_function21)
98
boundary_conditions_x_pos2 = BoundaryConditionCoupled(1, (:begin, :i_forward), Float64,
99
coupling_function21)
100
boundary_conditions_y_neg2 = BoundaryConditionCoupled(4, (:i_forward, :end), Float64,
101
coupling_function24)
102
boundary_conditions_y_pos2 = BoundaryConditionCoupled(4, (:i_forward, :begin), Float64,
103
coupling_function24)
104
105
# A semidiscretization collects data structures and functions for the spatial discretization
106
semi2 = SemidiscretizationHyperbolic(mesh2, equations, initial_condition_convergence_test,
107
solver,
108
boundary_conditions = (x_neg = boundary_conditions_x_neg2,
109
x_pos = boundary_conditions_x_pos2,
110
y_neg = boundary_conditions_y_neg2,
111
y_pos = boundary_conditions_y_pos2))
112
113
###########
114
# system #3
115
###########
116
117
coordinates_min3 = (-1.0, -1.0) # minimum coordinates (min(x), min(y))
118
coordinates_max3 = (0.0, 0.0) # maximum coordinates (max(x), max(y))
119
120
mesh3 = StructuredMesh(cells_per_dimension, coordinates_min3, coordinates_max3,
121
periodicity = false)
122
123
# Define the coupling functions
124
coupling_function34 = (x, u, equations_other, equations_own) -> u
125
coupling_function31 = (x, u, equations_other, equations_own) -> u
126
127
# Define the coupling boundary conditions and the system it is coupled to.
128
boundary_conditions_x_neg3 = BoundaryConditionCoupled(4, (:end, :i_forward), Float64,
129
coupling_function34)
130
boundary_conditions_x_pos3 = BoundaryConditionCoupled(4, (:begin, :i_forward), Float64,
131
coupling_function34)
132
boundary_conditions_y_neg3 = BoundaryConditionCoupled(1, (:i_forward, :end), Float64,
133
coupling_function31)
134
boundary_conditions_y_pos3 = BoundaryConditionCoupled(1, (:i_forward, :begin), Float64,
135
coupling_function31)
136
137
# A semidiscretization collects data structures and functions for the spatial discretization
138
semi3 = SemidiscretizationHyperbolic(mesh3, equations, initial_condition_convergence_test,
139
solver,
140
boundary_conditions = (x_neg = boundary_conditions_x_neg3,
141
x_pos = boundary_conditions_x_pos3,
142
y_neg = boundary_conditions_y_neg3,
143
y_pos = boundary_conditions_y_pos3))
144
145
###########
146
# system #4
147
###########
148
149
coordinates_min4 = (0.0, -1.0) # minimum coordinates (min(x), min(y))
150
coordinates_max4 = (1.0, 0.0) # maximum coordinates (max(x), max(y))
151
152
mesh4 = StructuredMesh(cells_per_dimension, coordinates_min4, coordinates_max4,
153
periodicity = false)
154
155
# Define the coupling functions
156
coupling_function43 = (x, u, equations_other, equations_own) -> u
157
coupling_function42 = (x, u, equations_other, equations_own) -> u
158
159
# Define the coupling boundary conditions and the system it is coupled to.
160
boundary_conditions_x_neg4 = BoundaryConditionCoupled(3, (:end, :i_forward), Float64,
161
coupling_function43)
162
boundary_conditions_x_pos4 = BoundaryConditionCoupled(3, (:begin, :i_forward), Float64,
163
coupling_function43)
164
boundary_conditions_y_neg4 = BoundaryConditionCoupled(2, (:i_forward, :end), Float64,
165
coupling_function42)
166
boundary_conditions_y_pos4 = BoundaryConditionCoupled(2, (:i_forward, :begin), Float64,
167
coupling_function42)
168
169
# A semidiscretization collects data structures and functions for the spatial discretization
170
semi4 = SemidiscretizationHyperbolic(mesh4, equations, initial_condition_convergence_test,
171
solver,
172
boundary_conditions = (x_neg = boundary_conditions_x_neg4,
173
x_pos = boundary_conditions_x_pos4,
174
y_neg = boundary_conditions_y_neg4,
175
y_pos = boundary_conditions_y_pos4))
176
177
# Create a semidiscretization that bundles all the semidiscretizations.
178
semi = SemidiscretizationCoupled(semi1, semi2, semi3, semi4)
179
180
###############################################################################
181
# ODE solvers, callbacks etc.
182
183
# Create ODE problem with time span from 0.0 to 2.0
184
ode = semidiscretize(semi, (0.0, 2.0))
185
186
# At the beginning of the main loop, the SummaryCallback prints a summary of the simulation setup
187
# and resets the timers
188
summary_callback = SummaryCallback()
189
190
# The AnalysisCallback allows to analyse the solution in regular intervals and prints the results
191
analysis_callback1 = AnalysisCallback(semi1, interval = 100)
192
analysis_callback2 = AnalysisCallback(semi2, interval = 100)
193
analysis_callback3 = AnalysisCallback(semi3, interval = 100)
194
analysis_callback4 = AnalysisCallback(semi4, interval = 100)
195
analysis_callback = AnalysisCallbackCoupled(semi, analysis_callback1, analysis_callback2,
196
analysis_callback3, analysis_callback4)
197
198
# The SaveSolutionCallback allows to save the solution to a file in regular intervals
199
save_solution = SaveSolutionCallback(interval = 100,
200
solution_variables = cons2prim)
201
202
# The StepsizeCallback handles the re-calculation of the maximum Δt after each time step
203
stepsize_callback = StepsizeCallback(cfl = 1.6)
204
205
# Create a CallbackSet to collect all callbacks such that they can be passed to the ODE solver
206
callbacks = CallbackSet(summary_callback, analysis_callback, save_solution,
207
stepsize_callback)
208
209
###############################################################################
210
# run the simulation
211
212
# OrdinaryDiffEq's `solve` method evolves the solution in time and executes the passed callbacks
213
sol = solve(ode, CarpenterKennedy2N54(williamson_condition = false);
214
dt = 1.0, # solve needs some value here but it will be overwritten by the stepsize_callback
215
ode_default_options()..., callback = callbacks);
216
217