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_rotated.jl
2055 views
1
# This elixir transforms the setup of elixir_advection_basic to a rotated square.
2
# The nodal values of the initial condition and the exact solution are the same as
3
# in elixir_advection_basic.
4
# However, on this rotated mesh, the metric terms are non-trivial.
5
# The same errors as with elixir_advection_basic are expected (except for rounding errors).
6
7
using OrdinaryDiffEqLowStorageRK
8
using Trixi
9
10
# Define new structs inside a module to allow re-evaluating the file.
11
# This module name needs to be unique among all examples, otherwise Julia will throw warnings
12
# if multiple test cases using the same module name are run in the same session.
13
module TrixiExtensionAdvectionRotated
14
15
using Trixi
16
17
# initial_condition_convergence_test transformed to the rotated rectangle
18
struct InitialConditionConvergenceTestRotated
19
sin_alpha::Float64
20
cos_alpha::Float64
21
end
22
23
function InitialConditionConvergenceTestRotated(alpha)
24
sin_alpha, cos_alpha = sincos(alpha)
25
26
InitialConditionConvergenceTestRotated(sin_alpha, cos_alpha)
27
end
28
29
function (initial_condition::InitialConditionConvergenceTestRotated)(x, t,
30
equation::LinearScalarAdvectionEquation2D)
31
sin_ = initial_condition.sin_alpha
32
cos_ = initial_condition.cos_alpha
33
34
# Rotate back to unit square
35
36
# Clockwise rotation by α and translation by 1
37
# Multiply with [ cos(α) sin(α);
38
# -sin(α) cos(α)]
39
x_rot = SVector(cos_ * x[1] + sin_ * x[2], -sin_ * x[1] + cos_ * x[2])
40
a = equation.advection_velocity
41
a_rot = SVector(cos_ * a[1] + sin_ * a[2], -sin_ * a[1] + cos_ * a[2])
42
43
# Store translated coordinate for easy use of exact solution
44
x_trans = x_rot - a_rot * t
45
46
c = 1.0
47
A = 0.5
48
L = 2
49
f = 1 / L
50
omega = 2 * pi * f
51
scalar = c + A * sin(omega * sum(x_trans))
52
53
return SVector(scalar)
54
end
55
56
end # module TrixiExtensionAdvectionRotated
57
58
import .TrixiExtensionAdvectionRotated
59
60
###############################################################################
61
# semidiscretization of the linear advection equation
62
63
alpha = pi * 0.1
64
initial_condition = TrixiExtensionAdvectionRotated.InitialConditionConvergenceTestRotated(alpha)
65
sin_ = initial_condition.sin_alpha
66
cos_ = initial_condition.cos_alpha
67
T = [cos_ -sin_; sin_ cos_]
68
69
advection_velocity = Tuple(T * [0.2, -0.7])
70
equations = LinearScalarAdvectionEquation2D(advection_velocity)
71
72
# Create DG solver with polynomial degree = 3 and (local) Lax-Friedrichs/Rusanov flux as surface flux
73
solver = DGSEM(polydeg = 3, surface_flux = flux_lax_friedrichs)
74
75
mapping(xi, eta) = T * SVector(xi, eta)
76
77
cells_per_dimension = (16, 16)
78
79
# Create curved mesh with 16 x 16 elements
80
mesh = StructuredMesh(cells_per_dimension, mapping)
81
82
# A semidiscretization collects data structures and functions for the spatial discretization
83
semi = SemidiscretizationHyperbolic(mesh, equations, initial_condition, solver)
84
85
###############################################################################
86
# ODE solvers, callbacks etc.
87
88
# Create ODE problem with time span from 0.0 to 1.0
89
ode = semidiscretize(semi, (0.0, 1.0))
90
91
# At the beginning of the main loop, the SummaryCallback prints a summary of the simulation setup
92
# and resets the timers
93
summary_callback = SummaryCallback()
94
95
# The AnalysisCallback allows to analyse the solution in regular intervals and prints the results
96
analysis_callback = AnalysisCallback(semi, interval = 100)
97
98
# The SaveSolutionCallback allows to save the solution to a file in regular intervals
99
save_solution = SaveSolutionCallback(interval = 100,
100
solution_variables = cons2prim)
101
102
# The StepsizeCallback handles the re-calculation of the maximum Δt after each time step
103
stepsize_callback = StepsizeCallback(cfl = 1.6)
104
105
# Create a CallbackSet to collect all callbacks such that they can be passed to the ODE solver
106
callbacks = CallbackSet(summary_callback, analysis_callback, save_solution,
107
stepsize_callback)
108
109
###############################################################################
110
# run the simulation
111
112
# OrdinaryDiffEq's `solve` method evolves the solution in time and executes the passed callbacks
113
sol = solve(ode, CarpenterKennedy2N54(williamson_condition = false);
114
dt = 1.0, # solve needs some value here but it will be overwritten by the stepsize_callback
115
ode_default_options()..., callback = callbacks);
116
117