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
5586 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
periodicity = true)
46
47
# Create the two mesh views, each of which takes half of the parent mesh.
48
mesh1 = StructuredMeshView(parent_mesh; indices_min = (1, 1), indices_max = (8, 16))
49
mesh2 = StructuredMeshView(parent_mesh; indices_min = (9, 1), indices_max = (16, 16))
50
51
# The coupling function is simply the identity, as we are dealing with two identical systems.
52
coupling_function = (x, u, equations_other, equations_own) -> u
53
54
# Define the coupled boundary conditions
55
# The indices (:end, :i_forward) and (:begin, :i_forward) denote the interface indexing.
56
# For a system with coupling in x and y see examples/structured_2d_dgsem/elixir_advection_coupled.jl.
57
boundary_conditions1 = (
58
# Connect left boundary with right boundary of left mesh
59
x_neg = BoundaryConditionCoupled(2, (:end, :i_forward), Float64,
60
coupling_function),
61
x_pos = BoundaryConditionCoupled(2, (:begin, :i_forward), Float64,
62
coupling_function),
63
y_neg = boundary_condition_periodic,
64
y_pos = boundary_condition_periodic)
65
boundary_conditions2 = (
66
# Connect left boundary with right boundary of left mesh
67
x_neg = BoundaryConditionCoupled(1, (:end, :i_forward), Float64,
68
coupling_function),
69
x_pos = BoundaryConditionCoupled(1, (:begin, :i_forward), Float64,
70
coupling_function),
71
y_neg = boundary_condition_periodic,
72
y_pos = boundary_condition_periodic)
73
74
# A semidiscretization collects data structures and functions for the spatial discretization
75
semi1 = SemidiscretizationHyperbolic(mesh1, equations, initial_condition_convergence_test,
76
solver; boundary_conditions = boundary_conditions1)
77
semi2 = SemidiscretizationHyperbolic(mesh2, equations, initial_condition_convergence_test,
78
solver; boundary_conditions = boundary_conditions2)
79
semi = SemidiscretizationCoupled(semi1, semi2)
80
81
###############################################################################
82
# ODE solvers, callbacks etc.
83
84
# Create ODE problem with time span from 0.0 to 1.0
85
ode = semidiscretize(semi, (0.0, 1.0))
86
87
# At the beginning of the main loop, the SummaryCallback prints a summary of the simulation setup
88
# and resets the timers
89
summary_callback = SummaryCallback()
90
91
# The AnalysisCallback allows to analyse the solution in regular intervals and prints the results
92
analysis_callback1 = AnalysisCallback(semi1, interval = 100)
93
analysis_callback2 = AnalysisCallback(semi2, interval = 100)
94
analysis_callback = AnalysisCallbackCoupled(semi, analysis_callback1, analysis_callback2)
95
96
analysis_interval = 100
97
alive_callback = AliveCallback(analysis_interval = analysis_interval)
98
99
# The SaveSolutionCallback allows to save the solution to a file in regular intervals
100
save_solution = SaveSolutionCallback(interval = 100,
101
save_initial_solution = true,
102
save_final_solution = true,
103
solution_variables = cons2prim)
104
105
# The StepsizeCallback handles the re-calculation of the maximum Δt after each time step
106
stepsize_callback = StepsizeCallback(cfl = 1.6)
107
108
# Create a CallbackSet to collect all callbacks such that they can be passed to the ODE solver
109
callbacks = CallbackSet(summary_callback, analysis_callback, save_solution,
110
stepsize_callback)
111
112
###############################################################################
113
# run the simulation
114
115
# OrdinaryDiffEq's `solve` method evolves the solution in time and executes the passed callbacks
116
sol = solve(ode, CarpenterKennedy2N54(williamson_condition = false);
117
dt = 5.0e-2, # solve needs some value here but it will be overwritten by the stepsize_callback
118
ode_default_options()..., callback = callbacks);
119
120