diff --git a/src/variable.cpp b/src/variable.cpp index a564847b68..979b0e4c64 100644 --- a/src/variable.cpp +++ b/src/variable.cpp @@ -4354,9 +4354,13 @@ int Variable::special_function(char *word, char *contents, Tree **tree, Tree **t } else argstack[nargstack++] = value; } else if (strcmp(word,"extract_setting") == 0) { - if (narg != 1) print_var_error(FLERR,"Invalid extract_setting() function in variable formula",ivar); + if (narg != 1) print_var_error(FLERR,"Invalid extract_setting() function syntax in variable formula",ivar); value = lammps_extract_setting(lmp, args[0]); + if (value < 0) { + auto mesg = fmt::format("Unknown setting {} for extract_setting() function in variable formula",args[0]); + print_var_error(FLERR,mesg,ivar); + } // save value in tree or on argstack diff --git a/unittest/commands/test_variables.cpp b/unittest/commands/test_variables.cpp index cf124eeafd..60a9f44095 100644 --- a/unittest/commands/test_variables.cpp +++ b/unittest/commands/test_variables.cpp @@ -380,6 +380,7 @@ TEST_F(VariableTest, Functions) command("variable ten1 equal tan(v_eight/2.0)"); command("variable ten2 equal asin(-1.0)+acos(0.0)"); command("variable ten3 equal floor(100*random(0.2,0.8,v_seed)+1)"); + command("variable ten4 equal extract_setting(world_size)"); END_HIDE_OUTPUT(); ASSERT_GT(variable->compute_equal(variable->find("two")), 0.99); @@ -393,9 +394,18 @@ TEST_F(VariableTest, Functions) ASSERT_FLOAT_EQ(variable->compute_equal(variable->find("ten1")), 1); ASSERT_GT(variable->compute_equal(variable->find("ten3")), 19); ASSERT_LT(variable->compute_equal(variable->find("ten3")), 81); + ASSERT_DOUBLE_EQ(variable->compute_equal(variable->find("ten4")), 1); TEST_FAILURE(".*ERROR: Variable four: Invalid syntax in variable formula.*", command("print \"${four}\"");); + TEST_FAILURE(".*ERROR on proc 0: Invalid immediate variable.*", + command("print \"$(extract_setting()\"");); + TEST_FAILURE(".*ERROR on proc 0: Invalid immediate variable.*", + command("print \"$(extract_setting()\"");); + TEST_FAILURE(".*ERROR: Invalid extract_setting.. function syntax in variable formula.*", + command("print \"$(extract_setting(one,two))\"");); + TEST_FAILURE(".*ERROR: Unknown setting nprocs for extract_setting.. function in variable formula.*", + command("print \"$(extract_setting(nprocs))\"");); } TEST_F(VariableTest, IfCommand)