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/Tensor/Tensor.hpp"
17 : #include "DataStructures/Variables.hpp"
18 : #include "Domain/Structure/Element.hpp"
19 : #include "Domain/Tags.hpp"
20 : #include "Evolution/BoundaryCorrectionTags.hpp"
21 : #include "Evolution/DgSubcell/CartesianFluxDivergence.hpp"
22 : #include "Evolution/DgSubcell/ComputeBoundaryTerms.hpp"
23 : #include "Evolution/DgSubcell/CorrectPackagedData.hpp"
24 : #include "Evolution/DgSubcell/SubcellOptions.hpp"
25 : #include "Evolution/DgSubcell/Tags/Coordinates.hpp"
26 : #include "Evolution/DgSubcell/Tags/Jacobians.hpp"
27 : #include "Evolution/DgSubcell/Tags/Mesh.hpp"
28 : #include "Evolution/DiscontinuousGalerkin/Actions/PackageDataImpl.hpp"
29 : #include "Evolution/DiscontinuousGalerkin/MortarTags.hpp"
30 : #include "Evolution/Systems/Burgers/FiniteDifference/BoundaryConditionGhostData.hpp"
31 : #include "Evolution/Systems/Burgers/FiniteDifference/Reconstructor.hpp"
32 : #include "Evolution/Systems/Burgers/FiniteDifference/Tags.hpp"
33 : #include "Evolution/Systems/Burgers/Fluxes.hpp"
34 : #include "Evolution/Systems/Burgers/Subcell/ComputeFluxes.hpp"
35 : #include "Evolution/Systems/Burgers/System.hpp"
36 : #include "NumericalAlgorithms/Spectral/Mesh.hpp"
37 : #include "Parallel/Tags/Metavariables.hpp"
38 : #include "Utilities/CallWithDynamicType.hpp"
39 : #include "Utilities/ErrorHandling/Assert.hpp"
40 : #include "Utilities/Gsl.hpp"
41 : #include "Utilities/MakeArray.hpp"
42 :
43 : namespace Burgers::subcell {
44 : /*!
45 : * \brief Compute the time derivative on the subcell grid using FD
46 : * reconstruction.
47 : *
48 : * The code makes the following unchecked assumptions:
49 : * - Assumes Cartesian coordinates with a diagonal Jacobian matrix
50 : * from the logical to the inertial frame
51 : * - Assumes the mesh is not moving (grid and inertial frame are the same)
52 : */
53 1 : struct TimeDerivative {
54 : template <typename DbTagsList>
55 0 : static void apply(const gsl::not_null<db::DataBox<DbTagsList>*> box) {
56 : ASSERT((db::get<::domain::CoordinateMaps::Tags::CoordinateMap<
57 : 1, Frame::Grid, Frame::Inertial>>(*box))
58 : .is_identity(),
59 : "Do not yet support moving mesh with DG-subcell.");
60 : const Element<1>& element = db::get<domain::Tags::Element<1>>(*box);
61 :
62 : const bool element_is_interior = element.external_boundaries().size() == 0;
63 : constexpr bool subcell_enabled_at_external_boundary =
64 : std::decay_t<decltype(db::get<Parallel::Tags::Metavariables>(
65 : *box))>::SubcellOptions::subcell_enabled_at_external_boundary;
66 :
67 : ASSERT(element_is_interior or subcell_enabled_at_external_boundary,
68 : "Subcell time derivative is called at a boundary element while "
69 : "using subcell is disabled at external boundaries."
70 : "ElementID "
71 : << element.id());
72 :
73 : using evolved_vars_tags = typename System::variables_tag::tags_list;
74 : using fluxes_tags = typename Fluxes::return_tags;
75 :
76 : // The copy of Mesh is intentional to avoid a GCC-7 internal compiler error.
77 : const Mesh<1> subcell_mesh =
78 : db::get<evolution::dg::subcell::Tags::Mesh<1>>(*box);
79 : const size_t num_reconstructed_pts =
80 : subcell_mesh.number_of_grid_points() + 1;
81 :
82 : const Burgers::fd::Reconstructor& recons =
83 : db::get<Burgers::fd::Tags::Reconstructor>(*box);
84 :
85 : const auto& boundary_correction =
86 : db::get<evolution::Tags::BoundaryCorrection<System>>(*box);
87 : using derived_boundary_corrections =
88 : typename std::decay_t<decltype(boundary_correction)>::creatable_classes;
89 : // Variables to store the boundary correction terms on FD subinterfaces
90 : std::array<Variables<evolved_vars_tags>, 1> fd_boundary_corrections{};
91 :
92 : // If the element has external boundaries and subcell is enabled for
93 : // boundary elements, compute FD ghost data with a given boundary condition.
94 : if constexpr (subcell_enabled_at_external_boundary) {
95 : if (element.external_boundaries().size() != 0) {
96 : fd::BoundaryConditionGhostData::apply(box, element, recons);
97 : }
98 : }
99 :
100 : // package the data and compute the boundary correction
101 : tmpl::for_each<derived_boundary_corrections>(
102 : [&boundary_correction, &box, &element, &fd_boundary_corrections,
103 : &num_reconstructed_pts, &recons,
104 : &subcell_mesh](auto derived_correction_v) {
105 : using derived_correction =
106 : tmpl::type_from<decltype(derived_correction_v)>;
107 : if (typeid(boundary_correction) == typeid(derived_correction)) {
108 : using dg_package_field_tags =
109 : typename derived_correction::dg_package_field_tags;
110 : using dg_package_data_argument_tags =
111 : tmpl::append<evolved_vars_tags, fluxes_tags>;
112 :
113 : // Variables that need to be reconstructed for dg_package_data()
114 : auto package_data_argvars_lower_face =
115 : make_array<1>(Variables<dg_package_data_argument_tags>(
116 : num_reconstructed_pts));
117 : auto package_data_argvars_upper_face =
118 : make_array<1>(Variables<dg_package_data_argument_tags>(
119 : num_reconstructed_pts));
120 :
121 : // Reconstruct the fields on interfaces
122 : call_with_dynamic_type<
123 : void, typename Burgers::fd::Reconstructor::creatable_classes>(
124 : &recons,
125 : [&box, &package_data_argvars_lower_face,
126 : &package_data_argvars_upper_face](const auto& reconstructor) {
127 : db::apply<typename std::decay_t<decltype(
128 : *reconstructor)>::reconstruction_argument_tags>(
129 : [&package_data_argvars_lower_face,
130 : &package_data_argvars_upper_face,
131 : &reconstructor](const auto&... args) {
132 : reconstructor->reconstruct(
133 : make_not_null(&package_data_argvars_lower_face),
134 : make_not_null(&package_data_argvars_upper_face),
135 : args...);
136 : },
137 : *box);
138 : });
139 :
140 : // Variables to store packaged data. Allocate outside of loop to
141 : // reduce allocations
142 : Variables<dg_package_field_tags> upper_packaged_data{
143 : num_reconstructed_pts};
144 : Variables<dg_package_field_tags> lower_packaged_data{
145 : num_reconstructed_pts};
146 :
147 : auto& vars_upper_face = gsl::at(package_data_argvars_upper_face, 0);
148 : auto& vars_lower_face = gsl::at(package_data_argvars_lower_face, 0);
149 :
150 : // Compute fluxes on each faces
151 : Burgers::subcell::compute_fluxes(make_not_null(&vars_upper_face));
152 : Burgers::subcell::compute_fluxes(make_not_null(&vars_lower_face));
153 :
154 : // Note that we use the sign convention on the normal vectors to
155 : // be compatible with DG.
156 : tnsr::i<DataVector, 1, Frame::Inertial> lower_outward_conormal{
157 : num_reconstructed_pts, 0.0};
158 : lower_outward_conormal.get(0) = 1.0;
159 : tnsr::i<DataVector, 1, Frame::Inertial> upper_outward_conormal{
160 : num_reconstructed_pts, 0.0};
161 : upper_outward_conormal.get(0) = -1.0;
162 :
163 : // Compute the packaged data
164 : evolution::dg::Actions::detail::dg_package_data<System>(
165 : make_not_null(&upper_packaged_data),
166 : dynamic_cast<const derived_correction&>(boundary_correction),
167 : vars_upper_face, upper_outward_conormal, {std::nullopt}, *box,
168 : typename derived_correction::dg_package_data_volume_tags{},
169 : dg_package_data_argument_tags{});
170 : evolution::dg::Actions::detail::dg_package_data<System>(
171 : make_not_null(&lower_packaged_data),
172 : dynamic_cast<const derived_correction&>(boundary_correction),
173 : vars_lower_face, lower_outward_conormal, {std::nullopt}, *box,
174 : typename derived_correction::dg_package_data_volume_tags{},
175 : dg_package_data_argument_tags{});
176 :
177 : // Now need to check if any of our neighbors are doing DG, because
178 : // if so then we need to use whatever boundary data they sent
179 : // instead of what we computed locally.
180 : //
181 : // Note: We could check this beforehand to avoid the extra work of
182 : // reconstruction and flux computations at the boundaries.
183 : evolution::dg::subcell::correct_package_data<true>(
184 : make_not_null(&lower_packaged_data),
185 : make_not_null(&upper_packaged_data), 0, element, subcell_mesh,
186 : db::get<evolution::dg::Tags::MortarData<1>>(*box), 0);
187 :
188 : // Compute the corrections on the faces. We only need to compute
189 : // this once because we can just flip the normal vectors then
190 : gsl::at(fd_boundary_corrections, 0)
191 : .initialize(num_reconstructed_pts);
192 : evolution::dg::subcell::compute_boundary_terms(
193 : make_not_null(&gsl::at(fd_boundary_corrections, 0)),
194 : dynamic_cast<const derived_correction&>(boundary_correction),
195 : upper_packaged_data, lower_packaged_data, db::as_access(*box),
196 : typename derived_correction::dg_boundary_terms_volume_tags{});
197 : }
198 : });
199 :
200 : std::array<double, 1> one_over_delta_xi{};
201 : {
202 : const tnsr::I<DataVector, 1, Frame::ElementLogical>&
203 : cell_centered_logical_coords =
204 : db::get<evolution::dg::subcell::Tags::Coordinates<
205 : 1, Frame::ElementLogical>>(*box);
206 :
207 : gsl::at(one_over_delta_xi, 0) =
208 : 1.0 / (get<0>(cell_centered_logical_coords)[1] -
209 : get<0>(cell_centered_logical_coords)[0]);
210 : }
211 :
212 : // Now compute the actual time derivative
213 : using dt_variables_tag =
214 : db::add_tag_prefix<::Tags::dt, typename System::variables_tag>;
215 : const size_t num_pts = subcell_mesh.number_of_grid_points();
216 : db::mutate<dt_variables_tag>(
217 : [&num_pts, &fd_boundary_corrections, &subcell_mesh, &one_over_delta_xi](
218 : const auto dt_vars_ptr,
219 : const InverseJacobian<DataVector, 1, Frame::ElementLogical,
220 : Frame::Grid>&
221 : cell_centered_logical_to_grid_inv_jacobian) {
222 : dt_vars_ptr->initialize(num_pts, 0.0);
223 : auto& dt_u = get<::Tags::dt<::Burgers::Tags::U>>(*dt_vars_ptr);
224 :
225 : Scalar<DataVector>& u_correction =
226 : get<::Burgers::Tags::U>(gsl::at(fd_boundary_corrections, 0));
227 : evolution::dg::subcell::add_cartesian_flux_divergence(
228 : make_not_null(&get(dt_u)), gsl::at(one_over_delta_xi, 0),
229 : cell_centered_logical_to_grid_inv_jacobian.get(0, 0),
230 : get(u_correction), subcell_mesh.extents(), 0);
231 : },
232 : box,
233 : db::get<
234 : evolution::dg::subcell::fd::Tags::InverseJacobianLogicalToGrid<1>>(
235 : *box));
236 : }
237 : };
238 : } // namespace Burgers::subcell
|