bhava 2.0.0

Emotion and personality engine — trait spectrums, mood vectors, archetypes, behavioral mapping
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
//! Jivanu microbiology integration — sickness behavior and immune state pressing on emotion.
//!
//! Provides bridge functions between jivanu's microbial/immune system models and
//! bhava's emotion/personality systems. Infection triggers sickness behavior:
//! fatigue, social withdrawal, anhedonia, irritability. Immune recovery restores
//! baseline mood. Drug effects modulate cognition and energy.
//!
//! Requires the `microbiology` feature.
//!
//! # Layer Model
//!
//! ```text
//! ┌──────────────────────────────────┐
//! │  Bhava (Personality Engine)      │
//! │  Mood, stress, energy, cognition │
//! ├──────────────────────────────────┤
//! │  This module (bridge)            │
//! │  Immune state → Emotion effects  │
//! ├──────────────────────────────────┤
//! │  Jivanu (Microbiology Engine)    │
//! │  SIR/SEIR, metabolism, pharma    │
//! └──────────────────────────────────┘
//! ```
//!
//! # Bridge Functions
//!
//! ## Infection → Sickness Behavior
//! - [`sickness_mood`] — infected fraction → cytokine-driven mood depression
//! - [`sickness_severity`] — SEIR state → normalized severity (0–1)
//!
//! ## Immune State → Recovery
//! - [`recovery_mood_boost`] — recovered fraction → mood restoration
//! - [`immune_energy_drain`] — infected fraction → energy cost of immune response
//!
//! ## Epidemiology → Social Behavior
//! - [`contagion_avoidance`] — R0 → social withdrawal pressure
//! - [`herd_safety`] — vaccination coverage → trust/safety feeling
//!
//! ## Metabolism → Energy
//! - [`metabolic_efficiency`] — growth rate modifier → energy availability
//! - [`temperature_stress`] — cardinal temperature model → thermal discomfort
//!
//! ## Pharmacology → Cognition
//! - [`drug_cognitive_effect`] — plasma concentration vs EC50 → cognitive modifier
//! - [`drug_sedation`] — concentration above therapeutic → drowsiness

use crate::mood::MoodVector;

// ── Infection → Sickness Behavior ──────────────────────────────────────

/// Convert infected fraction to sickness behavior mood shift.
///
/// Sickness behavior is a conserved adaptive response: cytokines trigger
/// fatigue, anhedonia (reduced joy), social withdrawal (reduced trust),
/// and irritability (increased frustration). Scales with infection severity.
///
/// ```
/// use bhava::microbiology::sickness_mood;
///
/// let healthy = sickness_mood(0.0);
/// assert!(healthy.frustration < 0.01);
///
/// let sick = sickness_mood(0.5);
/// assert!(sick.frustration > 0.2);
/// assert!(sick.joy < 0.0);
/// ```
#[cfg_attr(feature = "tracing", tracing::instrument(skip_all))]
#[must_use]
#[inline]
pub fn sickness_mood(infected_fraction: f32) -> MoodVector {
    let i = infected_fraction.clamp(0.0, 1.0);
    MoodVector {
        joy: (-i * 0.6).clamp(-1.0, 0.0),
        arousal: (-i * 0.4).clamp(-1.0, 0.0),
        dominance: (-i * 0.3).clamp(-1.0, 0.0),
        trust: (-i * 0.5).clamp(-1.0, 0.0),
        interest: (-i * 0.5).clamp(-1.0, 0.0),
        frustration: (i * 0.5).clamp(0.0, 1.0),
    }
}

/// Compute normalized sickness severity from SEIR state.
///
/// Combines exposed + infected fractions, weighted by disease progression.
/// Returns 0.0 (healthy) to 1.0 (critically ill).
///
/// ```
/// use bhava::microbiology::sickness_severity;
///
/// assert!((sickness_severity(0.0, 0.0) - 0.0).abs() < 0.01);
/// assert!(sickness_severity(0.1, 0.3) > 0.2);
/// ```
#[cfg_attr(feature = "tracing", tracing::instrument(skip_all))]
#[must_use]
#[inline]
pub fn sickness_severity(exposed_fraction: f32, infected_fraction: f32) -> f32 {
    let e = exposed_fraction.clamp(0.0, 1.0);
    let i = infected_fraction.clamp(0.0, 1.0);
    // Infected counts more than exposed (symptomatic vs incubating)
    (e * 0.3 + i * 0.7).clamp(0.0, 1.0)
}

// ── Immune State → Recovery ────────────────────────────────────────────

/// Convert recovered fraction to a mood restoration boost.
///
/// As the immune system wins, energy and mood recover. Higher recovered
/// fraction → more positive mood shift. Represents the relief and
/// vitality of overcoming illness.
///
/// ```
/// use bhava::microbiology::recovery_mood_boost;
///
/// let early = recovery_mood_boost(0.1);
/// let recovered = recovery_mood_boost(0.8);
/// assert!(recovered.joy > early.joy);
/// ```
#[cfg_attr(feature = "tracing", tracing::instrument(skip_all))]
#[must_use]
#[inline]
pub fn recovery_mood_boost(recovered_fraction: f32) -> MoodVector {
    let r = recovered_fraction.clamp(0.0, 1.0);
    MoodVector {
        joy: (r * 0.4).clamp(0.0, 1.0),
        arousal: (r * 0.2).clamp(0.0, 1.0),
        dominance: (r * 0.2).clamp(0.0, 1.0),
        trust: (r * 0.3).clamp(0.0, 1.0),
        interest: (r * 0.3).clamp(0.0, 1.0),
        frustration: 0.0,
    }
}

/// Compute energy drain from immune response.
///
/// Fighting infection costs energy — fever, immune cell production, cytokine
/// storms. Returns a drain multiplier (1.0 = normal, up to 3.0 = severe).
///
/// ```
/// use bhava::microbiology::immune_energy_drain;
///
/// assert!((immune_energy_drain(0.0) - 1.0).abs() < 0.01);
/// assert!(immune_energy_drain(0.5) > 1.5);
/// ```
#[cfg_attr(feature = "tracing", tracing::instrument(skip_all))]
#[must_use]
#[inline]
pub fn immune_energy_drain(infected_fraction: f32) -> f32 {
    let i = infected_fraction.clamp(0.0, 1.0);
    // Linear: 1.0 at healthy, up to 3.0 at fully infected
    1.0 + i * 2.0
}

// ── Epidemiology → Social Behavior ─────────────────────────────────────

/// Compute social withdrawal pressure from disease transmissibility.
///
/// Higher R0 → stronger instinct to avoid social contact. Uses jivanu's
/// R0 calculation. Returns withdrawal pressure [0.0, 1.0].
/// Falls back to 0.0 on error.
///
/// ```
/// use bhava::microbiology::contagion_avoidance;
///
/// let mild = contagion_avoidance(1.5, 0.2);
/// let severe = contagion_avoidance(5.0, 0.2);
/// assert!(severe > mild);
/// ```
#[cfg_attr(feature = "tracing", tracing::instrument(skip_all))]
#[must_use]
pub fn contagion_avoidance(beta: f64, gamma: f64) -> f32 {
    let r0 = jivanu::epidemiology::r_naught(beta, gamma).unwrap_or(1.0);
    // Sigmoid on R0: R0=1 → 0.0, R0=3 → ~0.5, R0=10 → ~0.9
    let pressure = 1.0 - (-0.3 * (r0 - 1.0)).exp();
    (pressure as f32).clamp(0.0, 1.0)
}

/// Convert vaccination/herd immunity coverage to a safety feeling.
///
/// Higher coverage → more trust, less anxiety. Uses jivanu's herd immunity
/// threshold. Returns trust modifier [-0.3, 0.3].
/// Falls back to 0.0 on error.
///
/// ```
/// use bhava::microbiology::herd_safety;
///
/// let low_coverage = herd_safety(0.2, 3.0);
/// let high_coverage = herd_safety(0.8, 3.0);
/// assert!(high_coverage > low_coverage);
/// ```
#[cfg_attr(feature = "tracing", tracing::instrument(skip_all))]
#[must_use]
pub fn herd_safety(vaccination_coverage: f64, r0: f64) -> f32 {
    let threshold = jivanu::epidemiology::herd_immunity_threshold(r0).unwrap_or(1.0);
    if threshold <= 0.0 {
        return 0.3;
    }
    // How close to herd immunity: 0 = none, 1 = at threshold, >1 = above
    let ratio = (vaccination_coverage / threshold).clamp(0.0, 2.0);
    ((ratio - 0.5) * 0.4) as f32
}

// ── Metabolism → Energy ────────────────────────────────────────────────

/// Convert microbial growth rate to energy availability modifier.
///
/// When gut microbiome is healthy (high growth rate under good conditions),
/// metabolic efficiency is high. Under stress (low rate), energy availability drops.
/// Uses jivanu's cardinal temperature model as an example input.
/// Returns energy modifier [0.5, 1.0].
///
/// ```
/// use bhava::microbiology::metabolic_efficiency;
///
/// let optimal = metabolic_efficiency(1.0);
/// let stressed = metabolic_efficiency(0.2);
/// assert!(optimal > stressed);
/// ```
#[cfg_attr(feature = "tracing", tracing::instrument(skip_all))]
#[must_use]
#[inline]
pub fn metabolic_efficiency(growth_rate_fraction: f32) -> f32 {
    // Growth fraction of max (0-1) maps to energy availability
    let g = growth_rate_fraction.clamp(0.0, 1.0);
    0.5 + g * 0.5
}

/// Convert temperature deviation from optimal to thermal discomfort.
///
/// Uses jivanu's cardinal temperature model to compute growth rate fraction,
/// then maps to discomfort. Returns stress input [0.0, 1.0].
/// Falls back to 0.5 (moderate discomfort) on error.
///
/// ```
/// use bhava::microbiology::temperature_stress;
///
/// // At optimal temperature, minimal stress
/// let optimal = temperature_stress(37.0, 15.0, 37.0, 45.0);
/// assert!(optimal < 0.1);
///
/// // At extreme temperature, high stress
/// let cold = temperature_stress(20.0, 15.0, 37.0, 45.0);
/// assert!(cold > optimal);
/// ```
#[cfg_attr(feature = "tracing", tracing::instrument(skip_all))]
#[must_use]
pub fn temperature_stress(temp_c: f64, t_min: f64, t_opt: f64, t_max: f64) -> f32 {
    let growth_fraction =
        jivanu::growth::cardinal_temperature(temp_c, t_min, t_opt, t_max).unwrap_or(0.0);
    // Invert: high growth = low stress, low growth = high stress
    (1.0 - growth_fraction.clamp(0.0, 1.0)) as f32
}

// ── Pharmacology → Cognition ───────────────────────────────────────────

/// Compute cognitive effect of a drug using the Emax model.
///
/// At low concentration, minimal effect. At EC50, half-maximal effect.
/// Returns a cognitive modifier [0.0, 1.0] where 1.0 = maximum drug effect.
/// Falls back to 0.0 on error.
///
/// ```
/// use bhava::microbiology::drug_cognitive_effect;
///
/// let sub_therapeutic = drug_cognitive_effect(0.5, 1.0, 5.0, 1.0);
/// let therapeutic = drug_cognitive_effect(5.0, 1.0, 5.0, 1.0);
/// assert!(therapeutic > sub_therapeutic);
/// ```
#[cfg_attr(feature = "tracing", tracing::instrument(skip_all))]
#[must_use]
pub fn drug_cognitive_effect(concentration: f64, e_max: f64, ec50: f64, hill_n: f64) -> f32 {
    let effect = jivanu::metabolism::emax_model(concentration, e_max, ec50, hill_n).unwrap_or(0.0);
    (effect as f32).clamp(0.0, 1.0)
}

/// Compute sedation level from drug concentration above therapeutic range.
///
/// Concentration below EC50 → minimal sedation. Above 2×EC50 → significant
/// drowsiness. Returns drowsiness [0.0, 1.0].
///
/// ```
/// use bhava::microbiology::drug_sedation;
///
/// assert!(drug_sedation(1.0, 5.0) < 0.2);
/// assert!(drug_sedation(15.0, 5.0) > 0.5);
/// ```
#[cfg_attr(feature = "tracing", tracing::instrument(skip_all))]
#[must_use]
#[inline]
pub fn drug_sedation(concentration: f64, ec50: f64) -> f32 {
    if ec50 <= 0.0 {
        return 0.0;
    }
    let ratio = (concentration / ec50).clamp(0.0, 10.0);
    // Sigmoid: ratio 1 → ~0.15, ratio 2 → ~0.4, ratio 5 → ~0.8
    let sedation = 1.0 - (-0.3 * (ratio - 0.5)).exp();
    (sedation as f32).clamp(0.0, 1.0)
}

// ── Tests ──────────────────────────────────────────────────────────────

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

    // ── Sickness Behavior ──────────────────────────────────────────────

    #[test]
    fn healthy_no_sickness() {
        let mood = sickness_mood(0.0);
        assert!(mood.frustration < 0.01);
        assert!(mood.joy.abs() < 0.01);
    }

    #[test]
    fn sick_depressed_mood() {
        let mood = sickness_mood(0.6);
        assert!(mood.joy < 0.0);
        assert!(mood.frustration > 0.2);
        assert!(mood.trust < 0.0);
        assert!(mood.interest < 0.0);
    }

    #[test]
    fn severity_zero_when_healthy() {
        assert!((sickness_severity(0.0, 0.0) - 0.0).abs() < 0.01);
    }

    #[test]
    fn severity_infected_worse_than_exposed() {
        let exposed_only = sickness_severity(0.5, 0.0);
        let infected_only = sickness_severity(0.0, 0.5);
        assert!(infected_only > exposed_only);
    }

    // ── Recovery ───────────────────────────────────────────────────────

    #[test]
    fn recovery_boosts_joy() {
        let early = recovery_mood_boost(0.1);
        let late = recovery_mood_boost(0.9);
        assert!(late.joy > early.joy);
        assert!(late.trust > early.trust);
    }

    #[test]
    fn immune_drain_at_rest() {
        assert!((immune_energy_drain(0.0) - 1.0).abs() < 0.01);
    }

    #[test]
    fn immune_drain_increases() {
        assert!(immune_energy_drain(0.5) > 1.5);
        assert!(immune_energy_drain(1.0) > 2.5);
    }

    // ── Social Behavior ────────────────────────────────────────────────

    #[test]
    fn low_r0_low_avoidance() {
        let pressure = contagion_avoidance(0.5, 0.3);
        assert!(pressure < 0.3);
    }

    #[test]
    fn high_r0_high_avoidance() {
        let pressure = contagion_avoidance(5.0, 0.2);
        assert!(pressure > 0.3);
    }

    #[test]
    fn high_coverage_more_safety() {
        let low = herd_safety(0.2, 3.0);
        let high = herd_safety(0.8, 3.0);
        assert!(high > low);
    }

    // ── Metabolism ──────────────────────────────────────────────────────

    #[test]
    fn optimal_growth_high_efficiency() {
        assert!(metabolic_efficiency(1.0) > 0.9);
    }

    #[test]
    fn stressed_growth_low_efficiency() {
        assert!(metabolic_efficiency(0.0) < 0.6);
    }

    #[test]
    fn optimal_temperature_low_stress() {
        let stress = temperature_stress(37.0, 15.0, 37.0, 45.0);
        assert!(stress < 0.1);
    }

    #[test]
    fn extreme_temperature_high_stress() {
        let cold = temperature_stress(18.0, 15.0, 37.0, 45.0);
        assert!(cold > 0.5);
    }

    // ── Pharmacology ───────────────────────────────────────────────────

    #[test]
    fn sub_therapeutic_low_effect() {
        let effect = drug_cognitive_effect(0.5, 1.0, 5.0, 1.0);
        assert!(effect < 0.2);
    }

    #[test]
    fn therapeutic_moderate_effect() {
        let effect = drug_cognitive_effect(5.0, 1.0, 5.0, 1.0);
        assert!(effect > 0.3 && effect < 0.7);
    }

    #[test]
    fn low_concentration_low_sedation() {
        assert!(drug_sedation(1.0, 5.0) < 0.3);
    }

    #[test]
    fn high_concentration_high_sedation() {
        assert!(drug_sedation(15.0, 5.0) > 0.5);
    }

    #[test]
    fn zero_ec50_no_sedation() {
        assert!((drug_sedation(5.0, 0.0) - 0.0).abs() < 0.01);
    }
}