1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/// A four-momentum source attached to an initial channel edge.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum InitialMomentum {
/// Use a fixed four-momentum directly.
P4(RealVec4),
/// Use a fixed three-momentum and derive the energy from the particle mass.
Momentum(RealVec3),
/// Sample the energy and orient the momentum along a fixed direction.
EnergyDirection {
/// Energy source.
energy: ScalarSource,
/// Fixed direction of the initial momentum.
direction: RealVec3,
},
}
/// A sampled initial four-momentum and its inverse proposal density.
#[derive(Clone, Copy, Debug)]
pub struct InitialMomentumResult {
/// Sampled on-shell four-momentum in `(E, px, py, pz)` order.
pub p4: RealVec4,
/// Inverse proposal-density correction.
pub weight: f64,
}
impl InitialMomentum {
/// Construct a fixed four-momentum source.
pub fn p4(p4: RealVec4) -> Self {
Self::P4(p4)
}
/// Construct a source from a fixed three-momentum and particle mass.
pub fn momentum(momentum: RealVec3) -> Self {
Self::Momentum(momentum)
}
/// Construct a fixed-energy source along a direction.
pub fn energy_direction(energy: f64, direction: RealVec3) -> Self {
Self::EnergyDirection {
energy: ScalarSource::constant(energy),
direction,
}
}
/// Construct a sampled-energy source along a direction.
pub fn energy_source_direction(energy: ScalarSource, direction: RealVec3) -> Self {
Self::EnergyDirection { energy, direction }
}
/// Validate this source against an edge name and particle definition.
///
/// # Errors
///
/// Returns [`LadduPhysicsError`] when particle mass metadata is missing,
/// momentum components are invalid or off shell, energy support is below
/// threshold, or the direction cannot be normalized.
pub fn validate(
&self,
edge: &str,
properties: Option<&ParticleProperties>,
) -> LadduPhysicsResult<()> {
match self {
Self::P4(p4) => {
let mass = particle_mass(edge, properties)?;
if ![p4.px(), p4.py(), p4.pz(), p4.e()]
.into_iter()
.all(f64::is_finite)
|| p4.e() < 0.0
{
return Err(LadduPhysicsError::invalid_value(
format!("initial four-momentum for edge `{edge}`"),
"finite components and nonnegative energy",
p4,
));
}
let tolerance = 1e-9 * (1.0 + mass * mass + p4.e() * p4.e());
if (p4.m2() - mass * mass).abs() > tolerance {
return Err(LadduPhysicsError::invalid_relation(format!(
"initial edge `{edge}` is off shell: p²={} but mass²={}",
p4.m2(),
mass * mass
)));
}
}
Self::Momentum(momentum) => {
particle_mass(edge, properties)?;
if ![momentum.px(), momentum.py(), momentum.pz()]
.into_iter()
.all(f64::is_finite)
{
return Err(LadduPhysicsError::invalid_value(
format!("initial momentum for edge `{edge}`"),
"finite components",
momentum,
));
}
}
Self::EnergyDirection { energy, direction } => {
let mass = particle_mass(edge, properties)?;
let (minimum, _) = energy.support()?;
if minimum < mass {
return Err(LadduPhysicsError::invalid_value(
format!("energy support for initial edge `{edge}`"),
format!("entirely at or above its particle mass {mass}"),
minimum,
));
}
direction.unit()?;
}
}
Ok(())
}
/// Draw an initial four-momentum after validating its particle definition.
///
/// # Errors
///
/// Returns [`LadduPhysicsError`] when source validation fails or a sampled
/// value cannot produce a physical on-shell momentum.
pub fn sample(
&self,
edge: &str,
properties: Option<&ParticleProperties>,
rng: &mut ProposalRng,
) -> LadduPhysicsResult<InitialMomentumResult> {
self.validate(edge, properties)?;
self.sample_prevalidated(particle_mass(edge, properties)?, rng)
}
/// Sample after channel validation has already established source invariants.
///
/// # Errors
///
/// Returns [`LadduPhysicsError`] when a scalar source cannot be sampled or
/// the supplied mass and sampled energy do not define a physical momentum.
#[doc(hidden)]
pub fn sample_prevalidated(
&self,
mass: f64,
rng: &mut ProposalRng,
) -> LadduPhysicsResult<InitialMomentumResult> {
match self {
Self::P4(p4) => Ok(InitialMomentumResult {
p4: *p4,
weight: 1.0,
}),
Self::Momentum(momentum) => Ok(InitialMomentumResult {
p4: momentum.with_mass(mass),
weight: 1.0,
}),
Self::EnergyDirection { energy, direction } => {
let sampled = energy.sample(rng)?;
if sampled.value < mass {
return Err(LadduPhysicsError::invalid_value(
"sampled initial-state energy",
format!("at or above the particle mass {mass}"),
sampled.value,
));
}
let momentum =
direction.unit()? * (sampled.value * sampled.value - mass * mass).sqrt();
Ok(InitialMomentumResult {
p4: momentum.with_energy(sampled.value),
weight: sampled.weight,
})
}
}
}
}
fn particle_mass(edge: &str, properties: Option<&ParticleProperties>) -> LadduPhysicsResult<f64> {
properties
.ok_or_else(|| {
LadduPhysicsError::invalid_relation(format!(
"initial edge `{edge}` has no particle properties"
))
})?
.mass()
}