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