A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
spatial-gaussian-field.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
10
11#include "ns3/rng-seed-manager.h"
12
13#include <cmath>
14#include <cstring>
15#include <utility>
16
17namespace ns3
18{
19
21 : m_salt(static_cast<uint64_t>(salt)),
22 m_generator(generator)
23{
24}
25
26uint64_t
28{
29 x += 0x9E3779B97F4A7C15ULL;
30 x = (x ^ (x >> 30)) * 0xBF58476D1CE4E5B9ULL;
31 x = (x ^ (x >> 27)) * 0x94D049BB133111EBULL;
32 return x ^ (x >> 31);
33}
34
35uint64_t
36SpatialGaussianField::Prefix(uint64_t fieldKey) const
37{
38 uint64_t h = SplitMix64(static_cast<uint64_t>(RngSeedManager::GetSeed()));
39 h = SplitMix64(h ^ (static_cast<uint64_t>(RngSeedManager::GetRun()) + 0x1000ULL));
40 h = SplitMix64(h ^ m_salt);
41 return SplitMix64(h ^ fieldKey);
42}
43
44double
45SpatialGaussianField::CellFromPrefix(uint64_t prefix, int64_t ix, int64_t iy) const
46{
47 uint64_t h = SplitMix64(prefix ^ static_cast<uint64_t>(static_cast<uint32_t>(ix)));
48 h = SplitMix64(h ^ static_cast<uint64_t>(static_cast<uint32_t>(iy)));
49
51 {
52 // Two independent uniforms, u1 in (0,1] and u2 in [0,1), from the
53 // 53-bit mantissa of the mixed state.
54 const double inv = 1.0 / 9007199254740992.0; // 2^-53
55 const double u1 = (static_cast<double>(SplitMix64(h) >> 11) + 1.0) * inv;
56 const double u2 = static_cast<double>(SplitMix64(h + 1) >> 11) * inv;
57 return std::sqrt(-2.0 * std::log(u1)) * std::cos(2.0 * M_PI * u2);
58 }
59
60 const auto sum =
61 static_cast<double>((h & 0xFFFF) + ((h >> 16) & 0xFFFF) + ((h >> 32) & 0xFFFF) + (h >> 48));
62 // Zero-mean: subtract 4 * 65535 / 2; unit variance: divide by
63 // sqrt(4 * (2^32 - 1) / 12).
64 return (sum - 131070.0) * (1.0 / 37837.2267);
65}
66
68SpatialGaussianField::ComputeWindow(const Vector& position, double corrDist)
69{
70 Window win;
71 // Filtering white noise with an exponential kernel of decay length lambda
72 // yields a (1 + tau/lambda) * exp(-tau/lambda) autocorrelation in the
73 // continuum; the half-lambda grid and the 14-cell truncation below decay
74 // faster, so lambda is scaled by the numerically fitted constant that puts
75 // the 1/e point of the discrete filter at tau = corrDist, matching the
76 // exp(-d/dcor) autocorrelation of TR 38.901.
77 const double lambda = corrDist / 1.996;
78 // Grid spacing of half the kernel length resolves the exponential kernel
79 // shape; the 14-cell window covers the +-3*lambda filter support (the
80 // truncated tail carries weight exp(-3), absorbed by the L2
81 // normalization).
82 const double spacing = 0.5 * lambda;
83 win.ix = static_cast<int64_t>(std::floor(position.x / spacing)) - 6;
84 win.iy = static_cast<int64_t>(std::floor(position.y / spacing)) - 6;
85 double wx2Sum = 0.0;
86 double wy2Sum = 0.0;
87 // win.ix and win.iy are negative near the origin: keep the cell index
88 // arithmetic signed, or the sum wraps to a huge unsigned value.
89 for (int64_t k = 0; std::cmp_less(k, WINDOW_CELLS); k++)
90 {
91 win.wx[k] = std::exp(-std::abs((win.ix + k) * spacing - position.x) / lambda);
92 win.wy[k] = std::exp(-std::abs((win.iy + k) * spacing - position.y) / lambda);
93 wx2Sum += win.wx[k] * win.wx[k];
94 wy2Sum += win.wy[k] * win.wy[k];
95 }
96 // The squared L2 norm of the separable 2D weights factorizes into the
97 // product of the squared 1D norms.
98 win.invL2Norm = 1.0 / std::sqrt(wx2Sum * wy2Sum);
99 return win;
100}
101
102double
103SpatialGaussianField::SampleWindow(uint64_t fieldKey, const Window& window) const
104{
105 return SampleWindowFromPrefix(Prefix(fieldKey), window);
106}
107
108double
109SpatialGaussianField::SampleWindowFromPrefix(uint64_t prefix, const Window& win) const
110{
111 double acc = 0.0;
112 for (int64_t j = 0; std::cmp_less(j, WINDOW_CELLS); j++)
113 {
114 double rowAcc = 0.0;
115 for (int64_t i = 0; std::cmp_less(i, WINDOW_CELLS); i++)
116 {
117 rowAcc += win.wx[i] * CellFromPrefix(prefix, win.ix + i, win.iy + j);
118 }
119 acc += win.wy[j] * rowAcc;
120 }
121 // i.i.d. N(0,1) cell values combined with L2-normalized weights yield an
122 // exactly N(0,1) marginal at every position.
123 return acc * win.invL2Norm;
124}
125
126double
127SpatialGaussianField::Sample(uint64_t fieldKey, const Vector& position, double corrDist) const
128{
129 const uint64_t prefix = Prefix(fieldKey);
130 if (corrDist <= 0.0)
131 {
132 // Single deterministic draw keyed on the bit pattern of the position.
133 int64_t ix;
134 int64_t iy;
135 std::memcpy(&ix, &position.x, sizeof(ix));
136 std::memcpy(&iy, &position.y, sizeof(iy));
137 // CellFromPrefix keeps the low 32 bits of each coordinate: fold the
138 // exponent and high mantissa bits in so they still tell positions apart.
139 return CellFromPrefix(prefix, ix ^ (ix >> 32), iy ^ (iy >> 32));
140 }
141 return SampleWindowFromPrefix(prefix, ComputeWindow(position, corrDist));
142}
143
144double
146 const Vector& position,
147 double corrDist) const
148{
149 return 0.5 * std::erfc(-Sample(fieldKey, position, corrDist) * M_SQRT1_2);
150}
151
152} // namespace ns3
cairo_uint64_t x
_cairo_uint_96by64_32x64_divrem:
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...
double SampleWindowFromPrefix(uint64_t prefix, const Window &window) const
Sample the field through a precomputed filter window.
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,...
uint64_t Prefix(uint64_t fieldKey) const
Hash prefix of one field, mixing the global RNG seed and run, the salt and the field key once for all...
static uint64_t SplitMix64(uint64_t x)
SplitMix64 mixing round.
uint64_t m_salt
class-specific salt
CellGenerator
Per-cell N(0,1) generator of the white-noise background.
@ BoxMuller
Box-Muller transform of two 53-bit uniforms.
double CellFromPrefix(uint64_t prefix, int64_t ix, int64_t iy) const
Deterministic N(0,1) value of one cell of the field.
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.
SpatialGaussianField(Salt salt, CellGenerator generator=CellGenerator::BoxMuller)
Construct a field.
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.
CellGenerator m_generator
per-cell N(0,1) generator
Salt
Salts of the fields of every model sampling a SpatialGaussianField.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Filter window of one sampling position (grid origin, separable weights and their L2 normalization),...
int64_t ix
grid x-coordinate of the first window cell
std::array< double, WINDOW_CELLS > wx
separable filter weights along x
int64_t iy
grid y-coordinate of the first window cell
double invL2Norm
reciprocal L2 norm of the 2D weights
std::array< double, WINDOW_CELLS > wy
separable filter weights along y