Nektar++
Public Member Functions | Static Public Member Functions | List of all members
Nektar::ParseUtils Class Reference

#include <ParseUtils.h>

Public Member Functions

template<>
bool GenerateVector (const std::string &str, std::vector< std::string > &out)
 Specialised version of ParseUtils::GenerateVector for std::string. More...
 

Static Public Member Functions

static bool GenerateSeqVector (const std::string &str, std::vector< unsigned int > &out)
 Takes a comma-separated compressed string and converts it to entries in a vector. More...
 
template<typename T >
static bool GenerateVector (const std::string &str, std::vector< T > &out)
 Takes a comma-separated string and converts it to entries in a vector. More...
 
template<typename T >
static std::string GenerateSeqString (const std::vector< T > &v)
 Generate a compressed comma-separated string representation of a vector of unsigned integers. More...
 
static bool GenerateVariableSet (const std::string &str, const std::vector< std::string > &variables, std::set< int > &out)
 Generate a set of variable locations. More...
 
static bool GenerateVariableVector (const std::string &str, const std::vector< std::string > &variables, std::vector< int > &out)
 

Detailed Description

Definition at line 47 of file ParseUtils.h.

Member Function Documentation

◆ GenerateSeqString()

template<typename T >
static std::string Nektar::ParseUtils::GenerateSeqString ( const std::vector< T > &  v)
inlinestatic

Generate a compressed comma-separated string representation of a vector of unsigned integers.

This utility routine takes entries of v and returns a string sequence. For example,

std::vector<unsigned int> vec = {1,2,3,4,6,7,8,5,2,3};
std::string output = ParseUtils::GenerateVector(vec);

will produce an output string containing 1-4,6-8,5,2,3.

Parameters
vVector of unsigned integers.
Returns
Compressed comma separated string.

Definition at line 72 of file ParseUtils.h.

73 {
74 static_assert(std::is_integral<T>::value && std::is_unsigned<T>::value,
75 "Unsigned integer type required.");
76
77 if (v.size() == 0)
78 {
79 return "";
80 }
81
82 std::ostringstream ss;
83 auto first = v[0], last = v[0];
84
85 ss << v[0];
86
87 for (auto &i : v)
88 {
89 if (i != last + 1 && i != last)
90 {
91 if (last != first)
92 {
93 ss << '-' << last;
94 }
95 ss << ',' << i;
96 first = i;
97 }
98 last = i;
99 }
100
101 if (last != first)
102 {
103 ss << '-' << last;
104 }
105
106 return ss.str();
107 }

Referenced by Nektar::SpatialDomains::MeshGraph::GetCompositeString(), Nektar::SpatialDomains::MeshGraphIO::GetCompositeString(), Nektar::SolverUtils::DriverAdaptive::ReplaceExpansion(), Nektar::LibUtilities::FieldIOXml::v_Write(), Nektar::SpatialDomains::MeshGraphIOHDF5::WriteDomain(), Nektar::SpatialDomains::MeshGraphIOXml::WriteDomain(), Nektar::SpatialDomains::Movement::WriteMovement(), Nektar::LibUtilities::FieldIOXml::WriteMultiFldFileIDs(), and Nektar::SpatialDomains::MeshGraphIOXml::WriteXMLGeometry().

◆ GenerateSeqVector()

bool Nektar::ParseUtils::GenerateSeqVector ( const std::string &  str,
std::vector< unsigned int > &  out 
)
static

Takes a comma-separated compressed string and converts it to entries in a vector.

This routine is the inverse of ParseUtils::GenerateSeqString. For example,

std::string input = "1-4,6-8,5,2,3";
std::vector<unsigned int> output;
ParseUtils::GenerateSeqString(input, output);

produces an output vector with the entries {1,2,3,4,6,7,8,5,2,3}.

Parameters
strInput CSV string of unsigned integers.
outOutput vector.
See also
ParseUtils::GenerateSeqString

Definition at line 104 of file ParseUtils.cpp.

106{
107 PushBackFunctor<unsigned int> f1(out), f2(out);
108
109 auto it = str.begin();
110 bool success = qi::phrase_parse(
111 it, str.end(),
112 ((qi::uint_ >> '-' >> qi::uint_)[f2] | qi::uint_[f1]) % ',',
113 qi::ascii::space);
114
115 return success && it == str.end();
116}

Referenced by Nektar::SpatialDomains::MeshGraphIOHDF5::CreateCompositeDescriptor(), Nektar::SolverUtils::FilterIntegral::FilterIntegral(), Nektar::SpatialDomains::MeshGraph::GetCompositeList(), Nektar::LibUtilities::FieldIOXml::ImportFieldDefs(), Nektar::LibUtilities::FieldIOXml::ImportMultiFldFileIDs(), Nektar::SpatialDomains::BoundaryConditions::ReadBoundaryConditions(), Nektar::Collections::CollectionOptimisation::ReadCollOps(), Nektar::SpatialDomains::MeshGraphIOHDF5::ReadComposites(), Nektar::SpatialDomains::MeshPartition::ReadExpansions(), Nektar::LibUtilities::SessionReader::ReadFunctions(), Nektar::SpatialDomains::MeshGraphIOXml::ResolveGeomRef1D(), Nektar::SpatialDomains::MeshGraphIOXml::ResolveGeomRef2D(), Nektar::SpatialDomains::MeshGraphIOXml::ResolveGeomRef3D(), Nektar::SolverUtils::FilterAeroForces::v_Initialise(), Nektar::SolverUtils::FilterLagrangianPoints::v_Initialise(), Nektar::FilterMovingBody::v_Initialise(), Nektar::SpatialDomains::MeshGraphIOHDF5::v_PartitionMesh(), Nektar::FieldUtils::ProcessInnerProduct::v_Process(), and Nektar::SpatialDomains::MeshGraphIOXml::WriteXMLGeometry().

◆ GenerateVariableSet()

bool Nektar::ParseUtils::GenerateVariableSet ( const std::string &  str,
const std::vector< std::string > &  variables,
std::set< int > &  out 
)
static

Generate a set of variable locations.

This utility routine takes entries of str and returns a integer set that contains locations of parsed variables in variables. For example,

str = "0,1,w,p";
variables = {"u", "v", "w", "p"};

will produce an set out = {0,1,2,3}.

Parameters
strA string of variables separated by comma.
variablesA vector of variables
Returns
out A set of locations of parsed variables in variables.

Definition at line 168 of file ParseUtils.cpp.

171{
172 std::vector<int> tempvect;
173 GenerateVariableVector(str, variables, tempvect);
174 out.clear();
175 for (auto i : tempvect)
176 {
177 out.insert(i);
178 }
179 return true;
180}
static bool GenerateVariableVector(const std::string &str, const std::vector< std::string > &variables, std::vector< int > &out)
Definition: ParseUtils.cpp:185

References GenerateVariableVector().

Referenced by Nektar::FieldUtils::ProcessGrad::ParserOptions(), Nektar::FieldUtils::ProcessHalfModeToFourier::v_Process(), Nektar::FieldUtils::ProcessInterpField::v_Process(), Nektar::FieldUtils::ProcessInterpPoints::v_Process(), and Nektar::FieldUtils::ProcessPowerSpectrum::v_Process().

◆ GenerateVariableVector()

bool Nektar::ParseUtils::GenerateVariableVector ( const std::string &  str,
const std::vector< std::string > &  variables,
std::vector< int > &  out 
)
static

Definition at line 185 of file ParseUtils.cpp.

188{
189 out.clear();
190 std::vector<std::string> vars;
192 "Failed to interpret variable numbers or names from " + str);
193 for (std::string s : vars)
194 {
195 int v = -1;
196 try
197 {
198 v = boost::lexical_cast<int>(s);
199 }
200 catch (const boost::bad_lexical_cast &)
201 {
202 auto index = find(variables.begin(), variables.end(), s);
203 v = index - variables.begin();
204 }
205 if (v < 0 || v >= variables.size())
206 {
207 WARNINGL0(false, "Warning: variable " + s + " not found");
208 }
209 else
210 {
211 out.push_back(v);
212 }
213 }
214 return true;
215}
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:208
#define WARNINGL0(condition, msg)
Definition: ErrorUtil.hpp:215
static bool GenerateVector(const std::string &str, std::vector< T > &out)
Takes a comma-separated string and converts it to entries in a vector.
Definition: ParseUtils.cpp:130
InputIterator find(InputIterator first, InputIterator last, InputIterator startingpoint, const EqualityComparable &value)
Definition: StdRegions.hpp:475

References ASSERTL0, Nektar::StdRegions::find(), GenerateVector(), and WARNINGL0.

Referenced by GenerateVariableSet(), and Nektar::FieldUtils::ProcessMRF::v_Process().

◆ GenerateVector() [1/2]

template<>
bool Nektar::ParseUtils::GenerateVector ( const std::string &  str,
std::vector< std::string > &  out 
)

Specialised version of ParseUtils::GenerateVector for std::string.

This routine specialises for the std::string data type as this type is not supported by boost::spirit::qi::auto_.

Definition at line 156 of file ParseUtils.cpp.

158{
159 auto it = str.begin();
160 bool success = qi::phrase_parse(it, str.end(), +~qi::char_(",") % ',',
161 qi::ascii::space, out);
162 return success && it == str.end();
163}

◆ GenerateVector() [2/2]

template<typename T >
template bool Nektar::ParseUtils::GenerateVector< float > ( const std::string &  str,
std::vector< T > &  out 
)
static

Takes a comma-separated string and converts it to entries in a vector.

This routine splits up a comma-separated string and returns a vector with the entries. Template specialisations should be defined in this file (and not in the header file) as the use of boost::spirit::qi makes compilation times quite slow.

Parameters
strInput CSV string.
outOutput vector.

Definition at line 130 of file ParseUtils.cpp.

131{
132 auto it = str.begin();
133 bool success =
134 qi::phrase_parse(it, str.end(), qi::auto_ % ',', qi::ascii::space, out);
135 return success && it == str.end();
136}

Referenced by Nektar::MovementTests::BOOST_AUTO_TEST_CASE(), Nektar::SolverUtils::ForcingAbsorption::CalcAbsorption(), Nektar::FieldUtils::ProcessInterpPoints::calcCp0(), Nektar::FieldUtils::ProcessInterpPtsToPts::calcCp0(), Nektar::FieldUtils::ProcessInterpPoints::CreateFieldPts(), Nektar::FieldUtils::ProcessInterpPtsToPts::CreateFieldPts(), Nektar::SolverUtils::FilterHistoryPoints::FilterHistoryPoints(), GenerateVariableVector(), Nektar::LibUtilities::FieldIOHdf5::ImportFieldDef(), Nektar::LibUtilities::FieldIOXml::ImportFieldDefs(), Nektar::SolverUtils::ForcingMovingReferenceFrame::InitBodySolver(), Nektar::SolverUtils::ForcingMovingReferenceFrame::LoadParameters(), Nektar::SpatialDomains::MeshGraph::ReadExpansionInfo(), Nektar::SpatialDomains::MeshPartition::ReadExpansions(), Nektar::LibUtilities::SessionReader::ReadFunctions(), Nektar::LibUtilities::SessionReader::ReadGlobalSysSolnInfo(), Nektar::SpatialDomains::MeshGraph::ReadRefinementInfo(), Nektar::SpatialDomains::Movement::ReadZones(), Nektar::IncNavierStokes::SetUpWomersley(), Nektar::LibUtilities::CsvIO::v_ImportPtsFieldData(), Nektar::LibUtilities::PtsIO::v_ImportPtsFieldData(), Nektar::SolverUtils::Coupling::v_Init(), Nektar::SolverUtils::FilterLagrangianPoints::v_Initialise(), Nektar::FieldUtils::InputDat::v_Process(), Nektar::FieldUtils::InputXml::v_Process(), Nektar::FieldUtils::ProcessAverageFld::v_Process(), Nektar::FieldUtils::ProcessBodyFittedVelocity::v_Process(), Nektar::FieldUtils::ProcessBoundaryExtract::v_Process(), Nektar::FieldUtils::ProcessC0Projection::v_Process(), Nektar::FieldUtils::ProcessInnerProduct::v_Process(), Nektar::FieldUtils::ProcessInterpField::v_Process(), Nektar::FieldUtils::ProcessInterpPoints::v_Process(), Nektar::FieldUtils::ProcessPowerSpectrum::v_Process(), Nektar::FieldUtils::ProcessRemoveField::v_Process(), and Nektar::FieldUtils::ProcessWallNormalData::v_Process().