Skip to content

Commit

Permalink
<cmath>: Use std:: C++ Functions
Browse files Browse the repository at this point in the history
Only the `std::` math functions have overloads for `double` and
`float`. Without the `std::` prefix, we use the C functions, which
will when we run ImpactX in single precision cause up and down casts
to `double` (performance penalty).
  • Loading branch information
ax3l committed Sep 26, 2024
1 parent 3a8ab8d commit 978a2e2
Show file tree
Hide file tree
Showing 36 changed files with 271 additions and 271 deletions.
10 changes: 5 additions & 5 deletions src/particles/ReferenceParticle.H
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ namespace impactx
using namespace amrex::literals;

amrex::ParticleReal const ref_gamma = -pt;
amrex::ParticleReal const ref_beta = sqrt(1.0_prt - 1.0_prt/pow(ref_gamma,2));
amrex::ParticleReal const ref_beta = std::sqrt(1.0_prt - 1.0_prt/pow(ref_gamma,2));
return ref_beta;
}

Expand All @@ -81,7 +81,7 @@ namespace impactx
using namespace amrex::literals;

amrex::ParticleReal const ref_gamma = -pt;
amrex::ParticleReal const ref_betagamma = sqrt(pow(ref_gamma, 2) - 1.0_prt);
amrex::ParticleReal const ref_betagamma = std::sqrt(std::pow(ref_gamma, 2) - 1.0_prt);
return ref_betagamma;
}

Expand Down Expand Up @@ -118,7 +118,7 @@ namespace impactx
if (pt != 0.0_prt)
{
pt = -kin_energy_MeV() / massE - 1.0_prt;
pz = sqrt(pow(pt, 2) - 1.0_prt);
pz = std::sqrt(std::pow(pt, 2) - 1.0_prt);
}

return *this;
Expand Down Expand Up @@ -155,7 +155,7 @@ namespace impactx
px = 0.0;
py = 0.0;
pt = -kin_energy / mass_MeV() - 1.0_prt;
pz = sqrt(pow(pt, 2) - 1.0_prt);
pz = std::sqrt(std::pow(pt, 2) - 1.0_prt);

return *this;
}
Expand All @@ -171,7 +171,7 @@ namespace impactx
using namespace amrex::literals;

amrex::ParticleReal const ref_gamma = -pt;
amrex::ParticleReal const ref_betagamma = sqrt(pow(ref_gamma, 2) - 1.0_prt);
amrex::ParticleReal const ref_betagamma = std::sqrt(std::pow(ref_gamma, 2) - 1.0_prt);
//amrex::ParticleReal const ref_rigidity = mass*ref_betagamma*(ablastr::constant::SI::c)/charge; //fails due to "charge"
amrex::ParticleReal const ref_rigidity = mass*ref_betagamma*(ablastr::constant::SI::c)/(ablastr::constant::SI::q_e);
return ref_rigidity;
Expand Down
2 changes: 1 addition & 1 deletion src/particles/diagnostics/NonlinearLensInvariants.H
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ namespace impactx::diagnostics
Complex const re1(1.0_prt, 0.0_prt);
Complex const im1(0.0_prt, 1.0_prt);

// compute croot = sqrt(1-zeta**2)
// compute croot = std::sqrt(1-zeta**2)
Complex croot = amrex::pow(zeta, 2);
croot = re1 - croot;
croot = amrex::sqrt(croot);
Expand Down
24 changes: 12 additions & 12 deletions src/particles/distribution/Gaussian.H
Original file line number Diff line number Diff line change
Expand Up @@ -100,34 +100,34 @@ namespace impactx::distribution

u1 = amrex::Random(engine);
u2 = amrex::Random(engine);
ln1 = sqrt(-2_prt*log(u1));
x = ln1*cos(2_prt*pi*u2);
px = ln1*sin(2_prt*pi*u2);
ln1 = std::sqrt(-2_prt*std::log(u1));
x = ln1 * std::cos(2_prt*pi*u2);
px = ln1 * std::sin(2_prt*pi*u2);

u1 = amrex::Random(engine);
u2 = amrex::Random(engine);
ln1 = sqrt(-2_prt*log(u1));
y = ln1*cos(2_prt*pi*u2);
py = ln1*sin(2_prt*pi*u2);
ln1 = std::sqrt(-2_prt*std::log(u1));
y = ln1 * std::cos(2_prt*pi*u2);
py = ln1 * std::sin(2_prt*pi*u2);

u1 = amrex::Random(engine);
u2 = amrex::Random(engine);
ln1 = sqrt(-2_prt*log(u1));
t = ln1*cos(2_prt*pi*u2);
pt = ln1*sin(2_prt*pi*u2);
ln1 = std::sqrt(-2_prt*std::log(u1));
t = ln1 * std::cos(2_prt*pi*u2);
pt = ln1 * std::sin(2_prt*pi*u2);

// Transform to produce the desired second moments/correlations:
root = sqrt(1.0_prt-m_muxpx*m_muxpx);
root = std::sqrt(1.0_prt-m_muxpx*m_muxpx);
a1 = m_lambdaX * x / root;
a2 = m_lambdaPx * (-m_muxpx * x / root + px);
x = a1;
px = a2;
root = sqrt(1.0_prt-m_muypy*m_muypy);
root = std::sqrt(1.0_prt-m_muypy*m_muypy);
a1 = m_lambdaY * y / root;
a2 = m_lambdaPy * (-m_muypy * y / root + py);
y = a1;
py = a2;
root = sqrt(1.0_prt-m_mutpt*m_mutpt);
root = std::sqrt(1.0_prt-m_mutpt*m_mutpt);
a1 = m_lambdaT * t / root;
a2 = m_lambdaPt * (-m_mutpt * t / root + pt);
t = a1;
Expand Down
24 changes: 12 additions & 12 deletions src/particles/distribution/KVdist.H
Original file line number Diff line number Diff line change
Expand Up @@ -101,27 +101,27 @@ namespace impactx::distribution
v = amrex::Random(engine);
phi = amrex::Random(engine);
phi = 2_prt*pi*phi;
r = sqrt(v);
x = r*cos(phi);
y = r*sin(phi);
r = std::sqrt(v);
x = r * std::cos(phi);
y = r * std::sin(phi);

// Sample and transform to define (px,py):
beta = amrex::Random(engine);
beta = 2_prt*pi*beta;
p = sqrt(1_prt-pow(r,2));
px = p*cos(beta);
py = p*sin(beta);
p = std::sqrt(1_prt-pow(r,2));
px = p * std::cos(beta);
py = p * std::sin(beta);

// Sample and transform to define (t,pt):
t = amrex::Random(engine);
t = 2.0_prt*(t-0.5_prt);
u1 = amrex::Random(engine);
u2 = amrex::Random(engine);
ln1 = sqrt(-2_prt*log(u1));
pt = ln1*cos(2_prt*pi*u2);
ln1 = std::sqrt(-2_prt*std::log(u1));
pt = ln1 * std::cos(2_prt*pi*u2);

// Scale to produce the identity covariance matrix:
amrex::ParticleReal const c = sqrt(3.0_prt);
amrex::ParticleReal const c = std::sqrt(3.0_prt);
x = 2_prt*x;
y = 2_prt*y;
t = c*t;
Expand All @@ -130,17 +130,17 @@ namespace impactx::distribution
// pt = pt;

// Transform to produce the desired second moments/correlations:
root = sqrt(1.0_prt-m_muxpx*m_muxpx);
root = std::sqrt(1.0_prt-m_muxpx*m_muxpx);
a1 = m_lambdaX * x / root;
a2 = m_lambdaPx * (-m_muxpx * x / root + px);
x = a1;
px = a2;
root = sqrt(1.0_prt-m_muypy*m_muypy);
root = std::sqrt(1.0_prt-m_muypy*m_muypy);
a1 = m_lambdaY * y / root;
a2 = m_lambdaPy * (-m_muypy * y / root + py);
y = a1;
py = a2;
root = sqrt(1.0_prt-m_mutpt*m_mutpt);
root = std::sqrt(1.0_prt-m_mutpt*m_mutpt);
a1 = m_lambdaT * t / root;
a2 = m_lambdaPt * (-m_mutpt * t / root + pt);
t = a1;
Expand Down
28 changes: 14 additions & 14 deletions src/particles/distribution/Kurth4D.H
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ namespace impactx::distribution
v = amrex::Random(engine);
phi = amrex::Random(engine);
phi = 2_prt*pi*phi;
r = sqrt(v);
x = r*cos(phi);
y = r*sin(phi);
r = std::sqrt(v);
x = r * std::cos(phi);
y = r * std::sin(phi);

// Random samples used to define Lz:
u = amrex::Random(engine);
Expand All @@ -113,25 +113,25 @@ namespace impactx::distribution
// Random samples used to define pr:
alpha = amrex::Random(engine);
alpha = pi*alpha;
pmax = 1.0_prt - pow((Lz/r),2) - pow(r,2) + pow(Lz,2);
pmax = sqrt(pmax);
pr = pmax*cos(alpha);
pmax = 1.0_prt - std::pow((Lz/r),2) - std::pow(r,2) + std::pow(Lz,2);
pmax = std::sqrt(pmax);
pr = pmax * std::cos(alpha);
pphi = Lz/r;

// Transformations used to obtain (px,py):
px = pr*cos(phi)-pphi*sin(phi);
py = pr*sin(phi)+pphi*cos(phi);
px = pr * std::cos(phi)-pphi * std::sin(phi);
py = pr * std::sin(phi)+pphi * std::cos(phi);

// Sample and transform to define (t,pt):
t = amrex::Random(engine);
t = 2.0_prt*(t-0.5_prt);
u1 = amrex::Random(engine);
u2 = amrex::Random(engine);
ln1 = sqrt(-2_prt*log(u1));
pt = ln1*cos(2_prt*pi*u2);
ln1 = std::sqrt(-2_prt*std::log(u1));
pt = ln1 * std::cos(2_prt*pi*u2);

// Scale to produce the identity covariance matrix:
amrex::ParticleReal const c = sqrt(3.0_prt);
amrex::ParticleReal const c = std::sqrt(3.0_prt);
x = 2_prt*x;
y = 2_prt*y;
t = c*t;
Expand All @@ -140,17 +140,17 @@ namespace impactx::distribution
// pt = pt;

// Transform to produce the desired second moments/correlations:
root = sqrt(1.0_prt-m_muxpx*m_muxpx);
root = std::sqrt(1.0_prt-m_muxpx*m_muxpx);
a1 = m_lambdaX * x / root;
a2 = m_lambdaPx * (-m_muxpx * x / root + px);
x = a1;
px = a2;
root = sqrt(1.0_prt-m_muypy*m_muypy);
root = std::sqrt(1.0_prt-m_muypy*m_muypy);
a1 = m_lambdaY * y / root;
a2 = m_lambdaPy * (-m_muypy * y / root + py);
y = a1;
py = a2;
root = sqrt(1.0_prt-m_mutpt*m_mutpt);
root = std::sqrt(1.0_prt-m_mutpt*m_mutpt);
a1 = m_lambdaT * t / root;
a2 = m_lambdaPt * (-m_mutpt * t / root + pt);
t = a1;
Expand Down
30 changes: 15 additions & 15 deletions src/particles/distribution/Kurth6D.H
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,14 @@ namespace impactx::distribution
v = amrex::Random(engine);
costheta = amrex::Random(engine);
costheta = 2_prt*(costheta-0.5_prt);
sintheta = sqrt(1_prt-pow(costheta,2));
sintheta = std::sqrt(1_prt-pow(costheta,2));
phi = amrex::Random(engine);
phi = 2_prt*pi*phi;

// Transformations for (x,y,t):
r = pow(v,1_prt/3_prt);
x = r*sintheta*cos(phi);
y = r*sintheta*sin(phi);
r = std::pow(v,1_prt/3_prt);
x = r*sintheta * std::cos(phi);
y = r*sintheta * std::sin(phi);
t = r*costheta;

// Random samples used to define L:
Expand All @@ -121,23 +121,23 @@ namespace impactx::distribution
// Random samples used to define pr:
alpha = amrex::Random(engine);
alpha = pi*alpha;
pmax = 1_prt - pow(L/r,2) - pow(r,2) + pow(L,2);
pmax = sqrt(pmax);
pr = pmax*cos(alpha);
pmax = 1_prt - std::pow(L/r,2) - std::pow(r,2) + std::pow(L,2);
pmax = std::sqrt(pmax);
pr = pmax * std::cos(alpha);

// Random samples used to define ptangent:
beta = amrex::Random(engine);
beta = 2_prt*pi*beta;
p1 = L/r*cos(beta); // This is phi component
p2 = L/r*sin(beta); // This is theta component
p1 = L/r * std::cos(beta); // This is phi component
p2 = L/r * std::sin(beta); // This is theta component

// Transformation from spherical to Cartesian coord.:
px = pr*sintheta*cos(phi) + p2*costheta*cos(phi) - p1*sin(phi);
py = pr*sintheta*sin(phi) + p2*costheta*sin(phi) + p1*cos(phi);
px = pr*sintheta * std::cos(phi) + p2*costheta * std::cos(phi) - p1 * std::sin(phi);
py = pr*sintheta * std::sin(phi) + p2*costheta * std::sin(phi) + p1 * std::cos(phi);
pt = pr*costheta - p2*sintheta;

// Scale to produce the identity covariance matrix:
amrex::ParticleReal const c = sqrt(5.0_prt);
amrex::ParticleReal const c = std::sqrt(5.0_prt);
x = c*x;
y = c*y;
t = c*t;
Expand All @@ -146,17 +146,17 @@ namespace impactx::distribution
pt = c*pt;

// Transform to produce the desired second moments/correlations:
root = sqrt(1.0_prt-m_muxpx*m_muxpx);
root = std::sqrt(1.0_prt-m_muxpx*m_muxpx);
a1 = m_lambdaX * x / root;
a2 = m_lambdaPx * (-m_muxpx * x / root + px);
x = a1;
px = a2;
root = sqrt(1.0_prt-m_muypy*m_muypy);
root = std::sqrt(1.0_prt-m_muypy*m_muypy);
a1 = m_lambdaY * y / root;
a2 = m_lambdaPy * (-m_muypy * y / root + py);
y = a1;
py = a2;
root = sqrt(1.0_prt-m_mutpt*m_mutpt);
root = std::sqrt(1.0_prt-m_mutpt*m_mutpt);
a1 = m_lambdaT * t / root;
a2 = m_lambdaPt * (-m_mutpt * t / root + pt);
t = a1;
Expand Down
24 changes: 12 additions & 12 deletions src/particles/distribution/Semigaussian.H
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,14 @@ namespace impactx::distribution
phi = amrex::Random(engine);
phi = 2_prt*pi*phi;
v = amrex::Random(engine);
r = sqrt(v);
x = r*cos(phi);
y = r*sin(phi);
r = std::sqrt(v);
x = r * std::cos(phi);
y = r * std::sin(phi);
t = amrex::Random(engine);
t = 2_prt*(t-0.5_prt);

// Scale to produce the identity covariance matrix:
amrex::ParticleReal const c = sqrt(3.0_prt);
amrex::ParticleReal const c = std::sqrt(3.0_prt);
x = 2_prt*x;
y = 2_prt*y;
t = c*t;
Expand All @@ -119,27 +119,27 @@ namespace impactx::distribution

u1 = amrex::Random(engine);
u2 = amrex::Random(engine);
ln1 = sqrt(-2_prt*log(u1));
px = ln1*cos(2_prt*pi*u2);
py = ln1*sin(2_prt*pi*u2);
ln1 = std::sqrt(-2_prt*std::log(u1));
px = ln1 * std::cos(2_prt*pi*u2);
py = ln1 * std::sin(2_prt*pi*u2);

u1 = amrex::Random(engine);
u2 = amrex::Random(engine);
ln1 = sqrt(-2_prt*log(u1));
pt = ln1*cos(2_prt*pi*u2);
ln1 = std::sqrt(-2_prt*std::log(u1));
pt = ln1 * std::cos(2_prt*pi*u2);

// Transform to produce the desired second moments/correlations:
root = sqrt(1.0_prt-m_muxpx*m_muxpx);
root = std::sqrt(1.0_prt-m_muxpx*m_muxpx);
a1 = m_lambdaX * x / root;
a2 = m_lambdaPx * (-m_muxpx * x / root + px);
x = a1;
px = a2;
root = sqrt(1.0_prt-m_muypy*m_muypy);
root = std::sqrt(1.0_prt-m_muypy*m_muypy);
a1 = m_lambdaY * y / root;
a2 = m_lambdaPy * (-m_muypy * y / root + py);
y = a1;
py = a2;
root = sqrt(1.0_prt-m_mutpt*m_mutpt);
root = std::sqrt(1.0_prt-m_mutpt*m_mutpt);
a1 = m_lambdaT * t / root;
a2 = m_lambdaPt * (-m_mutpt * t / root + pt);
t = a1;
Expand Down
Loading

0 comments on commit 978a2e2

Please sign in to comment.