From f621e19cef4b35868764e4dbb57b0eefa8d88906 Mon Sep 17 00:00:00 2001 From: Caleb Schilly Date: Wed, 29 Nov 2023 11:09:03 -0500 Subject: [PATCH] create parser test file --- packages/zoltan2/test/core/CMakeLists.txt | 1 + .../zoltan2/test/core/parser/CMakeLists.txt | 11 ++ packages/zoltan2/test/core/parser/config.yaml | 69 ++++++++++++ .../zoltan2/test/core/parser/test_parser.cpp | 103 ++++++++++++++++++ 4 files changed, 184 insertions(+) create mode 100644 packages/zoltan2/test/core/parser/CMakeLists.txt create mode 100644 packages/zoltan2/test/core/parser/config.yaml create mode 100644 packages/zoltan2/test/core/parser/test_parser.cpp diff --git a/packages/zoltan2/test/core/CMakeLists.txt b/packages/zoltan2/test/core/CMakeLists.txt index b5efcc25b1cc..4cf22451b8d5 100644 --- a/packages/zoltan2/test/core/CMakeLists.txt +++ b/packages/zoltan2/test/core/CMakeLists.txt @@ -1,6 +1,7 @@ ADD_SUBDIRECTORIES( unit partition +parser order TpetraCrsColorer color diff --git a/packages/zoltan2/test/core/parser/CMakeLists.txt b/packages/zoltan2/test/core/parser/CMakeLists.txt new file mode 100644 index 000000000000..05a8c4abafc6 --- /dev/null +++ b/packages/zoltan2/test/core/parser/CMakeLists.txt @@ -0,0 +1,11 @@ +TRIBITS_ADD_EXECUTABLE_AND_TEST( + test_parser + SOURCES test_parser.cpp + COMM serial mpi + ) + +TRIBITS_COPY_FILES_TO_BINARY_DIR(ParserTestFiles + SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR} + SOURCE_FILES config.yaml + EXEDEPS test_parser + ) diff --git a/packages/zoltan2/test/core/parser/config.yaml b/packages/zoltan2/test/core/parser/config.yaml new file mode 100644 index 000000000000..d875900f0f81 --- /dev/null +++ b/packages/zoltan2/test/core/parser/config.yaml @@ -0,0 +1,69 @@ +# Snippet from Trilinos/packages/panzer/mini-em/example/BlockPrec/maxwell2D.xml (below) +# Converted from xml to yaml using ChatGPT + +Physics Blocks: + Maxwell Physics: + Maxwell Physics: + Type: Maxwell + Basis Order: 1 + Integration Order: 2 + Model ID: electromagnetics + Permittivity: epsilon + Conductivity: sigma + Permeability: mu + Current: J + Auxiliary Physics Block: + Auxiliary Gradient Physics: + Type: Auxiliary Weak Gradient + Vector Name: AUXILIARY_EDGE + Scalar Name: AUXILIARY_NODE + Basis Order: 1 + Integration Order: 2 + +Wishlist: + Indentation: + - a + - b + Wrong indentation: + - c + - d + - e + List of lists: + - [1, 2, 3] + - [1, 2, 3, 4] + List of lists of lists: + - [[1,2,3], [4,5,6], [7,8,9]] + - [[9,7,8], [6,5,4], [3,2,1]] + Line continuation: + - [1, 2, 3, 4, 5, 6, + 7, 8, 9, 10] + +# allow unicode characters in comments: ± + +# +# +# +# +# +# +# +# +# +# +# +# +# +# +# +# +# +# +# +# +# +# +# +# +# +# + diff --git a/packages/zoltan2/test/core/parser/test_parser.cpp b/packages/zoltan2/test/core/parser/test_parser.cpp new file mode 100644 index 000000000000..3b6f209046da --- /dev/null +++ b/packages/zoltan2/test/core/parser/test_parser.cpp @@ -0,0 +1,103 @@ +#include +#include +#include +#include +#include "yaml-cpp/yaml.h" + + +void parse_mini_em_sample(std::string filename) { + + // ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + // ------------------------------------------------- Trilinos/packages/panzer/mini-em/example/BlockPrec/main.cpp (line 195) --------------------------------------------------------------------------- + // ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + + // Using yaml-cpp // Using Teuchos::ParameterList + + YAML::Node input_params = YAML::LoadFile(filename); // Teuchos::RCP input_params = Teuchos::rcp(new Teuchos::ParameterList("User_App Parameters")); + YAML::Node physicsBlock_pl = input_params["Physics Blocks"]; // Teuchos::ParameterList &physicsBlock_pl = input_params->sublist("Physics Blocks"); + + YAML::Node physicsEqSet = physicsBlock_pl["Maxwell Physics"]["Maxwell Physics"]; // Teuchos::ParameterList& physicsEqSet = physicsBlock_pl.sublist("Maxwell Physics").sublist("Maxwell Physics"); + + const std::string physicsTypeStr = physicsEqSet["Type"].as(); // const std::string physicsTypeStr = physicsEqSet.get("Type"); + + std::string physics; // physicsType physics; (for demonstration purposes, we replace physicsType with std::string); + + if (physicsTypeStr == "Maxwell") { // if (physicsTypeStr == "Maxwell") + physics = "MAXWELL"; // physics = MAXWELL; + } else if (physicsTypeStr == "Darcy") { // else if (physicsTypeStr == "Darcy") + physics = "DARCY"; // physics = DARCY; + } else { // else + std::cout << "Error: invalid physicsTypeStr" << std::endl; // TEUCHOS_ASSERT(false); + } + + int basis_order = physicsEqSet["Basis Order"].as(); // basis_order = physicsEqSet.get("Basis Order", basis_order); + + physicsEqSet["Integration Order"] = 2 * basis_order; // physicsEqSet.set("Integration Order", 2*basis_order); + + std::cout << "Physics Type: " << physics << std::endl; + std::cout << "Basis Order: " << basis_order << std::endl; + std::cout << "Integration Order: " << physicsEqSet["Integration Order"].as() << std::endl; +} + +void parse_wishlist(std::string filename) { + YAML::Node input_file = YAML::LoadFile(filename); + YAML::Node wishlist = input_file["Wishlist"]; + + // Check Indentation + const std::vector indentation = wishlist["Indentation"].as>(); + std::cout << "\nCheck correct indentation:" << std::endl; + for (const auto& element : indentation) { + std::cout << element << " "; + } std::cout << std::endl; + + // Check Wrong indentation + const std::vector wrong_indentation = wishlist["Wrong indentation"].as>(); + std::cout << "\nCheck incorrect indetation:" << std::endl; + for (const auto& element : wrong_indentation) { + std::cout << element << " "; + } std::cout << std::endl; + + // List of lists + const std::vector> list_of_lists = wishlist["List of lists"].as>>(); + std::cout << "\nCheck list of lists:" << std::endl; + for (const auto& list : list_of_lists) { + for (const auto& elt : list) { + std::cout << elt << " "; + } std::cout << std::endl; + } + + // List of lists of lists + const std::vector>> list_of_lists_of_lists = wishlist["List of lists of lists"].as>>>(); + std::cout << "\nCheck list of lists:" << std::endl; + for (const auto& lists : list_of_lists_of_lists) { + for (const auto& list : lists) { + std::cout << "[ "; + for (const auto& elt : list) { + std::cout << elt << " "; + } std::cout << "]"; + } std::cout << std::endl; + } + + // Line continuation + const std::vector> line_continuation = wishlist["Line continuation"].as>>(); + std::cout << "\nCheck line continuation:" << std::endl; + for (const auto& list : line_continuation) { + for (const auto& elt : list) { + std::cout << elt << " "; + } std::cout << std::endl; + } +} + +int main(int argc, char* argv[]) { + + std::string executable_path = std::filesystem::path(argv[0]).parent_path().string(); + std::string filename = executable_path + "/config.yaml"; + + // std::string filename = "config.yaml" // doesn't work + std::cout << "Parsing " << filename << "\n" << std::endl; + + parse_mini_em_sample(filename); + parse_wishlist(filename); + + return 0; +}