src/energyplus/ForwardTranslator/ForwardTranslateScheduleFixedInterval.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/ScheduleFixedInterval.hpp" | ||
| 11 | #include "../../model/ScheduleFixedInterval_Impl.hpp" | ||
| 12 | #include "../../model/ScheduleFile.hpp" | ||
| 13 | #include "../../model/ScheduleFile_Impl.hpp" | ||
| 14 | #include "../../model/ExternalFile.hpp" | ||
| 15 | #include "../../model/ExternalFile_Impl.hpp" | ||
| 16 | |||
| 17 | #include "../../utilities/data/TimeSeries.hpp" | ||
| 18 | #include "../../utilities/filetypes/CSVFile.hpp" | ||
| 19 | |||
| 20 | #include <utilities/idd/Schedule_Compact_FieldEnums.hxx> | ||
| 21 | |||
| 22 | #include "../../utilities/idd/IddEnums.hpp" | ||
| 23 | #include <utilities/idd/IddEnums.hxx> | ||
| 24 | #include <utilities/idd/IddFactory.hxx> | ||
| 25 | |||
| 26 | #include <fmt/format.h> | ||
| 27 | |||
| 28 | using namespace openstudio::model; | ||
| 29 | |||
| 30 | using namespace std; | ||
| 31 | |||
| 32 | namespace openstudio { | ||
| 33 | |||
| 34 | namespace energyplus { | ||
| 35 | |||
| 36 | 1474 | static unsigned startNewDay(IdfObject& idfObject, unsigned fieldIndex, const Date& date) { | |
| 37 |
3/3✓ Branch 1 taken 1474 times.
✓ Branch 4 taken 1474 times.
✓ Branch 7 taken 1474 times.
|
1474 | const std::string s = fmt::format("Through: {:02d}/{:02d}", month(date.monthOfYear()), date.dayOfMonth()); |
| 38 |
1/1✓ Branch 1 taken 1474 times.
|
1474 | idfObject.setString(fieldIndex, s); |
| 39 | 1474 | ++fieldIndex; | |
| 40 |
2/2✓ Branch 1 taken 1474 times.
✓ Branch 4 taken 1474 times.
|
1474 | idfObject.setString(fieldIndex, "For: AllDays"); |
| 41 | 1474 | ++fieldIndex; | |
| 42 | 1474 | return fieldIndex; | |
| 43 | 1474 | } | |
| 44 | |||
| 45 | 18633 | static unsigned addUntil(IdfObject& idfObject, unsigned fieldIndex, int hours, int minutes, double value) { | |
| 46 | 18633 | const std::string s = fmt::format("Until: {:02d}:{:02d}", hours, minutes); | |
| 47 |
1/1✓ Branch 1 taken 18633 times.
|
18633 | idfObject.setString(fieldIndex, s); |
| 48 | 18633 | ++fieldIndex; | |
| 49 |
1/1✓ Branch 1 taken 18633 times.
|
18633 | idfObject.setDouble(fieldIndex, value); |
| 50 | 18633 | ++fieldIndex; | |
| 51 | 18633 | return fieldIndex; | |
| 52 | 18633 | } | |
| 53 | |||
| 54 | 9 | boost::optional<IdfObject> ForwardTranslator::translateScheduleFixedInterval(ScheduleFixedInterval& modelObject) { | |
| 55 |
1/1✓ Branch 1 taken 9 times.
|
9 | const std::string name = modelObject.nameString(); |
| 56 | 9 | boost::optional<ScheduleTypeLimits> scheduleTypeLimits; | |
| 57 | |||
| 58 |
1/1✓ Branch 1 taken 9 times.
|
9 | const TimeSeries timeseries = modelObject.timeSeries(); |
| 59 | // Check that the time series has at least one point | ||
| 60 |
3/4✓ Branch 1 taken 9 times.
✓ Branch 4 taken 9 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 9 times.
|
9 | if (timeseries.values().empty()) { |
| 61 | ✗ | LOG(Error, "Time series in schedule '" << modelObject.name().get() << "' has no values, schedule will not be translated"); | |
| 62 | ✗ | return boost::none; | |
| 63 | } | ||
| 64 | |||
| 65 |
3/3✓ Branch 1 taken 9 times.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 8 times.
|
9 | if (modelObject.translatetoScheduleFile()) { // create a ScheduleFile |
| 66 | |||
| 67 |
2/2✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
|
1 | const openstudio::path fileNamePath = toPath(name + ".csv"); |
| 68 | 1 | openstudio::path filePath; | |
| 69 |
3/3✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
|
1 | std::vector<openstudio::path> absoluteFilePaths = modelObject.model().workflowJSON().absoluteFilePaths(); |
| 70 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
|
1 | if (absoluteFilePaths.empty()) { |
| 71 | ✗ | filePath = modelObject.model().workflowJSON().absoluteRootDir() / fileNamePath; | |
| 72 | } else { | ||
| 73 |
1/1✓ Branch 2 taken 1 time.
|
2 | filePath = absoluteFilePaths[0] / fileNamePath; |
| 74 | } | ||
| 75 | |||
| 76 |
1/1✓ Branch 1 taken 1 time.
|
1 | CSVFile csvFile; |
| 77 |
2/2✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
|
1 | csvFile.addColumn(timeseries.dateTimes()); |
| 78 |
2/2✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
|
1 | csvFile.addColumn(timeseries.values()); |
| 79 |
1/1✓ Branch 1 taken 1 time.
|
1 | csvFile.saveAs(filePath); |
| 80 | |||
| 81 |
3/3✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
|
1 | boost::optional<ExternalFile> externalFile = ExternalFile::getExternalFile(modelObject.model(), toString(filePath)); |
| 82 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
|
1 | if (!externalFile) { |
| 83 | ✗ | LOG(Error, "Cannot find file at '" << filePath << ", schedule will not be translated"); | |
| 84 | ✗ | return boost::none; | |
| 85 | } | ||
| 86 | |||
| 87 | // create ScheduleFile object pointing to ExternalFile | ||
| 88 |
2/2✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
|
1 | ScheduleFile scheduleFile = ScheduleFile(*externalFile, 2); |
| 89 |
2/2✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
|
2 | modelObject.setName("object will not be forward translated"); // otherwise you'd have two model objects with the same name |
| 90 |
1/1✓ Branch 1 taken 1 time.
|
1 | scheduleFile.setName(name); |
| 91 |
1/1✓ Branch 1 taken 1 time.
|
1 | scheduleTypeLimits = modelObject.scheduleTypeLimits(); |
| 92 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
|
1 | if (scheduleTypeLimits) { |
| 93 | ✗ | scheduleFile.setScheduleTypeLimits(*scheduleTypeLimits); | |
| 94 | } | ||
| 95 |
2/2✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
|
1 | scheduleFile.setInterpolatetoTimestep(modelObject.interpolatetoTimestep()); |
| 96 | |||
| 97 |
1/1✓ Branch 1 taken 1 time.
|
1 | return translateAndMapModelObject(scheduleFile); |
| 98 | |||
| 99 | 1 | } else { // create a ScheduleCompact | |
| 100 |
2/2✓ Branch 1 taken 8 times.
✓ Branch 4 taken 8 times.
|
8 | IdfObject idfObject(openstudio::IddObjectType::Schedule_Compact); |
| 101 | |||
| 102 |
1/1✓ Branch 1 taken 8 times.
|
8 | m_idfObjects.push_back(idfObject); |
| 103 | |||
| 104 |
1/1✓ Branch 1 taken 8 times.
|
8 | idfObject.setName(name); |
| 105 | |||
| 106 |
1/1✓ Branch 1 taken 8 times.
|
8 | scheduleTypeLimits = modelObject.scheduleTypeLimits(); |
| 107 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | if (scheduleTypeLimits) { |
| 108 | ✗ | boost::optional<IdfObject> idfScheduleTypeLimits = translateAndMapModelObject(*scheduleTypeLimits); | |
| 109 | ✗ | if (idfScheduleTypeLimits) { | |
| 110 | ✗ | idfObject.setString(Schedule_CompactFields::ScheduleTypeLimitsName, idfScheduleTypeLimits->name().get()); | |
| 111 | } | ||
| 112 | ✗ | } | |
| 113 | |||
| 114 |
1/1✓ Branch 1 taken 8 times.
|
8 | const DateTime firstReportDateTime = timeseries.firstReportDateTime(); |
| 115 |
1/1✓ Branch 1 taken 8 times.
|
8 | const Vector daysFromFirst = timeseries.daysFromFirstReport(); |
| 116 |
1/1✓ Branch 1 taken 8 times.
|
8 | std::vector<long> secondsFromFirst = timeseries.secondsFromFirstReport(); |
| 117 |
1/1✓ Branch 1 taken 8 times.
|
8 | Vector values = timeseries.values(); |
| 118 | |||
| 119 | // We aren't using this - should we? | ||
| 120 |
1/1✓ Branch 1 taken 8 times.
|
8 | std::string interpolateField = "Interpolate:No"; |
| 121 |
2/3✓ Branch 1 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
|
8 | if (modelObject.interpolatetoTimestep()) { |
| 122 | ✗ | interpolateField = "Interpolate:Yes"; | |
| 123 | } | ||
| 124 | |||
| 125 | // New version assumes that the interval is less than one day. | ||
| 126 | // The original version did not, so it was a bit more complicated. | ||
| 127 | // The last date data was written | ||
| 128 |
1/1✓ Branch 1 taken 8 times.
|
8 | Date lastDate = firstReportDateTime.date(); |
| 129 |
1/1✓ Branch 1 taken 8 times.
|
8 | const Time dayDelta = Time(1.0); |
| 130 | // The day number of the date that data was last written relative to the first date | ||
| 131 | //double lastDay = 0.0; | ||
| 132 | 8 | int lastDay = 0; | |
| 133 | // Adjust the floating point day delta to be relative to the beginning of the first day and | ||
| 134 | // shift the start of the loop if needed | ||
| 135 |
2/2✓ Branch 1 taken 8 times.
✓ Branch 4 taken 8 times.
|
8 | const int secondShift = firstReportDateTime.time().totalSeconds(); |
| 136 | 8 | unsigned int start = 0; | |
| 137 | 8 | unsigned int nDays = 1; | |
| 138 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 5 times.
|
8 | if (secondShift == 0) { |
| 139 | 3 | start = 1; | |
| 140 | // JJR: interval lengths of at least one day shouldn't shift the start of the loop, right? why would we ever start with the second element of the values vector? | ||
| 141 |
1/1✓ Branch 1 taken 3 times.
|
3 | const int intervalLength = modelObject.intervalLength(); |
| 142 | |||
| 143 | // If this is an interval representing one or more days | ||
| 144 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | if (intervalLength % 1440 == 0) { |
| 145 | 3 | start = 0; | |
| 146 | 3 | nDays = intervalLength / 1440; | |
| 147 |
1/1✓ Branch 1 taken 3 times.
|
3 | lastDate -= dayDelta; |
| 148 | } | ||
| 149 | } else { | ||
| 150 |
2/2✓ Branch 4 taken 17966 times.
✓ Branch 5 taken 5 times.
|
17971 | for (auto& i : secondsFromFirst) { |
| 151 | 17966 | i += secondShift; | |
| 152 | } | ||
| 153 | } | ||
| 154 | |||
| 155 | // Start the input into the schedule object | ||
| 156 | 8 | unsigned fieldIndex = Schedule_CompactFields::ScheduleTypeLimitsName + 1; | |
| 157 | //idfObject.setString(fieldIndex, interpolateField); | ||
| 158 | //++fieldIndex; | ||
| 159 |
1/1✓ Branch 1 taken 8 times.
|
8 | fieldIndex = startNewDay(idfObject, fieldIndex, lastDate); |
| 160 | |||
| 161 |
3/3✓ Branch 1 taken 18342 times.
✓ Branch 3 taken 18334 times.
✓ Branch 4 taken 8 times.
|
18342 | for (unsigned int i = start; i < values.size() - 1; i++) { |
| 162 | // Loop over the time series values and write out values to the | ||
| 163 | // schedule. This version is based on the seconds from the start | ||
| 164 | // of the time series, so should not be vulnerable to round-off. | ||
| 165 | // It was translated from the day version, so there could be | ||
| 166 | // issues associated with that. | ||
| 167 | // | ||
| 168 | // We still have a potential aliasing problem unless the API has | ||
| 169 | // enforced that the times in the time series are all distinct when | ||
| 170 | // rounded to the minute. Is that happening? | ||
| 171 | 18334 | const int secondsFromStartOfDay = secondsFromFirst[i] % 86400; | |
| 172 | 18334 | const int today = (secondsFromFirst[i] - secondsFromStartOfDay) / 86400; | |
| 173 | // Check to see if we are at the end of a day. | ||
| 174 |
3/4✓ Branch 0 taken 17160 times.
✓ Branch 1 taken 1174 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 17160 times.
|
18334 | if (secondsFromStartOfDay == 0 || secondsFromStartOfDay == 86400) { |
| 175 | // This value is an end of day value, so end the day and set up the next | ||
| 176 | // Note that 00:00:00 counts as the end of the previous day - we only write | ||
| 177 | // out the 24:00:00 value and not both. | ||
| 178 |
2/2✓ Branch 1 taken 1174 times.
✓ Branch 4 taken 1174 times.
|
1174 | fieldIndex = addUntil(idfObject, fieldIndex, 24, 0, values[i]); |
| 179 |
2/2✓ Branch 1 taken 1174 times.
✓ Branch 4 taken 1174 times.
|
1174 | lastDate += dayDelta * nDays; |
| 180 |
1/1✓ Branch 1 taken 1174 times.
|
1174 | fieldIndex = startNewDay(idfObject, fieldIndex, lastDate); |
| 181 | } else { | ||
| 182 | // This still could be on a different day | ||
| 183 |
2/2✓ Branch 0 taken 292 times.
✓ Branch 1 taken 16868 times.
|
17160 | if (today != lastDay) { |
| 184 | // We're on a new day, need a 24:00:00 value and set up the next day | ||
| 185 |
2/2✓ Branch 1 taken 292 times.
✓ Branch 4 taken 292 times.
|
292 | fieldIndex = addUntil(idfObject, fieldIndex, 24, 0, values[i]); |
| 186 |
2/2✓ Branch 1 taken 292 times.
✓ Branch 4 taken 292 times.
|
292 | lastDate += dayDelta * nDays; |
| 187 |
1/1✓ Branch 1 taken 292 times.
|
292 | fieldIndex = startNewDay(idfObject, fieldIndex, lastDate); |
| 188 | } | ||
| 189 |
4/4✓ Branch 1 taken 17160 times.
✓ Branch 4 taken 17160 times.
✓ Branch 6 taken 1 time.
✓ Branch 7 taken 17159 times.
|
17160 | if (values[i] == values[i + 1]) { |
| 190 | // Bail on values that match the next value | ||
| 191 | 1 | continue; | |
| 192 | } | ||
| 193 | // Write out the current entry | ||
| 194 |
1/1✓ Branch 1 taken 17159 times.
|
17159 | const Time time(0, 0, 0, secondsFromStartOfDay); |
| 195 |
1/1✓ Branch 1 taken 17159 times.
|
17159 | int hours = time.hours(); |
| 196 |
2/2✓ Branch 1 taken 17159 times.
✓ Branch 4 taken 17159 times.
|
17159 | int minutes = time.minutes() + static_cast<int>(std::floor((time.seconds() / 60.0) + 0.5)); |
| 197 | // This is a little dangerous, but all of the problematic 24:00 | ||
| 198 | // times that might need to cause a day++ should be caught above. | ||
| 199 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 17159 times.
|
17159 | if (minutes == 60) { |
| 200 | ✗ | hours += 1; | |
| 201 | ✗ | minutes = 0; | |
| 202 | } | ||
| 203 |
2/2✓ Branch 1 taken 17159 times.
✓ Branch 4 taken 17159 times.
|
17159 | fieldIndex = addUntil(idfObject, fieldIndex, hours, minutes, values[i]); |
| 204 | } | ||
| 205 | 18333 | lastDay = today; | |
| 206 | } | ||
| 207 | // Handle the last point a little differently to make sure that the schedule ends exactly on the end of a day | ||
| 208 |
1/1✓ Branch 1 taken 8 times.
|
8 | const unsigned int i = values.size() - 1; |
| 209 | // We'll skip a sanity check here, but it might be a good idea to add one at some point | ||
| 210 |
2/2✓ Branch 1 taken 8 times.
✓ Branch 4 taken 8 times.
|
8 | fieldIndex = addUntil(idfObject, fieldIndex, 24, 0, values[i]); |
| 211 | |||
| 212 |
1/1✓ Branch 1 taken 8 times.
|
8 | return idfObject; |
| 213 | 8 | } | |
| 214 | 9 | } | |
| 215 | |||
| 216 | } // namespace energyplus | ||
| 217 | |||
| 218 | } // namespace openstudio | ||
| 219 |