Line data Source code
1 0 : // Distributed under the MIT License.
2 : // See LICENSE.txt for details.
3 :
4 : #pragma once
5 :
6 : #include <array>
7 : #include <cstddef>
8 : #include <optional>
9 : #include <type_traits>
10 :
11 : #include "DataStructures/DataBox/AsAccess.hpp"
12 : #include "DataStructures/DataBox/DataBox.hpp"
13 : #include "DataStructures/DataBox/PrefixHelpers.hpp"
14 : #include "DataStructures/DataBox/Prefixes.hpp"
15 : #include "DataStructures/DataVector.hpp"
16 : #include "DataStructures/TaggedContainers.hpp"
17 : #include "DataStructures/Tensor/Tensor.hpp"
18 : #include "DataStructures/Variables.hpp"
19 : #include "DataStructures/VectorImpl.hpp"
20 : #include "Domain/Structure/Element.hpp"
21 : #include "Domain/Tags.hpp"
22 : #include "Domain/TagsTimeDependent.hpp"
23 : #include "Evolution/BoundaryCorrection.hpp"
24 : #include "Evolution/BoundaryCorrectionTags.hpp"
25 : #include "Evolution/DgSubcell/CartesianFluxDivergence.hpp"
26 : #include "Evolution/DgSubcell/ComputeBoundaryTerms.hpp"
27 : #include "Evolution/DgSubcell/CorrectPackagedData.hpp"
28 : #include "Evolution/DgSubcell/Projection.hpp"
29 : #include "Evolution/DgSubcell/ReconstructionOrder.hpp"
30 : #include "Evolution/DgSubcell/Tags/Coordinates.hpp"
31 : #include "Evolution/DgSubcell/Tags/GhostDataForReconstruction.hpp"
32 : #include "Evolution/DgSubcell/Tags/Jacobians.hpp"
33 : #include "Evolution/DgSubcell/Tags/Mesh.hpp"
34 : #include "Evolution/DgSubcell/Tags/OnSubcellFaces.hpp"
35 : #include "Evolution/DiscontinuousGalerkin/Actions/NormalCovectorAndMagnitude.hpp"
36 : #include "Evolution/DiscontinuousGalerkin/Actions/PackageDataImpl.hpp"
37 : #include "Evolution/DiscontinuousGalerkin/MortarTags.hpp"
38 : #include "Evolution/Systems/GrMhd/GhValenciaDivClean/AllSolutions.hpp"
39 : #include "Evolution/Systems/GrMhd/GhValenciaDivClean/FiniteDifference/BoundaryConditionGhostData.hpp"
40 : #include "Evolution/Systems/GrMhd/GhValenciaDivClean/FiniteDifference/Derivatives.hpp"
41 : #include "Evolution/Systems/GrMhd/GhValenciaDivClean/FiniteDifference/FilterOptions.hpp"
42 : #include "Evolution/Systems/GrMhd/GhValenciaDivClean/FiniteDifference/Filters.hpp"
43 : #include "Evolution/Systems/GrMhd/GhValenciaDivClean/FiniteDifference/Reconstructor.hpp"
44 : #include "Evolution/Systems/GrMhd/GhValenciaDivClean/FiniteDifference/Tag.hpp"
45 : #include "Evolution/Systems/GrMhd/GhValenciaDivClean/StressEnergy.hpp"
46 : #include "Evolution/Systems/GrMhd/GhValenciaDivClean/System.hpp"
47 : #include "Evolution/Systems/GrMhd/GhValenciaDivClean/Tags.hpp"
48 : #include "Evolution/Systems/GrMhd/GhValenciaDivClean/TimeDerivativeTerms.hpp"
49 : #include "Evolution/Systems/GrMhd/ValenciaDivClean/Fluxes.hpp"
50 : #include "Evolution/Systems/GrMhd/ValenciaDivClean/Sources.hpp"
51 : #include "Evolution/Systems/GrMhd/ValenciaDivClean/Subcell/ComputeFluxes.hpp"
52 : #include "Evolution/Systems/GrMhd/ValenciaDivClean/TimeDerivativeTerms.hpp"
53 : #include "NumericalAlgorithms/FiniteDifference/PartialDerivatives.hpp"
54 : #include "NumericalAlgorithms/Spectral/Mesh.hpp"
55 : #include "NumericalAlgorithms/Spectral/Parity.hpp"
56 : #include "PointwiseFunctions/GeneralRelativity/GeneralizedHarmonic/DerivSpatialMetric.hpp"
57 : #include "PointwiseFunctions/GeneralRelativity/GeneralizedHarmonic/ExtrinsicCurvature.hpp"
58 : #include "PointwiseFunctions/GeneralRelativity/GeneralizedHarmonic/SpatialDerivOfLapse.hpp"
59 : #include "PointwiseFunctions/GeneralRelativity/GeneralizedHarmonic/SpatialDerivOfShift.hpp"
60 : #include "PointwiseFunctions/Hydro/Tags.hpp"
61 : #include "Utilities/CallWithDynamicType.hpp"
62 : #include "Utilities/ErrorHandling/Assert.hpp"
63 : #include "Utilities/Gsl.hpp"
64 : #include "Utilities/TMPL.hpp"
65 :
66 : /// \cond
67 : namespace Tags {
68 : struct Time;
69 : } // namespace Tags
70 : /// \endcond
71 :
72 : namespace grmhd::GhValenciaDivClean::subcell {
73 : namespace detail {
74 : template <class GhDtTagsList, class GhTemporariesList, class GhGradientTagsList,
75 : class GhExtraTagsList, class GrmhdDtTagsList,
76 : class GrmhdSourceTagsList, class GrmhdArgumentSourceTagsList,
77 : typename System>
78 : struct ComputeTimeDerivImpl;
79 :
80 : template <class... GhDtTags, class... GhTemporaries, class... GhGradientTags,
81 : class... GhExtraTags, class... GrmhdDtTags, class... GrmhdSourceTags,
82 : class... GrmhdArgumentSourceTags, typename System>
83 : struct ComputeTimeDerivImpl<
84 : tmpl::list<GhDtTags...>, tmpl::list<GhTemporaries...>,
85 : tmpl::list<GhGradientTags...>, tmpl::list<GhExtraTags...>,
86 : tmpl::list<GrmhdDtTags...>, tmpl::list<GrmhdSourceTags...>,
87 : tmpl::list<GrmhdArgumentSourceTags...>, System> {
88 : template <class DbTagsList>
89 : static void apply(
90 : const gsl::not_null<db::DataBox<DbTagsList>*> box,
91 : const tnsr::I<DataVector, 3, Frame::Inertial>& inertial_coords,
92 : const Scalar<DataVector>& cell_centered_det_inv_jacobian,
93 : const InverseJacobian<DataVector, 3, Frame::ElementLogical,
94 : Frame::Inertial>&
95 : cell_centered_logical_to_inertial_inv_jacobian,
96 : const std::array<double, 3>& one_over_delta_xi,
97 : const std::array<Variables<tmpl::list<GrmhdDtTags...>>, 3>&
98 : boundary_corrections,
99 : const Variables<
100 : db::wrap_tags_in<::Tags::deriv, typename System::gradients_tags,
101 : tmpl::size_t<3>, Frame::Inertial>>& gh_derivs) {
102 : const Mesh<3>& subcell_mesh =
103 : db::get<evolution::dg::subcell::Tags::Mesh<3>>(*box);
104 : const size_t number_of_points = subcell_mesh.number_of_grid_points();
105 : // Note: GH+GRMHD tags are always GH,GRMHD
106 : using deriv_lapse = ::Tags::deriv<gr::Tags::Lapse<DataVector>,
107 : tmpl::size_t<3>, Frame::Inertial>;
108 : using deriv_shift = ::Tags::deriv<gr::Tags::Shift<DataVector, 3>,
109 : tmpl::size_t<3>, Frame::Inertial>;
110 : using deriv_spatial_metric =
111 : ::Tags::deriv<gr::Tags::SpatialMetric<DataVector, 3>, tmpl::size_t<3>,
112 : Frame::Inertial>;
113 : using extra_tags_for_grmhd =
114 : tmpl::list<deriv_lapse, deriv_shift, deriv_spatial_metric,
115 : gr::Tags::ExtrinsicCurvature<DataVector, 3>>;
116 : using temporary_tags = tmpl::remove_duplicates<tmpl::append<
117 : typename gh::TimeDerivative<ghmhd::GhValenciaDivClean::InitialData::
118 : analytic_solutions_and_data_list,
119 : 3_st>::temporary_tags,
120 : tmpl::push_front<typename grmhd::ValenciaDivClean::TimeDerivativeTerms::
121 : temporary_tags,
122 : ::gh::Tags::ConstraintGamma0>,
123 : extra_tags_for_grmhd,
124 : tmpl::list<
125 : Tags::TraceReversedStressEnergy, Tags::FourVelocityOneForm,
126 : grmhd::ValenciaDivClean::Tags::ComovingMagneticFieldOneForm>>>;
127 : Variables<temporary_tags> temp_tags{subcell_mesh.number_of_grid_points()};
128 : const auto temp_tags_ptr = make_not_null(&temp_tags);
129 :
130 : // Compute constraint damping terms.
131 : const double time = db::get<::Tags::Time>(*box);
132 : const auto& functions_of_time =
133 : db::get<::domain::Tags::FunctionsOfTime>(*box);
134 : const auto& grid_coords =
135 : db::get<evolution::dg::subcell::Tags::Coordinates<3, Frame::Grid>>(
136 : *box);
137 : db::get<gh::Tags::DampingFunctionGamma0<3, Frame::Grid>> (*box)(
138 : get<gh::Tags::ConstraintGamma0>(temp_tags_ptr), grid_coords, time,
139 : functions_of_time);
140 : db::get<gh::Tags::DampingFunctionGamma1<3, Frame::Grid>> (*box)(
141 : get<gh::Tags::ConstraintGamma1>(temp_tags_ptr), grid_coords, time,
142 : functions_of_time);
143 : db::get<gh::Tags::DampingFunctionGamma2<3, Frame::Grid>> (*box)(
144 : get<gh::Tags::ConstraintGamma2>(temp_tags_ptr), grid_coords, time,
145 : functions_of_time);
146 :
147 : using variables_tag = typename System::variables_tag;
148 : using dt_variables_tag = db::add_tag_prefix<::Tags::dt, variables_tag>;
149 : const gsl::not_null<typename dt_variables_tag::type*> dt_vars_ptr =
150 : db::mutate<dt_variables_tag>(
151 : [](const auto local_dt_vars_ptr) { return local_dt_vars_ptr; },
152 : box);
153 : dt_vars_ptr->initialize(subcell_mesh.number_of_grid_points());
154 :
155 : using primitives_tag = typename System::primitive_variables_tag;
156 : using evolved_vars_tag = typename System::variables_tag;
157 :
158 : const auto& primitive_vars = db::get<primitives_tag>(*box);
159 : const auto& evolved_vars = db::get<evolved_vars_tag>(*box);
160 :
161 : // Velocity of the moving mesh, if applicable. We project the value
162 : // stored on the DG grid onto the subcell grid.
163 : const Mesh<3>& dg_mesh = db::get<domain::Tags::Mesh<3>>(*box);
164 : const std::optional<tnsr::I<DataVector, 3, Frame::Inertial>>&
165 : mesh_velocity_dg = db::get<domain::Tags::MeshVelocity<3>>(*box);
166 : const std::optional<Scalar<DataVector>>& div_mesh_velocity_dg =
167 : db::get<domain::Tags::DivMeshVelocity>(*box);
168 : std::optional<tnsr::I<DataVector, 3, Frame::Inertial>>
169 : mesh_velocity_subcell = {};
170 : if (mesh_velocity_dg.has_value()) {
171 : mesh_velocity_subcell = tnsr::I<DataVector, 3, Frame::Inertial>{
172 : subcell_mesh.number_of_grid_points()};
173 : for (size_t i = 0; i < 3; i++) {
174 : mesh_velocity_subcell.value().get(i) =
175 : evolution::dg::subcell::fd::project(
176 : mesh_velocity_dg.value().get(i), dg_mesh,
177 : subcell_mesh.extents(),
178 : i == 0 ? Spectral::Parity::Odd : Spectral::Parity::Even);
179 : }
180 : }
181 :
182 : gh::TimeDerivative<
183 : ghmhd::GhValenciaDivClean::InitialData::
184 : analytic_solutions_and_data_list,
185 : 3_st>::apply(get<::Tags::dt<GhDtTags>>(dt_vars_ptr)...,
186 : get<GhTemporaries>(temp_tags_ptr)...,
187 : get<::Tags::deriv<GhGradientTags, tmpl::size_t<3>,
188 : Frame::Inertial>>(gh_derivs)...,
189 : get<GhExtraTags>(evolved_vars, temp_tags)...,
190 :
191 : db::get<::gh::gauges::Tags::GaugeCondition>(*box),
192 : db::get<evolution::dg::subcell::Tags::Mesh<3>>(*box), time,
193 : inertial_coords,
194 : cell_centered_logical_to_inertial_inv_jacobian,
195 : mesh_velocity_subcell);
196 : if (get<gh::gauges::Tags::GaugeCondition>(*box).is_harmonic()) {
197 : get(get<gr::Tags::SqrtDetSpatialMetric<DataVector>>(*temp_tags_ptr)) =
198 : sqrt(
199 : get(get<gr::Tags::DetSpatialMetric<DataVector>>(*temp_tags_ptr)));
200 : }
201 :
202 : // Add source terms from moving mesh
203 : if (mesh_velocity_dg.has_value()) {
204 : tmpl::for_each<tmpl::list<GhDtTags...>>([&dt_vars_ptr,
205 : &mesh_velocity_subcell,
206 : &gh_derivs](
207 : auto evolved_var_tag_v) {
208 : using evolved_var_tag = tmpl::type_from<decltype(evolved_var_tag_v)>;
209 : using dt_tag = ::Tags::dt<evolved_var_tag>;
210 : using grad_tag =
211 : ::Tags::deriv<evolved_var_tag, tmpl::size_t<3>, Frame::Inertial>;
212 : // Flux and gradients use the same indexing conventions,
213 : // replacing the direction of the face with the direction
214 : // of the derivative.
215 : using FluxTensor = typename grad_tag::type;
216 : auto& dt_var = get<dt_tag>(*dt_vars_ptr);
217 : const auto& grad_var = get<grad_tag>(gh_derivs);
218 : for (size_t i = 0; i < dt_var.size(); ++i) {
219 : const auto tensor_index = dt_var.get_tensor_index(i);
220 : for (size_t j = 0; j < 3; j++) {
221 : const auto grad_index =
222 : FluxTensor::get_storage_index(prepend(tensor_index, j));
223 : // Add (mesh_velocity)^j grad_j (var[i])
224 : dt_var[i] +=
225 : mesh_velocity_subcell.value().get(j) * grad_var[grad_index];
226 : }
227 : }
228 : });
229 : }
230 :
231 : {
232 : // Set extra tags needed for GRMHD source terms. We compute these from
233 : // quantities already computed inside the GH RHS computation to minimize
234 : // FLOPs.
235 : const auto& lapse = get<gr::Tags::Lapse<DataVector>>(temp_tags);
236 : const auto& half_phi_two_normals =
237 : get<gh::Tags::HalfPhiTwoNormals<3>>(temp_tags);
238 : const auto& phi = get<gh::Tags::Phi<DataVector, 3>>(evolved_vars);
239 : const auto& phi_one_normal = get<gh::Tags::PhiOneNormal<3>>(temp_tags);
240 : const auto& spacetime_normal_vector =
241 : get<gr::Tags::SpacetimeNormalVector<DataVector, 3>>(temp_tags);
242 : const auto& inverse_spacetime_metric =
243 : get<gr::Tags::InverseSpacetimeMetric<DataVector, 3>>(temp_tags);
244 :
245 : auto& spatial_deriv_lapse = get<deriv_lapse>(temp_tags);
246 : auto& spatial_deriv_shift = get<deriv_shift>(temp_tags);
247 : // Compute d_i beta^i
248 : for (size_t i = 0; i < 3; ++i) {
249 : // Use spatial_deriv_lapse as temp buffer to reduce number of 2*
250 : // operations.
251 : const auto& phi_two_normals_i = spatial_deriv_lapse.get(i) =
252 : 2.0 * half_phi_two_normals.get(i);
253 : for (size_t j = 0; j < 3; ++j) {
254 : spatial_deriv_shift.get(i, j) =
255 : spacetime_normal_vector.get(j + 1) * phi_two_normals_i;
256 : for (size_t a = 0; a < 4; ++a) {
257 : spatial_deriv_shift.get(i, j) +=
258 : inverse_spacetime_metric.get(j + 1, a) *
259 : phi_one_normal.get(i, a);
260 : }
261 : spatial_deriv_shift.get(i, j) *= get(lapse);
262 : }
263 : }
264 :
265 : // Compute d_i lapse
266 : for (size_t i = 0; i < 3; ++i) {
267 : spatial_deriv_lapse.get(i) = -get(lapse) * half_phi_two_normals.get(i);
268 : }
269 : // Extract d_i \gamma_{ij}
270 : for (size_t k = 0; k < 3; ++k) {
271 : for (size_t i = 0; i < 3; ++i) {
272 : for (size_t j = i; j < 3; ++j) {
273 : get<deriv_spatial_metric>(temp_tags).get(k, i, j) =
274 : phi.get(k, i + 1, j + 1);
275 : }
276 : }
277 : }
278 :
279 : // Compute extrinsic curvature
280 : const auto& pi = get<gh::Tags::Pi<DataVector, 3>>(evolved_vars);
281 : for (size_t i = 0; i < 3; ++i) {
282 : for (size_t j = i; j < 3; ++j) {
283 : get<gr::Tags::ExtrinsicCurvature<DataVector, 3>>(temp_tags).get(i,
284 : j) =
285 : 0.5 * (pi.get(i + 1, j + 1) + phi_one_normal.get(i, j + 1) +
286 : phi_one_normal.get(j, i + 1));
287 : }
288 : }
289 : } // End scope for computing metric terms in GRMHD source terms.
290 :
291 : grmhd::ValenciaDivClean::ComputeSources::apply(
292 : get<::Tags::dt<GrmhdSourceTags>>(dt_vars_ptr)...,
293 : get<GrmhdArgumentSourceTags>(temp_tags, primitive_vars, evolved_vars,
294 : *box)...);
295 :
296 : // Zero GRMHD tags that don't have sources.
297 : tmpl::for_each<tmpl::list<GrmhdDtTags...>>([&dt_vars_ptr](
298 : auto evolved_var_tag_v) {
299 : using evolved_var_tag = tmpl::type_from<decltype(evolved_var_tag_v)>;
300 : using dt_tag = ::Tags::dt<evolved_var_tag>;
301 : auto& dt_var = get<dt_tag>(*dt_vars_ptr);
302 : for (size_t i = 0; i < dt_var.size(); ++i) {
303 : if constexpr (not tmpl::list_contains_v<tmpl::list<GrmhdSourceTags...>,
304 : evolved_var_tag>) {
305 : // Zero the GRMHD dt(u) for variables that do not have a source term .
306 : // This is necessary to avoid `+=` to a `NaN` (debug mode) or random
307 : // garbage (release mode) when adding to dt_var below.
308 : dt_var[i] = 0.0;
309 : }
310 : }
311 : });
312 : // Correction to source terms due to moving mesh
313 : if (div_mesh_velocity_dg.has_value()) {
314 : const DataVector div_mesh_velocity_subcell =
315 : evolution::dg::subcell::fd::project(
316 : div_mesh_velocity_dg.value().get(), dg_mesh,
317 : subcell_mesh.extents(), Spectral::Parity::Even);
318 : tmpl::for_each<tmpl::list<GrmhdDtTags...>>(
319 : [&dt_vars_ptr, &div_mesh_velocity_subcell,
320 : &evolved_vars](auto evolved_var_tag_v) {
321 : using evolved_var_tag =
322 : tmpl::type_from<decltype(evolved_var_tag_v)>;
323 : using dt_tag = ::Tags::dt<evolved_var_tag>;
324 : auto& dt_var = get<dt_tag>(*dt_vars_ptr);
325 : const auto& evolved_var = get<evolved_var_tag>(evolved_vars);
326 : for (size_t i = 0; i < dt_var.size(); ++i) {
327 : dt_var[i] -= div_mesh_velocity_subcell * evolved_var[i];
328 : }
329 : });
330 : }
331 :
332 : const tnsr::ii<DataVector, 3> spatial_metric{};
333 : for (size_t i = 0; i < 3; ++i) {
334 : for (size_t j = i; j < 3; ++j) {
335 : make_const_view(
336 : make_not_null(&spatial_metric.get(i, j)),
337 : get<gr::Tags::SpacetimeMetric<DataVector, 3>>(evolved_vars)
338 : .get(i + 1, j + 1),
339 : 0, number_of_points);
340 : }
341 : }
342 :
343 : tenex::evaluate<ti::i>(get<hydro::Tags::SpatialVelocityOneForm<
344 : DataVector, 3, Frame::Inertial>>(temp_tags_ptr),
345 : get<hydro::Tags::SpatialVelocity<DataVector, 3>>(
346 : primitive_vars)(ti::J) *
347 : spatial_metric(ti::i, ti::j));
348 :
349 : tenex::evaluate<ti::i>(
350 : get<hydro::Tags::MagneticFieldOneForm<DataVector, 3, Frame::Inertial>>(
351 : temp_tags_ptr),
352 : get<hydro::Tags::MagneticField<DataVector, 3>>(primitive_vars)(ti::J) *
353 : spatial_metric(ti::i, ti::j));
354 :
355 : tenex::evaluate(
356 : get<hydro::Tags::MagneticFieldSquared<DataVector>>(temp_tags_ptr),
357 : get<hydro::Tags::MagneticField<DataVector, 3>>(primitive_vars)(ti::J) *
358 : get<hydro::Tags::MagneticFieldOneForm<DataVector, 3>>(temp_tags)(
359 : ti::j));
360 :
361 : tenex::evaluate(
362 : get<hydro::Tags::MagneticFieldDotSpatialVelocity<DataVector>>(
363 : temp_tags_ptr),
364 : get<hydro::Tags::SpatialVelocity<DataVector, 3>>(primitive_vars)(
365 : ti::J) *
366 : get<hydro::Tags::MagneticFieldOneForm<DataVector, 3>>(temp_tags)(
367 : ti::j));
368 :
369 : tenex::evaluate(get<typename ValenciaDivClean::TimeDerivativeTerms::
370 : OneOverLorentzFactorSquared>(temp_tags_ptr),
371 : 1.0 / (square(get<hydro::Tags::LorentzFactor<DataVector>>(
372 : primitive_vars)())));
373 :
374 : trace_reversed_stress_energy(
375 : get<Tags::TraceReversedStressEnergy>(temp_tags_ptr),
376 : get<Tags::FourVelocityOneForm>(temp_tags_ptr),
377 : get<grmhd::ValenciaDivClean::Tags::ComovingMagneticFieldOneForm>(
378 : temp_tags_ptr),
379 :
380 : get<hydro::Tags::RestMassDensity<DataVector>>(evolved_vars, temp_tags,
381 : primitive_vars),
382 : get<hydro::Tags::SpatialVelocityOneForm<DataVector, 3,
383 : Frame::Inertial>>(
384 : evolved_vars, temp_tags, primitive_vars),
385 :
386 : get<hydro::Tags::MagneticFieldOneForm<DataVector, 3, Frame::Inertial>>(
387 : evolved_vars, temp_tags, primitive_vars),
388 :
389 : get<hydro::Tags::MagneticFieldSquared<DataVector>>(
390 : evolved_vars, temp_tags, primitive_vars),
391 :
392 : get<hydro::Tags::MagneticFieldDotSpatialVelocity<DataVector>>(
393 : evolved_vars, temp_tags, primitive_vars),
394 : get<hydro::Tags::LorentzFactor<DataVector>>(evolved_vars, temp_tags,
395 : primitive_vars),
396 : get<typename ValenciaDivClean::TimeDerivativeTerms::
397 : OneOverLorentzFactorSquared>(evolved_vars, temp_tags,
398 : primitive_vars),
399 : get<hydro::Tags::Pressure<DataVector>>(evolved_vars, temp_tags,
400 : primitive_vars),
401 : get<hydro::Tags::SpecificInternalEnergy<DataVector>>(
402 : evolved_vars, temp_tags, primitive_vars),
403 : get<gr::Tags::SpacetimeMetric<DataVector, 3>>(evolved_vars, temp_tags,
404 : primitive_vars),
405 : get<gr::Tags::Shift<DataVector, 3>>(evolved_vars, temp_tags,
406 : primitive_vars),
407 : get<gr::Tags::Lapse<DataVector>>(evolved_vars, temp_tags,
408 : primitive_vars));
409 :
410 : add_stress_energy_term_to_dt_pi(
411 : get<::Tags::dt<gh::Tags::Pi<DataVector, 3>>>(dt_vars_ptr),
412 : get<Tags::TraceReversedStressEnergy>(temp_tags),
413 : get<gr::Tags::Lapse<DataVector>>(temp_tags));
414 :
415 : for (size_t dim = 0; dim < 3; ++dim) {
416 : const auto& boundary_correction_in_axis =
417 : gsl::at(boundary_corrections, dim);
418 : const double inverse_delta = gsl::at(one_over_delta_xi, dim);
419 : EXPAND_PACK_LEFT_TO_RIGHT([&dt_vars_ptr, &boundary_correction_in_axis,
420 : &cell_centered_det_inv_jacobian, dim,
421 : inverse_delta, &subcell_mesh]() {
422 : auto& dt_var = *get<::Tags::dt<GrmhdDtTags>>(dt_vars_ptr);
423 : const auto& var_correction =
424 : get<GrmhdDtTags>(boundary_correction_in_axis);
425 : for (size_t i = 0; i < dt_var.size(); ++i) {
426 : evolution::dg::subcell::add_cartesian_flux_divergence(
427 : make_not_null(&dt_var[i]), inverse_delta,
428 : get(cell_centered_det_inv_jacobian), var_correction[i],
429 : subcell_mesh.extents(), dim);
430 : }
431 : }());
432 : }
433 : }
434 : };
435 : } // namespace detail
436 :
437 : /*!
438 : * \brief Compute the time derivative on the subcell grid using FD
439 : * reconstruction.
440 : *
441 : * The code makes the following unchecked assumptions:
442 : * - Assumes Cartesian coordinates with a diagonal Jacobian matrix
443 : * from the logical to the inertial frame
444 : */
445 : template <typename System>
446 1 : struct TimeDerivative {
447 : template <typename DbTagsList>
448 0 : static void apply(const gsl::not_null<db::DataBox<DbTagsList>*> box) {
449 : using metavariables =
450 : typename std::decay_t<decltype(db::get<Parallel::Tags::Metavariables>(
451 : *box))>;
452 : using evolved_vars_tag = typename System::variables_tag;
453 : using evolved_vars_tags = typename evolved_vars_tag::tags_list;
454 : using grmhd_evolved_vars_tag =
455 : typename grmhd::ValenciaDivClean::System::variables_tag;
456 : using grmhd_evolved_vars_tags = typename grmhd_evolved_vars_tag::tags_list;
457 : using fluxes_tags =
458 : db::wrap_tags_in<::Tags::Flux, typename System::flux_variables,
459 : tmpl::size_t<3>, Frame::Inertial>;
460 : using prim_tag = typename System::primitive_variables_tag;
461 : using prim_tags = typename prim_tag::tags_list;
462 : using recons_prim_tags = tmpl::push_front<tmpl::push_back<
463 : prim_tags,
464 : hydro::Tags::LorentzFactorTimesSpatialVelocity<DataVector, 3>>>;
465 : using gradients_tags = typename System::gradients_tags;
466 :
467 : const Mesh<3>& dg_mesh = db::get<domain::Tags::Mesh<3>>(*box);
468 : const Mesh<3>& subcell_mesh =
469 : db::get<evolution::dg::subcell::Tags::Mesh<3>>(*box);
470 : ASSERT(
471 : subcell_mesh == Mesh<3>(subcell_mesh.extents(0), subcell_mesh.basis(0),
472 : subcell_mesh.quadrature(0)),
473 : "The subcell/FD mesh must be isotropic for the FD time derivative but "
474 : "got "
475 : << subcell_mesh);
476 : const size_t num_pts = subcell_mesh.number_of_grid_points();
477 : const size_t reconstructed_num_pts =
478 : (subcell_mesh.extents(0) + 1) *
479 : subcell_mesh.extents().slice_away(0).product();
480 :
481 : const tnsr::I<DataVector, 3, Frame::ElementLogical>&
482 : cell_centered_logical_coords =
483 : db::get<evolution::dg::subcell::Tags::Coordinates<
484 : 3, Frame::ElementLogical>>(*box);
485 : std::array<double, 3> one_over_delta_xi{};
486 : for (size_t i = 0; i < 3; ++i) {
487 : // Note: assumes isotropic extents
488 : gsl::at(one_over_delta_xi, i) =
489 : 1.0 / (get<0>(cell_centered_logical_coords)[1] -
490 : get<0>(cell_centered_logical_coords)[0]);
491 : }
492 : const auto& cell_centered_logical_to_inertial_inv_jacobian = db::get<
493 : evolution::dg::subcell::fd::Tags::InverseJacobianLogicalToInertial<3>>(
494 : *box);
495 : const auto& inertial_coords =
496 : db::get<evolution::dg::subcell::Tags::Coordinates<3, Frame::Inertial>>(
497 : *box);
498 :
499 : const Element<3>& element = db::get<domain::Tags::Element<3>>(*box);
500 : const bool element_is_interior = element.external_boundaries().empty();
501 : constexpr bool subcell_enabled_at_external_boundary =
502 : metavariables::SubcellOptions::subcell_enabled_at_external_boundary;
503 :
504 : ASSERT(element_is_interior or subcell_enabled_at_external_boundary,
505 : "Subcell time derivative is called at a boundary element while "
506 : "using subcell is disabled at external boundaries."
507 : "ElementID "
508 : << element.id());
509 :
510 : const fd::Reconstructor<System>& recons =
511 : db::get<fd::Tags::Reconstructor<System>>(*box);
512 : // If the element has external boundaries and subcell is enabled for
513 : // boundary elements, compute FD ghost data with a given boundary condition.
514 : if constexpr (subcell_enabled_at_external_boundary) {
515 : if (not element_is_interior) {
516 : fd::BoundaryConditionGhostData<System>::apply(box, element, recons);
517 : }
518 : }
519 : std::optional<std::array<gsl::span<std::uint8_t>, 3>>
520 : reconstruction_order{};
521 :
522 : if (const auto& filter_options =
523 : db::get<grmhd::GhValenciaDivClean::fd::Tags::FilterOptions>(*box);
524 : filter_options.spacetime_dissipation.has_value()) {
525 : db::mutate<evolved_vars_tag>(
526 : [&filter_options, &recons, &subcell_mesh](const auto evolved_vars_ptr,
527 : const auto& ghost_data) {
528 : typename evolved_vars_tag::type filtered_vars = *evolved_vars_ptr;
529 : // $(recons.ghost_zone_size() - 1) * 2 + 1$ => always use highest
530 : // order dissipation filter possible.
531 : grmhd::GhValenciaDivClean::fd::spacetime_kreiss_oliger_filter(
532 : make_not_null(&filtered_vars), *evolved_vars_ptr, ghost_data,
533 : subcell_mesh, 2 * recons.ghost_zone_size(),
534 : filter_options.spacetime_dissipation.value());
535 : *evolved_vars_ptr = filtered_vars;
536 : },
537 : box,
538 : db::get<evolution::dg::subcell::Tags::GhostDataForReconstruction<3>>(
539 : *box));
540 : }
541 :
542 : // Velocity of the moving mesh on the dg grid, if applicable.
543 : const std::optional<tnsr::I<DataVector, 3, Frame::Inertial>>&
544 : mesh_velocity_dg = db::get<domain::Tags::MeshVelocity<3>>(*box);
545 : // Inverse jacobian, to be projected on faces
546 : const auto& inv_jacobian_dg =
547 : db::get<domain::Tags::InverseJacobian<3, Frame::ElementLogical,
548 : Frame::Inertial>>(*box);
549 : const auto& det_inv_jacobian_dg = db::get<
550 : domain::Tags::DetInvJacobian<Frame::ElementLogical, Frame::Inertial>>(
551 : *box);
552 :
553 : // GH+GRMHD is a bit different.
554 : // 1. Compute GH time derivative, since this will also give us lapse, shift,
555 : // etc. that we need to reconstruct.
556 : // 2. Compute d_t Pi_{ab} source terms from MHD (or do we wait until post
557 : // MHD source terms?)
558 : // 3. Reconstruct MHD+spacetime vars to interfaces
559 : // 4. Compute MHD time derivatives.
560 : //
561 : // Compute FD GH derivatives with neighbor data
562 : // Use highest possible FD order for number of GZ, 2 * (ghost_zone_size)
563 : const auto& evolved_vars = db::get<evolved_vars_tag>(*box);
564 : Variables<db::wrap_tags_in<::Tags::deriv, gradients_tags, tmpl::size_t<3>,
565 : Frame::Inertial>>
566 : cell_centered_gh_derivs{num_pts};
567 : grmhd::GhValenciaDivClean::fd::spacetime_derivatives<System>(
568 : make_not_null(&cell_centered_gh_derivs), evolved_vars,
569 : db::get<evolution::dg::subcell::Tags::GhostDataForReconstruction<3>>(
570 : *box),
571 : recons.ghost_zone_size() * 2, subcell_mesh,
572 : cell_centered_logical_to_inertial_inv_jacobian);
573 :
574 : // Now package the data and compute the correction
575 : //
576 : // Note: Assumes a the GH and GRMHD corrections can be invoked separately.
577 : // This is reasonable since the systems are a tensor product system.
578 : const auto& base_boundary_correction =
579 : db::get<evolution::Tags::BoundaryCorrection>(*box);
580 : using derived_boundary_corrections =
581 : tmpl::at<typename metavariables::factory_creation::factory_classes,
582 : evolution::BoundaryCorrection>;
583 : std::array<Variables<grmhd_evolved_vars_tags>, 3> boundary_corrections{};
584 : call_with_dynamic_type<void, derived_boundary_corrections>(
585 : &base_boundary_correction, [&](const auto* gh_grmhd_correction) {
586 : // Need the GH packaged tags to avoid projecting them.
587 : using gh_dg_package_field_tags = typename std::decay_t<
588 : decltype(gh_grmhd_correction
589 : ->gh_correction())>::dg_package_field_tags;
590 : // Only apply correction to GRMHD variables.
591 : const auto& boundary_correction =
592 : gh_grmhd_correction->valencia_correction();
593 : using DerivedCorrection = std::decay_t<decltype(boundary_correction)>;
594 : using dg_package_data_temporary_tags =
595 : typename DerivedCorrection::dg_package_data_temporary_tags;
596 :
597 : using dg_package_data_argument_tags = tmpl::append<
598 : evolved_vars_tags, recons_prim_tags, fluxes_tags,
599 : tmpl::remove_duplicates<tmpl::push_back<
600 : dg_package_data_temporary_tags,
601 : gr::Tags::SpatialMetric<DataVector, 3>,
602 : gr::Tags::SqrtDetSpatialMetric<DataVector>,
603 : gr::Tags::InverseSpatialMetric<DataVector, 3>,
604 : evolution::dg::Actions::detail::NormalVector<3>>>>;
605 :
606 : // Computed prims and cons on face via reconstruction
607 : auto package_data_argvars_lower_face = make_array<3>(
608 : Variables<dg_package_data_argument_tags>(reconstructed_num_pts));
609 : auto package_data_argvars_upper_face = make_array<3>(
610 : Variables<dg_package_data_argument_tags>(reconstructed_num_pts));
611 :
612 : // Reconstruct data to the face
613 : call_with_dynamic_type<
614 : void, typename grmhd::GhValenciaDivClean::fd::Reconstructor<
615 : System>::creatable_classes>(
616 : &recons, [&box, &package_data_argvars_lower_face,
617 : &package_data_argvars_upper_face,
618 : &reconstruction_order](const auto& reconstructor) {
619 : using ReconstructorType =
620 : std::decay_t<decltype(*reconstructor)>;
621 : db::apply<
622 : typename ReconstructorType::reconstruction_argument_tags>(
623 : [&package_data_argvars_lower_face,
624 : &package_data_argvars_upper_face, &reconstructor,
625 : &reconstruction_order](const auto&... args) {
626 : if constexpr (ReconstructorType::use_adaptive_order) {
627 : reconstructor->reconstruct(
628 : make_not_null(&package_data_argvars_lower_face),
629 : make_not_null(&package_data_argvars_upper_face),
630 : make_not_null(&reconstruction_order), args...);
631 : } else {
632 : (void)reconstruction_order;
633 : reconstructor->reconstruct(
634 : make_not_null(&package_data_argvars_lower_face),
635 : make_not_null(&package_data_argvars_upper_face),
636 : args...);
637 : }
638 : },
639 : *box);
640 : });
641 :
642 : using dg_package_field_tags =
643 : typename DerivedCorrection::dg_package_field_tags;
644 : // Allocated outside for loop to reduce allocations
645 : Variables<dg_package_field_tags> upper_packaged_data{
646 : reconstructed_num_pts};
647 : Variables<dg_package_field_tags> lower_packaged_data{
648 : reconstructed_num_pts};
649 :
650 : // Compute fluxes on faces
651 : for (size_t i = 0; i < 3; ++i) {
652 : auto& vars_upper_face = gsl::at(package_data_argvars_upper_face, i);
653 : auto& vars_lower_face = gsl::at(package_data_argvars_lower_face, i);
654 : grmhd::ValenciaDivClean::subcell::compute_fluxes(
655 : make_not_null(&vars_upper_face));
656 : grmhd::ValenciaDivClean::subcell::compute_fluxes(
657 : make_not_null(&vars_lower_face));
658 :
659 : // Build extents of mesh shifted by half a grid cell in direction i
660 : const unsigned long& num_subcells_1d = subcell_mesh.extents(0);
661 : Index<3> face_mesh_extents(std::array<size_t, 3>{
662 : num_subcells_1d, num_subcells_1d, num_subcells_1d});
663 : face_mesh_extents[i] = num_subcells_1d + 1;
664 : // Add moving mesh corrections to the fluxes, if needed
665 : std::optional<tnsr::I<DataVector, 3, Frame::Inertial>>
666 : mesh_velocity_on_face = {};
667 : if (mesh_velocity_dg.has_value()) {
668 : // Project mesh velocity on face mesh.
669 : // Can we get away with only doing the normal component? It
670 : // is also used in the packaged data...
671 : mesh_velocity_on_face = tnsr::I<DataVector, 3, Frame::Inertial>{
672 : reconstructed_num_pts};
673 : for (size_t j = 0; j < 3; j++) {
674 : // j^th component of the velocity on the i^th directed face
675 : mesh_velocity_on_face.value().get(j) =
676 : evolution::dg::subcell::fd::project_to_faces(
677 : mesh_velocity_dg.value().get(j), dg_mesh,
678 : face_mesh_extents, i,
679 : j == 0 ? Spectral::Parity::Odd
680 : : Spectral::Parity::Even);
681 : }
682 :
683 : tmpl::for_each<grmhd_evolved_vars_tags>(
684 : [&vars_upper_face, &vars_lower_face,
685 : &mesh_velocity_on_face](auto tag_v) {
686 : using tag = tmpl::type_from<decltype(tag_v)>;
687 : using flux_tag =
688 : ::Tags::Flux<tag, tmpl::size_t<3>, Frame::Inertial>;
689 : using FluxTensor = typename flux_tag::type;
690 : const auto& var_upper = get<tag>(vars_upper_face);
691 : const auto& var_lower = get<tag>(vars_lower_face);
692 : auto& flux_upper = get<flux_tag>(vars_upper_face);
693 : auto& flux_lower = get<flux_tag>(vars_lower_face);
694 : for (size_t storage_index = 0;
695 : storage_index < var_upper.size(); ++storage_index) {
696 : const auto tensor_index =
697 : var_upper.get_tensor_index(storage_index);
698 : for (size_t j = 0; j < 3; j++) {
699 : const auto flux_storage_index =
700 : FluxTensor::get_storage_index(
701 : prepend(tensor_index, j));
702 : flux_upper[flux_storage_index] -=
703 : mesh_velocity_on_face.value().get(j) *
704 : var_upper[storage_index];
705 : flux_lower[flux_storage_index] -=
706 : mesh_velocity_on_face.value().get(j) *
707 : var_lower[storage_index];
708 : }
709 : }
710 : });
711 : }
712 :
713 : // Normal vectors in curved spacetime normalized by inverse
714 : // spatial metric. Since we assume a Cartesian grid, this is
715 : // relatively easy. Note that we use the sign convention on
716 : // the normal vectors to be compatible with DG.
717 : //
718 : // Note that these normal vectors are on all faces inside the DG
719 : // element since there are a bunch of subcells. We don't use the
720 : // NormalCovectorAndMagnitude tag in the DataBox right now to avoid
721 : // conflicts with the DG solver. We can explore in the future if
722 : // it's possible to reuse that allocation.
723 : //
724 : // The unnormalized normal vector is
725 : // n_j = d \xi^{\hat i}/dx^j
726 : // with "i" the current face.
727 : tnsr::i<DataVector, 3, Frame::Inertial> lower_outward_conormal{
728 : reconstructed_num_pts, 0.0};
729 : for (size_t j = 0; j < 3; j++) {
730 : lower_outward_conormal.get(j) =
731 : evolution::dg::subcell::fd::project_to_faces(
732 : inv_jacobian_dg.get(i, j), dg_mesh, face_mesh_extents, i,
733 : (i == 0) != (j == 0) ? Spectral::Parity::Odd
734 : : Spectral::Parity::Even);
735 : }
736 : const auto det_inv_jacobian_face =
737 : evolution::dg::subcell::fd::project_to_faces(
738 : get(det_inv_jacobian_dg), dg_mesh, face_mesh_extents, i,
739 : Spectral::Parity::Even);
740 :
741 : const Scalar<DataVector> normalization{sqrt(get(
742 : dot_product(lower_outward_conormal, lower_outward_conormal,
743 : get<gr::Tags::InverseSpatialMetric<DataVector, 3>>(
744 : vars_upper_face))))};
745 : for (size_t j = 0; j < 3; j++) {
746 : lower_outward_conormal.get(j) =
747 : lower_outward_conormal.get(j) / get(normalization);
748 : }
749 :
750 : tnsr::i<DataVector, 3, Frame::Inertial> upper_outward_conormal{
751 : reconstructed_num_pts, 0.0};
752 : for (size_t j = 0; j < 3; j++) {
753 : upper_outward_conormal.get(j) = -lower_outward_conormal.get(j);
754 : }
755 : // Note: we probably should compute the normal vector in addition to
756 : // the co-vector. Not a huge issue since we'll get an FPE right now
757 : // if it's used by a Riemann solver.
758 :
759 : // Compute the packaged data
760 : using dg_package_data_projected_tags = tmpl::append<
761 : grmhd_evolved_vars_tags, fluxes_tags,
762 : dg_package_data_temporary_tags,
763 : typename DerivedCorrection::dg_package_data_primitive_tags>;
764 : evolution::dg::Actions::detail::dg_package_data<System>(
765 : make_not_null(&upper_packaged_data),
766 : dynamic_cast<const DerivedCorrection&>(boundary_correction),
767 : vars_upper_face, upper_outward_conormal, mesh_velocity_on_face,
768 : *box, typename DerivedCorrection::dg_package_data_volume_tags{},
769 : dg_package_data_projected_tags{});
770 :
771 : evolution::dg::Actions::detail::dg_package_data<System>(
772 : make_not_null(&lower_packaged_data),
773 : dynamic_cast<const DerivedCorrection&>(boundary_correction),
774 : vars_lower_face, lower_outward_conormal, mesh_velocity_on_face,
775 : *box, typename DerivedCorrection::dg_package_data_volume_tags{},
776 : dg_package_data_projected_tags{});
777 :
778 : // Now need to check if any of our neighbors are doing DG,
779 : // because if so then we need to use whatever boundary data
780 : // they sent instead of what we computed locally.
781 : //
782 : // Note: We could check this beforehand to avoid the extra
783 : // work of reconstruction and flux computations at the
784 : // boundaries.
785 : evolution::dg::subcell::correct_package_data<true>(
786 : make_not_null(&lower_packaged_data),
787 : make_not_null(&upper_packaged_data), i, element, subcell_mesh,
788 : db::get<evolution::dg::Tags::MortarData<3>>(*box),
789 : Variables<gh_dg_package_field_tags>::
790 : number_of_independent_components);
791 :
792 : // Compute the corrections on the faces. We only need to
793 : // compute this once because we can just flip the normal
794 : // vectors then
795 : gsl::at(boundary_corrections, i).initialize(reconstructed_num_pts);
796 : evolution::dg::subcell::compute_boundary_terms(
797 : make_not_null(&gsl::at(boundary_corrections, i)),
798 : dynamic_cast<const DerivedCorrection&>(boundary_correction),
799 : upper_packaged_data, lower_packaged_data, db::as_access(*box),
800 : typename DerivedCorrection::dg_boundary_terms_volume_tags{});
801 : // We need to multiply by the normal vector normalization
802 : gsl::at(boundary_corrections, i) *= get(normalization);
803 : // Also multiply by determinant of Jacobian, following Eq.(34)
804 : // of 2109.11645
805 : gsl::at(boundary_corrections, i) *= 1.0 / det_inv_jacobian_face;
806 : }
807 : });
808 :
809 : // Now compute the actual time derivatives.
810 : using gh_variables_tags =
811 : typename System::gh_system::variables_tag::tags_list;
812 : using gh_gradient_tags = typename TimeDerivativeTerms::gh_gradient_tags;
813 : using gh_temporary_tags = typename TimeDerivativeTerms::gh_temp_tags;
814 : using gh_extra_tags =
815 : tmpl::list<gr::Tags::SpacetimeMetric<DataVector, 3>,
816 : gh::Tags::Pi<DataVector, 3>, gh::Tags::Phi<DataVector, 3>,
817 : ::gh::Tags::ConstraintGamma0, ::gh::Tags::ConstraintGamma1,
818 : ::gh::Tags::ConstraintGamma2>;
819 : using grmhd_source_tags =
820 : tmpl::transform<ValenciaDivClean::ComputeSources::return_tags,
821 : tmpl::bind<db::remove_tag_prefix, tmpl::_1>>;
822 : using grmhd_source_argument_tags =
823 : ValenciaDivClean::ComputeSources::argument_tags;
824 : detail::ComputeTimeDerivImpl<
825 : gh_variables_tags, gh_temporary_tags, gh_gradient_tags, gh_extra_tags,
826 : grmhd_evolved_vars_tags, grmhd_source_tags, grmhd_source_argument_tags,
827 : System>::apply(box, inertial_coords,
828 : db::get<evolution::dg::subcell::fd::Tags::
829 : DetInverseJacobianLogicalToInertial>(*box),
830 : cell_centered_logical_to_inertial_inv_jacobian,
831 : one_over_delta_xi, boundary_corrections,
832 : cell_centered_gh_derivs);
833 : evolution::dg::subcell::store_reconstruction_order_in_databox(
834 : box, reconstruction_order);
835 : }
836 : };
837 : } // namespace grmhd::GhValenciaDivClean::subcell
|