forked from pvlib/pvlib-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_utils.py
More file actions
192 lines (174 loc) · 8.15 KB
/
test_utils.py
File metadata and controls
192 lines (174 loc) · 8.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
"""
test bifical.utils
"""
import numpy as np
import pytest
from pvlib.bifacial import utils
from pvlib.shading import masking_angle, ground_angle
from pvlib.tools import cosd
from scipy.integrate import trapezoid
@pytest.fixture
def test_system_fixed_tilt():
syst = {'height': 1.0,
'pitch': 2.,
'surface_tilt': 30.,
'surface_azimuth': 180.,
'axis_azimuth': None,
'rotation': -30.}
syst['gcr'] = 1.0 / syst['pitch']
# view factors from 3 points on the ground between rows to the sky
pts = np.linspace(0, 1, num=3)
sqr3 = np.sqrt(3) / 4
# c_i,j = cos(angle from point i to edge of row j), j=0 is row = -1
# c_i,j = cos(angle from point i to edge of row j), j=0 is row = -1
c00 = (-2 - sqr3) / np.sqrt(1.25**2 + (2 + sqr3)**2) # right edge row -1
c01 = -sqr3 / np.sqrt(1.25**2 + sqr3**2) # right edge row 0
c02 = sqr3 / np.sqrt(0.75**2 + sqr3**2) # left edge of row 0
c03 = (2 - sqr3) / np.sqrt(1.25**2 + (2 - sqr3)**2) # right edge of row 1
vf_0 = 0.5 * (c03 - c02 + c01 - c00) # vf at point 0
c10 = (-3 - sqr3) / np.sqrt(1.25**2 + (3 + sqr3)**2) # right edge row -1
c11 = (-1 - sqr3) / np.sqrt(1.25**2 + (1 + sqr3)**2) # right edge row 0
c12 = (-1 + sqr3) / np.sqrt(0.75**2 + (-1 + sqr3)**2) # left edge row 0
c13 = (1 - sqr3) / np.sqrt(1.25**2 + (1 - sqr3)**2) # right edge row
vf_1 = 0.5 * (c13 - c12 + c11 - c10) # vf at point 1
c20 = -(4 + sqr3) / np.sqrt(1.25**2 + (4 + sqr3)**2) # right edge row -1
c21 = (-2 + sqr3) / np.sqrt(0.75**2 + (-2 + sqr3)**2) # left edge row 0
c22 = (-2 - sqr3) / np.sqrt(1.25**2 + (2 + sqr3)**2) # right edge row 0
c23 = (0 - sqr3) / np.sqrt(1.25**2 + (0 - sqr3)**2) # right edge row 1
vf_2 = 0.5 * (c23 - c22 + c21 - c20) # vf at point 1
vfs_ground_sky = np.array([[vf_0], [vf_1], [vf_2]])
return syst, pts, vfs_ground_sky
def test__solar_projection_tangent():
tan_phi_f = utils._solar_projection_tangent(
30, 150, 180)
tan_phi_b = utils._solar_projection_tangent(
30, 150, 0)
assert np.allclose(tan_phi_f, 0.5)
assert np.allclose(tan_phi_b, -0.5)
assert np.allclose(tan_phi_f, -tan_phi_b)
@pytest.mark.parametrize(
"gcr,surface_tilt,surface_azimuth,solar_zenith,solar_azimuth,expected",
[(0.5, 0., 180., 0., 180., 0.5),
(1.0, 0., 180., 0., 180., 0.0),
(1.0, 90., 180., 0., 180., 1.0),
(0.5, 45., 180., 45., 270., 1.0 - np.sqrt(2) / 4),
(0.5, 45., 180., 90., 180., 0.),
(np.sqrt(2) / 2, 45, 180, 0, 180, 0.5),
(np.sqrt(2) / 2, 45, 180, 45, 180, 0.0),
(np.sqrt(2) / 2, 45, 180, 45, 90, 0.5),
(np.sqrt(2) / 2, 45, 180, 45, 0, 1.0),
(np.sqrt(2) / 2, 45, 180, 45, 135, 0.5 * (1 - np.sqrt(2) / 2)),
])
def test__unshaded_ground_fraction(
surface_tilt, surface_azimuth, solar_zenith, solar_azimuth, gcr,
expected):
# frontside, same for both sides
f_sky_beam_f = utils._unshaded_ground_fraction(
surface_tilt, surface_azimuth, solar_zenith, solar_azimuth, gcr)
assert np.allclose(f_sky_beam_f, expected)
# backside, should be the same as frontside
f_sky_beam_b = utils._unshaded_ground_fraction(
180. - surface_tilt, surface_azimuth - 180., solar_zenith,
solar_azimuth, gcr)
assert np.allclose(f_sky_beam_b, expected)
def test__vf_ground_sky_2d(test_system_fixed_tilt):
# vector input
ts, pts, vfs_gnd_sky = test_system_fixed_tilt
vfs = utils.vf_ground_sky_2d(ts['rotation'], ts['gcr'], pts,
ts['pitch'], ts['height'], max_rows=1)
assert np.allclose(vfs, vfs_gnd_sky, rtol=0.1) # middle point vf is off
# test with singleton x
vf = utils.vf_ground_sky_2d(ts['rotation'], ts['gcr'], pts[0],
ts['pitch'], ts['height'], max_rows=1)
assert np.isclose(vf, vfs_gnd_sky[0])
@pytest.mark.parametrize("vectorize", [True, False])
def test_vf_ground_sky_2d_integ(test_system_fixed_tilt, vectorize):
ts, pts, vfs_gnd_sky = test_system_fixed_tilt
# pass rotation here since max_rows=1 for the hand-solved case in
# the fixture test_system, which means the ground-to-sky view factor
# isn't summed over enough rows for symmetry to hold.
vf_integ = utils.vf_ground_sky_2d_integ(
ts['rotation'], ts['gcr'], ts['height'], ts['pitch'],
max_rows=1, npoints=3, vectorize=vectorize)
expected_vf_integ = trapezoid(vfs_gnd_sky, pts, axis=0)
assert np.isclose(vf_integ, expected_vf_integ, rtol=0.1)
def test_vf_row_sky_2d(test_system_fixed_tilt):
ts, _, _ = test_system_fixed_tilt
# with float input, fx at top of row
vf = utils.vf_row_sky_2d(ts['surface_tilt'], ts['gcr'], 1.)
expected = 0.5 * (1 + cosd(ts['surface_tilt']))
assert np.isclose(vf, expected)
# with array input
fx = np.array([0., 0.5, 1.])
vf = utils.vf_row_sky_2d(ts['surface_tilt'], ts['gcr'], fx)
phi = masking_angle(ts['surface_tilt'], ts['gcr'], fx)
expected = 0.5 * (1 + cosd(ts['surface_tilt'] + phi))
assert np.allclose(vf, expected)
def test_vf_row_sky_2d_integ(test_system_fixed_tilt):
ts, _, _ = test_system_fixed_tilt
# with float input, check end position
with np.errstate(invalid='ignore'):
vf = utils.vf_row_sky_2d_integ(ts['surface_tilt'], ts['gcr'], 1., 1.)
expected = utils.vf_row_sky_2d(ts['surface_tilt'], ts['gcr'], 1.)
assert np.isclose(vf, expected)
# with array input
fx0 = np.array([0., 0.5])
fx1 = np.array([0., 0.8])
with np.errstate(invalid='ignore'):
vf = utils.vf_row_sky_2d_integ(ts['surface_tilt'], ts['gcr'], fx0, fx1)
phi = masking_angle(ts['surface_tilt'], ts['gcr'], fx0[0])
y0 = 0.5 * (1 + cosd(ts['surface_tilt'] + phi))
x = np.arange(fx0[1], fx1[1], 1e-4)
phi_y = masking_angle(ts['surface_tilt'], ts['gcr'], x)
y = 0.5 * (1 + cosd(ts['surface_tilt'] + phi_y))
y1 = trapezoid(y, x) / (fx1[1] - fx0[1])
expected = np.array([y0, y1])
assert np.allclose(vf, expected, rtol=1e-3)
# with defaults (0, 1)
vf = utils.vf_row_sky_2d_integ(ts['surface_tilt'], ts['gcr'])
x = np.arange(0, 1, 1e-4)
phi_y = masking_angle(ts['surface_tilt'], ts['gcr'], x)
y = 0.5 * (1 + cosd(ts['surface_tilt'] + phi_y))
y1 = trapezoid(y, x) / (1 - 0)
assert np.allclose(vf, y1, rtol=1e-3)
def test_vf_row_ground_2d(test_system_fixed_tilt):
ts, _, _ = test_system_fixed_tilt
# with float input, fx at bottom of row
vf = utils.vf_row_ground_2d(ts['surface_tilt'], ts['gcr'], 0.)
expected = 0.5 * (1. - cosd(ts['surface_tilt']))
assert np.isclose(vf, expected)
# with array input
fx = np.array([0., 0.5, 1.0])
vf = utils.vf_row_ground_2d(ts['surface_tilt'], ts['gcr'], fx)
phi = ground_angle(ts['surface_tilt'], ts['gcr'], fx)
expected = 0.5 * (1 - cosd(phi - ts['surface_tilt']))
assert np.allclose(vf, expected)
def test_vf_ground_2d_integ(test_system_fixed_tilt):
ts, _, _ = test_system_fixed_tilt
# with float input, check end position
with np.errstate(invalid='ignore'):
vf = utils.vf_row_ground_2d_integ(ts['surface_tilt'], ts['gcr'],
0., 0.)
expected = utils.vf_row_ground_2d(ts['surface_tilt'], ts['gcr'], 0.)
assert np.isclose(vf, expected)
# with array input
fx0 = np.array([0., 0.5])
fx1 = np.array([0., 0.8])
with np.errstate(invalid='ignore'):
vf = utils.vf_row_ground_2d_integ(ts['surface_tilt'], ts['gcr'],
fx0, fx1)
phi = ground_angle(ts['surface_tilt'], ts['gcr'], fx0[0])
y0 = 0.5 * (1 - cosd(phi - ts['surface_tilt']))
x = np.arange(fx0[1], fx1[1], 1e-4)
phi_y = ground_angle(ts['surface_tilt'], ts['gcr'], x)
y = 0.5 * (1 - cosd(phi_y - ts['surface_tilt']))
y1 = trapezoid(y, x) / (fx1[1] - fx0[1])
expected = np.array([y0, y1])
assert np.allclose(vf, expected, rtol=1e-2)
# with defaults (0, 1)
vf = utils.vf_row_ground_2d_integ(ts['surface_tilt'], ts['gcr'], 0, 1)
x = np.arange(0, 1, 1e-4)
phi_y = ground_angle(ts['surface_tilt'], ts['gcr'], x)
y = 0.5 * (1 - cosd(phi_y - ts['surface_tilt']))
y1 = trapezoid(y, x) / (1 - 0)
assert np.allclose(vf, y1, rtol=1e-2)