GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 100.0% 116 / 0 / 116
Functions: 100.0% 6 / 0 / 6
Branches: 97.3% 36 / 0 / 37

src/isomodel/SolarRadiation.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 "SolarRadiation.hpp"
7
8 #define PI 3.141592653589
9
10 namespace openstudio {
11 namespace isomodel {
12 /**
13 * Surface Azimuths of the "building" to calculate solar radiation for
14 */
15 double SurfaceAzimuths[] = {0, -45, -90, -135, 180, 135, 90, 45};
16
17 3 SolarRadiation::SolarRadiation(const TimeFrame& frame, const EpwData& wdata, double tilt)
18 3 : m_frame(frame),
19 3 m_weatherData(wdata),
20
21 3 m_surfaceTilt(tilt / 2.0), //surface tilt in radians (pi/2 is vertical, 0 is horizontal)
22 3 m_localMeridian(wdata.timezone() * 15.0), //compute the local meridian from the time zone. Negative is W of the prime meridian
23 3 m_longitude(wdata.longitude()),
24 3 m_latitude(wdata.latitude() * PI / 180.0), //convert latitute to radians
25
26
2/2
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
6 m_eglobe(TimeFrame::TIMESLICES, std::vector<double>(NUM_SURFACES, 0)),
27
28
1/1
✓ Branch 1 taken 3 times.
6 m_monthlyDryBulbTemp(MONTHS, 0),
29
1/1
✓ Branch 1 taken 3 times.
6 m_monthlyDewPointTemp(MONTHS, 0),
30
1/1
✓ Branch 1 taken 3 times.
6 m_monthlyRelativeHumidity(MONTHS, 0),
31
1/1
✓ Branch 1 taken 3 times.
6 m_monthlyWindspeed(MONTHS, 0),
32
1/1
✓ Branch 1 taken 3 times.
6 m_monthlyGlobalHorizontalRadiation(MONTHS, 0),
33
2/2
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
6 m_monthlySolarRadiation(MONTHS, std::vector<double>(NUM_SURFACES, 0)),
34
2/2
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
6 m_hourlyDryBulbTemp(MONTHS, std::vector<double>(HOURS, 0)),
35
2/2
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
6 m_hourlyDewPointTemp(MONTHS, std::vector<double>(HOURS, 0)),
36
2/2
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
6 m_hourlyGlobalHorizontalRadiation(MONTHS, std::vector<double>(HOURS, 0)) {}
37
38 /**
39 * compute the monthly average solar radiation incident on the vertical surfaces for the
40 * eight primary directions (N, S, E, W, NW, SW, NE, SE)
41 * these computations come from ASHRAE2007 Fundamentals , chapter 14
42 * or Duffie and Beckman "Solar engineering of thermal processes, 3rd ed",
43 * Wiley 2006
44 */
45 3 void SolarRadiation::calculateSurfaceSolarRadiation() {
46 3 double rhog = 0.14; //ground reflectivity coefficient
47
48 3 const std::vector<std::vector<double>>& data = m_weatherData.data();
49 3 const std::vector<double>& vecEB = data[EB];
50 3 const std::vector<double>& vecED = data[ED];
51
2/2
✓ Branch 0 taken 26280 times.
✓ Branch 1 taken 3 times.
26283 for (int i = 0; i < TimeFrame::TIMESLICES; i++) {
52 // First compute the solar azimuth for each hour of the year for our location
53 26280 double Revolution =
54 26280 2.0 * PI * (static_cast<double>(m_frame.YTD[i]) - 1.0) / 365.0; //should be .25? //calculation revolution angle around sun in radians
55 26280 double EquationOfTime = 2.2918
56 26280 * (0.0075 + 0.1868 * std::cos(Revolution) - 3.2077 * std::sin(Revolution) - 1.4615 * std::cos(2 * Revolution)
57 26280 - 4.089 * std::sin(2 * Revolution)); //equation of time??
58 26280 double ApparentSolarTime = m_frame.Hour[i] + EquationOfTime / 60.0 + (m_longitude - m_localMeridian) / 15.0; // Apparent Solar Time in hours
59
60 //the following is a more accurate formula for declination as taken from - Duffie and Beckman P. 14
61 26280 double SolarDeclination = 0.006918 - 0.399913 * std::cos(Revolution) + 0.070257 * std::sin(Revolution) - 0.006758 * std::cos(2.0 * Revolution)
62 26280 + 0.00907 * std::sin(2.0 * Revolution) - 0.002679 * std::cos(3.0 * Revolution)
63 26280 + 0.00148 * std::sin(3.0 * Revolution); //solar declination in radians
64 26280 double SolarHourAngles = 15 * (ApparentSolarTime - 12) * PI / 180.0; //solar hour angle in radians
65 26280 double SolarAltitudeAngles = asin(std::cos(m_latitude) * std::cos(SolarDeclination) * std::cos(SolarHourAngles)
66 26280 + std::sin(m_latitude) * std::sin(SolarDeclination)); //solar altitude angle in radians
67
68 26280 double SolarAzimuthSin = std::sin(SolarHourAngles) * std::cos(SolarDeclination) / std::cos(SolarAltitudeAngles); //sin of the solar azimuth
69 26280 double SolarAzimuthCos =
70 26280 (std::cos(SolarHourAngles) * std::cos(SolarDeclination) * std::sin(m_latitude) - std::sin(SolarDeclination) * std::cos(m_latitude))
71 26280 / std::cos(SolarAltitudeAngles); //cosine of solar azimuth
72 26280 double SolarAzimuth = std::atan2(SolarAzimuthSin, SolarAzimuthCos); //compute solar azimuth in radians
73
74 double GroundReflected =
75 26280 (vecEB[i] * std::sin(SolarAltitudeAngles) + vecED[i]) * rhog * (1 - std::cos(m_surfaceTilt)) / 2; // ground reflected component
76
77 //LOG(Trace, "surfaceRad " << i << " " << Revolution << " " << EquationOfTime << " " << ApparentSolarTime << " " << SolarDeclination << " " << SolarHourAngles << " " << SolarAltitudeAngles << " " << SolarAzimuthSin << " " << SolarAzimuthCos << " " << SolarAzimuth << " " << GroundReflected);
78 26280 std::vector<double>& vecEGI = m_eglobe[i];
79 //then compute the hourly radiation on each vertical surface given the solar azimuth for each hour
80 //std::stringstream ss;
81
2/2
✓ Branch 0 taken 210240 times.
✓ Branch 1 taken 26280 times.
236520 for (int s = 0; s < NUM_SURFACES; s++) {
82 210240 double SurfaceSolarAzimuth =
83 210240 ::fabs(SolarAzimuth - (SurfaceAzimuths[s] * (PI / 180.0))); //surface - solar azimuth in degrees, >pi/2 means surface is in shade
84
85 double AngleOfIncidence =
86 210240 acos(std::cos(SolarAltitudeAngles) * std::cos(SurfaceSolarAzimuth) * std::sin(m_surfaceTilt)
87 210240 + std::sin(SolarAltitudeAngles) * std::cos(m_surfaceTilt)); //ancle of incidence of sun's rays on surface in rad
88
89 210240 double DirectBeam = vecEB[i] * std::max(std::cos(AngleOfIncidence), 0.0); //Beam component of radiation
90
91 420480 double DiffuseRadiation = std::max(0.45, 0.55 + 0.437 * std::cos(AngleOfIncidence)
92 210240 + 0.313 * std::pow(std::cos(AngleOfIncidence), 2.0)); //Diffuse component of radiation
93 //diffuse component for sigma> pi/2 meaning it is a wall tilted outward, for sigma<= pi/2 meaning wall vertical or tilted inward
94
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 210240 times.
210240 double DiffuseComponent = (m_surfaceTilt > PI / 2) ? vecED[i] * DiffuseRadiation * std::sin(m_surfaceTilt)
95 210240 : vecED[i] * (DiffuseRadiation * std::sin(m_surfaceTilt) + std::cos(m_surfaceTilt));
96
97 210240 vecEGI[s] = DirectBeam + DiffuseComponent + GroundReflected; // add up all the components
98 //ss << s << " " << vecEB[i] << " " << SurfaceSolarAzimuth << " " << AngleOfIncidence << " " << DirectBeam << " " << DiffuseRadiation << " " << DiffuseComponent << vecEGI[s] << " ";
99 }
100
101 //LOG(Trace, "SurfaceRadDetail " << ss.str());
102 }
103 3 }
104 //average the data in the bins over the count or days
105 39 void SolarRadiation::calculateMonthAvg(int midx, int cnt) {
106
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 3 times.
39 if (midx > -1) {
107 //average rate by month
108 36 m_monthlyDryBulbTemp[midx] /= cnt;
109 36 m_monthlyDewPointTemp[midx] /= cnt;
110 36 m_monthlyRelativeHumidity[midx] /= cnt;
111 36 m_monthlyWindspeed[midx] /= cnt;
112 36 m_monthlyGlobalHorizontalRadiation[midx] /= cnt;
113
2/2
✓ Branch 0 taken 288 times.
✓ Branch 1 taken 36 times.
324 for (int s = 0; s < NUM_SURFACES; s++) {
114 288 m_monthlySolarRadiation[midx][s] /= cnt;
115 }
116 //hours are averaged over days in the month
117 36 int days = m_frame.monthLength(midx + 1);
118
2/2
✓ Branch 0 taken 864 times.
✓ Branch 1 taken 36 times.
900 for (int h = 0; h < 24; h++) {
119 864 m_hourlyDryBulbTemp[midx][h] /= days;
120 864 m_hourlyDewPointTemp[midx][h] /= days;
121 864 m_hourlyGlobalHorizontalRadiation[midx][h] /= days;
122 }
123 }
124 39 }
125 //Empty bins to start accumulating data for monthly averages
126 36 void SolarRadiation::clearMonthlyAvg(int midx) {
127
2/2
✓ Branch 0 taken 864 times.
✓ Branch 1 taken 36 times.
900 for (int h = 0; h < 24; h++) {
128 864 m_hourlyDryBulbTemp[midx][h] = 0;
129 864 m_hourlyDewPointTemp[midx][h] = 0;
130 864 m_hourlyGlobalHorizontalRadiation[midx][h] = 0;
131 }
132
2/2
✓ Branch 0 taken 288 times.
✓ Branch 1 taken 36 times.
324 for (int s = 0; s < NUM_SURFACES; s++) {
133 288 m_monthlySolarRadiation[midx][s] = 0;
134 }
135 36 m_monthlyDryBulbTemp[midx] = 0;
136 36 m_monthlyDewPointTemp[midx] = 0;
137 36 m_monthlyRelativeHumidity[midx] = 0;
138 36 m_monthlyWindspeed[midx] = 0;
139 36 m_monthlyGlobalHorizontalRadiation[midx] = 0;
140 36 }
141
142 3 void SolarRadiation::calculateAverages() {
143 3 int month = 0;
144 3 int midx = -1;
145 3 int cnt = 0;
146 // int days = 0;
147
148 3 const std::vector<std::vector<double>>& data = m_weatherData.data();
149 3 const std::vector<double>& vecDBT = data[DBT];
150 3 const std::vector<double>& vecDPT = data[DPT];
151 3 const std::vector<double>& vecRH = data[RH];
152
153 3 const std::vector<double>& vecEGH = data[EGH];
154 3 const std::vector<double>& vecWSPD = data[WSPD];
155
156
2/2
✓ Branch 0 taken 26280 times.
✓ Branch 1 taken 3 times.
26283 for (int i = 0; i < TimeFrame::TIMESLICES; i++, cnt++) {
157
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 26244 times.
26280 if (m_frame.Month[i] != month) {
158 36 month = m_frame.Month[i];
159 //on month change, average bin out over count
160 36 calculateMonthAvg(midx, cnt);
161 36 midx++;
162 //reset accumulators
163 36 clearMonthlyAvg(midx);
164 36 cnt = 0;
165 }
166 //accumulate data into bins
167 // cppcheck-suppress negativeContainerIndex
168 26280 m_monthlyDryBulbTemp[midx] += vecDBT[i];
169 // cppcheck-suppress negativeContainerIndex
170 26280 m_monthlyDewPointTemp[midx] += vecDPT[i];
171 // cppcheck-suppress negativeContainerIndex
172 26280 m_monthlyRelativeHumidity[midx] += vecRH[i];
173 // cppcheck-suppress negativeContainerIndex
174 26280 m_monthlyGlobalHorizontalRadiation[midx] += vecEGH[i];
175 // cppcheck-suppress negativeContainerIndex
176 26280 m_monthlyWindspeed[midx] += vecWSPD[i];
177
178 //std::stringstream ss;
179
2/2
✓ Branch 0 taken 210240 times.
✓ Branch 1 taken 26280 times.
236520 for (int s = 0; s < NUM_SURFACES; s++) {
180 // cppcheck-suppress negativeContainerIndex
181 210240 m_monthlySolarRadiation[midx][s] += m_eglobe[i][s];
182 //ss << s << " " << m_monthlySolarRadiation[midx][s] << " " << m_eglobe[i][s] << " ";
183 }
184 //LOG(Trace, "solarRad / eglobe " << ss.str());
185
186 26280 int h = m_frame.Hour[i] - 1;
187 // cppcheck-suppress negativeContainerIndex
188 26280 m_hourlyDryBulbTemp[midx][h] += vecDBT[i];
189 // cppcheck-suppress negativeContainerIndex
190 26280 m_hourlyDewPointTemp[midx][h] += vecDPT[i];
191 // cppcheck-suppress negativeContainerIndex
192 26280 m_hourlyGlobalHorizontalRadiation[midx][h] += vecEGH[i];
193 }
194 //final month average
195 3 calculateMonthAvg(midx, cnt);
196 3 }
197
198 //Calculate hourly solar radiation for each surface
199 //and then calculate the monthly/hourly averages
200 3 void SolarRadiation::Calculate() {
201 3 calculateSurfaceSolarRadiation();
202 3 calculateAverages();
203 3 }
204
205 } // namespace isomodel
206 } // namespace openstudio
207