Skip to main content

feynkit_graph/
thresholds.rs

1//! Initial-state attachment trees used to normalize physical threshold sides.
2
3use linnet::half_edge::{
4    HedgeGraph,
5    involution::{EdgeIndex, HedgePair},
6    nodestore::NodeStorageOps,
7    subgraph::{Inclusion, ModifySubSet, SuBitGraph, SubSetOps},
8};
9
10use crate::{EdgeId, FeynmanDiagram};
11
12/// Shared topology cleanup for threshold partitions after initial-state sewing.
13pub trait InitialStateTreeExt {
14    /// Select the vertex crowns adjacent to loop-independent paired edges.
15    /// Threshold sides are normalized during FeynKit finalization; runtime
16    /// threshold queries reuse the same selection without changing those sides.
17    ///
18    /// Prefer the edge's source crown when it touches the initial cut; otherwise
19    /// use its sink crown if that touches the cut. Internal bridges are retained.
20    fn initial_state_tree(
21        &self,
22        initial_state: &SuBitGraph,
23        loop_independent: impl FnMut(EdgeIndex) -> bool,
24    ) -> (SuBitGraph, Vec<EdgeIndex>);
25}
26
27impl<E, V, H, N: NodeStorageOps<NodeData = V>> InitialStateTreeExt for HedgeGraph<E, V, H, N> {
28    fn initial_state_tree(
29        &self,
30        initial_state: &SuBitGraph,
31        mut loop_independent: impl FnMut(EdgeIndex) -> bool,
32    ) -> (SuBitGraph, Vec<EdgeIndex>) {
33        let mut tree_like_edges = Vec::new();
34        let selected = self.full_filter().subtract(initial_state);
35        let mut result = self.empty_subgraph::<SuBitGraph>();
36        for (pair, edge, _) in self.iter_edges_of(&selected) {
37            if let HedgePair::Paired { source, sink } = pair
38                && loop_independent(edge)
39            {
40                // A fixed-momentum bridge can also join internal loop components.
41                // Amputate only an endpoint attached to the initial-state cut.
42                let initial_node = [source, sink]
43                    .into_iter()
44                    .map(|hedge| self.node_id(hedge))
45                    .find(|node| {
46                        self.iter_crown(*node)
47                            .any(|hedge| initial_state.includes(&hedge))
48                    });
49                if let Some(node) = initial_node {
50                    tree_like_edges.push(edge);
51                    for hedge in self.iter_crown(node) {
52                        result.add(hedge);
53                    }
54                }
55            }
56        }
57        (result, tree_like_edges)
58    }
59}
60
61impl FeynmanDiagram {
62    /// Native half-edge selection removed from topology threshold sides.
63    ///
64    /// Sewn external connections define the initial state. The edge list records
65    /// the loop-independent paired edges whose attached crowns form the selection.
66    pub fn initial_state_tree(&self) -> (SuBitGraph, Vec<EdgeId>) {
67        let mut initial_state = self.graph.empty_subgraph::<SuBitGraph>();
68        for (pair, _, edge) in self.graph.iter_edges() {
69            if edge.data.external.is_some()
70                && let HedgePair::Paired { source, sink } = pair
71            {
72                initial_state.add(source);
73                initial_state.add(sink);
74            }
75        }
76        let (tree, edges) = self.graph.initial_state_tree(&initial_state, |edge| {
77            self.loop_momentum_basis.edge_signatures[&EdgeId(edge.0)]
78                .loops
79                .iter()
80                .all(|sign| sign.is_zero())
81        });
82        (tree, edges.into_iter().map(|edge| EdgeId(edge.0)).collect())
83    }
84}
85
86#[cfg(test)]
87mod tests {
88    use super::*;
89    use linnet::half_edge::{
90        builder::HedgeGraphBuilder, involution::Orientation, subgraph::SubSetLike,
91    };
92
93    #[test]
94    fn initial_attachment_tree_retains_internal_bridges() {
95        let mut builder = HedgeGraphBuilder::new();
96        let a = builder.add_node(());
97        let b = builder.add_node(());
98        let c = builder.add_node(());
99        let d = builder.add_node(());
100        builder.add_edge(a, b, (), Orientation::Default); // sewn initial edge
101        builder.add_edge(a, c, (), Orientation::Default); // source touches initial state
102        builder.add_edge(c, d, (), Orientation::Default); // internal bridge: neither endpoint touches the initial cut
103        let graph: HedgeGraph<(), ()> = builder.into();
104        let mut initial: SuBitGraph = graph.empty_subgraph();
105        for (pair, edge, _) in graph.iter_edges() {
106            if edge == EdgeIndex(0)
107                && let HedgePair::Paired { source, sink } = pair
108            {
109                initial.add(source);
110                initial.add(sink);
111            }
112        }
113        let (tree, edges) = graph.initial_state_tree(&initial, |_| true);
114        let mut expected: SuBitGraph = graph.empty_subgraph();
115        for node in [a] {
116            for hedge in graph.iter_crown(node) {
117                expected.add(hedge);
118            }
119        }
120        assert_eq!(tree, expected);
121        assert_eq!(edges, vec![EdgeIndex(1)]);
122        let (empty, edges) = graph.initial_state_tree(&initial, |_| false);
123        assert!(empty.is_empty());
124        assert!(edges.is_empty());
125    }
126}