A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
spatial-gaussian-field.h
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#ifndef SPATIAL_GAUSSIAN_FIELD_H
10#define SPATIAL_GAUSSIAN_FIELD_H
11
12#include "ns3/vector.h"
13
14#include <array>
15#include <cstdint>
16
17namespace ns3
18{
19
20/**
21 * @ingroup propagation
22 * @brief Stateless, position-keyed, spatially-correlated Gaussian random field.
23 *
24 * The value at a point is a pure function of its coordinates, the global RNG
25 * seed and run, a class-specific salt and a per-field key, so every instance
26 * and every call observes the same realization, even when the models sampling
27 * it are regenerated (e.g. by a REM generator). This suits the drop-based
28 * spatial consistency of 3GPP TR 38.901 Sec. 7.6.3.1, and the absence of
29 * mutable state makes the field thread-safe.
30 *
31 * The field is white noise on a fixed grid (one hashed N(0,1) value per cell)
32 * filtered with a separable exponential kernel, L2-normalized so that every
33 * position has an exactly N(0,1) marginal, with the decay length scaled so
34 * that the autocorrelation equals 1/e at the correlation distance as the
35 * exp(-d/dcor) of TR 38.901.
36 *
37 * @note The autocorrelation is not exactly exponential: it is smooth at the
38 * origin, within about 0.04 of exp(-d/dcor) up to three correlation distances
39 * and exactly zero beyond. A spec-exact sum-of-sinusoids field (the QuaDRiGa
40 * approach) would carry a ripple of about 0.05 at every lag and cost more per
41 * sample, which was judged not worth the complexity.
42 */
44{
45 public:
46 /// Per-cell N(0,1) generator of the white-noise background.
47 enum class CellGenerator
48 {
49 BoxMuller, ///< Box-Muller transform of two 53-bit uniforms
50 /// Standardized sum of the four 16-bit chunks of the hashed state (Irwin-Hall),
51 /// cheaper than Box-Muller; the windowed sum of Sample() restores a Gaussian marginal
53 };
54
55 /// Number of grid cells per dimension of a filter window.
56 static constexpr std::size_t WINDOW_CELLS = 14;
57
58 /**
59 * @brief Filter window of one sampling position (grid origin, separable weights and
60 * their L2 normalization), computed once with ComputeWindow() and reusable
61 * with SampleWindow() for every field sampled at that position.
62 */
63 struct Window
64 {
65 int64_t ix{0}; ///< grid x-coordinate of the first window cell
66 int64_t iy{0}; ///< grid y-coordinate of the first window cell
67 std::array<double, WINDOW_CELLS> wx{}; ///< separable filter weights along x
68 std::array<double, WINDOW_CELLS> wy{}; ///< separable filter weights along y
69 double invL2Norm{0}; ///< reciprocal L2 norm of the 2D weights
70 };
71
72 /**
73 * @brief Salts of the fields of every model sampling a SpatialGaussianField.
74 *
75 * The salt is hashed into every cell, so fields of different salts are
76 * statistically independent even when keyed on the same field key (e.g.
77 * the same site). Listing them in one place keeps them distinct.
78 */
79 enum class Salt : uint64_t
80 {
81 LOS_NLOS_STATE = 1, ///< LOS/NLOS state, ThreeGppChannelConditionModel
82 O2I_STATE = 2, ///< Indoor state and building type, ThreeGppChannelConditionModel
83 SHADOW_FADING = 3, ///< Shadow fading, ThreeGppPropagationLossModel
84 O2I_PENETRATION = 4, ///< Indoor distance and penetration loss, ThreeGppPropagationLossModel
85 CHANNEL_PARAMETERS = 5, ///< LSP, cluster and ray variables, ThreeGppChannelModel
86 };
87
88 /**
89 * @brief Construct a field.
90 *
91 * @param salt Salt of the model owning the field, see Salt.
92 * @param generator The per-cell N(0,1) generator.
93 */
94 explicit SpatialGaussianField(Salt salt, CellGenerator generator = CellGenerator::BoxMuller);
95
96 /**
97 * @brief Compute the filter window of a sampling position.
98 *
99 * @param position The sampling position (only x and y are used).
100 * @param corrDist The correlation distance, must be positive.
101 * @return The filter window.
102 */
103 static Window ComputeWindow(const Vector& position, double corrDist);
104
105 /**
106 * @brief Sample the field through a precomputed filter window.
107 *
108 * @param fieldKey Per-field key selecting one independent realization
109 * (e.g. packed site id and slot).
110 * @param window The filter window, see ComputeWindow().
111 * @return A standard-normal sample.
112 */
113 double SampleWindow(uint64_t fieldKey, const Window& window) const;
114
115 /**
116 * @brief Sample the spatially-correlated field at a position; a non-positive
117 * correlation distance degrades to an uncorrelated, position-repeatable draw.
118 *
119 * @param fieldKey Per-field key selecting one independent realization
120 * (e.g. packed site id and slot).
121 * @param position The sampling position (only x and y are used).
122 * @param corrDist The correlation distance at which the autocorrelation
123 * equals 1/e.
124 * @return A standard-normal sample.
125 */
126 double Sample(uint64_t fieldKey, const Vector& position, double corrDist) const;
127
128 /**
129 * @brief Sample the field at a position and map it to a U(0,1) variate
130 * through the standard normal CDF, which preserves the spatial
131 * correlation of the underlying Gaussian field.
132 *
133 * @param fieldKey Per-field key, see Sample().
134 * @param position The sampling position (only x and y are used).
135 * @param corrDist The correlation distance, see Sample().
136 * @return A uniform sample in (0,1).
137 */
138 double SampleUniform(uint64_t fieldKey, const Vector& position, double corrDist) const;
139
140 private:
141 /**
142 * @brief SplitMix64 mixing round.
143 * @param x The state to mix.
144 * @return The mixed state.
145 */
146 static uint64_t SplitMix64(uint64_t x);
147
148 /**
149 * @brief Hash prefix of one field, mixing the global RNG seed and run, the salt
150 * and the field key once for all cells (see CellFromPrefix()).
151 *
152 * @param fieldKey Per-field key, see Sample().
153 * @return The mixed hash state of the field.
154 */
155 uint64_t Prefix(uint64_t fieldKey) const;
156
157 /**
158 * @brief Deterministic N(0,1) value of one cell of the field.
159 *
160 * @param prefix Hash prefix of the field, see Prefix().
161 * @param ix Integer grid x-coordinate of the cell.
162 * @param iy Integer grid y-coordinate of the cell.
163 * @return A standard-normal value deterministic in (prefix, ix, iy).
164 */
165 double CellFromPrefix(uint64_t prefix, int64_t ix, int64_t iy) const;
166
167 /**
168 * @brief Sample the field through a precomputed filter window.
169 *
170 * @param prefix Hash prefix of the field, see Prefix().
171 * @param window The filter window, see ComputeWindow().
172 * @return A standard-normal sample.
173 */
174 double SampleWindowFromPrefix(uint64_t prefix, const Window& window) const;
175
176 uint64_t m_salt; ///< class-specific salt
177 CellGenerator m_generator; ///< per-cell N(0,1) generator
178};
179
180} // namespace ns3
181
182#endif // SPATIAL_GAUSSIAN_FIELD_H
cairo_uint64_t x
_cairo_uint_96by64_32x64_divrem:
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.
@ 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 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.
@ SHADOW_FADING
Shadow fading, ThreeGppPropagationLossModel.
@ O2I_STATE
Indoor state and building type, ThreeGppChannelConditionModel.
@ LOS_NLOS_STATE
LOS/NLOS state, ThreeGppChannelConditionModel.
@ CHANNEL_PARAMETERS
LSP, cluster and ray variables, ThreeGppChannelModel.
@ O2I_PENETRATION
Indoor distance and penetration loss, ThreeGppPropagationLossModel.
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