GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 84.9% 62 / 0 / 73
Functions: 100.0% 3 / 0 / 3
Branches: 72.2% 65 / 0 / 90

src/energyplus/ForwardTranslator/ForwardTranslateScheduleVariableInterval.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
8 #include "../../model/Model.hpp"
9 #include "../../model/ScheduleTypeLimits.hpp"
10 #include "../../model/ScheduleVariableInterval.hpp"
11 #include "../../model/ScheduleVariableInterval_Impl.hpp"
12
13 #include "../../utilities/data/TimeSeries.hpp"
14
15 #include <utilities/idd/Schedule_Compact_FieldEnums.hxx>
16
17 #include "../../utilities/idd/IddEnums.hpp"
18 #include <utilities/idd/IddEnums.hxx>
19 #include <utilities/idd/IddFactory.hxx>
20
21 #include <fmt/format.h>
22
23 using namespace openstudio::model;
24
25 using namespace std;
26
27 namespace openstudio {
28
29 namespace energyplus {
30
31 730 static unsigned startNewDay(IdfObject& idfObject, unsigned fieldIndex, const Date& date) {
32
3/3
✓ Branch 1 taken 730 times.
✓ Branch 4 taken 730 times.
✓ Branch 7 taken 730 times.
730 const std::string s = fmt::format("Through: {:02d}/{:02d}", month(date.monthOfYear()), date.dayOfMonth());
33
1/1
✓ Branch 1 taken 730 times.
730 idfObject.setString(fieldIndex, s);
34 730 ++fieldIndex;
35
2/2
✓ Branch 1 taken 730 times.
✓ Branch 4 taken 730 times.
730 idfObject.setString(fieldIndex, "For: AllDays");
36 730 ++fieldIndex;
37 730 return fieldIndex;
38 730 }
39
40 9624 static unsigned addUntil(IdfObject& idfObject, unsigned fieldIndex, int hours, int minutes, double value) {
41 9624 const std::string s = fmt::format("Until: {:02d}:{:02d}", hours, minutes);
42
1/1
✓ Branch 1 taken 9624 times.
9624 idfObject.setString(fieldIndex, s);
43 9624 ++fieldIndex;
44
1/1
✓ Branch 1 taken 9624 times.
9624 idfObject.setDouble(fieldIndex, value);
45 9624 ++fieldIndex;
46 9624 return fieldIndex;
47 9624 }
48
49 2 boost::optional<IdfObject> ForwardTranslator::translateScheduleVariableInterval(ScheduleVariableInterval& modelObject) {
50
2/2
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
2 IdfObject idfObject(openstudio::IddObjectType::Schedule_Compact);
51
52
1/1
✓ Branch 1 taken 2 times.
2 m_idfObjects.push_back(idfObject);
53
54
3/3
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 2 times.
2 idfObject.setName(modelObject.name().get());
55
56
1/1
✓ Branch 1 taken 2 times.
2 boost::optional<ScheduleTypeLimits> scheduleTypeLimits = modelObject.scheduleTypeLimits();
57
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (scheduleTypeLimits) {
58 ✗ boost::optional<IdfObject> idfScheduleTypeLimits = translateAndMapModelObject(*scheduleTypeLimits);
59 ✗ if (idfScheduleTypeLimits) {
60 ✗ idfObject.setString(Schedule_CompactFields::ScheduleTypeLimitsName, idfScheduleTypeLimits->name().get());
61 }
62 ✗ }
63
64
1/1
✓ Branch 1 taken 2 times.
2 TimeSeries timeseries = modelObject.timeSeries();
65 // Check that the time series has at least one point
66
3/4
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
2 if (timeseries.values().empty()) {
67 ✗ LOG(Error, "Time series in schedule '" << modelObject.name().get() << "' has no values, schedule will not be translated");
68 ✗ return {};
69 }
70
1/1
✓ Branch 1 taken 2 times.
2 DateTime firstReportDateTime = timeseries.firstReportDateTime();
71
1/1
✓ Branch 1 taken 2 times.
2 Vector daysFromFirst = timeseries.daysFromFirstReport();
72
1/1
✓ Branch 1 taken 2 times.
2 std::vector<long> secondsFromFirst = timeseries.secondsFromFirstReport();
73
1/1
✓ Branch 1 taken 2 times.
2 Vector values = timeseries.values();
74
75 // We aren't using this - should we?
76
1/1
✓ Branch 1 taken 2 times.
2 std::string interpolateField = "Interpolate:No";
77
2/3
✓ Branch 1 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
2 if (modelObject.interpolatetoTimestep()) {
78 ✗ interpolateField = "Interpolate:Yes";
79 }
80
81 // New version assumes that the interval is less than one day.
82 // The original version did not, so it was a bit more complicated.
83 // The last date data was written
84
1/1
✓ Branch 1 taken 2 times.
2 Date lastDate = firstReportDateTime.date();
85
1/1
✓ Branch 1 taken 2 times.
2 Time dayDelta = Time(1.0);
86 // The day number of the date that data was last written relative to the first date
87 //double lastDay = 0.0;
88 2 int lastDay = 0;
89 // Adjust the floating point day delta to be relative to the beginning of the first day and
90 // shift the start of the loop if needed
91
2/2
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
2 int secondShift = firstReportDateTime.time().totalSeconds();
92 2 unsigned int start = 0;
93
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (secondShift == 0) {
94 ✗ start = 1;
95 } else {
96
2/2
✓ Branch 1 taken 9260 times.
✓ Branch 2 taken 2 times.
9262 for (unsigned int i = 0; i < secondsFromFirst.size(); i++) {
97 9260 secondsFromFirst[i] += secondShift;
98 }
99 }
100
101 // Start the input into the schedule object
102 2 unsigned fieldIndex = Schedule_CompactFields::ScheduleTypeLimitsName + 1;
103 //idfObject.setString(fieldIndex, interpolateField);
104 //++fieldIndex;
105
1/1
✓ Branch 1 taken 2 times.
2 fieldIndex = startNewDay(idfObject, fieldIndex, lastDate);
106
107
3/3
✓ Branch 1 taken 9260 times.
✓ Branch 3 taken 9258 times.
✓ Branch 4 taken 2 times.
9260 for (unsigned int i = start; i < values.size() - 1; i++) {
108 // Loop over the time series values and write out values to the
109 // schedule. This version is based on the seconds from the start
110 // of the time series, so should not be vulnerable to round-off.
111 // It was translated from the day version, so there could be
112 // issues associated with that.
113 //
114 // We still have a potential aliasing problem unless the API has
115 // enforced that the times in the time series are all distinct when
116 // rounded to the minute. Is that happening?
117 9258 int secondsFromStartOfDay = secondsFromFirst[i] % 86400;
118 9258 int today = (secondsFromFirst[i] - secondsFromStartOfDay) / 86400;
119 // Check to see if we are at the end of a day.
120
3/4
✓ Branch 0 taken 8894 times.
✓ Branch 1 taken 364 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8894 times.
9258 if (secondsFromStartOfDay == 0 || secondsFromStartOfDay == 86400) {
121 // This value is an end of day value, so end the day and set up the next
122 // Note that 00:00:00 counts as the end of the previous day - we only write
123 // out the 24:00:00 value and not both.
124
2/2
✓ Branch 1 taken 364 times.
✓ Branch 4 taken 364 times.
364 fieldIndex = addUntil(idfObject, fieldIndex, 24, 0, values[i]);
125
1/1
✓ Branch 1 taken 364 times.
364 lastDate += dayDelta;
126
1/1
✓ Branch 1 taken 364 times.
364 fieldIndex = startNewDay(idfObject, fieldIndex, lastDate);
127 } else {
128 // This still could be on a different day
129
2/2
✓ Branch 0 taken 364 times.
✓ Branch 1 taken 8530 times.
8894 if (today != lastDay) {
130 // We're on a new day, need a 24:00:00 value and set up the next day
131
2/2
✓ Branch 1 taken 364 times.
✓ Branch 4 taken 364 times.
364 fieldIndex = addUntil(idfObject, fieldIndex, 24, 0, values[i]);
132
1/1
✓ Branch 1 taken 364 times.
364 lastDate += dayDelta;
133
1/1
✓ Branch 1 taken 364 times.
364 fieldIndex = startNewDay(idfObject, fieldIndex, lastDate);
134 }
135
3/4
✓ Branch 1 taken 8894 times.
✓ Branch 4 taken 8894 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 8894 times.
8894 if (values[i] == values[i + 1]) {
136 // Bail on values that match the next value
137 ✗ continue;
138 }
139 // Write out the current entry
140
1/1
✓ Branch 1 taken 8894 times.
8894 Time time(0, 0, 0, secondsFromStartOfDay);
141
1/1
✓ Branch 1 taken 8894 times.
8894 int hours = time.hours();
142
2/2
✓ Branch 1 taken 8894 times.
✓ Branch 4 taken 8894 times.
8894 int minutes = time.minutes() + (int)std::floor((time.seconds() / 60.0) + 0.5);
143 // This is a little dangerous, but all of the problematic 24:00
144 // times that might need to cause a day++ should be caught above.
145
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8894 times.
8894 if (minutes == 60) {
146 ✗ hours += 1;
147 ✗ minutes = 0;
148 }
149
2/2
✓ Branch 1 taken 8894 times.
✓ Branch 4 taken 8894 times.
8894 fieldIndex = addUntil(idfObject, fieldIndex, hours, minutes, values[i]);
150 }
151 9258 lastDay = today;
152 }
153 // Handle the last point a little differently to make sure that the schedule ends exactly on the end of a day
154
1/1
✓ Branch 1 taken 2 times.
2 unsigned int i = values.size() - 1;
155 // We'll skip a sanity check here, but it might be a good idea to add one at some point
156
2/2
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
2 fieldIndex = addUntil(idfObject, fieldIndex, 24, 0, values[i]);
157
158
1/1
✓ Branch 1 taken 2 times.
2 return idfObject;
159 2 }
160
161 } // namespace energyplus
162
163 } // namespace openstudio
164