Skip to content

Commit 67c9e5b

Browse files
committed
Create test_layout.py
1 parent 860208c commit 67c9e5b

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed

test/test_layout.py

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
from twoaxistracking import layout
2+
from shapely import geometry
3+
import numpy as np
4+
import pytest
5+
6+
7+
@pytest.fixture
8+
def rectangular_geometry():
9+
collector_geometry = geometry.box(-2, -1, 2, 1)
10+
total_collector_area = collector_geometry.area
11+
l_min = layout._calculate_l_min(collector_geometry)
12+
return collector_geometry, total_collector_area, l_min
13+
14+
15+
def test_l_min_rectangle(rectangular_geometry):
16+
# Test calculation of L_min for a rectangular collector
17+
l_min = layout._calculate_l_min(rectangular_geometry[0])
18+
assert l_min == np.sqrt(4**2+2**2)
19+
20+
21+
def test_l_min_circle():
22+
# Test calculation of L_min for a circular collector with radius 1
23+
collector_geometry = geometry.Point(0, 0).buffer(1)
24+
l_min = layout._calculate_l_min(collector_geometry)
25+
assert l_min == 2
26+
27+
28+
def test_l_min_circle_offcenter():
29+
# Test calculation of L_min for a circular collector with radius 1 rotating
30+
# off-center around the point (0, 1)
31+
collector_geometry = geometry.Point(0, 1).buffer(1)
32+
l_min = layout._calculate_l_min(collector_geometry)
33+
assert l_min == 4
34+
35+
36+
def test_l_min_polygon():
37+
# Test calculation of L_min for a polygon
38+
collector_geometry = geometry.Polygon([(-1, -1), (3, 2), (4, 4), (1, 2), (-1, -1)])
39+
l_min = layout._calculate_l_min(collector_geometry)
40+
assert l_min == 2 * np.sqrt(4**2 + 4**2)
41+
42+
43+
def test_square_layout_generation(rectangular_geometry):
44+
# Test square field layout defined using tje built-in layout types
45+
collector_geometry, total_collector_area, L_min = rectangular_geometry
46+
47+
X, Y, Z, tracker_distance, relative_azimuth, relative_slope = \
48+
layout.generate_field_layout(
49+
gcr=0.25, total_collector_area=total_collector_area, L_min=L_min,
50+
neighbor_order=1, layout_type='square',
51+
slope_azimuth=0, slope_tilt=0, plot=False)
52+
assert np.isclose(X, np.array([-5.65685425, 0, 5.65685425, -5.65685425,
53+
5.65685425, -5.65685425, 0, 5.65685425])
54+
).all()
55+
56+
57+
def test_layout_generation_value_error(rectangular_geometry):
58+
# Test if value errors are correctly raised
59+
collector_geometry, total_collector_area, L_min = rectangular_geometry
60+
61+
# Test if ValueError is raised when an incorrect layout_type is specified
62+
with pytest.raises(ValueError, match="layout type specified was not recognized"):
63+
_ = layout.generate_field_layout(
64+
gcr=0.25, total_collector_area=total_collector_area, L_min=L_min,
65+
neighbor_order=1, layout_type='this_is_not_a_layout_type')
66+
67+
# Test if ValueError is raised if too few layout parameters are specified
68+
with pytest.raises(ValueError, match="no layout type has not been selected"):
69+
_ = layout.generate_field_layout(
70+
gcr=0.25, total_collector_area=total_collector_area, L_min=L_min,
71+
neighbor_order=1, rotation=0)
72+
73+
# Test if ValueError is raised if offset is out of range
74+
with pytest.raises(ValueError, match="offset is outside the valid range"):
75+
_ = layout.generate_field_layout(
76+
gcr=0.25, total_collector_area=total_collector_area, L_min=L_min,
77+
neighbor_order=1, rotation=0, offset=1.1, aspect_ratio=1)
78+
79+
# Test if ValueError is raised if aspect ratio is too low
80+
with pytest.raises(ValueError, match="Aspect ratio is too low"):
81+
_ = layout.generate_field_layout(
82+
gcr=0.25, total_collector_area=total_collector_area, L_min=L_min,
83+
neighbor_order=1, rotation=0, offset=0, aspect_ratio=0.6)
84+
85+
# Test if ValueError is raised if aspect ratio is too high
86+
with pytest.raises(ValueError, match="Aspect ratio is too high"):
87+
_ = layout.generate_field_layout(
88+
gcr=0.25, total_collector_area=total_collector_area, L_min=L_min,
89+
neighbor_order=1, rotation=0, offset=0, aspect_ratio=5)
90+
91+
# Test if ValueError is raised if rotation is outside valid range
92+
with pytest.raises(ValueError, match="rotation is outside the valid range"):
93+
_ = layout.generate_field_layout(
94+
gcr=0.25, total_collector_area=total_collector_area, L_min=L_min,
95+
neighbor_order=1, rotation=190, offset=0, aspect_ratio=1.2)
96+
97+
# Test if ValueError is raised if L_min is outside valid range
98+
with pytest.raises(ValueError, match="Lmin is not physically possible"):
99+
_ = layout.generate_field_layout(
100+
gcr=0.25, total_collector_area=total_collector_area, L_min=1,
101+
neighbor_order=1, rotation=90, offset=0, aspect_ratio=1.2)
102+
103+
# Test if ValueError is raised if rotation is outside valid range
104+
with pytest.raises(ValueError, match="Maximum ground cover ratio exceded"):
105+
_ = layout.generate_field_layout(
106+
gcr=0.5, total_collector_area=total_collector_area, L_min=L_min,
107+
neighbor_order=1, rotation=0, offset=0, aspect_ratio=1)
108+
109+
# Test custom layout
110+
# Test slope
111+
# Test neighbor order
112+
113+
# Inputs (0, negative numbers)
114+
# All types of inputs, e.g., scalars, numpy array and series, list
115+
# Test coverage

0 commit comments

Comments
 (0)