Merge pull request #3400 from vpalkar/unit-tests

Adding unit tests and improving errors in region commands
This commit is contained in:
Axel Kohlmeyer
2022-08-17 13:19:27 -04:00
committed by GitHub
12 changed files with 162 additions and 43 deletions

View File

@ -302,7 +302,7 @@ void Region::rotate(double &x, double &y, double &z, double angle)
void Region::options(int narg, char **arg)
{
if (narg < 0) error->all(FLERR, "Illegal region command");
if (narg < 0) utils::missing_cmd_args(FLERR, "region", error);
// option defaults
@ -316,46 +316,46 @@ void Region::options(int narg, char **arg)
int iarg = 0;
while (iarg < narg) {
if (strcmp(arg[iarg], "units") == 0) {
if (iarg + 2 > narg) error->all(FLERR, "Illegal region command");
if (iarg + 2 > narg) utils::missing_cmd_args(FLERR, "region units", error);
if (strcmp(arg[iarg + 1], "box") == 0)
scaleflag = 0;
else if (strcmp(arg[iarg + 1], "lattice") == 0)
scaleflag = 1;
else
error->all(FLERR, "Illegal region command");
error->all(FLERR, "Illegal region units: {}", arg[iarg+1]);
iarg += 2;
} else if (strcmp(arg[iarg], "side") == 0) {
if (iarg + 2 > narg) error->all(FLERR, "Illegal region command");
if (iarg + 2 > narg) utils::missing_cmd_args(FLERR, "region side", error);
if (strcmp(arg[iarg + 1], "in") == 0)
interior = 1;
else if (strcmp(arg[iarg + 1], "out") == 0)
interior = 0;
else
error->all(FLERR, "Illegal region command");
error->all(FLERR, "Illegal region side: {}", arg[iarg+1]);
iarg += 2;
} else if (strcmp(arg[iarg], "move") == 0) {
if (iarg + 4 > narg) error->all(FLERR, "Illegal region command");
if (iarg + 4 > narg) utils::missing_cmd_args(FLERR, "region move", error);
if (strcmp(arg[iarg + 1], "NULL") != 0) {
if (strstr(arg[iarg + 1], "v_") != arg[iarg + 1])
error->all(FLERR, "Illegal region command");
error->all(FLERR, "Illegal region move x displacement variable: {}", arg[iarg+1]);
xstr = utils::strdup(&arg[iarg + 1][2]);
}
if (strcmp(arg[iarg + 2], "NULL") != 0) {
if (strstr(arg[iarg + 2], "v_") != arg[iarg + 2])
error->all(FLERR, "Illegal region command");
error->all(FLERR, "Illegal region move y displacement variable: {}", arg[iarg+2]);
ystr = utils::strdup(&arg[iarg + 2][2]);
}
if (strcmp(arg[iarg + 3], "NULL") != 0) {
if (strstr(arg[iarg + 3], "v_") != arg[iarg + 3])
error->all(FLERR, "Illegal region command");
error->all(FLERR, "Illegal region move z displacement variable: {}", arg[iarg+3]);
zstr = utils::strdup(&arg[iarg + 3][2]);
}
moveflag = 1;
iarg += 4;
} else if (strcmp(arg[iarg], "rotate") == 0) {
if (iarg + 8 > narg) error->all(FLERR, "Illegal region command");
if (iarg + 8 > narg) utils::missing_cmd_args(FLERR, "region rotate", error);
if (strstr(arg[iarg + 1], "v_") != arg[iarg + 1]) error->all(FLERR, "Illegal region command");
tstr = utils::strdup(&arg[iarg + 1][2]);
point[0] = utils::numeric(FLERR, arg[iarg + 2], false, lmp);
@ -368,15 +368,14 @@ void Region::options(int narg, char **arg)
iarg += 8;
} else if (strcmp(arg[iarg], "open") == 0) {
if (iarg + 2 > narg) error->all(FLERR, "Illegal region command");
if (iarg + 2 > narg) utils::missing_cmd_args(FLERR, "region open", error);
int iface = utils::inumeric(FLERR, arg[iarg + 1], false, lmp);
if (iface < 1 || iface > 6) error->all(FLERR, "Illegal region command");
if (iface < 1 || iface > 6) error->all(FLERR, "Illegal region open face index: {}", iface);
// additional checks on valid face index are done by region classes
open_faces[iface - 1] = 1;
openflag = 1;
iarg += 2;
} else
error->all(FLERR, "Illegal region command");
} else error->all(FLERR, "Illegal region command argument: {}", arg[iarg]);
}
// error check

View File

@ -80,8 +80,9 @@ RegBlock::RegBlock(LAMMPS *lmp, int narg, char **arg) : Region(lmp, narg, arg)
// error check
if (xlo > xhi || ylo > yhi || zlo > zhi)
error->all(FLERR,"Illegal region block command");
if (xlo > xhi) error->all(FLERR,"Illegal region block xlo: {} >= xhi: {}", xlo, xhi);
if (ylo > yhi) error->all(FLERR,"Illegal region block ylo: {} >= yhi: {}", ylo, yhi);
if (zlo > zhi) error->all(FLERR,"Illegal region block zlo: {} >= zhi: {}", zlo, zhi);
// extent of block

View File

@ -37,11 +37,12 @@ RegCone::RegCone(LAMMPS *lmp, int narg, char **arg) :
// check open face settings
if (openflag && (open_faces[3] || open_faces[4] || open_faces[5]))
error->all(FLERR,"Invalid region cone open setting");
if (openflag)
for (int i=3; i<6; i++)
if (open_faces[i]) error->all(FLERR,"Illegal region cone open face: {}", i+1);
if (strcmp(arg[2],"x") != 0 && strcmp(arg[2],"y") && strcmp(arg[2],"z") != 0)
error->all(FLERR,"Illegal region cylinder command");
if (strcmp(arg[2],"x") != 0 && strcmp(arg[2],"y") != 0 && strcmp(arg[2],"z") != 0)
error->all(FLERR,"Illegal region cone axis: {}", arg[2]);
axis = arg[2][0];
if (axis == 'x') {

View File

@ -37,11 +37,12 @@ RegCylinder::RegCylinder(LAMMPS *lmp, int narg, char **arg) :
// check open face settings
if (openflag && (open_faces[3] || open_faces[4] || open_faces[5]))
error->all(FLERR,"Invalid region cylinder open setting");
if (openflag)
for (int i=3; i<6; i++)
if (open_faces[i]) error->all(FLERR,"Illegal region cylinder open face: {}", i+1);
if (strcmp(arg[2],"x") != 0 && strcmp(arg[2],"y") && strcmp(arg[2],"z") != 0)
error->all(FLERR,"Illegal region cylinder command");
if (strcmp(arg[2],"x") != 0 && strcmp(arg[2],"y") != 0 && strcmp(arg[2],"z") != 0)
error->all(FLERR,"Illegal region cylinder axis: {}", arg[2]);
axis = arg[2][0];
if (axis == 'x') {
@ -170,7 +171,7 @@ RegCylinder::RegCylinder(LAMMPS *lmp, int narg, char **arg) :
// error check
if (radius <= 0.0) error->all(FLERR,"Illegal region cylinder command");
if (radius <= 0.0) error->all(FLERR,"Illegal radius {} in region cylinder command", radius);
// extent of cylinder
// for variable radius, uses initial radius

View File

@ -101,7 +101,9 @@ RegEllipsoid::RegEllipsoid(LAMMPS *lmp, int narg, char **arg) :
// error check
if (a < 0.0 || b < 0.0 || c < 0.0) error->all(FLERR, "Illegal region ellipsoid command");
if (a < 0.0) error->all(FLERR, "Illegal region ellipsoid a: {}", a);
if (b < 0.0) error->all(FLERR, "Illegal region ellipsoid b: {}", b);
if (c < 0.0) error->all(FLERR, "Illegal region ellipsoid c: {}", c);
// extent of ellipsoid
// for variable axes, uses initial axes and origin for variable center

View File

@ -27,9 +27,9 @@ RegIntersect::RegIntersect(LAMMPS *lmp, int narg, char **arg) :
{
nregion = 0;
if (narg < 5) error->all(FLERR, "Illegal region command");
if (narg < 5) utils::missing_cmd_args(FLERR, "region intersect", error);;
int n = utils::inumeric(FLERR, arg[2], false, lmp);
if (n < 2) error->all(FLERR, "Illegal region command");
if (n < 2) error->all(FLERR, "Illegal region intersect n: {}", n);
options(narg - (n + 3), &arg[n + 3]);
// build list of regions to intersect

View File

@ -37,7 +37,7 @@ RegPlane::RegPlane(LAMMPS *lmp, int narg, char **arg) :
// enforce unit normal
double rsq = normal[0]*normal[0] + normal[1]*normal[1] + normal[2]*normal[2];
if (rsq == 0.0) error->all(FLERR,"Illegal region plane command");
if (rsq == 0.0) error->all(FLERR,"Illegal region plane normal vector: {} {} {}", normal[0], normal[1], normal[2]);
normal[0] /= sqrt(rsq);
normal[1] /= sqrt(rsq);
normal[2] /= sqrt(rsq);

View File

@ -84,23 +84,24 @@ RegPrism::RegPrism(LAMMPS *lmp, int narg, char **arg) : Region(lmp, narg, arg)
// prism cannot be 0 thickness in any dim, else inverse blows up
// non-zero tilt values cannot be used if either dim is INF on both ends
if (xlo >= xhi || ylo >= yhi || zlo >= zhi)
error->all(FLERR,"Illegal region prism command");
if (xlo >= xhi) error->all(FLERR,"Illegal region prism xlo: {} >= xhi: {}", xlo, xhi);
if (ylo >= yhi) error->all(FLERR,"Illegal region prism ylo: {} >= yhi: {}", ylo, yhi);
if (zlo >= zhi) error->all(FLERR,"Illegal region prism zlo: {} >= zhi: {}", zlo ,zhi);
if (xy != 0.0 && xlo == -BIG && xhi == BIG)
error->all(FLERR,"Illegal region prism command");
error->all(FLERR,"Illegal region prism non-zero xy tilt with infinite x size");
if (xy != 0.0 && ylo == -BIG && yhi == BIG)
error->all(FLERR,"Illegal region prism command");
error->all(FLERR,"Illegal region prism non-zero xy tilt with infinite y size");
if (xz != 0.0 && xlo == -BIG && xhi == BIG)
error->all(FLERR,"Illegal region prism command");
error->all(FLERR,"Illegal region prism non-zero xz tilt with infinite x size");
if (xz != 0.0 && zlo == -BIG && zhi == BIG)
error->all(FLERR,"Illegal region prism command");
error->all(FLERR,"Illegal region prism non-zero xz tilt with infinite z size");
if (yz != 0.0 && ylo == -BIG && yhi == BIG)
error->all(FLERR,"Illegal region prism command");
error->all(FLERR,"Illegal region prism non-zero yz tilt with infinite y size");
if (yz != 0.0 && zlo == -BIG && zhi == BIG)
error->all(FLERR,"Illegal region prism command");
error->all(FLERR,"Illegal region prism non-zero yz tilt with infinite z size");
// extent of prism

View File

@ -79,7 +79,7 @@ RegSphere::RegSphere(LAMMPS *lmp, int narg, char **arg) :
// error check
if (radius < 0.0) error->all(FLERR,"Illegal region sphere command");
if (radius < 0.0) error->all(FLERR,"Illegal region sphere radius: {}", radius);
// extent of sphere
// for variable radius, uses initial radius and origin for variable center

View File

@ -28,9 +28,9 @@ RegUnion::RegUnion(LAMMPS *lmp, int narg, char **arg) : Region(lmp, narg, arg),
{
nregion = 0;
if (narg < 5) error->all(FLERR, "Illegal region command");
if (narg < 5) utils::missing_cmd_args(FLERR, "region union", error);;
int n = utils::inumeric(FLERR, arg[2], false, lmp);
if (n < 2) error->all(FLERR, "Illegal region command");
if (n < 2) error->all(FLERR, "Illegal region union n: {}", n);
options(narg - (n + 3), &arg[n + 3]);
// build list of region indices to union

View File

@ -77,9 +77,10 @@ TEST_F(RegionTest, NoBox)
command("region reg6 union 3 reg1 reg2 reg3");
command("region reg7 intersect 3 reg1 reg2 reg4");
command("region reg8 ellipsoid 0 0 0 2 1 2");
command("region reg9 cylinder y 0 0 1 0 1 open 1 units box");
END_HIDE_OUTPUT();
list = domain->get_region_list();
EXPECT_EQ(list.size(), 8);
EXPECT_EQ(list.size(), 9);
auto reg = domain->get_region_by_id("reg1");
EXPECT_EQ(reg->interior, 1);
@ -161,21 +162,50 @@ TEST_F(RegionTest, NoBox)
EXPECT_EQ(reg->rotateflag, 0);
EXPECT_EQ(reg->openflag, 0);
reg = domain->get_region_by_id("reg9");
EXPECT_EQ(reg->interior, 1);
EXPECT_EQ(reg->scaleflag, 0);
EXPECT_EQ(reg->bboxflag, 1);
EXPECT_EQ(reg->varshape, 0);
EXPECT_EQ(reg->dynamic, 0);
EXPECT_EQ(reg->moveflag, 0);
EXPECT_EQ(reg->rotateflag, 0);
EXPECT_EQ(reg->openflag, 1);
BEGIN_HIDE_OUTPUT();
command("region reg3 delete");
command("region reg5 delete");
command("region reg6 delete");
command("region reg1 delete");
command("region reg9 delete");
END_HIDE_OUTPUT();
list = domain->get_region_list();
EXPECT_EQ(list.size(), 4);
reg = domain->get_region_by_id("reg7");
TEST_FAILURE(".*ERROR: Region intersect region reg1 does not exist.*", reg->init(););
TEST_FAILURE(".*ERROR: Delete region reg3 does not exist.*", command("region reg3 delete"););
}
TEST_F(RegionTest, DeathTests)
{
atomic_system();
auto list = domain->get_region_list();
ASSERT_EQ(list.size(), 1);
TEST_FAILURE(".*ERROR: Illegal region block xlo: 1 >= xhi: 0.*", command("region reg1 block 1 0 0 1 0 1"););
TEST_FAILURE(".*ERROR: Illegal region cone open face: 4.*", command("region reg2 cone x 0 0 2 1 0 1 open 4"););
TEST_FAILURE(".*ERROR: Illegal region plane normal vector: 0 0 0.*", command("region reg3 plane 0 0 0 0 0 0 side out"););
TEST_FAILURE(".*ERROR: Illegal region prism non-zero xy tilt with infinite x size.*", command("region reg4 prism INF INF 0 1 0 1 0.1 0.2 0.3"););
TEST_FAILURE(".*ERROR: Illegal region sphere radius: -1.*", command("region reg5 sphere 0 0 0 -1"););
TEST_FAILURE(".*ERROR: Illegal region ellipsoid c: -2.*", command("region reg8 ellipsoid 0 0 0 2 1 -2"););
TEST_FAILURE(".*ERROR: Illegal region cylinder axis: xx.*", command("region reg9 cylinder xx 0 0 1 0 1 open 1 units box"););
TEST_FAILURE(".*ERROR: Unrecognized region style 'xxx'.*", command("region new1 xxx"););
TEST_FAILURE(".*ERROR: Illegal region command.*", command("region new1 block 0 1"););
TEST_FAILURE(".*ERROR: Delete region reg3 does not exist.*", command("region reg3 delete"););
//TEST_FAILURE(".*ERROR: Illegal region command.*", command("region new1 block 0 1"););
TEST_FAILURE(".*ERROR: Illegal region command: missing argument\\(s\\).*", command("region new1 block 0 1"););
TEST_FAILURE(".*ERROR: Delete region new3 does not exist.*", command("region new3 delete"););
}
TEST_F(RegionTest, Counts)

View File

@ -0,0 +1,84 @@
---
lammps_version: 3 Aug 2022
tags: generated
date_generated: Mon Aug 15 01:14:02 2022
epsilon: 4e-14
skip_tests:
prerequisites: ! |
atom full
fix wall/region
pre_commands: ! |
boundary f f f
post_commands: ! |
fix move all nve
region box block EDGE EDGE EDGE EDGE EDGE EDGE
fix test solute wall/region box harmonic 0.1 1.0 1.0
fix_modify test virial yes
input_file: in.fourmol
natoms: 29
run_stress: ! |2-
0.0000000000000000e+00 0.0000000000000000e+00 0.0000000000000000e+00 0.0000000000000000e+00 0.0000000000000000e+00 0.0000000000000000e+00
global_scalar: 0
global_vector: ! |-
3 0 0 0
run_pos: ! |2
1 -2.7045559775384026e-01 2.4912159905679729e+00 -1.6695851791541885e-01
2 3.1004029573899528e-01 2.9612354631094391e+00 -8.5466363037021464e-01
3 -7.0398551400789466e-01 1.2305509955830618e+00 -6.2777526944456274e-01
4 -1.5818159336499285e+00 1.4837407818929933e+00 -1.2538710836062004e+00
5 -9.0719763672789266e-01 9.2652103885675297e-01 3.9954210488374786e-01
6 2.4831720524855988e-01 2.8313021497871271e-01 -1.2314233331711453e+00
7 3.4143527641386412e-01 -2.2646551041391422e-02 -2.5292291414903052e+00
8 1.1743552229100009e+00 -4.8863228565853944e-01 -6.3783432910825522e-01
9 1.3800524229500313e+00 -2.5274721030406683e-01 2.8353985887095157e-01
10 2.0510765220543883e+00 -1.4604063740302866e+00 -9.8323745081712954e-01
11 1.7878031944442556e+00 -1.9921863272948861e+00 -1.8890602447625777e+00
12 3.0063007039340053e+00 -4.9013350496963298e-01 -1.6231898107386231e+00
13 4.0515402959192999e+00 -8.9202011606653986e-01 -1.6400005529924957e+00
14 2.6066963345543819e+00 -4.1789253965514156e-01 -2.6634003608794394e+00
15 2.9695287185712913e+00 5.5422613165234036e-01 -1.2342022021790127e+00
16 2.6747029695228521e+00 -2.4124119054564295e+00 -2.3435746150616152e-02
17 2.2153577785283796e+00 -2.0897985186907717e+00 1.1963150794479436e+00
18 2.1369701704094664e+00 3.0158507413593139e+00 -3.5179348337135590e+00
19 1.5355837135395243e+00 2.6255292354730009e+00 -4.2353987771401354e+00
20 2.7727573003748263e+00 3.6923910441179069e+00 -3.9330842453167185e+00
21 4.9040128073837339e+00 -4.0752348170758461e+00 -3.6210314709795299e+00
22 4.3582355554510048e+00 -4.2126119427061379e+00 -4.4612844196307497e+00
23 5.7439382849366911e+00 -3.5821957939240279e+00 -3.8766361295959513e+00
24 2.0689243582454213e+00 3.1513346907303501e+00 3.1550389751128463e+00
25 1.3045351331414130e+00 3.2665125705869009e+00 2.5111855257365274e+00
26 2.5809237402714267e+00 4.0117602605512728e+00 3.2212060528800821e+00
27 -1.9611343130357228e+00 -4.3563411931359752e+00 2.1098293115523705e+00
28 -2.7473562684513411e+00 -4.0200819932379330e+00 1.5830052163433954e+00
29 -1.3126000191359855e+00 -3.5962518039482929e+00 2.2746342468737835e+00
run_vel: ! |2
1 8.1705744183262832e-03 1.6516406176274298e-02 4.7902264318913212e-03
2 5.4501493445687828e-03 5.1791699408496412e-03 -1.4372931530376577e-03
3 -8.2298292722385591e-03 -1.2926551614621364e-02 -4.0984181178163734e-03
4 -3.7699042590093506e-03 -6.5722892098813894e-03 -1.1184640360133316e-03
5 -1.1021961004346575e-02 -9.8906780939336039e-03 -2.8410737829284390e-03
6 -3.9676663166400027e-02 4.6817061464710263e-02 3.7148491979476131e-02
7 9.1033953013898753e-04 -1.0128524411938794e-02 -5.1568251805019748e-02
8 7.9064712058855742e-03 -3.3507254552631585e-03 3.4557098492564643e-02
9 1.5644176117320938e-03 3.7365546102722208e-03 1.5047408822037651e-02
10 2.9201446820573192e-02 -2.9249578745486140e-02 -1.5018077424322544e-02
11 -4.7835961513517542e-03 -3.7481385134185211e-03 -2.3464104142290089e-03
12 2.2696451841920672e-03 -3.4774154398129641e-04 -3.0640770327796966e-03
13 2.7531740451953164e-03 5.8171061612840502e-03 -7.9467454022160669e-04
14 3.5246182371994205e-03 -5.7939995585585538e-03 -3.9478431172751361e-03
15 -1.8547943640122950e-03 -5.8554729942777778e-03 6.2938485140538675e-03
16 1.8681499973445252e-02 -1.3262466204585332e-02 -4.5638651457003250e-02
17 -1.2896269981100378e-02 9.7527665265956451e-03 3.7296535360836762e-02
18 -8.0065795274987550e-04 -8.6270473974390637e-04 -1.4483040536385791e-03
19 1.2452390067376827e-03 -2.5061097800836321e-03 7.2998639311871857e-03
20 3.5930058460518109e-03 3.6938852051849871e-03 3.2322738480194727e-03
21 -1.4689219756961610e-03 -2.7352107824530291e-04 7.0581625180892197e-04
22 -7.0694199165145105e-03 -4.2577148692717545e-03 2.8079117911323598e-04
23 6.0446963236685230e-03 -1.4000131545098772e-03 2.5819754799379716e-03
24 3.1926368451268083e-04 -9.9445664487428820e-04 1.4999960207062409e-04
25 1.3789752933078488e-04 -4.4335894831520756e-03 -8.1808138106080120e-04
26 2.0485904023409989e-03 2.7813358660936129e-03 4.3245726853349256e-03
27 4.5604120293369840e-04 -1.0305523026921111e-03 2.1188058381358413e-04
28 -6.2544520861855151e-03 1.4127711176146879e-03 -1.8429821884794260e-03
29 6.4110631534402174e-04 3.1273432719593824e-03 3.7253671105656736e-03
...