new utility function "utils::expand_type" to convert type labels to numeric strings

This commit is contained in:
Axel Kohlmeyer
2022-09-05 05:45:31 -04:00
parent e1960bbb9a
commit 75e897b2fe
5 changed files with 91 additions and 5 deletions

View File

@ -20,6 +20,7 @@
#include "fix.h"
#include "fmt/chrono.h"
#include "input.h"
#include "label_map.h"
#include "memory.h"
#include "modify.h"
#include "text_file_reader.h"
@ -766,6 +767,26 @@ int utils::expand_args(const char *file, int line, int narg, char **arg, int mod
return newarg;
}
/* -------------------------------------------------------------------------
Expand type string to numeric string from labelmap.
Return copy of expanded type or null pointer.
------------------------------------------------------------------------- */
char *utils::expand_type(const char *file, int line, const std::string &str, int mode, LAMMPS *lmp)
{
if (!lmp) return nullptr;
if (is_type(str) == 1) {
if (!lmp->atom->labelmapflag)
lmp->error->all(file, line, "Type string {} cannot be used without a labelmap", str);
int type = lmp->atom->lmap->find(str, mode);
if (type == -1) lmp->error->all(file, line, "Type string {} not found in labelmap", str);
return utils::strdup(std::to_string(type));
} else
return nullptr;
}
/* ----------------------------------------------------------------------
Make copy of string in new storage. Works like the (non-portable)
C-style strdup() but also accepts a C++ string as argument.