From 5d4c91148c2dae1ef5467eb8c29746ee9e32cd7b Mon Sep 17 00:00:00 2001 From: Edwin Lee Date: Fri, 19 Jul 2024 12:46:21 -0500 Subject: [PATCH] Add API endpoint for accecssing the specified IDF path --- src/EnergyPlus/api/datatransfer.cc | 9 +++++++++ src/EnergyPlus/api/datatransfer.h | 6 ++++++ src/EnergyPlus/api/datatransfer.py | 13 +++++++++++++ tst/EnergyPlus/api/TestDataTransfer.c | 7 ++++++- 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/EnergyPlus/api/datatransfer.cc b/src/EnergyPlus/api/datatransfer.cc index fd8a303e07d..fa6d83bdad3 100644 --- a/src/EnergyPlus/api/datatransfer.cc +++ b/src/EnergyPlus/api/datatransfer.cc @@ -56,6 +56,7 @@ #include #include #include +#include #include #include #include @@ -239,6 +240,14 @@ void resetErrorFlag(EnergyPlusState state) thisState->dataPluginManager->apiErrorFlag = false; } +char *inputFilePath(EnergyPlusState state) +{ + const auto *thisState = static_cast(state); + char *p = new char[std::strlen(thisState->dataStrGlobals->inputFilePath.c_str()) + 1]; + std::strcpy(p, thisState->dataStrGlobals->inputFilePath.c_str()); + return p; +} + char **getObjectNames(EnergyPlusState state, const char *objectType, unsigned int *resultingSize) { const auto *thisState = static_cast(state); diff --git a/src/EnergyPlus/api/datatransfer.h b/src/EnergyPlus/api/datatransfer.h index 11bee4ce75b..6ab69306286 100644 --- a/src/EnergyPlus/api/datatransfer.h +++ b/src/EnergyPlus/api/datatransfer.h @@ -121,6 +121,12 @@ ENERGYPLUSLIB_API int apiErrorFlag(EnergyPlusState state); /// a calculation to continue, this function can reset the flag. /// \param[in] state An active EnergyPlusState instance created with `stateNew`. ENERGYPLUSLIB_API void resetErrorFlag(EnergyPlusState state); +/// \brief Provides the input file path back to the user +/// \details In most circumstances the client will know the path to the input file, but there are some cases where code +/// is generalized in unexpected workflows. Users have requested a way to get the input file path back from the running instance. +/// \param[in] state An active EnergyPlusState instance created with `stateNew`. +/// \return A char * of the input file path. This allocates a new char *, and calling clients must free this when done with it! +ENERGYPLUSLIB_API char *inputFilePath(EnergyPlusState state); // ----- DATA TRANSFER HELPER FUNCTIONS diff --git a/src/EnergyPlus/api/datatransfer.py b/src/EnergyPlus/api/datatransfer.py index bee6023f1f9..312da6c0a3b 100644 --- a/src/EnergyPlus/api/datatransfer.py +++ b/src/EnergyPlus/api/datatransfer.py @@ -139,6 +139,8 @@ def __init__(self, api: cdll, running_as_python_plugin: bool = False): self.api.apiErrorFlag.restype = c_int self.api.resetErrorFlag.argtypes = [c_void_p] self.api.resetErrorFlag.restype = c_void_p + self.api.inputFilePath.argtypes = [c_void_p] + self.api.inputFilePath.restype = c_char_p self.api.requestVariable.argtypes = [c_void_p, c_char_p, c_char_p] self.api.getNumNodesInCondFDSurfaceLayer.argtypes = [c_void_p, c_char_p, c_char_p] self.api.requestVariable.restype = c_void_p @@ -359,6 +361,17 @@ def reset_api_error_flag(self, state: c_void_p) -> None: """ self.api.resetErrorFlag(state) + def get_input_file_path(self, state: c_void_p) -> bytes: + """ + Provides the input file path back to the client. In most circumstances the client will know the path to the + input file, but there are some cases where code is generalized in unexpected workflows. Users have requested + a way to get the input file path back from the running instance. + + :param state: An active EnergyPlus "state" that is returned from a call to `api.state_manager.new_state()`. + :return: Returns a raw bytes representation of the input file path + """ + return self.api.inputFilePath(state) + def get_object_names(self, state: c_void_p, object_type_name: Union[str, bytes]) -> List[str]: """ Gets the instance names for a given object type in the current input file diff --git a/tst/EnergyPlus/api/TestDataTransfer.c b/tst/EnergyPlus/api/TestDataTransfer.c index 3980ba06440..ccd767010b0 100644 --- a/tst/EnergyPlus/api/TestDataTransfer.c +++ b/tst/EnergyPlus/api/TestDataTransfer.c @@ -95,7 +95,7 @@ void afterZoneTimeStepHandler(EnergyPlusState state) freeAPIData(data, arraySize); freeObjectNames(surfaceNames, arraySize); - printf("Got handles %d, %d, %d, %d, %d, %d", + printf("Got handles %d, %d, %d, %d, %d, %d\n", outdoorDewPointActuator, outdoorTempSensor, outdoorDewPointSensor, @@ -106,6 +106,11 @@ void afterZoneTimeStepHandler(EnergyPlusState state) wallConstruction == -1 || floorConstruction == -1) { exit(1); } + + char * filePath = inputFilePath(state); + printf("Input file path accessed via API: %s\n", filePath); + free(filePath); + handlesRetrieved = 1; } setActuatorValue(state, outdoorDewPointActuator, -25.0);