Skip to content

Conversation

@lucafedeli88
Copy link
Member

[TO DO]

@lucafedeli88 lucafedeli88 added component: QED QED module component: ABLASTR components shared with other PIC codes labels Feb 17, 2025
EZoni pushed a commit that referenced this pull request Feb 28, 2025
Pure functions like `linear_interp`, `bilinear_interp`, and
`trilinear_interp` are very general. Therefore, we can consider to move
them to `ablastr::math`. Besides, I will need some of these functions to
move `picsar_qed` inside `ablastr`
(#5677)
lucafedeli88 added a commit that referenced this pull request Nov 5, 2025
…n using single/double precision (#5828)

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

```C++
    /** Mathematical constants */
    namespace math
    {
        //! ratio of a circle's circumference to its diameter (variable template)
        template<typename T>
        constexpr auto pi_v = static_cast<T>(3.14159265358979323846);

        //! https://tauday.com/tau-manifesto (variable template)
        template<typename T>
        constexpr auto tau_v = static_cast<T>(2.0 * pi_v<double>);

        //! ratio of a circle's circumference to its diameter
        constexpr auto pi = pi_v<amrex::Real>;

        //! https://tauday.com/tau-manifesto
        constexpr auto tau = tau_v<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. :

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

const auto b = 2 * math::pi_v<double>; // b is a double !

const auto c = 2 * math::pi_v<float>; // c is a float !
```

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.

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.


## Previous discussions

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

```C++
    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. :

```C++
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.

### Updates
- Following @dpgrote 's suggestion, I've changed `variable_templates` to
`const_templates`
- Following @aeriforme 's suggestion, I've added `c2`, `invc` and
`inv_c2` constants, and I have tested them by modifying a couple of
source files.
- Following @WeiqunZhang 's suggestion, I am now using `_v` suffix to
indicate variable templates, as done in C++20
https://en.cppreference.com/w/cpp/numeric/constants.html
@lucafedeli88
Copy link
Member Author

We will do this step-by-step. See #6427

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 component: QED QED module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant