forked from trixi-framework/TrixiAtmo.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalysis_covariant.jl
More file actions
209 lines (179 loc) · 9.23 KB
/
analysis_covariant.jl
File metadata and controls
209 lines (179 loc) · 9.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
@muladd begin
#! format: noindent
# When the equations are of type AbstractCovariantEquations, the functions which we would
# like to integrate depend on the solution as well as the auxiliary variables
function Trixi.integrate(func::Func, u,
mesh::Union{TreeMesh{2}, StructuredMesh{2},
StructuredMeshView{2},
UnstructuredMesh2D, P4estMesh{2}, T8codeMesh{2}},
equations::AbstractCovariantEquations{2},
dg::Union{DGSEM, FDSBP},
cache; normalize = true) where {Func}
(; aux_node_vars) = cache.auxiliary_variables
Trixi.integrate_via_indices(u, mesh, equations, dg, cache;
normalize = normalize) do u, i, j, element, equations,
dg
u_local = Trixi.get_node_vars(u, equations, dg, i, j, element)
aux_local = get_node_aux_vars(aux_node_vars, equations, dg, i, j, element)
return func(u_local, aux_local, equations)
end
end
function Trixi.integrate(func::Func, u,
mesh::DGMultiMesh,
equations::AbstractCovariantEquations,
dg::DGMulti, cache; normalize = true) where {Func}
rd = dg.basis
md = mesh.md
@unpack u_values, aux_quad_values = cache
# interpolate u to quadrature points
Trixi.apply_to_each_field(Trixi.mul_by!(rd.Vq), u_values, u)
integral = zero(func(u_values[1], equations))
total_volume = zero(sum(rd.wq))
for element in Trixi.eachelement(dg, cache)
weights = area_element.(aux_quad_values[:, element], equations) .* rd.wq
integral += sum(weights .* func.(u_values[:, element], equations))
total_volume += sum(weights)
end
if normalize == true
integral /= total_volume
end
return integral
end
# For the covariant form, we want to integrate using the exact area element
# J = √G = (det(AᵀA))^(1/2), which is stored in cache.auxiliary_variables, not the approximate
# area element used in the Cartesian formulation, which stored in cache.elements
function Trixi.integrate_via_indices(func::Func, u,
mesh::Union{StructuredMesh{2},
StructuredMeshView{2},
UnstructuredMesh2D, P4estMesh{2},
T8codeMesh{2}},
equations::AbstractCovariantEquations{2},
dg::DGSEM, cache, args...;
normalize = true) where {Func}
(; weights) = dg.basis
(; aux_node_vars) = cache.auxiliary_variables
# Initialize integral with zeros of the right shape
integral = zero(func(u, 1, 1, 1, equations, dg, args...))
total_volume = zero(real(mesh))
# Use quadrature to numerically integrate over entire domain
for element in eachelement(dg, cache)
for j in eachnode(dg), i in eachnode(dg)
aux_node = get_node_aux_vars(aux_node_vars, equations, dg, i, j, element)
J = area_element(aux_node, equations)
integral += weights[i] * weights[j] * J *
func(u, i, j, element, equations, dg, args...)
total_volume += weights[i] * weights[j] * J
end
end
# Normalize with total volume
if normalize
integral = integral / total_volume
end
return integral
end
# Entropy time derivative for cons2entropy function which depends on auxiliary variables
function Trixi.analyze(::typeof(Trixi.entropy_timederivative), du, u, t,
mesh::P4estMesh{2}, equations::AbstractCovariantEquations{2},
dg::Union{DGSEM, FDSBP}, cache)
(; aux_node_vars) = cache.auxiliary_variables
# Calculate ∫(∂S/∂u ⋅ ∂u/∂t)dΩ
Trixi.integrate_via_indices(u, mesh, equations, dg, cache,
du) do u, i, j, element, equations, dg, du_node
# Get auxiliary variables, solution variables, and time derivative at given node
aux_node = get_node_aux_vars(aux_node_vars, equations, dg, i, j, element)
u_node = Trixi.get_node_vars(u, equations, dg, i, j, element)
du_node = Trixi.get_node_vars(du, equations, dg, i, j, element)
# compute ∂S/∂u ⋅ ∂u/∂t, where the entropy variables ∂S/∂u depend on the solution
# and auxiliary variables
dot(cons2entropy(u_node, aux_node, equations), du_node)
end
end
# Entropy time derivative for cons2entropy function which depends on auxiliary variables
function Trixi.analyze(::typeof(Trixi.entropy_timederivative), du, u, t,
mesh::DGMultiMesh, equations::AbstractCovariantEquations,
dg::DGMulti, cache)
rd = dg.basis
md = mesh.md
@unpack u_values, aux_quad_values = cache
# interpolate u, du to quadrature points
du_values = similar(u_values)
Trixi.apply_to_each_field(Trixi.mul_by!(rd.Vq), du_values, du)
Trixi.apply_to_each_field(Trixi.mul_by!(rd.Vq), u_values, u)
# compute ∫v(u) * du/dt = ∫dS/dt. We can directly compute v(u) instead of computing the entropy
# projection here, since the RHS will be projected to polynomials of degree N and testing with
# the L2 projection of v(u) would be equivalent to testing with v(u) due to the moment-preserving
# property of the L2 projection.
dS_dt = zero(eltype(first(du)))
for i in Base.OneTo(length(md.wJq))
ref_index = mod(i - 1, rd.Nq) + 1
node_weight = rd.wq[ref_index] * area_element(aux_quad_values[i], equations)
dS_dt += dot(cons2entropy(u_values[i], aux_quad_values[i], equations),
du_values[i]) * node_weight
end
return dS_dt
end
# L2 and Linf error calculation for the covariant form
function Trixi.calc_error_norms(func, u, t, analyzer, mesh::P4estMesh{2},
equations::AbstractCovariantEquations{2},
initial_condition, dg::DGSEM, cache, cache_analysis)
(; weights) = dg.basis
(; node_coordinates) = cache.elements
(; aux_node_vars) = cache.auxiliary_variables
# Set up data structures
l2_error = zero(func(Trixi.get_node_vars(u, equations, dg, 1, 1, 1), equations))
linf_error = copy(l2_error)
total_volume = zero(real(mesh))
# Iterate over all elements for error calculations
for element in eachelement(dg, cache)
# Calculate errors at each volume quadrature node
for j in eachnode(dg), i in eachnode(dg)
x_node = Trixi.get_node_coords(node_coordinates, equations, dg, i, j,
element)
# Convert exact solution into contravariant components using geometric
# information stored in aux vars
aux_node = get_node_aux_vars(aux_node_vars, equations, dg, i, j, element)
u_exact = initial_condition(x_node, t, aux_node, equations)
# Compute the difference as usual
u_numerical = Trixi.get_node_vars(u, equations, dg, i, j, element)
diff = func(u_exact, equations) - func(u_numerical, equations)
# For the L2 error, integrate with respect to area element stored in aux vars
J = area_element(aux_node, equations)
l2_error += diff .^ 2 * (weights[i] * weights[j] * J)
# Compute Linf error as usual
linf_error = @. max(linf_error, abs(diff))
# Increment total volume according to the volume element stored in aux vars
total_volume += weights[i] * weights[j] * J
end
end
# For L2 error, divide by total volume
l2_error = @. sqrt(l2_error / total_volume)
return l2_error, linf_error
end
# L2 and Linf error calculation for the covariant form
function Trixi.calc_error_norms(func, u, t, analyzer,
mesh::DGMultiMesh{NDIMS_AMBIENT},
equations::AbstractCovariantEquations,
initial_condition,
dg::DGMulti{NDIMS}, cache,
cache_analysis) where {NDIMS, NDIMS_AMBIENT}
rd = dg.basis
md = mesh.md
@unpack u_values, aux_quad_values = cache
# interpolate u to quadrature points
Trixi.apply_to_each_field(Trixi.mul_by!(rd.Vq), u_values, u)
component_l2_errors = zero(eltype(u_values))
component_linf_errors = zero(eltype(u_values))
total_volume = zero(eltype(u_values[1]))
for i in Trixi.each_quad_node_global(mesh, dg, cache)
u_exact = initial_condition(SVector(getindex.(md.xyzq, i)), t,
aux_quad_values[i], equations)
error_at_node = func(u_values[i], equations) - func(u_exact, equations)
ref_index = mod(i - 1, rd.Nq) + 1
node_weight = rd.wq[ref_index] * area_element(aux_quad_values[i], equations)
component_l2_errors += node_weight * error_at_node .^ 2
component_linf_errors = max.(component_linf_errors, abs.(error_at_node))
total_volume += node_weight
end
return sqrt.(component_l2_errors ./ total_volume), component_linf_errors
end
end # @muladd