muse2 2.1.0

A tool for running simulations of energy systems
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
//! Read and validate model parameters from `model.toml`.
//!
//! This module defines the `ModelParameters` struct and helpers for loading and validating the
//! `model.toml` configuration used by the model. Validation functions ensure sensible numeric
//! ranges and invariants for runtime use.
use crate::asset::check_capacity_valid_for_asset;
use crate::input::{
    deserialise_proportion_nonzero, input_err_msg, is_sorted_and_unique, read_toml,
};
use crate::units::{Capacity, Dimensionless, Flow, MoneyPerFlow};
use anyhow::{Context, Result, ensure};
use log::warn;
use serde::Deserialize;
use std::path::Path;
use std::sync::OnceLock;

const MODEL_PARAMETERS_FILE_NAME: &str = "model.toml";

/// The key in `model.toml` which enables potentially dangerous model options.
///
/// If this option is present and true, the model will permit certain experimental or unsafe
/// behaviours that are normally disallowed.
pub const ALLOW_DANGEROUS_OPTION_NAME: &str = "please_give_me_broken_results";

/// Global flag indicating whether potentially dangerous model options have been enabled.
///
/// This is stored in a `OnceLock` and must be set exactly once during startup (see
/// [`set_dangerous_model_options_flag`]).
static DANGEROUS_OPTIONS_ENABLED: OnceLock<bool> = OnceLock::new();

/// Whether potentially dangerous model options were enabled by the loaded config.
///
/// # Panics
///
/// Panics if the global flag has not been set yet (the flag should be set by
/// [`ModelParameters::from_path`] during program initialisation).
pub fn dangerous_model_options_enabled() -> bool {
    *DANGEROUS_OPTIONS_ENABLED
        .get()
        .expect("Dangerous options flag not set")
}

/// Set the global flag indicating whether potentially dangerous model options are enabled.
///
/// Can only be called once; subsequent calls will panic (except in tests, where it can be called
/// multiple times so long as the value is the same).
fn set_dangerous_model_options_flag(enabled: bool) {
    let result = DANGEROUS_OPTIONS_ENABLED.set(enabled);
    if result.is_err() {
        if cfg!(test) {
            // Sanity check
            assert_eq!(enabled, dangerous_model_options_enabled());
        } else {
            panic!("Attempted to set DANGEROUS_OPTIONS_ENABLED twice");
        }
    }
}

macro_rules! define_unit_param_default {
    ($name:ident, $type: ty, $value: expr) => {
        fn $name() -> $type {
            <$type>::new($value)
        }
    };
}

macro_rules! define_param_default {
    ($name:ident, $type: ty, $value: expr) => {
        fn $name() -> $type {
            $value
        }
    };
}

define_unit_param_default!(default_candidate_asset_capacity, Capacity, 0.0001);
define_unit_param_default!(default_capacity_limit_factor, Dimensionless, 0.1);
define_unit_param_default!(default_value_of_lost_load, MoneyPerFlow, 1e9);
define_unit_param_default!(default_price_tolerance, Dimensionless, 1e-6);
define_unit_param_default!(default_remaining_demand_absolute_tolerance, Flow, 1e-12);
define_param_default!(default_max_ironing_out_iterations, u32, 1);
define_param_default!(default_capacity_margin, f64, 0.2);
define_param_default!(default_mothball_years, u32, 0);

/// Model parameters as defined in the `model.toml` file.
///
/// NOTE: If you add or change a field in this struct, you must also update the schema in
/// `schemas/input/model.yaml`.
#[derive(Debug, Deserialize, PartialEq)]
pub struct ModelParameters {
    /// Milestone years
    pub milestone_years: Vec<u32>,
    /// Allow potentially dangerous options to be enabled.
    #[serde(default, rename = "please_give_me_broken_results")] // Can't use constant here :-(
    pub allow_dangerous_options: bool,
    /// The (small) value of capacity given to candidate assets.
    ///
    /// Don't change unless you know what you're doing.
    #[serde(default = "default_candidate_asset_capacity")]
    pub candidate_asset_capacity: Capacity,
    /// Affects the maximum capacity that can be given to a newly created asset.
    ///
    /// It is the proportion of maximum capacity that could be required across time slices.
    #[serde(default = "default_capacity_limit_factor")]
    #[serde(deserialize_with = "deserialise_proportion_nonzero")]
    pub capacity_limit_factor: Dimensionless,
    /// The cost applied to unmet demand.
    ///
    /// Currently this only applies to the LCOX appraisal.
    #[serde(default = "default_value_of_lost_load")]
    pub value_of_lost_load: MoneyPerFlow,
    /// The maximum number of iterations to run the "ironing out" step of agent investment for
    #[serde(default = "default_max_ironing_out_iterations")]
    pub max_ironing_out_iterations: u32,
    /// The relative tolerance for price convergence in the ironing out loop
    #[serde(default = "default_price_tolerance")]
    pub price_tolerance: Dimensionless,
    /// Slack applied during cycle balancing, allowing newly selected assets to flex their capacity
    /// by this proportion.
    ///
    /// Existing assets remain fixed; this gives newly selected assets the wiggle-room to absorb
    /// small demand changes before we would otherwise need to break for re-investment.
    #[serde(default = "default_capacity_margin")]
    pub capacity_margin: f64,
    /// Number of years an asset can remain unused before being decommissioned
    #[serde(default = "default_mothball_years")]
    pub mothball_years: u32,
    /// Absolute tolerance when checking if remaining demand is close enough to zero
    #[serde(default = "default_remaining_demand_absolute_tolerance")]
    pub remaining_demand_absolute_tolerance: Flow,
}

/// Check that the `milestone_years` parameter is valid
fn check_milestone_years(years: &[u32]) -> Result<()> {
    ensure!(!years.is_empty(), "`milestone_years` is empty");

    ensure!(
        is_sorted_and_unique(years),
        "`milestone_years` must be composed of unique values in order"
    );

    Ok(())
}

/// Check that the `value_of_lost_load` parameter is valid
fn check_value_of_lost_load(value: MoneyPerFlow) -> Result<()> {
    ensure!(
        value.is_finite() && value > MoneyPerFlow(0.0),
        "value_of_lost_load must be a finite number greater than zero"
    );

    Ok(())
}

/// Check that the `max_ironing_out_iterations` parameter is valid
fn check_max_ironing_out_iterations(value: u32) -> Result<()> {
    ensure!(value > 0, "max_ironing_out_iterations cannot be zero");

    Ok(())
}

/// Check the `price_tolerance` parameter is valid
fn check_price_tolerance(value: Dimensionless) -> Result<()> {
    ensure!(
        value.is_finite() && value >= Dimensionless(0.0),
        "price_tolerance must be a finite number greater than or equal to zero"
    );

    Ok(())
}

fn check_remaining_demand_absolute_tolerance(
    dangerous_options_enabled: bool,
    value: Flow,
) -> Result<()> {
    ensure!(
        value.is_finite() && value >= Flow(0.0),
        "remaining_demand_absolute_tolerance must be a finite number greater than or equal to zero"
    );

    let default_value = default_remaining_demand_absolute_tolerance();
    if !dangerous_options_enabled {
        ensure!(
            value == default_value,
            "Setting a remaining_demand_absolute_tolerance different from the default value of \
            {:e} is potentially dangerous, set {ALLOW_DANGEROUS_OPTION_NAME} to true if you want \
            to allow this.",
            default_value.0
        );
    }

    Ok(())
}

/// Check that the `capacity_margin` parameter is valid
fn check_capacity_margin(value: f64) -> Result<()> {
    ensure!(
        value.is_finite() && value >= 0.0,
        "capacity_margin must be a finite number greater than or equal to zero"
    );

    Ok(())
}

impl ModelParameters {
    /// Read a model file from the specified directory.
    ///
    /// # Arguments
    ///
    /// * `model_dir` - Folder containing model configuration files
    ///
    /// # Returns
    ///
    /// The model file contents as a [`ModelParameters`] struct or an error if the file is invalid
    pub fn from_path<P: AsRef<Path>>(model_dir: P) -> Result<ModelParameters> {
        let file_path = model_dir.as_ref().join(MODEL_PARAMETERS_FILE_NAME);
        let model_params: ModelParameters = read_toml(&file_path)?;

        set_dangerous_model_options_flag(model_params.allow_dangerous_options);

        model_params
            .validate()
            .with_context(|| input_err_msg(file_path))?;

        Ok(model_params)
    }

    /// Validate parameters after reading in file
    fn validate(&self) -> Result<()> {
        if self.allow_dangerous_options {
            warn!(
                "!!! You've enabled the {ALLOW_DANGEROUS_OPTION_NAME} option. !!!\n\
                I see you like to live dangerously 😈. This option should ONLY be used by \
                developers as it can cause peculiar behaviour that breaks things. NEVER enable it \
                for results you actually care about or want to publish. You have been warned!"
            );
        }

        // milestone_years
        check_milestone_years(&self.milestone_years)?;

        // capacity_limit_factor already validated with deserialise_proportion_nonzero

        // candidate_asset_capacity
        check_capacity_valid_for_asset(self.candidate_asset_capacity)
            .context("Invalid value for candidate_asset_capacity")?;

        // value_of_lost_load
        check_value_of_lost_load(self.value_of_lost_load)?;

        // max_ironing_out_iterations
        check_max_ironing_out_iterations(self.max_ironing_out_iterations)?;

        // price_tolerance
        check_price_tolerance(self.price_tolerance)?;

        // capacity_margin
        check_capacity_margin(self.capacity_margin)?;

        // remaining_demand_absolute_tolerance
        check_remaining_demand_absolute_tolerance(
            self.allow_dangerous_options,
            self.remaining_demand_absolute_tolerance,
        )?;

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use rstest::rstest;
    use std::fmt::Display;
    use std::fs::File;
    use std::io::Write;
    use tempfile::tempdir;

    /// Helper function to assert validation result based on expected validity
    fn assert_validation_result<T, U: Display>(
        result: Result<T>,
        expected_valid: bool,
        value: U,
        expected_error_fragment: &str,
    ) {
        if expected_valid {
            assert!(
                result.is_ok(),
                "Expected value {} to be valid, but got error: {:?}",
                value,
                result.err()
            );
        } else {
            assert!(
                result.is_err(),
                "Expected value {value} to be invalid, but it was accepted",
            );
            let error_message = result.err().unwrap().to_string();
            assert!(
                error_message.contains(expected_error_fragment),
                "Error message should mention the validation constraint, got: {error_message}",
            );
        }
    }

    #[test]
    fn check_milestone_years_works() {
        // Valid
        check_milestone_years(&[1]).unwrap();
        check_milestone_years(&[1, 2]).unwrap();

        // Invalid
        assert!(check_milestone_years(&[]).is_err());
        assert!(check_milestone_years(&[1, 1]).is_err());
        assert!(check_milestone_years(&[2, 1]).is_err());
    }

    #[test]
    fn model_params_from_path() {
        let dir = tempdir().unwrap();
        {
            let mut file = File::create(dir.path().join(MODEL_PARAMETERS_FILE_NAME)).unwrap();
            writeln!(file, "milestone_years = [2020, 2100]").unwrap();
        }

        let model_params = ModelParameters::from_path(dir.path()).unwrap();
        assert_eq!(model_params.milestone_years, [2020, 2100]);
    }

    #[rstest]
    #[case(1.0, true)] // Valid positive value
    #[case(1e-10, true)] // Valid very small positive value
    #[case(1e9, true)] // Valid large value (default)
    #[case(f64::MAX, true)] // Valid maximum finite value
    #[case(0.0, false)] // Invalid: exactly zero
    #[case(-1.0, false)] // Invalid: negative value
    #[case(-1e-10, false)] // Invalid: very small negative value
    #[case(f64::INFINITY, false)] // Invalid: infinite value
    #[case(f64::NEG_INFINITY, false)] // Invalid: negative infinite value
    #[case(f64::NAN, false)] // Invalid: NaN value
    fn check_value_of_lost_load_works(#[case] value: f64, #[case] expected_valid: bool) {
        let money_per_flow = MoneyPerFlow::new(value);
        let result = check_value_of_lost_load(money_per_flow);

        assert_validation_result(
            result,
            expected_valid,
            value,
            "value_of_lost_load must be a finite number greater than zero",
        );
    }

    #[rstest]
    #[case(1, true)] // Valid minimum value
    #[case(10, true)] // Valid default value
    #[case(100, true)] // Valid large value
    #[case(u32::MAX, true)] // Valid maximum value
    #[case(0, false)] // Invalid: zero
    fn check_max_ironing_out_iterations_works(#[case] value: u32, #[case] expected_valid: bool) {
        let result = check_max_ironing_out_iterations(value);

        assert_validation_result(
            result,
            expected_valid,
            value,
            "max_ironing_out_iterations cannot be zero",
        );
    }

    #[rstest]
    #[case(0.0, true)] // Valid minimum value (exactly zero)
    #[case(1e-10, true)] // Valid very small positive value
    #[case(1e-6, true)] // Valid default value
    #[case(1.0, true)] // Valid larger value
    #[case(f64::MAX, true)] // Valid maximum finite value
    #[case(-1e-10, false)] // Invalid: negative value
    #[case(-1.0, false)] // Invalid: negative value
    #[case(f64::INFINITY, false)] // Invalid: infinite value
    #[case(f64::NEG_INFINITY, false)] // Invalid: negative infinite value
    #[case(f64::NAN, false)] // Invalid: NaN value
    fn check_price_tolerance_works(#[case] value: f64, #[case] expected_valid: bool) {
        let dimensionless = Dimensionless::new(value);
        let result = check_price_tolerance(dimensionless);

        assert_validation_result(
            result,
            expected_valid,
            value,
            "price_tolerance must be a finite number greater than or equal to zero",
        );
    }

    #[rstest]
    #[case(true, 0.0, true)] // Valid minimum value dangerous options allowed
    #[case(true, 1e-10, true)] // Valid value with dangerous options allowed
    #[case(true, 1e-15, true)] // Valid value with dangerous options allowed
    #[case(false, 1e-12, true)] // Valid value same as default, no dangerous options needed
    #[case(true, 1.0, true)] // Valid larger value with dangerous options allowed
    #[case(true, f64::MAX, true)] // Valid maximum finite value with dangerous options allowed
    #[case(true, -1e-10, false)] // Invalid: negative value
    #[case(true, f64::INFINITY, false)] // Invalid: positive infinity
    #[case(true, f64::NEG_INFINITY, false)] // Invalid: negative infinity
    #[case(true, f64::NAN, false)] // Invalid: NaN
    #[case(false, -1e-10, false)] // Invalid: negative value
    #[case(false, f64::INFINITY, false)] // Invalid: positive infinity
    #[case(false, f64::NEG_INFINITY, false)] // Invalid: negative infinity
    #[case(false, f64::NAN, false)] // Invalid: NaN
    fn check_remaining_demand_absolute_tolerance_works(
        #[case] allow_dangerous_options: bool,
        #[case] value: f64,
        #[case] expected_valid: bool,
    ) {
        let flow = Flow::new(value);
        let result = check_remaining_demand_absolute_tolerance(allow_dangerous_options, flow);

        assert_validation_result(
            result,
            expected_valid,
            value,
            "remaining_demand_absolute_tolerance must be a finite number greater than or equal to zero",
        );
    }

    #[rstest]
    #[case(0.0)] // smaller than default
    #[case(1e-10)] // Larger than default (1e-12)
    #[case(1.0)] // Well above default
    #[case(f64::MAX)] // Maximum finite value
    fn check_remaining_demand_absolute_tolerance_requires_dangerous_options_if_non_default(
        #[case] value: f64,
    ) {
        let flow = Flow::new(value);
        let result = check_remaining_demand_absolute_tolerance(false, flow);
        assert_validation_result(
            result,
            false,
            value,
            "Setting a remaining_demand_absolute_tolerance different from the default value of \
            1e-12 is potentially dangerous, set please_give_me_broken_results to true if you want \
            to allow this.",
        );
    }

    #[rstest]
    #[case(0.0, true)] // Valid minimum value
    #[case(0.2, true)] // Valid default value
    #[case(10.0, true)] // Valid large value
    #[case(-1e-6, false)] // Invalid: negative margin
    #[case(f64::INFINITY, false)] // Invalid: infinite value
    #[case(f64::NEG_INFINITY, false)] // Invalid: negative infinite value
    #[case(f64::NAN, false)] // Invalid: NaN value
    fn check_capacity_margin_works(#[case] value: f64, #[case] expected_valid: bool) {
        let result = check_capacity_margin(value);

        assert_validation_result(
            result,
            expected_valid,
            value,
            "capacity_margin must be a finite number greater than or equal to zero",
        );
    }
}