vanaspati 1.0.0

Vanaspati — botany and plant behavior engine for growth, photosynthesis, seasonal cycles, and ecosystems
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
//! Cross-crate primitive-value conversions for AGNOS science crates.
//!
//! Bridge functions accept f64 inputs (matching badal/ushma convention) and
//! return f32 outputs (matching vanaspati convention). No external dependencies —
//! all conversions use primitive arithmetic only.

use crate::mortality::frost_mortality;
use crate::photosynthesis::temperature_factor;
use crate::season::growth_modifier_at;

// ── Constants ─────────────────────────────────────────────────────────

/// Solar PAR fraction — ~45% of total solar irradiance is photosynthetically active.
const SOLAR_PAR_FRACTION: f64 = 0.45;

/// PAR conversion — 1 W/m² of PAR ≈ 4.57 µmol photons/m²/s.
const PAR_CONVERSION: f64 = 4.57;

/// Combined solar to PAR conversion (W/m² total solar → µmol photons/m²/s PAR).
const SOLAR_TO_PAR: f64 = SOLAR_PAR_FRACTION * PAR_CONVERSION;

/// Root activity optimum temperature (°C).
const ROOT_OPTIMUM_CELSIUS: f64 = 20.0;

/// Beer-Lambert extinction coefficient for canopy cover (dimensionless).
const CANOPY_EXTINCTION_K: f64 = 0.5;

// ── Badal bridges (weather/atmosphere) ────────────────────────────────

/// Convert total solar irradiance (W/m²) to PAR (µmol photons/m²/s).
///
/// ~45% of total solar is in the PAR band (400–700 nm);
/// 1 W/m² PAR ≈ 4.57 µmol photons/m²/s.
///
/// Connects to: `photosynthesis_rate(max_rate, quantum_yield, light_intensity)`
///
/// - `irradiance_w_m2` — total solar irradiance (W/m²)
#[must_use]
#[inline]
pub fn solar_to_par(irradiance_w_m2: f64) -> f32 {
    let par = irradiance_w_m2.max(0.0) * SOLAR_TO_PAR;
    tracing::trace!(irradiance_w_m2, par, "solar_to_par");
    par as f32
}

/// Convert atmospheric conditions to photosynthesis inputs.
///
/// Returns `(temperature_celsius, par_umol_m2_s)` ready for
/// `temperature_factor()` and `photosynthesis_rate()`.
///
/// - `temperature_k` — air temperature (Kelvin)
/// - `solar_irradiance_w_m2` — total solar irradiance (W/m²)
#[must_use]
pub fn atmosphere_to_photosynthesis_inputs(
    temperature_k: f64,
    solar_irradiance_w_m2: f64,
) -> (f32, f32) {
    let temp_c = (temperature_k - 273.15) as f32;
    let par = solar_to_par(solar_irradiance_w_m2);
    tracing::trace!(
        temperature_k,
        temp_c,
        par,
        "atmosphere_to_photosynthesis_inputs"
    );
    (temp_c, par)
}

/// Convert rainfall rate and duration to water supply (liters/m²).
///
/// 1 mm rainfall = 1 liter/m².
///
/// Connects to: `drought_mortality(water_available, water_needed)`
///
/// - `rate_mm_hr` — precipitation rate (mm/hr)
/// - `duration_hours` — duration (hours)
#[must_use]
#[inline]
pub fn rainfall_to_water_supply(rate_mm_hr: f64, duration_hours: f64) -> f32 {
    let supply = rate_mm_hr.max(0.0) * duration_hours.max(0.0);
    tracing::trace!(
        rate_mm_hr,
        duration_hours,
        supply,
        "rainfall_to_water_supply"
    );
    supply as f32
}

/// Convert frost risk probability and plant hardiness to frost mortality.
///
/// Combines atmospheric frost probability with plant-level vulnerability.
/// Returns `frost_mortality(temp, hardiness) × frost_risk`.
///
/// Connects to: `frost_mortality(temp_celsius, hardiness_celsius)`
///
/// - `temperature_celsius` — air temperature (°C)
/// - `frost_risk` — frost probability from badal (0.0–1.0)
/// - `hardiness_celsius` — plant cold hardiness threshold (°C)
#[must_use]
pub fn frost_risk_to_mortality(
    temperature_celsius: f64,
    frost_risk: f64,
    hardiness_celsius: f64,
) -> f32 {
    if frost_risk <= 0.0 {
        return 0.0;
    }
    let risk = frost_risk.clamp(0.0, 1.0) as f32;
    let mort = frost_mortality(temperature_celsius as f32, hardiness_celsius as f32);
    let result = risk * mort;
    tracing::trace!(
        temperature_celsius,
        frost_risk,
        hardiness_celsius,
        result,
        "frost_risk_to_mortality"
    );
    result
}

/// Determine if frost conditions should trigger plant dormancy.
///
/// Returns `true` if frost risk exceeds 0.5 and temperature is below
/// the dormancy trigger point.
///
/// Connects to: `GrowthStage::Dormant`
///
/// - `temperature_celsius` — air temperature (°C)
/// - `frost_risk` — frost probability from badal (0.0–1.0)
/// - `dormancy_threshold_celsius` — temperature below which plant goes dormant (°C)
#[must_use]
#[inline]
pub fn frost_to_dormancy(
    temperature_celsius: f64,
    frost_risk: f64,
    dormancy_threshold_celsius: f64,
) -> bool {
    let dormant = frost_risk > 0.5 && temperature_celsius < dormancy_threshold_celsius;
    tracing::trace!(
        temperature_celsius,
        frost_risk,
        dormancy_threshold_celsius,
        dormant,
        "frost_to_dormancy"
    );
    dormant
}

/// Convert wind speed at reference height to wind at seed release height (m/s).
///
/// Logarithmic wind profile: `V(z) = V_ref × ln(z/z0) / ln(z_ref/z0)`
///
/// Connects to: `dispersal_distance(method, seed_mass_g, release_height_m, wind_speed_m_s)`
///
/// - `speed_ref_ms` — reference wind speed (m/s)
/// - `z_ref_m` — reference measurement height (m)
/// - `z_release_m` — seed release height (m)
/// - `z0_m` — surface roughness length (m, forest ≈ 1.0, grass ≈ 0.03)
#[must_use]
pub fn wind_to_dispersal_speed(
    speed_ref_ms: f64,
    z_ref_m: f64,
    z_release_m: f64,
    z0_m: f64,
) -> f32 {
    if z0_m <= 0.0 || z_ref_m <= z0_m || z_release_m <= z0_m || speed_ref_ms <= 0.0 {
        return 0.0;
    }
    let ln_ref = (z_ref_m / z0_m).ln();
    let ln_release = (z_release_m / z0_m).ln();
    let speed = speed_ref_ms * ln_release / ln_ref;
    tracing::trace!(
        speed_ref_ms,
        z_ref_m,
        z_release_m,
        z0_m,
        speed,
        "wind_to_dispersal_speed"
    );
    speed.max(0.0) as f32
}

/// Composite growth rate multiplier (0.0–1.0) from atmospheric conditions.
///
/// `multiplier = temperature_factor × light_factor × growth_modifier_at`
///
/// Light factor: `min(1.0, PAR / 800.0)` where 800 µmol/m²/s is full-sun
/// saturation for typical C3 plants.
///
/// Connects to: `GrowthModel::daily_growth()` (multiply result by this)
///
/// - `temperature_celsius` — air temperature (°C)
/// - `optimum_celsius` — species optimal temperature (°C)
/// - `solar_irradiance_w_m2` — total solar irradiance (W/m²)
/// - `day_of_year` — day (1–365)
/// - `latitude_deg` — latitude (degrees)
#[must_use]
pub fn growing_conditions_to_growth_multiplier(
    temperature_celsius: f64,
    optimum_celsius: f64,
    solar_irradiance_w_m2: f64,
    day_of_year: u16,
    latitude_deg: f64,
) -> f32 {
    let temp_f = temperature_factor(temperature_celsius as f32, optimum_celsius as f32);
    let par = solar_to_par(solar_irradiance_w_m2);
    let light_f = (par / 800.0).clamp(0.0, 1.0);
    let season_f = growth_modifier_at(day_of_year, latitude_deg as f32);
    let multiplier = temp_f * light_f * season_f;
    tracing::trace!(
        temperature_celsius,
        optimum_celsius,
        solar_irradiance_w_m2,
        temp_f,
        light_f,
        season_f,
        multiplier,
        "growing_conditions_to_growth_multiplier"
    );
    multiplier
}

/// Water stress factor on photosynthesis from soil moisture (0.0–1.0).
///
/// Wraps `photosynthesis::water_stress_factor` for use with soil water state.
/// Extracts relative water content from the soil and returns the stress factor.
///
/// Connects to: `photosynthesis_rate()` (multiply result by this)
///
/// - `relative_water_content` — soil RWC (0.0–1.0, from `SoilWater::relative_water_content()`)
#[must_use]
#[inline]
pub fn soil_water_to_photosynthesis_stress(relative_water_content: f64) -> f32 {
    crate::photosynthesis::water_stress_factor(relative_water_content as f32)
}

/// Water stress factor on growth from soil moisture (0.0–1.0).
///
/// Wraps `growth::water_stress_growth_factor` for use with soil water state.
/// Growth is more sensitive to drought than photosynthesis (threshold 0.6 vs 0.4).
///
/// Connects to: `GrowthModel::daily_growth()` (multiply result by this)
///
/// - `relative_water_content` — soil RWC (0.0–1.0, from `SoilWater::relative_water_content()`)
#[must_use]
#[inline]
pub fn soil_water_to_growth_stress(relative_water_content: f64) -> f32 {
    crate::growth::water_stress_growth_factor(relative_water_content as f32)
}

/// Nitrogen stress factor on growth from plant tissue N concentration (0.0–1.0).
///
/// Wraps `nitrogen::nitrogen_stress_factor` for cross-crate use.
///
/// Connects to: `GrowthModel::daily_growth()` (multiply result by this)
///
/// - `plant_n_concentration` — current tissue N (kg N / kg biomass)
/// - `critical_n_concentration` — minimum N for unstressed growth (kg N / kg biomass)
#[must_use]
#[inline]
pub fn nitrogen_to_growth_stress(plant_n_concentration: f64, critical_n_concentration: f64) -> f32 {
    crate::nitrogen::nitrogen_stress_factor(
        plant_n_concentration as f32,
        critical_n_concentration as f32,
    )
}

// ── Ushma bridges (thermodynamics) ────────────────────────────────────

/// Soil temperature to root activity scaling factor (0.0–1.0).
///
/// Gaussian bell curve with optimum at 20°C, σ² = 200.
/// Below 5°C roots are nearly inactive; above 35°C heat stress reduces activity.
///
/// Connects to: `RootSystem::water_uptake_rate` (scale by this factor)
///
/// - `soil_temperature_k` — soil temperature (Kelvin)
#[must_use]
#[inline]
pub fn soil_temperature_to_root_activity(soil_temperature_k: f64) -> f32 {
    let t_c = soil_temperature_k - 273.15;
    let diff = t_c - ROOT_OPTIMUM_CELSIUS;
    let activity = (-diff * diff / 200.0).exp();
    tracing::trace!(
        soil_temperature_k,
        t_c,
        activity,
        "soil_temperature_to_root_activity"
    );
    activity as f32
}

/// Soil temperature to growth factor (0.0–1.0).
///
/// Returns 0.0 if soil is frozen (< 273.15 K), otherwise returns root activity.
///
/// Connects to: `GrowthModel::daily_growth()` (multiply by this)
///
/// - `soil_temperature_k` — soil temperature (Kelvin)
#[must_use]
#[inline]
pub fn soil_temperature_to_growth_factor(soil_temperature_k: f64) -> f32 {
    if soil_temperature_k < 273.15 {
        tracing::trace!(
            soil_temperature_k,
            factor = 0.0,
            "soil_temperature_to_growth_factor (frozen)"
        );
        return 0.0;
    }
    soil_temperature_to_root_activity(soil_temperature_k)
}

/// Leaf cooling from evapotranspiration (°C reduction).
///
/// Transpiration cools leaves below air temperature.
/// Simplified: `ΔT ≈ ET_rate × 2.5` (°C per mm/hr), capped at 15°C.
///
/// Connects to: subtract from air temperature before `temperature_factor()`
///
/// - `et_rate_mm_hr` — evapotranspiration rate (mm/hr)
#[must_use]
#[inline]
pub fn evapotranspiration_cooling(et_rate_mm_hr: f64) -> f32 {
    let cooling = (et_rate_mm_hr.max(0.0) * 2.5).min(15.0);
    tracing::trace!(et_rate_mm_hr, cooling, "evapotranspiration_cooling");
    cooling as f32
}

/// Wet-bulb temperature to plant heat stress factor (0.0–1.0).
///
/// High wet-bulb temperatures impair transpirational cooling.
/// Stress onset at 28°C, severe at 35°C.
/// `stress = ((T_wb - 28) / 7).clamp(0, 1)`
///
/// Consumers subtract from 1.0 for a multiplier: `effective = 1.0 - stress`.
///
/// - `wet_bulb_k` — wet-bulb temperature (Kelvin)
#[must_use]
#[inline]
pub fn wet_bulb_to_heat_stress(wet_bulb_k: f64) -> f32 {
    let t_c = wet_bulb_k - 273.15;
    let stress = ((t_c - 28.0) / 7.0).clamp(0.0, 1.0);
    tracing::trace!(wet_bulb_k, t_c, stress, "wet_bulb_to_heat_stress");
    stress as f32
}

// ── Pravash bridges (fluid dynamics) ──────────────────────────────────

/// Convert wind speed to leaf boundary layer conductance (mol H₂O/m²/s).
///
/// Wraps `stomata::boundary_layer_conductance` for use with pravash/badal
/// wind speed outputs.
///
/// Connects to: `boundary_layer_conductance(wind, leaf_width)` → `total_leaf_conductance()`
///
/// - `wind_speed_ms` — wind at canopy height (m/s)
/// - `leaf_width_m` — characteristic leaf dimension (meters)
#[must_use]
#[inline]
pub fn wind_to_boundary_conductance(wind_speed_ms: f64, leaf_width_m: f64) -> f32 {
    crate::stomata::boundary_layer_conductance(wind_speed_ms as f32, leaf_width_m as f32)
}

/// Convert atmospheric humidity to stomatal VPD (kPa).
///
/// Computes VPD from air temperature and relative humidity — the driving
/// force for transpiration through stomata.
///
/// Connects to: `vapor_pressure_deficit()` → `transpiration_rate()`
///
/// - `temperature_celsius` — air temperature (°C)
/// - `relative_humidity_fraction` — RH (0.0–1.0)
#[must_use]
pub fn humidity_to_vpd(temperature_celsius: f64, relative_humidity_fraction: f64) -> f32 {
    let es = crate::stomata::saturation_vapor_pressure(temperature_celsius as f32);
    let ea = es * (relative_humidity_fraction as f32).clamp(0.0, 1.0);
    let vpd = crate::stomata::vapor_pressure_deficit(es, ea);
    tracing::trace!(
        temperature_celsius,
        relative_humidity_fraction,
        es,
        ea,
        vpd,
        "humidity_to_vpd"
    );
    vpd
}

// ── Succession bridges ───────────────────────────────────────────────

/// Understory light to successional growth advantage (dimensionless ratio).
///
/// Returns the ratio of climax to pioneer effective growth at a given light level.
/// Ratio > 1.0 means climax species are favored (canopy closure).
/// Ratio < 1.0 means pioneer species are favored (open conditions).
///
/// Connects to: `understory_light_fraction()` → succession decisions
///
/// - `understory_light_fraction` — light reaching forest floor (0.0–1.0)
#[must_use]
pub fn light_to_successional_advantage(understory_light_fraction: f64) -> f32 {
    let light = understory_light_fraction as f32;
    let pioneer = crate::succession::effective_growth_multiplier(
        light,
        crate::succession::SuccessionalStage::Pioneer,
    );
    let climax = crate::succession::effective_growth_multiplier(
        light,
        crate::succession::SuccessionalStage::Climax,
    );
    if pioneer <= 0.0 {
        return if climax > 0.0 { f32::INFINITY } else { 1.0 };
    }
    let ratio = climax / pioneer;
    tracing::trace!(
        understory_light_fraction,
        pioneer,
        climax,
        ratio,
        "light_to_successional_advantage"
    );
    ratio
}

// ── Fire bridges (badal weather → fire ecology) ──────────────────────

/// Fire weather to fire risk probability (0.0–1.0).
///
/// Combines temperature, humidity, and wind into a fire danger index.
/// Hot, dry, windy conditions increase fire risk.
///
/// `risk = temp_factor × (1.0 - humidity) × wind_factor`
///
/// Connects to: badal weather data → `fire_mortality()`, `resprout_vigor()`
///
/// - `temperature_celsius` — air temperature (°C)
/// - `relative_humidity` — relative humidity (0.0–1.0)
/// - `wind_speed_ms` — wind speed (m/s)
#[must_use]
pub fn fire_weather_risk(
    temperature_celsius: f64,
    relative_humidity: f64,
    wind_speed_ms: f64,
) -> f32 {
    let temp = temperature_celsius as f32;
    let rh = (relative_humidity as f32).clamp(0.0, 1.0);
    let wind = (wind_speed_ms as f32).max(0.0);

    // Temperature factor: ramps from 0 at 20°C to 1 at 40°C
    let temp_f = ((temp - 20.0) / 20.0).clamp(0.0, 1.0);
    // Dryness factor
    let dry_f = 1.0 - rh;
    // Wind factor: ramps from 0.2 at calm to 1.0 at 15 m/s
    let wind_f = (0.2 + 0.8 * (wind / 15.0).min(1.0)).clamp(0.0, 1.0);

    let risk = (temp_f * dry_f * wind_f).clamp(0.0, 1.0);
    tracing::trace!(
        temperature_celsius,
        relative_humidity,
        wind_speed_ms,
        temp_f,
        dry_f,
        wind_f,
        risk,
        "fire_weather_risk"
    );
    risk
}

// ── Mycorrhiza bridges ───────────────────────────────────────────────

/// Mycorrhizal-enhanced nitrogen uptake from soil state (kg N/m²/day).
///
/// Wraps `mycorrhiza::enhanced_n_uptake` for use with soil nitrogen pool.
/// Applies colonization and fungal type to scale base uptake.
///
/// Connects to: `nitrogen_uptake()` → `enhanced_n_uptake()`
///
/// - `base_n_uptake` — base nitrogen uptake without fungi (kg N/m²/day)
/// - `colonization_fraction` — root colonization (0.0–1.0)
/// - `is_ectomycorrhizal` — true for ECM (forest trees), false for AM (grasses/herbs)
#[must_use]
#[inline]
pub fn mycorrhiza_enhanced_uptake(
    base_n_uptake: f64,
    colonization_fraction: f64,
    is_ectomycorrhizal: bool,
) -> f32 {
    let myc_type = if is_ectomycorrhizal {
        crate::mycorrhiza::MycorrhizalType::Ectomycorrhizal
    } else {
        crate::mycorrhiza::MycorrhizalType::Arbuscular
    };
    crate::mycorrhiza::enhanced_n_uptake(
        base_n_uptake as f32,
        myc_type,
        colonization_fraction as f32,
    )
}

// ── Allelopathy bridges ──────────────────────────────────────────────

/// Allelopathic growth suppression factor (0.0–1.0, where 1.0 = no suppression).
///
/// Returns a multiplier to apply to neighbor growth rate.
/// Wraps `allelopathy::growth_inhibition` and inverts to a growth factor.
///
/// `factor = 1.0 - growth_inhibition(concentration, sensitivity)`
///
/// Connects to: `GrowthModel::daily_growth()` (multiply result by this)
///
/// - `soil_allelochemical_concentration` — soil toxin level (arbitrary units/m²)
/// - `species_sensitivity` — target species sensitivity (higher = more sensitive)
#[must_use]
#[inline]
pub fn allelopathy_growth_factor(
    soil_allelochemical_concentration: f64,
    species_sensitivity: f64,
) -> f32 {
    let inhibition = crate::allelopathy::growth_inhibition(
        soil_allelochemical_concentration as f32,
        species_sensitivity as f32,
    );
    1.0 - inhibition
}

// ── Jantu bridges (creature behavior) ─────────────────────────────────

/// Herbivore feeding intensity to plant biomass loss (kg).
///
/// Converts jantu creature feeding behavior into plant biomass removal.
/// Returns total biomass removed across all organs.
///
/// Connects to: `BiomassPool` (subtract returned value), `herbivory_mortality()`
///
/// - `leaf_kg` — plant leaf biomass (kg)
/// - `stem_kg` — plant stem biomass (kg)
/// - `root_kg` — plant root biomass (kg)
/// - `reproductive_kg` — plant reproductive biomass (kg)
/// - `feeding_intensity` — creature feeding intensity (0.0–1.0)
/// - `is_browser` — true for browsing (deer), false for grazing (cattle)
#[must_use]
pub fn herbivore_to_biomass_loss(
    leaf_kg: f64,
    stem_kg: f64,
    root_kg: f64,
    reproductive_kg: f64,
    feeding_intensity: f64,
    is_browser: bool,
) -> f32 {
    let htype = if is_browser {
        crate::herbivory::HerbivoryType::Browsing
    } else {
        crate::herbivory::HerbivoryType::Grazing
    };
    crate::herbivory::total_biomass_removed(
        leaf_kg as f32,
        stem_kg as f32,
        root_kg as f32,
        reproductive_kg as f32,
        htype,
        feeding_intensity as f32,
    )
}

/// Canopy leaf area index to habitat cover score (0.0–1.0).
///
/// Beer-Lambert extinction: `cover = 1 - e^(-k × LAI)`, k = 0.5.
/// Dense canopy provides better cover for creatures.
///
/// Connects to: `height_to_leaf_area()` via LAI computation
///
/// - `leaf_area_index` — LAI (m² leaf per m² ground, dimensionless)
#[must_use]
#[inline]
pub fn canopy_to_habitat_score(leaf_area_index: f64) -> f32 {
    let score = 1.0 - (-CANOPY_EXTINCTION_K * leaf_area_index.max(0.0)).exp();
    tracing::trace!(leaf_area_index, score, "canopy_to_habitat_score");
    score as f32
}

/// Reproductive biomass to food availability score (0.0–1.0).
///
/// Logarithmic saturation: `score = 1 - e^(-seed_count / 1000)`.
/// Many small seeds or fewer large fruits both provide food.
///
/// Connects to: `BiomassPool::reproductive_kg` and `SeedProfile::mass_g`
///
/// - `reproductive_biomass_kg` — reproductive organ mass (kilograms)
/// - `seed_mass_g` — individual seed/fruit mass (grams)
#[must_use]
pub fn seed_production_to_food(reproductive_biomass_kg: f64, seed_mass_g: f64) -> f32 {
    if reproductive_biomass_kg <= 0.0 || seed_mass_g <= 0.0 {
        return 0.0;
    }
    let seed_count = reproductive_biomass_kg * 1000.0 / seed_mass_g;
    let score = (1.0 - (-seed_count / 1000.0).exp()).min(1.0);
    tracing::trace!(
        reproductive_biomass_kg,
        seed_mass_g,
        seed_count,
        score,
        "seed_production_to_food"
    );
    score as f32
}

#[cfg(test)]
mod tests {
    use super::*;

    // ── Badal bridge tests ────────────────────────────────────────────

    #[test]
    fn solar_to_par_full_sun() {
        let par = solar_to_par(1000.0);
        // 1000 × 0.45 × 4.57 ≈ 2057
        assert!((par - 2056.5).abs() < 1.0, "got {par}");
    }

    #[test]
    fn solar_to_par_zero() {
        assert_eq!(solar_to_par(0.0), 0.0);
    }

    #[test]
    fn solar_to_par_negative_clamped() {
        assert_eq!(solar_to_par(-100.0), 0.0);
    }

    #[test]
    fn atmosphere_to_photosynthesis_summer() {
        let (temp, par) = atmosphere_to_photosynthesis_inputs(298.15, 800.0);
        assert!((temp - 25.0).abs() < 0.1);
        assert!(par > 1600.0);
    }

    #[test]
    fn atmosphere_to_photosynthesis_night() {
        let (temp, par) = atmosphere_to_photosynthesis_inputs(283.15, 0.0);
        assert!((temp - 10.0).abs() < 0.1);
        assert_eq!(par, 0.0);
    }

    #[test]
    fn rainfall_basic() {
        let supply = rainfall_to_water_supply(5.0, 2.0);
        assert!((supply - 10.0).abs() < 0.01);
    }

    #[test]
    fn rainfall_zero() {
        assert_eq!(rainfall_to_water_supply(0.0, 5.0), 0.0);
    }

    #[test]
    fn rainfall_negative_clamped() {
        assert_eq!(rainfall_to_water_supply(-3.0, 2.0), 0.0);
    }

    #[test]
    fn frost_risk_warm() {
        assert_eq!(frost_risk_to_mortality(10.0, 0.0, -10.0), 0.0);
    }

    #[test]
    fn frost_risk_cold() {
        let m = frost_risk_to_mortality(-15.0, 0.8, -10.0);
        assert!(
            m > 0.5,
            "cold + high risk should produce high mortality, got {m}"
        );
    }

    #[test]
    fn frost_risk_no_risk() {
        let m = frost_risk_to_mortality(-15.0, 0.0, -10.0);
        assert_eq!(m, 0.0, "zero frost risk → zero mortality");
    }

    #[test]
    fn dormancy_triggers() {
        assert!(frost_to_dormancy(-5.0, 0.7, 0.0));
    }

    #[test]
    fn dormancy_warm() {
        assert!(!frost_to_dormancy(10.0, 0.1, 0.0));
    }

    #[test]
    fn dormancy_low_risk() {
        assert!(!frost_to_dormancy(-5.0, 0.3, 0.0));
    }

    #[test]
    fn wind_same_height() {
        let s = wind_to_dispersal_speed(10.0, 10.0, 10.0, 1.0);
        assert!((s - 10.0).abs() < 0.01);
    }

    #[test]
    fn wind_higher() {
        let s = wind_to_dispersal_speed(10.0, 10.0, 20.0, 1.0);
        assert!(s > 10.0, "higher release → faster wind");
    }

    #[test]
    fn wind_lower() {
        let s = wind_to_dispersal_speed(10.0, 10.0, 5.0, 1.0);
        assert!(s < 10.0, "lower release → slower wind");
    }

    #[test]
    fn wind_invalid_z0() {
        assert_eq!(wind_to_dispersal_speed(10.0, 10.0, 20.0, 0.0), 0.0);
    }

    #[test]
    fn wind_zero_speed() {
        assert_eq!(wind_to_dispersal_speed(0.0, 10.0, 20.0, 1.0), 0.0);
    }

    #[test]
    fn growing_conditions_summer_optimal() {
        let m = growing_conditions_to_growth_multiplier(25.0, 25.0, 800.0, 172, 45.0);
        assert!(
            m > 0.7,
            "optimal summer conditions should give high multiplier, got {m}"
        );
    }

    #[test]
    fn growing_conditions_winter() {
        let m = growing_conditions_to_growth_multiplier(0.0, 25.0, 100.0, 356, 45.0);
        assert!(
            m < 0.1,
            "winter conditions should give low multiplier, got {m}"
        );
    }

    #[test]
    fn growing_conditions_night() {
        let m = growing_conditions_to_growth_multiplier(25.0, 25.0, 0.0, 172, 45.0);
        assert_eq!(m, 0.0, "no light → zero growth");
    }

    // ── Ushma bridge tests ────────────────────────────────────────────

    #[test]
    fn soil_temp_optimum() {
        let a = soil_temperature_to_root_activity(293.15); // 20°C
        assert!((a - 1.0).abs() < 0.01, "optimum should be ~1.0, got {a}");
    }

    #[test]
    fn soil_temp_frozen() {
        let a = soil_temperature_to_root_activity(268.15); // -5°C
        assert!(a < 0.1, "frozen soil should have low activity, got {a}");
    }

    #[test]
    fn soil_temp_hot() {
        let a = soil_temperature_to_root_activity(318.15); // 45°C
        assert!(a < 0.3, "hot soil should reduce activity, got {a}");
    }

    #[test]
    fn soil_growth_frozen() {
        assert_eq!(soil_temperature_to_growth_factor(270.0), 0.0);
    }

    #[test]
    fn soil_growth_warm() {
        let f = soil_temperature_to_growth_factor(293.15);
        assert!(f > 0.9);
    }

    #[test]
    fn et_cooling_typical() {
        let c = evapotranspiration_cooling(3.0);
        assert!((c - 7.5).abs() < 0.01);
    }

    #[test]
    fn et_cooling_zero() {
        assert_eq!(evapotranspiration_cooling(0.0), 0.0);
    }

    #[test]
    fn et_cooling_capped() {
        let c = evapotranspiration_cooling(100.0);
        assert_eq!(c, 15.0);
    }

    #[test]
    fn et_cooling_negative() {
        assert_eq!(evapotranspiration_cooling(-5.0), 0.0);
    }

    #[test]
    fn wet_bulb_no_stress() {
        let s = wet_bulb_to_heat_stress(295.15); // 22°C
        assert_eq!(s, 0.0);
    }

    #[test]
    fn wet_bulb_severe() {
        let s = wet_bulb_to_heat_stress(308.15); // 35°C
        assert_eq!(s, 1.0);
    }

    #[test]
    fn wet_bulb_onset() {
        let s = wet_bulb_to_heat_stress(301.15); // 28°C
        assert!(s < 0.01, "at onset, stress should be ~0, got {s}");
    }

    // ── Pravash bridge tests ───────────────────────────────────────────

    #[test]
    fn wind_boundary_conductance_basic() {
        let gb = wind_to_boundary_conductance(2.0, 0.05);
        assert!(gb > 0.5 && gb < 1.5, "got {gb}");
    }

    #[test]
    fn wind_boundary_conductance_zero_wind() {
        assert_eq!(wind_to_boundary_conductance(0.0, 0.05), 0.0);
    }

    #[test]
    fn humidity_to_vpd_dry() {
        let vpd = humidity_to_vpd(25.0, 0.3); // 30% RH at 25°C
        assert!(vpd > 1.5, "dry air should produce high VPD, got {vpd}");
    }

    #[test]
    fn humidity_to_vpd_saturated() {
        let vpd = humidity_to_vpd(25.0, 1.0); // 100% RH
        assert!(vpd < 0.01, "saturated air should have ~0 VPD, got {vpd}");
    }

    #[test]
    fn humidity_to_vpd_increases_with_dryness() {
        let humid = humidity_to_vpd(25.0, 0.8);
        let dry = humidity_to_vpd(25.0, 0.3);
        assert!(dry > humid);
    }

    // ── Jantu bridge tests ────────────────────────────────────────────

    #[test]
    fn canopy_zero_lai() {
        let s = canopy_to_habitat_score(0.0);
        assert!(s.abs() < 0.01);
    }

    #[test]
    fn canopy_high_lai() {
        let s = canopy_to_habitat_score(6.0);
        assert!(s > 0.9, "dense canopy should give high cover, got {s}");
    }

    #[test]
    fn canopy_moderate_lai() {
        let s = canopy_to_habitat_score(3.0);
        assert!(s > 0.7 && s < 0.85, "LAI=3 should give ~0.78, got {s}");
    }

    #[test]
    fn canopy_negative_clamped() {
        assert_eq!(canopy_to_habitat_score(-1.0), 0.0);
    }

    #[test]
    fn seed_production_none() {
        assert_eq!(seed_production_to_food(0.0, 1.0), 0.0);
    }

    #[test]
    fn seed_production_moderate() {
        let s = seed_production_to_food(1.0, 1.0); // 1000 seeds
        assert!(s > 0.5 && s < 0.7, "1000 seeds should give ~0.63, got {s}");
    }

    #[test]
    fn seed_production_heavy() {
        let s = seed_production_to_food(10.0, 0.1); // 100000 seeds
        assert!(s > 0.99, "many seeds should saturate, got {s}");
    }

    #[test]
    fn seed_production_zero_mass() {
        assert_eq!(seed_production_to_food(1.0, 0.0), 0.0);
    }

    #[test]
    fn seed_production_negative() {
        assert_eq!(seed_production_to_food(-1.0, 1.0), 0.0);
    }

    // ── Water stress bridge tests ────────────────────────────────────

    #[test]
    fn soil_water_photosynthesis_stress_wet() {
        assert_eq!(soil_water_to_photosynthesis_stress(1.0), 1.0);
    }

    #[test]
    fn soil_water_photosynthesis_stress_dry() {
        let f = soil_water_to_photosynthesis_stress(0.2);
        assert!((f - 0.5).abs() < 0.01, "got {f}");
    }

    #[test]
    fn soil_water_photosynthesis_stress_wilted() {
        assert_eq!(soil_water_to_photosynthesis_stress(0.0), 0.0);
    }

    #[test]
    fn soil_water_growth_stress_wet() {
        assert_eq!(soil_water_to_growth_stress(1.0), 1.0);
    }

    #[test]
    fn soil_water_growth_stress_moderate() {
        let f = soil_water_to_growth_stress(0.3);
        assert!((f - 0.5).abs() < 0.01, "got {f}");
    }

    // ── Nitrogen stress bridge tests ─────────────────────────────────

    #[test]
    fn nitrogen_growth_stress_sufficient() {
        assert_eq!(nitrogen_to_growth_stress(0.015, 0.012), 1.0);
    }

    #[test]
    fn nitrogen_growth_stress_deficient() {
        let f = nitrogen_to_growth_stress(0.006, 0.012);
        assert!((f - 0.5).abs() < 0.01, "got {f}");
    }

    #[test]
    fn nitrogen_growth_stress_zero() {
        assert_eq!(nitrogen_to_growth_stress(0.0, 0.012), 0.0);
    }

    #[test]
    fn growth_stress_more_sensitive_than_photosynthesis() {
        let rwc = 0.5;
        let growth = soil_water_to_growth_stress(rwc);
        let photo = soil_water_to_photosynthesis_stress(rwc);
        assert!(growth < photo, "growth={growth}, photo={photo}");
    }

    // ── Fire weather bridge tests ─────────────────────────────────────

    #[test]
    fn fire_weather_hot_dry_windy() {
        let risk = fire_weather_risk(40.0, 0.1, 15.0);
        assert!(risk > 0.5, "extreme fire weather, got {risk}");
    }

    #[test]
    fn fire_weather_cool_humid() {
        let risk = fire_weather_risk(15.0, 0.9, 2.0);
        assert_eq!(risk, 0.0, "cool humid = no fire risk");
    }

    #[test]
    fn fire_weather_moderate() {
        let risk = fire_weather_risk(30.0, 0.3, 10.0);
        assert!(
            (0.1..0.7).contains(&risk),
            "moderate conditions, got {risk}"
        );
    }

    // ── Mycorrhiza bridge tests ─────────────────────────────────────

    #[test]
    fn mycorrhiza_enhances_uptake() {
        let base = 0.001;
        let enhanced = mycorrhiza_enhanced_uptake(base, 0.7, true);
        assert!(enhanced > base as f32);
    }

    #[test]
    fn mycorrhiza_zero_colonization() {
        let base = 0.001;
        let enhanced = mycorrhiza_enhanced_uptake(base, 0.0, true);
        assert!((enhanced - base as f32).abs() < 0.0001);
    }

    // ── Allelopathy bridge tests ─────────────────────────────────────

    #[test]
    fn allelopathy_no_toxin() {
        assert_eq!(allelopathy_growth_factor(0.0, 5.0), 1.0);
    }

    #[test]
    fn allelopathy_high_toxin_sensitive() {
        let f = allelopathy_growth_factor(1.0, 10.0);
        assert!(f < 0.1, "high toxin should strongly suppress, got {f}");
    }

    #[test]
    fn allelopathy_tolerant_less_affected() {
        let tolerant = allelopathy_growth_factor(0.5, 1.0);
        let sensitive = allelopathy_growth_factor(0.5, 10.0);
        assert!(tolerant > sensitive);
    }

    // ── Succession bridge tests ──────────────────────────────────────

    #[test]
    fn successional_advantage_full_sun_favors_pioneer() {
        let ratio = light_to_successional_advantage(1.0);
        assert!(ratio < 1.0, "full sun should favor pioneer, got {ratio}");
    }

    #[test]
    fn successional_advantage_shade_favors_climax() {
        let ratio = light_to_successional_advantage(0.15);
        assert!(ratio > 1.0, "shade should favor climax, got {ratio}");
    }

    // ── Jantu herbivory bridge tests ─────────────────────────────────

    #[test]
    fn herbivore_grazing_removes_biomass() {
        let loss = herbivore_to_biomass_loss(50.0, 100.0, 30.0, 10.0, 0.5, false);
        assert!(loss > 0.0);
    }

    #[test]
    fn herbivore_browsing_removes_more_stem() {
        let graze = herbivore_to_biomass_loss(50.0, 100.0, 30.0, 10.0, 0.5, false);
        let browse = herbivore_to_biomass_loss(50.0, 100.0, 30.0, 10.0, 0.5, true);
        // Both should remove biomass; browsing hits more stem
        assert!(graze > 0.0);
        assert!(browse > 0.0);
    }

    #[test]
    fn herbivore_zero_intensity() {
        let loss = herbivore_to_biomass_loss(50.0, 100.0, 30.0, 10.0, 0.0, false);
        assert_eq!(loss, 0.0);
    }
}