SpECTRE  v2024.09.29
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
fd Namespace Reference

Functions and classes for finite difference methods. More...

Namespaces

namespace  reconstruction
 Variable and flux vector splitting reconstruction schemes for finite difference methods.
 

Enumerations

enum class  DerivativeOrder : int {
  OneHigherThanRecons = -1 , OneHigherThanReconsButFiveToFour = -2 , Two = 2 , Four = 4 ,
  Six = 6 , Eight = 8 , Ten = 10
}
 Controls which FD derivative order is used. More...
 

Functions

std::ostreamoperator<< (std::ostream &os, DerivativeOrder der_order)
 
template<size_t Dim>
void low_pass_filter (gsl::not_null< gsl::span< double > * > filtered_data, const gsl::span< const double > &volume_vars, const DirectionMap< Dim, gsl::span< const double > > &ghost_cell_vars, const Mesh< Dim > &volume_mesh, size_t number_of_variables, size_t fd_order, double epsilon)
 Apply a low-pass filter to the data. More...
 
template<size_t Dim>
void kreiss_oliger_filter (gsl::not_null< gsl::span< double > * > filtered_data, const gsl::span< const double > &volume_vars, const DirectionMap< Dim, gsl::span< const double > > &ghost_cell_vars, const Mesh< Dim > &volume_mesh, size_t number_of_variables, size_t fd_order, double epsilon)
 Apply Kreiss-Oliger dissipation [115] of order fd_order to the variables. More...
 
template<size_t Dim, typename FluxesTags >
void set_cartesian_neighbor_cell_centered_fluxes (const gsl::not_null< DirectionMap< Dim, Variables< FluxesTags > > * > flux_neighbor_data, const DirectionalIdMap< Dim, evolution::dg::subcell::GhostData > &all_ghost_data, const Mesh< Dim > &subcell_mesh, const size_t ghost_zone_size, const size_t number_of_rdmp_values_in_ghost_data)
 Fill the flux_neighbor_data with pointers into the all_ghost_data. More...
 
template<size_t Dim, typename... EvolvedVarsTags, typename FluxesTags = tmpl::list<::Tags::Flux< EvolvedVarsTags, tmpl::size_t<Dim>, Frame::Inertial>...>>
void cartesian_high_order_flux_corrections (const gsl::not_null< std::optional< std::array< Variables< tmpl::list< EvolvedVarsTags... > >, Dim > > * > high_order_corrections, const std::optional< Variables< FluxesTags > > &cell_centered_fluxes, const std::array< Variables< tmpl::list< EvolvedVarsTags... > >, Dim > &second_order_boundary_corrections, const fd::DerivativeOrder &fd_derivative_order, const DirectionalIdMap< Dim, evolution::dg::subcell::GhostData > &all_ghost_data, const Mesh< Dim > &subcell_mesh, const size_t ghost_zone_size, const std::array< gsl::span< std::uint8_t >, Dim > &reconstruction_order={}, const size_t number_of_rdmp_values_in_ghost_data=0)
 Computes the high-order Cartesian flux corrections if necessary. More...
 
template<size_t Dim, typename ReconstructionTags >
void neighbor_data_as_variables (const gsl::not_null< DirectionalIdMap< Dim, Variables< ReconstructionTags > > * > vars_neighbor_data, const DirectionalIdMap< Dim, evolution::dg::subcell::GhostData > &all_ghost_data, const size_t ghost_zone_size, const Mesh< Dim > &subcell_mesh)
 Given the type-erased neighbor data for reconstruction stored in a DataVector, have Variables point into them. More...
 
template<size_t StencilSize>
std::array< std::array< double, StencilSize >, StencilSize > non_uniform_1d_weights (const std::deque< double > &times)
 Returns the weights for a 1D non-uniform finite difference stencil. More...
 
template<size_t Dim>
void logical_partial_derivatives (const gsl::not_null< std::array< gsl::span< double >, Dim > * > logical_derivatives, const gsl::span< const double > &volume_vars, const DirectionMap< Dim, gsl::span< const double > > &ghost_cell_vars, const Mesh< Dim > &volume_mesh, size_t number_of_variables, size_t fd_order)
 Compute the logical partial derivatives using cell-centered finite difference derivatives. More...
 
template<typename DerivativeTags , size_t Dim, typename DerivativeFrame >
void partial_derivatives (gsl::not_null< Variables< db::wrap_tags_in< Tags::deriv, DerivativeTags, tmpl::size_t< Dim >, DerivativeFrame > > * > partial_derivatives, const gsl::span< const double > &volume_vars, const DirectionMap< Dim, gsl::span< const double > > &ghost_cell_vars, const Mesh< Dim > &volume_mesh, size_t number_of_variables, size_t fd_order, const InverseJacobian< DataVector, Dim, Frame::ElementLogical, DerivativeFrame > &inverse_jacobian)
 Compute the partial derivative on the DerivativeFrame using the inverse_jacobian. More...
 
template<DerivativeOrder DerivOrder, size_t Dim, typename... EvolvedVarsTags>
void cartesian_high_order_fluxes_using_nodes (const gsl::not_null< std::array< Variables< tmpl::list< EvolvedVarsTags... > >, Dim > * > high_order_boundary_corrections_in_logical_direction, const std::array< Variables< tmpl::list< EvolvedVarsTags... > >, Dim > &second_order_boundary_corrections_in_logical_direction, const Variables< tmpl::list< ::Tags::Flux< EvolvedVarsTags, tmpl::size_t< Dim >, Frame::Inertial >... > > &cell_centered_inertial_flux, const DirectionMap< Dim, Variables< tmpl::list<::Tags::Flux< EvolvedVarsTags, tmpl::size_t< Dim >, Frame::Inertial >... > > > &ghost_cell_inertial_flux, const Mesh< Dim > &subcell_mesh, const size_t number_of_ghost_cells, const std::array< gsl::span< std::uint8_t >, Dim > &reconstruction_order={})
 Computes a high-order boundary correction G at the FD interface. More...
 
template<size_t Dim, typename... EvolvedVarsTags>
void cartesian_high_order_fluxes_using_nodes (const gsl::not_null< std::array< Variables< tmpl::list< EvolvedVarsTags... > >, Dim > * > high_order_boundary_corrections_in_logical_direction, const std::array< Variables< tmpl::list< EvolvedVarsTags... > >, Dim > &second_order_boundary_corrections_in_logical_direction, const Variables< tmpl::list< ::Tags::Flux< EvolvedVarsTags, tmpl::size_t< Dim >, Frame::Inertial >... > > &cell_centered_inertial_flux, const DirectionMap< Dim, Variables< tmpl::list<::Tags::Flux< EvolvedVarsTags, tmpl::size_t< Dim >, Frame::Inertial >... > > > &ghost_cell_inertial_flux, const Mesh< Dim > &subcell_mesh, const size_t number_of_ghost_cells, const DerivativeOrder derivative_order, const std::array< gsl::span< std::uint8_t >, Dim > &reconstruction_order={})
 Computes a high-order boundary correction G at the FD interface. More...
 

Detailed Description

Functions and classes for finite difference methods.

Enumeration Type Documentation

◆ DerivativeOrder

enum class fd::DerivativeOrder : int
strong

Controls which FD derivative order is used.

Enumerator
OneHigherThanRecons 

Use one order high derivative.

For example, if fifth order reconstruction is used, then a sixth-order derivative is used.

OneHigherThanReconsButFiveToFour 

Same as OneHigherThanRecons except uses a fourth-order derivative if fifth-order reconstruction was used.

Two 

Use 2nd order derivatives.

Four 

Use 4th order derivatives.

Six 

Use 6th order derivatives.

Eight 

Use 8th order derivatives.

Ten 

Use 10th order derivatives.

Function Documentation

◆ cartesian_high_order_flux_corrections()

template<size_t Dim, typename... EvolvedVarsTags, typename FluxesTags = tmpl::list<::Tags::Flux< EvolvedVarsTags, tmpl::size_t<Dim>, Frame::Inertial>...>>
void fd::cartesian_high_order_flux_corrections ( const gsl::not_null< std::optional< std::array< Variables< tmpl::list< EvolvedVarsTags... > >, Dim > > * >  high_order_corrections,
const std::optional< Variables< FluxesTags > > &  cell_centered_fluxes,
const std::array< Variables< tmpl::list< EvolvedVarsTags... > >, Dim > &  second_order_boundary_corrections,
const fd::DerivativeOrder fd_derivative_order,
const DirectionalIdMap< Dim, evolution::dg::subcell::GhostData > &  all_ghost_data,
const Mesh< Dim > &  subcell_mesh,
const size_t  ghost_zone_size,
const std::array< gsl::span< std::uint8_t >, Dim > &  reconstruction_order = {},
const size_t  number_of_rdmp_values_in_ghost_data = 0 
)

Computes the high-order Cartesian flux corrections if necessary.

The cell_centered_fluxes is stored in the tag evolution::dg::subcell::Tags::CellCenteredFlux, fd_derivative_order is from evolution::dg::subcell::Tags::SubcellOptions (.finite_difference_derivative_order()), the all_ghost_data is stored in the tag evolution::dg::subcell::Tags::GhostDataForReconstruction, the ghost_zone_size should come from the FD reconstructor.

By default we assume no RDMP data is in the ghost_data buffer. In the future we will want to update how we store the data in order to eliminate more memory allocations and copies, in which case that value will be non-zero.

Note
high_order_corrections must either not have a value or have all elements be of the same size as second_order_boundary_corrections[0].number_of_grid_points(), where we've assumed second_order_boundary_corrections is the same in all directions.

◆ cartesian_high_order_fluxes_using_nodes() [1/2]

template<size_t Dim, typename... EvolvedVarsTags>
void fd::cartesian_high_order_fluxes_using_nodes ( const gsl::not_null< std::array< Variables< tmpl::list< EvolvedVarsTags... > >, Dim > * >  high_order_boundary_corrections_in_logical_direction,
const std::array< Variables< tmpl::list< EvolvedVarsTags... > >, Dim > &  second_order_boundary_corrections_in_logical_direction,
const Variables< tmpl::list< ::Tags::Flux< EvolvedVarsTags, tmpl::size_t< Dim >, Frame::Inertial >... > > &  cell_centered_inertial_flux,
const DirectionMap< Dim, Variables< tmpl::list<::Tags::Flux< EvolvedVarsTags, tmpl::size_t< Dim >, Frame::Inertial >... > > > &  ghost_cell_inertial_flux,
const Mesh< Dim > &  subcell_mesh,
const size_t  number_of_ghost_cells,
const DerivativeOrder  derivative_order,
const std::array< gsl::span< std::uint8_t >, Dim > &  reconstruction_order = {} 
)

Computes a high-order boundary correction G at the FD interface.

The correction to the second-order boundary correction is given by

G=G(2)G(4)+G(6)G(8)+G(10),

where

Gj+1/2(4)=16(Gj2G(2)+Gj+1),Gj+1/2(6)=1180(Gj19Gj+16G(2)9Gj+1+Gj+2),Gj+1/2(8)=12100(Gj2253Gj1+50Gj2563G(2)+50Gj+1253Gj+2+Gj+3),Gj+1/2(10)=117640(Gj3495Gj2+49Gj1245Gj+20485G(2)245Gj+1+49Gj+2495Gj+3+Gj+4),

where

Gj=Fjinij+1/2,Gj±1=Fj±1inij+1/2,Gj±2=Fj±2inij+1/2,Gj±3=Fj±3inij+1/2,Gj±4=Fj±4inij+1/2.

This is a generalization of the correction presented in [37].

This high-order flux can be fed into a flux limiter, e.g. to guarantee positivity.

Note
This implementation should be profiled and optimized.
Warning
This documentation is for the general case. In the restricted Cartesian case we use the cell-centered flux as opposed to G^{(4)}, which differs by a minus sign. This amounts to a minus sign change in front of the G(k) terms in computing G for k>2, and also a sign change in front of G(2) in all G(k) for k>2.

◆ cartesian_high_order_fluxes_using_nodes() [2/2]

template<DerivativeOrder DerivOrder, size_t Dim, typename... EvolvedVarsTags>
void fd::cartesian_high_order_fluxes_using_nodes ( const gsl::not_null< std::array< Variables< tmpl::list< EvolvedVarsTags... > >, Dim > * >  high_order_boundary_corrections_in_logical_direction,
const std::array< Variables< tmpl::list< EvolvedVarsTags... > >, Dim > &  second_order_boundary_corrections_in_logical_direction,
const Variables< tmpl::list< ::Tags::Flux< EvolvedVarsTags, tmpl::size_t< Dim >, Frame::Inertial >... > > &  cell_centered_inertial_flux,
const DirectionMap< Dim, Variables< tmpl::list<::Tags::Flux< EvolvedVarsTags, tmpl::size_t< Dim >, Frame::Inertial >... > > > &  ghost_cell_inertial_flux,
const Mesh< Dim > &  subcell_mesh,
const size_t  number_of_ghost_cells,
const std::array< gsl::span< std::uint8_t >, Dim > &  reconstruction_order = {} 
)

Computes a high-order boundary correction G at the FD interface.

The correction to the second-order boundary correction is given by

G=G(2)G(4)+G(6)G(8)+G(10),

where

Gj+1/2(4)=16(Gj2G(2)+Gj+1),Gj+1/2(6)=1180(Gj19Gj+16G(2)9Gj+1+Gj+2),Gj+1/2(8)=12100(Gj2253Gj1+50Gj2563G(2)+50Gj+1253Gj+2+Gj+3),Gj+1/2(10)=117640(Gj3495Gj2+49Gj1245Gj+20485G(2)245Gj+1+49Gj+2495Gj+3+Gj+4),

where

Gj=Fjinij+1/2,Gj±1=Fj±1inij+1/2,Gj±2=Fj±2inij+1/2,Gj±3=Fj±3inij+1/2,Gj±4=Fj±4inij+1/2.

This is a generalization of the correction presented in [37].

This high-order flux can be fed into a flux limiter, e.g. to guarantee positivity.

Note
This implementation should be profiled and optimized.
Warning
This documentation is for the general case. In the restricted Cartesian case we use the cell-centered flux as opposed to G^{(4)}, which differs by a minus sign. This amounts to a minus sign change in front of the G(k) terms in computing G for k>2, and also a sign change in front of G(2) in all G(k) for k>2.

◆ kreiss_oliger_filter()

template<size_t Dim>
void fd::kreiss_oliger_filter ( gsl::not_null< gsl::span< double > * >  filtered_data,
const gsl::span< const double > &  volume_vars,
const DirectionMap< Dim, gsl::span< const double > > &  ghost_cell_vars,
const Mesh< Dim > &  volume_mesh,
size_t  number_of_variables,
size_t  fd_order,
double  epsilon 
)

Apply Kreiss-Oliger dissipation [115] of order fd_order to the variables.

Define the operators D+ and D be defined as:

D+fi=(fi+1fi)ΔxDfi=(fifi1)Δx

where the subscript i refers to the grid index. The dissipation operators are generally applied dimension-by-dimension, and so we have:

D(2m)=(1)m22mΔx2m1ϵ(D+)m(D)m

where ϵ controls the amount of dissipation and is restricted to 0ϵ1, and m is the order of the finite difference derivative so as not to spoil the accuracy of the scheme. That is, for second order FD, m=2 and one should use D(4). However, this choice requires a larger stencil and whether or not this is necessary also depends on when and how in the algorithm the operators are applied.

We arrive at the following operators:

D(2)fi=ϵΔx(fi+12fi+fi1)D(4)fi=ϵ16Δx(fi+24fi+1+6fi4fi1+fi2)D(6)fi=ϵ64Δx(fi+36fi+2+15fi+120fi+15fi16fi2+fi3)D(8)fi=ϵ256Δx(fi+48fi+3+28fi+256fi+1+70fi56fi1+28fi28fi3+fi4)D(10)fi=ϵ1024Δx(fi+510fi+4+45fi+3120fi+2+210fi+1252fi+210fi1120fi2+45fi310fi4+fi5)

Note
This function applies ΔxD(2m).

◆ logical_partial_derivatives()

template<size_t Dim>
void fd::logical_partial_derivatives ( const gsl::not_null< std::array< gsl::span< double >, Dim > * >  logical_derivatives,
const gsl::span< const double > &  volume_vars,
const DirectionMap< Dim, gsl::span< const double > > &  ghost_cell_vars,
const Mesh< Dim > &  volume_mesh,
size_t  number_of_variables,
size_t  fd_order 
)

Compute the logical partial derivatives using cell-centered finite difference derivatives.

Up to 8th order stencils are supported.

Note
Currently the stride is always one because we transpose the data before reconstruction. However, it may be faster to have a non-unit stride without the transpose. We have the stride parameter in the derivative stencils to make testing performance easier in the future.
This code does not do any explicit SIMD vectorization. We will want to profile and decide if optimization are possible. The Vc SIMD library has an example of vectorizing single-precision FD derivatives. There is also a paper "Optimization of Finite-Differencing Kernels for Numerical Relativity Applications" by Alfieri, Bernuzzi, Perego, and Radice that uses compiler auto-vectorization.

◆ low_pass_filter()

template<size_t Dim>
void fd::low_pass_filter ( gsl::not_null< gsl::span< double > * >  filtered_data,
const gsl::span< const double > &  volume_vars,
const DirectionMap< Dim, gsl::span< const double > > &  ghost_cell_vars,
const Mesh< Dim > &  volume_mesh,
size_t  number_of_variables,
size_t  fd_order,
double  epsilon 
)

Apply a low-pass filter to the data.

The filter fits a Legendre polynomial of degree equal to fd_order to each grid point and its neighboring points, then subtracts out the highest mode contribution at the grid point. This is inspired by a Heaviside filter for Legendre polynomial-based spectral methods.

The filter at different orders is given by:

F(9)ui=35×531441128(16406400(ui4+ui+4)1800800(ui3+ui+3)+1228800(ui2+ui+2)1114400(ui1+ui+1)+191520ui)F(7)ui=5×1680716(195040(ui3+ui+3)115840(ui2+ui+2)+16336(ui1+ui+1)14572ui)F(5)ui=3×1258(1336(ui2+ui+2)184(ui1+ui+1)+156ui)F(3)ui=1×32(14(ui1+ui+1)12ui)

Note
The F(11) filter isn't implemented yet.
The argument ϵ controls how much of the mode is filter out. ϵ=1 means the highest mode is completely filtered while ϵ=0 means it's not at all filtered.

◆ neighbor_data_as_variables()

template<size_t Dim, typename ReconstructionTags >
void fd::neighbor_data_as_variables ( const gsl::not_null< DirectionalIdMap< Dim, Variables< ReconstructionTags > > * >  vars_neighbor_data,
const DirectionalIdMap< Dim, evolution::dg::subcell::GhostData > &  all_ghost_data,
const size_t  ghost_zone_size,
const Mesh< Dim > &  subcell_mesh 
)

Given the type-erased neighbor data for reconstruction stored in a DataVector, have Variables point into them.

This function is helpful for reconstruction, especially when wanting to apply different reconstruction methods to different tags. This can happen, for example, when doing positivity-preserving reconstruction. The density should remain positive, but negative velocities are fine.

◆ partial_derivatives()

template<typename DerivativeTags , size_t Dim, typename DerivativeFrame >
void fd::partial_derivatives ( gsl::not_null< Variables< db::wrap_tags_in< Tags::deriv, DerivativeTags, tmpl::size_t< Dim >, DerivativeFrame > > * >  partial_derivatives,
const gsl::span< const double > &  volume_vars,
const DirectionMap< Dim, gsl::span< const double > > &  ghost_cell_vars,
const Mesh< Dim > &  volume_mesh,
size_t  number_of_variables,
size_t  fd_order,
const InverseJacobian< DataVector, Dim, Frame::ElementLogical, DerivativeFrame > &  inverse_jacobian 
)

Compute the partial derivative on the DerivativeFrame using the inverse_jacobian.

Logical partial derivatives are first computed using the fd::logical_partial_derivatives() function.

◆ set_cartesian_neighbor_cell_centered_fluxes()

template<size_t Dim, typename FluxesTags >
void fd::set_cartesian_neighbor_cell_centered_fluxes ( const gsl::not_null< DirectionMap< Dim, Variables< FluxesTags > > * >  flux_neighbor_data,
const DirectionalIdMap< Dim, evolution::dg::subcell::GhostData > &  all_ghost_data,
const Mesh< Dim > &  subcell_mesh,
const size_t  ghost_zone_size,
const size_t  number_of_rdmp_values_in_ghost_data 
)

Fill the flux_neighbor_data with pointers into the all_ghost_data.

The all_ghost_data is stored in the tag evolution::dg::subcell::Tags::GhostDataForReconstruction, and the ghost_zone_size should come from the FD reconstructor.