Skip to content

Commit

Permalink
support ragged arrays, lists of lists of lists
Browse files Browse the repository at this point in the history
  • Loading branch information
cwschilly committed Dec 8, 2023
1 parent 5141eb3 commit 391d967
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 32 deletions.
10 changes: 1 addition & 9 deletions nga-ci/build-yamlcpp-off.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ cmake -G "${CMAKE_GENERATOR:-Ninja}" \
-D Trilinos_ENABLE_ALL_FORWARD_DEP_PACKAGES=ON \
-D Trilinos_VERBOSE_CONFIGURE=OFF \
-D BUILD_SHARED_LIBS:BOOL=ON \
-D Tpetra_ENABLE_DEPRECATED_CODE=ON \
\
-D Trilinos_ENABLE_Panzer=ON \
-D Trilinos_ENABLE_PanzerMiniEM=ON \
Expand Down Expand Up @@ -88,14 +89,5 @@ cmake -G "${CMAKE_GENERATOR:-Ninja}" \
-D MPI_EXEC_MAX_NUMPROCS=4 \
\
\
-D Trilinos_ENABLE_Rythmos=OFF \
-D Trilinos_ENABLE_STK=OFF \
-D Trilinos_ENABLE_Pike=OFF \
-D Trilinos_ENABLE_Komplex=OFF \
-D Trilinos_ENABLE_TriKota=OFF \
-D Trilinos_ENABLE_Moertel=OFF \
-D Trilinos_ENABLE_Domi=OFF \
-D Trilinos_ENABLE_FEI=OFF \
-D Trilinos_ENABLE_PyTrilinos=OFF \
-S /opt/src/Trilinos -B /opt/build/Trilinos
ninja -j 12
10 changes: 1 addition & 9 deletions nga-ci/build-yamlcpp-on.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ cmake -G "${CMAKE_GENERATOR:-Ninja}" \
-D Trilinos_ENABLE_ALL_FORWARD_DEP_PACKAGES=ON \
-D Trilinos_VERBOSE_CONFIGURE=OFF \
-D BUILD_SHARED_LIBS:BOOL=ON \
-D Tpetra_ENABLE_DEPRECATED_CODE=ON \
\
-D Trilinos_ENABLE_Panzer=ON \
-D Trilinos_ENABLE_PanzerMiniEM=ON \
Expand Down Expand Up @@ -89,14 +90,5 @@ cmake -G "${CMAKE_GENERATOR:-Ninja}" \
-D MPI_EXEC_MAX_NUMPROCS=4 \
\
\
-D Trilinos_ENABLE_Rythmos=OFF \
-D Trilinos_ENABLE_STK=OFF \
-D Trilinos_ENABLE_Pike=OFF \
-D Trilinos_ENABLE_Komplex=OFF \
-D Trilinos_ENABLE_TriKota=OFF \
-D Trilinos_ENABLE_Moertel=OFF \
-D Trilinos_ENABLE_Domi=OFF \
-D Trilinos_ENABLE_FEI=OFF \
-D Trilinos_ENABLE_PyTrilinos=OFF \
-S /opt/src/Trilinos -B /opt/build/Trilinos
ninja -j 12
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ User_App Parameters:
- e
- f
List of lists:
- [1, 2, 3, 4]
- [1, 2, 3]
- [1, 2, 3, 4]
List of lists of lists:
- [[1,2,3], [4,5,6], [7,8,9]]
Expand Down
146 changes: 133 additions & 13 deletions packages/teuchos/parameterlist/src/Teuchos_YamlParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,17 @@ Teuchos::Array<T> getYamlArray(const ::YAML::Node& node)
return arr;
}

void checkYamlTwoDArray(const ::YAML::Node& node, const std::string& key)
bool checkYamlTwoDArrayIsRagged(const ::YAML::Node& node)
{
bool ragged = false;
for (::YAML::const_iterator it = node.begin(); it != node.end(); ++it)
{
if (it->size() != node.begin()->size())
{
throw YamlSequenceError(std::string("TwoDArray \"") + key + "\" has irregular sizes");
ragged=true;
}
}
return ragged;
}

template<typename T> Teuchos::TwoDArray<T> getYamlTwoDArray(const ::YAML::Node& node)
Expand All @@ -139,6 +141,71 @@ template<typename T> Teuchos::TwoDArray<T> getYamlTwoDArray(const ::YAML::Node&
return arr;
}

int getYamlArrayDim(const ::YAML::Node& node)
{
int ndim = 0;

if (node.Type() == ::YAML::NodeType::Sequence)
{
++ndim;

if (node.begin()->Type() == ::YAML::NodeType::Sequence)
{
++ndim;

if (node.begin()->begin()->Type() == ::YAML::NodeType::Sequence)
{
++ndim;
}
}
}

return ndim;
}



template <typename tarray_t, typename T>
tarray_t getYaml2DRaggedArray(::YAML::Node node, int ndim, std::string key)
{
tarray_t base_arr;
if (ndim == 2) {
Teuchos::Array<T> sub_arr;
for (::YAML::const_iterator it1 = node.begin(); it1 != node.end(); ++it1) {
for (::YAML::const_iterator it2 = it1->begin(); it2 != it1->end(); ++it2) {
sub_arr.push_back(quoted_as<T>(*it2));
} base_arr.push_back(sub_arr);
}
}
else
{
throw YamlSequenceError(std::string("MDArray \"" + key + "\" must have dim 2."));
}
return base_arr;
}

template <typename tarray_t, typename T>
tarray_t getYaml3DArray(::YAML::Node node, int ndim, std::string key)
{
tarray_t base_arr;
if (ndim == 3) {
Teuchos::Array<Teuchos::Array<T>> sub_arr;
Teuchos::Array<T> sub_sub_arr;
for (::YAML::const_iterator it1 = node.begin(); it1 != node.end(); ++it1) {
for (::YAML::const_iterator it2 = it1->begin(); it2 != it1->end(); ++it2) {
for (::YAML::const_iterator it3 = it2->begin(); it3 != it2->end(); ++it3) {
sub_sub_arr.push_back(quoted_as<T>(*it3));
} sub_arr.push_back(sub_sub_arr);
} base_arr.push_back(sub_arr);
}
}
else
{
throw YamlSequenceError(std::string("MDArray \"" + key + "\" must have dim 3."));
}
return base_arr;
}

#endif

std::string remove_trailing_whitespace(std::string const& in) {
Expand Down Expand Up @@ -1298,58 +1365,111 @@ void processKeyValueNode(const std::string& key, const ::YAML::Node& node, Teuch
}
else if(node.Type() == ::YAML::NodeType::Sequence)
{
if (node.begin()->Type() == ::YAML::NodeType::Sequence) {
checkYamlTwoDArray(node, key);
int ndim = getYamlArrayDim(node);
if (ndim == 1)
{
::YAML::Node const& first_value = *(node.begin());
try
{
quoted_as<int>(first_value);
parent.set(key, getYamlArray<int>(node));
}
catch(...)
{
try
{
quoted_as<double>(first_value);
parent.set(key, getYamlArray<double>(node));
}
catch(...)
{
try
{
quoted_as<std::string>(first_value);
parent.set(key, getYamlArray<std::string>(node));
}
catch(...)
{
throw YamlSequenceError(std::string("Array \"") + key + "\" must contain int, double, bool or string");
}
}
}
}
else if (ndim == 2)
{
bool is_ragged = checkYamlTwoDArrayIsRagged(node);
::YAML::Node const& first_value = *(node.begin()->begin());
try
{
quoted_as<int>(first_value);
parent.set(key, getYamlTwoDArray<int>(node));
using arr_t = Teuchos::Array<Teuchos::Array<int>>;
if (is_ragged) {
parent.set(key, getYaml2DRaggedArray<arr_t, int>(node, ndim, key));
} else {
parent.set(key, getYamlTwoDArray<int>(node));
}
}
catch(...)
{
try
{
quoted_as<double>(first_value);
parent.set(key, getYamlTwoDArray<double>(node));
using arr_t = Teuchos::Array<Teuchos::Array<double>>;
if (is_ragged) {
parent.set(key, getYaml2DRaggedArray<arr_t, double>(node, ndim, key));
} else {
parent.set(key, getYamlTwoDArray<double>(node));
}
}
catch(...)
{
try
{
quoted_as<std::string>(first_value);
parent.set(key, getYamlTwoDArray<std::string>(node));
using arr_t = Teuchos::Array<Teuchos::Array<std::string>>;
if (is_ragged) {
parent.set(key, getYaml2DRaggedArray<arr_t, std::string>(node, ndim, key));
} else {
parent.set(key, getYamlTwoDArray<std::string>(node));
}
}
catch(...)
{
throw YamlSequenceError(std::string("TwoDArray \"") + key + "\" must contain int, double, bool or string");
}
}
}
} else {
::YAML::Node const& first_value = *(node.begin());
}
else if (ndim == 3)
{
::YAML::Node const& first_value = *(node.begin()->begin()->begin());
try
{
quoted_as<int>(first_value);
parent.set(key, getYamlArray<int>(node));
using arr_t = Teuchos::Array<Teuchos::Array<Teuchos::Array<int>>>;
parent.set(key, getYaml3DArray<arr_t, int>(node, ndim, key));
}
catch(...)
{
try
{
quoted_as<double>(first_value);
parent.set(key, getYamlArray<double>(node));
using arr_t = Teuchos::Array<Teuchos::Array<Teuchos::Array<double>>>;
parent.set(key, getYaml3DArray<arr_t, double>(node, ndim, key));

}
catch(...)
{
try
{
quoted_as<std::string>(first_value);
parent.set(key, getYamlArray<std::string>(node));
using arr_t = Teuchos::Array<Teuchos::Array<Teuchos::Array<std::string>>>;
parent.set(key, getYaml3DArray<arr_t, std::string>(node, ndim, key));

}
catch(...)
{
throw YamlSequenceError(std::string("Array \"") + key + "\" must contain int, double, bool or string");
throw YamlSequenceError(std::string("3DArray \"") + key + "\" must contain int, double, bool or string");
}
}
}
Expand Down

0 comments on commit 391d967

Please sign in to comment.