A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
spatial-gaussian-field-test-suite.cc
Go to the documentation of this file.
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/rng-seed-manager.h"
10#include "ns3/spatial-gaussian-field.h"
11#include "ns3/test.h"
12
13#include <cmath>
14
15/**
16 * @file
17 * @ingroup propagation-tests
18 * SpatialGaussianField test suite.
19 */
20
21namespace ns3
22{
23
24namespace tests
25{
26
27/**
28 * @ingroup propagation-tests
29 * The field must be a pure function of (seed, run, salt, key, position):
30 * repeated evaluations agree, and changing any input changes the value.
31 */
33{
34 public:
36 : TestCase("SpatialGaussianField determinism")
37 {
38 }
39
40 private:
41 void DoRun() override;
42};
43
44void
46{
47 const uint32_t savedSeed = RngSeedManager::GetSeed();
48 const uint64_t savedRun = RngSeedManager::GetRun();
51
56 const Vector pos(17.3, -4.9, 1.5);
57 const double corrDist = 25.0;
58
59 const double ref = f.Sample(7, pos, corrDist);
60 NS_TEST_ASSERT_MSG_EQ(f.Sample(7, pos, corrDist), ref, "Sample is not repeatable");
63 ref,
64 "Sample differs across instances of the same field");
65 NS_TEST_ASSERT_MSG_EQ(f.Sample(7, Vector(pos.x, pos.y, 30.0), corrDist),
66 ref,
67 "Sample depends on the z coordinate");
68 NS_TEST_ASSERT_MSG_NE(f.Sample(8, pos, corrDist), ref, "Sample ignores the field key");
69 NS_TEST_ASSERT_MSG_NE(g.Sample(7, pos, corrDist), ref, "Sample ignores the salt");
70 NS_TEST_ASSERT_MSG_NE(h.Sample(7, pos, corrDist), ref, "Sample ignores the cell generator");
71 NS_TEST_ASSERT_MSG_NE(f.Sample(7, pos, 2 * corrDist),
72 ref,
73 "Sample ignores the correlation distance");
74
76 ref,
77 "SampleWindow disagrees with Sample");
78 NS_TEST_ASSERT_MSG_EQ(f.SampleUniform(7, pos, corrDist),
79 0.5 * std::erfc(-ref * M_SQRT1_2),
80 "SampleUniform is not the normal CDF of Sample");
81
82 // Degenerate correlation distance: repeatable, but uncorrelated.
83 const double d = f.Sample(7, pos, 0.0);
84 NS_TEST_ASSERT_MSG_EQ(f.Sample(7, pos, 0.0), d, "Degenerate sample is not repeatable");
85 NS_TEST_ASSERT_MSG_NE(f.Sample(7, Vector(pos.x + 1e-9, pos.y, pos.z), 0.0),
86 d,
87 "Degenerate sample ignores the position");
88 // Positions differing only in the exponent or high mantissa bits of a
89 // coordinate must not alias.
90 NS_TEST_ASSERT_MSG_NE(f.Sample(7, Vector(1.0, 0.0, 0.0), 0.0),
91 f.Sample(7, Vector(2.0, 0.0, 0.0), 0.0),
92 "Degenerate sample ignores the high bits of x");
93 NS_TEST_ASSERT_MSG_NE(f.Sample(7, Vector(0.0, 1.0, 0.0), 0.0),
94 f.Sample(7, Vector(0.0, 2.0, 0.0), 0.0),
95 "Degenerate sample ignores the high bits of y");
96
98 NS_TEST_ASSERT_MSG_NE(f.Sample(7, pos, corrDist), ref, "Sample ignores the seed");
101 NS_TEST_ASSERT_MSG_NE(f.Sample(7, pos, corrDist), ref, "Sample ignores the run");
102
103 RngSeedManager::SetSeed(savedSeed);
104 RngSeedManager::SetRun(savedRun);
105}
106
107/**
108 * @ingroup propagation-tests
109 * The marginal of the field must be N(0,1), the sampled autocorrelation must
110 * match the exact autocorrelation of the discrete filter, and the exact
111 * autocorrelation must reach 1/e close to the correlation distance, for both
112 * cell generators.
113 */
115{
116 public:
117 /**
118 * Constructor.
119 * @param generator The per-cell generator under test.
120 */
122 : TestCase(generator == SpatialGaussianField::CellGenerator::BoxMuller
123 ? "SpatialGaussianField statistics (Box-Muller cells)"
124 : "SpatialGaussianField statistics (Irwin-Hall cells)"),
125 m_field(SpatialGaussianField::Salt::CHANNEL_PARAMETERS, generator)
126 {
127 }
128
129 private:
130 void DoRun() override;
131
132 /**
133 * Exact autocorrelation of the discrete filter between two positions,
134 * i.e. the normalized inner product of the two filter windows.
135 * @param a The first position.
136 * @param b The second position.
137 * @param corrDist The correlation distance.
138 * @return The autocorrelation.
139 */
140 static double ExactAutocorrelation(const Vector& a, const Vector& b, double corrDist);
141
142 /**
143 * Second position at a separation and angle from a first position.
144 * @param a The first position.
145 * @param separation The separation.
146 * @param theta The angle of the separation vector.
147 * @return The second position.
148 */
149 static Vector Offset(const Vector& a, double separation, double theta);
150
151 SpatialGaussianField m_field; ///< the field under test
152};
153
154double
156 const Vector& b,
157 double corrDist)
158{
159 const auto wa = SpatialGaussianField::ComputeWindow(a, corrDist);
160 const auto wb = SpatialGaussianField::ComputeWindow(b, corrDist);
161 // The windows share the cells whose grid coordinates coincide; the
162 // separable weights factorize the inner product into x and y sums.
163 double sx = 0.0;
164 double sy = 0.0;
165 for (std::size_t i = 0; i < SpatialGaussianField::WINDOW_CELLS; i++)
166 {
167 for (std::size_t k = 0; k < SpatialGaussianField::WINDOW_CELLS; k++)
168 {
169 if (wa.ix + static_cast<int64_t>(i) == wb.ix + static_cast<int64_t>(k))
170 {
171 sx += wa.wx[i] * wb.wx[k];
172 }
173 if (wa.iy + static_cast<int64_t>(i) == wb.iy + static_cast<int64_t>(k))
174 {
175 sy += wa.wy[i] * wb.wy[k];
176 }
177 }
178 }
179 return sx * sy * wa.invL2Norm * wb.invL2Norm;
180}
181
182Vector
183SpatialGaussianFieldStatisticsTestCase::Offset(const Vector& a, double separation, double theta)
184{
185 return Vector(a.x + separation * std::cos(theta), a.y + separation * std::sin(theta), a.z);
186}
187
188void
190{
191 const uint32_t savedSeed = RngSeedManager::GetSeed();
192 const uint64_t savedRun = RngSeedManager::GetRun();
195
196 const uint64_t nFields = 4000;
197 const double corrDist = 20.0;
198 const Vector origin(31.7, -12.3, 0);
199
200 // Marginal moments over independent fields at one position.
201 double sum = 0.0;
202 double sum2 = 0.0;
203 for (uint64_t key = 0; key < nFields; key++)
204 {
205 const double v = m_field.Sample(key, origin, corrDist);
206 sum += v;
207 sum2 += v * v;
208 }
209 const double mean = sum / nFields;
210 const double var = sum2 / nFields - mean * mean;
211 // Standard errors of the mean and variance estimates are 1/sqrt(n) and
212 // sqrt(2/n); the tolerances are about 4 standard errors.
213 NS_TEST_ASSERT_MSG_EQ_TOL(mean, 0.0, 0.07, "Field marginal is not zero-mean");
214 NS_TEST_ASSERT_MSG_EQ_TOL(var, 1.0, 0.1, "Field marginal is not unit-variance");
215
216 // Sampled autocorrelation over independent fields against the exact
217 // autocorrelation of the discrete filter. The separation vector is
218 // rotated per field so the estimate is not tied to the grid axes.
219 for (double ratio : {0.25, 0.5, 1.0, 2.0})
220 {
221 double sumAb = 0.0;
222 double sumA2 = 0.0;
223 double sumB2 = 0.0;
224 double exact = 0.0;
225 for (uint64_t key = 0; key < nFields; key++)
226 {
227 const Vector b = Offset(origin, ratio * corrDist, 2.0 * M_PI * key / nFields);
228 const double va = m_field.Sample(key, origin, corrDist);
229 const double vb = m_field.Sample(key, b, corrDist);
230 sumAb += va * vb;
231 sumA2 += va * va;
232 sumB2 += vb * vb;
233 exact += ExactAutocorrelation(origin, b, corrDist);
234 }
235 const double measured = sumAb / std::sqrt(sumA2 * sumB2);
236 exact /= nFields;
237 // The standard error of the correlation estimate is below 1/sqrt(n).
239 exact,
240 0.05,
241 "Sampled autocorrelation at " << ratio
242 << " correlation distances");
243 }
244
245 // The exact autocorrelation must reach 1/e close to the correlation
246 // distance (the TR 38.901 exp(-d/dcor) convention), averaged over
247 // directions and grid offsets.
248 const uint32_t nDirections = 90;
249 double crossing = 0.0;
250 for (uint32_t k = 0; k < nDirections; k++)
251 {
252 const Vector a(origin.x + 0.37 * k, origin.y + 0.11 * k, 0);
253 const double theta = 2.0 * M_PI * k / nDirections;
254 double lo = 0.0;
255 double hi = 2.0 * corrDist;
256 for (uint32_t it = 0; it < 30; it++)
257 {
258 const double mid = 0.5 * (lo + hi);
259 (ExactAutocorrelation(a, Offset(a, mid, theta), corrDist) > std::exp(-1.0) ? lo : hi) =
260 mid;
261 }
262 crossing += lo;
263 }
264 crossing /= nDirections;
265 NS_TEST_ASSERT_MSG_EQ_TOL(crossing / corrDist,
266 1.0,
267 0.01,
268 "Autocorrelation 1/e point in correlation distances");
269
270 RngSeedManager::SetSeed(savedSeed);
271 RngSeedManager::SetRun(savedRun);
272}
273
274/**
275 * @ingroup propagation-tests
276 * SpatialGaussianField test suite.
277 */
293
294/// Static variable for test initialization.
296
297} // namespace tests
298
299} // namespace ns3
uint32_t v
static void SetRun(uint64_t run)
Set the run number of simulation.
static void SetSeed(uint32_t seed)
Set the seed.
static uint64_t GetRun()
Get the current run number.
static uint32_t GetSeed()
Get the current seed value which will be used by all subsequently instantiated RandomVariableStream o...
Stateless, position-keyed, spatially-correlated Gaussian random field.
double SampleUniform(uint64_t fieldKey, const Vector &position, double corrDist) const
Sample the field at a position and map it to a U(0,1) variate through the standard normal CDF,...
CellGenerator
Per-cell N(0,1) generator of the white-noise background.
@ IrwinHall
Standardized sum of the four 16-bit chunks of the hashed state (Irwin-Hall), cheaper than Box-Muller;...
@ BoxMuller
Box-Muller transform of two 53-bit uniforms.
double Sample(uint64_t fieldKey, const Vector &position, double corrDist) const
Sample the spatially-correlated field at a position; a non-positive correlation distance degrades to ...
static constexpr std::size_t WINDOW_CELLS
Number of grid cells per dimension of a filter window.
static Window ComputeWindow(const Vector &position, double corrDist)
Compute the filter window of a sampling position.
double SampleWindow(uint64_t fieldKey, const Window &window) const
Sample the field through a precomputed filter window.
@ SHADOW_FADING
Shadow fading, ThreeGppPropagationLossModel.
@ O2I_PENETRATION
Indoor distance and penetration loss, ThreeGppPropagationLossModel.
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:296
@ QUICK
Fast test.
Definition test.h:1057
TestCase(const TestCase &)=delete
Caller graph was not generated because of its size.
Type
Type of test.
Definition test.h:1271
@ UNIT
This test suite implements a Unit Test.
Definition test.h:1273
TestSuite(std::string name, Type type=Type::UNIT)
Construct a new test suite.
Definition test.cc:494
The field must be a pure function of (seed, run, salt, key, position): repeated evaluations agree,...
void DoRun() override
Implementation to actually run this TestCase.
The marginal of the field must be N(0,1), the sampled autocorrelation must match the exact autocorrel...
SpatialGaussianFieldStatisticsTestCase(SpatialGaussianField::CellGenerator generator)
Constructor.
void DoRun() override
Implementation to actually run this TestCase.
static double ExactAutocorrelation(const Vector &a, const Vector &b, double corrDist)
Exact autocorrelation of the discrete filter between two positions, i.e.
static Vector Offset(const Vector &a, double separation, double theta)
Second position at a separation and angle from a first position.
#define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
Definition test.h:133
#define NS_TEST_ASSERT_MSG_NE(actual, limit, msg)
Test that an actual and expected (limit) value are not equal and report and abort if not.
Definition test.h:553
#define NS_TEST_ASSERT_MSG_EQ_TOL(actual, limit, tol, msg)
Test that actual and expected (limit) values are equal to plus or minus some tolerance and report and...
Definition test.h:326
Namespace for test files, TestCases and TestSuites.
static SpatialGaussianFieldTestSuite g_spatialGaussianFieldTestSuite
Static variable for test initialization.
Every class exported by the ns3 library is enclosed in the ns3 namespace.