GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 0.0% 0 / 0 / 288
Functions: 0.0% 0 / 0 / 16
Branches: 0.0% 0 / 0 / 386

src/airflow/contam/SimFile.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 "SimFile.hpp"
7
8 #include <boost/algorithm/string.hpp>
9 #include <boost/algorithm/string/classification.hpp>
10
11 namespace openstudio {
12 namespace contam {
13
14 ✗ int indexOf(const std::vector<int>& list, int target) {
15 ✗ for (size_t i = 0; i < list.size(); ++i) {
16 ✗ if (list[i] == target) {
17 ✗ return i;
18 }
19 }
20 ✗ return -1;
21 }
22
23 ✗ SimFile::SimFile(openstudio::path path) {
24 ✗ m_hasLfr = false;
25 ✗ m_hasNfr = false;
26 ✗ m_hasNcr = false;
27 // For now, we need to cheat and assume that the .lfr etc. actually exist
28 // This means that simread has to have been run for this to work
29 ✗ openstudio::path lfrPath = path.replace_extension(openstudio::toPath("lfr").string());
30 ✗ m_hasLfr = readLfr(openstudio::toString(lfrPath));
31 ✗ openstudio::path nfrPath = path.replace_extension(openstudio::toPath("nfr").string());
32 ✗ m_hasNfr = readNfr(openstudio::toString(nfrPath));
33 ✗ }
34
35 ✗ bool SimFile::computeDateTimes(const std::vector<std::string>& day, const std::vector<std::string>& time) {
36 ✗ int n = std::min((int)day.size(), (int)time.size());
37 ✗ for (int i = 0; i < n; i++) {
38 ✗ std::vector<std::string> split;
39 ✗ boost::split(split, day[i], boost::is_any_of("/"));
40 ✗ if (split.size() != 2) {
41 ✗ return false;
42 }
43
44 unsigned month;
45 unsigned dayOfMonth;
46 try {
47 ✗ month = std::stoul(split[0]);
48 ✗ dayOfMonth = std::stoul(split[1]);
49 // DLM: what about month == 0?
50 ✗ if (month > 12) {
51 ✗ return false;
52 }
53
54 ✗ m_dateTimes.push_back(DateTime(Date(monthOfYear(month), dayOfMonth), Time(time[i])));
55 ✗ } catch (const std::exception&) {
56 ✗ return false;
57 ✗ }
58 ✗ }
59 ✗ return true;
60 }
61
62 ✗ void SimFile::clearLfr() {
63 ✗ m_dP.clear();
64 ✗ m_F0.clear();
65 ✗ m_F1.clear();
66 ✗ }
67
68 ✗ bool SimFile::readLfr(const std::string& fileName) {
69 ✗ clearLfr();
70 ✗ std::vector<std::string> day;
71 ✗ std::vector<std::string> time;
72 ✗ openstudio::filesystem::ifstream file(openstudio::toPath(fileName));
73 ✗ if (!file.is_open()) {
74 ✗ LOG(Error, "Failed to open LFR file '" << fileName << "'");
75 ✗ return false;
76 }
77 ✗ std::map<int, int> nrMap;
78 // Read the header
79 ✗ std::string headerstr;
80 ✗ std::getline(file, headerstr);
81 ✗ std::string header = headerstr;
82 ✗ if (header.empty()) {
83 ✗ LOG(Error, "No data in LFR file '" << fileName << "'");
84 ✗ return false;
85 }
86 ✗ std::vector<std::string> row;
87 ✗ boost::split(row, header, boost::is_any_of("\t"));
88 ✗ unsigned ncols = 6;
89 ✗ if (row.size() != ncols) {
90 ✗ LOG(Error, "LFR file has " << row.size() << " columns, not the expected " << ncols);
91 ✗ return false;
92 }
93 // Read the data
94 ✗ while (file.good()) {
95 ✗ std::string linestr;
96 ✗ std::getline(file, linestr);
97 ✗ std::string line = linestr;
98 ✗ std::vector<std::string> row;
99 ✗ boost::split(row, line, boost::is_any_of("\t"));
100 ✗ if (row.size() != ncols) {
101 ✗ clearLfr();
102 ✗ LOG(Error, "LFR data line has " << row.size() << " columns, not the expected " << ncols);
103 ✗ return false;
104 }
105 ✗ if (!time.empty()) {
106 ✗ if (time[time.size() - 1] != row[1]) {
107 ✗ day.push_back(row[0]);
108 ✗ time.push_back(row[1]);
109 }
110 } else {
111 ✗ day.push_back(row[0]);
112 ✗ time.push_back(row[1]);
113 }
114
115 ✗ int nr = 0;
116 try {
117 ✗ nr = std::stoi(row[2]);
118 ✗ } catch (const std::exception&) {
119 ✗ clearLfr();
120 ✗ LOG(Error, "Invalid link number '" << row[2] << "'");
121 ✗ return false;
122 ✗ }
123 ✗ if (nrMap.find(nr) == nrMap.end()) {
124 ✗ int sz = nrMap.size();
125 ✗ nrMap[nr] = sz;
126 ✗ m_dP.resize(sz + 1);
127 ✗ m_F0.resize(sz + 1);
128 ✗ m_F1.resize(sz + 1);
129 }
130 ✗ double dP = 0;
131 try {
132 ✗ dP = std::stod(row[3]);
133 ✗ } catch (const std::exception&) {
134 ✗ clearLfr();
135 ✗ LOG(Error, "Invalid pressure difference '" << row[3] << "'");
136 ✗ return false;
137 ✗ }
138
139 ✗ double F0 = 0;
140 try {
141 ✗ F0 = std::stod(row[4]);
142 ✗ } catch (const std::exception&) {
143 ✗ clearLfr();
144 ✗ LOG(Error, "Invalid flow 0 '" << row[4] << "'");
145 ✗ return false;
146 ✗ }
147
148 ✗ double F1 = 0;
149 try {
150 ✗ F1 = std::stod(row[5]);
151 ✗ } catch (const std::exception&) {
152 ✗ clearLfr();
153 ✗ LOG(Error, "Invalid flow 1 '" << row[5] << "'");
154 ✗ return false;
155 ✗ }
156
157 ✗ m_dP[nrMap[nr]].push_back(dP);
158 ✗ m_F0[nrMap[nr]].push_back(F0);
159 ✗ m_F1[nrMap[nr]].push_back(F1);
160 ✗ }
161 // Unwind the zone number map;
162 ✗ std::vector<int> keys;
163 ✗ for (const auto& pair : nrMap) {
164 ✗ keys.push_back(pair.first);
165 }
166 ✗ m_pathNr.resize(keys.size());
167 ✗ for (std::vector<int>::size_type i = 0; i < keys.size(); ++i) {
168 ✗ m_pathNr[nrMap[keys[i]]] = keys[i];
169 }
170 ✗ file.close();
171 // Compute the required date/time objects - this needs to be moved elsewhere if the NCR and NFR are also read
172 ✗ if (!computeDateTimes(day, time)) {
173 ✗ clearLfr();
174 ✗ m_dateTimes.clear();
175 ✗ LOG(Error, "Failed to compute date and time objects from LFR input");
176 ✗ return false;
177 }
178 ✗ return true;
179 ✗ }
180
181 ✗ void SimFile::clearNfr() {
182 ✗ m_T.clear();
183 ✗ m_P.clear();
184 ✗ m_D.clear();
185 ✗ }
186
187 ✗ bool SimFile::readNfr(const std::string& fileName) {
188 ✗ clearNfr();
189 ✗ std::vector<std::string> day;
190 ✗ std::vector<std::string> time;
191 ✗ openstudio::filesystem::ifstream file(openstudio::toPath(fileName));
192 ✗ if (file.is_open()) {
193 ✗ LOG(Error, "Failed to open NFR file '" << fileName << "'");
194 ✗ return false;
195 }
196
197 ✗ std::map<int, int> nrMap;
198 // Read the header
199 ✗ std::string headerstr;
200 ✗ std::getline(file, headerstr);
201 ✗ std::string header = headerstr;
202 ✗ if (header.empty()) {
203 ✗ LOG(Error, "No data in LFR file '" << fileName << "'");
204 ✗ return false;
205 }
206 ✗ std::vector<std::string> row;
207 ✗ boost::split(row, header, boost::is_any_of("\t"));
208 ✗ unsigned ncols = 6;
209 ✗ if (row.size() != ncols && row.size() != ncols + 2) {
210 ✗ LOG(Error, "NFR file has " << row.size() << " columns, not the expected " << ncols);
211 ✗ return false;
212 }
213 // Read the data
214 ✗ while (file.good()) {
215 ✗ std::string linestr;
216 ✗ std::getline(file, linestr);
217 ✗ std::string line = linestr;
218
219 ✗ std::vector<std::string> row;
220 ✗ boost::split(row, line, boost::is_any_of("\t"));
221 ✗ if (row.size() != ncols && row.size() != ncols + 2) {
222 ✗ clearNfr();
223 ✗ LOG(Error, "NFR data line has " << row.size() << " columns, not the expected " << ncols);
224 ✗ return false;
225 }
226 ✗ if (!time.empty()) {
227 ✗ if (time[time.size() - 1] != row[1]) {
228 ✗ day.push_back(row[0]);
229 ✗ time.push_back(row[1]);
230 }
231 } else {
232 ✗ day.push_back(row[0]);
233 ✗ time.push_back(row[1]);
234 }
235
236 ✗ int nr = 0;
237 try {
238 ✗ nr = std::stoi(row[2]);
239 ✗ } catch (const std::exception&) {
240 ✗ clearNfr();
241 ✗ LOG(Error, "Invalid node number '" << row[2] << "'");
242 ✗ return false;
243 ✗ }
244 ✗ if (nrMap.find(nr) == nrMap.end()) {
245 ✗ int sz = nrMap.size();
246 ✗ nrMap[nr] = sz;
247 ✗ m_T.resize(sz + 1);
248 ✗ m_P.resize(sz + 1);
249 ✗ m_D.resize(sz + 1);
250 }
251 ✗ double T = 0;
252 try {
253 ✗ T = std::stod(row[3]);
254 ✗ } catch (const std::exception&) {
255 ✗ clearNfr();
256 ✗ LOG(Error, "Invalid temperature '" << row[3] << "'");
257 ✗ return false;
258 ✗ }
259
260 ✗ double P = 0;
261 try {
262 ✗ P = std::stod(row[4]);
263 ✗ } catch (const std::exception&) {
264 ✗ clearNfr();
265 ✗ LOG(Error, "Invalid pressure '" << row[4] << "'");
266 ✗ return false;
267 ✗ }
268
269 ✗ double D = 0;
270 try {
271 ✗ D = std::stod(row[5]);
272 ✗ } catch (const std::exception&) {
273 ✗ if (nr == 0) {
274 ✗ D = 0.0;
275 } else {
276 ✗ clearNfr();
277 ✗ LOG(Error, "Invalid density '" << row[5] << "'");
278 ✗ return false;
279 }
280 ✗ }
281 ✗ m_T[nrMap[nr]].push_back(T);
282 ✗ m_P[nrMap[nr]].push_back(P);
283 ✗ m_D[nrMap[nr]].push_back(D);
284 ✗ }
285 // Unwind the zone number map;
286 ✗ std::vector<int> keys;
287 ✗ for (const auto& pair : nrMap) {
288 ✗ keys.push_back(pair.first);
289 }
290 ✗ m_nodeNr.resize(keys.size());
291 ✗ for (std::vector<int>::size_type i = 0; i < keys.size(); ++i) {
292 ✗ m_nodeNr[nrMap[keys[i]]] = keys[i];
293 }
294 ✗ file.close();
295 // Something should probably be done here to make sure that the times here match up with what we
296 // already have. For now, if nothing is known about the dates, then try to compute it
297 ✗ if (m_dateTimes.empty()) {
298 ✗ if (!computeDateTimes(day, time)) {
299 ✗ clearLfr();
300 ✗ m_dateTimes.clear();
301 ✗ LOG(Error, "Failed to compute date and time objects from NFR input");
302 ✗ return false;
303 }
304 }
305 ✗ return true;
306 ✗ }
307
308 ✗ static openstudio::TimeSeries convertData(std::vector<openstudio::DateTime> inputDateTimes, std::vector<double> inputValues, std::string units) {
309 // Use a per-interval trapezoidal approximation to convert the CONTAM point data into E+ interval data
310 ✗ std::vector<openstudio::DateTime> dateTimes;
311 ✗ std::vector<double> values;
312 ✗ if (inputDateTimes.size() == 1) // Account for steady simulation results
313 {
314 ✗ return openstudio::TimeSeries(inputDateTimes, createVector(inputValues), units);
315 }
316 ✗ for (unsigned i = 1; i < inputDateTimes.size(); i++) {
317 ✗ dateTimes.push_back(inputDateTimes[i]);
318 ✗ values.push_back(0.5 * (inputValues[i - 1] + inputValues[i]));
319 }
320 ✗ return openstudio::TimeSeries(dateTimes, createVector(values), units);
321 ✗ }
322
323 ✗ boost::optional<openstudio::TimeSeries> SimFile::pathDeltaP(int nr) const {
324 ✗ int index = indexOf(m_pathNr, nr);
325 ✗ if (index == -1) {
326 ✗ return {};
327 }
328 ✗ openstudio::TimeSeries series = convertData(m_dateTimes, m_dP[index], "Pa");
329 ✗ return boost::optional<openstudio::TimeSeries>(series);
330 ✗ }
331
332 ✗ boost::optional<openstudio::TimeSeries> SimFile::pathFlow0(int nr) const {
333 ✗ int index = indexOf(m_pathNr, nr);
334 ✗ if (index == -1) {
335 ✗ return {};
336 }
337 ✗ openstudio::TimeSeries series = convertData(m_dateTimes, m_F0[index], "kg/s");
338 ✗ return boost::optional<openstudio::TimeSeries>(series);
339 ✗ }
340
341 ✗ boost::optional<openstudio::TimeSeries> SimFile::pathFlow1(int nr) const {
342 ✗ int index = indexOf(m_pathNr, nr);
343 ✗ if (index == -1) {
344 ✗ return {};
345 }
346 ✗ openstudio::TimeSeries series = convertData(m_dateTimes, m_F1[index], "kg/s");
347 ✗ return boost::optional<openstudio::TimeSeries>(series);
348 ✗ }
349
350 ✗ boost::optional<openstudio::TimeSeries> SimFile::pathFlow(int nr) const {
351 ✗ int index = indexOf(m_pathNr, nr);
352 ✗ if (index == -1) {
353 ✗ return {};
354 }
355 ✗ std::vector<double> flow(m_dateTimes.size());
356 ✗ for (unsigned i = 0; i < m_dateTimes.size(); i++) {
357 ✗ flow[i] = m_F0[index][i] + m_F1[index][i];
358 }
359 // Need to confirm that the total flow is F0+F1, since it also could be F0-F1
360 ✗ openstudio::TimeSeries series = convertData(m_dateTimes, flow, "kg/s");
361 ✗ return boost::optional<openstudio::TimeSeries>(series);
362 ✗ }
363
364 ✗ boost::optional<openstudio::TimeSeries> SimFile::nodeTemperature(int nr) const {
365 ✗ int index = indexOf(m_nodeNr, nr);
366 ✗ if (index == -1) {
367 ✗ return {};
368 }
369 ✗ openstudio::TimeSeries series = convertData(m_dateTimes, m_T[index], "K");
370 ✗ return boost::optional<openstudio::TimeSeries>(series);
371 ✗ }
372
373 ✗ boost::optional<openstudio::TimeSeries> SimFile::nodePressure(int nr) const {
374 ✗ int index = indexOf(m_nodeNr, nr);
375 ✗ if (index == -1) {
376 ✗ return {};
377 }
378 ✗ openstudio::TimeSeries series = convertData(m_dateTimes, m_P[index], "Pa");
379 ✗ return boost::optional<openstudio::TimeSeries>(series);
380 ✗ }
381
382 ✗ boost::optional<openstudio::TimeSeries> SimFile::nodeDensity(int nr) const {
383 ✗ int index = indexOf(m_nodeNr, nr);
384 ✗ if (index == -1) {
385 ✗ return {};
386 }
387 ✗ openstudio::TimeSeries series = convertData(m_dateTimes, m_D[index], "kg/m^3");
388 ✗ return boost::optional<openstudio::TimeSeries>(series);
389 ✗ }
390
391 ✗ std::vector<openstudio::DateTime> SimFile::dateTimes() const {
392 ✗ if (m_dateTimes.size() == 1) {
393 ✗ return {m_dateTimes.begin(), m_dateTimes.end()};
394 } else {
395 ✗ return {m_dateTimes.begin() + 1, m_dateTimes.end()};
396 }
397 }
398
399 /*
400
401 This code is a holdover from earlier versions that also read in contaminants. Hopefully, this text-based
402 version will not ever be needed. Also, the current OpenStudio implementation only supports airflow and
403 no contaminant transport.
404
405 bool Contaminants::read(std::string fileName)
406 {
407 bool ok=false;
408 clear();
409 QFile file(fileName);
410 if(file.open(QFile::ReadOnly))
411 {
412 QTextStream textStream(&file);
413 std::string line;
414 std::string header = textStream.readLine();
415 if(header.isNull())
416 return false;
417 std::stringList headerList = header.split('\t');
418 int ncols = headerList.size();
419 for(int i=3;i<ncols;i++)
420 {
421 m_nr.push_back(headerList[i].toInt(&ok));
422 if(!ok)return false;
423 }
424 // The number of zones is ncols - 3 (date,time,ctm) - 1 (ambient)
425 m_mf.resize(ncols-3);
426 while(!(line=textStream.readLine()).isNull())
427 {
428 std::stringList row = line.split('\t');
429 if(row.size() != ncols)
430 {
431 clear();
432 return false;
433 }
434 m_day << row[0];
435 m_time << row[1];
436 for(int i=3;i<ncols;i++)
437 m_mf[i-3] << row[i].toDouble();
438 }
439 file.close();
440 if(!computeSeconds())
441 m_s.clear();
442 ok=true;
443 }
444 return ok;
445 }
446
447 QVector<double> Contaminants::integrate()
448 {
449 QVector<double> value(m_mf.size());
450 int n = m_mf[0].size();
451 for(int i=1;i<m_mf.size();i++)
452 n = qMin(n,m_mf[i].size());
453 n = qMin(n,m_s.size());
454 for(int i=0;i<m_mf.size();i++)
455 {
456 value[i]=0.0;
457 for(int j=1;j<n;j++)
458 {
459 value[i] += 0.5*(m_mf[i][j-1]+m_mf[i][j])*(m_s[j]-m_s[j-1]);
460 }
461 }
462 return value;
463 }
464 */
465
466 } // namespace contam
467 } // namespace openstudio
468