Skip to content

ablastr constants : add variable templates to allow for flexibility in using single/double precision #5828

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: development
Choose a base branch
from

Conversation

lucafedeli88
Copy link
Member

@lucafedeli88 lucafedeli88 commented Apr 11, 2025

With this PR the constants in ablastr are first defined using variable templates and then specialized for amrex::Real, e.g., for mathematical constants:

    namespace math
    {
        namespace variable_templates
        {

            template<typename T>
            constexpr auto pi = static_cast<T>(3.14159265358979323846);

            template<typename T>
            constexpr auto tau = static_cast<T>(2.0 * pi<double>);

        } 

        constexpr auto pi = variable_templates::pi<amrex::Real>;

        constexpr auto tau = variable_templates::tau<amrex::Real>;

    } // namespace math

This is done with the intent of allowing the use of double precision constants even if the code is compiled in single precision (or vice versa if you really want it !), e.g. :

using namespace ablastr;
namespace math_vt = math::variable_templates;

const auto a = 2_rt * math::pi; // a is an amrex::Real

const auto b = 2 * math_vt::pi<double>; // b is a double !

I will need this feature to import the QED module from PICSAR into WarpX. I've started migrating the code in #5677 ,but the PR is becoming too big, so I decided to split it into smaller chunks.

The new feature is now used in the template function Algorithms::KineticEnergy . This function is conceived to be able to force double precision in some cases regardless of how WarpX has been compiled. Therefore, using c in double precision in these cases is coherent with its goal.

In case you don't like the long namespace name variable_template, an alternative would be to call it vt.

Note that I've changed the variables from static constexpr to constexpr because, to my understanding, static does not make any difference in this context.

@lucafedeli88 lucafedeli88 added the component: ABLASTR components shared with other PIC codes label Apr 11, 2025
@lucafedeli88 lucafedeli88 requested review from RemiLehe and EZoni April 11, 2025 14:09
@lucafedeli88 lucafedeli88 changed the title Ablastr constants : add variable templates to allow for flexibility in using different precisions for constants Ablastr constants : add variable templates to allow for flexibility in using different precisions Apr 11, 2025
@lucafedeli88 lucafedeli88 changed the title Ablastr constants : add variable templates to allow for flexibility in using different precisions Ablastr constants : add variable templates to allow for flexibility in using single/double precision Apr 11, 2025
@lucafedeli88 lucafedeli88 changed the title Ablastr constants : add variable templates to allow for flexibility in using single/double precision ablastr constants : add variable templates to allow for flexibility in using single/double precision Apr 11, 2025
@@ -17,13 +17,24 @@ namespace ablastr::constant
/** Mathematical constants */
namespace math
{
using namespace amrex::literals;
namespace variable_templates
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is for constants, perhaps call it constant_templates?

@EZoni EZoni requested review from WeiqunZhang and atmyers April 15, 2025 23:49
@EZoni EZoni self-assigned this Apr 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: ABLASTR components shared with other PIC codes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants