Skip to content

Commit

Permalink
Range checking for vector parameters (idaholab#3988)
Browse files Browse the repository at this point in the history
  • Loading branch information
dschwen committed Oct 3, 2014
1 parent 5c3112f commit d978bf3
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 1 deletion.
99 changes: 99 additions & 0 deletions framework/include/utils/InputParameters.h
Expand Up @@ -91,6 +91,9 @@ class InputParameters : public Parameters
*/
template <typename T, typename UP_T> void rangeCheck(const std::string & full_name, const std::string & short_name,
InputParameters::Parameter<T> * param, std::ostream & oss=Moose::out);
template <typename T, typename UP_T> void rangeCheck(const std::string & full_name, const std::string & short_name,
InputParameters::Parameter<std::vector<T> > * param,
std::ostream & oss=Moose::out);

/**
* Verifies that the requested parameter exists and is not NULL and returns it to the caller.
Expand Down Expand Up @@ -525,6 +528,102 @@ InputParameters::set (const std::string& name)
return cast_ptr<Parameter<T>*>(_values[name])->set();
}

template <typename T, typename UP_T>
void
InputParameters::rangeCheck(const std::string & full_name, const std::string & short_name, InputParameters::Parameter<std::vector<T> > * param, std::ostream & oss)
{
mooseAssert(param, "Parameter is NULL");

if (_range_functions.find(short_name) == _range_functions.end())
return;

/**
* Automatically detect teh variables used in the range checking expression.
* We allow the following variables (where snam is the short_name of the parameter)
*
* snam : tests every component in the vector
* 'snam > 0'
* snam_size : the size of the vector
* 'snam_size = 5'
* snam_i : where i is a number from 0 to sname_size-1 tests a specific component
* 'snam_0 > snam_1'
*/
FunctionParserBase<UP_T> fp;
std::vector<std::string> vars;
if (fp.ParseAndDeduceVariables(_range_functions[short_name], vars) != -1) // -1 for success
{
oss << "Error parsing expression: " << _range_functions[short_name] << '\n';
return;
}

// Fparser parameter buffer
std::vector<UP_T> parbuf(vars.size());

// parameter vector
const std::vector<T> & value = param->set();

// iterate over all vector values (maybe ;)
bool need_to_iterate = false;
for (unsigned int i = 0; i < value.size(); i++)
{
// set parameters
for (unsigned int j = 0; j < vars.size(); j++)
{
if (vars[j] == short_name)
{
parbuf[j] = value[i];
need_to_iterate = true;
}
else if (vars[j] == short_name + "_size")
parbuf[j] = value.size();
else
{
std::size_t found = vars[j].find_last_of('_');
if (found==std::string::npos)
{
oss << "Error parsing expression: " << _range_functions[short_name] << '\n';
return;
}
std::istringstream iss(vars[j]);
iss.seekg(found+1);

int index;
if (iss >> index)
{
if (index < 0 || index >= value.size())
{
oss << "Error parsing expression: " << _range_functions[short_name] << "\nOut of range variable " << vars[j] << '\n';
return;
}
parbuf[j] = value[index];
}
else
{
oss << "Error parsing expression: " << _range_functions[short_name] << "\nInvalid variable " << vars[j] << ' ' << found << '\n';
return;
}
}
}

// test function using the parameters determined above
UP_T result = fp.Eval(&parbuf[0]);
if (fp.EvalError())
{
oss << "Error evaluating expression: " << _range_functions[short_name] << '\n';
return;
}

if (!result)
{
oss << "Range check failed for parameter " << full_name << "\n\tExpression: " << _range_functions[short_name] << "\n";
if (need_to_iterate)
oss << "\t Component: " << i << '\n';
}

if (!need_to_iterate) break;
}
}

template <typename T, typename UP_T>
void
InputParameters::rangeCheck(const std::string & full_name, const std::string & short_name, InputParameters::Parameter<T> * param, std::ostream & oss)
Expand Down
4 changes: 3 additions & 1 deletion framework/src/utils/InputParameters.C
Expand Up @@ -272,9 +272,11 @@ InputParameters::mooseObjectSyntaxVisibility() const
InputParameters::Parameter<type> * scalar_p = dynamic_cast<InputParameters::Parameter<type>*>(param); \
if (scalar_p) \
rangeCheck<type, up_type>(long_name, short_name, scalar_p, oss); \
InputParameters::Parameter<std::vector<type> > * vector_p = dynamic_cast<InputParameters::Parameter<std::vector<type> >*>(param); \
if (vector_p) \
rangeCheck<type, up_type>(long_name, short_name, vector_p, oss); \
} while (0)


void
InputParameters::checkParams(const std::string &prefix)
{
Expand Down

0 comments on commit d978bf3

Please sign in to comment.