|
| 1 | +from twoaxistracking import layout |
| 2 | +from shapely import geometry |
| 3 | +import numpy as np |
| 4 | +import pytest |
| 5 | + |
| 6 | + |
| 7 | +def test_min_tracker_spacing_rectangle(rectangular_geometry): |
| 8 | + # Test calculation of min_tracker_spacing for a rectangular collector |
| 9 | + min_tracker_spacing = layout._calculate_min_tracker_spacing(rectangular_geometry[0]) |
| 10 | + np.testing.assert_allclose(min_tracker_spacing, np.sqrt(4**2+2**2)) |
| 11 | + |
| 12 | + |
| 13 | +def test_min_tracker_spacing_circle(): |
| 14 | + # Test calculation of min_tracker_spacing for a circular collector with radius 1 |
| 15 | + collector_geometry = geometry.Point(0, 0).buffer(1) |
| 16 | + min_tracker_spacing = layout._calculate_min_tracker_spacing(collector_geometry) |
| 17 | + assert min_tracker_spacing == 2 |
| 18 | + |
| 19 | + |
| 20 | +def test_min_tracker_spacing_circle_offcenter(): |
| 21 | + # Test calculation of min_tracker_spacing for a circular collector with radius 1 rotating |
| 22 | + # off-center around the point (0, 1) |
| 23 | + collector_geometry = geometry.Point(0, 1).buffer(1) |
| 24 | + min_tracker_spacing = layout._calculate_min_tracker_spacing(collector_geometry) |
| 25 | + assert min_tracker_spacing == 4 |
| 26 | + |
| 27 | + |
| 28 | +def test_min_tracker_spacingpolygon(): |
| 29 | + # Test calculation of min_tracker_spacing for a polygon |
| 30 | + collector_geometry = geometry.Polygon([(-1, -1), (3, 2), (4, 4), (1, 2), (-1, -1)]) |
| 31 | + min_tracker_spacing = layout._calculate_min_tracker_spacing(collector_geometry) |
| 32 | + np.testing.assert_allclose(min_tracker_spacing, 2 * np.sqrt(4**2 + 4**2)) |
| 33 | + |
| 34 | + |
| 35 | +def test_square_layout_generation(rectangular_geometry, square_field_layout): |
| 36 | + # Test that a square field layout is returned correctly |
| 37 | + collector_geometry, total_collector_area, min_tracker_spacing = rectangular_geometry |
| 38 | + X_exp, Y_exp, Z_exp, tracker_distance_exp, relative_azimuth_exp, relative_slope_exp = \ |
| 39 | + square_field_layout |
| 40 | + |
| 41 | + X, Y, Z, tracker_distance, relative_azimuth, relative_slope = \ |
| 42 | + layout.generate_field_layout( |
| 43 | + gcr=0.125, |
| 44 | + total_collector_area=total_collector_area, |
| 45 | + min_tracker_spacing=min_tracker_spacing, |
| 46 | + neighbor_order=1, |
| 47 | + aspect_ratio=1, |
| 48 | + offset=0, |
| 49 | + rotation=0) |
| 50 | + np.testing.assert_allclose(X, X_exp) |
| 51 | + np.testing.assert_allclose(Y, Y_exp) |
| 52 | + np.testing.assert_allclose(Z, Z_exp) |
| 53 | + np.testing.assert_allclose(tracker_distance_exp, tracker_distance_exp) |
| 54 | + np.testing.assert_allclose(relative_azimuth, relative_azimuth_exp) |
| 55 | + np.testing.assert_allclose(relative_slope, relative_slope_exp) |
| 56 | + |
| 57 | + |
| 58 | +def test_field_slope(rectangular_geometry, square_field_layout_sloped): |
| 59 | + # Test that a square field layout on tilted surface is returned correctly |
| 60 | + collector_geometry, total_collector_area, min_tracker_spacing = rectangular_geometry |
| 61 | + X_exp, Y_exp, Z_exp, tracker_distance_exp, relative_azimuth_exp, relative_slope_exp = \ |
| 62 | + square_field_layout_sloped |
| 63 | + X, Y, Z, tracker_distance, relative_azimuth, relative_slope = \ |
| 64 | + layout.generate_field_layout( |
| 65 | + gcr=0.125, |
| 66 | + total_collector_area=total_collector_area, |
| 67 | + min_tracker_spacing=min_tracker_spacing, |
| 68 | + neighbor_order=1, |
| 69 | + aspect_ratio=1, |
| 70 | + offset=0, |
| 71 | + rotation=0, |
| 72 | + slope_azimuth=45, |
| 73 | + slope_tilt=5) |
| 74 | + np.testing.assert_allclose(X, X_exp) |
| 75 | + np.testing.assert_allclose(Y, Y_exp) |
| 76 | + np.testing.assert_allclose(Z, Z_exp, atol=10**-9) |
| 77 | + np.testing.assert_allclose(tracker_distance_exp, tracker_distance_exp) |
| 78 | + np.testing.assert_allclose(relative_azimuth, relative_azimuth_exp) |
| 79 | + np.testing.assert_allclose(relative_slope, relative_slope_exp, atol=10**-9) |
| 80 | + |
| 81 | + |
| 82 | +def test_layout_generation_value_error(rectangular_geometry): |
| 83 | + # Test if value errors are correctly raised |
| 84 | + collector_geometry, total_collector_area, min_tracker_spacing = rectangular_geometry |
| 85 | + |
| 86 | + # Test if ValueError is raised if offset is out of range |
| 87 | + with pytest.raises(ValueError, match="offset is outside the valid range"): |
| 88 | + _ = layout.generate_field_layout( |
| 89 | + gcr=0.25, total_collector_area=total_collector_area, |
| 90 | + min_tracker_spacing=min_tracker_spacing, neighbor_order=1, |
| 91 | + aspect_ratio=1, offset=1.1, rotation=0) |
| 92 | + |
| 93 | + # Test if ValueError is raised if aspect ratio is too low |
| 94 | + with pytest.raises(ValueError, match="Aspect ratio is too low"): |
| 95 | + _ = layout.generate_field_layout( |
| 96 | + gcr=0.25, total_collector_area=total_collector_area, |
| 97 | + min_tracker_spacing=min_tracker_spacing, neighbor_order=1, |
| 98 | + aspect_ratio=0.6, offset=0, rotation=0) |
| 99 | + |
| 100 | + # Test if ValueError is raised if aspect ratio is too high |
| 101 | + with pytest.raises(ValueError, match="Aspect ratio is too high"): |
| 102 | + _ = layout.generate_field_layout( |
| 103 | + gcr=0.25, total_collector_area=total_collector_area, |
| 104 | + min_tracker_spacing=min_tracker_spacing, neighbor_order=1, |
| 105 | + aspect_ratio=5, offset=0, rotation=0) |
| 106 | + |
| 107 | + # Test if ValueError is raised if rotation is greater than 180 degrees |
| 108 | + with pytest.raises(ValueError, match="rotation is outside the valid range"): |
| 109 | + _ = layout.generate_field_layout( |
| 110 | + gcr=0.25, total_collector_area=total_collector_area, |
| 111 | + min_tracker_spacing=min_tracker_spacing, neighbor_order=1, |
| 112 | + aspect_ratio=1.2, offset=0, rotation=190) |
| 113 | + |
| 114 | + # Test if ValueError is raised if rotation is less than 0 |
| 115 | + with pytest.raises(ValueError, match="rotation is outside the valid range"): |
| 116 | + _ = layout.generate_field_layout( |
| 117 | + gcr=0.5, total_collector_area=total_collector_area, |
| 118 | + min_tracker_spacing=min_tracker_spacing, neighbor_order=1, |
| 119 | + aspect_ratio=1, offset=0, rotation=-1) |
| 120 | + |
| 121 | + # Test if ValueError is raised if min_tracker_spacing is outside valid range |
| 122 | + with pytest.raises(ValueError, match="Lmin is not physically possible"): |
| 123 | + _ = layout.generate_field_layout( |
| 124 | + gcr=0.25, total_collector_area=total_collector_area, |
| 125 | + min_tracker_spacing=1, neighbor_order=1, aspect_ratio=1.2, |
| 126 | + offset=0, rotation=90) |
| 127 | + |
| 128 | + # Test if ValueError is raised if maximum ground cover ratio is exceeded |
| 129 | + with pytest.raises(ValueError, match="Maximum ground cover ratio exceded"): |
| 130 | + _ = layout.generate_field_layout( |
| 131 | + gcr=0.5, total_collector_area=total_collector_area, |
| 132 | + min_tracker_spacing=min_tracker_spacing, neighbor_order=1, |
| 133 | + aspect_ratio=1, offset=0, rotation=0) |
| 134 | + |
| 135 | + |
| 136 | +def test_neighbor_order(rectangular_geometry): |
| 137 | + collector_geometry, total_collector_area, min_tracker_spacing = rectangular_geometry |
| 138 | + X, Y, Z, tracker_distance, relative_azimuth, relative_slope = \ |
| 139 | + layout.generate_field_layout( |
| 140 | + gcr=0.125, |
| 141 | + total_collector_area=total_collector_area, |
| 142 | + min_tracker_spacing=min_tracker_spacing, |
| 143 | + neighbor_order=3, |
| 144 | + aspect_ratio=1, |
| 145 | + offset=0, |
| 146 | + rotation=0) |
| 147 | + assert len(X) == (7*7-1) |
0 commit comments