khanij 1.1.0

Khanij — geology and mineralogy engine for crystal structures, rock cycles, soil, and mineral properties
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
use hisab::calc;
use serde::{Deserialize, Serialize};

/// Ore deposit type.
///
/// # Examples
///
/// ```
/// # use khanij::*;
/// let dt = DepositType::Porphyry;
/// assert_eq!(dt, DepositType::Porphyry);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub enum DepositType {
    Vein,
    Placer,
    Massive,
    Disseminated,
    Skarn,
    Porphyry,
}

/// Resource confidence classification (JORC/CIM/NI 43-101 style).
///
/// # Examples
///
/// ```
/// # use khanij::*;
/// assert!(ResourceCategory::Inferred < ResourceCategory::Measured);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[non_exhaustive]
pub enum ResourceCategory {
    /// Lowest confidence — inferred from limited data.
    Inferred,
    /// Moderate confidence — reasonably estimated from adequate data.
    Indicated,
    /// Highest confidence — estimated with high level of confidence.
    Measured,
}

/// An ore deposit with validated fields.
///
/// # Examples
///
/// ```
/// # use khanij::*;
/// let d = OreDeposit::new("Gold", DepositType::Vein, 0.01, 200.0, 50_000.0);
/// assert!(d.is_some());
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OreDeposit {
    pub mineral: String,
    pub deposit_type: DepositType,
    pub grade: f64,   // fraction (0.0-1.0) of target mineral
    pub depth_m: f64, // positive depth in metres
    pub tonnage: f64, // metric tonnes (positive)
}

impl OreDeposit {
    /// Create a new ore deposit with validated fields.
    /// Returns `None` if grade is not in `[0.0, 1.0]`, depth is not positive,
    /// or tonnage is not positive.
    ///
    /// # Examples
    ///
    /// ```
    /// # use khanij::*;
    /// let valid = OreDeposit::new("Cu", DepositType::Porphyry, 0.02, 500.0, 1e5);
    /// assert!(valid.is_some());
    /// let invalid = OreDeposit::new("Cu", DepositType::Porphyry, -0.1, 500.0, 1e5);
    /// assert!(invalid.is_none());
    /// ```
    #[must_use]
    pub fn new(
        mineral: impl Into<String>,
        deposit_type: DepositType,
        grade: f64,
        depth_m: f64,
        tonnage: f64,
    ) -> Option<Self> {
        if !(0.0..=1.0).contains(&grade) || depth_m <= 0.0 || tonnage <= 0.0 {
            return None;
        }
        Some(Self {
            mineral: mineral.into(),
            deposit_type,
            grade,
            depth_m,
            tonnage,
        })
    }

    /// Contained metal in tonnes (grade × tonnage).
    ///
    /// # Examples
    ///
    /// ```
    /// # use khanij::*;
    /// let d = OreDeposit::new("Cu", DepositType::Porphyry, 0.05, 100.0, 1_000_000.0).unwrap();
    /// assert!((d.contained_metal() - 50_000.0).abs() < 1.0);
    /// ```
    #[must_use]
    pub fn contained_metal(&self) -> f64 {
        self.grade * self.tonnage
    }

    /// Revenue estimate at a given metal price (contained metal × price).
    ///
    /// # Examples
    ///
    /// ```
    /// # use khanij::*;
    /// let d = OreDeposit::new("Au", DepositType::Vein, 0.001, 200.0, 1e6).unwrap();
    /// let rev = d.gross_revenue(60_000_000.0);
    /// assert!(rev > 0.0);
    /// ```
    #[must_use]
    pub fn gross_revenue(&self, price_per_tonne: f64) -> f64 {
        self.contained_metal() * price_per_tonne
    }

    /// Stripping ratio estimate: depth-based proxy for open-pit vs underground.
    /// Returns approximate waste-to-ore ratio. Higher depth → higher ratio.
    ///
    /// # Examples
    ///
    /// ```
    /// # use khanij::*;
    /// let d = OreDeposit::new("Cu", DepositType::Porphyry, 0.02, 500.0, 1e5).unwrap();
    /// assert!(d.stripping_ratio() > 1.0);
    /// ```
    #[must_use]
    pub fn stripping_ratio(&self) -> f64 {
        // Simple model: ratio increases linearly with depth
        // Shallow deposits (<50m) are ~2:1, deep (500m+) approach 10:1+
        (self.depth_m / 50.0).max(1.0)
    }
}

/// A tonnage-grade data point for building curves.
///
/// # Examples
///
/// ```
/// # use khanij::*;
/// let pt = TonnageGradePoint {
///     cutoff_grade: 0.01,
///     tonnage_above_cutoff: 5000.0,
///     average_grade_above_cutoff: 0.025,
/// };
/// assert!(pt.tonnage_above_cutoff > 0.0);
/// ```
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct TonnageGradePoint {
    pub cutoff_grade: f64,
    pub tonnage_above_cutoff: f64,
    pub average_grade_above_cutoff: f64,
}

/// Build a tonnage-grade curve from a set of block grades.
///
/// Given a list of `(tonnage, grade)` blocks, computes at each cutoff grade
/// the total tonnage and average grade above that cutoff.
///
/// - `blocks`: slice of `(tonnage, grade)` pairs
/// - `cutoff_steps`: number of cutoff grades to evaluate between min and max grade
///
/// Returns a sorted vector of [`TonnageGradePoint`]s from lowest to highest cutoff.
///
/// # Examples
///
/// ```
/// # use khanij::*;
/// let blocks = vec![(1000.0, 0.01), (2000.0, 0.02), (500.0, 0.05)];
/// let curve = tonnage_grade_curve(&blocks, 4);
/// assert!(!curve.is_empty());
/// assert!(curve[0].tonnage_above_cutoff >= curve.last().unwrap().tonnage_above_cutoff);
/// ```
#[must_use]
pub fn tonnage_grade_curve(blocks: &[(f64, f64)], cutoff_steps: usize) -> Vec<TonnageGradePoint> {
    if blocks.is_empty() || cutoff_steps == 0 {
        return Vec::new();
    }

    let min_grade = blocks.iter().map(|b| b.1).fold(f64::INFINITY, f64::min);
    let max_grade = blocks.iter().map(|b| b.1).fold(f64::NEG_INFINITY, f64::max);

    if (max_grade - min_grade).abs() < f64::EPSILON {
        let total_tonnage: f64 = blocks.iter().map(|b| b.0).sum();
        return vec![TonnageGradePoint {
            cutoff_grade: min_grade,
            tonnage_above_cutoff: total_tonnage,
            average_grade_above_cutoff: min_grade,
        }];
    }

    let step = (max_grade - min_grade) / cutoff_steps as f64;
    (0..=cutoff_steps)
        .map(|i| {
            let cutoff = min_grade + step * i as f64;
            let mut total_tonnage = 0.0;
            let mut weighted_grade = 0.0;
            for &(t, g) in blocks {
                if g >= cutoff {
                    total_tonnage += t;
                    weighted_grade += t * g;
                }
            }
            let avg = if total_tonnage > 0.0 {
                weighted_grade / total_tonnage
            } else {
                0.0
            };
            TonnageGradePoint {
                cutoff_grade: cutoff,
                tonnage_above_cutoff: total_tonnage,
                average_grade_above_cutoff: avg,
            }
        })
        .collect()
}

/// Cutoff grade: the minimum grade at which mining is profitable.
///
/// Solves for the grade where `grade × price × recovery = cost_per_tonne`.
///
/// - `price_per_tonne`: metal price per tonne of pure metal
/// - `cost_per_tonne`: total mining + processing cost per tonne of ore
/// - `recovery`: metallurgical recovery fraction (0.0-1.0, typically 0.7-0.95)
///
/// Returns the cutoff grade as a fraction.
///
/// # Examples
///
/// ```
/// # use khanij::*;
/// let cog = cutoff_grade(60_000_000.0, 50.0, 0.90).unwrap();
/// assert!(cog > 0.0 && cog < 0.001);
/// ```
#[must_use]
pub fn cutoff_grade(price_per_tonne: f64, cost_per_tonne: f64, recovery: f64) -> Option<f64> {
    if price_per_tonne <= 0.0 || recovery <= 0.0 || recovery > 1.0 {
        return None;
    }
    let cog = cost_per_tonne / (price_per_tonne * recovery);
    if cog > 1.0 {
        None // uneconomic at any grade
    } else {
        Some(cog)
    }
}

/// Economic viability check using hisab numerical integration to model
/// decreasing marginal return over the deposit's tonnage.
///
/// Integrates `grade * price_per_tonne` over the tonnage range and compares
/// against extraction cost.
///
/// # Examples
///
/// ```
/// # use khanij::*;
/// assert!(is_economically_viable(0.05, 1_000_000.0, 5000.0, 100_000_000.0));
/// ```
#[must_use]
pub fn is_economically_viable(
    grade: f64,
    tonnage: f64,
    price_per_tonne: f64,
    extraction_cost: f64,
) -> bool {
    let g = grade;
    // Integrate revenue over tonnage with a diminishing-return factor:
    // as you mine more, later tonnes are slightly harder to extract.
    let revenue = calc::integral_simpson(
        |t| g * price_per_tonne * (-0.0001 * t / tonnage).exp(),
        0.0,
        tonnage,
        50,
    )
    .unwrap_or(0.0);
    revenue > extraction_cost
}

/// Net present value of a deposit mined over `years` at a given discount rate.
///
/// - `annual_revenue`: expected yearly revenue
/// - `annual_cost`: expected yearly operating cost
/// - `discount_rate`: annual discount rate (e.g., 0.08 for 8%)
/// - `years`: mine life in years
///
/// Uses hisab integration over continuous discounting.
///
/// # Examples
///
/// ```
/// # use khanij::*;
/// let npv = net_present_value(10_000_000.0, 7_000_000.0, 0.08, 10.0).unwrap();
/// assert!(npv > 0.0);
/// ```
#[must_use]
pub fn net_present_value(
    annual_revenue: f64,
    annual_cost: f64,
    discount_rate: f64,
    years: f64,
) -> Option<f64> {
    if discount_rate < 0.0 || years <= 0.0 {
        return None;
    }
    let net_annual = annual_revenue - annual_cost;
    // NPV = ∫₀ᵗ net_annual · e^(-r·t) dt
    calc::integral_simpson(|t| net_annual * (-discount_rate * t).exp(), 0.0, years, 50).ok()
}

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

    #[test]
    fn high_grade_viable() {
        assert!(is_economically_viable(
            0.05,
            1_000_000.0,
            5000.0,
            100_000_000.0
        ));
    }

    #[test]
    fn low_grade_not_viable() {
        assert!(!is_economically_viable(0.001, 1000.0, 100.0, 1_000_000.0));
    }

    #[test]
    fn deposit_serde() {
        let d = OreDeposit::new("Gold", DepositType::Vein, 0.01, 200.0, 50_000.0).unwrap();
        let json = serde_json::to_string(&d).unwrap();
        let back: OreDeposit = serde_json::from_str(&json).unwrap();
        assert_eq!(back.mineral, "Gold");
    }

    #[test]
    fn validated_deposit_rejects_invalid_grade() {
        assert!(OreDeposit::new("Iron", DepositType::Massive, -0.1, 100.0, 1000.0).is_none());
        assert!(OreDeposit::new("Iron", DepositType::Massive, 1.5, 100.0, 1000.0).is_none());
    }

    #[test]
    fn validated_deposit_rejects_invalid_depth() {
        assert!(OreDeposit::new("Iron", DepositType::Massive, 0.5, -10.0, 1000.0).is_none());
        assert!(OreDeposit::new("Iron", DepositType::Massive, 0.5, 0.0, 1000.0).is_none());
    }

    #[test]
    fn validated_deposit_rejects_invalid_tonnage() {
        assert!(OreDeposit::new("Iron", DepositType::Massive, 0.5, 100.0, -500.0).is_none());
        assert!(OreDeposit::new("Iron", DepositType::Massive, 0.5, 100.0, 0.0).is_none());
    }

    #[test]
    fn valid_deposit_created() {
        let d = OreDeposit::new("Copper", DepositType::Porphyry, 0.02, 500.0, 100_000.0);
        assert!(d.is_some());
    }

    #[test]
    fn contained_metal() {
        let d = OreDeposit::new("Cu", DepositType::Porphyry, 0.05, 100.0, 1_000_000.0).unwrap();
        assert!((d.contained_metal() - 50_000.0).abs() < 1.0);
    }

    #[test]
    fn cutoff_grade_gold() {
        // Gold at $60M/t, mining cost $50/t ore, 90% recovery
        let cog = cutoff_grade(60_000_000.0, 50.0, 0.90).unwrap();
        // cog = 50 / (60_000_000 * 0.9) ≈ 9.26e-7
        assert!(cog > 0.0 && cog < 0.001);
    }

    #[test]
    fn cutoff_grade_uneconomic() {
        // Price too low to cover costs at any grade
        assert!(cutoff_grade(10.0, 100.0, 0.90).is_none());
    }

    #[test]
    fn tonnage_grade_curve_basic() {
        let blocks = vec![
            (1000.0, 0.01),
            (2000.0, 0.02),
            (1500.0, 0.03),
            (500.0, 0.05),
        ];
        let curve = tonnage_grade_curve(&blocks, 4);
        assert_eq!(curve.len(), 5); // 0..=4 cutoff steps
        // At lowest cutoff, all tonnage included
        assert!((curve[0].tonnage_above_cutoff - 5000.0).abs() < 1.0);
        // Tonnage decreases as cutoff increases
        for pair in curve.windows(2) {
            assert!(pair[1].tonnage_above_cutoff <= pair[0].tonnage_above_cutoff);
        }
        // Average grade above cutoff increases as cutoff increases
        for pair in curve.windows(2) {
            if pair[1].tonnage_above_cutoff > 0.0 {
                assert!(pair[1].average_grade_above_cutoff >= pair[0].average_grade_above_cutoff);
            }
        }
    }

    #[test]
    fn tonnage_grade_empty() {
        assert!(tonnage_grade_curve(&[], 10).is_empty());
    }

    #[test]
    fn npv_positive_project() {
        let npv = net_present_value(10_000_000.0, 7_000_000.0, 0.08, 10.0).unwrap();
        assert!(npv > 0.0); // net $3M/yr for 10 years at 8% discount
    }

    #[test]
    fn npv_negative_project() {
        let npv = net_present_value(5_000_000.0, 8_000_000.0, 0.10, 5.0).unwrap();
        assert!(npv < 0.0); // losing $3M/yr
    }

    #[test]
    fn resource_category_ordering() {
        assert!(ResourceCategory::Inferred < ResourceCategory::Indicated);
        assert!(ResourceCategory::Indicated < ResourceCategory::Measured);
    }

    #[test]
    fn stripping_ratio_increases_with_depth() {
        let shallow = OreDeposit::new("Cu", DepositType::Porphyry, 0.02, 50.0, 100_000.0).unwrap();
        let deep = OreDeposit::new("Cu", DepositType::Porphyry, 0.02, 500.0, 100_000.0).unwrap();
        assert!(deep.stripping_ratio() > shallow.stripping_ratio());
    }

    #[test]
    fn gross_revenue() {
        let d = OreDeposit::new("Au", DepositType::Vein, 0.001, 200.0, 1_000_000.0).unwrap();
        let rev = d.gross_revenue(60_000_000.0); // gold ~$60M/t
        // 1000t contained * $60M = $60B
        assert!((rev - 60_000_000_000.0).abs() < 1_000_000.0);
    }
}