src/workflow/RunEnergyPlus.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /*********************************************************************************************************************** | ||
| 2 | * OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC. | ||
| 3 | * See also https://openstudio.net/license | ||
| 4 | ***********************************************************************************************************************/ | ||
| 5 | |||
| 6 | #include "OSWorkflow.hpp" | ||
| 7 | |||
| 8 | #include "Util.hpp" | ||
| 9 | |||
| 10 | #include "../model/Model.hpp" | ||
| 11 | #include "../model/WeatherFile.hpp" | ||
| 12 | #include "../model/FileOperations.hpp" | ||
| 13 | |||
| 14 | #include "../energyplus/ErrorFile.hpp" | ||
| 15 | #include "../epjson/epJSONTranslator.hpp" | ||
| 16 | #include "../utilities/idf/Workspace.hpp" | ||
| 17 | |||
| 18 | #include "../utilities/filetypes/WorkflowJSON.hpp" | ||
| 19 | #include "../utilities/filetypes/RunOptions.hpp" | ||
| 20 | |||
| 21 | #include "../utilities/core/Filesystem.hpp" | ||
| 22 | #include "../utilities/core/FilesystemHelpers.hpp" | ||
| 23 | #include "../utilities/core/ASCIIStrings.hpp" | ||
| 24 | #include "../utilities/core/ApplicationPathHelpers.hpp" | ||
| 25 | |||
| 26 | #include <fmt/format.h> | ||
| 27 | #include <fmt/color.h> | ||
| 28 | |||
| 29 | #include <json/json.h> | ||
| 30 | #include <boost/process.hpp> | ||
| 31 | #include <boost/regex.hpp> | ||
| 32 | |||
| 33 | #include <cstdlib> | ||
| 34 | #include <stdexcept> | ||
| 35 | |||
| 36 | namespace openstudio { | ||
| 37 | |||
| 38 | /** PrepareRunDirResults is an RAII helper | ||
| 39 | * This will locate E+ exes, copy idd/epsjon to run Directory, and Chdir to the runDirectory. | ||
| 40 | * It uses RAII to cleanup after itself (remove copied files, chdir back to original directory) */ | ||
| 41 | struct PrepareRunDirResults | ||
| 42 | { | ||
| 43 | openstudio::filesystem::path energyPlusExe; // NOLINT(misc-non-private-member-variables-in-classes) | ||
| 44 | openstudio::filesystem::path expandObjectsExe; // NOLINT(misc-non-private-member-variables-in-classes) | ||
| 45 | std::vector<openstudio::filesystem::path> copiedEnergyPlusFiles; // NOLINT(misc-non-private-member-variables-in-classes) | ||
| 46 | |||
| 47 | // Doing this with a destructor to ensure that the directory gets cleaned up even if I throw an exception, and I can't forget to do it | ||
| 48 | 11 | explicit PrepareRunDirResults(openstudio::filesystem::path runDirPath, openstudio::filesystem::path energyPlusDirectory = {}) | |
| 49 |
1/1✓ Branch 6 taken 11 times.
|
11 | : m_runDirPath(std::move(runDirPath)) { |
| 50 |
6/6✓ Branch 1 taken 11 times.
✓ Branch 4 taken 11 times.
✓ Branch 7 taken 11 times.
✓ Branch 10 taken 11 times.
✓ Branch 13 taken 11 times.
✓ Branch 16 taken 11 times.
|
11 | LOG(Debug, "Original Directory: " << m_curDirPath); |
| 51 |
6/6✓ Branch 1 taken 11 times.
✓ Branch 4 taken 11 times.
✓ Branch 7 taken 11 times.
✓ Branch 10 taken 11 times.
✓ Branch 13 taken 11 times.
✓ Branch 16 taken 11 times.
|
11 | LOG(Debug, "Changing To run directory: " << m_runDirPath); |
| 52 |
1/1✓ Branch 1 taken 11 times.
|
11 | boost::filesystem::current_path(m_runDirPath); |
| 53 | |||
| 54 | // TODO: is this really necessary?! the part that copies the idd ini epjson in particular I question | ||
| 55 | static constexpr std::array<std::string_view, 3> copyFileExtensions{".idd", ".ini", ".epjson"}; | ||
| 56 | #if defined _WIN32 | ||
| 57 | static const boost::regex energyplusRegex(R"(^energyplus.exe$)"); | ||
| 58 | static const boost::regex expandObjectsRegex(R"(^expandobjects.exe$)"); | ||
| 59 | #else | ||
| 60 |
3/7✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 11 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 11 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
|
11 | static const boost::regex energyplusRegex(R"(^energyplus\d{0,4}$)"); |
| 61 |
3/7✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 11 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 11 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
|
11 | static const boost::regex expandObjectsRegex(R"(^expandobjects\d{0,4}$)"); |
| 62 | #endif | ||
| 63 |
1/1✓ Branch 1 taken 11 times.
|
11 | boost::smatch matches; |
| 64 | |||
| 65 |
1/2✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
|
11 | if (energyPlusDirectory.empty()) { |
| 66 |
1/1✓ Branch 1 taken 11 times.
|
11 | energyPlusDirectory = openstudio::getEnergyPlusDirectory(); |
| 67 | } | ||
| 68 | |||
| 69 |
7/7✓ Branch 1 taken 11 times.
✓ Branch 5 taken 11 times.
✓ Branch 9 taken 407 times.
✓ Branch 12 taken 407 times.
✓ Branch 15 taken 418 times.
✓ Branch 17 taken 407 times.
✓ Branch 18 taken 11 times.
|
418 | for (const auto& dirEnt : openstudio::filesystem::directory_iterator{energyPlusDirectory}) { |
| 70 | 407 | const auto& dirEntryPath = dirEnt.path(); | |
| 71 |
3/3✓ Branch 1 taken 407 times.
✓ Branch 3 taken 143 times.
✓ Branch 4 taken 264 times.
|
407 | if (!openstudio::filesystem::is_regular_file(dirEntryPath)) { |
| 72 | 143 | continue; | |
| 73 | } | ||
| 74 |
1/1✓ Branch 3 taken 264 times.
|
264 | auto lower_ext = openstudio::ascii_to_lower_copy(dirEntryPath.extension().string()); |
| 75 |
3/3✓ Branch 3 taken 264 times.
✓ Branch 6 taken 22 times.
✓ Branch 7 taken 242 times.
|
264 | if (std::find(copyFileExtensions.cbegin(), copyFileExtensions.cend(), lower_ext) != copyFileExtensions.cend()) { |
| 76 |
1/1✓ Branch 1 taken 22 times.
|
44 | auto targetPath = m_runDirPath / dirEntryPath.filename(); |
| 77 |
1/1✓ Branch 1 taken 22 times.
|
22 | openstudio::filesystem::copy_file(dirEntryPath, targetPath, openstudio::filesystem::copy_options::overwrite_existing); |
| 78 |
1/1✓ Branch 2 taken 22 times.
|
22 | copiedEnergyPlusFiles.emplace_back(std::move(targetPath)); |
| 79 | 22 | } else { | |
| 80 |
1/1✓ Branch 3 taken 242 times.
|
242 | auto lower_filename = openstudio::ascii_to_lower_copy(dirEntryPath.filename().string()); |
| 81 |
3/3✓ Branch 1 taken 242 times.
✓ Branch 3 taken 11 times.
✓ Branch 4 taken 231 times.
|
242 | if (boost::regex_match(lower_filename, matches, energyplusRegex)) { |
| 82 |
1/1✓ Branch 1 taken 11 times.
|
11 | energyPlusExe = dirEntryPath; |
| 83 |
3/3✓ Branch 1 taken 231 times.
✓ Branch 3 taken 11 times.
✓ Branch 4 taken 220 times.
|
231 | } else if (boost::regex_match(lower_filename, matches, expandObjectsRegex)) { |
| 84 |
1/1✓ Branch 1 taken 11 times.
|
11 | expandObjectsExe = dirEntryPath; |
| 85 | } | ||
| 86 | 242 | } | |
| 87 | 275 | } | |
| 88 | |||
| 89 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
|
11 | if (energyPlusExe.empty()) { |
| 90 | ✗ | throw std::runtime_error(fmt::format("Could not find EnergyPlus executable in {}\n", energyPlusDirectory.string())); | |
| 91 | } | ||
| 92 | |||
| 93 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
|
11 | if (expandObjectsExe.empty()) { |
| 94 | ✗ | throw std::runtime_error(fmt::format("Could not find ExpandObjects executable in {}\n", energyPlusDirectory.string())); | |
| 95 | } | ||
| 96 | |||
| 97 | // for (const auto& filePath : copiedEnergyPlusFiles) { | ||
| 98 | // fmt::print("{}\n", filePath.string()); | ||
| 99 | // } | ||
| 100 | |||
| 101 |
6/6✓ Branch 1 taken 11 times.
✓ Branch 4 taken 11 times.
✓ Branch 7 taken 11 times.
✓ Branch 10 taken 11 times.
✓ Branch 13 taken 11 times.
✓ Branch 16 taken 11 times.
|
11 | LOG(Info, "EnergyPlus executable path is " << energyPlusExe); |
| 102 |
6/6✓ Branch 1 taken 11 times.
✓ Branch 4 taken 11 times.
✓ Branch 7 taken 11 times.
✓ Branch 10 taken 11 times.
✓ Branch 13 taken 11 times.
✓ Branch 16 taken 11 times.
|
11 | LOG(Info, "ExpandObjects executable path is " << expandObjectsExe); |
| 103 | 11 | } | |
| 104 | |||
| 105 | PrepareRunDirResults(const PrepareRunDirResults&) noexcept = default; | ||
| 106 | PrepareRunDirResults(PrepareRunDirResults&&) noexcept = default; | ||
| 107 | PrepareRunDirResults& operator=(const PrepareRunDirResults&) noexcept = default; | ||
| 108 | PrepareRunDirResults& operator=(PrepareRunDirResults&&) noexcept = default; | ||
| 109 | |||
| 110 | 11 | ~PrepareRunDirResults() { | |
| 111 | 11 | LOG(Info, "Removing any copied EnergyPlus files"); | |
| 112 |
2/2✓ Branch 5 taken 22 times.
✓ Branch 6 taken 11 times.
|
33 | for (const auto& filePath : copiedEnergyPlusFiles) { |
| 113 | 22 | openstudio::filesystem::remove(filePath); | |
| 114 | } | ||
| 115 | |||
| 116 |
2/2✓ Branch 6 taken 22 times.
✓ Branch 7 taken 11 times.
|
77 | for (const auto& p : {m_runDirPath / "packaged_measures", m_runDirPath / "Energy+.ini"}) { |
| 117 | 22 | openstudio::filesystem::remove_all(p); | |
| 118 | 33 | } | |
| 119 | |||
| 120 | 11 | LOG(Debug, "Changing Current Directory back to: " << m_curDirPath); | |
| 121 | 11 | boost::filesystem::current_path(m_curDirPath); | |
| 122 | 11 | } | |
| 123 | |||
| 124 | private: | ||
| 125 | − | REGISTER_LOGGER("openstudio.OSWorkflow.prepareEnergyPlusDir"); | |
| 126 | openstudio::filesystem::path m_runDirPath; | ||
| 127 | openstudio::filesystem::path m_curDirPath = boost::filesystem::current_path(); | ||
| 128 | }; | ||
| 129 | |||
| 130 | 11 | void OSWorkflow::runEnergyPlus() { | |
| 131 | |||
| 132 | 11 | state = State::EnergyPlus; | |
| 133 | |||
| 134 | 11 | constexpr bool useBoostProcess = true; | |
| 135 | |||
| 136 |
2/3✓ Branch 1 taken 11 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11 times.
|
11 | if (runner.halted()) { |
| 137 | ✗ | LOG(Info, "Workflow halted, skipping the EnergyPlus simulation"); | |
| 138 | ✗ | return; | |
| 139 | } | ||
| 140 | |||
| 141 | // TODO: there is no point chdir'ing here. Just pass --output-directory to E+? | ||
| 142 |
1/1✓ Branch 1 taken 11 times.
|
11 | auto currentDir = boost::filesystem::current_path(); |
| 143 | |||
| 144 | // TODO: we need to think about exception handling... workflow gem is full of try catch, instead we could just use enum return types to indicate | ||
| 145 | // whether it failed or not or something | ||
| 146 | // Eg here I'm supposed to wrap all of the above in a try/catch, so I can ensure that clean_directory is called, then reraise the exception... | ||
| 147 | try { | ||
| 148 |
1/1✓ Branch 1 taken 11 times.
|
11 | auto runDirPath = workflowJSON.absoluteRunDir(); |
| 149 |
2/2✓ Branch 2 taken 11 times.
✓ Branch 5 taken 11 times.
|
11 | PrepareRunDirResults runDirResults(runDirPath); |
| 150 |
6/6✓ Branch 1 taken 11 times.
✓ Branch 4 taken 11 times.
✓ Branch 7 taken 11 times.
✓ Branch 10 taken 11 times.
✓ Branch 13 taken 11 times.
✓ Branch 16 taken 11 times.
|
11 | LOG(Info, "Starting simulation in run directory: " << runDirPath); |
| 151 | |||
| 152 |
1/1✓ Branch 1 taken 11 times.
|
22 | auto inIDF = runDirPath / "in.idf"; |
| 153 | // TODO: is this the right place /Do we want to do that if we chose epJSON? | ||
| 154 |
2/2✓ Branch 1 taken 11 times.
✓ Branch 4 taken 11 times.
|
33 | detailedTimeBlock("Saving IDF", [this, &inIDF] { workspace_->save(inIDF, true); }); |
| 155 | |||
| 156 |
3/3✓ Branch 1 taken 11 times.
✓ Branch 4 taken 11 times.
✓ Branch 7 taken 11 times.
|
22 | std::ofstream stdout_ofs(openstudio::toString(runDirPath / "stdout-energyplus"), std::ofstream::trunc); |
| 157 | |||
| 158 | // TODO: workflow-gem was manually running expandObjects prior to the potential serialization to json | ||
| 159 | // Should we rather pass -x to the E+ cmd line? | ||
| 160 |
4/5✓ Branch 1 taken 11 times.
✓ Branch 4 taken 11 times.
✓ Branch 7 taken 11 times.
✓ Branch 10 taken 11 times.
✗ Branch 11 not taken.
|
11 | if (!workflowJSON.runOptions()->skipExpandObjects()) { |
| 161 |
1/1✓ Branch 2 taken 11 times.
|
22 | const std::string cmd = fmt::format("\"{}\"", openstudio::toString(runDirResults.expandObjectsExe.native())); |
| 162 |
7/7✓ Branch 1 taken 11 times.
✓ Branch 4 taken 11 times.
✓ Branch 7 taken 11 times.
✓ Branch 10 taken 11 times.
✓ Branch 13 taken 11 times.
✓ Branch 16 taken 11 times.
✓ Branch 19 taken 11 times.
|
11 | LOG(Info, "Running command '" << cmd << "'"); |
| 163 | |||
| 164 | 11 | int result = 0; | |
| 165 |
2/2✓ Branch 1 taken 11 times.
✓ Branch 4 taken 11 times.
|
22 | detailedTimeBlock("Running ExpandObjects", [this, /*&cmd,*/ &result, &runDirResults, &stdout_ofs] { |
| 166 | // result = std::system(cmd.c_str()); | ||
| 167 | namespace bp = boost::process; | ||
| 168 |
1/1✓ Branch 1 taken 11 times.
|
11 | bp::ipstream is; |
| 169 | 11 | std::string line; | |
| 170 |
2/2✓ Branch 1 taken 11 times.
✓ Branch 4 taken 11 times.
|
11 | bp::child c(runDirResults.expandObjectsExe, bp::std_out > is); |
| 171 |
8/9✓ Branch 1 taken 44 times.
✓ Branch 3 taken 44 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 44 times.
✓ Branch 9 taken 44 times.
✓ Branch 11 taken 33 times.
✓ Branch 12 taken 11 times.
✓ Branch 13 taken 33 times.
✓ Branch 14 taken 11 times.
|
55 | while (c.running() && std::getline(is, line)) { |
| 172 |
3/3✓ Branch 2 taken 33 times.
✓ Branch 5 taken 33 times.
✓ Branch 8 taken 33 times.
|
33 | stdout_ofs << openstudio::ascii_trim_right(line) << '\n'; // Fix for windows... |
| 173 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 30 times.
|
33 | if (m_show_stdout) { |
| 174 | 6 | fmt::print("{}\n", line); | |
| 175 | } | ||
| 176 | } | ||
| 177 |
1/1✓ Branch 1 taken 11 times.
|
11 | c.wait(); |
| 178 | 11 | result = c.exit_code(); | |
| 179 | 11 | }); | |
| 180 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
|
11 | if (result != 0) { |
| 181 | ✗ | LOG(Warn, "ExpandObjects returned a non-zero exit code (" << result << ")."); | |
| 182 | } | ||
| 183 |
1/1✓ Branch 1 taken 11 times.
|
22 | auto expanded = runDirPath / "expanded.idf"; |
| 184 |
2/3✓ Branch 1 taken 11 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11 times.
|
11 | if (openstudio::filesystem::exists(expanded)) { |
| 185 | ✗ | if (openstudio::filesystem::is_regular_file(inIDF)) { | |
| 186 | ✗ | boost::filesystem::rename(inIDF, runDirPath / "pre-expand.idf"); | |
| 187 | ✗ | boost::filesystem::rename(expanded, inIDF); | |
| 188 | } | ||
| 189 | } | ||
| 190 | 11 | } | |
| 191 | |||
| 192 |
4/5✓ Branch 1 taken 11 times.
✓ Branch 4 taken 11 times.
✓ Branch 7 taken 11 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 11 times.
|
11 | if (workflowJSON.runOptions()->epjson()) { |
| 193 | ✗ | LOG(Info, "Beginning the translation to epJSON using OpenStudio"); | |
| 194 | ✗ | Json::Value jsonVal; | |
| 195 | |||
| 196 | ✗ | detailedTimeBlock("Translating to EnergyPlus epJSON", [this, &jsonVal]() { jsonVal = openstudio::epJSON::toJSON(workspace_.get()); }); | |
| 197 | |||
| 198 | ✗ | inIDF = runDirPath / openstudio::toPath("in.epJSON"); | |
| 199 | ✗ | if (openstudio::filesystem::is_regular_file(inIDF)) { | |
| 200 | ✗ | openstudio::filesystem::remove(inIDF); | |
| 201 | } | ||
| 202 | ✗ | detailedTimeBlock("Saving epJSON", [&jsonVal, &inIDF]() { | |
| 203 | ✗ | std::ofstream ofs(openstudio::toString(inIDF), std::ofstream::trunc); | |
| 204 | ✗ | ofs << jsonVal.toStyledString() << '\n'; | |
| 205 | ✗ | }); | |
| 206 | ✗ | } | |
| 207 | |||
| 208 | // TODO: eventually we should change this system call to be an API call to libenergyplusapi (but we need E+ to add cmake exports) | ||
| 209 | // cf https://github.com/NREL/EnergyPlus/pull/9712 and my proof of concept at https://github.com/jmarrec/EnergyPlus-Cpp-Demo | ||
| 210 |
1/1✓ Branch 3 taken 11 times.
|
22 | const std::string cmd = fmt::format("\"{}\" {}", openstudio::toString(runDirResults.energyPlusExe.native()), inIDF.filename().string()); |
| 211 |
7/7✓ Branch 1 taken 11 times.
✓ Branch 4 taken 11 times.
✓ Branch 7 taken 11 times.
✓ Branch 10 taken 11 times.
✓ Branch 13 taken 11 times.
✓ Branch 16 taken 11 times.
✓ Branch 19 taken 11 times.
|
11 | LOG(Info, "Running command '" << cmd << "'"); |
| 212 | |||
| 213 | // boost::process allows redirecting stdout / stderr easily, but I can no longer debug in LLDB, which is annoying | ||
| 214 | // Edit: actually std::system has the same issue... it captures a SIGVTALRM | ||
| 215 | // Disable with: `pro hand -p true -s false SIGVTALRM` | ||
| 216 | 11 | int result = 0; | |
| 217 | |||
| 218 | if constexpr (useBoostProcess) { | ||
| 219 |
2/2✓ Branch 1 taken 11 times.
✓ Branch 4 taken 11 times.
|
22 | detailedTimeBlock("Running EnergyPlus", [this, /*&cmd,*/ &result, &runDirResults, &inIDF, &stdout_ofs] { |
| 220 | // result = std::system(cmd.c_str()); | ||
| 221 | namespace bp = boost::process; | ||
| 222 |
1/1✓ Branch 1 taken 11 times.
|
11 | bp::ipstream is; |
| 223 | 11 | std::string line; | |
| 224 |
3/3✓ Branch 2 taken 11 times.
✓ Branch 5 taken 11 times.
✓ Branch 8 taken 11 times.
|
22 | bp::child c(runDirResults.energyPlusExe, inIDF.filename(), (bp::std_out & bp::std_err) > is); |
| 225 |
8/9✓ Branch 1 taken 1681 times.
✓ Branch 3 taken 1681 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 1681 times.
✓ Branch 9 taken 1681 times.
✓ Branch 11 taken 1670 times.
✓ Branch 12 taken 11 times.
✓ Branch 13 taken 1670 times.
✓ Branch 14 taken 11 times.
|
1692 | while (c.running() && std::getline(is, line)) { |
| 226 |
3/3✓ Branch 2 taken 1670 times.
✓ Branch 5 taken 1670 times.
✓ Branch 8 taken 1670 times.
|
1670 | stdout_ofs << openstudio::ascii_trim_right(line) << '\n'; // Fix for windows... |
| 227 |
2/2✓ Branch 0 taken 68 times.
✓ Branch 1 taken 1602 times.
|
1670 | if (m_show_stdout) { |
| 228 | 136 | fmt::print("{}\n", line); | |
| 229 | } | ||
| 230 | } | ||
| 231 |
1/1✓ Branch 1 taken 11 times.
|
11 | c.wait(); |
| 232 | 11 | result = c.exit_code(); | |
| 233 | 11 | }); | |
| 234 | } else { | ||
| 235 | detailedTimeBlock("Running EnergyPlus", [&cmd, &result] { result = std::system(cmd.c_str()); }); | ||
| 236 | } | ||
| 237 | |||
| 238 |
7/7✓ Branch 1 taken 11 times.
✓ Branch 4 taken 11 times.
✓ Branch 7 taken 11 times.
✓ Branch 10 taken 11 times.
✓ Branch 13 taken 11 times.
✓ Branch 16 taken 11 times.
✓ Branch 19 taken 11 times.
|
11 | LOG(Info, "EnergyPlus returned '" << result << "'"); |
| 239 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
|
11 | if (result != 0) { |
| 240 | ✗ | LOG(Warn, "EnergyPlus returned a non-zero exit code (" << result << "). Check the stdout-energyplus log"); | |
| 241 | } | ||
| 242 | |||
| 243 | { | ||
| 244 |
1/1✓ Branch 1 taken 11 times.
|
22 | auto errPath = runDirPath / "eplusout.err"; |
| 245 |
2/3✓ Branch 1 taken 11 times.
✓ Branch 3 taken 11 times.
✗ Branch 4 not taken.
|
11 | if (openstudio::filesystem::is_regular_file(errPath)) { |
| 246 | |||
| 247 |
1/1✓ Branch 1 taken 11 times.
|
11 | const auto errContent = openstudio::filesystem::read_as_string(errPath); |
| 248 |
1/1✓ Branch 1 taken 11 times.
|
11 | workflowJSON.setEplusoutErr(errContent); |
| 249 | |||
| 250 | // TODO: just do a search in the errContent? | ||
| 251 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
|
11 | if (errContent.find("EnergyPlus Terminated--Fatal Error Detected") != std::string::npos) { |
| 252 | ✗ | throw std::runtime_error("EnergyPlus Terminated with a Fatal Error. Check eplusout.err log."); | |
| 253 | } | ||
| 254 | |||
| 255 | // TODO: or we use ErrorFile... In which case what's the point of parsing the number of warnings/severe from eplusout.end? | ||
| 256 | // Actually, ErrorFile doesn't understand recurring warnings | ||
| 257 | // TODO: add a channel filter on the logger to avoid catching all the debug statements in the ErrorFile class | ||
| 258 |
1/1✓ Branch 1 taken 11 times.
|
11 | const openstudio::energyplus::ErrorFile errFile(errPath); |
| 259 |
3/4✓ Branch 1 taken 11 times.
✓ Branch 3 taken 11 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 11 times.
|
11 | std::string status = errFile.completedSuccessfully() ? "Completed Successfully" : "Failed"; |
| 260 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 10 times.
|
11 | if (m_show_stdout) { |
| 261 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
|
1 | if (m_style_stdout) { |
| 262 | ✗ | fmt::print("RunEnergyPlus: {} with ", status); | |
| 263 | ✗ | fmt::print(fmt::fg(fmt::color::red), "{} Fatal Errors, ", errFile.fatalErrors().size()); | |
| 264 | ✗ | fmt::print(fmt::fg(fmt::color::orange), "{} Severe Errors, ", errFile.severeErrors().size()); | |
| 265 | ✗ | fmt::print(fmt::fg(fmt::color::yellow), "{} Warnings.", errFile.warnings().size()); | |
| 266 | ✗ | fmt::print("\n"); | |
| 267 | } else { | ||
| 268 | 2 | fmt::print("RunEnergyPlus: {} with {} Fatal Errors, {} Severe Errors, {} Warnings.\n", status, errFile.fatalErrors().size(), | |
| 269 |
3/3✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 9 taken 1 time.
|
2 | errFile.severeErrors().size(), errFile.warnings().size()); |
| 270 | } | ||
| 271 | } | ||
| 272 |
2/3✓ Branch 1 taken 11 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11 times.
|
11 | if (!errFile.completedSuccessfully()) { |
| 273 | ✗ | throw std::runtime_error("EnergyPlus Terminated with a Fatal Error. Check eplusout.err log."); | |
| 274 | } | ||
| 275 | 11 | } else { | |
| 276 | ✗ | LOG(Warn, "EnergyPlus did not create an eplusout.err"); | |
| 277 | } | ||
| 278 | 11 | } | |
| 279 | |||
| 280 | { | ||
| 281 |
1/1✓ Branch 1 taken 11 times.
|
22 | auto endPath = runDirPath / "eplusout.end"; |
| 282 |
2/3✓ Branch 1 taken 11 times.
✓ Branch 3 taken 11 times.
✗ Branch 4 not taken.
|
11 | if (openstudio::filesystem::is_regular_file(endPath)) { |
| 283 |
1/1✓ Branch 1 taken 11 times.
|
11 | const auto endContent = openstudio::filesystem::read_as_string(endPath); |
| 284 |
3/7✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 11 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 11 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
|
11 | static const boost::regex warningRegex(R"((\d*).Warning)"); |
| 285 |
3/7✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 11 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 11 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
|
11 | static const boost::regex severeRegex(R"((\d*).Severe.Errors)"); |
| 286 |
1/1✓ Branch 1 taken 11 times.
|
11 | boost::smatch matches; |
| 287 | 11 | std::string nWarningStr; | |
| 288 |
2/3✓ Branch 1 taken 11 times.
✓ Branch 3 taken 11 times.
✗ Branch 4 not taken.
|
11 | if (boost::regex_search(endContent, matches, warningRegex)) { |
| 289 |
3/3✓ Branch 1 taken 11 times.
✓ Branch 4 taken 11 times.
✓ Branch 7 taken 11 times.
|
22 | nWarningStr = std::string(matches[1].first, matches[1].second); |
| 290 | } else { | ||
| 291 | ✗ | OS_ASSERT(false); | |
| 292 | } | ||
| 293 | 11 | std::string nSevereStr; | |
| 294 |
2/3✓ Branch 1 taken 11 times.
✓ Branch 3 taken 11 times.
✗ Branch 4 not taken.
|
11 | if (boost::regex_search(endContent, matches, severeRegex)) { |
| 295 |
3/3✓ Branch 1 taken 11 times.
✓ Branch 4 taken 11 times.
✓ Branch 7 taken 11 times.
|
22 | nSevereStr = std::string(matches[1].first, matches[1].second); |
| 296 | } else { | ||
| 297 | ✗ | OS_ASSERT(false); | |
| 298 | } | ||
| 299 |
9/9✓ Branch 1 taken 11 times.
✓ Branch 4 taken 11 times.
✓ Branch 7 taken 11 times.
✓ Branch 10 taken 11 times.
✓ Branch 13 taken 11 times.
✓ Branch 16 taken 11 times.
✓ Branch 19 taken 11 times.
✓ Branch 22 taken 11 times.
✓ Branch 25 taken 11 times.
|
11 | LOG(Info, "EnergyPlus finished with " << nWarningStr << " warnings and " << nSevereStr << " severe errors"); |
| 300 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
|
11 | if (endContent.find("EnergyPlus Terminated--Fatal Error Detected") != std::string::npos) { |
| 301 | ✗ | throw std::runtime_error("EnergyPlus Terminated with a Fatal Error. Check eplusout.err log."); | |
| 302 | } | ||
| 303 | |||
| 304 | 11 | } else { | |
| 305 | ✗ | throw std::runtime_error("EnergyPlus failed and did not create an eplusout.end file. Check the stdout-energyplus log."); | |
| 306 | } | ||
| 307 | 11 | } | |
| 308 | |||
| 309 |
0/2✗ Branch 10 not taken.
✗ Branch 11 not taken.
|
11 | } catch (const std::exception& e) { |
| 310 | ✗ | LOG_AND_THROW(e.what()); | |
| 311 | ✗ | } | |
| 312 | |||
| 313 | // Set the SQL path | ||
| 314 |
1/1✓ Branch 1 taken 11 times.
|
22 | auto expectedSqlPath = workflowJSON.absoluteRunDir() / "eplusout.sql"; |
| 315 |
2/3✓ Branch 1 taken 11 times.
✓ Branch 3 taken 11 times.
✗ Branch 4 not taken.
|
11 | if (openstudio::filesystem::is_regular_file(expectedSqlPath)) { |
| 316 |
1/1✓ Branch 1 taken 11 times.
|
11 | sqlPath = expectedSqlPath; |
| 317 | } else { | ||
| 318 | ✗ | LOG(Warn, "Unable to find sql file at " << expectedSqlPath); | |
| 319 | } | ||
| 320 | 11 | } | |
| 321 | |||
| 322 | } // namespace openstudio | ||
| 323 |