diff --git a/src/atom.cpp b/src/atom.cpp index 54e5c34b36..8a5bacc645 100755 --- a/src/atom.cpp +++ b/src/atom.cpp @@ -1991,6 +1991,7 @@ void Atom::add_molecule_atom(Molecule *onemol, int iatom, void Atom::add_label_map() { + labelmapflag = 1; lmap = new LabelMap(lmp); lmap->natomtypes = ntypes; lmap->nbondtypes = nbondtypes; diff --git a/src/input.cpp b/src/input.cpp old mode 100644 new mode 100755 diff --git a/src/input.h b/src/input.h old mode 100644 new mode 100755 diff --git a/src/label_map.cpp b/src/label_map.cpp index 9c5db1af01..5031a8fc9c 100755 --- a/src/label_map.cpp +++ b/src/label_map.cpp @@ -25,6 +25,7 @@ using namespace LAMMPS_NS; LabelMap::LabelMap(LAMMPS *lmp) : Pointers(lmp) { + MPI_Comm_rank(world,&me); natomtypes = nbondtypes = nangletypes = 0; ndihedraltypes = nimpropertypes = 0; } @@ -50,8 +51,6 @@ LabelMap::~LabelMap() /* ---------------------------------------------------------------------- allocate character-based type arrays (labels) of length ntypes - always allocated (for both numeric and character-based type modes) - initialize label with (a string of) its numeric counterpart ------------------------------------------------------------------------- */ void LabelMap::allocate_type_labels() @@ -59,25 +58,17 @@ void LabelMap::allocate_type_labels() typelabel.resize(natomtypes); lmap2lmap.atom = new int[natomtypes]; - if (force->bond) { - btypelabel.resize(nbondtypes); - lmap2lmap.bond = new int[nbondtypes]; - } + btypelabel.resize(nbondtypes); + lmap2lmap.bond = new int[nbondtypes]; - if (force->angle) { - atypelabel.resize(nangletypes); - lmap2lmap.angle = new int[nangletypes]; - } + atypelabel.resize(nangletypes); + lmap2lmap.angle = new int[nangletypes]; - if (force->dihedral) { - dtypelabel.resize(ndihedraltypes); - lmap2lmap.dihedral = new int[ndihedraltypes]; - } + dtypelabel.resize(ndihedraltypes); + lmap2lmap.dihedral = new int[ndihedraltypes]; - if (force->improper) { - itypelabel.resize(nimpropertypes); - lmap2lmap.improper = new int[nimpropertypes]; - } + itypelabel.resize(nimpropertypes); + lmap2lmap.improper = new int[nimpropertypes]; } /* ---------------------------------------------------------------------- @@ -251,3 +242,105 @@ void LabelMap::write_data(FILE *fp) fmt::print(fp,"{} {}\n",i+1,itypelabel[i]); } } + +/* ---------------------------------------------------------------------- + proc 0 reads from restart file, bcasts +------------------------------------------------------------------------- */ + +void LabelMap::read_restart(FILE *fp) +{ + char *charlabel; + + for (int i = 0; i < natomtypes; i++) { + charlabel = read_string(fp); + typelabel[i] = charlabel; + delete [] charlabel; + } + + for (int i = 0; i < nbondtypes; i++) { + charlabel = read_string(fp); + btypelabel[i] = charlabel; + delete [] charlabel; + } + + for (int i = 0; i < nangletypes; i++) { + charlabel = read_string(fp); + atypelabel[i] = charlabel; + delete [] charlabel; + } + + for (int i = 0; i < ndihedraltypes; i++) { + charlabel = read_string(fp); + dtypelabel[i] = charlabel; + delete [] charlabel; + } + + for (int i = 0; i < nimpropertypes; i++) { + charlabel = read_string(fp); + itypelabel[i] = charlabel; + delete [] charlabel; + } +} + +/* ---------------------------------------------------------------------- + proc 0 writes to restart file +------------------------------------------------------------------------- */ + +void LabelMap::write_restart(FILE *fp) +{ + for (int i = 0; i < natomtypes; i++) + write_string(typelabel[i],fp); + + for (int i = 0; i < nbondtypes; i++) + write_string(btypelabel[i],fp); + + for (int i = 0; i < nangletypes; i++) + write_string(atypelabel[i],fp); + + for (int i = 0; i < ndihedraltypes; i++) + write_string(dtypelabel[i],fp); + + for (int i = 0; i < nimpropertypes; i++) + write_string(itypelabel[i],fp); +} + +/* ---------------------------------------------------------------------- + read a char string (including nullptr) and bcast it + str is allocated here, ptr is returned, caller must deallocate +------------------------------------------------------------------------- */ + +char *LabelMap::read_string(FILE *fp) +{ + int n = read_int(fp); + if (n < 0) error->all(FLERR,"Illegal size string or corrupt restart"); + char *value = new char[n]; + if (me == 0) utils::sfread(FLERR,value,sizeof(char),n,fp,nullptr,error); + MPI_Bcast(value,n,MPI_CHAR,0,world); + return value; +} + +/* ---------------------------------------------------------------------- + write a flag and a C-style char string (including the terminating null + byte) into the restart file +------------------------------------------------------------------------- */ + +void LabelMap::write_string(std::string str, FILE *fp) +{ + const char *cstr = str.c_str(); + int n = strlen(cstr) + 1; + fwrite(&n,sizeof(int),1,fp); + fwrite(cstr,sizeof(char),n,fp); +} + +/* ---------------------------------------------------------------------- + read an int from restart file and bcast it +------------------------------------------------------------------------- */ + +int LabelMap::read_int(FILE *fp) +{ + int value; + if ((me == 0) && (fread(&value,sizeof(int),1,fp) < 1)) + value = -1; + MPI_Bcast(&value,1,MPI_INT,0,world); + return value; +} diff --git a/src/label_map.h b/src/label_map.h index a01f238fc3..978923252e 100755 --- a/src/label_map.h +++ b/src/label_map.h @@ -43,13 +43,23 @@ class LabelMap : protected Pointers { void allocate_type_labels(); void modify_lmap(int, char **); - void merge_lmap(class LabelMap *, int); // copy another lmap into this one - void create_lmap2lmap(class LabelMap *, int); // index mapping between two lmaps + void merge_lmap(class LabelMap *, int); // copy another lmap into this one + void create_lmap2lmap(class LabelMap *, int); // index mapping between two lmaps int find_or_create(std::string, std::vector &, int); // look up type or create new type - int find(std::string, std::vector, int); // look up type index - void write_data(FILE *); + int find(std::string, std::vector, int); // look up type index - protected: + // input/output for atom class label map + + void write_data(FILE *); + void read_restart(FILE *fp); + void write_restart(FILE *); + + private: + int me; + + char *read_string(FILE *); + void write_string(std::string, FILE *); + int read_int(FILE *); }; } diff --git a/src/lmprestart.h b/src/lmprestart.h index ecdd272d81..4b542d946c 100644 --- a/src/lmprestart.h +++ b/src/lmprestart.h @@ -37,7 +37,7 @@ enum{VERSION,SMALLINT,TAGINT,BIGINT, COMM_MODE,COMM_CUTOFF,COMM_VEL,NO_PAIR, EXTRA_BOND_PER_ATOM,EXTRA_ANGLE_PER_ATOM,EXTRA_DIHEDRAL_PER_ATOM, EXTRA_IMPROPER_PER_ATOM,EXTRA_SPECIAL_PER_ATOM,ATOM_MAXSPECIAL, - NELLIPSOIDS,NLINES,NTRIS,NBODIES}; + NELLIPSOIDS,NLINES,NTRIS,NBODIES,LABELMAP}; #define LB_FACTOR 1.1 diff --git a/src/molecule.cpp b/src/molecule.cpp old mode 100644 new mode 100755 diff --git a/src/molecule.h b/src/molecule.h old mode 100644 new mode 100755 diff --git a/src/read_data.cpp b/src/read_data.cpp index a8581668aa..9734539492 100755 --- a/src/read_data.cpp +++ b/src/read_data.cpp @@ -441,7 +441,6 @@ void ReadData::command(int narg, char **arg) else n = static_cast (LB_FACTOR * atom->natoms / comm->nprocs); atom->allocate_type_arrays(); - atom->add_label_map(); atom->deallocate_topology(); // allocate atom arrays to N, rounded up by AtomVec->DELTA @@ -1975,7 +1974,7 @@ void ReadData::typelabels(std::vector &mytypelabel, int myntypes, i char *buf = new char[myntypes*MAXLINE]; labelflag = 1; - atom->labelmapflag = 1; + if (atom->labelmapflag == 0) atom->add_label_map(); int eof = comm->read_lines_from_file(fp,myntypes,MAXLINE,buf); if (eof) error->all(FLERR,"Unexpected end of data file"); diff --git a/src/read_restart.cpp b/src/read_restart.cpp index 9e8fbce91c..89994abab5 100644 --- a/src/read_restart.cpp +++ b/src/read_restart.cpp @@ -26,6 +26,7 @@ #include "group.h" #include "improper.h" #include "irregular.h" +#include "label_map.h" #include "memory.h" #include "modify.h" #include "mpiio.h" @@ -929,6 +930,11 @@ void ReadRestart::type_arrays() atom->set_mass(mass); delete [] mass; + } else if (flag == LABELMAP) { + read_int(); + atom->add_label_map(); + atom->lmap->read_restart(fp); + } else error->all(FLERR, "Invalid flag in type arrays section of restart file"); diff --git a/src/write_data.cpp b/src/write_data.cpp old mode 100644 new mode 100755 diff --git a/src/write_data.h b/src/write_data.h old mode 100644 new mode 100755 diff --git a/src/write_restart.cpp b/src/write_restart.cpp old mode 100644 new mode 100755 index afe1d2d528..f97cdcd966 --- a/src/write_restart.cpp +++ b/src/write_restart.cpp @@ -25,6 +25,7 @@ #include "force.h" #include "group.h" #include "improper.h" +#include "label_map.h" #include "memory.h" #include "modify.h" #include "mpiio.h" @@ -520,6 +521,10 @@ void WriteRestart::header() void WriteRestart::type_arrays() { if (atom->mass) write_double_vec(MASS,atom->ntypes,&atom->mass[1]); + if (atom->labelmapflag) { + write_int(LABELMAP,atom->labelmapflag); + atom->lmap->write_restart(fp); + } // -1 flag signals end of type arrays diff --git a/src/write_restart.h b/src/write_restart.h old mode 100644 new mode 100755