From 047be536bc3fb88c7ca04ecc6af1604c25524820 Mon Sep 17 00:00:00 2001 From: sjplimp Date: Wed, 2 Sep 2015 20:39:55 +0000 Subject: [PATCH] git-svn-id: svn://svn.icms.temple.edu/lammps-ro/trunk@13983 f3b2605a-c512-4ea7-a41b-209d697bcdaa --- src/atom_map.cpp | 2 +- src/atom_vec_atomic.cpp | 2 +- src/atom_vec_body.cpp | 4 +- src/atom_vec_charge.cpp | 2 +- src/atom_vec_ellipsoid.cpp | 4 +- src/atom_vec_hybrid.cpp | 2 +- src/atom_vec_line.cpp | 4 +- src/atom_vec_sphere.cpp | 2 +- src/atom_vec_tri.cpp | 4 +- src/balance.cpp | 1 + src/delete_atoms.cpp | 2 + src/dump.cpp | 6 ++- src/dump_image.cpp | 5 ++- src/finish.cpp | 4 +- src/fix_ave_atom.cpp | 2 + src/fix_ave_chunk.cpp | 10 ++++- src/fix_ave_correlate.cpp | 11 +++++- src/fix_ave_histo.cpp | 12 +++++- src/fix_ave_histo_weight.cpp | 13 +++++-- src/fix_ave_time.cpp | 11 +++++- src/group.cpp | 5 ++- src/library.cpp | 11 ++++++ src/library.h | 1 + src/molecule.cpp | 10 +++-- src/rcb.cpp | 1 + src/read_data.cpp | 2 + src/read_restart.cpp | 47 ++++++++++++++--------- src/reader_native.cpp | 10 ++++- src/run.cpp | 8 ++-- src/thermo.cpp | 2 +- src/universe.cpp | 48 +++++++++++++++++++++++- src/universe.h | 4 +- src/variable.cpp | 72 ++++++++++++++++-------------------- src/write_restart.cpp | 14 +++++-- src/write_restart.h | 2 +- 35 files changed, 236 insertions(+), 104 deletions(-) diff --git a/src/atom_map.cpp b/src/atom_map.cpp index a63db79cce..3613b59fca 100644 --- a/src/atom_map.cpp +++ b/src/atom_map.cpp @@ -369,7 +369,7 @@ int Atom::next_prime(int n) if (nprime % 2 == 0) nprime++; int root = static_cast (sqrt(1.0*n)) + 2; - while (nprime <= MAXSMALLINT) { + while (nprime < MAXSMALLINT) { for (factor = 3; factor < root; factor++) if (nprime % factor == 0) break; if (factor == root) return nprime; diff --git a/src/atom_vec_atomic.cpp b/src/atom_vec_atomic.cpp index c23d81e3cc..24d6f3d92c 100644 --- a/src/atom_vec_atomic.cpp +++ b/src/atom_vec_atomic.cpp @@ -51,7 +51,7 @@ void AtomVecAtomic::grow(int n) if (n == 0) grow_nmax(); else nmax = n; atom->nmax = nmax; - if (nmax < 0 || nmax > MAXSMALLINT) + if (nmax < 0) error->one(FLERR,"Per-processor system is too big"); tag = memory->grow(atom->tag,nmax,"atom:tag"); diff --git a/src/atom_vec_body.cpp b/src/atom_vec_body.cpp index efe6bd1200..b4b1d3f210 100644 --- a/src/atom_vec_body.cpp +++ b/src/atom_vec_body.cpp @@ -117,7 +117,7 @@ void AtomVecBody::grow(int n) if (n == 0) grow_nmax(); else nmax = n; atom->nmax = nmax; - if (nmax < 0 || nmax > MAXSMALLINT) + if (nmax < 0) error->one(FLERR,"Per-processor system is too big"); tag = memory->grow(atom->tag,nmax,"atom:tag"); @@ -158,7 +158,7 @@ void AtomVecBody::grow_reset() void AtomVecBody::grow_bonus() { nmax_bonus = grow_nmax_bonus(nmax_bonus); - if (nmax_bonus < 0 || nmax_bonus > MAXSMALLINT) + if (nmax_bonus < 0) error->one(FLERR,"Per-processor system is too big"); bonus = (Bonus *) memory->srealloc(bonus,nmax_bonus*sizeof(Bonus), diff --git a/src/atom_vec_charge.cpp b/src/atom_vec_charge.cpp index da10ef42dd..0cdea95e1a 100644 --- a/src/atom_vec_charge.cpp +++ b/src/atom_vec_charge.cpp @@ -53,7 +53,7 @@ void AtomVecCharge::grow(int n) if (n == 0) grow_nmax(); else nmax = n; atom->nmax = nmax; - if (nmax < 0 || nmax > MAXSMALLINT) + if (nmax < 0) error->one(FLERR,"Per-processor system is too big"); tag = memory->grow(atom->tag,nmax,"atom:tag"); diff --git a/src/atom_vec_ellipsoid.cpp b/src/atom_vec_ellipsoid.cpp index 81e4fbee20..889636963a 100755 --- a/src/atom_vec_ellipsoid.cpp +++ b/src/atom_vec_ellipsoid.cpp @@ -72,7 +72,7 @@ void AtomVecEllipsoid::grow(int n) if (n == 0) grow_nmax(); else nmax = n; atom->nmax = nmax; - if (nmax < 0 || nmax > MAXSMALLINT) + if (nmax < 0) error->one(FLERR,"Per-processor system is too big"); tag = memory->grow(atom->tag,nmax,"atom:tag"); @@ -113,7 +113,7 @@ void AtomVecEllipsoid::grow_reset() void AtomVecEllipsoid::grow_bonus() { nmax_bonus = grow_nmax_bonus(nmax_bonus); - if (nmax_bonus < 0 || nmax_bonus > MAXSMALLINT) + if (nmax_bonus < 0) error->one(FLERR,"Per-processor system is too big"); bonus = (Bonus *) memory->srealloc(bonus,nmax_bonus*sizeof(Bonus), diff --git a/src/atom_vec_hybrid.cpp b/src/atom_vec_hybrid.cpp index 63e7dd15e8..fb0dd9da7a 100644 --- a/src/atom_vec_hybrid.cpp +++ b/src/atom_vec_hybrid.cpp @@ -144,7 +144,7 @@ void AtomVecHybrid::grow(int n) if (n == 0) grow_nmax(); else nmax = n; atom->nmax = nmax; - if (nmax < 0 || nmax > MAXSMALLINT) + if (nmax < 0) error->one(FLERR,"Per-processor system is too big"); // sub-styles perform all reallocation diff --git a/src/atom_vec_line.cpp b/src/atom_vec_line.cpp index 6760589ecb..ccbed481d0 100644 --- a/src/atom_vec_line.cpp +++ b/src/atom_vec_line.cpp @@ -80,7 +80,7 @@ void AtomVecLine::grow(int n) if (n == 0) grow_nmax(); else nmax = n; atom->nmax = nmax; - if (nmax < 0 || nmax > MAXSMALLINT) + if (nmax < 0) error->one(FLERR,"Per-processor system is too big"); tag = memory->grow(atom->tag,nmax,"atom:tag"); @@ -123,7 +123,7 @@ void AtomVecLine::grow_reset() void AtomVecLine::grow_bonus() { nmax_bonus = grow_nmax_bonus(nmax_bonus); - if (nmax_bonus < 0 || nmax_bonus > MAXSMALLINT) + if (nmax_bonus < 0) error->one(FLERR,"Per-processor system is too big"); bonus = (Bonus *) memory->srealloc(bonus,nmax_bonus*sizeof(Bonus), diff --git a/src/atom_vec_sphere.cpp b/src/atom_vec_sphere.cpp index de85a2eb15..f67435a10f 100644 --- a/src/atom_vec_sphere.cpp +++ b/src/atom_vec_sphere.cpp @@ -84,7 +84,7 @@ void AtomVecSphere::grow(int n) if (n == 0) grow_nmax(); else nmax = n; atom->nmax = nmax; - if (nmax < 0 || nmax > MAXSMALLINT) + if (nmax < 0) error->one(FLERR,"Per-processor system is too big"); tag = memory->grow(atom->tag,nmax,"atom:tag"); diff --git a/src/atom_vec_tri.cpp b/src/atom_vec_tri.cpp index 4746a22c0b..9a120522cb 100644 --- a/src/atom_vec_tri.cpp +++ b/src/atom_vec_tri.cpp @@ -81,7 +81,7 @@ void AtomVecTri::grow(int n) if (n == 0) grow_nmax(); else nmax = n; atom->nmax = nmax; - if (nmax < 0 || nmax > MAXSMALLINT) + if (nmax < 0) error->one(FLERR,"Per-processor system is too big"); tag = memory->grow(atom->tag,nmax,"atom:tag"); @@ -124,7 +124,7 @@ void AtomVecTri::grow_reset() void AtomVecTri::grow_bonus() { nmax_bonus = grow_nmax_bonus(nmax_bonus); - if (nmax_bonus < 0 || nmax_bonus > MAXSMALLINT) + if (nmax_bonus < 0) error->one(FLERR,"Per-processor system is too big"); bonus = (Bonus *) memory->srealloc(bonus,nmax_bonus*sizeof(Bonus), diff --git a/src/balance.cpp b/src/balance.cpp index 40a59531b3..88ca0a62fa 100644 --- a/src/balance.cpp +++ b/src/balance.cpp @@ -659,6 +659,7 @@ int Balance::shift() if (bdim[idim] == X) split = comm->xsplit; else if (bdim[idim] == Y) split = comm->ysplit; else if (bdim[idim] == Z) split = comm->zsplit; + else continue; // intial count and sum diff --git a/src/delete_atoms.cpp b/src/delete_atoms.cpp index 0aaa3713ae..eca1cbd752 100644 --- a/src/delete_atoms.cpp +++ b/src/delete_atoms.cpp @@ -414,6 +414,8 @@ void DeleteAtoms::delete_porosity(int narg, char **arg) for (int i = 0; i < nlocal; i++) if (domain->regions[iregion]->match(x[i][0],x[i][1],x[i][2])) if (random->uniform() <= porosity_fraction) dlist[i] = 1; + + delete random; } /* ---------------------------------------------------------------------- diff --git a/src/dump.cpp b/src/dump.cpp index e1fc9f31ed..73c805df5e 100644 --- a/src/dump.cpp +++ b/src/dump.cpp @@ -160,6 +160,7 @@ Dump::~Dump() } else { if (filewriter) fclose(fp); } + fp = NULL; } } @@ -413,10 +414,11 @@ void Dump::write() if (multifile) { if (compressed) { - if (filewriter && fp) pclose(fp); + if (filewriter && fp != NULL) pclose(fp); } else { - if (filewriter && fp) fclose(fp); + if (filewriter && fp != NULL) fclose(fp); } + fp = NULL; } } diff --git a/src/dump_image.cpp b/src/dump_image.cpp index 6939bc9d5c..05074e609c 100644 --- a/src/dump_image.cpp +++ b/src/dump_image.cpp @@ -681,7 +681,7 @@ void DumpImage::create_image() color = colorelement[itype]; } else if (acolor == ATTRIBUTE) { color = image->map_value2color(0,buf[m]); - } + } else color = image->color2rgb("white"); if (adiam == NUMERIC) { diameter = adiamvalue; @@ -784,6 +784,9 @@ void DumpImage::create_image() } else if (acolor == ATTRIBUTE) { color1 = image->map_value2color(0,bufcopy[atom1][0]); color2 = image->map_value2color(0,bufcopy[atom2][0]); + } else { + color1 = image->color2rgb("white"); + color2 = image->color2rgb("white"); } } else if (bcolor == TYPE) { itype = btype; diff --git a/src/finish.cpp b/src/finish.cpp index b145809282..36a7973724 100644 --- a/src/finish.cpp +++ b/src/finish.cpp @@ -823,7 +823,7 @@ void Finish::stats(int n, double *data, /* ---------------------------------------------------------------------- */ -static void mpi_timings(const char *label, Timer *t, enum Timer::ttype tt, +void mpi_timings(const char *label, Timer *t, enum Timer::ttype tt, MPI_Comm world, const int nprocs, const int nthreads, const int me, double time_loop, FILE *scr, FILE *log) { @@ -876,7 +876,7 @@ static void mpi_timings(const char *label, Timer *t, enum Timer::ttype tt, /* ---------------------------------------------------------------------- */ #ifdef LMP_USER_OMP -static void omp_times(FixOMP *fix, const char *label, enum Timer::ttype which, +void omp_times(FixOMP *fix, const char *label, enum Timer::ttype which, const int nthreads,FILE *scr, FILE *log) { const char fmt[] = "%-8s|%- 12.5g|%- 12.5g|%- 12.5g|%6.1f |%6.2f\n"; diff --git a/src/fix_ave_atom.cpp b/src/fix_ave_atom.cpp index 3ee245c97d..68e370d60b 100644 --- a/src/fix_ave_atom.cpp +++ b/src/fix_ave_atom.cpp @@ -380,6 +380,8 @@ void FixAveAtom::end_of_step() nvalid = ntimestep+peratom_freq - (nrepeat-1)*nevery; modify->addstep_compute(nvalid); + if (array == NULL) return; + // average the final result for the Nfreq timestep double repeat = nrepeat; diff --git a/src/fix_ave_chunk.cpp b/src/fix_ave_chunk.cpp index 4e0006d166..07e858e592 100644 --- a/src/fix_ave_chunk.cpp +++ b/src/fix_ave_chunk.cpp @@ -325,6 +325,7 @@ FixAveChunk::FixAveChunk(LAMMPS *lmp, int narg, char **arg) : // print file comment lines if (fp && me == 0) { + clearerr(fp); if (title1) fprintf(fp,"%s\n",title1); else fprintf(fp,"# Chunk-averaged data for fix %s and group %s\n", id,arg[1]); @@ -350,6 +351,9 @@ FixAveChunk::FixAveChunk(LAMMPS *lmp, int narg, char **arg) : for (int i = 0; i < nvalues; i++) fprintf(fp," %s",arg[7+i]); fprintf(fp,"\n"); } + if (ferror(fp)) + error->one(FLERR,"Error writing file header"); + filepos = ftell(fp); } @@ -878,6 +882,7 @@ void FixAveChunk::end_of_step() // output result to file if (fp && me == 0) { + clearerr(fp); if (overwrite) fseek(fp,filepos,SEEK_SET); double count = 0.0; for (m = 0; m < nchunk; m++) count += count_total[m]; @@ -959,11 +964,14 @@ void FixAveChunk::end_of_step() } } } + if (ferror(fp)) + error->one(FLERR,"Error writing averaged chunk data"); fflush(fp); + if (overwrite) { long fileend = ftell(fp); - ftruncate(fileno(fp),fileend); + if (fileend > 0) ftruncate(fileno(fp),fileend); } } } diff --git a/src/fix_ave_correlate.cpp b/src/fix_ave_correlate.cpp index e240bdeee0..a3e20cb2db 100644 --- a/src/fix_ave_correlate.cpp +++ b/src/fix_ave_correlate.cpp @@ -232,6 +232,7 @@ FixAveCorrelate::FixAveCorrelate(LAMMPS * lmp, int narg, char **arg): // print file comment lines if (fp && me == 0) { + clearerr(fp); if (title1) fprintf(fp,"%s\n",title1); else fprintf(fp,"# Time-correlated data for fix %s\n",id); if (title2) fprintf(fp,"%s\n",title2); @@ -264,6 +265,9 @@ FixAveCorrelate::FixAveCorrelate(LAMMPS * lmp, int narg, char **arg): fprintf(fp," %s*%s",arg[6+i],arg[6+j]); fprintf(fp,"\n"); } + if (ferror(fp)) + error->one(FLERR,"Error writing file header"); + filepos = ftell(fp); } @@ -479,6 +483,7 @@ void FixAveCorrelate::end_of_step() // output result to file if (fp && me == 0) { + clearerr(fp); if (overwrite) fseek(fp,filepos,SEEK_SET); fprintf(fp,BIGINT_FORMAT " %d\n",ntimestep,nrepeat); for (i = 0; i < nrepeat; i++) { @@ -491,10 +496,14 @@ void FixAveCorrelate::end_of_step() fprintf(fp," 0.0"); fprintf(fp,"\n"); } + if (ferror(fp)) + error->one(FLERR,"Error writing out correlation data"); + fflush(fp); + if (overwrite) { long fileend = ftell(fp); - ftruncate(fileno(fp),fileend); + if (fileend > 0) ftruncate(fileno(fp),fileend); } } diff --git a/src/fix_ave_histo.cpp b/src/fix_ave_histo.cpp index cecb81ea34..921d6aacf8 100644 --- a/src/fix_ave_histo.cpp +++ b/src/fix_ave_histo.cpp @@ -436,6 +436,7 @@ FixAveHisto::FixAveHisto(LAMMPS *lmp, int narg, char **arg) : // print file comment lines if (fp && me == 0) { + clearerr(fp); if (title1) fprintf(fp,"%s\n",title1); else fprintf(fp,"# Histogrammed data for fix %s\n",id); if (title2) fprintf(fp,"%s\n",title2); @@ -443,6 +444,10 @@ FixAveHisto::FixAveHisto(LAMMPS *lmp, int narg, char **arg) : "Total-counts Missing-counts Min-value Max-value\n"); if (title3) fprintf(fp,"%s\n",title3); else fprintf(fp,"# Bin Coord Count Count/Total\n"); + + if (ferror(fp)) + error->one(FLERR,"Error writing file header"); + filepos = ftell(fp); } @@ -812,6 +817,7 @@ void FixAveHisto::end_of_step() // output result to file if (fp && me == 0) { + clearerr(fp); if (overwrite) fseek(fp,filepos,SEEK_SET); fprintf(fp,BIGINT_FORMAT " %d %g %g %g %g\n",ntimestep,nbins, stats_total[0],stats_total[1],stats_total[2],stats_total[3]); @@ -822,10 +828,14 @@ void FixAveHisto::end_of_step() else for (i = 0; i < nbins; i++) fprintf(fp,"%d %g %g %g\n",i+1,coord[i],0.0,0.0); + + if (ferror(fp)) + error->one(FLERR,"Error writing out histogram data"); + fflush(fp); if (overwrite) { long fileend = ftell(fp); - ftruncate(fileno(fp),fileend); + if (fileend > 0) ftruncate(fileno(fp),fileend); } } } diff --git a/src/fix_ave_histo_weight.cpp b/src/fix_ave_histo_weight.cpp index 432ce3c2f3..55a55fd5f9 100644 --- a/src/fix_ave_histo_weight.cpp +++ b/src/fix_ave_histo_weight.cpp @@ -274,11 +274,11 @@ void FixAveHistoWeight::end_of_step() // atom attributes - if (which[i] == X) + if (which[i] == X && weights != NULL) bin_atoms_weights(&atom->x[0][j],3,weights,stride); - else if (which[i] == V) + else if (which[i] == V && weights != NULL) bin_atoms_weights(&atom->v[0][j],3,weights,stride); - else if (which[i] == F) + else if (which[i] == F && weights != NULL) bin_atoms_weights(&atom->f[0][j],3,weights,stride); // invoke compute if not previously invoked @@ -479,6 +479,7 @@ void FixAveHistoWeight::end_of_step() // output result to file if (fp && me == 0) { + clearerr(fp); if (overwrite) fseek(fp,filepos,SEEK_SET); fprintf(fp,BIGINT_FORMAT " %d %g %g %g %g\n",ntimestep,nbins, stats_total[0],stats_total[1],stats_total[2],stats_total[3]); @@ -489,10 +490,14 @@ void FixAveHistoWeight::end_of_step() else for (i = 0; i < nbins; i++) fprintf(fp,"%d %g %g %g\n",i+1,coord[i],0.0,0.0); + + if (ferror(fp)) + error->one(FLERR,"Error writing out histogram data"); + fflush(fp); if (overwrite) { long fileend = ftell(fp); - ftruncate(fileno(fp),fileend); + if (fileend > 0) ftruncate(fileno(fp),fileend); } } } diff --git a/src/fix_ave_time.cpp b/src/fix_ave_time.cpp index 958c00d031..9c3b9e6753 100644 --- a/src/fix_ave_time.cpp +++ b/src/fix_ave_time.cpp @@ -294,6 +294,7 @@ FixAveTime::FixAveTime(LAMMPS *lmp, int narg, char **arg) : // since array args may have been expanded to multiple vectors if (fp && me == 0) { + clearerr(fp); if (title1) fprintf(fp,"%s\n",title1); else fprintf(fp,"# Time-averaged data for fix %s\n",id); if (title2) fprintf(fp,"%s\n",title2); @@ -313,6 +314,9 @@ FixAveTime::FixAveTime(LAMMPS *lmp, int narg, char **arg) : } fprintf(fp,"\n"); } + if (ferror(fp)) + error->one(FLERR,"Error writing file header"); + filepos = ftell(fp); } @@ -692,14 +696,19 @@ void FixAveTime::invoke_scalar(bigint ntimestep) // output result to file if (fp && me == 0) { + clearerr(fp); if (overwrite) fseek(fp,filepos,SEEK_SET); fprintf(fp,BIGINT_FORMAT,ntimestep); for (i = 0; i < nvalues; i++) fprintf(fp,format,vector_total[i]/norm); fprintf(fp,"\n"); + if (ferror(fp)) + error->one(FLERR,"Error writing out time averaged data"); + fflush(fp); + if (overwrite) { long fileend = ftell(fp); - ftruncate(fileno(fp),fileend); + if (fileend > 0) ftruncate(fileno(fp),fileend); } } } diff --git a/src/group.cpp b/src/group.cpp index f0d16c6df6..0b5d6974ea 100644 --- a/src/group.cpp +++ b/src/group.cpp @@ -301,6 +301,7 @@ void Group::assign(int narg, char **arg) tagint start,stop,delta; for (int iarg = 2; iarg < narg; iarg++) { + delta = 1; if (strchr(arg[iarg],':')) { ptr = strtok(arg[iarg],":"); start = force->tnumeric(FLERR,ptr); @@ -308,11 +309,11 @@ void Group::assign(int narg, char **arg) stop = force->tnumeric(FLERR,ptr); ptr = strtok(NULL,":"); if (ptr) delta = force->tnumeric(FLERR,ptr); - else delta = 1; } else { start = stop = force->tnumeric(FLERR,arg[iarg]); - delta = 1; } + if (delta < 1) + error->all(FLERR,"Illegal range increment value"); // add to group if attribute matches value or sequence diff --git a/src/library.cpp b/src/library.cpp index 545bef4e9f..ea3c66398a 100644 --- a/src/library.cpp +++ b/src/library.cpp @@ -19,6 +19,7 @@ #include "stdlib.h" #include "library.h" #include "lammps.h" +#include "universe.h" #include "input.h" #include "atom.h" #include "domain.h" @@ -79,6 +80,16 @@ void lammps_close(void *ptr) delete lmp; } +/* ---------------------------------------------------------------------- + get the numerical representation of the current LAMMPS version +------------------------------------------------------------------------- */ + +int lammps_version(void *ptr) +{ + LAMMPS *lmp = (LAMMPS *) ptr; + return atoi(lmp->universe->num_ver); +} + /* ---------------------------------------------------------------------- process an input script in filename str ------------------------------------------------------------------------- */ diff --git a/src/library.h b/src/library.h index fddc7312a4..a01d593b21 100644 --- a/src/library.h +++ b/src/library.h @@ -27,6 +27,7 @@ extern "C" { void lammps_open(int, char **, MPI_Comm, void **); void lammps_open_no_mpi(int, char **, void **); void lammps_close(void *); +int lammps_version(void *); void lammps_file(void *, char *); char *lammps_command(void *, char *); void lammps_free(void *); diff --git a/src/molecule.cpp b/src/molecule.cpp index a5b34170ad..ee8f9ae424 100644 --- a/src/molecule.cpp +++ b/src/molecule.cpp @@ -409,11 +409,13 @@ void Molecule::read(int flag) else break; } - // error check + // error checks - if (flag == 0) { - if (natoms == 0) error->all(FLERR,"No atom count in molecule file"); - } + if (natoms < 1) error->all(FLERR,"No or invalid atom count in molecule file"); + if (nbonds < 0) error->all(FLERR,"Invalid bond count in molecule file"); + if (nangles < 0) error->all(FLERR,"Invalid angle count in molecule file"); + if (ndihedrals < 0) error->all(FLERR,"Invalid dihedral count in molecule file"); + if (nimpropers < 0) error->all(FLERR,"Invalid improper count in molecule file"); // count = vector for tallying bonds,angles,etc per atom diff --git a/src/rcb.cpp b/src/rcb.cpp index df58567758..94cdb155ac 100644 --- a/src/rcb.cpp +++ b/src/rcb.cpp @@ -270,6 +270,7 @@ void RCB::compute(int dimension, int n, double **x, double *wt, valuemin = lo[dim]; valuemax = hi[dim]; first_iteration = 1; + indexlo = indexhi = 0; while (1) { diff --git a/src/read_data.cpp b/src/read_data.cpp index 2ab972cef7..036e333bdb 100644 --- a/src/read_data.cpp +++ b/src/read_data.cpp @@ -78,6 +78,7 @@ ReadData::ReadData(LAMMPS *lmp) : Pointers(lmp) buffer = new char[CHUNK*MAXLINE]; narg = maxarg = 0; arg = NULL; + fp = NULL; // customize for new sections // pointers to atom styles that store extra info @@ -618,6 +619,7 @@ void ReadData::command(int narg, char **arg) if (me == 0) { if (compressed) pclose(fp); else fclose(fp); + fp = NULL; } // done if this was 2nd pass diff --git a/src/read_restart.cpp b/src/read_restart.cpp index 8b76181e4b..7e757b75ff 100644 --- a/src/read_restart.cpp +++ b/src/read_restart.cpp @@ -187,7 +187,10 @@ void ReadRestart::command(int narg, char **arg) // close header file if in multiproc mode - if (multiproc && me == 0) fclose(fp); + if (multiproc && me == 0) { + fclose(fp); + fp = NULL; + } // read per-proc info @@ -263,7 +266,10 @@ void ReadRestart::command(int narg, char **arg) } } - if (me == 0) fclose(fp); + if (me == 0) { + fclose(fp); + fp = NULL; + } } // input of multiple native files with procs <= files @@ -311,6 +317,7 @@ void ReadRestart::command(int narg, char **arg) } fclose(fp); + fp = NULL; } delete [] procfile; @@ -412,7 +419,10 @@ void ReadRestart::command(int narg, char **arg) } } - if (filereader) fclose(fp); + if (filereader && fp != NULL) { + fclose(fp); + fp = NULL; + } MPI_Comm_free(&clustercomm); } @@ -641,7 +651,7 @@ void ReadRestart::file_search(char *infile, char *outfile) void ReadRestart::header(int incompatible) { - int xperiodic,yperiodic,zperiodic; + int xperiodic(-1),yperiodic(-1),zperiodic(-1); // read flags and fields until flag = -1 @@ -783,6 +793,9 @@ void ReadRestart::header(int incompatible) domain->boundary[2][0] = boundary[2][0]; domain->boundary[2][1] = boundary[2][1]; + if (xperiodic < 0 || yperiodic < 0 || zperiodic < 0) + error->all(FLERR,"Illegal or unset periodicity in restart"); + domain->periodicity[0] = domain->xperiodic = xperiodic; domain->periodicity[1] = domain->yperiodic = yperiodic; domain->periodicity[2] = domain->zperiodic = zperiodic; @@ -1089,9 +1102,7 @@ void ReadRestart::magic_string() void ReadRestart::endian() { - int endian; - if (me == 0) fread(&endian,sizeof(int),1,fp); - MPI_Bcast(&endian,1,MPI_INT,0,world); + int endian = read_int(); if (endian == ENDIAN) return; if (endian == ENDIANSWAP) error->all(FLERR,"Restart file byte ordering is swapped"); @@ -1103,9 +1114,7 @@ void ReadRestart::endian() int ReadRestart::version_numeric() { - int vn; - if (me == 0) fread(&vn,sizeof(int),1,fp); - MPI_Bcast(&vn,1,MPI_INT,0,world); + int vn = read_int(); if (vn != VERSION_NUMERIC) return 1; return 0; } @@ -1117,7 +1126,8 @@ int ReadRestart::version_numeric() int ReadRestart::read_int() { int value; - if (me == 0) fread(&value,sizeof(int),1,fp); + if ((me == 0) && (fread(&value,sizeof(int),1,fp) < sizeof(int))) + value = -1; MPI_Bcast(&value,1,MPI_INT,0,world); return value; } @@ -1129,7 +1139,8 @@ int ReadRestart::read_int() bigint ReadRestart::read_bigint() { bigint value; - if (me == 0) fread(&value,sizeof(bigint),1,fp); + if ((me == 0) && (fread(&value,sizeof(bigint),1,fp) < sizeof(bigint))) + value = -1; MPI_Bcast(&value,1,MPI_LMP_BIGINT,0,world); return value; } @@ -1141,7 +1152,8 @@ bigint ReadRestart::read_bigint() double ReadRestart::read_double() { double value; - if (me == 0) fread(&value,sizeof(double),1,fp); + if ((me == 0) && (fread(&value,sizeof(double),1,fp) < sizeof(double))) + value = 0.0; MPI_Bcast(&value,1,MPI_DOUBLE,0,world); return value; } @@ -1153,9 +1165,8 @@ double ReadRestart::read_double() char *ReadRestart::read_string() { - int n; - if (me == 0) fread(&n,sizeof(int),1,fp); - MPI_Bcast(&n,1,MPI_INT,0,world); + int n = read_int(); + if (n < 0) error->all(FLERR,"Illegal size string or corrupt restart"); char *value = new char[n]; if (me == 0) fread(value,sizeof(char),n,fp); MPI_Bcast(value,n,MPI_CHAR,0,world); @@ -1164,22 +1175,22 @@ char *ReadRestart::read_string() /* ---------------------------------------------------------------------- read vector of N ints from restart file and bcast them - do not bcast them, caller does that if required ------------------------------------------------------------------------- */ void ReadRestart::read_int_vec(int n, int *vec) { + if (n < 0) error->all(FLERR,"Illegal size integer vector read requested"); if (me == 0) fread(vec,sizeof(int),n,fp); MPI_Bcast(vec,n,MPI_INT,0,world); } /* ---------------------------------------------------------------------- read vector of N doubles from restart file and bcast them - do not bcast them, caller does that if required ------------------------------------------------------------------------- */ void ReadRestart::read_double_vec(int n, double *vec) { + if (n < 0) error->all(FLERR,"Illegal size double vector read requested"); if (me == 0) fread(vec,sizeof(double),n,fp); MPI_Bcast(vec,n,MPI_DOUBLE,0,world); } diff --git a/src/reader_native.cpp b/src/reader_native.cpp index f4f802947d..769a651739 100644 --- a/src/reader_native.cpp +++ b/src/reader_native.cpp @@ -141,10 +141,16 @@ bigint ReaderNative::read_header(double box[3][3], int &triclinic, nwords = atom->count_words(labelline); char **labels = new char*[nwords]; labels[0] = strtok(labelline," \t\n\r\f"); - if (labels[0] == NULL) return 1; + if (labels[0] == NULL) { + delete[] labels; + return 1; + } for (int m = 1; m < nwords; m++) { labels[m] = strtok(NULL," \t\n\r\f"); - if (labels[m] == NULL) return 1; + if (labels[m] == NULL) { + delete[] labels; + return 1; + } } // match each field with a column of per-atom data diff --git a/src/run.cpp b/src/run.cpp index 62b888114f..d321f921f8 100644 --- a/src/run.cpp +++ b/src/run.cpp @@ -118,13 +118,13 @@ void Run::command(int narg, char **arg) // error check if (startflag) { - if (start < 0 || start > MAXBIGINT) + if (start < 0) error->all(FLERR,"Invalid run command start/stop value"); if (start > update->ntimestep) error->all(FLERR,"Run command start value is after start of run"); } if (stopflag) { - if (stop < 0 || stop > MAXBIGINT) + if (stop < 0) error->all(FLERR,"Invalid run command start/stop value"); if (stop < update->ntimestep + nsteps) error->all(FLERR,"Run command stop value is before end of run"); @@ -157,7 +157,7 @@ void Run::command(int narg, char **arg) update->nsteps = nsteps; update->firststep = update->ntimestep; update->laststep = update->ntimestep + nsteps; - if (update->laststep < 0 || update->laststep > MAXBIGINT) + if (update->laststep < 0 || update->laststep < update->firststep) error->all(FLERR,"Too many timesteps"); if (startflag) update->beginstep = start; @@ -195,7 +195,7 @@ void Run::command(int narg, char **arg) update->nsteps = nsteps; update->firststep = update->ntimestep; update->laststep = update->ntimestep + nsteps; - if (update->laststep < 0 || update->laststep > MAXBIGINT) + if (update->laststep < 0 || update->laststep < update->firststep) error->all(FLERR,"Too many timesteps"); if (startflag) update->beginstep = start; diff --git a/src/thermo.cpp b/src/thermo.cpp index bef74d0358..34ccfce9f4 100644 --- a/src/thermo.cpp +++ b/src/thermo.cpp @@ -378,7 +378,7 @@ bigint Thermo::lost_check() bigint ntotal; bigint nblocal = atom->nlocal; MPI_Allreduce(&nblocal,&ntotal,1,MPI_LMP_BIGINT,MPI_SUM,world); - if (ntotal < 0 || ntotal > MAXBIGINT) + if (ntotal < 0) error->all(FLERR,"Too many total atoms"); if (ntotal == atom->natoms) return ntotal; diff --git a/src/universe.cpp b/src/universe.cpp index d1665599cf..35415bffa9 100644 --- a/src/universe.cpp +++ b/src/universe.cpp @@ -12,6 +12,7 @@ ------------------------------------------------------------------------- */ #include "mpi.h" +#include "ctype.h" #include "stdlib.h" #include "string.h" #include "stdio.h" @@ -25,13 +26,16 @@ using namespace LAMMPS_NS; #define MAXLINE 256 +static char *date2num(const char *version); + /* ---------------------------------------------------------------------- create & initialize the universe of processors in communicator ------------------------------------------------------------------------- */ Universe::Universe(LAMMPS *lmp, MPI_Comm communicator) : Pointers(lmp) { - version = (char *) LAMMPS_VERSION; + version = (const char *) LAMMPS_VERSION; + num_ver = date2num(version); uworld = uorig = communicator; MPI_Comm_rank(uworld,&me); @@ -57,6 +61,7 @@ Universe::~Universe() memory->destroy(procs_per_world); memory->destroy(root_proc); memory->destroy(uni2orig); + delete [] num_ver; } /* ---------------------------------------------------------------------- @@ -194,3 +199,44 @@ int Universe::consistent() if (n == nprocs) return 1; else return 0; } + +// helper function to convert the LAMMPS date string to a version id +// that can be used for both string and numerical comparisons +// where newer versions are larger than older ones. + +char *date2num(const char *version) +{ + int day,month,year; + day = month = year = 0; + + if (version) { + + day = atoi(version); + + while (*version != '\0' && (isdigit(*version) || *version == ' ')) + ++version; + + if (strncmp(version,"Jan",3) == 0) month = 1; + if (strncmp(version,"Feb",3) == 0) month = 2; + if (strncmp(version,"Mar",3) == 0) month = 3; + if (strncmp(version,"Apr",3) == 0) month = 4; + if (strncmp(version,"May",3) == 0) month = 5; + if (strncmp(version,"Jun",3) == 0) month = 6; + if (strncmp(version,"Jul",3) == 0) month = 7; + if (strncmp(version,"Aug",3) == 0) month = 8; + if (strncmp(version,"Sep",3) == 0) month = 9; + if (strncmp(version,"Oct",3) == 0) month = 10; + if (strncmp(version,"Nov",3) == 0) month = 11; + if (strncmp(version,"Dec",3) == 0) month = 12; + + while (*version != '\0' && !isdigit(*version)) + ++version; + + year = atoi(version); + } + + char *ver = new char[10]; + sprintf(ver,"%04d%02d%02d", year % 10000, month, day % 100); + + return ver; +} diff --git a/src/universe.h b/src/universe.h index 3dbd6f9fc5..897b6c9681 100644 --- a/src/universe.h +++ b/src/universe.h @@ -21,7 +21,9 @@ namespace LAMMPS_NS { class Universe : protected Pointers { public: - char *version; // LAMMPS version string = date + const char *version; // LAMMPS version string = date + const char *num_ver; // numeric version id derived from version that + // can be used for string or numeric comparisons MPI_Comm uworld; // communicator for entire universe int me,nprocs; // my place in universe diff --git a/src/variable.cpp b/src/variable.cpp index 0b11372c71..b929caa50c 100644 --- a/src/variable.cpp +++ b/src/variable.cpp @@ -262,8 +262,11 @@ void Variable::set(int narg, char **arg) if (universe->me == 0) { FILE *fp = fopen("tmp.lammps.variable","w"); + if (fp == NULL) + error->one(FLERR,"Cannot open temporary file for world counter."); fprintf(fp,"%d\n",universe->nworlds); fclose(fp); + fp = NULL; } for (int jvar = 0; jvar < nvar; jvar++) @@ -518,7 +521,7 @@ int Variable::next(int narg, char **arg) for (int iarg = 0; iarg < narg; iarg++) { ivar = find(arg[iarg]); - if (ivar == -1) error->all(FLERR,"Invalid variable in next command"); + if (ivar < 0) error->all(FLERR,"Invalid variable in next command"); if (style[ivar] == ULOOP && style[find(arg[0])] == UNIVERSE) continue; else if (style[ivar] == UNIVERSE && style[find(arg[0])] == ULOOP) continue; else if (style[ivar] != style[find(arg[0])]) @@ -614,6 +617,7 @@ int Variable::next(int narg, char **arg) fprintf(fp,"%d\n",nextindex+1); //printf("WRITE %d %d\n",universe->me,nextindex+1); fclose(fp); + fp = NULL; rename("tmp.lammps.variable.lock","tmp.lammps.variable"); if (universe->uscreen) fprintf(universe->uscreen, @@ -716,7 +720,7 @@ int Variable::atomstyle(int ivar) char *Variable::pythonstyle(char *name, char *funcname) { int ivar = find(name); - if (ivar == -1) return NULL; + if (ivar < 0) return NULL; if (style[ivar] != PYTHON) return NULL; if (strcmp(data[ivar][0],funcname) != 0) return NULL; return data[ivar][1]; @@ -739,7 +743,7 @@ char *Variable::pythonstyle(char *name, char *funcname) char *Variable::retrieve(char *name) { int ivar = find(name); - if (ivar == -1) return NULL; + if (ivar < 0) return NULL; if (which[ivar] >= num[ivar]) return NULL; if (eval_in_progress[ivar]) @@ -857,6 +861,11 @@ void Variable::compute_atom(int ivar, int igroup, collapse_tree(tree); } else vstore = reader[ivar]->fixstore->vstore; + if (result == NULL) { + eval_in_progress[ivar] = 0; + return; + } + int groupbit = group->bitmask[igroup]; int *mask = atom->mask; int nlocal = atom->nlocal; @@ -1139,15 +1148,10 @@ double Variable::evaluate(char *str, Tree **tree) error->all(FLERR, "Variable evaluation before simulation box is defined"); - n = strlen(word) - 2 + 1; - char *id = new char[n]; - strcpy(id,&word[2]); - - int icompute = modify->find_compute(id); + int icompute = modify->find_compute(word+2); if (icompute < 0) error->all(FLERR,"Invalid compute ID in variable formula"); Compute *compute = modify->compute[icompute]; - delete [] id; // parse zero or one or two trailing brackets // point i beyond last bracket @@ -1366,14 +1370,9 @@ double Variable::evaluate(char *str, Tree **tree) error->all(FLERR, "Variable evaluation before simulation box is defined"); - n = strlen(word) - 2 + 1; - char *id = new char[n]; - strcpy(id,&word[2]); - - int ifix = modify->find_fix(id); + int ifix = modify->find_fix(word+2); if (ifix < 0) error->all(FLERR,"Invalid fix ID in variable formula"); Fix *fix = modify->fix[ifix]; - delete [] id; // parse zero or one or two trailing brackets // point i beyond last bracket @@ -1545,11 +1544,8 @@ double Variable::evaluate(char *str, Tree **tree) // ---------------- } else if (strncmp(word,"v_",2) == 0) { - n = strlen(word) - 2 + 1; - char *id = new char[n]; - strcpy(id,&word[2]); - int ivar = find(id); + int ivar = find(word+2); if (ivar < 0) error->all(FLERR,"Invalid variable name in variable formula"); if (eval_in_progress[ivar]) @@ -1574,7 +1570,7 @@ double Variable::evaluate(char *str, Tree **tree) if (nbracket == 0 && style[ivar] != ATOM && style[ivar] != ATOMFILE) { - char *var = retrieve(id); + char *var = retrieve(word+2); if (var == NULL) error->all(FLERR,"Invalid variable evaluation in variable formula"); if (tree) { @@ -1636,8 +1632,6 @@ double Variable::evaluate(char *str, Tree **tree) } else error->all(FLERR,"Mismatched variable in variable formula"); - delete [] id; - // ---------------- // math/group/special function or atom value/vector or // constant or thermo keyword @@ -3745,7 +3739,7 @@ int Variable::special_function(char *word, char *contents, Tree **tree, error->all(FLERR,"Invalid special function in variable formula"); int ivar = find(args[0]); - if (ivar == -1) + if (ivar < 0) error->all(FLERR,"Variable ID in variable formula does not exist"); // SCALARFILE has single current value, read next one @@ -3981,23 +3975,25 @@ void Variable::atom_vector(char *word, Tree **tree, /* ---------------------------------------------------------------------- check if word matches a constant return 1 if yes, else 0 - customize by adding a constant: PI + customize by adding a constant: PI, version ------------------------------------------------------------------------- */ int Variable::is_constant(char *word) { if (strcmp(word,"PI") == 0) return 1; + if (strcmp(word,"version") == 0) return 1; return 0; } /* ---------------------------------------------------------------------- process a constant in formula - customize by adding a constant: PI + customize by adding a constant: PI, version ------------------------------------------------------------------------- */ double Variable::constant(char *word) { if (strcmp(word,"PI") == 0) return MY_PI; + if (strcmp(word,"version") == 0) return atof(universe->num_ver); return 0.0; } @@ -4344,17 +4340,11 @@ unsigned int Variable::data_mask(char *str) error->all(FLERR, "Variable evaluation before simulation box is defined"); - n = strlen(word) - 2 + 1; - char *id = new char[n]; - strcpy(id,&word[2]); - - int icompute = modify->find_compute(id); + int icompute = modify->find_compute(word+2); if (icompute < 0) error->all(FLERR,"Invalid compute ID in variable formula"); datamask &= modify->compute[icompute]->data_mask(); - - delete [] id; } if ((strncmp(word,"f_",2) == 0) && (i>0) && (!isalnum(str[i-1]))) { @@ -4362,19 +4352,15 @@ unsigned int Variable::data_mask(char *str) error->all(FLERR, "Variable evaluation before simulation box is defined"); - n = strlen(word) - 2 + 1; - char *id = new char[n]; - strcpy(id,&word[2]); - - int ifix = modify->find_fix(id); + int ifix = modify->find_fix(word+2); if (ifix < 0) error->all(FLERR,"Invalid fix ID in variable formula"); datamask &= modify->fix[ifix]->data_mask(); - delete [] id; } if ((strncmp(word,"v_",2) == 0) && (i>0) && (!isalnum(str[i-1]))) { - int ivar = find(word); + int ivar = find(word+2); + if (ivar < 0) error->all(FLERR,"Invalid variable name in variable formula"); datamask &= data_mask(ivar); } @@ -4395,6 +4381,7 @@ VarReader::VarReader(LAMMPS *lmp, char *name, char *file, int flag) : { me = comm->me; style = flag; + fp = NULL; if (me == 0) { fp = fopen(file,"r"); @@ -4403,7 +4390,7 @@ VarReader::VarReader(LAMMPS *lmp, char *name, char *file, int flag) : sprintf(str,"Cannot open file variable file %s",file); error->one(FLERR,str); } - } else fp = NULL; + } // if atomfile-style variable, must store per-atom values read from file // allocate a new fix STORE, so they persist @@ -4441,7 +4428,10 @@ VarReader::VarReader(LAMMPS *lmp, char *name, char *file, int flag) : VarReader::~VarReader() { - if (me == 0) fclose(fp); + if (me == 0) { + fclose(fp); + fp = NULL; + } // check modify in case all fixes have already been deleted diff --git a/src/write_restart.cpp b/src/write_restart.cpp index d891a7f782..c2824eff99 100644 --- a/src/write_restart.cpp +++ b/src/write_restart.cpp @@ -72,6 +72,7 @@ WriteRestart::WriteRestart(LAMMPS *lmp) : Pointers(lmp) MPI_Comm_rank(world,&me); MPI_Comm_size(world,&nprocs); multiproc = 0; + fp = NULL; } /* ---------------------------------------------------------------------- @@ -308,7 +309,10 @@ void WriteRestart::write(char *file) // write PROCSPERFILE into new file if (multiproc) { - if (me == 0) fclose(fp); + if (me == 0 && fp) { + fclose(fp); + fp = NULL; + } char *multiname = new char[strlen(file) + 16]; char *ptr = strchr(file,'%'); @@ -387,7 +391,10 @@ void WriteRestart::write(char *file) // MPI-IO output to single file if (mpiioflag) { - if (me == 0) fclose(fp); + if (me == 0 && fp) { + fclose(fp); + fp = NULL; + } mpiio->openForWrite(file); mpiio->write(headerOffset,send_size,buf); mpiio->close(); @@ -415,6 +422,7 @@ void WriteRestart::write(char *file) write_double_vec(PERPROC,recv_size,buf); } fclose(fp); + fp = NULL; } else { MPI_Recv(&tmp,0,MPI_INT,fileproc,0,world,MPI_STATUS_IGNORE); @@ -667,7 +675,7 @@ void WriteRestart::write_double(int flag, double value) write a flag and a char string (including NULL) into restart file ------------------------------------------------------------------------- */ -void WriteRestart::write_string(int flag, char *value) +void WriteRestart::write_string(int flag, const char *value) { int n = strlen(value) + 1; fwrite(&flag,sizeof(int),1,fp); diff --git a/src/write_restart.h b/src/write_restart.h index 16249e4bb6..a27da32e3d 100644 --- a/src/write_restart.h +++ b/src/write_restart.h @@ -62,7 +62,7 @@ class WriteRestart : protected Pointers { void write_int(int, int); void write_bigint(int, bigint); void write_double(int, double); - void write_string(int, char *); + void write_string(int, const char *); void write_int_vec(int, int, int *); void write_double_vec(int, int, double *); };