From f7f5a3e3e4399f07338c390148ace5e85a0b7c0d Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Mon, 23 Jun 2025 23:07:13 -0400 Subject: [PATCH] replace static_cast(val + 0.5) with C++11's std::lround() --- src/APIP/pair_eam_apip.cpp | 7 +++-- src/GRANULAR/fix_pour.cpp | 2 +- src/MANYBODY/pair_eam.cpp | 23 ++++++++------- src/MC/fix_charge_regulation.cpp | 16 +++++------ src/compute_chunk_atom.cpp | 2 +- src/dump_atom.cpp | 24 ++++++++-------- src/dump_image.cpp | 24 ++++++++-------- src/image.cpp | 48 ++++++++++++++++---------------- 8 files changed, 73 insertions(+), 73 deletions(-) diff --git a/src/APIP/pair_eam_apip.cpp b/src/APIP/pair_eam_apip.cpp index 5cdc618f48..55b9e13c18 100644 --- a/src/APIP/pair_eam_apip.cpp +++ b/src/APIP/pair_eam_apip.cpp @@ -30,6 +30,8 @@ #include "potential_file_reader.h" #include "update.h" +#include + 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(rmax / dr + 0.5); - nrho = static_cast(rhomax / drho + 0.5); + nr = std::lround(rmax / dr); + nrho = std::lround(rhomax / drho); // ------------------------------------------------------------------ // setup frho arrays diff --git a/src/GRANULAR/fix_pour.cpp b/src/GRANULAR/fix_pour.cpp index d7ab40b658..2a50a6fdda 100644 --- a/src/GRANULAR/fix_pour.cpp +++ b/src/GRANULAR/fix_pour.cpp @@ -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(t / update->dt + 0.5); + nfreq = std::lround(t / update->dt); // 1st insertion on next timestep diff --git a/src/MANYBODY/pair_eam.cpp b/src/MANYBODY/pair_eam.cpp index fcef67b985..c073888806 100644 --- a/src/MANYBODY/pair_eam.cpp +++ b/src/MANYBODY/pair_eam.cpp @@ -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 (p); + m = static_cast(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 (p); + m = static_cast(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 (p); + m = static_cast(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 (rmax/dr + 0.5); - nrho = static_cast (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 (p); + k = static_cast(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 (p); + k = static_cast(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 (p); + k = static_cast(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 (p); + k = static_cast(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 (p); + m = static_cast(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 (p); + m = static_cast(p); m = MIN(m,nr-1); p -= m; p = MIN(p,1.0); diff --git a/src/MC/fix_charge_regulation.cpp b/src/MC/fix_charge_regulation.cpp index f5be7a041b..b2aa5f0dc1 100644 --- a/src/MC/fix_charge_regulation.cpp +++ b/src/MC/fix_charge_regulation.cpp @@ -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(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(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(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(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(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(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 (list[n++]); + seed = static_cast(list[n++]); random_equal->reset(seed); - seed = static_cast (list[n++]); + seed = static_cast(list[n++]); random_unequal->reset(seed); nacid_attempts = list[n++]; diff --git a/src/compute_chunk_atom.cpp b/src/compute_chunk_atom.cpp index 7e6d1d41a9..9abed97227 100644 --- a/src/compute_chunk_atom.cpp +++ b/src/compute_chunk_atom.cpp @@ -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((hi - lo) * invdelta[m] + 0.5); + nlayers[m] = std::lround((hi - lo) * invdelta[m]); nbins *= nlayers[m]; } diff --git a/src/dump_atom.cpp b/src/dump_atom.cpp index e81157b4ad..23dcbb8dcb 100644 --- a/src/dump_atom.cpp +++ b/src/dump_atom.cpp @@ -677,12 +677,12 @@ int DumpAtom::convert_image(int n, double *mybuf) offset += snprintf(&sbuf[offset], maxsbuf - offset, format, - static_cast (mybuf[m]), - static_cast (mybuf[m+1]), + static_cast(mybuf[m]), + static_cast(mybuf[m+1]), mybuf[m+2],mybuf[m+3],mybuf[m+4], - static_cast (mybuf[m+5]), - static_cast (mybuf[m+6]), - static_cast (mybuf[m+7])); + static_cast(mybuf[m+5]), + static_cast(mybuf[m+6]), + static_cast(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 (mybuf[m]), - static_cast (mybuf[m+1]), + static_cast(mybuf[m]), + static_cast(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 (mybuf[m]), static_cast (mybuf[m+1]), - mybuf[m+2],mybuf[m+3],mybuf[m+4], static_cast (mybuf[m+5]), - static_cast (mybuf[m+6]), static_cast (mybuf[m+7])); + static_cast(mybuf[m]), static_cast(mybuf[m+1]), + mybuf[m+2],mybuf[m+3],mybuf[m+4], static_cast(mybuf[m+5]), + static_cast(mybuf[m+6]), static_cast(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 (mybuf[m]), static_cast (mybuf[m+1]), - mybuf[m+2],mybuf[m+3],mybuf[m+4]); + static_cast(mybuf[m]), static_cast(mybuf[m+1]), + mybuf[m+2], mybuf[m+3], mybuf[m+4]); m += size_one; } } diff --git a/src/dump_image.cpp b/src/dump_image.cpp index 83f4999f43..30d73f3b47 100644 --- a/src/dump_image.cpp +++ b/src/dump_image.cpp @@ -928,10 +928,10 @@ void DumpImage::create_image() j = clist[i]; if (acolor == TYPE) { - itype = static_cast (buf[m]); + itype = static_cast(buf[m]); color = colortype[itype]; } else if (acolor == ELEMENT) { - itype = static_cast (buf[m]); + itype = static_cast(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 (buf[m+1]); + itype = static_cast(buf[m+1]); diameter = diamtype[itype]; } else if (adiam == ELEMENT) { - itype = static_cast (buf[m+1]); + itype = static_cast(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 (buf[m]); + itype = static_cast(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 (fixarray[i][0]); + itype = static_cast(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 (fixarray[i][0]); + itype = static_cast(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 (buf[m++]); + for (i = first; i < last; i++) chooseghost[i] = static_cast(buf[m++]); else { for (i = first; i < last; i++) { - chooseghost[i] = static_cast (buf[m++]); + chooseghost[i] = static_cast(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 (color[0]*255.0); - image->background[1] = static_cast (color[1]*255.0); - image->background[2] = static_cast (color[2]*255.0); + image->background[0] = static_cast(color[0]*255.0); + image->background[1] = static_cast(color[1]*255.0); + image->background[2] = static_cast(color[2]*255.0); return 2; } diff --git a/src/image.cpp b/src/image.cpp index 7954857423..f56bc7902a 100644 --- a/src/image.cpp +++ b/src/image.cpp @@ -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 (8.0 + 32.0*ssaoint); + SSAOSamples = static_cast(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 (1.0*me/nprocs * npixels); - int pixelstop = 3 * static_cast (1.0*(me+1)/nprocs * npixels); + int pixelstart = 3 * static_cast(1.0*me/nprocs * npixels); + int pixelstop = 3 * static_cast(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 (pixelRadiusFull + 0.5) + 1; + int pixelRadius = std::lround(pixelRadiusFull) + 1; double xf = xmap / pixelWidth; double yf = ymap / pixelWidth; - int xc = static_cast (xf); - int yc = static_cast (yf); + int xc = static_cast(xf); + int yc = static_cast(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 (pixelHalfWidthFull + 0.5); + int pixelHalfWidth = std::lround(pixelHalfWidthFull); double xf = xmap / pixelWidth; double yf = ymap / pixelWidth; - int xc = static_cast (xf); - int yc = static_cast (yf); + int xc = static_cast(xf); + int yc = static_cast(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 (xf); - int yc = static_cast (yf); + int xc = static_cast(xf); + int yc = static_cast(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 (pixelHalfWidthFull + 0.5); - int pixelHalfHeight = static_cast (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 (xf); - int yc = static_cast (yf); + int xc = static_cast(xf); + int yc = static_cast(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 (pixelLeftFull + 0.5); - int pixelRight = static_cast (pixelRightFull + 0.5); - int pixelDown = static_cast (pixelDownFull + 0.5); - int pixelUp = static_cast (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 (1.0*me/nprocs * npixels); - int pixelstop = static_cast (1.0*(me+1)/nprocs * npixels); + int pixelstart = static_cast(1.0*me/nprocs * npixels); + int pixelstop = static_cast(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 (hx * pixelRadius); - int dy = static_cast (hy * pixelRadius); + int dx = static_cast(hx * pixelRadius); + int dy = static_cast(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 ((value-lo) * mbinsizeinv); + int ibin = static_cast((value-lo) * mbinsizeinv); return mentry[ibin%nentry].color; }