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
// Copyright 2026 COOLJAPAN OU (Team KitaSan)
// SPDX-License-Identifier: Apache-2.0
//! FEM-LBM coupling module using the Immersed Boundary Method (IBM).
//!
//! Provides fluid-structure interaction (FSI) between a Finite Element Method
//! (FEM) structural solver and a Lattice Boltzmann Method (LBM) fluid solver.
//!
//! The coupling uses a smoothed delta function (4-point Roma kernel) to spread
//! forces from Lagrangian (FEM) points to the Eulerian (LBM) grid and to
//! interpolate velocities from the grid to the Lagrangian points.
//!
//! # References
//!
//! - Roma, Peskin, Berger (1999), "An adaptive version of the immersed boundary method"
//! - Peskin (2002), "The immersed boundary method", Acta Numerica
use std::collections::HashMap;
/// Configuration parameters for FEM-LBM coupling.
#[derive(Debug, Clone)]
pub struct FemLbmCouplingConfig {
/// Relaxation factor for under-relaxation of force/velocity transfer (0, 1].
/// A value of 1.0 means no relaxation; smaller values improve stability.
pub relaxation_factor: f64,
/// Interpolation order for the IBM delta function kernel.
/// Currently only order 4 (Roma kernel) is supported.
pub interpolation_order: usize,
/// Penalty stiffness for enforcing the no-slip condition at the interface.
/// Larger values enforce the constraint more rigidly but may reduce stability.
pub penalty_stiffness: f64,
/// Number of sub-iterations for the coupling exchange per time step.
pub sub_iterations: usize,
/// Fluid dynamic viscosity (used in viscous stress computation).
pub fluid_viscosity: f64,
}
impl Default for FemLbmCouplingConfig {
fn default() -> Self {
Self {
relaxation_factor: 1.0,
interpolation_order: 4,
penalty_stiffness: 1.0e3,
sub_iterations: 1,
fluid_viscosity: 1.0e-3,
}
}
}
/// A single immersed boundary point at the FEM-LBM interface.
#[derive(Debug, Clone)]
pub struct ImmersedBoundaryPoint {
/// Position of the IB point in physical space.
pub position: [f64; 3],
/// Velocity of the IB point (from FEM structural solver).
pub velocity: [f64; 3],
/// Force accumulated on this IB point (from LBM fluid solver).
pub force: [f64; 3],
/// Reference (initial) position for tracking deformation.
pub reference_position: [f64; 3],
/// Area weight associated with this IB point for force spreading.
pub area_weight: f64,
}
impl ImmersedBoundaryPoint {
/// Create a new immersed boundary point at the given position.
pub fn new(position: [f64; 3]) -> Self {
Self {
position,
velocity: [0.0; 3],
force: [0.0; 3],
reference_position: position,
area_weight: 1.0,
}
}
/// Create a new immersed boundary point with a specified area weight.
pub fn with_area_weight(position: [f64; 3], area_weight: f64) -> Self {
Self {
position,
velocity: [0.0; 3],
force: [0.0; 3],
reference_position: position,
area_weight,
}
}
}
/// Main FEM-LBM coupling state using the Immersed Boundary Method.
///
/// Manages the bidirectional data transfer between a FEM structural domain
/// and an LBM fluid domain across a fluid-structure interface.
#[derive(Debug, Clone)]
pub struct FemLbmCoupling {
/// Configuration parameters.
pub config: FemLbmCouplingConfig,
/// FEM node positions (all nodes, not just interface).
pub fem_nodes: Vec<[f64; 3]>,
/// LBM grid cell spacing.
pub lbm_grid_spacing: f64,
/// Indices of FEM nodes that lie on the FSI interface.
pub interface_nodes: Vec<usize>,
/// Boundary velocities to impose on LBM cells near the interface.
/// One velocity per interface node.
pub boundary_velocities: Vec<[f64; 3]>,
/// Fluid forces to apply to FEM nodes at the interface.
/// One force per interface node.
pub fluid_forces: Vec<[f64; 3]>,
/// Immersed boundary points at the interface.
pub ib_points: Vec<ImmersedBoundaryPoint>,
/// Previous iteration forces for relaxation.
prev_forces: Vec<[f64; 3]>,
/// Whether the coupling has been initialized with interface nodes.
initialized: bool,
}
impl FemLbmCoupling {
/// Create a new FEM-LBM coupling with the given configuration.
pub fn new(config: FemLbmCouplingConfig) -> Self {
Self {
config,
fem_nodes: Vec::new(),
lbm_grid_spacing: 1.0,
interface_nodes: Vec::new(),
boundary_velocities: Vec::new(),
fluid_forces: Vec::new(),
ib_points: Vec::new(),
prev_forces: Vec::new(),
initialized: false,
}
}
/// Define the fluid-structure interface by specifying which FEM nodes
/// participate in coupling and their current positions.
///
/// # Arguments
/// * `node_indices` - Indices into the FEM mesh identifying interface nodes.
/// * `positions` - Current 3D positions of the interface nodes.
/// * `grid_spacing` - LBM grid cell spacing.
///
/// # Errors
/// Returns an error if `node_indices` and `positions` have different lengths.
pub fn set_interface_nodes(
&mut self,
node_indices: Vec<usize>,
positions: Vec<[f64; 3]>,
grid_spacing: f64,
) -> Result<(), CouplingError> {
if node_indices.len() != positions.len() {
return Err(CouplingError::DimensionMismatch {
expected: node_indices.len(),
got: positions.len(),
});
}
if grid_spacing <= 0.0 {
return Err(CouplingError::InvalidParameter(
"grid_spacing must be positive".to_string(),
));
}
let n = node_indices.len();
self.interface_nodes = node_indices;
self.fem_nodes = positions.clone();
self.lbm_grid_spacing = grid_spacing;
self.boundary_velocities = vec![[0.0; 3]; n];
self.fluid_forces = vec![[0.0; 3]; n];
self.prev_forces = vec![[0.0; 3]; n];
// Create immersed boundary points from interface nodes
self.ib_points = positions
.into_iter()
.map(ImmersedBoundaryPoint::new)
.collect();
self.initialized = true;
Ok(())
}
/// Update the positions and velocities of the interface nodes from the FEM solver.
///
/// # Errors
/// Returns an error if the coupling is not initialized or dimensions mismatch.
pub fn update_fem_state(
&mut self,
positions: &[[f64; 3]],
velocities: &[[f64; 3]],
) -> Result<(), CouplingError> {
if !self.initialized {
return Err(CouplingError::NotInitialized);
}
let n = self.interface_nodes.len();
if positions.len() != n || velocities.len() != n {
return Err(CouplingError::DimensionMismatch {
expected: n,
got: positions.len().min(velocities.len()),
});
}
for (ibp, (&pos, &vel)) in self
.ib_points
.iter_mut()
.zip(positions.iter().zip(velocities.iter()))
{
ibp.position = pos;
ibp.velocity = vel;
}
for (bv, &vel) in self.boundary_velocities.iter_mut().zip(velocities.iter()) {
*bv = vel;
}
Ok(())
}
/// Transfer velocity boundary conditions from FEM interface to LBM grid.
///
/// Returns a list of `(grid_index, velocity)` pairs that should be imposed
/// as velocity boundary conditions on the LBM grid. Forces are spread using
/// the IBM delta function kernel.
///
/// # Arguments
/// * `grid_dims` - Dimensions of the LBM grid `[nx, ny, nz]`.
///
/// # Errors
/// Returns an error if the coupling is not initialized.
pub fn transfer_velocity_to_lbm(&self, grid_dims: [usize; 3]) -> LbmCouplingResult {
if !self.initialized {
return Err(CouplingError::NotInitialized);
}
let mut velocity_bcs: HashMap<[usize; 3], [f64; 3]> = HashMap::new();
let mut weight_map: HashMap<[usize; 3], f64> = HashMap::new();
let h = self.lbm_grid_spacing;
for ib_point in &self.ib_points {
let contributions = spread_to_grid(ib_point.position, ib_point.velocity, h, grid_dims);
for (idx, vel) in contributions {
let entry = velocity_bcs.entry(idx).or_insert([0.0; 3]);
let w_entry = weight_map.entry(idx).or_insert(0.0);
// The spread value already includes the delta weight; we accumulate
// and track total weight for later normalisation.
let w = roma_delta_1d(ib_point.position[0] / h - idx[0] as f64, 1.0)
* roma_delta_1d(ib_point.position[1] / h - idx[1] as f64, 1.0)
* roma_delta_1d(ib_point.position[2] / h - idx[2] as f64, 1.0);
entry[0] += vel[0] * w;
entry[1] += vel[1] * w;
entry[2] += vel[2] * w;
*w_entry += w;
}
}
// Normalise so each grid cell receives a properly weighted velocity
let result: Vec<([usize; 3], [f64; 3])> = velocity_bcs
.into_iter()
.filter_map(|(idx, vel)| {
let w = weight_map.get(&idx).copied().unwrap_or(0.0);
if w > 1e-30 {
Some((idx, [vel[0] / w, vel[1] / w, vel[2] / w]))
} else {
None
}
})
.collect();
Ok(result)
}
/// Compute fluid forces on FEM interface nodes from LBM pressure and velocity fields.
///
/// Uses the momentum exchange method: forces arise from pressure (normal stress)
/// and viscous stress contributions interpolated from the LBM grid.
///
/// # Arguments
/// * `lbm_density` - Density field on the LBM grid (flattened, row-major).
/// * `lbm_velocity` - Velocity field on the LBM grid (flattened, row-major).
/// * `grid_spacing` - LBM grid cell spacing.
/// * `grid_dims` - Dimensions of the LBM grid `[nx, ny, nz]`.
///
/// # Errors
/// Returns an error if the coupling is not initialized or field sizes mismatch.
pub fn compute_fluid_forces(
&mut self,
lbm_density: &[f64],
lbm_velocity: &[[f64; 3]],
grid_spacing: f64,
grid_dims: [usize; 3],
) -> Result<(), CouplingError> {
if !self.initialized {
return Err(CouplingError::NotInitialized);
}
let total_cells = grid_dims[0] * grid_dims[1] * grid_dims[2];
if lbm_density.len() != total_cells || lbm_velocity.len() != total_cells {
return Err(CouplingError::FieldSizeMismatch {
expected: total_cells,
density_len: lbm_density.len(),
velocity_len: lbm_velocity.len(),
});
}
let h = grid_spacing;
let cs2 = 1.0 / 3.0; // LBM speed of sound squared (lattice units)
let nu = self.config.fluid_viscosity;
let n = self.interface_nodes.len();
// Save previous forces for relaxation
for (prev, &curr) in self.prev_forces.iter_mut().zip(self.fluid_forces.iter()) {
*prev = curr;
}
for i in 0..n {
let pos = self.ib_points[i].position;
// Interpolate density at IB point
let rho = interpolate_scalar(pos, lbm_density, h, grid_dims);
// Interpolate velocity at IB point
let vel_fluid = interpolate_velocity(pos, lbm_velocity, h, grid_dims);
// Compute velocity gradient tensor at IB point using finite differences
let grad_u = compute_velocity_gradient(pos, lbm_velocity, h, grid_dims);
// Pressure force (isotropic): F_p = -p * n * dA
// For IBM, the pressure is p = rho * cs^2
let pressure = rho * cs2;
// Estimate local surface normal from velocity difference
let dv = [
vel_fluid[0] - self.ib_points[i].velocity[0],
vel_fluid[1] - self.ib_points[i].velocity[1],
vel_fluid[2] - self.ib_points[i].velocity[2],
];
// Penalty force to enforce no-slip: F_penalty = -k * (u_fluid - u_solid)
let penalty_k = self.config.penalty_stiffness;
// Viscous stress contribution: tau_ij = nu * (du_i/dx_j + du_j/dx_i)
// Force from viscous stress (simplified: trace-free symmetric part)
let mut viscous_force = [0.0; 3];
for (d, vf_d) in viscous_force.iter_mut().enumerate() {
let mut tau_sum = 0.0;
for (e, &grad_u_de) in grad_u[d].iter().enumerate() {
// Symmetric rate-of-strain tensor
tau_sum += nu * (grad_u_de + grad_u[e][d]);
}
*vf_d = tau_sum * h * h; // scale by area element
}
// Total force: pressure + viscous + penalty
let area = self.ib_points[i].area_weight * h * h;
let force = [
(-pressure * dv[0] / (dv[0].abs() + 1e-30) * area + viscous_force[0]
- penalty_k * dv[0] * h.powi(3)),
(-pressure * dv[1] / (dv[1].abs() + 1e-30) * area + viscous_force[1]
- penalty_k * dv[1] * h.powi(3)),
(-pressure * dv[2] / (dv[2].abs() + 1e-30) * area + viscous_force[2]
- penalty_k * dv[2] * h.powi(3)),
];
// Apply under-relaxation
let alpha = self.config.relaxation_factor;
self.fluid_forces[i] = [
alpha * force[0] + (1.0 - alpha) * self.prev_forces[i][0],
alpha * force[1] + (1.0 - alpha) * self.prev_forces[i][1],
alpha * force[2] + (1.0 - alpha) * self.prev_forces[i][2],
];
// Store force on IB point as well
self.ib_points[i].force = self.fluid_forces[i];
}
Ok(())
}
/// Retrieve the accumulated fluid forces on FEM interface nodes.
pub fn get_fem_forces(&self) -> &[[f64; 3]] {
&self.fluid_forces
}
/// Perform a full coupling exchange step.
///
/// Runs the configured number of sub-iterations of:
/// 1. Transfer FEM velocities to LBM grid
/// 2. (Caller advances LBM)
/// 3. Compute fluid forces on FEM nodes
///
/// Returns the velocity BCs for the LBM grid after the final sub-iteration.
///
/// # Errors
/// Returns an error if the coupling is not initialized or field sizes mismatch.
pub fn coupling_step(
&mut self,
lbm_density: &[f64],
lbm_velocity: &[[f64; 3]],
grid_spacing: f64,
grid_dims: [usize; 3],
) -> LbmCouplingResult {
if !self.initialized {
return Err(CouplingError::NotInitialized);
}
// Compute forces from the current LBM state
self.compute_fluid_forces(lbm_density, lbm_velocity, grid_spacing, grid_dims)?;
// Transfer updated velocities to LBM
self.transfer_velocity_to_lbm(grid_dims)
}
/// Reset accumulated forces to zero.
pub fn reset_forces(&mut self) {
for f in &mut self.fluid_forces {
*f = [0.0; 3];
}
for ib in &mut self.ib_points {
ib.force = [0.0; 3];
}
}
}
/// Error type for FEM-LBM coupling operations.
#[derive(Debug, Clone)]
pub enum CouplingError {
/// Dimension mismatch between inputs.
DimensionMismatch {
/// Expected size.
expected: usize,
/// Actual size received.
got: usize,
},
/// Invalid parameter value.
InvalidParameter(String),
/// Coupling has not been initialized (no interface nodes set).
NotInitialized,
/// LBM field size does not match expected grid dimensions.
FieldSizeMismatch {
/// Expected number of cells.
expected: usize,
/// Density field length.
density_len: usize,
/// Velocity field length.
velocity_len: usize,
},
}
impl std::fmt::Display for CouplingError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::DimensionMismatch { expected, got } => {
write!(f, "dimension mismatch: expected {expected}, got {got}")
}
Self::InvalidParameter(msg) => write!(f, "invalid parameter: {msg}"),
Self::NotInitialized => write!(
f,
"coupling not initialized: call set_interface_nodes first"
),
Self::FieldSizeMismatch {
expected,
density_len,
velocity_len,
} => write!(
f,
"field size mismatch: expected {expected} cells, got density={density_len}, velocity={velocity_len}"
),
}
}
}
impl std::error::Error for CouplingError {}
/// A list of `(grid_index, velocity)` pairs for LBM velocity boundary conditions.
pub type LbmVelocityBcList = Vec<([usize; 3], [f64; 3])>;
/// Result type for LBM velocity transfer and coupling step operations.
pub type LbmCouplingResult = Result<LbmVelocityBcList, CouplingError>;
// ---------------------------------------------------------------------------
// Free functions for IBM delta function and interpolation
// ---------------------------------------------------------------------------
/// Convert a physical position to the nearest LBM grid index.
///
/// The grid is assumed to start at the origin with uniform spacing `grid_spacing`.
/// Coordinates are clamped so the returned index is always valid for a grid of
/// the given dimensions.
pub fn interpolate_to_grid(pos: [f64; 3], grid_spacing: f64) -> [usize; 3] {
let safe_h = if grid_spacing > 0.0 {
grid_spacing
} else {
1.0
};
[
(pos[0] / safe_h).round().max(0.0) as usize,
(pos[1] / safe_h).round().max(0.0) as usize,
(pos[2] / safe_h).round().max(0.0) as usize,
]
}
/// 1D Roma (4-point) smoothed delta function.
///
/// The Roma kernel is defined piecewise on `|r| / h`:
///
/// ```text
/// phi(r) = (1/6h) * (5 - 3|r|/h - sqrt(-3(1 - |r|/h)^2 + 1)) for 0.5 <= |r|/h <= 1.5
/// phi(r) = (1/3h) * (1 + sqrt(-3(r/h)^2 + 1)) for |r|/h <= 0.5
/// phi(r) = 0 otherwise
/// ```
///
/// # Arguments
/// * `r` - Distance (may be negative; absolute value is used).
/// * `h` - Grid spacing (support width parameter).
///
/// # Returns
/// The value of the smoothed delta function.
pub fn delta_function(r: f64, h: f64) -> f64 {
if h <= 0.0 {
return 0.0;
}
roma_delta_1d(r, h)
}
/// Internal 1D Roma delta kernel evaluation.
///
/// Takes the normalised distance `r` and spacing `h`. The result is scaled
/// by `1/h` so that the 3D product `delta_3d = phi(x)*phi(y)*phi(z)` integrates
/// to 1 over all space when each factor uses spacing `h`.
fn roma_delta_1d(r: f64, h: f64) -> f64 {
let s = (r / h).abs();
if s >= 1.5 {
0.0
} else if s >= 0.5 {
let inner = -3.0 * (1.0 - s) * (1.0 - s) + 1.0;
if inner < 0.0 {
// Numerically should not happen for s in [0.5, 1.5], but guard anyway
0.0
} else {
(1.0 / (6.0 * h)) * (5.0 - 3.0 * s - inner.sqrt())
}
} else {
let inner = -3.0 * s * s + 1.0;
if inner < 0.0 {
0.0
} else {
(1.0 / (3.0 * h)) * (1.0 + inner.sqrt())
}
}
}
/// 3D IBM smoothed delta function as a product of 1D Roma kernels.
///
/// ```text
/// delta_3d(r, h) = phi(rx, h) * phi(ry, h) * phi(rz, h)
/// ```
#[cfg(test)]
fn delta_3d(r: [f64; 3], h: f64) -> f64 {
roma_delta_1d(r[0], h) * roma_delta_1d(r[1], h) * roma_delta_1d(r[2], h)
}
/// Spread a force from a Lagrangian point to nearby Eulerian grid cells.
///
/// Uses the 3D Roma delta function to distribute the force. Returns a list of
/// `(grid_index, force_contribution)` pairs.
///
/// # Arguments
/// * `pos` - Position of the Lagrangian point.
/// * `force` - Force vector to spread.
/// * `grid_spacing` - LBM grid cell spacing.
/// * `grid_dims` - LBM grid dimensions `[nx, ny, nz]`.
pub fn spread_force(
pos: [f64; 3],
force: [f64; 3],
grid_spacing: f64,
grid_dims: [usize; 3],
) -> Vec<([usize; 3], [f64; 3])> {
spread_to_grid(pos, force, grid_spacing, grid_dims)
}
/// Internal spreading: distributes a vector quantity from a Lagrangian point
/// to nearby grid cells using the IBM delta function.
fn spread_to_grid(
pos: [f64; 3],
quantity: [f64; 3],
h: f64,
grid_dims: [usize; 3],
) -> Vec<([usize; 3], [f64; 3])> {
if h <= 0.0 {
return Vec::new();
}
let mut result = Vec::new();
// The Roma kernel has support of width 3h centered on the point.
// We need to iterate over grid cells within +/- 1.5 * h of the point.
let base = [pos[0] / h, pos[1] / h, pos[2] / h];
// Integer range for each dimension
let i_min = ((base[0] - 1.5).floor() as i64).max(0) as usize;
let i_max = ((base[0] + 1.5).ceil() as i64).max(0) as usize;
let j_min = ((base[1] - 1.5).floor() as i64).max(0) as usize;
let j_max = ((base[1] + 1.5).ceil() as i64).max(0) as usize;
let k_min = ((base[2] - 1.5).floor() as i64).max(0) as usize;
let k_max = ((base[2] + 1.5).ceil() as i64).max(0) as usize;
for i in i_min..=i_max.min(grid_dims[0].saturating_sub(1)) {
let dx = pos[0] - (i as f64) * h;
let phi_x = roma_delta_1d(dx, h);
if phi_x.abs() < 1e-30 {
continue;
}
for j in j_min..=j_max.min(grid_dims[1].saturating_sub(1)) {
let dy = pos[1] - (j as f64) * h;
let phi_y = roma_delta_1d(dy, h);
if phi_y.abs() < 1e-30 {
continue;
}
for k in k_min..=k_max.min(grid_dims[2].saturating_sub(1)) {
let dz = pos[2] - (k as f64) * h;
let phi_z = roma_delta_1d(dz, h);
let w = phi_x * phi_y * phi_z;
if w.abs() < 1e-30 {
continue;
}
// In IBM, spreading multiplies by h^3 (volume element)
let h3 = h * h * h;
result.push((
[i, j, k],
[
quantity[0] * w * h3,
quantity[1] * w * h3,
quantity[2] * w * h3,
],
));
}
}
}
result
}
/// Interpolate a velocity field from the Eulerian grid to a Lagrangian point.
///
/// Uses the 3D Roma delta function for interpolation.
///
/// # Arguments
/// * `pos` - Position of the Lagrangian point.
/// * `velocities` - Velocity field on the grid (flattened, row-major `[nx][ny][nz]`).
/// * `grid_spacing` - LBM grid cell spacing.
/// * `grid_dims` - LBM grid dimensions `[nx, ny, nz]`.
pub fn interpolate_velocity(
pos: [f64; 3],
velocities: &[[f64; 3]],
grid_spacing: f64,
grid_dims: [usize; 3],
) -> [f64; 3] {
if grid_spacing <= 0.0 {
return [0.0; 3];
}
let h = grid_spacing;
let base = [pos[0] / h, pos[1] / h, pos[2] / h];
let mut result = [0.0; 3];
let i_min = ((base[0] - 1.5).floor() as i64).max(0) as usize;
let i_max = ((base[0] + 1.5).ceil() as i64).max(0) as usize;
let j_min = ((base[1] - 1.5).floor() as i64).max(0) as usize;
let j_max = ((base[1] + 1.5).ceil() as i64).max(0) as usize;
let k_min = ((base[2] - 1.5).floor() as i64).max(0) as usize;
let k_max = ((base[2] + 1.5).ceil() as i64).max(0) as usize;
for i in i_min..=i_max.min(grid_dims[0].saturating_sub(1)) {
let dx = pos[0] - (i as f64) * h;
let phi_x = roma_delta_1d(dx, h);
if phi_x.abs() < 1e-30 {
continue;
}
for j in j_min..=j_max.min(grid_dims[1].saturating_sub(1)) {
let dy = pos[1] - (j as f64) * h;
let phi_y = roma_delta_1d(dy, h);
if phi_y.abs() < 1e-30 {
continue;
}
for k in k_min..=k_max.min(grid_dims[2].saturating_sub(1)) {
let dz = pos[2] - (k as f64) * h;
let phi_z = roma_delta_1d(dz, h);
let w = phi_x * phi_y * phi_z;
if w.abs() < 1e-30 {
continue;
}
let flat_idx = i * grid_dims[1] * grid_dims[2] + j * grid_dims[2] + k;
if flat_idx < velocities.len() {
// Interpolation: u(X) = sum_x u(x) * delta(x - X) * h^3
let h3 = h * h * h;
result[0] += velocities[flat_idx][0] * w * h3;
result[1] += velocities[flat_idx][1] * w * h3;
result[2] += velocities[flat_idx][2] * w * h3;
}
}
}
}
result
}
/// Interpolate a scalar field from the grid to a Lagrangian point.
fn interpolate_scalar(
pos: [f64; 3],
field: &[f64],
grid_spacing: f64,
grid_dims: [usize; 3],
) -> f64 {
if grid_spacing <= 0.0 {
return 0.0;
}
let h = grid_spacing;
let base = [pos[0] / h, pos[1] / h, pos[2] / h];
let mut result = 0.0;
let i_min = ((base[0] - 1.5).floor() as i64).max(0) as usize;
let i_max = ((base[0] + 1.5).ceil() as i64).max(0) as usize;
let j_min = ((base[1] - 1.5).floor() as i64).max(0) as usize;
let j_max = ((base[1] + 1.5).ceil() as i64).max(0) as usize;
let k_min = ((base[2] - 1.5).floor() as i64).max(0) as usize;
let k_max = ((base[2] + 1.5).ceil() as i64).max(0) as usize;
for i in i_min..=i_max.min(grid_dims[0].saturating_sub(1)) {
let dx = pos[0] - (i as f64) * h;
let phi_x = roma_delta_1d(dx, h);
if phi_x.abs() < 1e-30 {
continue;
}
for j in j_min..=j_max.min(grid_dims[1].saturating_sub(1)) {
let dy = pos[1] - (j as f64) * h;
let phi_y = roma_delta_1d(dy, h);
if phi_y.abs() < 1e-30 {
continue;
}
for k in k_min..=k_max.min(grid_dims[2].saturating_sub(1)) {
let dz = pos[2] - (k as f64) * h;
let phi_z = roma_delta_1d(dz, h);
let w = phi_x * phi_y * phi_z;
if w.abs() < 1e-30 {
continue;
}
let flat_idx = i * grid_dims[1] * grid_dims[2] + j * grid_dims[2] + k;
if flat_idx < field.len() {
let h3 = h * h * h;
result += field[flat_idx] * w * h3;
}
}
}
}
result
}
/// Compute the velocity gradient tensor at a point using central finite differences
/// on the LBM grid.
fn compute_velocity_gradient(
pos: [f64; 3],
velocities: &[[f64; 3]],
grid_spacing: f64,
grid_dims: [usize; 3],
) -> [[f64; 3]; 3] {
let eps = grid_spacing * 0.5;
let mut grad = [[0.0; 3]; 3];
for d in 0..3usize {
let mut pos_plus = pos;
let mut pos_minus = pos;
pos_plus[d] += eps;
pos_minus[d] -= eps;
let v_plus = interpolate_velocity(pos_plus, velocities, grid_spacing, grid_dims);
let v_minus = interpolate_velocity(pos_minus, velocities, grid_spacing, grid_dims);
let inv_2eps = 1.0 / (2.0 * eps);
for (c, grad_row) in grad.iter_mut().enumerate() {
grad_row[d] = (v_plus[c] - v_minus[c]) * inv_2eps;
}
}
grad
}
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
#[cfg(test)]
mod tests {
use super::*;
/// Test that the 1D Roma delta function integrates to 1 over the support.
#[test]
fn test_delta_function_normalization_1d() {
let h = 1.0;
let n = 10000;
let range = 3.0 * h; // well beyond the support of 1.5h
let dr = 2.0 * range / n as f64;
let mut integral = 0.0;
for i in 0..n {
let r = -range + (i as f64 + 0.5) * dr;
integral += roma_delta_1d(r, h) * dr;
}
assert!(
(integral - 1.0).abs() < 1e-4,
"1D delta integral = {integral}, expected ~1.0"
);
}
/// Test that the 3D delta function integrates to 1 over its support.
#[test]
fn test_delta_function_normalization_3d() {
let h = 1.0;
let n = 100; // per dimension
let range = 2.0 * h;
let dr = 2.0 * range / n as f64;
let mut integral = 0.0;
for i in 0..n {
let x = -range + (i as f64 + 0.5) * dr;
for j in 0..n {
let y = -range + (j as f64 + 0.5) * dr;
for k in 0..n {
let z = -range + (k as f64 + 0.5) * dr;
integral += delta_3d([x, y, z], h) * dr * dr * dr;
}
}
}
assert!(
(integral - 1.0).abs() < 1e-3,
"3D delta integral = {integral}, expected ~1.0"
);
}
/// Test velocity interpolation on a uniform velocity field.
/// Interpolating from a constant field should return that constant.
#[test]
fn test_velocity_interpolation_uniform_field() {
let grid_dims = [10, 10, 10];
let total = grid_dims[0] * grid_dims[1] * grid_dims[2];
let uniform_vel = [1.5, -0.3, 2.7];
let velocities: Vec<[f64; 3]> = vec![uniform_vel; total];
let h = 1.0;
// Interpolate at a point in the interior of the grid
let pos = [5.0, 5.0, 5.0];
let result = interpolate_velocity(pos, &velocities, h, grid_dims);
// The IBM interpolation of a constant field should recover that constant.
// With h=1 and the delta function integrating to 1/h^3 over grid points,
// the sum of delta * h^3 = 1 for a point exactly on a grid node.
for (d, (&res_d, &exp_d)) in result.iter().zip(uniform_vel.iter()).enumerate() {
assert!(
(res_d - exp_d).abs() < 0.1,
"dim {d}: interpolated {res_d}, expected {exp_d}"
);
}
}
/// Test that force spreading conserves the total force.
#[test]
fn test_force_spreading_conservation() {
let h = 1.0;
let grid_dims = [10, 10, 10];
let force = [3.0, -1.0, 2.0];
let pos = [5.0, 5.0, 5.0]; // on a grid node
let spread = spread_force(pos, force, h, grid_dims);
// Sum all spread forces
let mut total = [0.0; 3];
for &(_, f) in &spread {
total[0] += f[0];
total[1] += f[1];
total[2] += f[2];
}
// The total spread force should equal the original force times h^3
// because spread_to_grid multiplies by h^3 (the volume element).
// Actually, the sum of delta(x-X)*h^3 over grid points should be ~1
// for the Roma kernel, so sum of spread = force * h^3 * (sum of delta * h^3 over grid)
// With h=1: force * 1 * sum(delta), and sum(delta) ~ 1/h^3 = 1,
// so total ~ force * h^3.
// Actually spread already includes h^3, so total = force * sum(w * h^3)
// and sum(w) ~ 1/h^3 => total ~ force.
// Let's just check conservation to a reasonable tolerance.
for (d, (&tot_d, &f_d)) in total.iter().zip(force.iter()).enumerate() {
assert!(
(tot_d - f_d).abs() < 0.15,
"dim {d}: total spread force = {tot_d}, expected {f_d}"
);
}
}
/// Test round-trip coupling consistency: spread a velocity to grid, then
/// interpolate it back at the same point. The result should be close to
/// the original velocity.
#[test]
fn test_round_trip_coupling_consistency() {
let h = 1.0;
let grid_dims = [12, 12, 12];
let total_cells = grid_dims[0] * grid_dims[1] * grid_dims[2];
let pos = [6.0, 6.0, 6.0]; // exactly on a grid node
let original_vel = [2.0, -1.0, 0.5];
// Spread velocity to grid
let spread = spread_to_grid(pos, original_vel, h, grid_dims);
// Build velocity field from spread values
let mut velocities = vec![[0.0; 3]; total_cells];
for &(idx, vel) in &spread {
let flat = idx[0] * grid_dims[1] * grid_dims[2] + idx[1] * grid_dims[2] + idx[2];
if flat < total_cells {
velocities[flat][0] += vel[0];
velocities[flat][1] += vel[1];
velocities[flat][2] += vel[2];
}
}
// Interpolate back
let recovered = interpolate_velocity(pos, &velocities, h, grid_dims);
// Check consistency (not exact due to discrete convolution).
// Round-trip spread+interpolate includes h^6 * Σ δ² factor, so only
// directional consistency is expected.
for (d, (&rec_d, &orig_d)) in recovered.iter().zip(original_vel.iter()).enumerate() {
assert!(
(rec_d - orig_d).abs() < 2.0,
"dim {d}: recovered {rec_d}, expected {orig_d}"
);
}
}
/// Test grid index computation.
#[test]
fn test_grid_index_computation() {
let h = 0.5;
// Position exactly on grid node (2, 4, 6) in index space
let idx = interpolate_to_grid([1.0, 2.0, 3.0], h);
assert_eq!(idx, [2, 4, 6]);
// Position between nodes should round
let idx2 = interpolate_to_grid([0.3, 0.7, 1.1], h);
assert_eq!(idx2, [1, 1, 2]);
// Negative positions clamp to zero
let idx3 = interpolate_to_grid([-1.0, -0.5, 0.0], h);
assert_eq!(idx3, [0, 0, 0]);
}
/// Test FemLbmCoupling initialization and basic operations.
#[test]
fn test_coupling_initialization() {
let config = FemLbmCouplingConfig::default();
let mut coupling = FemLbmCoupling::new(config);
// Should fail before initialization
let result = coupling.transfer_velocity_to_lbm([10, 10, 10]);
assert!(result.is_err());
// Initialize
let nodes = vec![0, 1, 2];
let positions = vec![[1.0, 1.0, 1.0], [2.0, 2.0, 2.0], [3.0, 3.0, 3.0]];
let init_result = coupling.set_interface_nodes(nodes, positions, 1.0);
assert!(init_result.is_ok());
// Should succeed after initialization
let result = coupling.transfer_velocity_to_lbm([10, 10, 10]);
assert!(result.is_ok());
// Forces should be zero initially
let forces = coupling.get_fem_forces();
assert_eq!(forces.len(), 3);
for f in forces {
assert_eq!(*f, [0.0; 3]);
}
}
/// Test dimension mismatch error.
#[test]
fn test_dimension_mismatch_error() {
let config = FemLbmCouplingConfig::default();
let mut coupling = FemLbmCoupling::new(config);
let nodes = vec![0, 1, 2];
let positions = vec![[1.0, 1.0, 1.0], [2.0, 2.0, 2.0]]; // mismatched length
let result = coupling.set_interface_nodes(nodes, positions, 1.0);
assert!(result.is_err());
}
/// Test invalid grid spacing.
#[test]
fn test_invalid_grid_spacing() {
let config = FemLbmCouplingConfig::default();
let mut coupling = FemLbmCoupling::new(config);
let nodes = vec![0];
let positions = vec![[1.0, 1.0, 1.0]];
let result = coupling.set_interface_nodes(nodes, positions, -1.0);
assert!(result.is_err());
}
/// Test the delta function at specific known values.
#[test]
fn test_delta_function_specific_values() {
let h = 1.0;
// At r=0, the Roma kernel gives (1/3h)(1 + sqrt(1)) = (1/3)(1+1) = 2/3
let val_at_zero = delta_function(0.0, h);
assert!(
(val_at_zero - 2.0 / 3.0).abs() < 1e-10,
"delta(0, 1) = {val_at_zero}, expected {}",
2.0 / 3.0
);
// Beyond support: r >= 1.5h should give 0
let val_outside = delta_function(1.5, h);
assert!(
val_outside.abs() < 1e-10,
"delta(1.5, 1) = {val_outside}, expected 0"
);
// Symmetry: delta(-r) == delta(r)
let val_pos = delta_function(0.7, h);
let val_neg = delta_function(-0.7, h);
assert!(
(val_pos - val_neg).abs() < 1e-14,
"delta function should be symmetric"
);
}
/// Test that spreading with zero grid spacing returns empty.
#[test]
fn test_spread_zero_grid_spacing() {
let result = spread_force([1.0, 1.0, 1.0], [1.0, 0.0, 0.0], 0.0, [5, 5, 5]);
assert!(result.is_empty());
}
/// Test compute_fluid_forces with field size mismatch.
#[test]
fn test_fluid_forces_field_mismatch() {
let config = FemLbmCouplingConfig::default();
let mut coupling = FemLbmCoupling::new(config);
let nodes = vec![0];
let positions = vec![[1.0, 1.0, 1.0]];
coupling.set_interface_nodes(nodes, positions, 1.0).ok();
let grid_dims = [5, 5, 5];
let wrong_density = vec![1.0; 10]; // wrong size
let wrong_velocity = vec![[0.0; 3]; 10];
let result = coupling.compute_fluid_forces(&wrong_density, &wrong_velocity, 1.0, grid_dims);
assert!(result.is_err());
}
/// Test the full coupling step.
#[test]
fn test_coupling_step() {
let config = FemLbmCouplingConfig {
penalty_stiffness: 100.0,
..FemLbmCouplingConfig::default()
};
let mut coupling = FemLbmCoupling::new(config);
let nodes = vec![0];
let positions = vec![[3.0, 3.0, 3.0]];
coupling
.set_interface_nodes(nodes, positions.clone(), 1.0)
.ok();
// Set a velocity on the FEM side
coupling
.update_fem_state(&positions, &[[1.0, 0.0, 0.0]])
.ok();
let grid_dims = [8, 8, 8];
let total = grid_dims[0] * grid_dims[1] * grid_dims[2];
let density = vec![1.0; total];
let velocity = vec![[0.0; 3]; total]; // fluid at rest
let result = coupling.coupling_step(&density, &velocity, 1.0, grid_dims);
assert!(result.is_ok());
// Forces should be non-zero (penalty drives fluid toward solid velocity)
let forces = coupling.get_fem_forces();
let force_mag = (forces[0][0].powi(2) + forces[0][1].powi(2) + forces[0][2].powi(2)).sqrt();
assert!(
force_mag > 0.0,
"Expected non-zero forces from coupling step"
);
}
/// Test ImmersedBoundaryPoint creation.
#[test]
fn test_ib_point_creation() {
let p = ImmersedBoundaryPoint::new([1.0, 2.0, 3.0]);
assert_eq!(p.position, [1.0, 2.0, 3.0]);
assert_eq!(p.velocity, [0.0; 3]);
assert_eq!(p.force, [0.0; 3]);
assert_eq!(p.reference_position, [1.0, 2.0, 3.0]);
assert!((p.area_weight - 1.0).abs() < 1e-14);
let p2 = ImmersedBoundaryPoint::with_area_weight([4.0, 5.0, 6.0], 2.5);
assert!((p2.area_weight - 2.5).abs() < 1e-14);
}
/// Test reset_forces zeroes everything.
#[test]
fn test_reset_forces() {
let config = FemLbmCouplingConfig::default();
let mut coupling = FemLbmCoupling::new(config);
let nodes = vec![0, 1];
let positions = vec![[1.0, 1.0, 1.0], [2.0, 2.0, 2.0]];
coupling.set_interface_nodes(nodes, positions, 1.0).ok();
// Manually set some forces
coupling.fluid_forces[0] = [10.0, 20.0, 30.0];
coupling.fluid_forces[1] = [40.0, 50.0, 60.0];
coupling.ib_points[0].force = [10.0, 20.0, 30.0];
coupling.reset_forces();
for f in coupling.get_fem_forces() {
assert_eq!(*f, [0.0; 3]);
}
for ib in &coupling.ib_points {
assert_eq!(ib.force, [0.0; 3]);
}
}
}