GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 80.5% 318 / 0 / 395
Functions: 86.4% 19 / 0 / 22
Branches: 71.5% 587 / 0 / 821

src/workflow/OSWorkflow.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 #include "Util.hpp"
8 #include "WorkflowRunOptions.hpp"
9
10 #include "../osversion/VersionTranslator.hpp"
11 #include "../measure/OSMeasure.hpp"
12 #include "../measure/ModelMeasure.hpp"
13 #include "../measure/EnergyPlusMeasure.hpp"
14 #include "../measure/ReportingMeasure.hpp"
15 #include "../measure/OSArgument.hpp"
16 #include "../measure/OSRunner.hpp"
17 #include "../model/Model.hpp"
18 #include "../model/Model_Impl.hpp"
19 #include "../utilities/bcl/BCLMeasure.hpp"
20 #include "../utilities/core/Assert.hpp"
21 #include "../utilities/core/Filesystem.hpp"
22 #include "../utilities/core/FileLogSink.hpp"
23 #include "../utilities/core/Json.hpp"
24 #include "../utilities/core/Logger.hpp"
25 #include "../utilities/data/Variant.hpp"
26 #include "../utilities/filetypes/WorkflowStep.hpp"
27 #include "../utilities/idf/Workspace.hpp"
28 #include "../energyplus/ForwardTranslator.hpp"
29
30 #include <fmt/color.h>
31 #include <fmt/chrono.h>
32 #include <fmt/format.h>
33 #include <fmt/ostream.h>
34 #include <json/json.h>
35
36 #include <array>
37 #include <chrono>
38 #include <limits>
39 #include <string_view>
40 #include <stdexcept>
41 #include <boost/filesystem/operations.hpp>
42
43 // TODO: this should really in Variant.hpp, but I'm getting pwned by the fact that ruby defines int128_t as a macro, no time to track it down
44 template <>
45 struct fmt::formatter<openstudio::Variant> : fmt::ostream_formatter
46 {
47 };
48
49 template <>
50 struct fmt::formatter<openstudio::path> : fmt::ostream_formatter
51 {
52 };
53
54 namespace openstudio {
55
56 ✗ OSWorkflow::OSWorkflow(const filesystem::path& oswPath, ScriptEngineInstance& ruby, ScriptEngineInstance& python)
57 :
58 #if USE_RUBY_ENGINE
59 ✗ rubyEngine(ruby),
60 #endif
61 #if USE_PYTHON_ENGINE
62 ✗ pythonEngine(python),
63 #endif
64 ✗ workflowJSON(oswPath) {
65
66 ✗ runner.setRegisterMsgAlsoLogs(true);
67 ✗ }
68
69 22 OSWorkflow::OSWorkflow(const WorkflowRunOptions& t_workflowRunOptions, ScriptEngineInstance& ruby, ScriptEngineInstance& python)
70 :
71 #if USE_RUBY_ENGINE
72 22 rubyEngine(ruby),
73 #endif
74 #if USE_PYTHON_ENGINE
75 22 pythonEngine(python),
76 #endif
77 22 workflowJSON(t_workflowRunOptions.osw_path),
78 22 m_no_simulation(t_workflowRunOptions.no_simulation),
79 22 m_post_process_only(t_workflowRunOptions.post_process_only),
80 22 m_show_stdout(t_workflowRunOptions.show_stdout),
81 22 m_add_timings(t_workflowRunOptions.add_timings),
82
2/2
✓ Branch 1 taken 22 times.
✓ Branch 4 taken 22 times.
22 m_style_stdout(t_workflowRunOptions.style_stdout) {
83
84
1/1
✓ Branch 1 taken 22 times.
22 runner.setRegisterMsgAlsoLogs(true);
85
86
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (m_add_timings) {
87 ✗ m_timers = std::make_unique<workflow::util::TimerCollection>();
88 }
89
90
10/21
✓ Branch 1 taken 22 times.
✓ Branch 3 taken 15 times.
✓ Branch 4 taken 7 times.
✓ Branch 6 taken 15 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 15 times.
✗ Branch 12 not taken.
✗ Branch 15 not taken.
✗ Branch 18 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✓ Branch 23 taken 22 times.
✓ Branch 25 taken 15 times.
✓ Branch 26 taken 7 times.
✓ Branch 28 taken 7 times.
✓ Branch 29 taken 15 times.
✗ Branch 30 not taken.
✗ Branch 31 not taken.
✗ Branch 33 not taken.
✗ Branch 34 not taken.
37 if (t_workflowRunOptions.runOptions.debug() || (workflowJSON.runOptions() && workflowJSON.runOptions()->debug())) {
91
6/6
✓ Branch 1 taken 7 times.
✓ Branch 4 taken 7 times.
✓ Branch 8 taken 7 times.
✓ Branch 13 taken 7 times.
✓ Branch 16 taken 7 times.
✓ Branch 19 taken 7 times.
14 LOG(Debug, fmt::format("Original workflowJSON={}\n", workflowJSON.string()));
92
1/1
✓ Branch 1 taken 7 times.
7 t_workflowRunOptions.debug_print();
93 }
94
1/1
✓ Branch 1 taken 22 times.
22 auto runOpt_ = workflowJSON.runOptions();
95
1/2
✓ Branch 1 taken 22 times.
✗ Branch 2 not taken.
22 if (!runOpt_) {
96
1/1
✓ Branch 1 taken 22 times.
22 workflowJSON.setRunOptions(t_workflowRunOptions.runOptions);
97 } else {
98 // user supplied CLI flags trump everything
99 ✗ runOpt_->overrideValuesWith(t_workflowRunOptions.runOptions);
100 }
101
102
5/5
✓ Branch 1 taken 22 times.
✓ Branch 4 taken 22 times.
✓ Branch 7 taken 22 times.
✓ Branch 10 taken 7 times.
✓ Branch 11 taken 15 times.
22 if (workflowJSON.runOptions()->debug()) {
103
1/1
✓ Branch 1 taken 7 times.
14 fmt::print("workflowJSON={}\n", workflowJSON.string());
104 }
105 22 }
106
107 19 void OSWorkflow::initializeWeatherFileFromOSW() {
108
5/5
✓ Branch 1 taken 19 times.
✓ Branch 4 taken 19 times.
✓ Branch 7 taken 19 times.
✓ Branch 10 taken 19 times.
✓ Branch 13 taken 19 times.
19 LOG(Debug, "Initialize the weather file from osw");
109
1/1
✓ Branch 1 taken 19 times.
19 auto epwPath_ = workflowJSON.weatherFile();
110
111
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 4 times.
19 if (epwPath_) {
112
7/7
✓ Branch 1 taken 15 times.
✓ Branch 4 taken 15 times.
✓ Branch 7 taken 15 times.
✓ Branch 10 taken 15 times.
✓ Branch 13 taken 15 times.
✓ Branch 16 taken 15 times.
✓ Branch 19 taken 15 times.
15 LOG(Debug, "Search for weather file defined by osw " << epwPath_.get());
113
2/2
✓ Branch 1 taken 15 times.
✓ Branch 4 taken 15 times.
15 auto epwFullPath_ = workflowJSON.findFile(epwPath_.get());
114
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
15 if (!epwFullPath_) {
115 ✗ auto epwFullPath_ = workflowJSON.findFile(epwPath_->filename());
116 ✗ }
117
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
15 if (!epwFullPath_) {
118 ✗ throw std::runtime_error(fmt::format("Weather file {} specified but cannot be found", epwPath_->string()));
119 }
120
121
2/2
✓ Branch 1 taken 15 times.
✓ Branch 4 taken 15 times.
15 epwPath = epwFullPath_.get();
122
123
2/3
✓ Branch 1 taken 15 times.
✓ Branch 3 taken 15 times.
✗ Branch 4 not taken.
30 if (auto epwFile_ = openstudio::EpwFile::load(epwPath)) {
124
2/2
✓ Branch 1 taken 15 times.
✓ Branch 4 taken 15 times.
15 model::WeatherFile::setWeatherFile(model, epwFile_.get());
125
1/1
✓ Branch 1 taken 15 times.
15 runner.setLastEpwFilePath(epwPath);
126 } else {
127 ✗ LOG(Warn, "Could not load weather file from " << epwPath_.get());
128 15 }
129 15 } else {
130
5/5
✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
✓ Branch 7 taken 4 times.
✓ Branch 10 taken 4 times.
✓ Branch 13 taken 4 times.
4 LOG(Debug, "No weather file specified in OSW, looking in model");
131
1/1
✓ Branch 1 taken 4 times.
4 updateLastWeatherFileFromModel();
132
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 if (epwPath.empty()) {
133
5/5
✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
✓ Branch 7 taken 4 times.
✓ Branch 10 taken 4 times.
✓ Branch 13 taken 4 times.
4 LOG(Warn, "No valid weather file defined in either the osm or osw.");
134 }
135 }
136 19 }
137
138 68 void OSWorkflow::updateLastWeatherFileFromModel() {
139
5/5
✓ Branch 1 taken 68 times.
✓ Branch 4 taken 68 times.
✓ Branch 7 taken 68 times.
✓ Branch 10 taken 68 times.
✓ Branch 13 taken 68 times.
68 LOG(Debug, "Find model's weather file and update LastEpwFilePath");
140
3/3
✓ Branch 1 taken 68 times.
✓ Branch 3 taken 60 times.
✓ Branch 4 taken 8 times.
136 if (auto epwFile_ = model.weatherFile()) {
141
3/4
✓ Branch 1 taken 60 times.
✓ Branch 4 taken 60 times.
✓ Branch 6 taken 60 times.
✗ Branch 7 not taken.
120 if (auto epwPath_ = epwFile_->path()) {
142
7/7
✓ Branch 1 taken 60 times.
✓ Branch 4 taken 60 times.
✓ Branch 7 taken 60 times.
✓ Branch 10 taken 60 times.
✓ Branch 13 taken 60 times.
✓ Branch 16 taken 60 times.
✓ Branch 19 taken 60 times.
60 LOG(Debug, "Search for weather file " << epwPath_.get());
143
2/2
✓ Branch 1 taken 60 times.
✓ Branch 4 taken 60 times.
60 auto epwFullPath_ = workflowJSON.findFile(epwPath_.get());
144
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 60 times.
60 if (!epwFullPath_) {
145 ✗ auto epwFullPath_ = workflowJSON.findFile(epwPath_->filename());
146 ✗ }
147
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 60 times.
60 if (!epwFullPath_) {
148 ✗ throw std::runtime_error(fmt::format("Weather file {} specified but cannot be found", epwPath_->string()));
149 }
150
151
2/2
✓ Branch 1 taken 60 times.
✓ Branch 4 taken 60 times.
60 epwPath = epwFullPath_.get();
152
1/1
✓ Branch 1 taken 60 times.
60 runner.setLastEpwFilePath(epwPath);
153
154 60 return;
155 120 }
156 68 }
157
158
5/5
✓ Branch 1 taken 8 times.
✓ Branch 4 taken 8 times.
✓ Branch 7 taken 8 times.
✓ Branch 10 taken 8 times.
✓ Branch 13 taken 8 times.
8 LOG(Debug, "Weather file is not defined by the model");
159 }
160
161 ✗ void OSWorkflow::saveModelicaFileToPath(const openstudio::path& filePath) {
162 ✗ if (!modelicaFile) {
163 ✗ return;
164 }
165
166 ✗ auto parent = filePath.parent_path();
167 ✗ if (!parent.empty()) {
168 ✗ openstudio::filesystem::create_directories(parent);
169 }
170
171 ✗ openstudio::filesystem::ofstream out(filePath);
172 ✗ if (!out.is_open()) {
173 ✗ throw std::runtime_error(fmt::format("Unable to write Modelica seed file to '{}'", filePath.generic_string()));
174 }
175 ✗ out << modelicaFile->getText();
176 ✗ out.close();
177
178 ✗ m_latestModelicaFilePath = boost::filesystem::absolute(filePath);
179 ✗ }
180
181 ✗ void OSWorkflow::saveModelicaFileSnapshot(const openstudio::path& directory) {
182 ✗ if (!modelicaFile || !m_modelicaSeedFileName) {
183 ✗ return;
184 }
185
186 ✗ if (!directory.empty()) {
187 ✗ openstudio::filesystem::create_directories(directory);
188 }
189
190 ✗ auto targetPath = directory / m_modelicaSeedFileName.get();
191 ✗ saveModelicaFileToPath(targetPath);
192 ✗ }
193
194 11 void OSWorkflow::applyArguments(measure::OSArgumentMap& argumentMap, const std::string& argumentName, const openstudio::Variant& argumentValue) {
195
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, "Setting argument value '" << argumentName << "' to '" << argumentValue << "'");
196
197 // if (!argumentValue.hasValue()) {
198 // fmt::print("Warn: Value for argument '{}' not set in argument list therefore will use default\n", argumentName);
199 // return;
200 // }
201
2/3
✓ Branch 1 taken 11 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11 times.
11 if (!argumentMap.contains(argumentName)) {
202 ✗ throw std::runtime_error(fmt::format("Could not find argument '{}' in argument_map\n", argumentName));
203 }
204
205
1/1
✓ Branch 1 taken 11 times.
11 auto& v = argumentMap.at(argumentName);
206
207 11 bool value_set = false;
208
1/1
✓ Branch 1 taken 11 times.
11 VariantType variantType = argumentValue.variantType();
209
210
3/3
✓ Branch 1 taken 11 times.
✓ Branch 4 taken 1 time.
✓ Branch 5 taken 10 times.
11 if (variantType == VariantType::String) {
211
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
1 value_set = v.setValue(argumentValue.valueAsString());
212
3/3
✓ Branch 1 taken 10 times.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 6 times.
10 } else if (variantType == VariantType::Double) {
213
2/2
✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
4 value_set = v.setValue(argumentValue.valueAsDouble());
214
2/3
✓ Branch 1 taken 6 times.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
6 } else if (variantType == VariantType::Integer) {
215
2/2
✓ Branch 1 taken 6 times.
✓ Branch 4 taken 6 times.
6 value_set = v.setValue(argumentValue.valueAsInteger());
216 ✗ } else if (variantType == VariantType::Boolean) {
217 ✗ value_set = v.setValue(argumentValue.valueAsBoolean());
218 }
219
220
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (!value_set) {
221 ✗ throw std::runtime_error(fmt::format("Could not set argument '{}' to value '{}': passed argument has type '{}' while '{}' was expected",
222 ✗ argumentName, argumentValue, argumentValue.variantType().valueName(), v.type().valueName()));
223 }
224 11 }
225
226 12 void OSWorkflow::saveOSMToRootDirIfDebug() {
227
11/18
✓ Branch 1 taken 12 times.
✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 12 times.
✓ Branch 10 taken 12 times.
✓ Branch 13 taken 12 times.
✓ Branch 15 taken 10 times.
✓ Branch 16 taken 2 times.
✓ Branch 17 taken 12 times.
✗ Branch 18 not taken.
✓ Branch 20 taken 12 times.
✗ Branch 21 not taken.
✓ Branch 23 taken 10 times.
✓ Branch 24 taken 2 times.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
12 if (!workflowJSON.runOptions() || !workflowJSON.runOptions()->debug()) {
228 10 return;
229 }
230
231
5/5
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 2 times.
✓ Branch 10 taken 2 times.
✓ Branch 13 taken 2 times.
2 LOG(Info, "Saving OSM to Root Directory");
232
1/1
✓ Branch 1 taken 2 times.
4 auto savePath = workflowJSON.absoluteRootDir() / "in.osm";
233
2/2
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
4 detailedTimeBlock("Saving OSM", [this, &savePath]() {
234 // TODO: workflow gem was actually serializating via model.to_s for speed...
235 2 model.save(savePath, true);
236 2 });
237
6/6
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 2 times.
✓ Branch 10 taken 2 times.
✓ Branch 13 taken 2 times.
✓ Branch 16 taken 2 times.
2 LOG(Info, "Saved OSM as " << savePath);
238 2 }
239
240 36 void OSWorkflow::saveIDFToRootDirIfDebug() {
241
11/18
✓ Branch 1 taken 36 times.
✓ Branch 4 taken 36 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 36 times.
✓ Branch 10 taken 36 times.
✓ Branch 13 taken 36 times.
✓ Branch 15 taken 30 times.
✓ Branch 16 taken 6 times.
✓ Branch 17 taken 36 times.
✗ Branch 18 not taken.
✓ Branch 20 taken 36 times.
✗ Branch 21 not taken.
✓ Branch 23 taken 30 times.
✓ Branch 24 taken 6 times.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
36 if (!workflowJSON.runOptions() || !workflowJSON.runOptions()->debug()) {
242 30 return;
243 }
244
5/5
✓ Branch 1 taken 6 times.
✓ Branch 4 taken 6 times.
✓ Branch 7 taken 6 times.
✓ Branch 10 taken 6 times.
✓ Branch 13 taken 6 times.
6 LOG(Info, "Saving IDF to Root Directory");
245
1/1
✓ Branch 1 taken 6 times.
12 auto savePath = workflowJSON.absoluteRootDir() / "in.idf";
246
2/2
✓ Branch 1 taken 6 times.
✓ Branch 4 taken 6 times.
12 detailedTimeBlock("Saving IDF To Root Directory (debug)", [this, &savePath]() {
247 // TODO: workflow gem was actually serializating via model.to_s for speed...
248 6 workspace_->save(savePath, true);
249 6 });
250
6/6
✓ Branch 1 taken 6 times.
✓ Branch 4 taken 6 times.
✓ Branch 7 taken 6 times.
✓ Branch 10 taken 6 times.
✓ Branch 13 taken 6 times.
✓ Branch 16 taken 6 times.
6 LOG(Info, "Saved IDF as " << savePath);
251 6 }
252
253 18 void standardFormatterWithStringSeverity(boost::log::record_view const& rec, boost::log::formatting_ostream& strm) {
254
255 static constexpr std::array<std::string_view, 6> logLevelStrs = {"Trace", "Debug", "Info", "Warn", "Error", "Fatal"};
256 // static constexpr std::array<std::string_view, 6> logLevelStrs = {"TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL"};
257
258 18 LogLevel logLevel = Trace;
259
3/4
✓ Branch 1 taken 18 times.
✓ Branch 4 taken 18 times.
✓ Branch 7 taken 18 times.
✗ Branch 8 not taken.
36 if (auto logLevel_ = boost::log::extract<LogLevel>("Severity", rec)) {
260 18 logLevel = logLevel_.get();
261 }
262
263 // if (auto pt_ = boost::log::extract<boost::posix_time::ptime>("TimeStamp", rec)) {
264 // // TimeStamp as [%H:%M:%S.%f]
265 // strm << "[" << boost::posix_time::to_simple_string(pt_.get().time_of_day()) << "] ";
266 // }
267
3/3
✓ Branch 2 taken 18 times.
✓ Branch 5 taken 18 times.
✓ Branch 8 taken 18 times.
18 strm << "[" << boost::log::extract<LogChannel>("Channel", rec) << "] <" //
268
2/2
✓ Branch 1 taken 18 times.
✓ Branch 5 taken 18 times.
18 << logLevelStrs[static_cast<size_t>(logLevel) - static_cast<size_t>(LogLevel::Trace)]
269 << "> "
270 // Finally, the record message
271
3/3
✓ Branch 1 taken 18 times.
✓ Branch 4 taken 18 times.
✓ Branch 7 taken 18 times.
18 << rec[boost::log::expressions::smessage];
272 18 }
273
274 22 bool OSWorkflow::run() {
275
276 // If the user passed something like `openstudio --loglevel Trace run --debug -w workflow.osw`, we retain the Trace
277
4/4
✓ Branch 1 taken 22 times.
✓ Branch 4 taken 22 times.
✓ Branch 7 taken 22 times.
✓ Branch 10 taken 22 times.
22 LogLevel oriLogLevel = openstudio::Logger::instance().standardOutLogger().logLevel().value_or(Warn);
278
5/5
✓ Branch 1 taken 22 times.
✓ Branch 4 taken 22 times.
✓ Branch 7 taken 22 times.
✓ Branch 9 taken 7 times.
✓ Branch 10 taken 15 times.
22 LogLevel targetLogLevel = workflowJSON.runOptions()->debug() ? Debug : oriLogLevel;
279
280
2/2
✓ Branch 1 taken 22 times.
✓ Branch 4 taken 22 times.
22 openstudio::Logger::instance().addTimeStampToLogger(); // Needed for run.log formatting
281
4/4
✓ Branch 1 taken 22 times.
✓ Branch 4 taken 22 times.
✓ Branch 7 taken 22 times.
✓ Branch 10 taken 22 times.
22 openstudio::Logger::instance().standardOutLogger().setFormatter(&standardFormatterWithStringSeverity);
282
283 // TODO: ideally we want stdErr logger to always receive Error and Fatal
284 // and stdOut logger should receive all the others. This is definitely doable (cf LogSink::updateFilter) but now is not the time.
285
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 4 times.
22 if (!m_show_stdout) {
286
3/3
✓ Branch 1 taken 18 times.
✓ Branch 4 taken 18 times.
✓ Branch 7 taken 18 times.
18 openstudio::Logger::instance().standardOutLogger().setLogLevel(Error); // Still show errors
287 } else {
288
3/3
✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
✓ Branch 7 taken 4 times.
4 openstudio::Logger::instance().standardOutLogger().setLogLevel(targetLogLevel);
289 }
290
291 // Need to recreate the runDir as fast as possible, so I can direct a file log sink there
292 22 bool hasDeletedRunDir = false;
293
1/1
✓ Branch 1 taken 22 times.
22 auto runDirPath = workflowJSON.absoluteRunDir();
294
295
2/3
✓ Branch 1 taken 22 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 22 times.
44 if (runDirPath == workflowJSON.oswDir()) {
296 ✗ workflowJSON.runOptions()->setPreserveRunDir(true);
297 }
298
299
9/13
✓ Branch 1 taken 22 times.
✓ Branch 4 taken 22 times.
✓ Branch 7 taken 22 times.
✓ Branch 9 taken 22 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 16 times.
✓ Branch 12 taken 6 times.
✓ Branch 13 taken 22 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 16 times.
✓ Branch 17 taken 6 times.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
22 if (!workflowJSON.runOptions()->preserveRunDir() && !m_post_process_only) {
300 // We don't have a run_dir argument anyways
301
3/3
✓ Branch 1 taken 16 times.
✓ Branch 3 taken 14 times.
✓ Branch 4 taken 2 times.
16 if (openstudio::filesystem::is_directory(runDirPath)) {
302 14 hasDeletedRunDir = true;
303
1/1
✓ Branch 1 taken 14 times.
14 openstudio::filesystem::remove_all(runDirPath);
304 }
305 }
306
3/3
✓ Branch 1 taken 22 times.
✓ Branch 3 taken 16 times.
✓ Branch 4 taken 6 times.
22 if (!openstudio::filesystem::is_directory(runDirPath)) {
307
1/1
✓ Branch 1 taken 16 times.
16 openstudio::filesystem::create_directories(runDirPath);
308 }
309
310
2/2
✓ Branch 1 taken 22 times.
✓ Branch 4 taken 22 times.
44 FileLogSink logFile(runDirPath / "run.log");
311 22 constexpr bool use_workflow_gem_fmt = true;
312 22 constexpr bool include_channel = true; // or workflowJSON.runOptions()->debug();
313
1/1
✓ Branch 1 taken 22 times.
22 logFile.useWorkflowGemFormatter(use_workflow_gem_fmt, include_channel);
314
1/1
✓ Branch 1 taken 22 times.
22 logFile.setLogLevel(targetLogLevel);
315
316
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 8 times.
22 if (hasDeletedRunDir) {
317
6/6
✓ Branch 1 taken 14 times.
✓ Branch 4 taken 14 times.
✓ Branch 7 taken 14 times.
✓ Branch 10 taken 14 times.
✓ Branch 13 taken 14 times.
✓ Branch 16 taken 14 times.
14 LOG(Debug, "Removing existing run directory: " << runDirPath);
318 }
319
320 // Communicate that the workflow has been started
321
5/5
✓ Branch 1 taken 22 times.
✓ Branch 4 taken 22 times.
✓ Branch 7 taken 22 times.
✓ Branch 10 taken 22 times.
✓ Branch 13 taken 22 times.
22 LOG(Debug, "Registering that the workflow has started with the adapter");
322 {
323 // @output_adapter.communicate_started
324
2/2
✓ Branch 1 taken 22 times.
✓ Branch 4 taken 22 times.
44 openstudio::filesystem::ofstream file(runDirPath / "started.job");
325
1/3
✗ Branch 1 not taken.
✓ Branch 2 taken 22 times.
✗ Branch 4 not taken.
22 OS_ASSERT(file.is_open());
326
1/1
✓ Branch 3 taken 22 times.
44 file << fmt::format("Started Workflow {}\n", std::chrono::system_clock::now());
327
1/1
✓ Branch 1 taken 22 times.
22 file.close();
328 22 }
329
330 // bool no_simulation = false;
331 // bool post_process = false;
332 // bool ep_json = false;
333 // bool show_stdout = false;
334 // bool add_timings = false;
335 // bool style_stdout = false;
336 // unsigned socket_port = 0;
337
338 //// O. Need to apply measure steps IN ORDER. (eg: OpenStudio Measures before Eplus measures etc)
339 //// https://github.com/NREL/OpenStudio-workflow-gem/blob/develop/lib/openstudio/workflow/util/measure.rb
340
341 129 auto timeJob = [this](memJobFunPtr job, const std::string& message) {
342
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 129 times.
129 if (m_style_stdout) {
343 ✗ fmt::print(fmt::fg(fmt::color::green),
344 "\n"
345 "┌{0:─^{2}}┐\n"
346 "│{1: ^{2}}│\n"
347 "└{0:─^{2}}┘",
348 ✗ "", std::string("Starting State ") + message, 80);
349 ✗ fmt::print("\n");
350 }
351
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 129 times.
129 if (m_add_timings) {
352 ✗ m_timers->newTimer(message);
353 }
354
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 129 times.
129 (this->*job)();
355
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 124 times.
124 if (m_add_timings) {
356 ✗ m_timers->tockCurrentTimer();
357 }
358
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 124 times.
124 if (m_style_stdout) {
359 ✗ fmt::print(fmt::fg(fmt::color::green), "{0:#^80}", std::string("Returned from State ") + message);
360 ✗ fmt::print("\n");
361 }
362 124 };
363
364 struct JobInfo
365 {
366 memJobFunPtr jobFun;
367 bool selected = true;
368 };
369
370 struct JobMap
371 {
372 std::array<std::pair<std::string_view, JobInfo>, 11> data;
373
374 84 [[nodiscard]] JobInfo& at(const std::string_view& key) {
375 791 auto itr = std::find_if(std::begin(data), std::end(data), [&key](const auto& v) { return v.first == key; });
376
1/2
✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
168 if (itr != std::end(data)) {
377 84 return itr->second;
378 } else {
379 ✗ throw std::range_error("Not Found");
380 }
381 }
382
383 // Class methods
384 auto cbegin() const {
385 return data.cbegin();
386 }
387 auto cend() const {
388 return data.cend();
389 }
390 auto begin() const {
391 return cbegin();
392 }
393 auto end() const {
394 return cend();
395 }
396 22 auto begin() {
397 22 return data.begin();
398 }
399 22 auto end() {
400 22 return data.end();
401 }
402 };
403
404 // Can't use a regular map, it's not retaining order
405 static constexpr std::array<std::pair<std::string_view, JobInfo>, 11> known_jobs{{
406 {"Initialization", {.jobFun = &OSWorkflow::runInitialization, .selected = true}},
407 {"OpenStudioMeasures", {.jobFun = &OSWorkflow::runOpenStudioMeasures, .selected = true}},
408 {"Translator", {.jobFun = &OSWorkflow::runTranslator, .selected = true}},
409 {"EnergyPlusMeasures", {.jobFun = &OSWorkflow::runEnergyPlusMeasures, .selected = true}},
410 {"ModelicaMeasures", {.jobFun = &OSWorkflow::runModelicaMeasures, .selected = false}},
411 {"PreProcess", {.jobFun = &OSWorkflow::runPreProcess, .selected = true}},
412 {"EnergyPlus", {.jobFun = &OSWorkflow::runEnergyPlus, .selected = true}},
413 {"Modelica", {.jobFun = &OSWorkflow::runModelica, .selected = false}},
414 {"ReportingMeasures", {.jobFun = &OSWorkflow::runReportingMeasures, .selected = true}},
415 {"PostProcess", {.jobFun = &OSWorkflow::runPostProcess, .selected = true}},
416 {"Cleanup", {.jobFun = &OSWorkflow::runCleanup, .selected = true}},
417 }};
418
419 22 JobMap jobMap{{known_jobs}};
420
421
2/3
✓ Branch 1 taken 22 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 22 times.
44 if (workflowJSON.seedModelicaModel()) {
422 ✗ jobMap.at("Initialization").selected = true;
423 ✗ jobMap.at("OpenStudioMeasures").selected = true;
424 ✗ jobMap.at("Translator").selected = true;
425 ✗ jobMap.at("EnergyPlusMeasures").selected = true;
426 ✗ jobMap.at("ModelicaMeasures").selected = true;
427 ✗ jobMap.at("PreProcess").selected = true;
428 ✗ jobMap.at("EnergyPlus").selected = false;
429 ✗ jobMap.at("Modelica").selected = true;
430 ✗ jobMap.at("ReportingMeasures").selected = true;
431 ✗ jobMap.at("PostProcess").selected = false;
432 ✗ jobMap.at("Cleanup").selected = true;
433
434 ✗ workflowJSON.runOptions()->forwardTranslatorOptions().setExcludeSpaceTranslation(true);
435
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 21 times.
22 } else if (m_no_simulation) {
436
1/1
✓ Branch 2 taken 1 time.
1 jobMap.at("Initialization").selected = true;
437
1/1
✓ Branch 2 taken 1 time.
1 jobMap.at("OpenStudioMeasures").selected = true;
438
1/1
✓ Branch 2 taken 1 time.
1 jobMap.at("Translator").selected = true;
439
1/1
✓ Branch 2 taken 1 time.
1 jobMap.at("EnergyPlusMeasures").selected = true;
440
1/1
✓ Branch 2 taken 1 time.
1 jobMap.at("ModelicaMeasures").selected = false;
441
1/1
✓ Branch 2 taken 1 time.
1 jobMap.at("PreProcess").selected = true;
442
1/1
✓ Branch 2 taken 1 time.
1 jobMap.at("EnergyPlus").selected = false;
443
1/1
✓ Branch 2 taken 1 time.
1 jobMap.at("Modelica").selected = false;
444
1/1
✓ Branch 2 taken 1 time.
1 jobMap.at("ReportingMeasures").selected = false;
445
1/1
✓ Branch 2 taken 1 time.
1 jobMap.at("PostProcess").selected = true;
446
1/1
✓ Branch 2 taken 1 time.
1 jobMap.at("Cleanup").selected = true;
447
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 15 times.
21 } else if (m_post_process_only) {
448
1/1
✓ Branch 2 taken 6 times.
6 jobMap.at("Initialization").selected = true;
449
1/1
✓ Branch 2 taken 6 times.
6 jobMap.at("OpenStudioMeasures").selected = false;
450
1/1
✓ Branch 2 taken 6 times.
6 jobMap.at("Translator").selected = false;
451
1/1
✓ Branch 2 taken 6 times.
6 jobMap.at("EnergyPlusMeasures").selected = false;
452
1/1
✓ Branch 2 taken 6 times.
6 jobMap.at("ModelicaMeasures").selected = false;
453
1/1
✓ Branch 2 taken 6 times.
6 jobMap.at("PreProcess").selected = false;
454
1/1
✓ Branch 2 taken 6 times.
6 jobMap.at("EnergyPlus").selected = false;
455
1/1
✓ Branch 2 taken 6 times.
6 jobMap.at("Modelica").selected = false;
456
1/1
✓ Branch 2 taken 6 times.
6 jobMap.at("ReportingMeasures").selected = true;
457
1/1
✓ Branch 2 taken 6 times.
6 jobMap.at("PostProcess").selected = true;
458
1/1
✓ Branch 2 taken 6 times.
6 jobMap.at("Cleanup").selected = true;
459
460
3/3
✓ Branch 1 taken 6 times.
✓ Branch 4 taken 6 times.
✓ Branch 7 taken 6 times.
6 workflowJSON.runOptions()->setPreserveRunDir(true);
461 }
462
463
13/20
✓ Branch 1 taken 22 times.
✓ Branch 4 taken 22 times.
✓ Branch 7 taken 22 times.
✓ Branch 9 taken 22 times.
✗ Branch 10 not taken.
✓ Branch 12 taken 22 times.
✓ Branch 15 taken 22 times.
✓ Branch 18 taken 22 times.
✓ Branch 20 taken 7 times.
✓ Branch 21 taken 15 times.
✓ Branch 22 taken 22 times.
✗ Branch 23 not taken.
✓ Branch 25 taken 22 times.
✗ Branch 26 not taken.
✓ Branch 28 taken 7 times.
✓ Branch 29 taken 15 times.
✗ Branch 30 not taken.
✗ Branch 31 not taken.
✗ Branch 33 not taken.
✗ Branch 34 not taken.
22 if (!workflowJSON.runOptions()->cleanup() || workflowJSON.runOptions()->debug()) {
464
1/1
✓ Branch 2 taken 7 times.
7 jobMap.at("Cleanup").selected = false;
465 }
466
467 22 std::string lastFatalError;
468
469
2/2
✓ Branch 4 taken 203 times.
✓ Branch 5 taken 18 times.
221 for (auto& [jobName, jobInfo] : jobMap) {
470
5/5
✓ Branch 1 taken 203 times.
✓ Branch 5 taken 203 times.
✓ Branch 9 taken 203 times.
✓ Branch 12 taken 203 times.
✓ Branch 15 taken 203 times.
406 LOG(Debug, fmt::format("{} - selected = {}\n", jobName, jobInfo.selected));
471
2/2
✓ Branch 0 taken 129 times.
✓ Branch 1 taken 74 times.
203 if (jobInfo.selected) {
472 try {
473
2/2
✓ Branch 1 taken 129 times.
✓ Branch 4 taken 124 times.
258 timeJob(jobInfo.jobFun, std::string{jobName});
474
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 } catch (std::exception& e) {
475
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (m_add_timings) {
476 ✗ m_timers->tockCurrentTimer();
477 }
478 10 lastFatalError = fmt::format("Found error in state '{}' with message: '{}'", jobName, e.what());
479
5/5
✓ Branch 1 taken 5 times.
✓ Branch 4 taken 5 times.
✓ Branch 7 taken 5 times.
✓ Branch 10 taken 5 times.
✓ Branch 13 taken 5 times.
5 LOG(Error, lastFatalError);
480 // Allow continuing anyways if it fails in reporting measures
481
2/2
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 1 time.
5 if (jobName != "ReportingMeasures") {
482 4 state = State::Errored;
483 4 break;
484 }
485 5 }
486 } else {
487
6/6
✓ Branch 1 taken 74 times.
✓ Branch 4 taken 74 times.
✓ Branch 7 taken 74 times.
✓ Branch 10 taken 74 times.
✓ Branch 13 taken 74 times.
✓ Branch 16 taken 74 times.
74 LOG(Info, "Skipping job " << jobName);
488 }
489 }
490
491 // No need to save the workspace (IDF) here, done before already (in RunPreProcess)
492
493
4/5
✓ Branch 1 taken 22 times.
✓ Branch 4 taken 22 times.
✓ Branch 7 taken 22 times.
✓ Branch 10 taken 22 times.
✗ Branch 11 not taken.
22 if (!workflowJSON.runOptions()->fast()) {
494
1/1
✓ Branch 1 taken 22 times.
22 communicateResults();
495 }
496
497
2/2
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 17 times.
22 if (!lastFatalError.empty()) {
498 // Because we allow RunReportingMeasures to fail so the workflow continues with the RunPostProcess / RunCleanup
499 // but we still want to return a failure
500 5 state = State::Errored;
501 10 fmt::print(stderr, "Failed to run workflow. Last Error:\n {}\n", lastFatalError);
502 }
503
504
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 17 times.
22 if (state == State::Errored) {
505
2/2
✓ Branch 1 taken 5 times.
✓ Branch 4 taken 5 times.
15 workflowJSON.setCompletedStatus("Fail");
506 } else {
507 // completed status will already be set if workflow was halted
508
2/3
✓ Branch 1 taken 17 times.
✓ Branch 5 taken 17 times.
✗ Branch 6 not taken.
17 if (!workflowJSON.completedStatus()) {
509
2/2
✓ Branch 1 taken 17 times.
✓ Branch 4 taken 17 times.
51 workflowJSON.setCompletedStatus("Success");
510 ✗ } else if (workflowJSON.completedStatus().get() == "Fail") {
511 ✗ state = State::Errored;
512 }
513 }
514
515
4/5
✓ Branch 1 taken 22 times.
✓ Branch 4 taken 22 times.
✓ Branch 7 taken 22 times.
✓ Branch 10 taken 22 times.
✗ Branch 11 not taken.
22 if (!workflowJSON.runOptions()->fast()) {
516 // Save workflow
517
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (m_add_timings) {
518 ✗ m_timers->newTimer("Save WorkflowJSON out.osw");
519 }
520
2/2
✓ Branch 1 taken 22 times.
✓ Branch 4 taken 22 times.
22 workflowJSON.saveAs(workflowJSON.absoluteOutPath());
521
5/5
✓ Branch 1 taken 22 times.
✓ Branch 4 taken 22 times.
✓ Branch 7 taken 22 times.
✓ Branch 10 taken 7 times.
✓ Branch 11 taken 15 times.
22 if (workflowJSON.runOptions()->debug()) {
522
1/1
✓ Branch 1 taken 7 times.
14 fmt::print("workflowJSON={}\n", workflowJSON.string());
523 }
524
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (m_add_timings) {
525 ✗ m_timers->tockCurrentTimer();
526 }
527 }
528
529
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 17 times.
22 if (state == State::Errored) {
530 // TODO: communicate_failure
531
2/2
✓ Branch 1 taken 5 times.
✓ Branch 4 taken 5 times.
10 openstudio::filesystem::ofstream file(runDirPath / "failed.job");
532
1/3
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 4 not taken.
5 OS_ASSERT(file.is_open());
533
1/1
✓ Branch 3 taken 5 times.
10 file << fmt::format("Failed Workflow {}\n", std::chrono::system_clock::now());
534
1/1
✓ Branch 1 taken 5 times.
5 file.close();
535 5 } else {
536 // TODO: communicate_complete
537
2/2
✓ Branch 1 taken 17 times.
✓ Branch 4 taken 17 times.
34 openstudio::filesystem::ofstream file(runDirPath / "finished.job");
538
1/3
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
✗ Branch 4 not taken.
17 OS_ASSERT(file.is_open());
539
1/1
✓ Branch 3 taken 17 times.
34 file << fmt::format("Finished Workflow {}\n", std::chrono::system_clock::now());
540
1/1
✓ Branch 1 taken 17 times.
17 file.close();
541 17 state = State::Finished;
542 17 }
543
544
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (m_add_timings) {
545 ✗ fmt::print("\nTiming:\n\n{}\n", m_timers->timeReport());
546 // TODO: create profile.json in the run folder
547 }
548
549 22 return (state == State::Finished);
550 22 }
551
552 78 Json::Value outputAttributesToJSON(const std::map<std::string, std::map<std::string, openstudio::Variant>>& output_attributes,
553 bool sanitize = false) {
554 78 Json::Value root(Json::objectValue);
555
2/2
✓ Branch 7 taken 124 times.
✓ Branch 8 taken 78 times.
202 for (const auto& [oriMeasureName, argMap] : output_attributes) {
556
7/9
✓ Branch 0 taken 62 times.
✓ Branch 1 taken 62 times.
✓ Branch 3 taken 62 times.
✓ Branch 6 taken 62 times.
✓ Branch 9 taken 62 times.
✓ Branch 11 taken 62 times.
✓ Branch 12 taken 62 times.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
124 const std::string measureName = sanitize ? openstudio::workflow::util::sanitizeKey(oriMeasureName) : oriMeasureName;
557
1/1
✓ Branch 1 taken 124 times.
124 Json::Value measureValues(Json::objectValue);
558
2/2
✓ Branch 7 taken 270 times.
✓ Branch 8 taken 124 times.
394 for (const auto& [oriArgName, variantValue] : argMap) {
559
7/9
✓ Branch 0 taken 144 times.
✓ Branch 1 taken 126 times.
✓ Branch 3 taken 144 times.
✓ Branch 6 taken 144 times.
✓ Branch 9 taken 126 times.
✓ Branch 11 taken 144 times.
✓ Branch 12 taken 126 times.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
270 const std::string argName = sanitize ? openstudio::workflow::util::sanitizeKey(oriArgName) : oriArgName;
560
4/4
✓ Branch 1 taken 270 times.
✓ Branch 4 taken 270 times.
✓ Branch 7 taken 4 times.
✓ Branch 8 taken 266 times.
270 if (variantValue.variantType() == VariantType::String) {
561
3/3
✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
✓ Branch 7 taken 4 times.
4 measureValues[argName] = variantValue.valueAsString();
562
4/4
✓ Branch 1 taken 266 times.
✓ Branch 4 taken 266 times.
✓ Branch 7 taken 85 times.
✓ Branch 8 taken 181 times.
266 } else if (variantValue.variantType() == VariantType::Double) {
563
3/3
✓ Branch 1 taken 85 times.
✓ Branch 4 taken 85 times.
✓ Branch 7 taken 85 times.
85 measureValues[argName] = variantValue.valueAsDouble();
564
4/4
✓ Branch 1 taken 181 times.
✓ Branch 4 taken 181 times.
✓ Branch 7 taken 24 times.
✓ Branch 8 taken 157 times.
181 } else if (variantValue.variantType() == VariantType::Integer) {
565
3/3
✓ Branch 1 taken 24 times.
✓ Branch 4 taken 24 times.
✓ Branch 7 taken 24 times.
24 measureValues[argName] = variantValue.valueAsInteger();
566
3/4
✓ Branch 1 taken 157 times.
✓ Branch 4 taken 157 times.
✓ Branch 7 taken 157 times.
✗ Branch 8 not taken.
157 } else if (variantValue.variantType() == VariantType::Boolean) {
567
3/3
✓ Branch 1 taken 157 times.
✓ Branch 4 taken 157 times.
✓ Branch 7 taken 157 times.
157 measureValues[argName] = variantValue.valueAsBoolean();
568 }
569 270 }
570
571
2/2
✓ Branch 1 taken 124 times.
✓ Branch 4 taken 124 times.
124 root[measureName] = measureValues;
572 124 }
573 78 return root;
574 ✗ }
575
576 40 void OSWorkflow::communicateMeasureAttributes() const {
577
578
1/1
✓ Branch 1 taken 40 times.
40 const Json::Value root = outputAttributesToJSON(output_attributes, false);
579
1/1
✓ Branch 1 taken 40 times.
40 Json::StreamWriterBuilder wbuilder;
580 // mimic the old StyledWriter behavior:
581
3/3
✓ Branch 1 taken 40 times.
✓ Branch 4 taken 40 times.
✓ Branch 7 taken 40 times.
120 wbuilder["indentation"] = " ";
582
583
1/1
✓ Branch 1 taken 40 times.
40 const std::string result = Json::writeString(wbuilder, root);
584
585
1/1
✓ Branch 1 taken 40 times.
80 auto jsonPath = workflowJSON.absoluteRunDir() / "measure_attributes.json";
586
1/1
✓ Branch 1 taken 40 times.
40 openstudio::filesystem::ofstream file(jsonPath);
587
1/3
✗ Branch 1 not taken.
✓ Branch 2 taken 40 times.
✗ Branch 4 not taken.
40 OS_ASSERT(file.is_open());
588
1/1
✓ Branch 1 taken 40 times.
40 file << result;
589
1/1
✓ Branch 1 taken 40 times.
40 file.close();
590 40 }
591
592 16 void OSWorkflow::runExtractInputsAndOutputs() const {
593
1/1
✓ Branch 1 taken 16 times.
16 const Json::Value results = outputAttributesToJSON(output_attributes, true);
594
1/1
✓ Branch 1 taken 16 times.
16 Json::StreamWriterBuilder wbuilder;
595
3/3
✓ Branch 1 taken 16 times.
✓ Branch 4 taken 16 times.
✓ Branch 7 taken 16 times.
48 wbuilder["indentation"] = " ";
596
597 {
598
1/1
✓ Branch 1 taken 16 times.
16 const std::string result = Json::writeString(wbuilder, results);
599
600
1/1
✓ Branch 1 taken 16 times.
32 auto jsonPath = workflowJSON.absoluteRunDir() / "results.json";
601
1/1
✓ Branch 1 taken 16 times.
16 openstudio::filesystem::ofstream file(jsonPath);
602
1/3
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✗ Branch 4 not taken.
16 OS_ASSERT(file.is_open());
603
1/1
✓ Branch 1 taken 16 times.
16 file << result;
604
1/1
✓ Branch 1 taken 16 times.
16 file.close();
605 16 }
606
607
2/2
✓ Branch 1 taken 16 times.
✓ Branch 4 taken 16 times.
32 const auto osa_abs_path = workflowJSON.absoluteRootDir().parent_path() / "analysis.json";
608
3/3
✓ Branch 1 taken 16 times.
✓ Branch 3 taken 13 times.
✓ Branch 4 taken 3 times.
16 if (!openstudio::filesystem::is_regular_file(osa_abs_path)) {
609 13 return;
610 }
611
612
2/2
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
3 std::ifstream ifs(openstudio::toSystemFilename(osa_abs_path));
613
614
1/1
✓ Branch 1 taken 3 times.
3 Json::CharReaderBuilder rbuilder;
615 3 std::string formattedErrors;
616
617
1/1
✓ Branch 1 taken 3 times.
3 Json::Value analysis_json;
618
1/1
✓ Branch 1 taken 3 times.
3 const bool parsingSuccessful = Json::parseFromStream(rbuilder, ifs, &analysis_json, &formattedErrors);
619
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!parsingSuccessful) {
620 ✗ LOG_AND_THROW("OSA Analysis JSON '" << toString(osa_abs_path) << "' cannot be processed, " << formattedErrors);
621 }
622
623
3/4
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 3 times.
9 if (!openstudio::checkKeyAndType(analysis_json, "analysis", Json::objectValue)) {
624 ✗ return;
625 }
626
627
4/5
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 3 times.
9 if (!openstudio::checkKeyAndType(analysis_json["analysis"], "output_variables", Json::arrayValue)) {
628 ✗ return;
629 }
630
631
1/1
✓ Branch 1 taken 3 times.
3 Json::Value objectiveFunctions(Json::objectValue);
632
633
2/2
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
3 auto& outputVars = analysis_json["analysis"]["output_variables"];
634
7/7
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 9 times.
✓ Branch 10 taken 9 times.
✓ Branch 13 taken 12 times.
✓ Branch 15 taken 9 times.
✓ Branch 16 taken 3 times.
12 for (const auto& variable : outputVars) {
635
11/18
✓ Branch 1 taken 9 times.
✓ Branch 4 taken 9 times.
✓ Branch 6 taken 9 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 9 times.
✓ Branch 12 taken 9 times.
✓ Branch 14 taken 6 times.
✓ Branch 15 taken 3 times.
✓ Branch 16 taken 9 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 9 times.
✗ Branch 20 not taken.
✓ Branch 21 taken 6 times.
✓ Branch 22 taken 3 times.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
27 if (openstudio::checkKeyAndType(variable, "objective_function", Json::booleanValue) && variable["objective_function"].asBool()) {
636
2/2
✓ Branch 1 taken 6 times.
✓ Branch 4 taken 6 times.
12 assertKeyAndType(variable, "name", Json::stringValue);
637
2/2
✓ Branch 1 taken 6 times.
✓ Branch 4 taken 6 times.
12 assertKeyAndType(variable, "objective_function_index", Json::intValue);
638
2/2
✓ Branch 1 taken 6 times.
✓ Branch 4 taken 6 times.
6 const std::string name = variable["name"].asString();
639
2/2
✓ Branch 1 taken 6 times.
✓ Branch 4 taken 6 times.
6 const int idx = variable["objective_function_index"].asInt() + 1;
640
641
6/6
✓ Branch 1 taken 6 times.
✓ Branch 4 taken 6 times.
✓ Branch 7 taken 6 times.
✓ Branch 10 taken 6 times.
✓ Branch 13 taken 6 times.
✓ Branch 16 taken 6 times.
6 LOG(Info, "Looking for objective function " << name);
642
643 // Splitting on a `.` feels very unrealiable
644 6 const size_t pos = name.find('.');
645
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (pos == std::string::npos) {
646 ✗ LOG(Warn, "Objective function name='" << name << "' does not contain a dot (`.`)");
647 ✗ continue;
648 ✗ }
649
1/1
✓ Branch 1 taken 6 times.
6 const std::string measureName = name.substr(0, pos);
650
1/1
✓ Branch 1 taken 6 times.
6 const std::string argName = name.substr(pos + 1);
651
8/9
✓ Branch 1 taken 6 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 6 taken 3 times.
✓ Branch 9 taken 3 times.
✓ Branch 11 taken 3 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 3 times.
✓ Branch 14 taken 3 times.
6 if (results.isMember(measureName) && results[measureName].isMember(argName)) {
652
4/4
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
✓ Branch 10 taken 3 times.
6 objectiveFunctions[fmt::format("objective_function_{}", idx)] = results[measureName][argName];
653
654
3/4
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
✗ Branch 8 not taken.
9 if (openstudio::checkKeyAndType(variable, "objective_function_target", Json::realValue)) {
655
6/6
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
✓ Branch 10 taken 3 times.
✓ Branch 13 taken 3 times.
✓ Branch 16 taken 3 times.
3 LOG(Info, "Found objective function target for " << name);
656
4/4
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
✓ Branch 10 taken 3 times.
6 objectiveFunctions[fmt::format("objective_function_target_{}", idx)] = variable["objective_function_target"].asDouble();
657 }
658
659
3/4
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
✗ Branch 8 not taken.
9 if (openstudio::checkKeyAndType(variable, "scaling_factor", Json::realValue)) {
660
6/6
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
✓ Branch 10 taken 3 times.
✓ Branch 13 taken 3 times.
✓ Branch 16 taken 3 times.
3 LOG(Info, "Found scaling factor for " << name);
661
4/4
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
✓ Branch 10 taken 3 times.
6 objectiveFunctions[fmt::format("scaling_factor_{}", idx)] = variable["scaling_factor"].asDouble();
662 }
663
664
3/4
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
✗ Branch 8 not taken.
9 if (openstudio::checkKeyAndType(variable, "objective_function_group", Json::realValue)) {
665
6/6
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
✓ Branch 10 taken 3 times.
✓ Branch 13 taken 3 times.
✓ Branch 16 taken 3 times.
3 LOG(Info, "Found objective function group for " << name);
666
4/4
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
✓ Branch 10 taken 3 times.
6 objectiveFunctions[fmt::format("objective_function_group_{}", idx)] = variable["objective_function_group"].asDouble();
667 }
668
669 } else {
670
6/6
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
✓ Branch 10 taken 3 times.
✓ Branch 13 taken 3 times.
✓ Branch 16 taken 3 times.
3 LOG(Warn, "No results for objective function " << name);
671
2/2
✓ Branch 2 taken 3 times.
✓ Branch 5 taken 3 times.
6 objectiveFunctions[fmt::format("objective_function_{}", idx)] = std::numeric_limits<double>::max();
672
2/2
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
6 objectiveFunctions[fmt::format("objective_function_target_{}", idx)] = Json::nullValue;
673
2/2
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
6 objectiveFunctions[fmt::format("scaling_factor_{}", idx)] = Json::nullValue;
674
2/2
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
6 objectiveFunctions[fmt::format("objective_function_group_{}", idx)] = Json::nullValue;
675 }
676 6 }
677 }
678
679 {
680
1/1
✓ Branch 1 taken 3 times.
3 const std::string objectives = Json::writeString(wbuilder, objectiveFunctions);
681
682
1/1
✓ Branch 1 taken 3 times.
6 auto objectivesJsonPath = workflowJSON.absoluteRunDir() / "objectives.json";
683
1/1
✓ Branch 1 taken 3 times.
3 openstudio::filesystem::ofstream file(objectivesJsonPath);
684
1/3
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 4 not taken.
3 OS_ASSERT(file.is_open());
685
1/1
✓ Branch 1 taken 3 times.
3 file << objectives;
686
1/1
✓ Branch 1 taken 3 times.
3 file.close();
687 3 }
688 42 }
689
690 22 void OSWorkflow::communicateResults() const {
691
4/5
✓ Branch 1 taken 22 times.
✓ Branch 4 taken 22 times.
✓ Branch 7 taken 22 times.
✓ Branch 10 taken 22 times.
✗ Branch 11 not taken.
22 if (!workflowJSON.runOptions()->skipZipResults()) {
692
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (m_add_timings) {
693 ✗ m_timers->newTimer("Zip datapoint");
694 }
695
2/2
✓ Branch 1 taken 22 times.
✓ Branch 4 taken 22 times.
22 openstudio::workflow::util::zipResults(workflowJSON.absoluteRunDir());
696
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (m_add_timings) {
697 ✗ m_timers->tockCurrentTimer();
698 }
699 }
700
701
1/1
✓ Branch 1 taken 22 times.
22 const Json::Value root = outputAttributesToJSON(output_attributes, true);
702
1/1
✓ Branch 1 taken 22 times.
22 Json::StreamWriterBuilder wbuilder;
703 // mimic the old StyledWriter behavior:
704
3/3
✓ Branch 1 taken 22 times.
✓ Branch 4 taken 22 times.
✓ Branch 7 taken 22 times.
66 wbuilder["indentation"] = " ";
705
706
1/1
✓ Branch 1 taken 22 times.
22 const std::string result = Json::writeString(wbuilder, root);
707
708
1/1
✓ Branch 1 taken 22 times.
44 auto jsonPath = workflowJSON.absoluteRunDir() / "data_point_out.json";
709
1/1
✓ Branch 1 taken 22 times.
22 openstudio::filesystem::ofstream file(jsonPath);
710
1/3
✗ Branch 1 not taken.
✓ Branch 2 taken 22 times.
✗ Branch 4 not taken.
22 OS_ASSERT(file.is_open());
711
1/1
✓ Branch 1 taken 22 times.
22 file << result;
712
1/1
✓ Branch 1 taken 22 times.
22 file.close();
713 22 }
714
715 } // namespace openstudio
716