|
| 1 | +/* |
| 2 | + * Copyright (c) 2026 Centre Tecnologic de Telecomunicacions de Catalunya (CTTC) |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: GPL-2.0-only |
| 5 | + * |
| 6 | + * Author: Gabriel Ferreira <gabrielcarvfer@gmail.com> |
| 7 | + */ |
| 8 | + |
| 9 | +#include "ns3/geocentric-constant-position-mobility-model.h" |
| 10 | +#include "ns3/geographic-positions.h" |
| 11 | +#include "ns3/test.h" |
| 12 | + |
| 13 | +#include <cmath> |
| 14 | + |
| 15 | +using namespace ns3; |
| 16 | + |
| 17 | +/** |
| 18 | + * @ingroup mobility-test |
| 19 | + * |
| 20 | + * @brief Tests GeocentricConstantPositionMobilityModel::GetElevationAngle (@issueid{1308}). |
| 21 | + * |
| 22 | + * The elevation angle is measured at the lower terminal, with the local |
| 23 | + * vertical given by the geocentric radial direction (the model uses a perfect |
| 24 | + * sphere). The angle must be signed (negative when the other terminal is |
| 25 | + * below the local horizon), independent of the argument order, and identical |
| 26 | + * for mirrored geometries in either hemisphere. This test pins that behavior. |
| 27 | + */ |
| 28 | +class GeocentricElevationAngleTestCase : public TestCase |
| 29 | +{ |
| 30 | + public: |
| 31 | + GeocentricElevationAngleTestCase() |
| 32 | + : TestCase("GeocentricConstantPositionMobilityModel elevation angle") |
| 33 | + { |
| 34 | + } |
| 35 | + |
| 36 | + private: |
| 37 | + void DoRun() override; |
| 38 | + |
| 39 | + /// Build a model at the given geographic position. |
| 40 | + /// @param lat latitude (deg) |
| 41 | + /// @param lon longitude (deg) |
| 42 | + /// @param alt altitude (m) |
| 43 | + /// @return the configured mobility model |
| 44 | + static Ptr<GeocentricConstantPositionMobilityModel> Make(double lat, double lon, double alt) |
| 45 | + { |
| 46 | + auto m = CreateObject<GeocentricConstantPositionMobilityModel>(); |
| 47 | + m->SetGeographicPosition(Vector(lat, lon, alt)); |
| 48 | + return m; |
| 49 | + } |
| 50 | +}; |
| 51 | + |
| 52 | +void |
| 53 | +GeocentricElevationAngleTestCase::DoRun() |
| 54 | +{ |
| 55 | + constexpr double TOL = 1e-2; // degrees |
| 56 | + const double geoAlt = 35786e3; |
| 57 | + const double leoAlt = 600e3; |
| 58 | + |
| 59 | + // A satellite directly above the ground station is at zenith: 90 degrees. |
| 60 | + auto gs = Make(45.0, 0.0, 0.0); |
| 61 | + auto overhead = Make(45.0, 0.0, geoAlt); |
| 62 | + NS_TEST_ASSERT_MSG_EQ_TOL(gs->GetElevationAngle(overhead), |
| 63 | + 90.0, |
| 64 | + TOL, |
| 65 | + "Satellite at zenith should have 90 degrees elevation"); |
| 66 | + |
| 67 | + // GEO satellite over the equator seen from a ground station at 45 N. |
| 68 | + auto geoSat = Make(0.0, 0.0, geoAlt); |
| 69 | + double elevFromGs = gs->GetElevationAngle(geoSat); |
| 70 | + NS_TEST_ASSERT_MSG_EQ_TOL(elevFromGs, 38.1771, TOL, "Wrong elevation for GEO over the equator"); |
| 71 | + // The result must be independent of the argument order. |
| 72 | + NS_TEST_ASSERT_MSG_EQ_TOL(geoSat->GetElevationAngle(gs), |
| 73 | + elevFromGs, |
| 74 | + TOL, |
| 75 | + "Elevation angle must not depend on the argument order"); |
| 76 | + |
| 77 | + // A terminal below the local horizon must yield a negative elevation. |
| 78 | + auto equatorGs = Make(0.0, 0.0, 0.0); |
| 79 | + auto farSat = Make(0.0, 120.0, leoAlt); |
| 80 | + NS_TEST_ASSERT_MSG_EQ_TOL(equatorGs->GetElevationAngle(farSat), |
| 81 | + -58.5127, |
| 82 | + TOL, |
| 83 | + "Below-horizon terminal should have a negative elevation"); |
| 84 | + |
| 85 | + // Southern-hemisphere mirror of the GEO case: the geometry is symmetric, |
| 86 | + // so the elevation must match the northern result. |
| 87 | + auto southGs = Make(-45.0, 0.0, 0.0); |
| 88 | + NS_TEST_ASSERT_MSG_EQ_TOL(southGs->GetElevationAngle(geoSat), |
| 89 | + elevFromGs, |
| 90 | + TOL, |
| 91 | + "Southern-hemisphere GEO elevation must mirror the northern one"); |
| 92 | + |
| 93 | + // Oblique LEO geometry at high latitude and its southern mirror: the |
| 94 | + // endpoints have genuinely different local verticals, so hemisphere |
| 95 | + // symmetry is a strong discriminator for the vertex selection. |
| 96 | + auto northHighGs = Make(60.0, 0.0, 0.0); |
| 97 | + auto northLeo = Make(50.0, 0.0, leoAlt); |
| 98 | + double northElev = northHighGs->GetElevationAngle(northLeo); |
| 99 | + NS_TEST_ASSERT_MSG_EQ_TOL(northElev, 22.2040, TOL, "Wrong elevation for oblique LEO geometry"); |
| 100 | + auto southHighGs = Make(-60.0, 0.0, 0.0); |
| 101 | + auto southLeo = Make(-50.0, 0.0, leoAlt); |
| 102 | + NS_TEST_ASSERT_MSG_EQ_TOL(southHighGs->GetElevationAngle(southLeo), |
| 103 | + northElev, |
| 104 | + TOL, |
| 105 | + "North and south mirror geometries must have equal elevation"); |
| 106 | + |
| 107 | + // Longitude rotational invariance: shifting both terminals by the same |
| 108 | + // longitude must not change the (purely relative) geometry. |
| 109 | + auto rotatedGs = Make(60.0, 130.0, 0.0); |
| 110 | + auto rotatedLeo = Make(50.0, 130.0, leoAlt); |
| 111 | + NS_TEST_ASSERT_MSG_EQ_TOL(rotatedGs->GetElevationAngle(rotatedLeo), |
| 112 | + northElev, |
| 113 | + TOL, |
| 114 | + "Elevation must be invariant under a common longitude shift"); |
| 115 | + |
| 116 | + // Exact horizon: place the satellite so the line of sight grazes the |
| 117 | + // sphere and the ground station sees it at exactly 0 degrees, pinning the |
| 118 | + // sign boundary between visible and below-horizon terminals. |
| 119 | + const double horizonLon = std::acos(GeographicPositions::EARTH_SPHERE_RADIUS / |
| 120 | + (GeographicPositions::EARTH_SPHERE_RADIUS + leoAlt)) * |
| 121 | + 180.0 / M_PI; |
| 122 | + auto horizonSat = Make(0.0, horizonLon, leoAlt); |
| 123 | + NS_TEST_ASSERT_MSG_EQ_TOL(equatorGs->GetElevationAngle(horizonSat), |
| 124 | + 0.0, |
| 125 | + TOL, |
| 126 | + "Satellite at the geometric horizon should have 0 degrees elevation"); |
| 127 | + |
| 128 | + // Zenith sanity sweep across latitudes. Zenith is degenerate (both radial |
| 129 | + // directions coincide), so this is an invariant check. |
| 130 | + for (double lat : {-80.0, -30.0, 0.0, 30.0, 80.0}) |
| 131 | + { |
| 132 | + NS_TEST_ASSERT_MSG_EQ_TOL(Make(lat, 0.0, 0.0)->GetElevationAngle(Make(lat, 0.0, leoAlt)), |
| 133 | + 90.0, |
| 134 | + TOL, |
| 135 | + "Zenith satellite should have 90 degrees elevation at latitude " |
| 136 | + << lat); |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +/** |
| 141 | + * @ingroup mobility-test |
| 142 | + * |
| 143 | + * @brief Geocentric elevation angle test suite. |
| 144 | + */ |
| 145 | +class GeocentricElevationAngleTestSuite : public TestSuite |
| 146 | +{ |
| 147 | + public: |
| 148 | + GeocentricElevationAngleTestSuite() |
| 149 | + : TestSuite("geocentric-elevation-angle", Type::UNIT) |
| 150 | + { |
| 151 | + AddTestCase(new GeocentricElevationAngleTestCase, TestCase::Duration::QUICK); |
| 152 | + } |
| 153 | +}; |
| 154 | + |
| 155 | +static GeocentricElevationAngleTestSuite |
| 156 | + g_geocentricElevationAngleTestSuite; //!< Static variable for test initialization |
0 commit comments