grid1d 0.3.6

A mathematically rigorous, type-safe Rust library for 1D grid operations and interval partitions, supporting both native and arbitrary-precision numerics.
Documentation
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
#![deny(rustdoc::broken_intra_doc_links)]

//! Grid union operations for combining multiple grids with bidirectional mappings.
//!
//! This module provides functionality for creating a unified grid from two grids
//! that share the same domain. The unified grid contains all coordinate points
//! from both input grids, with mappings that allow tracking which intervals in
//! each original grid correspond to each unified interval.
//!
//! ## Key Types
//!
//! | Type | Description |
//! |------|-------------|
//! | [`Grid1DUnion`] | Unified grid with bidirectional mappings to original grids |
//! | [`ErrorsGrid1DUnion`] | Error types for union construction failures |
//!
//! ## Core Concept
//!
//! Given two grids A and B over the same domain, the union operation creates a new
//! grid whose coordinate points are the union of points from both grids:
//!
//! ```text
//! Grid A:    |-------|-------|--------|
//!            0      0.5      1      1.5
//!
//! Grid B:    |---|-----------|--------|
//!            0  0.3          1       1.5
//!
//! Union:     |---|---|-------|--------|
//!            0  0.3 0.5      1       1.5
//! ```
//!
//! Each interval in the unified grid maps back to exactly one interval in each
//! original grid, enabling data transfer and interpolation between grids.
//!
//! ## Basic Usage
//!
//! ```rust
//! use grid1d::{*, intervals::*, scalars::*};
//! use sorted_vec::partial::SortedSet;
//! use try_create::TryNew;
//!
//! let domain = IntervalClosed::new(0.0, 2.0);
//!
//! // Create two grids with different discretizations
//! let grid_a = Grid1D::uniform(domain.clone(), NumIntervals::try_new(4).unwrap());
//! let grid_b = Grid1D::<IntervalClosed<f64>>::try_from_sorted(
//!     SortedSet::from_unsorted(vec![0.0, 0.3, 0.8, 1.5, 2.0])
//! ).unwrap();
//!
//! // Create unified grid
//! let union = Grid1DUnion::try_new(&grid_a, &grid_b).unwrap();
//!
//! // Access mappings
//! for (unified_id, a_id, b_id) in union.iter_interval_mappings() {
//!     println!("Unified interval {} maps to A[{}] and B[{}]",
//!              *unified_id.as_ref(), *a_id.as_ref(), *b_id.as_ref());
//! }
//! ```
//!
//! ## Bidirectional Mappings
//!
//! The union maintains mappings from unified intervals to both original grids:
//!
//! ```rust
//! # use grid1d::{*, intervals::*, scalars::*};
//! # use try_create::TryNew;
//! # let domain = IntervalClosed::new(0.0, 2.0);
//! # let grid_a = Grid1D::uniform(domain.clone(), NumIntervals::try_new(4).unwrap());
//! # let grid_b = Grid1D::uniform(domain, NumIntervals::try_new(2).unwrap());
//! # let union = Grid1DUnion::try_new(&grid_a, &grid_b).unwrap();
//! // For any unified interval, find corresponding original intervals
//! let (a_id, b_id) = union.find_original_intervals(&IntervalId::new(0));
//!
//! // Access the mapping arrays directly
//! let mapping_a = union.mapping_to_grid_a();
//! let mapping_b = union.mapping_to_grid_b();
//! ```
//!
//! ## Use Cases
//!
//! ### Multi-Physics Coupling
//! When different physics simulations use different grid resolutions, the union
//! provides a common discretization for data exchange:
//!
//! ```rust
//! # use grid1d::{*, intervals::*, scalars::*};
//! fn couple_physics(
//!     flow_grid: &Grid1D<IntervalClosed<f64>>,
//!     chemistry_grid: &Grid1D<IntervalClosed<f64>>,
//!     flow_data: &[f64],
//!     chemistry_data: &[f64],
//! ) -> Vec<f64> {
//!     let union = Grid1DUnion::try_new(flow_grid, chemistry_grid).unwrap();
//!     let mut coupled = Vec::with_capacity(*union.num_refined_intervals().as_ref());
//!     
//!     for (_, flow_id, chem_id) in union.iter_interval_mappings() {
//!         // Combine data from both physics
//!         let combined = flow_data[*flow_id.as_ref()] + chemistry_data[*chem_id.as_ref()];
//!         coupled.push(combined);
//!     }
//!     
//!     coupled
//! }
//! ```
//!
//! ### Conservative Data Transfer
//! Transfer integral quantities while preserving conservation:
//!
//! ```rust
//! # use grid1d::{*, intervals::*, scalars::*};
//! # use try_create::TryNew;
//! # let domain = IntervalClosed::new(0.0, 1.0);
//! # let source_grid = Grid1D::uniform(domain.clone(), NumIntervals::try_new(2).unwrap());
//! # let target_grid = Grid1D::uniform(domain, NumIntervals::try_new(4).unwrap());
//! # let source_data = vec![1.0, 2.0];
//! let union = Grid1DUnion::try_new(&source_grid, &target_grid).unwrap();
//!
//! // Query intersections with physical subdomain
//! let subdomain = IntervalClosed::new(0.2, 0.8);
//! let intersections = union.find_intersections_with_mappings(&subdomain);
//! ```
//!
//! ## Requirements
//!
//! - **Domain equality**: Both input grids must have exactly the same domain
//! - **Valid grids**: Both inputs must be valid interval partitions
//!
//! ## Error Handling
//!
//! ```rust
//! use grid1d::{*, intervals::*, scalars::*};
//! use try_create::TryNew;
//!
//! let grid_a = Grid1D::uniform(
//!     IntervalClosed::new(0.0, 1.0),
//!     NumIntervals::try_new(4).unwrap()
//! );
//! let grid_b = Grid1D::uniform(
//!     IntervalClosed::new(0.0, 2.0), // Different domain!
//!     NumIntervals::try_new(4).unwrap()
//! );
//!
//! match Grid1DUnion::try_new(&grid_a, &grid_b) {
//!     Ok(_) => println!("Union created"),
//!     Err(ErrorsGrid1DUnion::DomainsMismatch { .. }) => {
//!         println!("Grids have different domains");
//!     }
//!     Err(e) => println!("Other error: {:?}", e),
//! }
//! ```
//!
//! ## Mathematical Properties
//!
//! - **Domain preservation**: `union.domain() == grid_a.domain() == grid_b.domain()`
//! - **Point superset**: Union coordinates ⊇ grid_a coordinates ∪ grid_b coordinates
//! - **Mapping completeness**: Every unified interval maps to valid intervals in both grids
//! - **Refinement property**: The union is a refinement of both input grids
//!
//! ## Performance
//!
//! | Operation | Complexity | Notes |
//! |-----------|------------|-------|
//! | `try_new` | O(n + m) | Merge-sort style coordinate union |
//! | `find_original_intervals` | O(1) | Direct array lookup |
//! | `iter_interval_mappings` | O(k) | k = unified interval count |
//! | `find_intersections_with_mappings` | O(log k + j) | j = intersection count |
//!
//! ## See Also
//!
//! - [`Grid1DRefinement`](super::refinement::Grid1DRefinement) - For subdividing single grids
//! - [`IntervalPartition`] - Trait implemented by unified grids
//! - [`Grid1D`] - Main grid type

use crate::{
    ErrorsGrid1D, Grid1D, IntervalPartition, SubIntervalInPartition,
    coords::Coords1D,
    intervals::bounded::{IntervalFinitePositiveLength, IntervalFinitePositiveLengthTrait},
    operations::refinement::{Grid1DNonUniformRefinement, Grid1DUniformRefinement},
    scalars::{IntervalId, NumIntervals, PositiveNumPoints1D},
    traits::{BuildIntervalInPartition, HasCoords1D, HasDomain1D},
};
use derive_more::Into;
use num_valid::{Constants, core::errors::capture_backtrace};
use serde::{Deserialize, Serialize};
use std::backtrace::Backtrace;
use thiserror::Error;

///  Comprehensive error types for 1D grid union construction and operations.
///
/// [`ErrorsGrid1DUnion`] provides specific error information for all failure modes
/// in grid union operations, enabling robust error handling and debugging when
/// combining multiple 1D grids into a unified partition.
///
/// Grid union operations require strict mathematical consistency between input grids,
/// and this error type captures the specific ways these operations can fail.
///
/// ## Error Philosophy
///
/// The error design follows the library's principle of **mathematical correctness first**:
/// - **Domain compatibility**: Input grids must have identical domains
/// - **Early failure detection**: Errors are caught at construction time, not during use
/// - **Detailed diagnostics**: Each error provides specific information about what went wrong
/// - **Type safety**: Generic over domain types to provide compile-time guarantees
///
/// ## Error Variants
///
/// ### Domain Compatibility Errors
/// - [`ErrorsGrid1DUnion::DomainsMismatch`]: Input grids have incompatible domains
///
/// ### Construction Errors  
/// - [`ErrorsGrid1DUnion::FromGrid1DConstructor`]: Error from underlying unified grid construction
///
/// ## Design Rationale
///
/// ### Why Domain Matching is Required
/// Grid union operations create a **unified partition** that must be mathematically
/// consistent with both input grids. This requires:
/// ```text
/// domain(grid_a) = domain(grid_b) = domain(unified_grid)
/// ```
///
/// If domains don't match, the resulting unified grid would be:
/// - **Mathematically undefined**: Cannot create consistent interval mappings
/// - **Physically meaningless**: Points from different physical domains cannot be combined
/// - **Computationally invalid**: Algorithms expecting domain consistency would fail
///
/// ### Error Propagation Strategy
/// The enum uses `#[from]` delegation for construction errors, ensuring that:
/// - **Root cause preservation**: Original error information is maintained
/// - **Error chain clarity**: Developers can trace errors back to their source
/// - **Consistent error handling**: Same patterns across the library
///
/// ## Usage Examples
///
/// ### Handling Domain Mismatch Errors
/// ```rust
/// use grid1d::{*, intervals::*, scalars::*};
/// use sorted_vec::partial::SortedSet;
///
/// let grid_a = Grid1D::<IntervalClosed<f64>>::try_from_sorted(
///     SortedSet::from_unsorted(vec![0.0, 0.5, 1.0])
/// ).unwrap();
/// let grid_b = Grid1D::<IntervalClosed<f64>>::try_from_sorted(
///     SortedSet::from_unsorted(vec![0.0, 1.0, 2.0])
/// ).unwrap();
///
/// match Grid1DUnion::try_new(&grid_a, &grid_b) {
///     Err(ErrorsGrid1DUnion::DomainsMismatch {
///         domain_grid_a,
///         domain_grid_b,
///         ..
///     }) => {
///         println!("Cannot union grids with different domains:");
///         println!("  Grid A domain: [{}, {}]",
///                  domain_grid_a.lower_bound_value(),
///                  domain_grid_a.upper_bound_value());
///         println!("  Grid B domain: [{}, {}]",
///                  domain_grid_b.lower_bound_value(),
///                  domain_grid_b.upper_bound_value());
///         
///         // Suggest potential fixes
///         println!("Potential solutions:");
///         println!("  1. Ensure both grids cover the same physical domain");
///         println!("  2. Trim grids to their common domain intersection");
///         println!("  3. Extend the smaller grid to match the larger domain");
///     }
///     Ok(union) => {
///         println!("Successfully created grid union");
///     }
///     Err(e) => {
///         println!("Other error: {}", e);
///     }
/// }
/// ```
///
/// ### Handling Propagated Construction Errors
/// ```rust
/// use grid1d::{*, intervals::*, scalars::*};
/// use sorted_vec::partial::SortedSet;
/// use try_create::TryNew;
///
/// fn safe_grid_union<D>(
///     grid_a: &Grid1D<D>,
///     grid_b: &Grid1D<D>,
/// ) -> Result<Grid1DUnion<D>, String>
/// where
///     D: BuildIntervalInPartition + std::fmt::Debug + Clone,
/// {
///     Grid1DUnion::try_new(grid_a, grid_b)
///         .map_err(|e| match e {
///             ErrorsGrid1DUnion::DomainsMismatch { domain_grid_a, domain_grid_b, .. } => {
///                 format!("Domain mismatch: {:?} vs {:?}", domain_grid_a, domain_grid_b)
///             }
///             ErrorsGrid1DUnion::FromGrid1DConstructor { source } => {
///                 format!("Failed to construct unified grid: {}", source)
///             }
///         })
/// }
///
/// // Usage
/// let domain = IntervalClosed::new(0.0, 1.0);
/// let grid_a = Grid1D::uniform(domain.clone(), NumIntervals::try_new(4).unwrap());
/// let grid_b = Grid1D::uniform(domain, NumIntervals::try_new(8).unwrap());
///
/// match safe_grid_union(&grid_a, &grid_b) {
///     Ok(union) => println!("Union successful: {} intervals",
///                          union.num_refined_intervals().as_ref()),
///     Err(msg) => println!("Union failed: {}", msg),
/// }
/// ```
///
/// ### Domain Validation Before Union
/// ```rust
/// use grid1d::{*, intervals::*, scalars::*};
/// use sorted_vec::partial::SortedSet;
///
/// fn validate_grids_for_union<D>(
///     grid_a: &Grid1D<D>,
///     grid_b: &Grid1D<D>,
/// ) -> Result<(), String>
/// where
///     D: BuildIntervalInPartition + PartialEq + std::fmt::Debug,
/// {
///     // Pre-validate domains to provide better error messages
///     if grid_a.domain() != grid_b.domain() {
///         return Err(format!(
///             "Grids have incompatible domains:\n  Grid A: {:?}\n  Grid B: {:?}",
///             grid_a.domain(),
///             grid_b.domain()
///         ));
///     }
///     
///     // Check for degenerate cases
///     if grid_a.num_intervals().as_ref() < &1 || grid_b.num_intervals().as_ref() < &1 {
///         return Err("Both grids must have at least one interval".to_string());
///     }
///     
///     Ok(())
/// }
///
/// fn create_validated_union<D>(
///     grid_a: &Grid1D<D>,
///     grid_b: &Grid1D<D>,
/// ) -> Result<Grid1DUnion<D>, String>
/// where
///     D: BuildIntervalInPartition + PartialEq + std::fmt::Debug + Clone,
/// {
///     // Validate first for better error messages
///     validate_grids_for_union(grid_a, grid_b)?;
///     
///     // Then attempt union
///     Grid1DUnion::try_new(grid_a, grid_b)
///         .map_err(|e| format!("Union failed despite validation: {}", e))
/// }
/// ```
///
/// ### Integration with Error Handling Frameworks
/// ```rust
/// use grid1d::{*, intervals::*, scalars::*};
/// use sorted_vec::partial::SortedSet;
/// use std::collections::HashMap;
///
/// #[derive(Debug)]
/// enum MultiGridError {
///     DomainMismatch(String),
///     ConstructionFailure(String),
///     InvalidInput(String),
/// }
///
/// impl From<ErrorsGrid1DUnion<IntervalClosed<f64>>> for MultiGridError {
///     fn from(error: ErrorsGrid1DUnion<IntervalClosed<f64>>) -> Self {
///         match error {
///             ErrorsGrid1DUnion::DomainsMismatch { domain_grid_a, domain_grid_b, .. } => {
///                 MultiGridError::DomainMismatch(format!(
///                     "Grid domains don't match: [{}, {}] vs [{}, {}]",
///                     domain_grid_a.lower_bound_value(),
///                     domain_grid_a.upper_bound_value(),
///                     domain_grid_b.lower_bound_value(),
///                     domain_grid_b.upper_bound_value()
///                 ))
///             }
///             ErrorsGrid1DUnion::FromGrid1DConstructor { source } => {
///                 MultiGridError::ConstructionFailure(format!(
///                     "Failed to build unified grid: {}", source
///                 ))
///             }
///         }
///     }
/// }
///
/// fn multi_grid_union(
///     grids: &[Grid1D<IntervalClosed<f64>>]
/// ) -> Result<Grid1D<IntervalClosed<f64>>, MultiGridError> {
///     if grids.len() < 2 {
///         return Err(MultiGridError::InvalidInput(
///             "Need at least 2 grids for union".to_string()
///         ));
///     }
///     
///     let (mut result, _, _) = Grid1DUnion::try_new(&grids[0], &grids[1])?.into_parts();
///     
///     for grid in &grids[2..] {
///         let union = Grid1DUnion::try_new(&result, grid)?;
///         (result, _, _) = union.into_parts();
///     }
///     
///     Ok(result)
/// }
/// ```
///
/// ## Error Variant Details
///
/// ### `DomainsMismatch`
///
/// **When it occurs**: When attempting to create a union of grids with different domains.
///
/// **Root cause**: The mathematical requirement that unified grids must have identical domains.
///
/// **Fields**:
/// - `domain_grid_a`: The domain of the first input grid
/// - `domain_grid_b`: The domain of the second input grid  
/// - `backtrace`: Stack trace for debugging (when backtrace capture is enabled)
///
/// **Common scenarios**:
/// - Grids created for different physical regions
/// - Grids with different coordinate systems or units
/// - Programming errors where wrong grids are passed to union
/// - Floating-point precision issues in domain bounds
///
/// **Resolution strategies**:
/// 1. **Domain trimming**: Reduce both grids to their common intersection
/// 2. **Domain extension**: Extend one grid to match the other's domain
/// 3. **Coordinate transformation**: Transform one grid to match the other's coordinate system
/// 4. **Input validation**: Check domain compatibility before attempting union
///
/// ### `FromGrid1DConstructor`
///
/// **When it occurs**: When the unified grid construction fails due to invalid coordinate sets.
///
/// **Root cause**: The merged coordinate set violates [`Grid1D`] construction requirements.
///
/// **Fields**:
/// - `source`: The underlying [`ErrorsGrid1D`] that caused the failure
///
/// **Common scenarios**:
/// - Merged coordinates create an invalid grid structure
/// - Numerical precision issues during coordinate merging
/// - Boundary coordinate mismatches after merging
/// - Memory allocation failures for large unified grids
///
/// **Resolution strategies**:
/// 1. **Input validation**: Verify individual grids before union
/// 2. **Precision management**: Use appropriate scalar types for coordinate precision
/// 3. **Memory management**: Ensure sufficient memory for large unions
/// 4. **Graceful degradation**: Fall back to simpler union strategies
///
/// ## Performance Implications
///
/// ### Error Construction Costs
/// - **Domain comparison**: O(1) for scalar bounds, O(k) for complex domains
/// - **Error message formatting**: Deferred until error is formatted (lazy evaluation)
/// - **Backtrace capture**: Significant overhead when enabled, zero when disabled
/// - **Memory allocation**: Minimal - errors store references and small data
///
/// ### Error Handling Best Practices
/// ```rust
/// use grid1d::{*, intervals::*, scalars::*};
/// use num_valid::core::errors::capture_backtrace;
///
/// // Efficient: Check domains before expensive operations
/// fn efficient_union<D>(
///     grid_a: &Grid1D<D>,
///     grid_b: &Grid1D<D>,
/// ) -> Result<Grid1DUnion<D>, ErrorsGrid1DUnion<D>>
/// where
///     D: BuildIntervalInPartition + PartialEq + Clone,
/// {
///     // Fast domain check before expensive union
///     if grid_a.domain() != grid_b.domain() {
///         return Err(ErrorsGrid1DUnion::DomainsMismatch {
///             domain_grid_a: grid_a.domain().clone(),
///             domain_grid_b: grid_b.domain().clone(),
///             backtrace: capture_backtrace(),
///         });
///     }
///     
///     Grid1DUnion::try_new(grid_a, grid_b)
/// }
///
/// // Memory-efficient: Use references where possible
/// fn analyze_union_error<D>(error: &ErrorsGrid1DUnion<D>) -> String
/// where
///     D: IntervalFinitePositiveLengthTrait,
/// {
///     match error {
///         ErrorsGrid1DUnion::DomainsMismatch { domain_grid_a, domain_grid_b, .. } => {
///             format!("Domain mismatch: {:?} ≠ {:?}", domain_grid_a, domain_grid_b)
///         }
///         ErrorsGrid1DUnion::FromGrid1DConstructor { source } => {
///             format!("Construction error: {}", source)
///         }
///     }
/// }
/// ```
///
/// ## Integration with the other numerical Ecosystem
///
/// ### Error Propagation Patterns
/// ```rust
/// use grid1d::{*, intervals::*, scalars::*};
///
/// // Pattern 1: Direct error propagation
/// fn create_multi_physics_union(
///     fluid_grid: &Grid1D<IntervalClosed<f64>>,
///     thermal_grid: &Grid1D<IntervalClosed<f64>>,
/// ) -> Result<Grid1DUnion<IntervalClosed<f64>>, ErrorsGrid1DUnion<IntervalClosed<f64>>> {
///     Grid1DUnion::try_new(fluid_grid, thermal_grid)
/// }
///
/// // Pattern 2: Error transformation
/// fn create_union_with_context(
///     grid_a: &Grid1D<IntervalClosed<f64>>,
///     grid_b: &Grid1D<IntervalClosed<f64>>,
///     context: &str,
/// ) -> Result<Grid1DUnion<IntervalClosed<f64>>, String> {
///     Grid1DUnion::try_new(grid_a, grid_b)
///         .map_err(|e| format!("Union failed in {}: {}", context, e))
/// }
///
/// // Pattern 3: Error aggregation
/// fn create_union_with_fallback(
///     primary_a: &Grid1D<IntervalClosed<f64>>,
///     primary_b: &Grid1D<IntervalClosed<f64>>,
///     fallback_a: &Grid1D<IntervalClosed<f64>>,
///     fallback_b: &Grid1D<IntervalClosed<f64>>,
/// ) -> Result<Grid1DUnion<IntervalClosed<f64>>, Vec<ErrorsGrid1DUnion<IntervalClosed<f64>>>> {
///     let mut errors = Vec::new();
///     
///     // Try primary union
///     match Grid1DUnion::try_new(primary_a, primary_b) {
///         Ok(union) => return Ok(union),
///         Err(e) => errors.push(e),
///     }
///     
///     // Try fallback union
///     match Grid1DUnion::try_new(fallback_a, fallback_b) {
///         Ok(union) => Ok(union),
///         Err(e) => {
///             errors.push(e);
///             Err(errors)
///         }
///     }
/// }
/// ```
///
/// ## Mathematical Properties and Guarantees
///
/// ### Error Correctness
/// ```text
/// ∀ grids A, B: Grid1DUnion::try_new(A, B) fails ⟺
///   domain(A) ≠ domain(B) ∨ unified_coords_invalid
/// ```
/// Errors occur if and only if there's a genuine mathematical incompatibility.
///
/// ### Error Determinism
/// ```text
/// ∀ grids A, B:
///   Grid1DUnion::try_new(A, B) = Grid1DUnion::try_new(A, B) (same error or success)
/// ```
/// Error conditions are deterministic and reproducible.
///
/// ### Error Completeness
/// ```text
/// ∀ error conditions: ∃ variant ∈ ErrorsGrid1DUnion that captures the condition
/// ```
/// All meaningful failure modes are represented by error variants.
///
/// ## See Also
///
/// - [`Grid1DUnion`]: The struct that uses this error type
/// - [`ErrorsGrid1D`]: Related error type for general grid construction
/// - [`Grid1D`]: The fundamental grid type used in unions
/// - [`IntervalPartition`]: The trait providing partition operations
/// - [`HasDomain1D`]: Core trait for domain access and comparison
/// - Error handling patterns in the [module-level documentation](crate)
#[derive(Debug, Error)]
pub enum ErrorsGrid1DUnion<Domain1D: IntervalFinitePositiveLengthTrait> {
    /// Error indicating that the two grids have incompatible domains.
    #[error("The 1D domains of the two grids do not match!")]
    DomainsMismatch {
        /// The domain of the first grid.
        domain_grid_a: Domain1D,
        /// The domain of the second grid.
        domain_grid_b: Domain1D,
        /// Captured backtrace for debugging.
        backtrace: Backtrace,
    },

    /// Error propagated from the underlying [`Grid1D`] construction during union.
    #[error("Error from the Grid1D constructor")]
    FromGrid1DConstructor {
        /// The underlying error from [`Grid1D`] construction.
        #[from]
        source: ErrorsGrid1D<Domain1D>,
    },
}

///  Result of computing the union of two 1D grids with interval mappings.
///
/// This struct contains the refined grid that represents the union of all points
/// from both input grids (defined over the same domain), along with bidirectional mappings that allow tracking
/// how intervals in the refined grid correspond to intervals in the original grids.
///
/// ## Mathematical Definition
///
/// Given two grids `G_A` and `G_B` with point sets `P_A` and `P_B`, the union creates:
/// ```text
/// P_refined = P_A ∪ P_B (sorted and deduplicated)
/// G_refined = Grid(domain, P_refined)
/// ```
///
/// The mapping functions satisfy:
/// ```text
/// map_A: IntervalId(G_refined) → IntervalId(G_A)
/// map_B: IntervalId(G_refined) → IntervalId(G_B)
/// ```
///
/// where `map_A[i]` gives the interval in `G_A` containing the midpoint of refined interval `i`.
///
/// ## Usage Patterns
///
/// ### Basic Access
/// ```rust
/// use grid1d::{*, intervals::*, scalars::*};
/// use sorted_vec::partial::SortedSet;
///
/// let grid_a = Grid1D::<IntervalClosed<f64>>::try_from_sorted(SortedSet::from_unsorted(vec![0.0, 1.0, 2.0])).unwrap();
/// let grid_b = Grid1D::<IntervalClosed<f64>>::try_from_sorted(SortedSet::from_unsorted(vec![0.0, 0.5, 2.0])).unwrap();
///
/// let union = Grid1DUnion::try_new(&grid_a, &grid_b).unwrap();
///
/// // Access the unified grid
/// println!("Unified grid: {:?}", union.unified_grid().coords());
///
/// // Find which original intervals correspond to a refined interval
/// let refined_id = IntervalId::new(1);
/// let original_a_id = union.mapping_to_grid_a()[1];
/// let original_b_id = union.mapping_to_grid_b()[1];
/// println!("Refined interval {} maps to A[{}], B[{}]",
///          refined_id.as_ref(), original_a_id.as_ref(), original_b_id.as_ref());
/// ```
///
/// ### Iteration Patterns
/// ```rust
/// use grid1d::{*, intervals::*, scalars::*};
/// use sorted_vec::partial::SortedSet;
///
/// # let grid_a = Grid1D::<IntervalClosed<f64>>::try_from_sorted(SortedSet::from_unsorted(vec![0.0, 1.0, 2.0])).unwrap();
/// # let grid_b = Grid1D::<IntervalClosed<f64>>::try_from_sorted(SortedSet::from_unsorted(vec![0.0, 0.5, 2.0])).unwrap();
/// # let union = Grid1DUnion::try_new(&grid_a, &grid_b).unwrap();
///
/// // Iterate over all refined intervals with their mappings
/// for (refined_id, a_id, b_id) in union.iter_interval_mappings() {
///     println!("Refined {}: A={}, B={}", refined_id.as_ref(), a_id.as_ref(), b_id.as_ref());
/// }
///
/// // Alternative: using the unified grid directly
/// for (refined_id, _) in union.unified_grid().iter_intervals() {
///     let (a_id, b_id) = union.find_original_intervals(&refined_id);
///     println!("Refined {}: A={}, B={}", refined_id.as_ref(), a_id.as_ref(), b_id.as_ref());
/// }
/// ```
///
/// ### Finite Element Assembly
/// ```rust
/// use grid1d::{*, intervals::*, scalars::*};
/// use sorted_vec::partial::SortedSet;
///
/// # let grid_a = Grid1D::<IntervalClosed<f64>>::try_from_sorted(SortedSet::from_unsorted(vec![0.0, 1.0, 2.0])).unwrap();
/// # let grid_b = Grid1D::<IntervalClosed<f64>>::try_from_sorted(SortedSet::from_unsorted(vec![0.0, 0.5, 2.0])).unwrap();
/// # let union = Grid1DUnion::try_new(&grid_a, &grid_b).unwrap();
/// # struct Element { data: f64 }
/// # let elements_a = vec![Element { data: 1.0 }, Element { data: 2.0 }];
/// # let elements_b = vec![Element { data: 3.0 }, Element { data: 4.0 }];
/// # fn assemble_element_contribution(_: &Element, _: &Element, _: &grid1d::intervals::SubIntervalInPartition<IntervalClosed<f64>>) {}
///
/// // Typical usage in finite element methods
/// for (refined_id, refined_interval) in union.unified_grid().iter_intervals() {
///     let (a_id, b_id) = union.find_original_intervals(&refined_id);
///     let element_a = &elements_a[*a_id.as_ref()];
///     let element_b = &elements_b[*b_id.as_ref()];
///     
///     // Perform element assembly over the refined interval
///     assemble_element_contribution(element_a, element_b, &refined_interval);
/// }
/// ```
///
/// ### Working with Domain Traits
/// ```rust
/// use grid1d::{*, intervals::*, scalars::*};
/// use sorted_vec::partial::SortedSet;
/// use std::ops::Deref;
///
/// # let grid_a = Grid1D::<IntervalClosed<f64>>::try_from_sorted(SortedSet::from_unsorted(vec![0.0, 1.0, 2.0])).unwrap();
/// # let grid_b = Grid1D::<IntervalClosed<f64>>::try_from_sorted(SortedSet::from_unsorted(vec![0.0, 0.5, 2.0])).unwrap();
/// # let union = Grid1DUnion::try_new(&grid_a, &grid_b).unwrap();
///
/// // Grid1DUnion implements HasDomain1D, HasCoords1D, and IntervalPartition
/// assert_eq!(union.domain(), grid_a.domain());
/// assert_eq!(union.domain(), grid_b.domain());
/// assert_eq!(union.coords().deref(), &[0.0, 0.5, 1.0, 2.0]);
/// assert_eq!(union.num_intervals().as_ref(), &3);
///
/// // Can be used with generic functions expecting IntervalPartition
/// fn analyze_partition<P: IntervalPartition>(partition: &P) {
///     println!("Partition has {} intervals", partition.num_intervals().as_ref());
///     for (id, interval) in partition.iter_intervals() {
///         println!("Interval {}: length = {}", id.as_ref(), partition.interval_length(&id));
///     }
/// }
///
/// analyze_partition(&union);
/// ```
///
/// ## Performance Characteristics
///
/// | Operation | Time Complexity | Space Complexity | Notes |
/// |-----------|----------------|------------------|--------|
/// | **Creation** | O(n+m) | O(n+m) | Using merge algorithm |
/// | **Grid access** | O(1) | O(1) | Direct reference |
/// | **Mapping lookup** | O(1) | O(1) | Vector indexing |
/// | **Iteration** | O(k) | O(1) | Where k is result size |
/// | **Point location** | O(log(n+m)) | O(1) | Binary search on unified grid |
///
/// Where `n` and `m` are the sizes of the input grids.
///
/// ## Memory Layout
///
/// - **Unified grid**: Contains merged coordinates and detected structure (uniform/non-uniform)
/// - **Mappings**: Two `Vec<IntervalId>` for O(1) lookup
/// - **Total overhead**: Minimal - only stores the essential mapping information
///
/// ## Integration with other numerical Ecosystem
///
/// `Grid1DUnion` implements the core domain traits:
/// - **[`HasDomain1D`]**: Access to the unified grid's domain
/// - **[`HasCoords1D`]**: Access to the unified grid's coordinates  
/// - **[`IntervalPartition`]**: Full partition interface on the unified grid
///
/// This means `Grid1DUnion` can be used anywhere these traits are expected,
/// providing seamless integration with the broader mathematical ecosystem.
///
/// ## Mathematical Properties
///
/// ### Completeness
/// The unified grid contains all unique points from both input grids:
/// ```text
/// ∀p ∈ P_A ∪ P_B ⟹ p ∈ P_refined
/// ```
///
/// ### Mapping Consistency
/// Each refined interval maps to exactly one interval in each original grid:
/// ```text
/// ∀i ∈ refined_intervals: map_A[i] ∈ [0, |G_A|-1] ∧ map_B[i] ∈ [0, |G_B|-1]
/// ```
///
/// ### Containment Property
/// The midpoint strategy ensures each refined interval is properly contained:
/// ```text
/// ∀i: midpoint(refined_interval[i]) ∈ original_interval[map_A[i]]
/// ```
///
/// ## Error Handling
///
/// [`Grid1DUnion::try_new()`] can fail with:
/// - **[`ErrorsGrid1DUnion::DomainsMismatch`]**: Input grids have different domains
/// - **[`ErrorsGrid1DUnion::FromGrid1DConstructor`]**: Unified grid construction fails
///
/// ### Domain Mismatch
/// ```rust
/// use grid1d::{*, intervals::*, scalars::*};
/// use sorted_vec::partial::SortedSet;
///
/// // Grids on different domains!
/// let grid_a = Grid1D::<IntervalClosed<f64>>::try_from_sorted(SortedSet::from_unsorted(vec![0.0, 1.0])).unwrap();
/// let grid_b = Grid1D::<IntervalClosed<f64>>::try_from_sorted(SortedSet::from_unsorted(vec![0.0, 2.0])).unwrap();
///
/// let err = Grid1DUnion::try_new(&grid_a, &grid_b).unwrap_err();
/// assert!(matches!(err, ErrorsGrid1DUnion::DomainsMismatch { domain_grid_a, domain_grid_b, .. }));
/// ```
///
/// ## See Also
///
/// - [`Grid1D`]: The fundamental 1D grid type
/// - [`IntervalPartition`]: The trait providing partition operations
/// - [`HasDomain1D`], [`HasCoords1D`]: Core domain access traits
#[derive(Debug, Clone, PartialEq, Into, Serialize, Deserialize)]
#[serde(bound(deserialize = "Domain1D: for<'d> serde::Deserialize<'d>, \
                  Domain1D::RealType: for<'d> serde::Deserialize<'d>"))]
pub struct Grid1DUnion<Domain1D: BuildIntervalInPartition> {
    /// The refined grid containing all unique points from both input grids
    unified_grid: Grid1D<Domain1D>,

    /// Mapping from refined grid intervals to the first input grid intervals
    mapping_to_grid_a: Vec<IntervalId>,

    /// Mapping from refined grid intervals to the second input grid intervals
    mapping_to_grid_b: Vec<IntervalId>,
}

impl<Domain1D: BuildIntervalInPartition> Grid1DUnion<Domain1D> {
    /// Computes the union of two 1D grids with interval mapping.
    ///
    /// This function creates a refined grid containing all unique points from both
    /// input grids, then computes bidirectional mappings between interval IDs.
    ///
    /// # Parameters
    ///
    /// - `grid1d_a`: First input grid
    /// - `grid1d_b`: Second input grid (must have same domain as `grid1d_a`)
    ///
    /// # Returns
    ///
    /// A [`Grid1DUnion`] struct containing the unified grid and interval mappings.
    pub fn try_new(
        grid1d_a: &Grid1D<Domain1D>,
        grid1d_b: &Grid1D<Domain1D>,
    ) -> Result<Self, ErrorsGrid1DUnion<Domain1D>> {
        let domain = grid1d_a.domain().clone();

        if &domain != grid1d_b.domain() {
            return Err(ErrorsGrid1DUnion::DomainsMismatch {
                domain_grid_a: domain,
                domain_grid_b: grid1d_b.domain().clone(),
                backtrace: capture_backtrace(),
            });
        }

        // Get sorted slices
        let coords_a = grid1d_a.coords();
        let coords_b = grid1d_b.coords();

        // Merge the two sorted vectors with deduplication
        let merged_coords = coords_a.union(coords_b);
        let unified_grid = Grid1D::try_from_coords(merged_coords)?;

        let compute_interval_map =
            |unified_grid: &Grid1D<Domain1D>, grid1d_coarse: &Grid1D<Domain1D>| {
                let n_intervals = *unified_grid.num_intervals().as_ref();
                let mut map = Vec::with_capacity(n_intervals);

                let one_div_2 = Domain1D::RealType::one_div_2();

                let v = unified_grid.coords();
                for i in 0..n_intervals {
                    // Find the midpoint of the current interval in the unified grid.
                    let midpoint_interval_unified_grid = (v[i].clone() + &v[i + 1]) * &one_div_2;

                    // Find which interval in the coarse grid contains this midpoint.
                    // Using the panicking version is safe here because the midpoint is guaranteed
                    // to be within the coarse grid's domain.
                    let coarse_interval_id =
                        grid1d_coarse.find_interval_id_of_point(&midpoint_interval_unified_grid);
                    map.push(coarse_interval_id);
                }

                map
            };

        let mapping_to_grid_a = compute_interval_map(&unified_grid, grid1d_a);
        let mapping_to_grid_b = compute_interval_map(&unified_grid, grid1d_b);

        Ok(Self {
            unified_grid,
            mapping_to_grid_a,
            mapping_to_grid_b,
        })
    }

    /// Returns a reference to the unified grid.
    ///
    /// The unified grid contains all unique points from both input grids,
    /// creating a refined partition that allows consistent operations
    /// across both original grids.
    ///
    /// # Example
    ///
    /// ```rust
    /// use grid1d::{*, intervals::*, scalars::*};
    /// use sorted_vec::partial::SortedSet;
    /// use std::ops::Deref;
    ///
    /// let grid_a = Grid1D::<IntervalClosed<f64>>::try_from_sorted(SortedSet::from_unsorted(vec![0.0, 1.0, 2.0])).unwrap();
    /// let grid_b = Grid1D::<IntervalClosed<f64>>::try_from_sorted(SortedSet::from_unsorted(vec![0.0, 0.5, 2.0])).unwrap();
    ///
    /// let union = Grid1DUnion::try_new(&grid_a, &grid_b).unwrap();
    /// let refined_coords = union.unified_grid().coords();
    /// assert_eq!(refined_coords.deref(), &[0.0, 0.5, 1.0, 2.0]);
    /// ```
    pub fn unified_grid(&self) -> &Grid1D<Domain1D> {
        &self.unified_grid
    }

    /// Returns a reference to the mapping from refined grid intervals to the first input grid.
    ///
    /// This mapping allows you to determine which interval in the first input grid
    /// contains the midpoint of each refined interval.
    ///
    /// # Example
    ///
    /// ```rust
    /// use grid1d::{*, intervals::*, scalars::*};
    /// use sorted_vec::partial::SortedSet;
    ///
    /// let grid_a = Grid1D::<IntervalClosed<f64>>::try_from_sorted(SortedSet::from_unsorted(vec![0.0, 1.0, 2.0])).unwrap();
    /// let grid_b = Grid1D::<IntervalClosed<f64>>::try_from_sorted(SortedSet::from_unsorted(vec![0.0, 0.5, 2.0])).unwrap();
    ///
    /// let union = Grid1DUnion::try_new(&grid_a, &grid_b).unwrap();
    /// let mapping_a = union.mapping_to_grid_a();
    ///
    /// // Refined interval [0.0, 0.5] maps to grid_a interval [0.0, 1.0]
    /// assert_eq!(mapping_a[0], IntervalId::new(0));
    /// ```
    pub fn mapping_to_grid_a(&self) -> &Vec<IntervalId> {
        &self.mapping_to_grid_a
    }

    /// Returns a reference to the mapping from refined grid intervals to the second input grid.
    ///
    /// This mapping allows you to determine which interval in the second input grid
    /// contains the midpoint of each refined interval.
    ///
    /// # Example
    ///
    /// ```rust
    /// use grid1d::{*, intervals::*, scalars::*};
    /// use sorted_vec::partial::SortedSet;
    ///
    /// let grid_a = Grid1D::<IntervalClosed<f64>>::try_from_sorted(SortedSet::from_unsorted(vec![0.0, 1.0, 2.0])).unwrap();
    /// let grid_b = Grid1D::<IntervalClosed<f64>>::try_from_sorted(SortedSet::from_unsorted(vec![0.0, 0.5, 2.0])).unwrap();
    ///
    /// let union = Grid1DUnion::try_new(&grid_a, &grid_b).unwrap();
    /// let mapping_b = union.mapping_to_grid_b();
    ///
    /// // Refined interval [0.0, 0.5] maps to grid_b interval [0.0, 0.5]
    /// assert_eq!(mapping_b[0], IntervalId::new(0));
    /// ```
    pub fn mapping_to_grid_b(&self) -> &Vec<IntervalId> {
        &self.mapping_to_grid_b
    }

    /// Returns the number of intervals in the unified grid.
    ///
    /// # Example
    ///
    /// ```rust
    /// use grid1d::{*, intervals::*, scalars::*};
    /// use sorted_vec::partial::SortedSet;
    ///
    /// let grid_a = Grid1D::<IntervalClosed<f64>>::try_from_sorted(SortedSet::from_unsorted(vec![0.0, 1.0, 2.0])).unwrap();
    /// let grid_b = Grid1D::<IntervalClosed<f64>>::try_from_sorted(SortedSet::from_unsorted(vec![0.0, 0.5, 2.0])).unwrap();
    ///
    /// let union = Grid1DUnion::try_new(&grid_a, &grid_b).unwrap();
    /// assert_eq!(union.num_refined_intervals().as_ref(), &3);
    /// ```
    pub fn num_refined_intervals(&self) -> NumIntervals {
        self.unified_grid.num_intervals()
    }

    /// Finds the corresponding interval IDs in both original grids for a given refined interval.
    ///
    /// This is a convenience method that combines both mapping lookups.
    ///
    /// # Parameters
    ///
    /// - `refined_id`: The interval ID in the unified grid
    ///
    /// # Returns
    ///
    /// A tuple `(grid_a_id, grid_b_id)` containing the corresponding interval IDs
    /// in both original grids.
    ///
    /// # Panics
    ///
    /// Panics if the `refined_id` is not found in the mappings (which should never
    /// happen if the struct is constructed correctly).
    ///
    /// # Example
    ///
    /// ```rust
    /// use grid1d::{*, intervals::*, scalars::*};
    /// use sorted_vec::partial::SortedSet;
    ///
    /// let grid_a = Grid1D::<IntervalClosed<f64>>::try_from_sorted(SortedSet::from_unsorted(vec![0.0, 1.0, 2.0])).unwrap();
    /// let grid_b = Grid1D::<IntervalClosed<f64>>::try_from_sorted(SortedSet::from_unsorted(vec![0.0, 0.5, 2.0])).unwrap();
    ///
    /// let union = Grid1DUnion::try_new(&grid_a, &grid_b).unwrap();
    /// let (a_id, b_id) = union.find_original_intervals(&IntervalId::new(1));
    ///
    /// println!("Refined interval 1 maps to: grid_a[{}], grid_b[{}]", a_id.as_ref(), b_id.as_ref());
    /// ```
    pub fn find_original_intervals(&self, refined_id: &IntervalId) -> (IntervalId, IntervalId) {
        let refined_id = *refined_id.as_ref();
        let grid_a_id = self.mapping_to_grid_a[refined_id];
        let grid_b_id = self.mapping_to_grid_b[refined_id];
        (grid_a_id, grid_b_id)
    }

    /// Returns an iterator over all refined intervals with their corresponding original interval IDs.
    ///
    /// This provides a convenient way to iterate over all mappings simultaneously.
    ///
    /// # Returns
    ///
    /// An iterator yielding tuples of `(refined_id, grid_a_id, grid_b_id)`.
    ///
    /// # Example
    ///
    /// ```rust
    /// use grid1d::{*, intervals::*, scalars::*};
    /// use sorted_vec::partial::SortedSet;
    ///
    /// let grid_a = Grid1D::<IntervalClosed<f64>>::try_from_sorted(SortedSet::from_unsorted(vec![0.0, 1.0, 2.0])).unwrap();
    /// let grid_b = Grid1D::<IntervalClosed<f64>>::try_from_sorted(SortedSet::from_unsorted(vec![0.0, 0.5, 2.0])).unwrap();
    ///
    /// let union = Grid1DUnion::try_new(&grid_a, &grid_b).unwrap();
    ///
    /// for (refined_id, a_id, b_id) in union.iter_interval_mappings() {
    ///     println!("Refined {}: A={}, B={}", *refined_id.as_ref(), *a_id.as_ref(), *b_id.as_ref());
    /// }
    /// ```
    pub fn iter_interval_mappings(
        &self,
    ) -> impl Iterator<Item = (IntervalId, IntervalId, IntervalId)> + '_ {
        (0..*self.num_refined_intervals().as_ref()).map(move |i| {
            let refined_id = IntervalId::new(i);
            let (a_id, b_id) = self.find_original_intervals(&refined_id);
            (refined_id, a_id, b_id)
        })
    }

    /// Returns an iterator over refined intervals with their physical domains.
    ///
    /// This provides a convenient way to iterate over refined intervals
    /// along with their actual geometric domains.
    ///
    /// # Example
    ///
    /// ```rust
    /// use grid1d::{*, intervals::*, scalars::*};
    /// use sorted_vec::partial::SortedSet;
    ///
    /// let grid_a = Grid1D::<IntervalClosed<f64>>::try_from_sorted(SortedSet::from_unsorted(vec![0.0, 1.0, 2.0])).unwrap();
    /// let grid_b = Grid1D::<IntervalClosed<f64>>::try_from_sorted(SortedSet::from_unsorted(vec![0.0, 0.5, 2.0])).unwrap();
    ///
    /// let union = Grid1DUnion::try_new(&grid_a, &grid_b).unwrap();
    ///
    /// for (refined_id, interval, a_id, b_id) in union.iter_intervals_with_mappings() {
    ///     println!("Refined interval {}: {:?} -> A[{}], B[{}]",
    ///              refined_id.as_ref(), interval, a_id.as_ref(), b_id.as_ref());
    /// }
    /// ```
    pub fn iter_intervals_with_mappings(
        &self,
    ) -> impl Iterator<
        Item = (
            IntervalId,
            SubIntervalInPartition<Domain1D>,
            IntervalId,
            IntervalId,
        ),
    > + '_ {
        self.unified_grid()
            .iter_intervals()
            .map(move |(refined_id, interval)| {
                let (a_id, b_id) = self.find_original_intervals(&refined_id);
                (refined_id, interval, a_id, b_id)
            })
    }

    /// Finds all refined intervals that intersect with the given domain.
    ///
    /// This combines the functionality of `intervals_in_intersection` with
    /// the mapping information, providing a complete view of how the intersection
    /// relates to both original grids.
    ///
    /// # Example
    ///
    /// ```rust
    /// use grid1d::{*, intervals::*, scalars::*};
    /// use sorted_vec::partial::SortedSet;
    ///
    /// let grid_a = Grid1D::<IntervalClosed<f64>>::try_from_sorted(SortedSet::from_unsorted(vec![0.0, 1.0, 2.0])).unwrap();
    /// let grid_b = Grid1D::<IntervalClosed<f64>>::try_from_sorted(SortedSet::from_unsorted(vec![0.0, 0.5, 2.0])).unwrap();
    ///
    /// let union = Grid1DUnion::try_new(&grid_a, &grid_b).unwrap();
    /// let subdomain = IntervalClosed::new(0.3, 1.2);
    ///
    /// let intersections = union.find_intersections_with_mappings(&subdomain);
    /// for (refined_id, intersection, a_id, b_id) in intersections {
    ///     println!("Intersection in refined[{}]: {:?} -> A[{}], B[{}]",
    ///              refined_id.as_ref(), intersection, a_id.as_ref(), b_id.as_ref());
    /// }
    /// ```
    pub fn find_intersections_with_mappings<
        IntervalType: IntervalFinitePositiveLengthTrait<RealType = Domain1D::RealType>,
    >(
        &self,
        domain_in: &IntervalType,
    ) -> Vec<(
        IntervalId,
        IntervalFinitePositiveLength<Domain1D::RealType>,
        IntervalId,
        IntervalId,
    )> {
        self.unified_grid()
            .intervals_in_intersection(domain_in)
            .into_iter()
            .map(|(refined_id, intersection)| {
                let (a_id, b_id) = self.find_original_intervals(&refined_id);
                (refined_id, intersection, a_id, b_id)
            })
            .collect()
    }

    /// Deconstructs the `Grid1DUnion` into its component parts.
    ///
    /// This method consumes the struct and returns the individual components,
    /// which can be useful when you need to work with the parts separately
    /// or integrate with legacy code that expects the tuple format.
    ///
    /// # Returns
    ///
    /// A tuple containing:
    /// 1. The unified grid
    /// 2. Mapping from refined grid to first input grid
    /// 3. Mapping from refined grid to second input grid
    ///
    /// # Example
    ///
    /// ```rust
    /// use grid1d::{*, intervals::*, scalars::*};
    /// use sorted_vec::partial::SortedSet;
    ///
    /// let grid_a = Grid1D::<IntervalClosed<f64>>::try_from_sorted(SortedSet::from_unsorted(vec![0.0, 1.0, 2.0])).unwrap();
    /// let grid_b = Grid1D::<IntervalClosed<f64>>::try_from_sorted(SortedSet::from_unsorted(vec![0.0, 0.5, 2.0])).unwrap();
    ///
    /// let union = Grid1DUnion::try_new(&grid_a, &grid_b).unwrap();
    /// let (unified_grid, map_a, map_b) = union.into_parts();
    ///
    /// // Now you can use the individual components
    /// println!("Unified grid has {} intervals", unified_grid.num_intervals().as_ref());
    /// ```
    pub fn into_parts(self) -> (Grid1D<Domain1D>, Vec<IntervalId>, Vec<IntervalId>) {
        (
            self.unified_grid,
            self.mapping_to_grid_a,
            self.mapping_to_grid_b,
        )
    }
}

impl<Domain1D: BuildIntervalInPartition> HasDomain1D for Grid1DUnion<Domain1D> {
    type Domain1D = Domain1D;

    /// Returns a reference to the domain of the unified grid.
    ///
    /// The domain is shared among both original grids and the unified grid,
    /// as grid union operations require identical domains.
    ///
    /// # Example
    /// ```rust
    /// use grid1d::{*, intervals::*, scalars::*};
    /// use sorted_vec::partial::SortedSet;
    ///
    /// let grid_a = Grid1D::<IntervalClosed<f64>>::try_from_sorted(SortedSet::from_unsorted(vec![0.0, 1.0, 2.0])).unwrap();
    /// let grid_b = Grid1D::<IntervalClosed<f64>>::try_from_sorted(SortedSet::from_unsorted(vec![0.0, 0.5, 2.0])).unwrap();
    ///
    /// let union = Grid1DUnion::try_new(&grid_a, &grid_b).unwrap();
    /// assert_eq!(union.domain(), grid_a.domain());
    /// assert_eq!(union.domain(), grid_b.domain());
    /// ```
    fn domain(&self) -> &Self::Domain1D {
        self.unified_grid.domain()
    }
}

impl<Domain1D: BuildIntervalInPartition> HasCoords1D for Grid1DUnion<Domain1D> {
    type Point1DType = Domain1D::RealType;

    /// Returns a reference to the coordinates of the unified grid.
    ///
    /// These coordinates represent the union of all unique points from both
    /// original grids, sorted in ascending order.
    ///
    /// # Example
    /// ```rust
    /// use grid1d::{*, intervals::*, scalars::*};
    /// use sorted_vec::partial::SortedSet;
    /// use std::ops::Deref;
    ///
    /// let grid_a = Grid1D::<IntervalClosed<f64>>::try_from_sorted(SortedSet::from_unsorted(vec![0.0, 1.0, 2.0])).unwrap();
    /// let grid_b = Grid1D::<IntervalClosed<f64>>::try_from_sorted(SortedSet::from_unsorted(vec![0.0, 0.5, 2.0])).unwrap();
    ///
    /// let union = Grid1DUnion::try_new(&grid_a, &grid_b).unwrap();
    /// assert_eq!(union.coords().deref(), &[0.0, 0.5, 1.0, 2.0]);
    /// ```
    fn coords(&self) -> &Coords1D<Self::Point1DType> {
        self.unified_grid.coords()
    }
}

impl<Domain1D: BuildIntervalInPartition> IntervalPartition for Grid1DUnion<Domain1D> {
    type UniformlyRefinedGrid1DType = Grid1D<Domain1D>;

    fn refine_uniform(
        self,
        num_extra_points_each_interval: &PositiveNumPoints1D,
    ) -> Grid1DUniformRefinement<Self> {
        let unified_grid = self.unified_grid;
        let mapping_to_grid_a = self.mapping_to_grid_a;
        let mapping_to_grid_b = self.mapping_to_grid_b;

        let (
            refined_grid,
            original_unified_grid,
            refined_to_original_interval_mapping,
            original_to_refined_interval_mapping,
        ) = unified_grid
            .refine_uniform(num_extra_points_each_interval)
            .into();

        let original_grid = Grid1DUnion {
            unified_grid: original_unified_grid,
            mapping_to_grid_a,
            mapping_to_grid_b,
        };

        Grid1DUniformRefinement::new(
            refined_grid,
            original_grid,
            refined_to_original_interval_mapping,
            original_to_refined_interval_mapping,
        )
    }

    fn refine(
        self,
        intervals_to_refine: &std::collections::BTreeMap<IntervalId, PositiveNumPoints1D>,
    ) -> Grid1DNonUniformRefinement<Self> {
        let unified_grid = self.unified_grid;
        let mapping_to_grid_a = self.mapping_to_grid_a;
        let mapping_to_grid_b = self.mapping_to_grid_b;

        let (
            refined_grid,
            original_unified_grid,
            refined_to_original_interval_mapping,
            original_to_refined_interval_mapping,
        ) = unified_grid.refine(intervals_to_refine).into();

        let original_grid = Grid1DUnion {
            unified_grid: original_unified_grid,
            mapping_to_grid_a,
            mapping_to_grid_b,
        };

        Grid1DNonUniformRefinement::new(
            refined_grid,
            original_grid,
            refined_to_original_interval_mapping,
            original_to_refined_interval_mapping,
        )
    }
}
//===============================================================================
// TESTS
//===============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        intervals::IntervalClosed,
        scalars::NumIntervals,
        traits::{HasCoords1D, HasDomain1D, IntervalPartition},
    };
    use num_valid::RealNative64StrictFinite;
    use sorted_vec::partial::SortedSet;
    use std::ops::Deref;
    use try_create::TryNew;

    type Real = RealNative64StrictFinite;

    /// Helper function to create a Real value from f64
    fn real(x: f64) -> Real {
        Real::try_new(x).unwrap()
    }

    mod construction {
        use super::*;

        #[test]
        fn union_of_identical_grids() {
            let grid = Grid1D::uniform(
                IntervalClosed::new(real(0.0), real(2.0)),
                NumIntervals::try_new(2).unwrap(),
            );
            let union = Grid1DUnion::try_new(&grid, &grid).unwrap();

            // Union of identical grids should be the same grid
            assert_eq!(union.unified_grid().coords(), grid.coords());
            assert_eq!(union.num_refined_intervals(), grid.num_intervals());

            // All mappings should point to corresponding intervals
            for i in 0..*grid.num_intervals().as_ref() {
                assert_eq!(union.mapping_to_grid_a()[i], IntervalId::new(i));
                assert_eq!(union.mapping_to_grid_b()[i], IntervalId::new(i));
            }
        }

        #[test]
        fn union_with_disjoint_points() {
            let grid_a =
                Grid1D::<IntervalClosed<Real>>::try_from_sorted(SortedSet::from_unsorted(vec![
                    real(0.0),
                    real(2.0),
                    real(4.0),
                ]))
                .unwrap();
            let grid_b =
                Grid1D::<IntervalClosed<Real>>::try_from_sorted(SortedSet::from_unsorted(vec![
                    real(0.0),
                    real(1.0),
                    real(3.0),
                    real(4.0),
                ]))
                .unwrap();

            let union = Grid1DUnion::try_new(&grid_a, &grid_b).unwrap();

            // Unified grid should have all unique points
            assert_eq!(
                union.unified_grid().coords().deref(),
                &[real(0.0), real(1.0), real(2.0), real(3.0), real(4.0)]
            );
            assert_eq!(union.num_refined_intervals().as_ref(), &4);
        }

        #[test]
        fn union_preserves_domain() {
            let domain = IntervalClosed::new(real(-5.0), real(10.0));
            let grid_a = Grid1D::uniform(domain.clone(), NumIntervals::try_new(3).unwrap());
            let grid_b = Grid1D::uniform(domain.clone(), NumIntervals::try_new(5).unwrap());

            let union = Grid1DUnion::try_new(&grid_a, &grid_b).unwrap();

            assert_eq!(union.domain(), &domain);
        }

        #[test]
        fn union_with_uniform_and_non_uniform() {
            let domain = IntervalClosed::new(real(0.0), real(1.0));
            let uniform_grid = Grid1D::uniform(domain, NumIntervals::try_new(4).unwrap());
            let non_uniform_grid =
                Grid1D::<IntervalClosed<Real>>::try_from_sorted(SortedSet::from_unsorted(vec![
                    real(0.0),
                    real(0.1),
                    real(0.9),
                    real(1.0),
                ]))
                .unwrap();

            let union = Grid1DUnion::try_new(&uniform_grid, &non_uniform_grid).unwrap();

            // Should contain points from both grids
            assert!(union.unified_grid().coords().len() >= 5); // At least 5 unique points
        }
    }

    mod domain_mismatch {
        use super::*;

        #[test]
        fn different_upper_bounds() {
            let grid_a = Grid1D::uniform(
                IntervalClosed::new(real(0.0), real(1.0)),
                NumIntervals::try_new(2).unwrap(),
            );
            let grid_b = Grid1D::uniform(
                IntervalClosed::new(real(0.0), real(2.0)),
                NumIntervals::try_new(2).unwrap(),
            );

            let result = Grid1DUnion::try_new(&grid_a, &grid_b);
            assert!(matches!(
                result,
                Err(ErrorsGrid1DUnion::DomainsMismatch { .. })
            ));
        }

        #[test]
        fn different_lower_bounds() {
            let grid_a = Grid1D::uniform(
                IntervalClosed::new(real(-1.0), real(1.0)),
                NumIntervals::try_new(2).unwrap(),
            );
            let grid_b = Grid1D::uniform(
                IntervalClosed::new(real(0.0), real(1.0)),
                NumIntervals::try_new(2).unwrap(),
            );

            let result = Grid1DUnion::try_new(&grid_a, &grid_b);
            assert!(matches!(
                result,
                Err(ErrorsGrid1DUnion::DomainsMismatch { .. })
            ));
        }

        #[test]
        fn completely_different_domains() {
            let grid_a = Grid1D::uniform(
                IntervalClosed::new(real(0.0), real(1.0)),
                NumIntervals::try_new(2).unwrap(),
            );
            let grid_b = Grid1D::uniform(
                IntervalClosed::new(real(10.0), real(20.0)),
                NumIntervals::try_new(2).unwrap(),
            );

            let result = Grid1DUnion::try_new(&grid_a, &grid_b);
            assert!(matches!(
                result,
                Err(ErrorsGrid1DUnion::DomainsMismatch { .. })
            ));
        }
    }

    mod mappings {
        use super::*;

        #[test]
        fn find_original_intervals() {
            let grid_a =
                Grid1D::<IntervalClosed<Real>>::try_from_sorted(SortedSet::from_unsorted(vec![
                    real(0.0),
                    real(1.0),
                    real(2.0),
                ]))
                .unwrap();
            let grid_b =
                Grid1D::<IntervalClosed<Real>>::try_from_sorted(SortedSet::from_unsorted(vec![
                    real(0.0),
                    real(0.5),
                    real(2.0),
                ]))
                .unwrap();

            let union = Grid1DUnion::try_new(&grid_a, &grid_b).unwrap();

            // Verify find_original_intervals matches direct mapping access
            for i in 0..*union.num_refined_intervals().as_ref() {
                let refined_id = IntervalId::new(i);
                let (a_id, b_id) = union.find_original_intervals(&refined_id);
                assert_eq!(a_id, union.mapping_to_grid_a()[i]);
                assert_eq!(b_id, union.mapping_to_grid_b()[i]);
            }
        }

        #[test]
        fn mapping_to_grid_a() {
            let domain = IntervalClosed::new(real(0.0), real(2.0));
            let grid_a = Grid1D::uniform(domain.clone(), NumIntervals::try_new(2).unwrap());
            let grid_b = Grid1D::uniform(domain, NumIntervals::try_new(4).unwrap());

            let union = Grid1DUnion::try_new(&grid_a, &grid_b).unwrap();

            // Grid B has more intervals, so unified grid should match grid_b
            // Grid A intervals: [0, 1], [1, 2]
            // Grid B intervals: [0, 0.5], [0.5, 1], [1, 1.5], [1.5, 2]
            // Mapping to A: [0, 0, 1, 1]
            assert_eq!(union.mapping_to_grid_a()[0], IntervalId::new(0));
            assert_eq!(union.mapping_to_grid_a()[1], IntervalId::new(0));
            assert_eq!(union.mapping_to_grid_a()[2], IntervalId::new(1));
            assert_eq!(union.mapping_to_grid_a()[3], IntervalId::new(1));
        }
    }

    mod iteration {
        use super::*;

        #[test]
        fn iter_interval_mappings() {
            let grid_a =
                Grid1D::<IntervalClosed<Real>>::try_from_sorted(SortedSet::from_unsorted(vec![
                    real(0.0),
                    real(1.0),
                    real(2.0),
                ]))
                .unwrap();
            let grid_b =
                Grid1D::<IntervalClosed<Real>>::try_from_sorted(SortedSet::from_unsorted(vec![
                    real(0.0),
                    real(0.5),
                    real(2.0),
                ]))
                .unwrap();

            let union = Grid1DUnion::try_new(&grid_a, &grid_b).unwrap();

            let mappings: Vec<(IntervalId, IntervalId, IntervalId)> =
                union.iter_interval_mappings().collect();

            assert_eq!(mappings.len(), *union.num_refined_intervals().as_ref());

            // Verify consistency with find_original_intervals
            for (refined_id, expected_a, expected_b) in &mappings {
                let (actual_a, actual_b) = union.find_original_intervals(refined_id);
                assert_eq!(actual_a, *expected_a);
                assert_eq!(actual_b, *expected_b);
            }
        }

        #[test]
        fn iter_intervals_with_mappings() {
            let domain = IntervalClosed::new(real(0.0), real(2.0));
            let grid_a = Grid1D::uniform(domain.clone(), NumIntervals::try_new(2).unwrap());
            let grid_b = Grid1D::uniform(domain, NumIntervals::try_new(4).unwrap());

            let union = Grid1DUnion::try_new(&grid_a, &grid_b).unwrap();

            let interval_mappings: Vec<_> = union.iter_intervals_with_mappings().collect();

            assert_eq!(
                interval_mappings.len(),
                *union.num_refined_intervals().as_ref()
            );

            // Each tuple should have (refined_id, interval, a_id, b_id)
            for (refined_id, _interval, a_id, b_id) in &interval_mappings {
                let (check_a, check_b) = union.find_original_intervals(refined_id);
                assert_eq!(&check_a, a_id);
                assert_eq!(&check_b, b_id);
            }
        }
    }

    mod intersection_queries {
        use super::*;

        #[test]
        fn find_intersections_with_mappings() {
            let domain = IntervalClosed::new(real(0.0), real(3.0));
            let grid_a = Grid1D::uniform(domain, NumIntervals::try_new(3).unwrap());
            let grid_b =
                Grid1D::<IntervalClosed<Real>>::try_from_sorted(SortedSet::from_unsorted(vec![
                    real(0.0),
                    real(0.5),
                    real(1.5),
                    real(3.0),
                ]))
                .unwrap();

            let union = Grid1DUnion::try_new(&grid_a, &grid_b).unwrap();
            let subdomain = IntervalClosed::new(real(0.7), real(2.2));

            let intersections = union.find_intersections_with_mappings(&subdomain);
            assert!(!intersections.is_empty());

            // Verify that each intersection has valid mapping information
            for (refined_id, _intersection, a_id, b_id) in &intersections {
                let (check_a, check_b) = union.find_original_intervals(refined_id);
                assert_eq!(check_a, *a_id);
                assert_eq!(check_b, *b_id);
            }
        }

        #[test]
        fn intersection_subdomain_at_boundary() {
            let domain = IntervalClosed::new(real(0.0), real(2.0));
            let grid_a = Grid1D::uniform(domain.clone(), NumIntervals::try_new(4).unwrap());
            let grid_b = Grid1D::uniform(domain.clone(), NumIntervals::try_new(4).unwrap());

            let union = Grid1DUnion::try_new(&grid_a, &grid_b).unwrap();

            // Query subdomain that starts at a grid point
            let subdomain = IntervalClosed::new(real(0.5), real(1.5));
            let intersections = union.find_intersections_with_mappings(&subdomain);

            // Should find intervals in the range [0.5, 1.5]
            assert!(!intersections.is_empty());
        }
    }

    mod decomposition {
        use super::*;

        #[test]
        fn into_parts() {
            let domain = IntervalClosed::new(real(0.0), real(1.0));
            let grid_a = Grid1D::uniform(domain.clone(), NumIntervals::try_new(2).unwrap());
            let grid_b = Grid1D::uniform(domain, NumIntervals::try_new(4).unwrap());

            let union = Grid1DUnion::try_new(&grid_a, &grid_b).unwrap();
            let num_intervals = *union.num_refined_intervals().as_ref();

            let (unified_grid, map_a, map_b) = union.into_parts();

            assert_eq!(unified_grid.num_intervals().as_ref(), &num_intervals);
            assert_eq!(map_a.len(), num_intervals);
            assert_eq!(map_b.len(), num_intervals);
        }

        #[test]
        fn decomposition_preserves_data() {
            let domain = IntervalClosed::new(real(0.0), real(2.0));
            let grid_a = Grid1D::uniform(domain.clone(), NumIntervals::try_new(2).unwrap());
            let grid_b = Grid1D::uniform(domain, NumIntervals::try_new(4).unwrap());

            let union = Grid1DUnion::try_new(&grid_a, &grid_b).unwrap();

            // Store expected values before decomposition
            let expected_coords = union.unified_grid().coords().deref().to_vec();
            let expected_map_a: Vec<_> = union.mapping_to_grid_a().to_vec();
            let expected_map_b: Vec<_> = union.mapping_to_grid_b().to_vec();

            let (unified_grid, map_a, map_b) = union.into_parts();

            assert_eq!(unified_grid.coords().deref(), &expected_coords);
            assert_eq!(map_a, expected_map_a);
            assert_eq!(map_b, expected_map_b);
        }
    }

    mod refinement_of_union {
        use super::*;
        use std::collections::BTreeMap;

        #[test]
        fn uniform_refinement_of_union() {
            let domain = IntervalClosed::new(real(0.0), real(2.0));
            let grid_a = Grid1D::uniform(domain.clone(), NumIntervals::try_new(2).unwrap());
            let grid_b = Grid1D::uniform(domain, NumIntervals::try_new(4).unwrap());

            let union = Grid1DUnion::try_new(&grid_a, &grid_b).unwrap();
            let original_intervals = *union.num_refined_intervals().as_ref();

            // Uniform refinement doubles the number of intervals
            let uniform_refinement =
                union.refine_uniform(&PositiveNumPoints1D::try_new(1).unwrap());

            assert_eq!(
                uniform_refinement.refined_grid().num_intervals().as_ref(),
                &(original_intervals * 2)
            );
        }

        #[test]
        fn selective_refinement_of_union() {
            let domain = IntervalClosed::new(real(0.0), real(2.0));
            let grid_a = Grid1D::uniform(domain.clone(), NumIntervals::try_new(2).unwrap());
            let grid_b = Grid1D::uniform(domain, NumIntervals::try_new(4).unwrap());

            let union = Grid1DUnion::try_new(&grid_a, &grid_b).unwrap();

            let refinement_plan =
                BTreeMap::from([(IntervalId::new(1), PositiveNumPoints1D::try_new(1).unwrap())]);
            let selective_refinement = union.refine(&refinement_plan);

            // Original 4 intervals, refine interval 1 into 2 -> 5 intervals
            assert_eq!(
                selective_refinement.refined_grid().num_intervals().as_ref(),
                &5
            );
        }

        #[test]
        fn chained_union_refinements() {
            let domain = IntervalClosed::new(real(0.0), real(2.0));
            let grid_a = Grid1D::uniform(domain.clone(), NumIntervals::try_new(2).unwrap());
            let grid_b = Grid1D::uniform(domain, NumIntervals::try_new(4).unwrap());

            let union = Grid1DUnion::try_new(&grid_a, &grid_b).unwrap();

            // First uniform refinement
            let first_refinement = union.refine_uniform(&PositiveNumPoints1D::try_new(1).unwrap());
            assert_eq!(first_refinement.refined_grid().num_intervals().as_ref(), &8);

            // Second selective refinement on the result
            let refinement_plan =
                BTreeMap::from([(IntervalId::new(1), PositiveNumPoints1D::try_new(1).unwrap())]);
            let second_refinement = first_refinement
                .into_original_grid()
                .refine(&refinement_plan);
            assert_eq!(
                second_refinement.refined_grid().num_intervals().as_ref(),
                &5
            );
        }
    }

    mod trait_implementations {
        use super::*;

        #[test]
        fn has_domain_1d() {
            let domain = IntervalClosed::new(real(0.0), real(2.0));
            let grid_a = Grid1D::uniform(domain.clone(), NumIntervals::try_new(2).unwrap());
            let grid_b = Grid1D::uniform(domain.clone(), NumIntervals::try_new(4).unwrap());

            let union = Grid1DUnion::try_new(&grid_a, &grid_b).unwrap();

            assert_eq!(union.domain(), &domain);
        }

        #[test]
        fn has_coords_1d() {
            let grid_a =
                Grid1D::<IntervalClosed<Real>>::try_from_sorted(SortedSet::from_unsorted(vec![
                    real(0.0),
                    real(1.0),
                    real(2.0),
                ]))
                .unwrap();
            let grid_b =
                Grid1D::<IntervalClosed<Real>>::try_from_sorted(SortedSet::from_unsorted(vec![
                    real(0.0),
                    real(0.5),
                    real(2.0),
                ]))
                .unwrap();

            let union = Grid1DUnion::try_new(&grid_a, &grid_b).unwrap();

            // Coords should be the union of all unique points
            assert_eq!(
                union.coords().deref(),
                &[real(0.0), real(0.5), real(1.0), real(2.0)]
            );
        }

        #[test]
        fn interval_partition_num_intervals() {
            let domain = IntervalClosed::new(real(0.0), real(2.0));
            let grid_a = Grid1D::uniform(domain.clone(), NumIntervals::try_new(2).unwrap());
            let grid_b = Grid1D::uniform(domain, NumIntervals::try_new(4).unwrap());

            let union = Grid1DUnion::try_new(&grid_a, &grid_b).unwrap();

            // The unified grid should have the resolution of the finer grid
            assert_eq!(union.num_intervals().as_ref(), &4);
        }
    }

    mod edge_cases {
        use super::*;

        #[test]
        fn union_with_single_interval_grids() {
            let domain = IntervalClosed::new(real(0.0), real(1.0));
            let grid_a = Grid1D::uniform(domain.clone(), NumIntervals::try_new(1).unwrap());
            let grid_b = Grid1D::uniform(domain, NumIntervals::try_new(1).unwrap());

            let union = Grid1DUnion::try_new(&grid_a, &grid_b).unwrap();

            assert_eq!(union.num_refined_intervals().as_ref(), &1);
        }

        #[test]
        fn union_with_very_different_resolutions() {
            let domain = IntervalClosed::new(real(0.0), real(1.0));
            let coarse_grid = Grid1D::uniform(domain.clone(), NumIntervals::try_new(2).unwrap());
            let fine_grid = Grid1D::uniform(domain, NumIntervals::try_new(100).unwrap());

            let union = Grid1DUnion::try_new(&coarse_grid, &fine_grid).unwrap();

            // Should have at least as many intervals as the fine grid
            assert!(*union.num_refined_intervals().as_ref() >= 100);
        }

        #[test]
        fn union_of_non_uniform_grids() {
            let grid_a =
                Grid1D::<IntervalClosed<Real>>::try_from_sorted(SortedSet::from_unsorted(vec![
                    real(0.0),
                    real(0.1),
                    real(0.2),
                    real(1.0),
                ]))
                .unwrap();
            let grid_b =
                Grid1D::<IntervalClosed<Real>>::try_from_sorted(SortedSet::from_unsorted(vec![
                    real(0.0),
                    real(0.8),
                    real(0.9),
                    real(1.0),
                ]))
                .unwrap();

            let union = Grid1DUnion::try_new(&grid_a, &grid_b).unwrap();

            // Unified grid should have all unique points from both grids
            let expected_points = vec![
                real(0.0),
                real(0.1),
                real(0.2),
                real(0.8),
                real(0.9),
                real(1.0),
            ];
            assert_eq!(union.unified_grid().coords().deref(), &expected_points);
        }

        #[test]
        fn union_with_overlapping_points() {
            // Both grids share some points
            let grid_a =
                Grid1D::<IntervalClosed<Real>>::try_from_sorted(SortedSet::from_unsorted(vec![
                    real(0.0),
                    real(0.5),
                    real(1.0),
                    real(2.0),
                ]))
                .unwrap();
            let grid_b =
                Grid1D::<IntervalClosed<Real>>::try_from_sorted(SortedSet::from_unsorted(vec![
                    real(0.0),
                    real(1.0),
                    real(1.5),
                    real(2.0),
                ]))
                .unwrap();

            let union = Grid1DUnion::try_new(&grid_a, &grid_b).unwrap();

            // Unified should have unique points: 0, 0.5, 1, 1.5, 2
            assert_eq!(
                union.unified_grid().coords().deref(),
                &[real(0.0), real(0.5), real(1.0), real(1.5), real(2.0)]
            );
            assert_eq!(union.num_refined_intervals().as_ref(), &4);
        }
    }
}