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