GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 52.7% 355 / 0 / 673
Functions: 35.9% 14 / 0 / 39
Branches: 46.4% 514 / 0 / 1108

src/airflow/contam/ForwardTranslator.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 "ForwardTranslator.hpp"
7 #include "../WindPressure.hpp"
8
9 #include "../../model/Model.hpp"
10 #include "../../model/Building.hpp"
11 #include "../../model/Building_Impl.hpp"
12 #include "../../model/BuildingStory.hpp"
13 #include "../../model/BuildingStory_Impl.hpp"
14 #include "../../model/ThermalZone.hpp"
15 #include "../../model/ThermalZone_Impl.hpp"
16 #include "../../model/Space.hpp"
17 #include "../../model/Space_Impl.hpp"
18 #include "../../model/Surface.hpp"
19 #include "../../model/Surface_Impl.hpp"
20 #include "../../model/SubSurface.hpp"
21 #include "../../model/SubSurface_Impl.hpp"
22 #include "../../model/AirLoopHVAC.hpp"
23 #include "../../model/AirLoopHVAC_Impl.hpp"
24 #include "../../model/Node.hpp"
25 #include "../../model/Node_Impl.hpp"
26 #include "../../model/PortList.hpp"
27 #include "../../model/WeatherFile.hpp"
28 #include "../../model/RunPeriod.hpp"
29
30 #include "../../utilities/time/Date.hpp"
31
32 #include "../../utilities/sql/SqlFile.hpp"
33 #include "../../utilities/core/Logger.hpp"
34 #include "../../utilities/geometry/Geometry.hpp"
35 #include "../../utilities/plot/ProgressBar.hpp"
36
37 #include <boost/math/constants/constants.hpp>
38
39 #include <airflow/embedded_files.hxx>
40
41 #include <fmt/format.h>
42
43 #include <algorithm>
44 #include <cmath>
45 #include <thread>
46
47 namespace openstudio {
48 namespace contam {
49
50
2/2
✓ Branch 3 taken 4 times.
✓ Branch 6 taken 4 times.
4 CvFile::CvFile() {
51
1/1
✓ Branch 1 taken 4 times.
4 clear();
52 4 }
53
54 4 void CvFile::clear() {
55
3/3
✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
✓ Branch 7 taken 4 times.
4 m_start = Date(MonthOfYear::Jan, 1);
56
3/3
✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
✓ Branch 7 taken 4 times.
4 m_end = Date(MonthOfYear::Dec, 31);
57 4 m_names = std::vector<std::string>();
58 4 m_series = std::vector<TimeSeries>();
59 4 }
60
61 ✗ void CvFile::addTimeSeries(std::string name, TimeSeries series) {
62 ✗ m_names.push_back(name);
63 ✗ m_series.push_back(series);
64 ✗ }
65
66 ✗ static std::string convertDateTime(const DateTime& datetime) {
67 ✗ return fmt::format("{:02d}/{:02d}\t{:02d}:{:02d}:{:02d}\n", month(datetime.date().monthOfYear()), datetime.date().dayOfMonth(),
68 ✗ datetime.time().hours(), datetime.time().minutes(), datetime.time().seconds());
69 }
70
71 ✗ bool CvFile::write(openstudio::path filepath) {
72 ✗ openstudio::filesystem::ofstream file{filepath};
73 ✗ if (file.good()) {
74 ✗ file << "ContinuousValuesFile ContamW 2.1\n";
75 ✗ file << "CVF file from E+ results\n";
76 ✗ file << month(m_start.monthOfYear()) << '/' << m_start.dayOfMonth() << '\t' << month(m_end.monthOfYear()) << '/' << m_end.dayOfMonth() << '\n';
77 ✗ file << m_names.size() << '\n';
78 ✗ for (const auto& m_name : m_names) {
79 ✗ file << m_name << '\n';
80 }
81 ✗ Time delta(0, 1, 0, 0); // Hard code hourly data for now
82 ✗ DateTime current(m_start);
83 ✗ DateTime last = current;
84 // Another hard code for 8760 hours, really not sure what to do with 1/1 00:00:00
85 ✗ file << convertDateTime(current);
86 ✗ for (unsigned int i = 0; i < m_names.size(); i++) {
87 ✗ double value = m_series[i].value(current + delta); // This works now...
88 ✗ if (m_series[i].units() == "C") {
89 ✗ value += 273.15;
90 }
91 ✗ file << '\t' << value;
92 }
93 ✗ file << '\n';
94 // And the big hardcoded loop
95 ✗ for (int j = 0; j < 8760; j++) {
96 ✗ current += delta;
97 // Mess with the time a little bit to put 24:00:00 in for 00:00:00
98 ✗ if (current.time().hours() == 0 && current.time().minutes() == 0 && current.time().seconds() == 0) {
99 ✗ file << fmt::format("{:02d}/{:02d}\t24:00:00", month(last.date().monthOfYear()), last.date().dayOfMonth());
100 } else {
101 ✗ file << convertDateTime(current);
102 }
103 ✗ last = current;
104 // Write out the data. May need more conversion here in the future.
105 ✗ for (unsigned int i = 0; i < m_names.size(); i++) {
106 ✗ double value = m_series[i].value(current);
107 ✗ if (m_series[i].units() == "C") {
108 ✗ value += 273.15;
109 }
110 ✗ file << '\t' << value;
111 }
112 ✗ file << '\n';
113 }
114 ✗ file.close();
115 ✗ return true;
116 }
117 ✗ return false;
118 ✗ }
119
120
3/3
✓ Branch 2 taken 4 times.
✓ Branch 11 taken 4 times.
✓ Branch 20 taken 4 times.
4 ForwardTranslator::ForwardTranslator() {
121
1/1
✓ Branch 1 taken 4 times.
4 m_logSink.setLogLevel(Warn);
122
2/2
✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
4 m_logSink.setChannelRegex(boost::regex("openstudio\\.contam\\.ForwardTranslator"));
123
1/1
✓ Branch 2 taken 4 times.
4 m_logSink.setThreadId(std::this_thread::get_id());
124
1/1
✓ Branch 1 taken 4 times.
4 clear();
125 4 }
126
127 4 void ForwardTranslator::clear() {
128 4 m_afeMap = std::map<std::string, int>();
129 4 m_levelMap = std::map<Handle, int>();
130 4 m_zoneMap = std::map<Handle, int>();
131 4 m_pathMap = std::map<std::string, int>();
132 4 m_surfaceMap = std::map<Handle, int>();
133 4 m_ahsMap = std::map<Handle, int>();
134
1/1
✓ Branch 1 taken 4 times.
4 m_leakageDescriptor = boost::optional<std::string>("Average");
135 4 m_returnSupplyRatio = 1.0;
136 4 m_ratioOverride = false;
137
1/1
✓ Branch 2 taken 4 times.
4 m_startDateTime = boost::optional<DateTime>();
138
1/1
✓ Branch 2 taken 4 times.
4 m_endDateTime = boost::optional<DateTime>();
139 4 m_translateHVAC = true;
140 4 }
141
142 ✗ int ForwardTranslator::tableLookup(std::map<std::string, int> map, std::string str, const char* name) {
143 ✗ const auto& it = map.find(str);
144 ✗ if (it == map.end()) {
145 ✗ LOG(Warn, "Unable to look up '" << str << "' in " << name);
146 }
147 ✗ return it->second;
148 }
149
150 62 int ForwardTranslator::tableLookup(std::map<Handle, int> map, Handle handle, const char* name) {
151
1/1
✓ Branch 1 taken 62 times.
62 const auto& it = map.find(handle);
152
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 62 times.
62 if (it == map.end()) {
153 ✗ LOG(Warn, "Unable to look up '" << handle << "' in " << name);
154 }
155 124 return it->second;
156 }
157
158 ✗ std::string ForwardTranslator::reverseLookup(std::map<std::string, int> map, int nr, const char* name) {
159 ✗ if (nr > 0) {
160 ✗ std::vector<std::string> keys;
161 ✗ for (const auto& pair : map) {
162 ✗ if (pair.second == nr) {
163 ✗ keys.push_back(pair.first);
164 }
165 }
166 ✗ if (!keys.empty()) {
167 ✗ if (keys.size() > 1) {
168 ✗ LOG(Warn, "Lookup table " << name << " contains multiple " << nr << " values");
169 }
170 ✗ return keys[0];
171 }
172 ✗ }
173 ✗ LOG(Warn, "Unable to reverse look up " << nr << " in " << name);
174 ✗ return {};
175 }
176
177 ✗ Handle ForwardTranslator::reverseLookup(std::map<Handle, int> map, int nr, const char* name) {
178 ✗ if (nr > 0) {
179 ✗ std::vector<Handle> keys;
180 ✗ for (const auto& pair : map) {
181 ✗ if (pair.second == nr) {
182 ✗ keys.push_back(pair.first);
183 }
184 }
185 ✗ if (!keys.empty()) {
186 ✗ if (keys.size() > 1) {
187 ✗ LOG(Warn, "Lookup table " << name << " contains multiple " << nr << " values");
188 }
189 ✗ return keys[0];
190 }
191 ✗ }
192 ✗ LOG(Warn, "Unable to reverse look up " << nr << " in " << name);
193 ✗ return {};
194 }
195
196 // Getters and setters
197 ✗ double ForwardTranslator::returnSupplyRatio() const {
198 ✗ return m_returnSupplyRatio;
199 }
200 ✗ void ForwardTranslator::setReturnSupplyRatio(double returnSupplyRatio) {
201 ✗ m_returnSupplyRatio = std::fabs(returnSupplyRatio);
202 ✗ }
203
204 ✗ bool ForwardTranslator::ratioOverride() const {
205 ✗ return m_ratioOverride;
206 }
207 ✗ void ForwardTranslator::setRatioOverride(bool ratioOverride) {
208 ✗ m_ratioOverride = ratioOverride;
209 ✗ }
210
211 ✗ bool ForwardTranslator::translateHVAC() const {
212 ✗ return m_translateHVAC;
213 }
214 1 void ForwardTranslator::setTranslateHVAC(bool translateHVAC) {
215 1 m_translateHVAC = translateHVAC;
216 1 }
217
218 ✗ boost::optional<DateTime> ForwardTranslator::startDateTime() const {
219 ✗ return m_startDateTime;
220 }
221
222 ✗ boost::optional<DateTime> ForwardTranslator::endDateTime() const {
223 ✗ return m_endDateTime;
224 }
225
226 ✗ boost::optional<std::string> ForwardTranslator::airtightnessLevel() const {
227 ✗ return m_leakageDescriptor;
228 }
229
230 1 void ForwardTranslator::setAirtightnessLevel(std::string level) {
231
1/1
✓ Branch 1 taken 1 time.
1 m_leakageDescriptor = boost::optional<std::string>(level);
232 1 m_flow = boost::optional<double>();
233 1 m_n = boost::optional<double>();
234 1 m_deltaP = boost::optional<double>();
235 1 }
236
237 6 bool ForwardTranslator::applyAirtightnessLevel(contam::IndexModel model) {
238 // For this to work, the "standard" names must be in the PRJ data
239
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if (!m_leakageDescriptor) {
240 ✗ return false;
241 }
242 6 std::map<std::string, int> afeMap;
243
2/4
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 6 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
30 std::vector<std::string> grade({"Leaky", "Average", "Tight"});
244
2/4
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 6 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
30 std::vector<std::string> wallExt({"ExtWallLeaky", "ExtWallAvg", "ExtWallTight"});
245
2/4
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 6 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
30 std::vector<std::string> wallInt({"IntWallLeaky", "IntWallAvg", "IntWallTight"});
246
2/4
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 6 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
30 std::vector<std::string> floor({"FloorLeaky", "FloorAvg", "FloorTight"});
247
2/4
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 6 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
24 std::vector<std::string> roof({"RoofLeaky", "RoofAvg", "RoofTight"});
248 6 int index = -1;
249
2/2
✓ Branch 1 taken 18 times.
✓ Branch 2 taken 6 times.
24 for (std::vector<std::string>::size_type i = 0; i < grade.size(); ++i) {
250
3/3
✓ Branch 1 taken 18 times.
✓ Branch 5 taken 6 times.
✓ Branch 6 taken 12 times.
18 if (grade[i] == m_leakageDescriptor.get()) {
251 6 index = i;
252 }
253 }
254
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (index == -1) // Default to average, unknown leakage level
255 {
256 ✗ LOG(Warn, "Unknown airtightness level '" << m_leakageDescriptor.get() << "', defaulting to 'Average'");
257 ✗ index = 1;
258 }
259 int nr;
260 // Exterior walls
261
2/2
✓ Branch 2 taken 6 times.
✓ Branch 5 taken 6 times.
6 nr = model.airflowElementNrByName(wallExt[index]);
262
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (nr == 0) {
263 ✗ return false;
264 }
265
2/2
✓ Branch 1 taken 6 times.
✓ Branch 4 taken 6 times.
12 afeMap["exterior"] = nr;
266 // Interior walls
267
2/2
✓ Branch 2 taken 6 times.
✓ Branch 5 taken 6 times.
6 nr = model.airflowElementNrByName(wallInt[index]);
268
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (nr == 0) {
269 ✗ return false;
270 }
271
2/2
✓ Branch 1 taken 6 times.
✓ Branch 4 taken 6 times.
12 afeMap["interior"] = nr;
272 // Floors
273
2/2
✓ Branch 2 taken 6 times.
✓ Branch 5 taken 6 times.
6 nr = model.airflowElementNrByName(floor[index]);
274
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (nr == 0) {
275 ✗ return false;
276 }
277
2/2
✓ Branch 1 taken 6 times.
✓ Branch 4 taken 6 times.
12 afeMap["floor"] = nr;
278 // Roof
279
2/2
✓ Branch 2 taken 6 times.
✓ Branch 5 taken 6 times.
6 nr = model.airflowElementNrByName(roof[index]);
280
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (nr == 0) {
281 ✗ return false;
282 }
283
2/2
✓ Branch 1 taken 6 times.
✓ Branch 4 taken 6 times.
12 afeMap["roof"] = nr;
284
1/1
✓ Branch 1 taken 6 times.
6 m_afeMap = afeMap;
285 6 return true;
286 96 }
287
288 ✗ boost::optional<double> ForwardTranslator::exteriorFlowRate() const {
289 ✗ return m_flow;
290 }
291
292 ✗ boost::optional<double> ForwardTranslator::exteriorExponent() const {
293 ✗ return m_n;
294 }
295
296 ✗ boost::optional<double> ForwardTranslator::exteriorDeltaP() const {
297 ✗ return m_deltaP;
298 }
299
300 ✗ bool ForwardTranslator::setExteriorFlowRate(double flow, double n, double deltaP) {
301 ✗ if (flow > 0 && n > 0 && deltaP > 0) {
302 ✗ m_flow = boost::optional<double>(flow);
303 ✗ m_n = boost::optional<double>(n);
304 ✗ m_deltaP = boost::optional<double>(deltaP);
305 ✗ m_leakageDescriptor = boost::optional<std::string>();
306 ✗ return true;
307 }
308 ✗ return false;
309 }
310
311 ✗ bool ForwardTranslator::applyExteriorFlowRate(contam::IndexModel model) {
312 ✗ if (m_flow && m_n && m_deltaP) {
313 ✗ std::map<std::string, int> afeMap;
314 ✗ afeMap["exterior"] = addNewAirflowElement(model, "CustomExterior", m_flow.get(), m_n.get(), m_deltaP.get());
315 ✗ afeMap["roof"] = addNewAirflowElement(model, "CustomRoof", m_flow.get(), m_n.get(), m_deltaP.get());
316 ✗ afeMap["interior"] = addNewAirflowElement(model, "CustomInterior", 2 * m_flow.get(), m_n.get(), m_deltaP.get());
317 ✗ afeMap["floor"] = addNewAirflowElement(model, "CustomFloor", 2 * m_flow.get(), m_n.get(), m_deltaP.get());
318 ✗ m_afeMap = afeMap;
319 ✗ m_leakageDescriptor = boost::optional<std::string>();
320 ✗ return true;
321 ✗ }
322 ✗ return false;
323 }
324
325 1 bool ForwardTranslator::modelToPrj(const openstudio::model::Model& model, const openstudio::path& path, bool translateHVAC,
326 std::string leakageDescriptor, ProgressBar* /*progressBar*/) {
327
1/1
✓ Branch 1 taken 1 time.
1 ForwardTranslator translator;
328
1/1
✓ Branch 1 taken 1 time.
1 translator.setTranslateHVAC(translateHVAC);
329
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
1 translator.setAirtightnessLevel(leakageDescriptor);
330
331
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
1 boost::optional<contam::IndexModel> prjModel = translator.translateModel(model);
332
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 if (prjModel) {
333
3/3
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
1 boost::optional<std::string> output = prjModel->toString();
334
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
1 if (!output) {
335 ✗ return false;
336 }
337
1/1
✓ Branch 1 taken 1 time.
1 openstudio::filesystem::ofstream file(path);
338
2/3
✓ Branch 1 taken 1 time.
✓ Branch 3 taken 1 time.
✗ Branch 4 not taken.
1 if (file.good()) {
339
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
1 file << *output;
340 1 return true;
341 }
342 2 }
343 ✗ return false;
344 1 }
345
346 ✗ bool compareElevation(openstudio::model::BuildingStory a, openstudio::model::BuildingStory b) {
347 ✗ double a_z = a.nominalZCoordinate().get();
348 ✗ double b_z = b.nominalZCoordinate().get();
349 ✗ return a_z < b_z;
350 }
351
352 // This function is altogether too long and needs to be broken up into smaller functions.
353 // This is particularly true for the HVAC translation.
354 6 boost::optional<contam::IndexModel> ForwardTranslator::translateModel(model::Model model) {
355
1/1
✓ Branch 2 taken 6 times.
6 m_logSink.setThreadId(std::this_thread::get_id());
356
1/1
✓ Branch 1 taken 6 times.
6 m_logSink.resetStringStream();
357
358 {
359
2/2
✓ Branch 1 taken 6 times.
✓ Branch 4 taken 6 times.
6 std::string s = ::openstudiocontam::embedded_files::getFileAsString(":/templates/template.prj");
360
1/1
✓ Branch 1 taken 6 times.
6 Reader r(s);
361
1/1
✓ Branch 1 taken 6 times.
6 m_prjModel.read(r);
362
2/3
✓ Branch 1 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
6 if (!m_prjModel.valid()) {
363 ✗ LOG(Error, "Unable to load templates");
364 ✗ return boost::none;
365 }
366 6 }
367
368 // The template is a legal PRJ file, so it has one level. Not for long.
369
1/1
✓ Branch 2 taken 6 times.
6 m_prjModel.setLevels(std::vector<Level>());
370
371 // Do some setup work
372
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
12 if (m_leakageDescriptor) {
373
3/4
✓ Branch 1 taken 6 times.
✓ Branch 4 taken 6 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 6 times.
6 if (!applyAirtightnessLevel(m_prjModel)) {
374 ✗ LOG(Error, "Application of airtightness level failed.");
375 ✗ return {};
376 }
377 } else {
378 ✗ if (!applyExteriorFlowRate(m_prjModel)) {
379 ✗ LOG(Error, "Application of exterior flow rate failed.");
380 ✗ return {};
381 }
382 }
383
384 6 std::string output;
385 int nr;
386 // Set top-level model info
387
1/1
✓ Branch 1 taken 6 times.
6 boost::optional<model::Building> building = model.getOptionalUniqueModelObject<model::Building>();
388
1/1
✓ Branch 1 taken 6 times.
12 std::string modelDescr = std::string("Automatically generated OpenStudio model");
389
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (building) {
390
2/2
✓ Branch 1 taken 6 times.
✓ Branch 4 taken 6 times.
6 boost::optional<std::string> name = building->name();
391
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (name) {
392
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.
24 modelDescr = std::string("Automatically generated from \"") + name.get() + std::string("\" OpenStudio model");
393 }
394 6 }
395
1/1
✓ Branch 1 taken 6 times.
6 m_prjModel.setDesc(modelDescr);
396 // Set the simulation length to match the length of the E+ simulation
397
1/1
✓ Branch 1 taken 6 times.
6 boost::optional<openstudio::model::RunPeriod> rp = model.runPeriod();
398
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 5 times.
6 if (rp) {
399 1 bool goodDates = true;
400 1 std::string startString;
401 1 std::string endString;
402 try {
403
6/6
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
✓ Branch 10 taken 1 time.
✓ Branch 13 taken 1 time.
✓ Branch 16 taken 1 time.
1 const openstudio::Date start(rp->getBeginMonth(), rp->getBeginDayOfMonth());
404
3/3
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
2 startString = fmt::format("{}{:02d}", start.monthOfYear().valueName(), start.dayOfMonth());
405
6/6
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
✓ Branch 10 taken 1 time.
✓ Branch 13 taken 1 time.
✓ Branch 16 taken 1 time.
1 const openstudio::Date end(rp->getEndMonth(), rp->getEndDayOfMonth());
406
3/3
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
2 endString = fmt::format("{}{:02d}", end.monthOfYear().valueName(), end.dayOfMonth());
407
4/4
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
✓ Branch 10 taken 1 time.
1 m_startDateTime = boost::optional<DateTime>(DateTime(start, Time(0)));
408
4/4
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
✓ Branch 10 taken 1 time.
1 m_endDateTime = boost::optional<DateTime>(DateTime(end, Time(0, 24)));
409 ✗ } catch (...) {
410 ✗ goodDates = false;
411 ✗ LOG(Error, "Failed to get usable start of end date from run period object, defaulting to steady state")
412 ✗ }
413
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 if (goodDates) {
414 // Turn on transient simulation
415
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
1 m_prjModel.rc().setSim_af(1);
416 // Set start date
417
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
1 m_prjModel.rc().setDate_0(startString);
418
3/3
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
3 m_prjModel.rc().setTime_0("00:00:00");
419 // Set start date
420
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
1 m_prjModel.rc().setDate_1(endString);
421
3/3
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
3 m_prjModel.rc().setTime_1("24:00:00");
422 }
423 1 }
424 // Get stories
425
1/1
✓ Branch 1 taken 6 times.
6 std::vector<openstudio::model::BuildingStory> stories = model.getConcreteModelObjects<openstudio::model::BuildingStory>();
426 // We will need each story to have an elevation and a floor-to-floor height.
427
2/2
✓ Branch 5 taken 5 times.
✓ Branch 6 taken 4 times.
9 for (const openstudio::model::BuildingStory& buildingStory : stories) {
428
1/1
✓ Branch 1 taken 5 times.
5 boost::optional<double> elevation = buildingStory.nominalZCoordinate();
429
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 3 times.
5 if (!elevation) {
430
9/9
✓ 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.
✓ Branch 20 taken 2 times.
✓ Branch 23 taken 2 times.
✓ Branch 26 taken 2 times.
2 LOG(Error, "Story '" << buildingStory.name().get() << "' has no elevation, translation aborted");
431 2 return boost::none;
432 }
433
1/1
✓ Branch 1 taken 3 times.
3 boost::optional<double> optHt = buildingStory.nominalFloortoFloorHeight();
434
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (!optHt) {
435 ✗ LOG(Error, "Story '" << buildingStory.name().get() << "' has no nominal floor-to-floor height, translation aborted");
436 ✗ return boost::none;
437 }
438 }
439 // Sort the stories by elevation
440
1/1
✓ Branch 3 taken 4 times.
4 std::sort(stories.begin(), stories.end(), compareElevation);
441 4 nr = 1;
442 4 double totalHeight = 0;
443
2/2
✓ Branch 5 taken 3 times.
✓ Branch 6 taken 4 times.
7 for (const openstudio::model::BuildingStory& buildingStory : stories) {
444
1/1
✓ Branch 1 taken 3 times.
3 openstudio::contam::Level level;
445
5/5
✓ Branch 1 taken 3 times.
✓ Branch 5 taken 3 times.
✓ Branch 8 taken 3 times.
✓ Branch 11 taken 3 times.
✓ Branch 14 taken 3 times.
9 level.setName(std::string("<") + std::to_string(nr) + std::string(">"));
446
2/2
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
3 m_levelMap[buildingStory.handle()] = nr;
447
2/2
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
3 double ht = buildingStory.nominalFloortoFloorHeight().get();
448 3 totalHeight += ht;
449
2/2
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
3 double z = buildingStory.nominalZCoordinate().get();
450
1/1
✓ Branch 1 taken 3 times.
3 level.setNr(nr);
451
2/2
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
3 level.setRefht(std::to_string(z));
452
2/2
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
3 level.setDelht(std::to_string(ht));
453
1/1
✓ Branch 1 taken 3 times.
3 m_prjModel.addLevel(level);
454 3 nr++;
455 //if (m_progressBar) {
456 // m_progressBar->setValue(m_progressBar->value() + 1);
457 //}
458 3 }
459
2/2
✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
4 m_prjModel.setWind_H(std::to_string(totalHeight));
460 // Check for levels - translation can't proceed without levels
461
3/3
✓ Branch 1 taken 4 times.
✓ Branch 5 taken 1 time.
✓ Branch 6 taken 3 times.
4 if (m_prjModel.levels().empty()) {
462
5/5
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
✓ Branch 10 taken 1 time.
✓ Branch 13 taken 1 time.
1 LOG(Error, "Failed to find building stories in model, translation aborted");
463 1 return {};
464 }
465 // Translate each thermal zone and generate a lookup table by name.
466
1/1
✓ Branch 1 taken 3 times.
3 std::vector<model::ThermalZone> thermalZones = model.getConcreteModelObjects<model::ThermalZone>();
467
2/2
✓ Branch 1 taken 3 times.
✓ Branch 5 taken 3 times.
6 initProgress(thermalZones.size(), "Translating Zones");
468
469 3 nr = 0;
470
2/2
✓ Branch 5 taken 9 times.
✓ Branch 6 taken 3 times.
12 for (const model::ThermalZone& thermalZone : thermalZones) {
471 9 nr++;
472
1/1
✓ Branch 1 taken 9 times.
9 openstudio::contam::Zone zone;
473
2/2
✓ Branch 1 taken 9 times.
✓ Branch 4 taken 9 times.
9 m_zoneMap[thermalZone.handle()] = nr;
474 //volumeMap[thermalZone.name().get()] = nr;
475
1/1
✓ Branch 1 taken 9 times.
9 zone.setNr(nr);
476
3/3
✓ Branch 2 taken 9 times.
✓ Branch 5 taken 9 times.
✓ Branch 8 taken 9 times.
27 zone.setName(std::string("Zone_") + std::to_string(nr));
477
1/1
✓ Branch 1 taken 9 times.
9 boost::optional<double> volume = thermalZone.volume();
478
1/1
✓ Branch 1 taken 9 times.
18 std::string volString("0.0");
479
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (volume) {
480 ✗ volString = std::to_string(*volume);
481 } else {
482 // Since it seems this is a pretty common thing, no warning unless we can't get a value
483 // LOG(Warn, "Zone '" << name.toStdString() << "' has zero volume, trying to sum space volumes");
484 9 double vol = 0.0;
485
3/3
✓ Branch 1 taken 9 times.
✓ Branch 7 taken 12 times.
✓ Branch 8 taken 9 times.
21 for (const openstudio::model::Space& space : thermalZone.spaces()) {
486
1/1
✓ Branch 1 taken 12 times.
12 vol += space.volume();
487 9 }
488
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (vol == 0.0) {
489 ✗ LOG(Warn, "Failed to compute volume for Zone '" << thermalZone.name().get() << "'");
490 } else {
491
1/1
✓ Branch 1 taken 9 times.
9 volString = std::to_string(vol);
492 }
493 }
494
1/1
✓ Branch 1 taken 9 times.
9 zone.setVol(volString);
495
1/1
✓ Branch 1 taken 9 times.
9 zone.setVariablePressure(true);
496
1/1
✓ Branch 1 taken 9 times.
9 zone.setVariableContaminants(true);
497 // Set level - this is not great and will fail to create a legitimate model in cases
498 // where a zone is on more than one level. There are ugly workarounds - will need to
499 // think about
500 9 int levelNr = 0;
501
2/3
✓ Branch 1 taken 9 times.
✓ Branch 8 taken 9 times.
✗ Branch 9 not taken.
9 for (const openstudio::model::Space& space : thermalZone.spaces()) {
502
1/1
✓ Branch 1 taken 9 times.
9 boost::optional<openstudio::model::BuildingStory> story = space.buildingStory();
503
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 if (story) {
504
4/4
✓ Branch 1 taken 9 times.
✓ Branch 4 taken 9 times.
✓ Branch 7 taken 9 times.
✓ Branch 10 taken 9 times.
9 levelNr = tableLookup(m_levelMap, (*story).handle(), "levelMap");
505 9 break;
506 }
507 18 }
508
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 if (levelNr != 0) {
509
1/1
✓ Branch 1 taken 9 times.
9 zone.setPl(levelNr);
510 } else {
511 ✗ LOG(Error, "Unable to set level for zone '" << thermalZone.name().get() << "', translation aborted");
512 ✗ return {};
513 }
514 // set T0
515
2/2
✓ Branch 1 taken 9 times.
✓ Branch 4 taken 9 times.
9 zone.setT0("293.15");
516
2/2
✓ Branch 1 taken 9 times.
✓ Branch 4 taken 9 times.
9 m_prjModel.addZone(zone);
517
1/1
✓ Branch 1 taken 9 times.
9 progress();
518 9 }
519
520 // Create paths and generate a lookup table by name
521
1/1
✓ Branch 1 taken 3 times.
3 std::vector<openstudio::model::Surface> surfaces = model.getConcreteModelObjects<openstudio::model::Surface>();
522
2/2
✓ Branch 1 taken 3 times.
✓ Branch 5 taken 3 times.
6 initProgress(surfaces.size(), "Translating Surfaces");
523
524 3 nr = 0;
525 // Loop over surfaces and generate paths
526
1/1
✓ Branch 1 taken 3 times.
3 build(model);
527
528
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (m_translateHVAC) {
529 // Generate air handling systems
530
1/1
✓ Branch 1 taken 3 times.
3 std::vector<openstudio::model::AirLoopHVAC> systems = model.getConcreteModelObjects<openstudio::model::AirLoopHVAC>();
531
2/2
✓ Branch 1 taken 3 times.
✓ Branch 5 taken 3 times.
6 initProgress(systems.size(), "Translating AirLoops");
532
533 3 nr = 0;
534
2/2
✓ Branch 5 taken 3 times.
✓ Branch 6 taken 3 times.
6 for (const openstudio::model::AirLoopHVAC& airloop : systems) {
535 // Skip loops with no zones attached
536
2/3
✓ Branch 1 taken 3 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 3 times.
3 if (airloop.thermalZones().empty()) {
537 ✗ progress();
538 ✗ continue;
539 }
540
1/1
✓ Branch 1 taken 3 times.
3 openstudio::contam::Ahs ahs;
541
1/1
✓ Branch 1 taken 3 times.
3 ahs.setNr(++nr);
542
2/2
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
3 m_ahsMap[airloop.handle()] = nr;
543
3/3
✓ Branch 2 taken 3 times.
✓ Branch 5 taken 3 times.
✓ Branch 8 taken 3 times.
9 ahs.setName(std::string("AHS_") + std::to_string(nr));
544 // Create supply and return zones
545
1/1
✓ Branch 1 taken 3 times.
3 openstudio::contam::Zone rz;
546
2/2
✓ Branch 1 taken 3 times.
✓ Branch 5 taken 3 times.
3 rz.setNr(m_prjModel.zones().size() + 1);
547
1/1
✓ Branch 1 taken 3 times.
3 rz.setPl(1);
548
2/2
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
3 rz.setT0("293.15");
549
1/1
✓ Branch 1 taken 3 times.
3 rz.setSystem(true);
550
1/1
✓ Branch 1 taken 3 times.
3 rz.setVariableContaminants(true);
551
5/5
✓ Branch 1 taken 3 times.
✓ Branch 5 taken 3 times.
✓ Branch 8 taken 3 times.
✓ Branch 11 taken 3 times.
✓ Branch 14 taken 3 times.
9 rz.setName(std::string("AHS_") + std::to_string(nr) + std::string("(Rec)"));
552 //volumeMap[rz.name.toStdString()] = rz.nr;
553
1/1
✓ Branch 1 taken 3 times.
3 openstudio::contam::Zone sz;
554
2/2
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
3 sz.setNr(rz.nr() + 1);
555
1/1
✓ Branch 1 taken 3 times.
3 sz.setPl(1);
556
2/2
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
3 sz.setT0("293.15");
557
1/1
✓ Branch 1 taken 3 times.
3 sz.setSystem(true);
558
1/1
✓ Branch 1 taken 3 times.
3 sz.setVariableContaminants(true);
559
5/5
✓ Branch 1 taken 3 times.
✓ Branch 5 taken 3 times.
✓ Branch 8 taken 3 times.
✓ Branch 11 taken 3 times.
✓ Branch 14 taken 3 times.
9 sz.setName(std::string("AHS_") + std::to_string(nr) + std::string("(Sup)"));
560 //volumeMap[sz.name.toStdString()] = sz.nr;
561 // Store the zone numbers in the ahs
562
2/2
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
3 ahs.setZone_r(rz.nr());
563
2/2
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
3 ahs.setZone_s(sz.nr());
564 // Add them to the zone list
565
2/2
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
3 m_prjModel.addZone(rz);
566
2/2
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
3 m_prjModel.addZone(sz);
567 // Now hook the served zones up to the supply and return zones
568
3/3
✓ Branch 1 taken 3 times.
✓ Branch 8 taken 7 times.
✓ Branch 9 taken 3 times.
10 for (const openstudio::model::ThermalZone& thermalZone : airloop.thermalZones()) {
569
3/3
✓ Branch 1 taken 7 times.
✓ Branch 4 taken 7 times.
✓ Branch 7 taken 7 times.
7 int zoneNr = tableLookup(m_zoneMap, thermalZone.handle(), "zoneMap");
570 // Supply path
571
1/1
✓ Branch 1 taken 7 times.
7 openstudio::contam::AirflowPath sp;
572
2/2
✓ Branch 1 taken 7 times.
✓ Branch 5 taken 7 times.
7 sp.setNr(m_prjModel.airflowPaths().size() + 1);
573
1/1
✓ Branch 1 taken 7 times.
7 sp.setPld(1);
574
2/2
✓ Branch 1 taken 7 times.
✓ Branch 4 taken 7 times.
7 sp.setPzn(ahs.zone_s());
575
1/1
✓ Branch 1 taken 7 times.
7 sp.setPzm(zoneNr);
576
2/2
✓ Branch 1 taken 7 times.
✓ Branch 4 taken 7 times.
7 sp.setPa(ahs.nr());
577
1/1
✓ Branch 1 taken 7 times.
7 sp.setSystem(true);
578
5/5
✓ Branch 1 taken 7 times.
✓ Branch 4 taken 7 times.
✓ Branch 7 taken 7 times.
✓ Branch 10 taken 7 times.
✓ Branch 13 taken 7 times.
7 m_pathMap[(thermalZone.name().get() + " supply")] = sp.nr();
579 // Return path
580
1/1
✓ Branch 1 taken 7 times.
7 openstudio::contam::AirflowPath rp;
581
2/2
✓ Branch 1 taken 7 times.
✓ Branch 4 taken 7 times.
7 rp.setNr(sp.nr() + 1);
582
1/1
✓ Branch 1 taken 7 times.
7 rp.setPld(1);
583
1/1
✓ Branch 1 taken 7 times.
7 rp.setPzn(zoneNr);
584
2/2
✓ Branch 1 taken 7 times.
✓ Branch 4 taken 7 times.
7 rp.setPzm(ahs.zone_r());
585
2/2
✓ Branch 1 taken 7 times.
✓ Branch 4 taken 7 times.
7 rp.setPa(ahs.nr());
586
1/1
✓ Branch 1 taken 7 times.
7 rp.setSystem(true);
587
5/5
✓ Branch 1 taken 7 times.
✓ Branch 4 taken 7 times.
✓ Branch 7 taken 7 times.
✓ Branch 10 taken 7 times.
✓ Branch 13 taken 7 times.
7 m_pathMap[(thermalZone.name().get() + " return")] = rp.nr();
588 // Add the paths to the path list
589
2/2
✓ Branch 1 taken 7 times.
✓ Branch 4 taken 7 times.
7 m_prjModel.addAirflowPath(sp);
590
2/2
✓ Branch 1 taken 7 times.
✓ Branch 4 taken 7 times.
7 m_prjModel.addAirflowPath(rp);
591 10 }
592
2/2
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
3 m_prjModel.addAhs(ahs);
593
1/1
✓ Branch 1 taken 3 times.
3 progress();
594 3 }
595
596
3/3
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 8 taken 3 times.
6 initProgress(m_prjModel.ahs().size(), "Connecting AHS to zones");
597 // Now loop back through the AHS list and connect the supply and return zones together
598
3/3
✓ Branch 1 taken 6 times.
✓ Branch 5 taken 3 times.
✓ Branch 6 taken 3 times.
6 for (unsigned int i = 0; i < m_prjModel.ahs().size(); i++) {
599
2/2
✓ Branch 2 taken 3 times.
✓ Branch 5 taken 3 times.
9 std::string loopName = std::string("AHS_") + std::to_string(i + 1);
600 // Recirculation path
601
1/1
✓ Branch 1 taken 3 times.
3 openstudio::contam::AirflowPath recirc;
602
2/2
✓ Branch 1 taken 3 times.
✓ Branch 5 taken 3 times.
3 recirc.setNr(m_prjModel.airflowPaths().size() + 1);
603
1/1
✓ Branch 1 taken 3 times.
3 recirc.setPld(1);
604 // Set the OA fraction schedule here
605 //recirc.ps = ?
606
3/3
✓ Branch 1 taken 3 times.
✓ Branch 5 taken 3 times.
✓ Branch 8 taken 3 times.
3 recirc.setPzn(m_prjModel.ahs()[i].zone_r());
607
3/3
✓ Branch 1 taken 3 times.
✓ Branch 5 taken 3 times.
✓ Branch 8 taken 3 times.
3 recirc.setPzm(m_prjModel.ahs()[i].zone_s());
608
1/1
✓ Branch 1 taken 3 times.
3 recirc.setRecirculation(true);
609
3/3
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
3 m_pathMap[loopName + " recirculation"] = recirc.nr();
610 // Outside air path
611
1/1
✓ Branch 1 taken 3 times.
3 openstudio::contam::AirflowPath oa;
612
2/2
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
3 oa.setNr(recirc.nr() + 1);
613
1/1
✓ Branch 1 taken 3 times.
3 oa.setPld(1);
614
1/1
✓ Branch 1 taken 3 times.
3 oa.setPzn(-1);
615
3/3
✓ Branch 1 taken 3 times.
✓ Branch 5 taken 3 times.
✓ Branch 8 taken 3 times.
3 oa.setPzm(m_prjModel.ahs()[i].zone_s());
616
1/1
✓ Branch 1 taken 3 times.
3 oa.setOutsideAir(true);
617
3/3
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
3 m_pathMap[loopName + " oa"] = oa.nr();
618 // Exhaust path;
619
1/1
✓ Branch 1 taken 3 times.
3 openstudio::contam::AirflowPath exhaust;
620
2/2
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
3 exhaust.setNr(oa.nr() + 1);
621
1/1
✓ Branch 1 taken 3 times.
3 exhaust.setPld(1);
622
3/3
✓ Branch 1 taken 3 times.
✓ Branch 5 taken 3 times.
✓ Branch 8 taken 3 times.
3 exhaust.setPzn(m_prjModel.ahs()[i].zone_r());
623
1/1
✓ Branch 1 taken 3 times.
3 exhaust.setPzm(-1);
624
1/1
✓ Branch 1 taken 3 times.
3 exhaust.setExhaust(true);
625
3/3
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
3 m_pathMap[loopName + " exhaust"] = exhaust.nr();
626 // Add the paths to the path list
627
2/2
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
3 m_prjModel.addAirflowPath(recirc);
628
2/2
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
3 m_prjModel.addAirflowPath(oa);
629
2/2
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
3 m_prjModel.addAirflowPath(exhaust);
630 // Store the nrs in the ahs
631
3/3
✓ Branch 1 taken 3 times.
✓ Branch 5 taken 3 times.
✓ Branch 8 taken 3 times.
3 m_prjModel.ahs()[i].setPath_r(recirc.nr());
632
3/3
✓ Branch 1 taken 3 times.
✓ Branch 5 taken 3 times.
✓ Branch 8 taken 3 times.
3 m_prjModel.ahs()[i].setPath_s(oa.nr());
633
3/3
✓ Branch 1 taken 3 times.
✓ Branch 5 taken 3 times.
✓ Branch 8 taken 3 times.
3 m_prjModel.ahs()[i].setPath_x(exhaust.nr());
634
1/1
✓ Branch 1 taken 3 times.
3 progress();
635 3 }
636
637 // The rest of this isn't hooked into the progress bar yet, will need to do that at some point
638
639 // Try to use E+ results to set temperatures and flow rates. The supply and return flow paths are in the path
640 // lookup table under the names thermalZone.name + supply|return (see above)
641
1/1
✓ Branch 1 taken 3 times.
3 boost::optional<openstudio::SqlFile> sqlFile = model.sqlFile();
642
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (sqlFile) {
643 ✗ std::vector<std::string> available = sqlFile->availableTimeSeries();
644 //for (std::string var : available)
645 //{
646 // std::cout << '\t' << var << '\n';
647 //}
648 ✗ std::string envPeriod;
649 ✗ for (const std::string& t : sqlFile->availableEnvPeriods()) {
650 ✗ envPeriod = t; // should only ever be one
651 ✗ break;
652 ✗ }
653 // bool setTime=false;
654 ✗ if (std::find(available.begin(), available.end(), "Zone Mean Air Temperature") != available.end()) {
655 // Loop through and get a time series for each zone we can find
656 ✗ for (const model::ThermalZone& thermalZone : model.getConcreteModelObjects<model::ThermalZone>()) {
657 ✗ boost::optional<std::string> name = thermalZone.name();
658 ✗ if (!name) {
659 ✗ LOG(Warn, "Zone " << openstudio::toString(thermalZone.handle()) << " has no name and will have constant temperature");
660 ✗ continue;
661 ✗ }
662 ✗ std::string keyValue = name.get();
663 ✗ keyValue = boost::regex_replace(keyValue, boost::regex("([a-z])"), "\\u$1");
664 ✗ boost::optional<TimeSeries> timeSeries = sqlFile->timeSeries(envPeriod, "Hourly", "Zone Mean Air Temperature", keyValue);
665 ✗ if (timeSeries) {
666 ✗ int nr = m_zoneMap[thermalZone.handle()];
667 // std::cout << "Found time series for zone " << name.get() << ", CONTAM index " << nr << '\n';
668 // Create a control node
669 ✗ std::string controlName = std::string("ctrl_z_") + std::to_string(nr);
670 ✗ std::string valueName = std::string("temp_") + std::to_string(nr);
671 ✗ m_cvf.addTimeSeries(valueName, *timeSeries);
672 ✗ CvfDat ctrl;
673 ✗ ctrl.setName(controlName);
674 ✗ ctrl.setValuename(valueName);
675 ✗ m_prjModel.addControlNode(ctrl);
676 // Connect to the zone
677 ✗ m_prjModel.zones()[nr - 1].setPc(ctrl.nr());
678 ✗ } else {
679 ✗ LOG(Warn, "Zone '" << name.get() << "' has no Zone Mean Air Temperature time series");
680 }
681 ✗ }
682 } else {
683 ✗ LOG(Warn, "Activate \"Zone Mean Air Temperature\" output to set zone temperature controls.");
684 }
685 ✗ if (std::find(available.begin(), available.end(), "System Node MassFlowRate") != available.end()) {
686 ✗ LOG(Warn, "Zone equipment not yet accounted for.");
687 // get sizing results, get flow rate schedules for each zone's inlet, return, and exhaust nodes
688 // This should be moved to inside the contam translator
689 ✗ for (const model::ThermalZone& thermalZone : model.getConcreteModelObjects<model::ThermalZone>()) {
690 // todo: this does not include OA from zone equipment (PTAC, PTHP, etc) or exhaust fans
691
692 ✗ boost::optional<model::Node> supplyAirNode;
693 ✗ boost::optional<model::ModelObject> supplyAirModelObject = thermalZone.inletPortList().airLoopHVACModelObject();
694 ✗ if (supplyAirModelObject) {
695 ✗ supplyAirNode = supplyAirModelObject->optionalCast<model::Node>();
696 }
697 ✗ if (supplyAirNode) {
698 ✗ std::string keyValue = supplyAirNode->name().get();
699 ✗ keyValue = boost::regex_replace(keyValue, boost::regex("([a-z])"), "\\u$1");
700 ✗ boost::optional<TimeSeries> timeSeries = sqlFile->timeSeries(envPeriod, "Hourly", "System Node MassFlowRate", keyValue);
701 ✗ if (timeSeries) {
702 //std::cout << "Found time series for supply to zone " << thermalZone.name().get() << '\n';
703 ✗ nr = 0;
704 ✗ const auto& it = m_pathMap.find(thermalZone.name().get() + " supply");
705 ✗ if (it != m_pathMap.end()) {
706 ✗ nr = it->second;
707 }
708 // There really should not be a case of missing number here, but it is better to be safe
709 ✗ if (nr == 0) {
710 ✗ LOG(Error, "Supply node for zone '" << thermalZone.name().get() << "' has no associated CONTAM path");
711 ✗ continue;
712 ✗ }
713 // Create a control node
714 ✗ std::string controlName = std::string("ctrl_p_") + std::to_string(nr);
715 ✗ std::string valueName = std::string("supply_") + std::to_string(nr);
716 ✗ m_cvf.addTimeSeries(valueName, *timeSeries);
717 ✗ CvfDat ctrl;
718 ✗ ctrl.setName(controlName);
719 ✗ ctrl.setValuename(valueName);
720 ✗ m_prjModel.addControlNode(ctrl);
721 // Connect to the path
722 ✗ m_prjModel.airflowPaths()[nr - 1].setPc(ctrl.nr());
723 ✗ if (m_ratioOverride) { // This assumes that there *is* a return, which could be wrong? maybe?
724 // Create a new time series
725 ✗ TimeSeries returnSeries = (*timeSeries) * m_returnSupplyRatio;
726 ✗ nr = 0;
727 ✗ const auto& it = m_pathMap.find(thermalZone.name().get() + " return");
728 ✗ if (it != m_pathMap.end()) {
729 ✗ nr = it->second;
730 }
731 // There really should not be a case of missing number here, but it is better to be safe
732 ✗ if (nr == 0) {
733 ✗ LOG(Error, "Failed to find return path for zone '" << thermalZone.name().get() << "'");
734 ✗ continue;
735 ✗ }
736 // Create a control node
737 ✗ std::string controlName = std::string("ctrl_p_") + std::to_string(nr);
738 ✗ std::string valueName = std::string("return_") + std::to_string(nr);
739 ✗ m_cvf.addTimeSeries(valueName, *timeSeries);
740 ✗ CvfDat ctrl;
741 ✗ ctrl.setName(controlName);
742 ✗ ctrl.setValuename(valueName);
743 ✗ m_prjModel.addControlNode(ctrl);
744 // Connect to the path
745 ✗ m_prjModel.airflowPaths()[nr - 1].setPc(ctrl.nr());
746 ✗ }
747 ✗ }
748 ✗ }
749
750 ✗ if (!m_ratioOverride) {
751 ✗ boost::optional<model::Node> returnAirNode;
752 ✗ boost::optional<model::ModelObject> returnAirModelObject = thermalZone.returnAirModelObject();
753 ✗ if (returnAirModelObject) {
754 ✗ returnAirNode = returnAirModelObject->optionalCast<model::Node>();
755 }
756 ✗ if (returnAirNode) {
757 ✗ std::string keyValue = returnAirNode->name().get();
758 ✗ keyValue = boost::regex_replace(keyValue, boost::regex("([a-z])"), "\\u$1");
759 ✗ boost::optional<TimeSeries> timeSeries = sqlFile->timeSeries(envPeriod, "Hourly", "System Node MassFlowRate", keyValue);
760 ✗ if (timeSeries) {
761 //std::cout << "Found time series for return from zone " << thermalZone.name().get() << '\n';
762 ✗ nr = 0;
763 ✗ const auto& it = m_pathMap.find(thermalZone.name().get() + " return");
764 ✗ if (it != m_pathMap.end()) {
765 ✗ nr = it->second;
766 }
767 // There really should not be a case of missing number here, but it is better to be safe
768 ✗ if (nr == 0) {
769 ✗ LOG(Error, "Return node for zone '" << thermalZone.name().get() << "' has no associated CONTAM path");
770 ✗ continue;
771 ✗ }
772 // Create a control node
773 ✗ std::string controlName = std::string("ctrl_p_") + std::to_string(nr);
774 ✗ std::string valueName = std::string("return_") + std::to_string(nr);
775 ✗ m_cvf.addTimeSeries(valueName, *timeSeries);
776 ✗ CvfDat ctrl;
777 ✗ ctrl.setName(controlName);
778 ✗ ctrl.setValuename(valueName);
779 ✗ m_prjModel.addControlNode(ctrl);
780 // Connect to the path
781 ✗ m_prjModel.airflowPaths()[nr - 1].setPc(ctrl.nr());
782 ✗ }
783 ✗ }
784 ✗ }
785 ✗ }
786 } else {
787 ✗ LOG(Warn, "Activate \"System Node MassFlowRate\" output to set zone supply/return flows.");
788 }
789 ✗ } else {
790
5/5
✓ 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.
3 LOG(Warn, "Simulation results not available, using 1 scfm/ft^2 to set supply flows");
791 // Use the 1 scfm/ft^2 approximation with 90% return
792
3/3
✓ Branch 1 taken 3 times.
✓ Branch 8 taken 9 times.
✓ Branch 9 taken 3 times.
12 for (const openstudio::model::ThermalZone& thermalZone : model.getConcreteModelObjects<openstudio::model::ThermalZone>()) {
793 9 double area = 0.0;
794
3/3
✓ Branch 1 taken 9 times.
✓ Branch 7 taken 12 times.
✓ Branch 8 taken 9 times.
21 for (const openstudio::model::Space& space : thermalZone.spaces()) {
795
1/1
✓ Branch 1 taken 12 times.
12 area += space.floorArea();
796 9 }
797
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (area == 0.0) {
798 ✗ LOG(Warn, "Failed to compute floor area for Zone '" << thermalZone.name().get() << "'");
799 } else {
800 9 double flowRate = area * 0.00508 * 1.2041; // Assume 1 scfm/ft^2 as an approximation
801
3/3
✓ Branch 1 taken 9 times.
✓ Branch 4 taken 9 times.
✓ Branch 7 taken 9 times.
9 std::string supplyName = thermalZone.name().get() + " supply";
802
3/3
✓ Branch 1 taken 9 times.
✓ Branch 4 taken 9 times.
✓ Branch 7 taken 9 times.
9 std::string returnName = thermalZone.name().get() + " return";
803 9 int supplyNr = 0;
804
1/1
✓ Branch 1 taken 9 times.
9 const auto& its = m_pathMap.find(supplyName);
805
2/2
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 2 times.
9 if (its != m_pathMap.end()) {
806 7 supplyNr = its->second;
807 }
808
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 2 times.
9 if (supplyNr != 0) {
809
3/3
✓ Branch 1 taken 7 times.
✓ Branch 5 taken 7 times.
✓ Branch 8 taken 7 times.
7 m_prjModel.airflowPaths()[supplyNr - 1].setFahs(std::to_string(flowRate));
810 }
811
812 9 int returnNr = 0;
813
1/1
✓ Branch 1 taken 9 times.
9 const auto& itr = m_pathMap.find(returnName);
814
2/2
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 2 times.
9 if (itr != m_pathMap.end()) {
815 7 returnNr = itr->second;
816 }
817
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 2 times.
9 if (returnNr != 0) {
818
3/3
✓ Branch 1 taken 7 times.
✓ Branch 5 taken 7 times.
✓ Branch 8 taken 7 times.
7 m_prjModel.airflowPaths()[returnNr - 1].setFahs(std::to_string(m_returnSupplyRatio * flowRate));
819 }
820 9 }
821 3 }
822 }
823 3 }
824
825
1/1
✓ Branch 1 taken 3 times.
3 return boost::optional<contam::IndexModel>(m_prjModel);
826
827 // these are probably useful, will have to ask Kyle
828 // Kyle, should these functions be const?
829 //boost::optional<model::ModelObject> airInletModelObject = zone.airInletModelObject();
830 //boost::optional<model::ModelObject> returnAirModelObject = zone.returnAirModelObject();
831 //boost::optional<model::Node> exhaustAirNode = zone.exhaustAirNode();
832 //boost::optional<model::Node> zoneAirNode = zone.zoneAirNode();
833
834 //ZoneData *afz = zoneList.at(i);
835 //double flowRate = afz->area*0.00508*1.2041; // Assume 1 scfm/ft^2 as an approximation
836 6 }
837
838 ✗ static double laminarCoefficient(double Ct, double x) {
839 // Ct turbulent flow coefficient
840 // x exponent
841 double Clam; // laminar flow coefficient
842 double F; // mass flow rate at L-T transition
843 ✗ double dP = 0; // pressure drop at L-T transition
844 ✗ double RHOAIR = 1.20410; // density of standard air
845 ✗ double SRHO = 1.097315; // std::sqrt( RHOAIR )
846 ✗ double SQRT2 = 1.414213562; // std::sqrt( 2.0 )
847 ✗ double MUAIR = 1.81625e-5; // viscosity of standard air
848 ✗ double DPTMIN = 1.0e-10; // minimum L-T transition dP
849 ✗ double Re = 30.0; // Reynolds number of L-T transition
850
851 ✗ double A = Ct / (0.6 * SQRT2); // flow area
852 ✗ double D = std::sqrt(A); // hydraulic diameter
853
854 // Re = rho * V * D / mu; F = rho * V * A; therefore
855 ✗ F = MUAIR * Re * A / D;
856 // F = Ct * std::sqrt(rho) * dP^x
857 ✗ dP = std::pow(F / (Ct * SRHO), 1.0 / x);
858 ✗ if (dP < DPTMIN) {
859 ✗ dP = DPTMIN; // 1998/07/17 - dPt > 0
860 }
861 // F = Clam * (rho / mu) * dP
862 ✗ Clam = ((MUAIR * F) / (RHOAIR * dP));
863
864 ✗ return Clam;
865 }
866
867 ✗ int ForwardTranslator::addNewAirflowElement(contam::IndexModel model, std::string name, double flow, double n, double deltaP) {
868 // flow - volume flow rate in m^3/h
869 // deltaP - pressure difference in Pa
870 // n - exponent
871
872 ✗ double RHOAIR = 1.20410; // density of standard air
873 ✗ double SRHO = 1.097315; // std::sqrt( RHOAIR )
874
875 ✗ double F = RHOAIR * flow / 3600.0; // mass flow in kg/s
876
877 ✗ double Ct = F / (SRHO * std::pow(deltaP, n));
878 ✗ double Cl = laminarCoefficient(Ct, n);
879
880 ✗ std::string lam = std::to_string(Cl);
881 ✗ std::string turb = std::to_string(Ct);
882 ✗ std::string expt = std::to_string(n);
883 ✗ std::string dP = std::to_string(deltaP);
884 ✗ std::string Flow = std::to_string(F);
885 ✗ int u_P = 0;
886 ✗ int u_F = 1; // Display units are m^3/h
887
888 // Create a 1-point test element with display units of m^3/h
889 ✗ PlrTest1 afe(0, 0, name, " ", lam, turb, expt, dP, Flow, u_P, u_F);
890
891 ✗ model.addAirflowElement(afe);
892
893 ✗ return afe.nr();
894 ✗ }
895
896 ✗ bool ForwardTranslator::writeCvFile(openstudio::path filepath) {
897 ✗ if (!m_cvf.isEmpty()) {
898 ✗ return m_cvf.write(filepath);
899 }
900 ✗ return false;
901 }
902
903 36 bool ForwardTranslator::linkExteriorSurface(openstudio::model::ThermalZone zone, openstudio::model::Space /*space*/,
904 openstudio::model::Surface surface) {
905
1/1
✓ Branch 1 taken 36 times.
36 contam::AirflowPath path;
906 // Use the lookup table to get the zone info
907 int zoneNr;
908
4/5
✓ Branch 1 taken 36 times.
✓ Branch 4 taken 36 times.
✓ Branch 7 taken 36 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 36 times.
36 if ((zoneNr = tableLookup(m_zoneMap, zone.handle(), "zoneMap")) == 0) {
909 // Maybe this needs a warning?
910 ✗ return false;
911 }
912
2/2
✓ Branch 1 taken 36 times.
✓ Branch 5 taken 36 times.
36 contam::Zone airflowZone = m_prjModel.zones()[zoneNr - 1];
913 // Get the surface area - will need to do more work here later if large openings are present
914
1/1
✓ Branch 1 taken 36 times.
36 double area = surface.grossArea();
915
1/1
✓ Branch 1 taken 36 times.
36 std::string type = surface.surfaceType();
916 36 double averageZ = 0;
917
1/1
✓ Branch 1 taken 36 times.
36 double numVertices = (double)surface.vertices().size();
918
3/3
✓ Branch 1 taken 36 times.
✓ Branch 7 taken 148 times.
✓ Branch 8 taken 36 times.
184 for (const Point3d& point : surface.vertices()) {
919
1/1
✓ Branch 1 taken 148 times.
148 averageZ += point.z();
920 36 }
921 // Now set the path info
922
4/4
✓ Branch 1 taken 36 times.
✓ Branch 4 taken 36 times.
✓ Branch 8 taken 36 times.
✓ Branch 11 taken 36 times.
36 path.setRelHt(averageZ / numVertices - m_prjModel.levels()[airflowZone.pl() - 1].refht());
923
2/2
✓ Branch 1 taken 36 times.
✓ Branch 4 taken 36 times.
36 path.setPld(airflowZone.pl());
924
1/1
✓ Branch 1 taken 36 times.
36 path.setMult(area);
925 // Make an exterior flow path
926
2/2
✓ Branch 1 taken 36 times.
✓ Branch 4 taken 36 times.
36 path.setPzn(airflowZone.nr());
927
1/1
✓ Branch 1 taken 36 times.
36 path.setPzm(-1);
928 // Set the wind-related stuff here
929
3/3
✓ Branch 1 taken 36 times.
✓ Branch 4 taken 36 times.
✓ Branch 7 taken 36 times.
36 path.setWazm(openstudio::radToDeg(surface.azimuth()));
930
1/1
✓ Branch 1 taken 36 times.
36 path.setWindPressure(true);
931
3/3
✓ Branch 1 taken 36 times.
✓ Branch 4 taken 36 times.
✓ Branch 7 taken 36 times.
36 path.setWPmod(openstudio::wind::pressureModifier(openstudio::wind::Default, m_prjModel.wind_H()));
932
1/1
✓ Branch 1 taken 36 times.
36 path.setPw(4); // Assume standard template
933 // Set flow element
934
3/3
✓ Branch 1 taken 36 times.
✓ Branch 3 taken 12 times.
✓ Branch 4 taken 24 times.
36 if (type == "RoofCeiling") {
935
3/3
✓ Branch 1 taken 12 times.
✓ Branch 4 taken 12 times.
✓ Branch 7 taken 12 times.
24 path.setPe(m_afeMap["roof"]);
936
1/1
✓ Branch 1 taken 12 times.
12 path.setPw(5); // Assume standard template
937 } else {
938
3/3
✓ Branch 1 taken 24 times.
✓ Branch 4 taken 24 times.
✓ Branch 7 taken 24 times.
72 path.setPe(m_afeMap["exterior"]);
939 }
940
2/2
✓ Branch 1 taken 36 times.
✓ Branch 4 taken 36 times.
36 m_prjModel.addAirflowPath(path);
941
3/3
✓ Branch 1 taken 36 times.
✓ Branch 4 taken 36 times.
✓ Branch 7 taken 36 times.
36 m_surfaceMap[surface.handle()] = path.nr();
942 36 return true;
943 36 }
944
945 10 bool ForwardTranslator::linkInteriorSurface(openstudio::model::ThermalZone zone, openstudio::model::Space /*space*/,
946 openstudio::model::Surface surface, openstudio::model::Surface adjacentSurface,
947 openstudio::model::Space /*adjacentSpace*/, openstudio::model::ThermalZone adjacentZone) {
948
1/1
✓ Branch 1 taken 10 times.
10 contam::AirflowPath path;
949 // Use the lookup table to get the zone info
950 int zoneNr;
951
4/5
✓ Branch 1 taken 10 times.
✓ Branch 4 taken 10 times.
✓ Branch 7 taken 10 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 10 times.
10 if ((zoneNr = tableLookup(m_zoneMap, zone.handle(), "zoneMap")) == 0) {
952 // Maybe this needs a warning?
953 ✗ return false;
954 }
955
2/2
✓ Branch 1 taken 10 times.
✓ Branch 5 taken 10 times.
10 contam::Zone airflowZone = m_prjModel.zones()[zoneNr - 1];
956 // Get the surface area - will need to do more work here later if large openings are present
957
2/2
✓ Branch 1 taken 10 times.
✓ Branch 4 taken 10 times.
10 double area = 0.5 * (surface.grossArea() + adjacentSurface.grossArea());
958
1/1
✓ Branch 1 taken 10 times.
10 std::string type = surface.surfaceType();
959 10 double averageZ = 0;
960
1/1
✓ Branch 1 taken 10 times.
10 double numVertices = (double)surface.vertices().size();
961
3/3
✓ Branch 1 taken 10 times.
✓ Branch 7 taken 40 times.
✓ Branch 8 taken 10 times.
50 for (const Point3d& point : surface.vertices()) {
962
1/1
✓ Branch 1 taken 40 times.
40 averageZ += point.z();
963 10 }
964 // Now set the path info
965
4/4
✓ Branch 1 taken 10 times.
✓ Branch 4 taken 10 times.
✓ Branch 8 taken 10 times.
✓ Branch 11 taken 10 times.
10 path.setRelHt(averageZ / numVertices - m_prjModel.levels()[airflowZone.pl() - 1].refht());
966
2/2
✓ Branch 1 taken 10 times.
✓ Branch 4 taken 10 times.
10 path.setPld(airflowZone.pl());
967
1/1
✓ Branch 1 taken 10 times.
10 path.setMult(area);
968
969 // Make an interior flow path
970
2/2
✓ Branch 1 taken 10 times.
✓ Branch 4 taken 10 times.
10 path.setPzn(airflowZone.nr());
971
3/3
✓ Branch 1 taken 10 times.
✓ Branch 4 taken 10 times.
✓ Branch 7 taken 10 times.
10 path.setPzm(m_zoneMap[adjacentZone.handle()]);
972 // Set flow element
973
5/8
✓ Branch 1 taken 10 times.
✓ Branch 3 taken 10 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 10 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 10 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 10 times.
10 if (type == "Floor" || type == "RoofCeiling") {
974 ✗ path.setPe(m_afeMap["floor"]);
975 } else {
976
3/3
✓ Branch 1 taken 10 times.
✓ Branch 4 taken 10 times.
✓ Branch 7 taken 10 times.
30 path.setPe(m_afeMap["interior"]);
977 }
978
2/2
✓ Branch 1 taken 10 times.
✓ Branch 4 taken 10 times.
10 m_prjModel.addAirflowPath(path);
979
3/3
✓ Branch 1 taken 10 times.
✓ Branch 4 taken 10 times.
✓ Branch 7 taken 10 times.
10 m_surfaceMap[surface.handle()] = path.nr();
980
981 10 return true;
982 10 }
983
984 30 bool ForwardTranslator::linkExteriorSubSurface(openstudio::model::ThermalZone /*zone*/, openstudio::model::Space /*space*/,
985 openstudio::model::Surface /*surface*/, openstudio::model::SubSurface /*subSurface*/) {
986 // Not doing this yet
987 30 return true;
988 }
989
990 3 bool ForwardTranslator::linkInteriorSubSurface(openstudio::model::ThermalZone /*zone*/, openstudio::model::Space /*space*/,
991 openstudio::model::Surface /*surface*/, openstudio::model::SubSurface /*subSurface*/,
992 openstudio::model::SubSurface /*adjacentSubSurface*/, openstudio::model::Surface /*adjacentSurface*/,
993 openstudio::model::Space /*adjacentSpace*/, openstudio::model::ThermalZone /*adjacentZone*/) {
994 // Not doing this yet
995 3 return true;
996 }
997
998 ✗ std::vector<LogMessage> ForwardTranslator::warnings() const {
999 ✗ std::vector<LogMessage> result;
1000
1001 ✗ for (const LogMessage& logMessage : m_logSink.logMessages()) {
1002 ✗ if (logMessage.logLevel() == Warn) {
1003 ✗ result.push_back(logMessage);
1004 }
1005 ✗ }
1006
1007 ✗ return result;
1008 ✗ }
1009
1010 ✗ std::vector<LogMessage> ForwardTranslator::errors() const {
1011 ✗ std::vector<LogMessage> result;
1012
1013 ✗ for (const LogMessage& logMessage : m_logSink.logMessages()) {
1014 ✗ if (logMessage.logLevel() > Warn) {
1015 ✗ result.push_back(logMessage);
1016 }
1017 ✗ }
1018
1019 ✗ return result;
1020 ✗ }
1021
1022 } // namespace contam
1023 } // namespace openstudio
1024