Skip to content

Commit

Permalink
Add API endpoint for accecssing the specified IDF path
Browse files Browse the repository at this point in the history
  • Loading branch information
Myoldmopar committed Jul 19, 2024
1 parent c1a2396 commit 5d4c911
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/EnergyPlus/api/datatransfer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
#include <EnergyPlus/DataGlobalConstants.hh>
#include <EnergyPlus/DataHVACGlobals.hh>
#include <EnergyPlus/DataRuntimeLanguage.hh>
#include <EnergyPlus/DataStringGlobals.hh>
#include <EnergyPlus/HeatBalFiniteDiffManager.hh>
#include <EnergyPlus/InputProcessing/InputProcessor.hh>
#include <EnergyPlus/OutputProcessor.hh>
Expand Down Expand Up @@ -239,6 +240,14 @@ void resetErrorFlag(EnergyPlusState state)
thisState->dataPluginManager->apiErrorFlag = false;
}

char *inputFilePath(EnergyPlusState state)
{
const auto *thisState = static_cast<EnergyPlus::EnergyPlusData *>(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<EnergyPlus::EnergyPlusData *>(state);
Expand Down
6 changes: 6 additions & 0 deletions src/EnergyPlus/api/datatransfer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 13 additions & 0 deletions src/EnergyPlus/api/datatransfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion tst/EnergyPlus/api/TestDataTransfer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
Expand Down

6 comments on commit 5d4c911

@nrel-bot-3
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACoupleAPIEndpoints (Myoldmopar) - x86_64-MacOS-10.18-clang-15.0.0: OK (3644 of 3644 tests passed, 0 test warnings)

Build Badge Test Badge

@nrel-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACoupleAPIEndpoints (Myoldmopar) - Win64-Windows-10-VisualStudio-16: Build Failed

Failures:\n

API Test Summary

  • Failed: 10
  • notrun: 5

ConvertInputFormat Test Summary

  • Failed: 4
  • notrun: 1

integration Test Summary

  • Passed: 2
  • Failed: 792

Build Badge Test Badge

@nrel-bot-2c
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACoupleAPIEndpoints (Myoldmopar) - x86_64-Linux-Ubuntu-22.04-gcc-11.4: OK (3685 of 3685 tests passed, 0 test warnings)

Build Badge Test Badge

@nrel-bot-2c
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACoupleAPIEndpoints (Myoldmopar) - x86_64-Linux-Ubuntu-22.04-gcc-11.4-UnitTestsCoverage-Debug: OK (2060 of 2060 tests passed, 0 test warnings)

Build Badge Test Badge Coverage Badge

@nrel-bot-2
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACoupleAPIEndpoints (Myoldmopar) - x86_64-Linux-Ubuntu-22.04-gcc-11.4-IntegrationCoverage-Debug: OK (795 of 795 tests passed, 0 test warnings)

Build Badge Test Badge Coverage Badge

@nrel-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACoupleAPIEndpoints (Myoldmopar) - Win64-Windows-10-VisualStudio-16: Build Failed

Failures:\n

API Test Summary

  • Failed: 10
  • notrun: 5

integration Test Summary

  • Passed: 2
  • Failed: 792

Build Badge Test Badge

Please sign in to comment.