implement "labelmap write" command

This commit is contained in:
Axel Kohlmeyer
2022-09-03 19:52:07 -04:00
parent bb45137b1a
commit 7ecfb77e8d
3 changed files with 64 additions and 3 deletions

View File

@ -10,8 +10,14 @@ Syntax
labelmap option args
* *option* = *atom* or *bond* or *angle* or *dihedral* or *improper* or *clear*
* except for the *clear* option, one or more numeric-type/type-label pairs may be appended
* *option* = *atom* or *bond* or *angle* or *dihedral* or *improper* or *clear* or *write*
.. parsed-literal::
*clear* = no args
*write* arg = filename
*atom* or *bond* or *angle* or *dihedral* or *improper*
args = list of one or more numeric-type/type-label pairs
Examples
""""""""
@ -22,6 +28,7 @@ Examples
labelmap bond 1 carbonyl 2 nitrile
labelmap atom $(label(carbon)) C # change type label from 'carbon' to 'C'
labelmap clear
labelmap write mymap.include
Description
"""""""""""
@ -59,6 +66,11 @@ that when type labels are used, that there is a label defined for
The *clear* option resets the labelmap and thus discards all previous
settings.
The *write* option takes a filename as argument and writes the current
label mappings to a file as labelmap commands, so the file can be copied
into a different LAMMPS input file or read using the :doc:`include
<include>` command.
----------
Restrictions

View File

@ -123,9 +123,13 @@ void LabelMap::modify_lmap(int narg, char **arg)
labels = &itypelabel;
labels_map = &itypelabel_map;
} else if (tlabel == "clear") {
if (narg != 1) error->all(FLERR, "Incorrect number of arguments for labelmap command");
if (narg != 1) error->all(FLERR, "Incorrect number of arguments for labelmap clear command");
reset_type_labels();
return;
} else if (tlabel == "write") {
if (narg != 2) error->all(FLERR, "Incorrect number of arguments for labelmap write command");
write_map(arg[1]);
return;
} else
error->all(FLERR, "Unknown labelmap keyword {}", tlabel);
@ -441,3 +445,46 @@ int LabelMap::read_int(FILE *fp)
MPI_Bcast(&value, 1, MPI_INT, 0, world);
return value;
}
/* ----------------------------------------------------------------------
write out all current label map values as labelmap commands
------------------------------------------------------------------------- */
void LabelMap::write_map(const std::string &filename)
{
if (comm->me == 0) {
FILE *fp = fopen(filename.c_str(), "w");
if (!fp) error->one(FLERR, "Cannot open label map file {}: {}", filename, utils::getsyserror());
if (typelabel_map.size() > 0) {
fputs("labelmap atom", fp);
for (int i = 0; i < natomtypes; ++i)
if (!typelabel[i].empty()) fmt::print(fp, " {} \"\"\" {} \"\"\"", i + 1, typelabel[i]);
fputc('\n', fp);
}
if (btypelabel_map.size() > 0) {
fputs("labelmap bond", fp);
for (int i = 0; i < nbondtypes; ++i)
if (!btypelabel[i].empty()) fmt::print(fp, " {} \"\"\" {} \"\"\"", i + 1, btypelabel[i]);
fputc('\n', fp);
}
if (atypelabel_map.size() > 0) {
fputs("labelmap angle", fp);
for (int i = 0; i < nangletypes; ++i)
if (!atypelabel[i].empty()) fmt::print(fp, " {} \"\"\" {} \"\"\"", i + 1, atypelabel[i]);
fputc('\n', fp);
}
if (dtypelabel_map.size() > 0) {
fputs("labelmap dihedral", fp);
for (int i = 0; i < ndihedraltypes; ++i)
if (!dtypelabel[i].empty()) fmt::print(fp, " {} \"\"\" {} \"\"\"", i + 1, dtypelabel[i]);
fputc('\n', fp);
}
if (itypelabel_map.size() > 0) {
fputs("labelmap improper", fp);
for (int i = 0; i < nimpropertypes; ++i)
if (!itypelabel[i].empty()) fmt::print(fp, " {} \"\"\" {} \"\"\"", i + 1, itypelabel[i]);
fputc('\n', fp);
}
fclose(fp);
}
}

View File

@ -69,6 +69,8 @@ class LabelMap : protected Pointers {
char *read_string(FILE *);
void write_string(const std::string &, FILE *);
int read_int(FILE *);
void write_map(const std::string &);
};
} // namespace LAMMPS_NS