refactor into label_map class

This commit is contained in:
Jacob Gissinger
2020-12-16 23:07:58 -05:00
parent 40953643ec
commit bc32dfb480
5 changed files with 203 additions and 98 deletions

View File

@ -23,6 +23,7 @@
#include "force.h" #include "force.h"
#include "group.h" #include "group.h"
#include "input.h" #include "input.h"
#include "label_map.h"
#include "math_const.h" #include "math_const.h"
#include "memory.h" #include "memory.h"
#include "modify.h" #include "modify.h"
@ -83,8 +84,6 @@ Atom::Atom(LAMMPS *lmp) : Pointers(lmp)
ntypes = 0; ntypes = 0;
nellipsoids = nlines = ntris = nbodies = 0; nellipsoids = nlines = ntris = nbodies = 0;
nbondtypes = nangletypes = ndihedraltypes = nimpropertypes = 0; nbondtypes = nangletypes = ndihedraltypes = nimpropertypes = 0;
atomtypelabel = bondtypelabel = angletypelabel = NULL;
dihedraltypelabel = impropertypelabel = NULL;
nbonds = nangles = ndihedrals = nimpropers = 0; nbonds = nangles = ndihedrals = nimpropers = 0;
firstgroupname = nullptr; firstgroupname = nullptr;
@ -203,6 +202,10 @@ Atom::Atom(LAMMPS *lmp) : Pointers(lmp)
nmolecule = 0; nmolecule = 0;
molecules = nullptr; molecules = nullptr;
// type labels
lmap = nullptr;
// custom atom arrays // custom atom arrays
nivector = ndvector = 0; nivector = ndvector = 0;
@ -309,18 +312,8 @@ Atom::~Atom()
for (int i = 0; i < nmolecule; i++) delete molecules[i]; for (int i = 0; i < nmolecule; i++) delete molecules[i];
memory->sfree(molecules); memory->sfree(molecules);
// delete type labels // delete label map
delete lmap;
for (int i = 0; i < ntypes; i++) delete atomtypelabel[i];
memory->sfree(atomtypelabel);
for (int i = 0; i < nbondtypes; i++) delete bondtypelabel[i];
memory->sfree(bondtypelabel);
for (int i = 0; i < nangletypes; i++) delete angletypelabel[i];
memory->sfree(angletypelabel);
for (int i = 0; i < ndihedraltypes; i++) delete dihedraltypelabel[i];
memory->sfree(dihedraltypelabel);
for (int i = 0; i < nimpropertypes; i++) delete impropertypelabel[i];
memory->sfree(impropertypelabel);
// delete per-type arrays // delete per-type arrays
@ -1707,80 +1700,6 @@ void Atom::allocate_type_arrays()
} }
} }
/* ----------------------------------------------------------------------
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 Atom::allocate_type_labels()
{
char *char_type = new char[256];
atomtypelabel = (char **) memory->srealloc(atomtypelabel,
ntypes*sizeof(char *),"atom:atomtypelabel");
for (int i = 0; i < ntypes; i++) {
sprintf(char_type,"%d",i+1);
int n = strlen(char_type) + 1;
atom->atomtypelabel[i] = new char[n];
strcpy(atom->atomtypelabel[i],char_type);
}
if (force->bond) {
bondtypelabel = (char **) memory->srealloc(bondtypelabel,
nbondtypes*sizeof(char *),"atom:bondtypelabel");
for (int i = 0; i < nbondtypes; i++) {
sprintf(char_type,"%d",i+1);
int n = strlen(char_type) + 1;
atom->bondtypelabel[i] = new char[n];
strcpy(atom->bondtypelabel[i],char_type);
}
}
if (force->angle) {
angletypelabel = (char **) memory->srealloc(angletypelabel,
nangletypes*sizeof(char *),"atom:angletypelabel");
for (int i = 0; i < nangletypes; i++) {
sprintf(char_type,"%d",i+1);
int n = strlen(char_type) + 1;
atom->angletypelabel[i] = new char[n];
strcpy(atom->angletypelabel[i],char_type);
}
}
if (force->dihedral) {
dihedraltypelabel = (char **) memory->srealloc(dihedraltypelabel,
ndihedraltypes*sizeof(char *),"atom:dihedraltypelabel");
for (int i = 0; i < ndihedraltypes; i++) {
sprintf(char_type,"%d",i+1);
int n = strlen(char_type) + 1;
atom->dihedraltypelabel[i] = new char[n];
strcpy(atom->dihedraltypelabel[i],char_type);
}
}
if (force->improper) {
impropertypelabel = (char **) memory->srealloc(impropertypelabel,
nimpropertypes*sizeof(char *),"atom:impropertypelabel");
for (int i = 0; i < nimpropertypes; i++) {
sprintf(char_type,"%d",i+1);
int n = strlen(char_type) + 1;
atom->impropertypelabel[i] = new char[n];
strcpy(atom->impropertypelabel[i],char_type);
}
}
delete [] char_type;
}
/* ----------------------------------------------------------------------
find integer type given a type label
return -1 if type not yet defined
------------------------------------------------------------------------- */
int Atom::find_type(char *typelabel, char **typelabelarray, int num_types)
{
for (int i = 0; i < num_types; i++) {
if (typelabelarray[i] && strcmp(typelabel,typelabelarray[i]) == 0) return i+1;
}
return -1;
}
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------
set a mass and flag it as set set a mass and flag it as set
called from reading of data file called from reading of data file
@ -2056,6 +1975,21 @@ void Atom::add_molecule_atom(Molecule *onemol, int iatom,
} }
} }
/* ----------------------------------------------------------------------
allocate space for type label map
------------------------------------------------------------------------- */
void Atom::add_label_map()
{
lmap = new LabelMap(lmp);
lmap->natomtypes = ntypes;
lmap->nbondtypes = nbondtypes;
lmap->nangletypes = nangletypes;
lmap->ndihedraltypes = ndihedraltypes;
lmap->nimpropertypes = nimpropertypes;
lmap->allocate_type_labels();
}
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------
reorder owned atoms so those in firstgroup appear first reorder owned atoms so those in firstgroup appear first
called by comm->exchange() if atom_modify first group is set called by comm->exchange() if atom_modify first group is set

View File

@ -52,8 +52,6 @@ class Atom : protected Pointers {
bigint nbonds,nangles,ndihedrals,nimpropers; bigint nbonds,nangles,ndihedrals,nimpropers;
int ntypes,nbondtypes,nangletypes,ndihedraltypes,nimpropertypes; int ntypes,nbondtypes,nangletypes,ndihedraltypes,nimpropertypes;
char **atomtypelabel,**bondtypelabel,**angletypelabel;
char **dihedraltypelabel,**impropertypelabel;
int bond_per_atom,angle_per_atom,dihedral_per_atom,improper_per_atom; int bond_per_atom,angle_per_atom,dihedral_per_atom,improper_per_atom;
int extra_bond_per_atom,extra_angle_per_atom; int extra_bond_per_atom,extra_angle_per_atom;
int extra_dihedral_per_atom,extra_improper_per_atom; int extra_dihedral_per_atom,extra_improper_per_atom;
@ -230,6 +228,10 @@ class Atom : protected Pointers {
int nmolecule; int nmolecule;
class Molecule **molecules; class Molecule **molecules;
// type labels
class LabelMap *lmap;
// extra peratom info in restart file destined for fix & diag // extra peratom info in restart file destined for fix & diag
double **extra; double **extra;
@ -310,8 +312,6 @@ class Atom : protected Pointers {
void data_fix_compute_variable(int, int); void data_fix_compute_variable(int, int);
virtual void allocate_type_arrays(); virtual void allocate_type_arrays();
void allocate_type_labels();
int find_type(char *, char **, int);
void set_mass(const char *, int, const char *, int); void set_mass(const char *, int, const char *, int);
void set_mass(const char *, int, int, double); void set_mass(const char *, int, int, double);
void set_mass(const char *, int, int, char **); void set_mass(const char *, int, int, char **);
@ -325,6 +325,8 @@ class Atom : protected Pointers {
int find_molecule(char *); int find_molecule(char *);
void add_molecule_atom(class Molecule *, int, int, tagint); void add_molecule_atom(class Molecule *, int, int, tagint);
void add_label_map();
void first_reorder(); void first_reorder();
virtual void sort(); virtual void sort();

124
src/label_map.cpp Normal file
View File

@ -0,0 +1,124 @@
/* ----------------------------------------------------------------------
LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
https://lammps.sandia.gov/, Sandia National Laboratories
Steve Plimpton, sjplimp@sandia.gov
Copyright (2003) Sandia Corporation. Under the terms of Contract
DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains
certain rights in this software. This software is distributed under
the GNU General Public License.
See the README file in the top-level LAMMPS directory.
------------------------------------------------------------------------- */
#include "label_map.h"
#include "force.h"
#include "memory.h"
#include <cstring>
using namespace LAMMPS_NS;
/* ---------------------------------------------------------------------- */
LabelMap::LabelMap(LAMMPS *lmp) : Pointers(lmp)
{
natomtypes = nbondtypes = nangletypes = 0;
ndihedraltypes = nimpropertypes = 0;
typelabel = btypelabel = atypelabel = NULL;
dtypelabel = itypelabel = NULL;
}
/* ---------------------------------------------------------------------- */
LabelMap::~LabelMap()
{
// delete type labels
for (int i = 0; i < natomtypes; i++) delete typelabel[i];
memory->sfree(typelabel);
for (int i = 0; i < nbondtypes; i++) delete btypelabel[i];
memory->sfree(btypelabel);
for (int i = 0; i < nangletypes; i++) delete atypelabel[i];
memory->sfree(atypelabel);
for (int i = 0; i < ndihedraltypes; i++) delete dtypelabel[i];
memory->sfree(dtypelabel);
for (int i = 0; i < nimpropertypes; i++) delete itypelabel[i];
memory->sfree(itypelabel);
}
/* ----------------------------------------------------------------------
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()
{
char *char_type = new char[256];
typelabel = (char **) memory->srealloc(typelabel,
natomtypes*sizeof(char *),"atom:typelabel");
for (int i = 0; i < natomtypes; i++) {
sprintf(char_type,"%d",i+1);
int n = strlen(char_type) + 1;
typelabel[i] = new char[n];
strcpy(typelabel[i],char_type);
}
if (force->bond) {
btypelabel = (char **) memory->srealloc(btypelabel,
nbondtypes*sizeof(char *),"atom:btypelabel");
for (int i = 0; i < nbondtypes; i++) {
sprintf(char_type,"%d",i+1);
int n = strlen(char_type) + 1;
btypelabel[i] = new char[n];
strcpy(btypelabel[i],char_type);
}
}
if (force->angle) {
atypelabel = (char **) memory->srealloc(atypelabel,
nangletypes*sizeof(char *),"atom:atypelabel");
for (int i = 0; i < nangletypes; i++) {
sprintf(char_type,"%d",i+1);
int n = strlen(char_type) + 1;
atypelabel[i] = new char[n];
strcpy(atypelabel[i],char_type);
}
}
if (force->dihedral) {
dtypelabel = (char **) memory->srealloc(dtypelabel,
ndihedraltypes*sizeof(char *),"atom:dtypelabel");
for (int i = 0; i < ndihedraltypes; i++) {
sprintf(char_type,"%d",i+1);
int n = strlen(char_type) + 1;
dtypelabel[i] = new char[n];
strcpy(dtypelabel[i],char_type);
}
}
if (force->improper) {
itypelabel = (char **) memory->srealloc(itypelabel,
nimpropertypes*sizeof(char *),"atom:itypelabel");
for (int i = 0; i < nimpropertypes; i++) {
sprintf(char_type,"%d",i+1);
int n = strlen(char_type) + 1;
itypelabel[i] = new char[n];
strcpy(itypelabel[i],char_type);
}
}
delete [] char_type;
}
/* ----------------------------------------------------------------------
find integer type given a type label
return -1 if type not yet defined
------------------------------------------------------------------------- */
int LabelMap::find_type(char *mytypelabel, char **typelabelarray, int num_types)
{
for (int i = 0; i < num_types; i++) {
if (typelabelarray[i] && strcmp(mytypelabel,typelabelarray[i]) == 0) return i+1;
}
return -1;
}

44
src/label_map.h Normal file
View File

@ -0,0 +1,44 @@
/* -*- c++ -*- ----------------------------------------------------------
LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
http://lammps.sandia.gov, Sandia National Laboratories
Steve Plimpton, sjplimp@sandia.gov
Copyright (2003) Sandia Corporation. Under the terms of Contract
DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains
certain rights in this software. This software is distributed under
the GNU General Public License.
See the README file in the top-level LAMMPS directory.
------------------------------------------------------------------------- */
#ifndef LMP_LABEL_MAP_H
#define LMP_LABEL_MAP_H
#include "pointers.h" // IWYU pragma: export
namespace LAMMPS_NS {
class LabelMap : protected Pointers {
public:
int natomtypes,nbondtypes,nangletypes;
int ndihedraltypes,nimpropertypes;
char **typelabel,**btypelabel,**atypelabel;
char **dtypelabel,**itypelabel;
LabelMap(LAMMPS *lmp);
~LabelMap();
void allocate_type_labels();
int find_type(char *, char **, int);
protected:
};
}
#endif
/* ERROR/WARNING messages:
*/

View File

@ -33,6 +33,7 @@
#include "group.h" #include "group.h"
#include "improper.h" #include "improper.h"
#include "irregular.h" #include "irregular.h"
#include "label_map.h"
#include "memory.h" #include "memory.h"
#include "modify.h" #include "modify.h"
#include "molecule.h" #include "molecule.h"
@ -440,7 +441,7 @@ void ReadData::command(int narg, char **arg)
else n = static_cast<int> (LB_FACTOR * atom->natoms / comm->nprocs); else n = static_cast<int> (LB_FACTOR * atom->natoms / comm->nprocs);
atom->allocate_type_arrays(); atom->allocate_type_arrays();
atom->allocate_type_labels(); atom->add_label_map();
atom->deallocate_topology(); atom->deallocate_topology();
// allocate atom arrays to N, rounded up by AtomVec->DELTA // allocate atom arrays to N, rounded up by AtomVec->DELTA
@ -714,27 +715,27 @@ void ReadData::command(int narg, char **arg)
else skip_lines(nimpropertypes); else skip_lines(nimpropertypes);
} else if (strcmp(keyword,"Atom Type Labels") == 0) { } else if (strcmp(keyword,"Atom Type Labels") == 0) {
if (firstpass) typelabels(atom->atomtypelabel,ntypes); if (firstpass) typelabels(atom->lmap->typelabel,ntypes);
else skip_lines(ntypes); else skip_lines(ntypes);
} else if (strcmp(keyword,"Bond Type Labels") == 0) { } else if (strcmp(keyword,"Bond Type Labels") == 0) {
if (nbondtypes) if (nbondtypes)
if (firstpass) typelabels(atom->bondtypelabel,nbondtypes); if (firstpass) typelabels(atom->lmap->btypelabel,nbondtypes);
else skip_lines(nbondtypes); else skip_lines(nbondtypes);
} else if (strcmp(keyword,"Angle Type Labels") == 0) { } else if (strcmp(keyword,"Angle Type Labels") == 0) {
if (nangletypes) if (nangletypes)
if (firstpass) typelabels(atom->angletypelabel,nangletypes); if (firstpass) typelabels(atom->lmap->atypelabel,nangletypes);
else skip_lines(nangletypes); else skip_lines(nangletypes);
} else if (strcmp(keyword,"Dihedral Type Labels") == 0) { } else if (strcmp(keyword,"Dihedral Type Labels") == 0) {
if (ndihedraltypes) if (ndihedraltypes)
if (firstpass) typelabels(atom->dihedraltypelabel,ndihedraltypes); if (firstpass) typelabels(atom->lmap->dtypelabel,ndihedraltypes);
else skip_lines(ndihedraltypes); else skip_lines(ndihedraltypes);
} else if (strcmp(keyword,"Improper Type Labels") == 0) { } else if (strcmp(keyword,"Improper Type Labels") == 0) {
if (nimpropertypes) if (nimpropertypes)
if (firstpass) typelabels(atom->impropertypelabel,nimpropertypes); if (firstpass) typelabels(atom->lmap->itypelabel,nimpropertypes);
else skip_lines(nimpropertypes); else skip_lines(nimpropertypes);
} else error->all(FLERR,fmt::format("Unknown identifier in data file: {}", } else error->all(FLERR,fmt::format("Unknown identifier in data file: {}",