replace static_cast<int>(val + 0.5) with C++11's std::lround()

This commit is contained in:
Axel Kohlmeyer
2025-06-23 23:07:13 -04:00
parent d5d8887eb9
commit f7f5a3e3e4
8 changed files with 73 additions and 73 deletions

View File

@ -30,6 +30,8 @@
#include "potential_file_reader.h"
#include "update.h"
#include <cmath>
using namespace LAMMPS_NS;
/* ---------------------------------------------------------------------- */
@ -686,10 +688,9 @@ void PairEAMAPIP::file2array()
}
// set nr,nrho from cutoff and spacings
// 0.5 is for round-off in divide
nr = static_cast<int>(rmax / dr + 0.5);
nrho = static_cast<int>(rhomax / drho + 0.5);
nr = std::lround(rmax / dr);
nrho = std::lround(rhomax / drho);
// ------------------------------------------------------------------
// setup frho arrays

View File

@ -250,7 +250,7 @@ void FixPour::init()
delta = yhi - ylo;
}
double t = (-v_relative - sqrt(v_relative * v_relative - 2.0 * grav * delta)) / grav;
nfreq = static_cast<int>(t / update->dt + 0.5);
nfreq = std::lround(t / update->dt);
// 1st insertion on next timestep

View File

@ -206,7 +206,7 @@ void PairEAM::compute(int eflag, int vflag)
if (rsq < cutforcesq) {
jtype = type[j];
p = sqrt(rsq)*rdr + 1.0;
m = static_cast<int> (p);
m = static_cast<int>(p);
m = MIN(m,nr-1);
p -= m;
p = MIN(p,1.0);
@ -232,7 +232,7 @@ void PairEAM::compute(int eflag, int vflag)
for (ii = 0; ii < inum; ii++) {
i = ilist[ii];
p = rho[i]*rdrho + 1.0;
m = static_cast<int> (p);
m = static_cast<int>(p);
m = MAX(1,MIN(m,nrho-1));
p -= m;
p = MIN(p,1.0);
@ -283,7 +283,7 @@ void PairEAM::compute(int eflag, int vflag)
jtype = type[j];
r = sqrt(rsq);
p = r*rdr + 1.0;
m = static_cast<int> (p);
m = static_cast<int>(p);
m = MIN(m,nr-1);
p -= m;
p = MIN(p,1.0);
@ -636,10 +636,9 @@ void PairEAM::file2array()
}
// set nr,nrho from cutoff and spacings
// 0.5 is for round-off in divide
nr = static_cast<int> (rmax/dr + 0.5);
nrho = static_cast<int> (rhomax/drho + 0.5);
nr = std::lround(rmax/dr);
nrho = std::lround(rhomax/drho);
// ------------------------------------------------------------------
// setup frho arrays
@ -662,7 +661,7 @@ void PairEAM::file2array()
for (m = 1; m <= nrho; m++) {
r = (m-1)*drho;
p = r/file->drho + 1.0;
k = static_cast<int> (p);
k = static_cast<int>(p);
k = MIN(k,file->nrho-2);
k = MAX(k,2);
p -= k;
@ -709,7 +708,7 @@ void PairEAM::file2array()
for (m = 1; m <= nr; m++) {
r = (m-1)*dr;
p = r/file->dr + 1.0;
k = static_cast<int> (p);
k = static_cast<int>(p);
k = MIN(k,file->nr-2);
k = MAX(k,2);
p -= k;
@ -759,7 +758,7 @@ void PairEAM::file2array()
r = (m-1)*dr;
p = r/ifile->dr + 1.0;
k = static_cast<int> (p);
k = static_cast<int>(p);
k = MIN(k,ifile->nr-2);
k = MAX(k,2);
p -= k;
@ -772,7 +771,7 @@ void PairEAM::file2array()
cof3*ifile->zr[k+1] + cof4*ifile->zr[k+2];
p = r/jfile->dr + 1.0;
k = static_cast<int> (p);
k = static_cast<int>(p);
k = MIN(k,jfile->nr-2);
k = MAX(k,2);
p -= k;
@ -896,7 +895,7 @@ double PairEAM::single(int i, int j, int itype, int jtype,
if (numforce[i] > 0) {
p = rho[i]*rdrho + 1.0;
m = static_cast<int> (p);
m = static_cast<int>(p);
m = MAX(1,MIN(m,nrho-1));
p -= m;
p = MIN(p,1.0);
@ -908,7 +907,7 @@ double PairEAM::single(int i, int j, int itype, int jtype,
r = sqrt(rsq);
p = r*rdr + 1.0;
m = static_cast<int> (p);
m = static_cast<int>(p);
m = MIN(m,nr-1);
p -= m;
p = MIN(p,1.0);

View File

@ -1087,11 +1087,11 @@ int FixChargeRegulation::get_random_particle(int ptype, double charge, double rd
double dx, dy, dz, distance_check;
for (int i = 0; i < nlocal; i++) {
dx = fabs(atom->x[i][0] - target[0]);
dx -= static_cast<int>(1.0 * dx / (xhi - xlo) + 0.5) * (xhi - xlo);
dx -= std::lround(1.0 * dx / (xhi - xlo)) * (xhi - xlo);
dy = fabs(atom->x[i][1] - target[1]);
dy -= static_cast<int>(1.0 * dy / (yhi - ylo) + 0.5) * (yhi - ylo);
dy -= std::lround(1.0 * dy / (yhi - ylo)) * (yhi - ylo);
dz = fabs(atom->x[i][2] - target[2]);
dz -= static_cast<int>(1.0 * dz / (zhi - zlo) + 0.5) * (zhi - zlo);
dz -= std::lround(1.0 * dz / (zhi - zlo)) * (zhi - zlo);
distance_check = dx * dx + dy * dy + dz * dz;
if ((distance_check < rd * rd) && atom->type[i] == ptype &&
fabs(atom->q[i] - charge) < SMALL && atom->mask[i] != exclusion_group_bit) {
@ -1193,11 +1193,11 @@ int FixChargeRegulation::particle_number_xrd(int ptype, double charge, double rd
double dx, dy, dz, distance_check;
for (int i = 0; i < atom->nlocal; i++) {
dx = fabs(atom->x[i][0] - target[0]);
dx -= static_cast<int>(1.0 * dx / (xhi - xlo) + 0.5) * (xhi - xlo);
dx -= std::lround(1.0 * dx / (xhi - xlo)) * (xhi - xlo);
dy = fabs(atom->x[i][1] - target[1]);
dy -= static_cast<int>(1.0 * dy / (yhi - ylo) + 0.5) * (yhi - ylo);
dy -= std::lround(1.0 * dy / (yhi - ylo)) * (yhi - ylo);
dz = fabs(atom->x[i][2] - target[2]);
dz -= static_cast<int>(1.0 * dz / (zhi - zlo) + 0.5) * (zhi - zlo);
dz -= std::lround(1.0 * dz / (zhi - zlo)) * (zhi - zlo);
distance_check = dx * dx + dy * dy + dz * dz;
if ((distance_check < rd * rd) && atom->type[i] == ptype &&
fabs(atom->q[i] - charge) < SMALL && atom->mask[i] != exclusion_group_bit) {
@ -1283,10 +1283,10 @@ void FixChargeRegulation::restart(char *buf)
int n = 0;
auto list = (double *) buf;
seed = static_cast<int> (list[n++]);
seed = static_cast<int>(list[n++]);
random_equal->reset(seed);
seed = static_cast<int> (list[n++]);
seed = static_cast<int>(list[n++]);
random_unequal->reset(seed);
nacid_attempts = list[n++];

View File

@ -1268,7 +1268,7 @@ int ComputeChunkAtom::setup_xyz_bins()
if (lo > hi) error->all(FLERR, Error::NOLASTLINE, "Invalid bin bounds in compute chunk/atom");
offset[m] = lo;
nlayers[m] = static_cast<int>((hi - lo) * invdelta[m] + 0.5);
nlayers[m] = std::lround((hi - lo) * invdelta[m]);
nbins *= nlayers[m];
}

View File

@ -677,12 +677,12 @@ int DumpAtom::convert_image(int n, double *mybuf)
offset += snprintf(&sbuf[offset],
maxsbuf - offset,
format,
static_cast<tagint> (mybuf[m]),
static_cast<int> (mybuf[m+1]),
static_cast<tagint>(mybuf[m]),
static_cast<int>(mybuf[m+1]),
mybuf[m+2],mybuf[m+3],mybuf[m+4],
static_cast<int> (mybuf[m+5]),
static_cast<int> (mybuf[m+6]),
static_cast<int> (mybuf[m+7]));
static_cast<int>(mybuf[m+5]),
static_cast<int>(mybuf[m+6]),
static_cast<int>(mybuf[m+7]));
m += size_one;
}
@ -705,8 +705,8 @@ int DumpAtom::convert_noimage(int n, double *mybuf)
offset += snprintf(&sbuf[offset],
maxsbuf - offset,
format,
static_cast<tagint> (mybuf[m]),
static_cast<int> (mybuf[m+1]),
static_cast<tagint>(mybuf[m]),
static_cast<int>(mybuf[m+1]),
mybuf[m+2],mybuf[m+3],mybuf[m+4]);
m += size_one;
}
@ -738,9 +738,9 @@ void DumpAtom::write_lines_image(int n, double *mybuf)
int m = 0;
for (int i = 0; i < n; i++) {
fprintf(fp,format,
static_cast<tagint> (mybuf[m]), static_cast<int> (mybuf[m+1]),
mybuf[m+2],mybuf[m+3],mybuf[m+4], static_cast<int> (mybuf[m+5]),
static_cast<int> (mybuf[m+6]), static_cast<int> (mybuf[m+7]));
static_cast<tagint>(mybuf[m]), static_cast<int>(mybuf[m+1]),
mybuf[m+2],mybuf[m+3],mybuf[m+4], static_cast<int>(mybuf[m+5]),
static_cast<int>(mybuf[m+6]), static_cast<int>(mybuf[m+7]));
m += size_one;
}
}
@ -752,8 +752,8 @@ void DumpAtom::write_lines_noimage(int n, double *mybuf)
int m = 0;
for (int i = 0; i < n; i++) {
fprintf(fp,format,
static_cast<tagint> (mybuf[m]), static_cast<int> (mybuf[m+1]),
mybuf[m+2],mybuf[m+3],mybuf[m+4]);
static_cast<tagint>(mybuf[m]), static_cast<int>(mybuf[m+1]),
mybuf[m+2], mybuf[m+3], mybuf[m+4]);
m += size_one;
}
}

View File

@ -928,10 +928,10 @@ void DumpImage::create_image()
j = clist[i];
if (acolor == TYPE) {
itype = static_cast<int> (buf[m]);
itype = static_cast<int>(buf[m]);
color = colortype[itype];
} else if (acolor == ELEMENT) {
itype = static_cast<int> (buf[m]);
itype = static_cast<int>(buf[m]);
color = colorelement[itype];
} else if (acolor == ATTRIBUTE) {
color = image->map_value2color(0,buf[m]);
@ -940,10 +940,10 @@ void DumpImage::create_image()
if (adiam == NUMERIC) {
diameter = adiamvalue;
} else if (adiam == TYPE) {
itype = static_cast<int> (buf[m+1]);
itype = static_cast<int>(buf[m+1]);
diameter = diamtype[itype];
} else if (adiam == ELEMENT) {
itype = static_cast<int> (buf[m+1]);
itype = static_cast<int>(buf[m+1]);
diameter = diamelement[itype];
} else if (adiam == ATTRIBUTE) {
diameter = buf[m+1];
@ -1108,7 +1108,7 @@ void DumpImage::create_image()
if (body[j] < 0) continue;
if (bodycolor == TYPE) {
itype = static_cast<int> (buf[m]);
itype = static_cast<int>(buf[m]);
color = colortype[itype];
}
@ -1282,14 +1282,14 @@ void DumpImage::create_image()
// no fix draws spheres yet
} else if (fixvec[i] == LINE) {
if (fixcolor == TYPE) {
itype = static_cast<int> (fixarray[i][0]);
itype = static_cast<int>(fixarray[i][0]);
color = colortype[itype];
}
image->draw_cylinder(&fixarray[i][1],&fixarray[i][4],
color,fixflag1,3);
} else if (fixvec[i] == TRI) {
if (fixcolor == TYPE) {
itype = static_cast<int> (fixarray[i][0]);
itype = static_cast<int>(fixarray[i][0]);
color = colortype[itype];
}
p1 = &fixarray[i][1];
@ -1532,10 +1532,10 @@ void DumpImage::unpack_forward_comm(int n, int first, double *buf)
last = first + n;
if (comm_forward == 1)
for (i = first; i < last; i++) chooseghost[i] = static_cast<int> (buf[m++]);
for (i = first; i < last; i++) chooseghost[i] = static_cast<int>(buf[m++]);
else {
for (i = first; i < last; i++) {
chooseghost[i] = static_cast<int> (buf[m++]);
chooseghost[i] = static_cast<int>(buf[m++]);
bufcopy[i][0] = buf[m++];
bufcopy[i][1] = buf[m++];
}
@ -1638,9 +1638,9 @@ int DumpImage::modify_param(int narg, char **arg)
if (narg < 2) error->all(FLERR,"Illegal dump_modify command");
double *color = image->color2rgb(arg[1]);
if (color == nullptr) error->all(FLERR,"Invalid color in dump_modify command");
image->background[0] = static_cast<int> (color[0]*255.0);
image->background[1] = static_cast<int> (color[1]*255.0);
image->background[2] = static_cast<int> (color[2]*255.0);
image->background[0] = static_cast<int>(color[0]*255.0);
image->background[1] = static_cast<int>(color[1]*255.0);
image->background[2] = static_cast<int>(color[2]*255.0);
return 2;
}

View File

@ -272,7 +272,7 @@ void Image::view_params(double boxxlo, double boxxhi, double boxylo,
if (ssao) {
if (!random) random = new RanMars(lmp,seed+me);
SSAORadius = maxdel * 0.05 * ssaoint;
SSAOSamples = static_cast<int> (8.0 + 32.0*ssaoint);
SSAOSamples = static_cast<int>(8.0 + 32.0*ssaoint);
SSAOJitter = MY_PI / 12;
ambientColor[0] = 0.5;
ambientColor[1] = 0.5;
@ -364,8 +364,8 @@ void Image::merge()
MPI_Bcast(depthBuffer,npixels,MPI_DOUBLE,0,world);
compute_SSAO();
int pixelstart = 3 * static_cast<int> (1.0*me/nprocs * npixels);
int pixelstop = 3 * static_cast<int> (1.0*(me+1)/nprocs * npixels);
int pixelstart = 3 * static_cast<int>(1.0*me/nprocs * npixels);
int pixelstop = 3 * static_cast<int>(1.0*(me+1)/nprocs * npixels);
int mypixels = pixelstop - pixelstart;
if (npixels % nprocs == 0) {
@ -469,12 +469,12 @@ void Image::draw_sphere(double *x, double *surfaceColor, double diameter)
double pixelWidth = (tanPerPixel > 0) ? tanPerPixel * dist :
-tanPerPixel / zoom;
double pixelRadiusFull = radius / pixelWidth;
int pixelRadius = static_cast<int> (pixelRadiusFull + 0.5) + 1;
int pixelRadius = std::lround(pixelRadiusFull) + 1;
double xf = xmap / pixelWidth;
double yf = ymap / pixelWidth;
int xc = static_cast<int> (xf);
int yc = static_cast<int> (yf);
int xc = static_cast<int>(xf);
int yc = static_cast<int>(yf);
double width_error = xf - xc;
double height_error = yf - yc;
@ -531,12 +531,12 @@ void Image::draw_cube(double *x, double *surfaceColor, double diameter)
double halfWidth = diameter;
double pixelHalfWidthFull = halfWidth / pixelWidth;
int pixelHalfWidth = static_cast<int> (pixelHalfWidthFull + 0.5);
int pixelHalfWidth = std::lround(pixelHalfWidthFull);
double xf = xmap / pixelWidth;
double yf = ymap / pixelWidth;
int xc = static_cast<int> (xf);
int yc = static_cast<int> (yf);
int xc = static_cast<int>(xf);
int yc = static_cast<int>(yf);
double width_error = xf - xc;
double height_error = yf - yc;
@ -658,8 +658,8 @@ void Image::draw_cylinder(double *x, double *y,
double xf = xmap / pixelWidth;
double yf = ymap / pixelWidth;
int xc = static_cast<int> (xf);
int yc = static_cast<int> (yf);
int xc = static_cast<int>(xf);
int yc = static_cast<int>(yf);
double width_error = xf - xc;
double height_error = yf - yc;
@ -670,8 +670,8 @@ void Image::draw_cylinder(double *x, double *y,
double pixelHalfWidthFull = (rasterWidth * 0.5) / pixelWidth;
double pixelHalfHeightFull = (rasterHeight * 0.5) / pixelWidth;
int pixelHalfWidth = static_cast<int> (pixelHalfWidthFull + 0.5);
int pixelHalfHeight = static_cast<int> (pixelHalfHeightFull + 0.5);
int pixelHalfWidth = std::lround(pixelHalfWidthFull);
int pixelHalfHeight = std::lround(pixelHalfHeightFull);
if (zaxis[0] == camDir[0] && zaxis[1] == camDir[1] && zaxis[2] == camDir[2])
return;
@ -804,8 +804,8 @@ void Image::draw_triangle(double *x, double *y, double *z, double *surfaceColor)
double xf = xmap / pixelWidth;
double yf = ymap / pixelWidth;
int xc = static_cast<int> (xf);
int yc = static_cast<int> (yf);
int xc = static_cast<int>(xf);
int yc = static_cast<int>(yf);
double width_error = xf - xc;
double height_error = yf - yc;
@ -818,10 +818,10 @@ void Image::draw_triangle(double *x, double *y, double *z, double *surfaceColor)
double pixelRightFull = rasterRight / pixelWidth;
double pixelDownFull = rasterDown / pixelWidth;
double pixelUpFull = rasterUp / pixelWidth;
int pixelLeft = static_cast<int> (pixelLeftFull + 0.5);
int pixelRight = static_cast<int> (pixelRightFull + 0.5);
int pixelDown = static_cast<int> (pixelDownFull + 0.5);
int pixelUp = static_cast<int> (pixelUpFull + 0.5);
int pixelLeft = std::lround(pixelLeftFull);
int pixelRight = std::lround(pixelRightFull);
int pixelDown = std::lround(pixelDownFull);
int pixelUp = std::lround(pixelUpFull);
for (int iy = yc - pixelDown; iy <= yc + pixelUp; iy ++) {
for (int ix = xc - pixelLeft; ix <= xc + pixelRight; ix ++) {
@ -956,8 +956,8 @@ void Image::compute_SSAO()
// x = column # from 0 to width-1
// y = row # from 0 to height-1
int pixelstart = static_cast<int> (1.0*me/nprocs * npixels);
int pixelstop = static_cast<int> (1.0*(me+1)/nprocs * npixels);
int pixelstart = static_cast<int>(1.0*me/nprocs * npixels);
int pixelstop = static_cast<int>(1.0*(me+1)/nprocs * npixels);
// file buffer with random numbers to avoid race conditions
double *uniform = new double[pixelstop - pixelstart];
@ -992,8 +992,8 @@ void Image::compute_SSAO()
// Bresenham's line algorithm to march over depthBuffer
int dx = static_cast<int> (hx * pixelRadius);
int dy = static_cast<int> (hy * pixelRadius);
int dx = static_cast<int>(hx * pixelRadius);
int dy = static_cast<int>(hy * pixelRadius);
int ex = x + dx;
if (ex < 0) { ex = 0; } if (ex >= width) { ex = width - 1; }
int ey = y + dy;
@ -1997,7 +1997,7 @@ double *ColorMap::value2color(double value)
if (value >= mentry[i].lvalue && value <= mentry[i].hvalue)
return mentry[i].color;
} else {
int ibin = static_cast<int> ((value-lo) * mbinsizeinv);
int ibin = static_cast<int>((value-lo) * mbinsizeinv);
return mentry[ibin%nentry].color;
}