Don't store group if initial assignment failed

This commit is contained in:
Richard Berger
2021-04-20 14:24:07 -04:00
parent aba4dfc42e
commit fcf17a709e
2 changed files with 340 additions and 326 deletions

View File

@ -30,6 +30,7 @@
#include "region.h"
#include "tokenizer.h"
#include "variable.h"
#include "exceptions.h"
#include <cmath>
#include <cstring>
@ -152,12 +153,14 @@ void Group::assign(int narg, char **arg)
// add a new group if igroup = -1
int igroup = find(arg[0]);
bool created = false;
if (igroup == -1) {
if (ngroup == MAX_GROUP) error->all(FLERR,"Too many groups");
igroup = find_unused();
names[igroup] = utils::strdup(arg[0]);
ngroup++;
created = true;
}
double **x = atom->x;
@ -165,6 +168,7 @@ void Group::assign(int narg, char **arg)
int nlocal = atom->nlocal;
int bit = bitmask[igroup];
try {
// style = region
// add to group if atom is in region
@ -509,6 +513,16 @@ void Group::assign(int narg, char **arg)
} else error->all(FLERR,"Illegal group command");
} catch (LAMMPSException & e) {
// undo created group in case of an error
if (created) {
delete [] names[igroup];
names[igroup] = nullptr;
ngroup--;
}
throw e;
}
// print stats for changed group
int n;

View File

@ -297,7 +297,7 @@ TEST_F(GroupTest, Dynamic)
command("group grow delete");
command("variable ramp equal step");
END_HIDE_OUTPUT();
ASSERT_EQ(group->ngroup, 4);
ASSERT_EQ(group->ngroup, 3);
TEST_FAILURE(".*ERROR: Group dynamic cannot reference itself.*",
command("group half dynamic half region top"););