Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding parsing for double indexed/separated fields #6459

Merged
merged 1 commit into from Mar 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions framework/include/actions/GlobalParamsAction.h
Expand Up @@ -50,5 +50,13 @@ class GlobalParamsAction: public Action
{
return parameters().set<std::vector<T> >(name);
}

template <typename T>
inline
std::vector<std::vector<T> > & setDoubleIndexParam(const std::string &name)
{
return parameters().set<std::vector<std::vector<T> > >(name);
}

};
#endif //GLOBALPARAMSACTION_H
5 changes: 5 additions & 0 deletions framework/include/parser/Parser.h
Expand Up @@ -137,6 +137,11 @@ class Parser : public ConsoleStreamInterface
void setVectorParameter(const std::string & full_name, const std::string & short_name,
InputParameters::Parameter<std::vector<T> >* param, bool in_global, GlobalParamsAction * global_block);

/// Template method for setting any double indexed type parameter read from the input file or command line
template<typename T>
void setDoubleIndexParameter(const std::string & full_name, const std::string & short_name,
InputParameters::Parameter<std::vector<std::vector<T> > >* param, bool in_global, GlobalParamsAction * global_block);

/// Template method for setting any multivalue "scalar" type parameter read from the input file or command line. Examples include "Point" and "RealVectorValue"
template<typename T>
void setScalarComponentParameter(const std::string & full_name, const std::string & short_name,
Expand Down
22 changes: 22 additions & 0 deletions framework/include/utils/MooseUtils.h
Expand Up @@ -276,6 +276,28 @@ namespace MooseUtils
pos = str.find_first_of(delims, std::min(last_pos + min_len, str.size()));
}
}

/**
* tokenizeAndConvert splits a string using delimiter and then converts to type T.
* If the conversion fails tokenizeAndConvert returns false, otherwise true.
*/
template <typename T>
bool
tokenizeAndConvert(const std::string & str, std::vector<T> & tokenized_vector, const std::string & delimiter = " \t\n\v\f\r")
{
std::vector<std::string> tokens;
MooseUtils::tokenize(str, tokens, 1, delimiter);
tokenized_vector.resize(tokens.size());
for (unsigned int j = 0; j < tokens.size(); ++j)
{
std::stringstream ss(tokens[j]);
// we have to make sure that the conversion succeeded _and_ that the string
// was fully read to avoid situations like [conversion to Real] 3.0abc to work
if ((ss >> tokenized_vector[j]).fail() || !ss.eof())
return false;
}
return true;
}
}

#endif //MOOSEUTILS_H
87 changes: 87 additions & 0 deletions framework/src/parser/Parser.C
Expand Up @@ -649,6 +649,9 @@ template<>
void Parser::setVectorParameter<VariableName>(const std::string & full_name, const std::string & short_name,
InputParameters::Parameter<std::vector<VariableName> > * param, bool in_global, GlobalParamsAction * global_block);

template<>
void Parser::setDoubleIndexParameter<VariableName>(const std::string & full_name, const std::string & short_name,
InputParameters::Parameter<std::vector<std::vector<VariableName> > >* param, bool /*in_global*/, GlobalParamsAction * /*global_block*/);

// Macros for parameter extraction
#define dynamicCastAndExtractScalar(type, param, full_name, short_name, in_global, global_block) \
Expand All @@ -675,6 +678,14 @@ void Parser::setVectorParameter<VariableName>(const std::string & full_name, con
setVectorParameter<type>(full_name, short_name, vector_p, in_global, global_block); \
} while (0)

#define dynamicCastAndExtractDoubleIndex(type, param, full_name, short_name, in_global, global_block) \
do \
{ \
InputParameters::Parameter<std::vector<std::vector<type> > > * double_index_p = dynamic_cast<InputParameters::Parameter<std::vector<std::vector<type> > > *>(param); \
if (double_index_p) \
setDoubleIndexParameter<type>(full_name, short_name, double_index_p, in_global, global_block); \
} while (0)

void
Parser::extractParams(const std::string & prefix, InputParameters &p)
{
Expand Down Expand Up @@ -834,6 +845,41 @@ Parser::extractParams(const std::string & prefix, InputParameters &p)
dynamicCastAndExtractVector(OutputName , it->second, full_name, it->first, in_global, global_params_block);
dynamicCastAndExtractVector(MaterialPropertyName , it->second, full_name, it->first, in_global, global_params_block);
dynamicCastAndExtractVector(DiscreteMaterialName , it->second, full_name, it->first, in_global, global_params_block);

/**
* Double indexed types
*/
// built-ins
dynamicCastAndExtractDoubleIndex(Real , it->second, full_name, it->first, in_global, global_params_block);
dynamicCastAndExtractDoubleIndex(int , it->second, full_name, it->first, in_global, global_params_block);
dynamicCastAndExtractDoubleIndex(long , it->second, full_name, it->first, in_global, global_params_block);
dynamicCastAndExtractDoubleIndex(unsigned int , it->second, full_name, it->first, in_global, global_params_block);
// See vector type explanation
#if LIBMESH_DOF_ID_BYTES == 8
dynamicCastAndExtractDoubleIndex(uint64_t , it->second, full_name, it->first, in_global, global_params_block);
#endif

dynamicCastAndExtractDoubleIndex(SubdomainID , it->second, full_name, it->first, in_global, global_params_block);
dynamicCastAndExtractDoubleIndex(BoundaryID , it->second, full_name, it->first, in_global, global_params_block);

// Moose String-derived vectors
dynamicCastAndExtractDoubleIndex(/*std::*/string , it->second, full_name, it->first, in_global, global_params_block);
dynamicCastAndExtractDoubleIndex(FileName , it->second, full_name, it->first, in_global, global_params_block);
dynamicCastAndExtractDoubleIndex(FileNameNoExtension , it->second, full_name, it->first, in_global, global_params_block);
dynamicCastAndExtractDoubleIndex(MeshFileName , it->second, full_name, it->first, in_global, global_params_block);
dynamicCastAndExtractDoubleIndex(SubdomainName , it->second, full_name, it->first, in_global, global_params_block);
dynamicCastAndExtractDoubleIndex(BoundaryName , it->second, full_name, it->first, in_global, global_params_block);
// reading double indexed Variable name is problematic because Coupleable assumes they come as vectors
// therefore they not included in this list
dynamicCastAndExtractDoubleIndex(FunctionName , it->second, full_name, it->first, in_global, global_params_block);
dynamicCastAndExtractDoubleIndex(UserObjectName , it->second, full_name, it->first, in_global, global_params_block);
dynamicCastAndExtractDoubleIndex(IndicatorName , it->second, full_name, it->first, in_global, global_params_block);
dynamicCastAndExtractDoubleIndex(MarkerName , it->second, full_name, it->first, in_global, global_params_block);
dynamicCastAndExtractDoubleIndex(MultiAppName , it->second, full_name, it->first, in_global, global_params_block);
dynamicCastAndExtractDoubleIndex(PostprocessorName , it->second, full_name, it->first, in_global, global_params_block);
dynamicCastAndExtractDoubleIndex(VectorPostprocessorName, it->second, full_name, it->first, in_global, global_params_block);
dynamicCastAndExtractDoubleIndex(OutputName , it->second, full_name, it->first, in_global, global_params_block);
dynamicCastAndExtractDoubleIndex(MaterialPropertyName , it->second, full_name, it->first, in_global, global_params_block);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I want to support every type in the double syntax. It's unlikely that we'll use very many of them. I'd suggest we add the types you want and leave the rest out until there's a use case and better yet, a test case to support them.

}
}

Expand Down Expand Up @@ -931,6 +977,45 @@ void Parser::setVectorParameter(const std::string & full_name, const std::string
}
}


template<typename T>
void Parser::setDoubleIndexParameter(const std::string & full_name, const std::string & short_name, InputParameters::Parameter<std::vector<std::vector<T> > >* param, bool in_global, GlobalParamsAction * global_block)
{
GetPot *gp;

// See if this variable was passed on the command line
// if it was then we will retrieve the value from the command line instead of the file
if (_app.commandLine() && _app.commandLine()->haveVariable(full_name.c_str()))
gp = _app.commandLine()->getPot();
else
gp = &_getpot_file;

// Get the full string assigned to the variable full_name
std::string buffer = gp->get_value_no_default(full_name, "");

// split vector at delim ;
// NOTE: the substrings are _not_ of type T yet
std::vector<std::string> first_tokenized_vector;
MooseUtils::tokenize(buffer, first_tokenized_vector, 1, ";");
param->set().resize(first_tokenized_vector.size());

for (unsigned j = 0; j < first_tokenized_vector.size(); ++j)
if (!MooseUtils::tokenizeAndConvert<T>(first_tokenized_vector[j], param->set()[j]))
mooseError("Reading parameter " << short_name << " failed.");

if (in_global)
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is a WIP but this will definitely need to be filled out before you are done.

global_block->remove(short_name);
global_block->setDoubleIndexParam<T>(short_name).resize(first_tokenized_vector.size());
for (unsigned j = 0; j < first_tokenized_vector.size(); ++j)
{
global_block->setDoubleIndexParam<T>(short_name)[j].resize(param->get()[j].size());
for (unsigned int i = 0; i < param->get()[j].size(); ++i)
global_block->setDoubleIndexParam<T>(short_name)[j][i] = param->get()[j][i];
}
}
}

template<typename T>
void Parser::setScalarComponentParameter(const std::string & full_name, const std::string & short_name, InputParameters::Parameter<T> * param, bool in_global, GlobalParamsAction * global_block)
{
Expand Down Expand Up @@ -1204,6 +1289,8 @@ void Parser::setVectorParameter<VariableName>(const std::string & full_name, con

// If we are able to convert this value into a Real, then set a default coupled value
if (ss >> real_value && ss.eof())
// FIXME: the real_value is assigned to defaultCoupledValue overriding the value assigned before.
// Currently there is no functionality to separately assign the correct "real_value[i]" in InputParameters
_current_params->defaultCoupledValue(short_name, real_value);
else
{
Expand Down
67 changes: 67 additions & 0 deletions test/include/userobjects/ReadDoubleIndex.h
@@ -0,0 +1,67 @@
/****************************************************************/
/* DO NOT MODIFY THIS HEADER */
/* MOOSE - Multiphysics Object Oriented Simulation Environment */
/* */
/* (c) 2010 Battelle Energy Alliance, LLC */
/* ALL RIGHTS RESERVED */
/* */
/* Prepared by Battelle Energy Alliance, LLC */
/* Under Contract No. DE-AC07-05ID14517 */
/* With the U. S. Department of Energy */
/* */
/* See COPYRIGHT for full restrictions */
/****************************************************************/

#ifndef READDOUBLEINDEX_H
#define READDOUBLEINDEX_H

#include "GeneralUserObject.h"

class ReadDoubleIndex;

template<>
InputParameters validParams<ReadDoubleIndex>();


/**
* User Object for testing double index parsing
*/
class ReadDoubleIndex : public GeneralUserObject
{
public:
ReadDoubleIndex(const InputParameters & params);

virtual void initialize() {};
virtual void execute();
virtual void finalize() {};

protected:
const std::vector<std::vector<Real> > & _real_di;
const std::vector<std::vector<unsigned int> > & _uint_di;
const std::vector<std::vector<int> > & _int_di;
const std::vector<std::vector<long> > & _long_di;
const std::vector<std::vector<SubdomainID> > & _subid_di;
const std::vector<std::vector<BoundaryID> > & _bid_di;

const std::vector<std::vector<std::string> > & _str_di;
const std::vector<std::vector<FileName> > & _file_di;
const std::vector<std::vector<FileNameNoExtension> > & _file_no_di;

const std::vector<std::vector<MeshFileName> > & _mesh_file_di;
const std::vector<std::vector<SubdomainName> > & _subdomain_name_di;
const std::vector<std::vector<BoundaryName> > & _boundary_name_di;
const std::vector<std::vector<FunctionName> > & _function_name_di;
const std::vector<std::vector<UserObjectName> > & _userobject_name_di;
const std::vector<std::vector<IndicatorName> > & _indicator_name_di;

const std::vector<std::vector<MarkerName> > & _marker_name_di;
const std::vector<std::vector<MultiAppName> > & _multiapp_name_di;
const std::vector<std::vector<PostprocessorName> > & _postprocessor_name_di;
const std::vector<std::vector<VectorPostprocessorName> > & _vector_postprocessor_name_di;
const std::vector<std::vector<OutputName> > & _output_name_di;
const std::vector<std::vector<MaterialPropertyName> > & _material_property_name_di;

};


#endif /* ReadDoubleIndex_H */
2 changes: 2 additions & 0 deletions test/src/base/MooseTestApp.C
Expand Up @@ -176,6 +176,7 @@
#include "TestBoundaryRestrictableAssert.h"
#include "GetMaterialPropertyBoundaryBlockNamesTest.h"
#include "SetupInterfaceCount.h"
#include "ReadDoubleIndex.h"

// Postprocessors
#include "TestCopyInitialSolution.h"
Expand Down Expand Up @@ -465,6 +466,7 @@ MooseTestApp::registerObjects(Factory & factory)
registerUserObject(SideSetupInterfaceCount);
registerUserObject(InternalSideSetupInterfaceCount);
registerUserObject(NodalSetupInterfaceCount);
registerUserObject(ReadDoubleIndex);

registerPostprocessor(InsideValuePPS);
registerPostprocessor(TestCopyInitialSolution);
Expand Down