From ceca6fe665f968c8d1608b05f3435ffaf610ec24 Mon Sep 17 00:00:00 2001 From: "Thomas J. Otahal" Date: Mon, 30 Oct 2023 10:50:31 -0600 Subject: [PATCH] Catalyst API 2 Added tests to Catalyst manager to inspect IOSS properties passed into the data and handle them like the Catalyst API 1 database. --- .../src/catalyst/Iocatalyst_CatalystManager.C | 121 ++++++++++++- .../src/catalyst/Iocatalyst_CatalystManager.h | 40 +++++ .../Iocatalyst_DatabaseIOTest.C | 25 +-- .../catalyst_tests/Iocatalyst_ManagerTest.C | 160 +++++++++++++++++- 4 files changed, 325 insertions(+), 21 deletions(-) diff --git a/packages/seacas/libraries/ioss/src/catalyst/Iocatalyst_CatalystManager.C b/packages/seacas/libraries/ioss/src/catalyst/Iocatalyst_CatalystManager.C index b27dd4acbc..b6543867f8 100644 --- a/packages/seacas/libraries/ioss/src/catalyst/Iocatalyst_CatalystManager.C +++ b/packages/seacas/libraries/ioss/src/catalyst/Iocatalyst_CatalystManager.C @@ -4,15 +4,132 @@ // // See packages/seacas/LICENSE for details +#include #include +#include namespace Iocatalyst { - CatalystManager::CatalystManager() {} + CatalystManager::CatalystManager() { catalystOutputIDNumber = 0; } CatalystManager::~CatalystManager() {} void CatalystManager::writeToCatalystLogFile(const Ioss::ParallelUtils &putils, - const Ioss::PropertyManager &props) {} + const Ioss::PropertyManager &props) + { + if (putils.parallel_rank() == 0) { + CatalystLogging catLog = CatalystLogging(); + catLog.setProperties(&props); + if (catLog.isCatalystLoggingON()) { + catLog.writeToLogFile(); + } + } + putils.barrier(); + } + + CatalystManager::CatalystProps + CatalystManager::registerDatabase(const Ioss::PropertyManager &props, + const Ioss::ParallelUtils &putils) + { + CatalystManager::CatalystProps catalystProps; + catalystProps.catalystPipelineID = catalystOutputIDNumber; + incrementOutputCounts(); + + if (props.exists("CATALYST_BLOCK_PARSE_JSON_STRING")) { + catalystProps.catalystBlockJSON = props.get("CATALYST_BLOCK_PARSE_JSON_STRING").get_string(); + } + else if (props.exists("PHACTORI_JSON_SCRIPT")) { + bool readOkay = false; + std::string phactoriJSONFilePath = props.get("PHACTORI_JSON_SCRIPT").get_string(); + if (putils.parallel_rank() == 0) { + std::ifstream f(phactoriJSONFilePath); + if (f) { + std::ostringstream ss; + ss << f.rdbuf(); + catalystProps.catalystBlockJSON = ss.str(); + readOkay = true; + } + } + this->broadCastStatusCode(readOkay, putils); + if (!readOkay) { + std::ostringstream errmsg; + errmsg << "Unable to read input file: " << phactoriJSONFilePath << "\n"; + IOSS_ERROR(errmsg); + } + else { + this->broadCastString(catalystProps.catalystBlockJSON, putils); + } + } + + if (props.exists("CATALYST_SCRIPT")) { + catalystProps.catalystPythonFilename = props.get("CATALYST_SCRIPT").get_string(); + } + else { + catalystProps.catalystPythonFilename = this->getCatalystPythonDriverPath(); + } + + if (props.exists("CATALYST_SCRIPT_EXTRA_FILE")) { + catalystProps.catalystScriptExtraFile = props.get("CATALYST_SCRIPT_EXTRA_FILE").get_string(); + } + + if (props.exists("CATALYST_BLOCK_PARSE_INPUT_DECK_NAME")) { + catalystProps.catalystInputDeckName = + props.get("CATALYST_BLOCK_PARSE_INPUT_DECK_NAME").get_string(); + } + + if (props.exists("CATALYST_ENABLE_LOGGING")) { + catalystProps.enableLogging = props.get("CATALYST_ENABLE_LOGGING").get_int(); + } + + if (props.exists("CATALYST_DEBUG_LEVEL")) { + catalystProps.debugLevel = props.get("CATALYST_DEBUG_LEVEL").get_int(); + } + + if (props.exists("CATALYST_OUTPUT_DIRECTORY")) { + catalystProps.catalystOutputDirectory = props.get("CATALYST_OUTPUT_DIRECTORY").get_string(); + } + + if (props.exists("CATALYST_INPUT_NAME")) { + catalystProps.catalystInputName = props.get("CATALYST_INPUT_NAME").get_string(); + } + + if (props.exists("CATALYST_MULTI_INPUT_PIPELINE_NAME")) { + catalystProps.enableCatalystMultiInputPipeline = true; + catalystProps.catalystMultiInputPipelineName = + props.get("CATALYST_MULTI_INPUT_PIPELINE_NAME").get_string(); + } + + return catalystProps; + } + + void CatalystManager::incrementOutputCounts() { catalystOutputIDNumber++; } + + void CatalystManager::broadCastString(IOSS_MAYBE_UNUSED std::string &s, + IOSS_MAYBE_UNUSED const Ioss::ParallelUtils &putils) + { + IOSS_PAR_UNUSED(s); + IOSS_PAR_UNUSED(dbinfo); +#ifdef SEACAS_HAVE_MPI + int size = s.size(); + putils.broadcast(size); + if (putils.parallel_rank() != 0) { + s.resize(size); + } + putils.broadcast(s); +#endif + } + + void CatalystManager::broadCastStatusCode(IOSS_MAYBE_UNUSED bool &statusCode, + IOSS_MAYBE_UNUSED const Ioss::ParallelUtils &putils) + { + IOSS_PAR_UNUSED(statusCode); + IOSS_PAR_UNUSED(dbinfo); +#ifdef SEACAS_HAVE_MPI + + int code = statusCode; + putils.broadcast(code); + statusCode = code; +#endif + } } // namespace Iocatalyst \ No newline at end of file diff --git a/packages/seacas/libraries/ioss/src/catalyst/Iocatalyst_CatalystManager.h b/packages/seacas/libraries/ioss/src/catalyst/Iocatalyst_CatalystManager.h index 1b7504c6d1..622b0f9470 100644 --- a/packages/seacas/libraries/ioss/src/catalyst/Iocatalyst_CatalystManager.h +++ b/packages/seacas/libraries/ioss/src/catalyst/Iocatalyst_CatalystManager.h @@ -15,20 +15,60 @@ namespace Iocatalyst { class IOCATALYST_EXPORT CatalystManager { public: + using CatalystPipelineID = unsigned int; + inline static const std::string CATALYST_OUTPUT_DIRECTORY = "CatalystOutput"; + inline static const std::string CATALYST_INPUT_NAME = "input"; + static CatalystManager &getInstance() { static CatalystManager instance; return instance; } + std::string getCatalystPythonDriverPath() { return "/todo/create/real/path"; } + void writeToCatalystLogFile(const Ioss::ParallelUtils &putils, const Ioss::PropertyManager &props); + struct CatalystProps + { + CatalystProps() + { + catalystPipelineID = 0; + enableLogging = false; + debugLevel = 0; + catalystOutputDirectory = CATALYST_OUTPUT_DIRECTORY; + catalystInputName = CATALYST_INPUT_NAME; + enableCatalystMultiInputPipeline = false; + } + CatalystPipelineID catalystPipelineID; + std::string catalystBlockJSON; + std::string catalystPythonFilename; + std::string catalystScriptExtraFile; + std::string catalystInputDeckName; + bool enableLogging; + int debugLevel; + std::string catalystOutputDirectory; + std::string catalystInputName; + bool enableCatalystMultiInputPipeline; + std::string catalystMultiInputPipelineName; + }; + + CatalystProps registerDatabase(const Ioss::PropertyManager &props, + const Ioss::ParallelUtils &putils); + private: CatalystManager(); ~CatalystManager(); CatalystManager(const CatalystManager &) = delete; CatalystManager &operator=(const CatalystManager &) = delete; + + void broadCastString(std::string &s, const Ioss::ParallelUtils &putils); + void broadCastStatusCode(bool &statusCode, const Ioss::ParallelUtils &putils); + + void incrementOutputCounts(); + + CatalystPipelineID catalystOutputIDNumber; }; } // namespace Iocatalyst diff --git a/packages/seacas/libraries/ioss/src/catalyst_tests/Iocatalyst_DatabaseIOTest.C b/packages/seacas/libraries/ioss/src/catalyst_tests/Iocatalyst_DatabaseIOTest.C index 07455b96a8..e203091c0c 100644 --- a/packages/seacas/libraries/ioss/src/catalyst_tests/Iocatalyst_DatabaseIOTest.C +++ b/packages/seacas/libraries/ioss/src/catalyst_tests/Iocatalyst_DatabaseIOTest.C @@ -6,13 +6,13 @@ #include #include +#include #include #include #include -#include #include -#include #include +#include Iocatalyst_DatabaseIOTest::Iocatalyst_DatabaseIOTest() { @@ -31,17 +31,18 @@ bool Iocatalyst_DatabaseIOTest::regionsAreEqual(const std::string &fileName, const std::string &iossDatabaseType) { Ioss::PropertyManager dbProps; - auto inputFileName = fileName; - auto inputCatalystFileName = catFileName; - Ioss::ParallelUtils pu; - int numRanks = pu.parallel_size(); - int rank = pu.parallel_rank(); - if(iossDatabaseType == EXODUS_DATABASE_TYPE && numRanks > 1) { + auto inputFileName = fileName; + auto inputCatalystFileName = catFileName; + Ioss::ParallelUtils pu; + int numRanks = pu.parallel_size(); + int rank = pu.parallel_rank(); + if (iossDatabaseType == EXODUS_DATABASE_TYPE && numRanks > 1) { inputFileName += "." + std::to_string(numRanks) + "." + std::to_string(rank); - inputCatalystFileName += "." + std::to_string(numRanks) + "." + std::to_string(rank); + inputCatalystFileName += "." + std::to_string(numRanks) + "." + std::to_string(rank); } - Ioss::DatabaseIO *dbi = Ioss::IOFactory::create(iossDatabaseType, inputFileName, Ioss::READ_RESTART, - Ioss::ParallelUtils::comm_self(), dbProps); + Ioss::DatabaseIO *dbi = + Ioss::IOFactory::create(iossDatabaseType, inputFileName, Ioss::READ_RESTART, + Ioss::ParallelUtils::comm_self(), dbProps); if (dbi == nullptr || !dbi->ok(true)) { return false; } @@ -102,7 +103,7 @@ void Iocatalyst_DatabaseIOTest::checkZeroCopyFields(Iocatalyst::BlockMeshSet::IO return; } - Ioss::Region cir(cdbi); + Ioss::Region cir(cdbi); Iocatalyst::DatabaseIO::RegionContainer rc; rc.push_back(&cir); checkEntityContainerZeroCopyFields(rc); diff --git a/packages/seacas/libraries/ioss/src/catalyst_tests/Iocatalyst_ManagerTest.C b/packages/seacas/libraries/ioss/src/catalyst_tests/Iocatalyst_ManagerTest.C index d589f671e4..4d7216313b 100644 --- a/packages/seacas/libraries/ioss/src/catalyst_tests/Iocatalyst_ManagerTest.C +++ b/packages/seacas/libraries/ioss/src/catalyst_tests/Iocatalyst_ManagerTest.C @@ -4,19 +4,165 @@ // // See packages/seacas/LICENSE for details +#include +#include #include #include -#include -#include +#include +#include +#include + +class ManagerTest : public ::testing::Test +{ +protected: + Ioss::PropertyManager props; + Ioss::ParallelUtils putils; + Iocatalyst::CatalystManager::CatalystProps catalystProps; + void registerDatabase() { + catalystProps = Iocatalyst::CatalystManager::getInstance().registerDatabase(props, putils); + } +}; TEST_F(LoggingTest, LoggingDefault) { - Ioss::ParallelUtils putils; - Iocatalyst::CatalystManager::getInstance().writeToCatalystLogFile(putils, props); + Ioss::ParallelUtils putils; + Iocatalyst::CatalystManager::getInstance().writeToCatalystLogFile(putils, props); + EXPECT_FALSE(isFileExists(Iocatalyst::CatalystLogging::getDefaultLogFileName().c_str())); +} - //log.setProperties(&props); - //writeToCatalystLogFile(const DatabaseInfo &dbinfo, const Ioss::PropertyManager &props) - //log.writeToLogFile(); +TEST_F(LoggingTest, LoggingNotEnabled) +{ + Ioss::ParallelUtils putils; + props.add(Ioss::Property("CATALYST_LOGGING_ENABLED", false)); + Iocatalyst::CatalystManager::getInstance().writeToCatalystLogFile(putils, props); + EXPECT_FALSE(isFileExists(Iocatalyst::CatalystLogging::getDefaultLogFileName().c_str())); +} + +TEST_F(LoggingTest, LoggingEnabled) +{ + Ioss::ParallelUtils putils; + props.add(Ioss::Property("CATALYST_LOGGING_ENABLED", true)); + props.add(Ioss::Property("CATALYST_LOGGING_STRING_PROP", "foo")); + props.add(Ioss::Property("CATALYST_LOGGING_INTEGER_PROP", 6)); + props.add(Ioss::Property("CATALYST_LOGGING_REAL_PROP", 3.7556)); + Iocatalyst::CatalystManager::getInstance().writeToCatalystLogFile(putils, props); + EXPECT_TRUE(isFileExists(Iocatalyst::CatalystLogging::getDefaultLogFileName().c_str())); +} +TEST_F(ManagerTest, CatalystPipelineID) +{ + registerDatabase(); + EXPECT_EQ(catalystProps.catalystPipelineID, 0); + registerDatabase(); + EXPECT_EQ(catalystProps.catalystPipelineID, 1); + + registerDatabase(); + EXPECT_EQ(catalystProps.catalystPipelineID, 2); +} + +TEST_F(ManagerTest, CATALYST_BLOCK_PARSE_JSON_STRING) +{ + std::string jsonScript = "{foo: 12}"; + props.add(Ioss::Property("CATALYST_BLOCK_PARSE_JSON_STRING", jsonScript)); + registerDatabase(); + EXPECT_EQ(catalystProps.catalystBlockJSON, jsonScript); +} + +TEST_F(ManagerTest, PHACTORI_JSON_SCRIPT) +{ + std::string jsonFileName = "jsonFile.json"; + std::string jsonScript = "{foo: 12}"; + std::ofstream jsonFile; + jsonFile.open(jsonFileName); + jsonFile << jsonScript; + jsonFile.close(); + props.add(Ioss::Property("PHACTORI_JSON_SCRIPT", jsonFileName)); + registerDatabase(); + EXPECT_EQ(catalystProps.catalystBlockJSON, jsonScript); + remove(jsonFileName.c_str()); +} + +TEST_F(ManagerTest, CATALYST_SCRIPT) +{ + registerDatabase(); + EXPECT_EQ(catalystProps.catalystPythonFilename, + Iocatalyst::CatalystManager::getInstance().getCatalystPythonDriverPath()); + + std::string catalystFileName = "/path/to/file/catalystFile.txt"; + props.add(Ioss::Property("CATALYST_SCRIPT", catalystFileName)); + registerDatabase(); + EXPECT_EQ(catalystProps.catalystPythonFilename, catalystFileName); } + +TEST_F(ManagerTest, CATALYST_SCRIPT_EXTRA_FILE) +{ + std::string extraFileName = "extraFileName.txt"; + props.add(Ioss::Property("CATALYST_SCRIPT_EXTRA_FILE", extraFileName)); + registerDatabase(); + EXPECT_EQ(catalystProps.catalystScriptExtraFile, extraFileName); +} + +TEST_F(ManagerTest, CATALYST_BLOCK_PARSE_INPUT_DECK_NAME) +{ + std::string inputDeckName = "contact.i"; + props.add(Ioss::Property("CATALYST_BLOCK_PARSE_INPUT_DECK_NAME", inputDeckName)); + registerDatabase(); + EXPECT_EQ(catalystProps.catalystInputDeckName, inputDeckName); +} + +TEST_F(ManagerTest, CATALYST_ENABLE_LOGGING) +{ + registerDatabase(); + EXPECT_FALSE(catalystProps.enableLogging); + + props.add(Ioss::Property("CATALYST_ENABLE_LOGGING", true)); + registerDatabase(); + EXPECT_TRUE(catalystProps.enableLogging); +} + +TEST_F(ManagerTest, CATALYST_DEBUG_LEVEL) +{ + registerDatabase(); + EXPECT_EQ(catalystProps.debugLevel, 0); + + props.add(Ioss::Property("CATALYST_DEBUG_LEVEL", 3)); + registerDatabase(); + EXPECT_EQ(catalystProps.debugLevel, 3); +} + +TEST_F(ManagerTest, CATALYST_OUTPUT_DIRECTORY) +{ + registerDatabase(); + EXPECT_EQ(catalystProps.catalystOutputDirectory, + Iocatalyst::CatalystManager::getInstance().CATALYST_OUTPUT_DIRECTORY); + + std::string catalystOutputDirectory = "catalyst"; + props.add(Ioss::Property("CATALYST_OUTPUT_DIRECTORY", catalystOutputDirectory)); + registerDatabase(); + EXPECT_EQ(catalystProps.catalystOutputDirectory, catalystOutputDirectory); +} + +TEST_F(ManagerTest, CATALYST_INPUT_NAME) +{ + registerDatabase(); + EXPECT_EQ(catalystProps.catalystInputName, + Iocatalyst::CatalystManager::getInstance().CATALYST_INPUT_NAME); + + std::string catalystInputName = "mesh"; + props.add(Ioss::Property("CATALYST_INPUT_NAME", catalystInputName)); + registerDatabase(); + EXPECT_EQ(catalystProps.catalystInputName, catalystInputName); +} + +TEST_F(ManagerTest, CATALYST_MULTI_INPUT_PIPELINE_NAME) +{ + registerDatabase(); + EXPECT_FALSE(catalystProps.enableCatalystMultiInputPipeline); + + std::string catalystMultiInputPipelineName = "multi"; + props.add(Ioss::Property("CATALYST_MULTI_INPUT_PIPELINE_NAME", catalystMultiInputPipelineName)); + registerDatabase(); + EXPECT_EQ(catalystProps.catalystMultiInputPipelineName, catalystMultiInputPipelineName); + EXPECT_TRUE(catalystProps.enableCatalystMultiInputPipeline); +} \ No newline at end of file