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
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
//! Processes are used for converting between different commodities. The data structures in this
//! module are used to represent these conversions along with the associated costs.
use crate::commodity::{Commodity, CommodityID};
use crate::id::define_id_type;
use crate::region::RegionID;
use crate::time_slice::{Season, TimeSliceID, TimeSliceInfo, TimeSliceSelection};
use crate::units::{
    ActivityPerCapacity, Capacity, Dimensionless, FlowPerActivity, MoneyPerActivity,
    MoneyPerCapacity, MoneyPerCapacityPerYear, MoneyPerFlow,
};
use anyhow::{Result, ensure};
use indexmap::{IndexMap, IndexSet};
use itertools::Itertools;
use serde_string_enum::DeserializeLabeledStringEnum;
use std::collections::HashMap;
use std::ops::RangeInclusive;
use std::rc::Rc;

define_id_type! {ProcessID}

/// A map of [`Process`]es, keyed by process ID
pub type ProcessMap = IndexMap<ProcessID, Rc<Process>>;

/// A map indicating activity limits for a [`Process`] throughout the year.
pub type ProcessActivityLimitsMap = HashMap<(RegionID, u32), Rc<ActivityLimits>>;

/// A map of [`ProcessParameter`]s, keyed by region and year
pub type ProcessParameterMap = HashMap<(RegionID, u32), Rc<ProcessParameter>>;

/// A map of process flows, keyed by region and year.
///
/// The value is actually a map itself, keyed by commodity ID.
pub type ProcessFlowsMap = HashMap<(RegionID, u32), Rc<IndexMap<CommodityID, ProcessFlow>>>;

/// Map of process investment constraints, keyed by region and year
pub type ProcessInvestmentConstraintsMap =
    HashMap<(RegionID, u32), Rc<ProcessInvestmentConstraint>>;

/// Represents a process within the simulation
#[derive(PartialEq, Debug)]
pub struct Process {
    /// A unique identifier for the process (e.g. GASDRV)
    pub id: ProcessID,
    /// A human-readable description for the process (e.g. dry gas extraction)
    pub description: String,
    /// The years in which this process is available for investment.
    pub years: RangeInclusive<u32>,
    /// Limits on activity for each time slice (as a fraction of maximum)
    pub activity_limits: ProcessActivityLimitsMap,
    /// Maximum annual commodity flows for this process
    pub flows: ProcessFlowsMap,
    /// Additional parameters for this process
    pub parameters: ProcessParameterMap,
    /// The regions in which this process can operate
    pub regions: IndexSet<RegionID>,
    /// The primary output for this process, if any
    pub primary_output: Option<CommodityID>,
    /// Factor for calculating the maximum consumption/production over a year.
    ///
    /// Used for converting one unit of capacity to maximum energy of asset per year. For example,
    /// if capacity is measured in GW and energy is measured in PJ, the `capacity_to_activity` for the
    /// process is 31.536 because 1 GW of capacity can produce 31.536 PJ energy output in a year.
    pub capacity_to_activity: ActivityPerCapacity,
    /// Investment constraints for this process
    pub investment_constraints: ProcessInvestmentConstraintsMap,
    /// Capacity of the units in which an asset for this process will be divided into when commissioned, if any.
    ///
    /// By default, an asset will not be divided when commissioned (`unit_size` will be None), but
    /// if this is set, then it will be divided in as many assets as needed to commission the total
    /// capacity, each having a `unit_size` capacity or a fraction of it.
    pub unit_size: Option<Capacity>,
}

impl Process {
    /// Whether the process can be commissioned in a given year
    pub fn active_for_year(&self, year: u32) -> bool {
        self.years.contains(&year)
    }
}

/// Defines the activity limits for a process in a given region and year
///
/// Activity limits represent the minimum and maximum fraction of the potential annual activity that
/// can be undertaken in each time slice, season, or the year as a whole. The limits stored and
/// returned by this struct are dimensionless; to convert to actual activity limits for an asset,
/// multiply by the capacity to activity factor of the process and the installed capacity of the
/// asset. In other words, the limits stored and returned by this struct are the absolute limits on
/// activity per `1/capacity_to_activity` units of capacity.
///
/// All time slices must have an entry in `self.time_slice_limits`. If no specific availability limit
/// is provided for a time slice, this will just represent the length of the time slice as a
/// fraction of the year. Seasonal and annual limits may be stored if provided by the user, and only
/// if they provide an extra level of constraint on top of the time slice limits.
///
/// Limits can be retrieved in three ways:
/// - Retrieve the limit for a specific time slice using `get_limit_for_time_slice()`.
/// - Retrieve a limit for a specific time slice selection (time slice, season, or annual)
///   using `get_limit()`.
/// - Retrieve all limits as an iterator using `iter_limits()`. Note: individual
///   limits within this iterator cannot be relied upon, but the totality of these limits can be
///   used to construct a set of constraints that ensures that all limits are respected.
#[derive(PartialEq, Debug, Clone)]
pub struct ActivityLimits {
    /// Optional annual limit
    annual_limit: Option<RangeInclusive<Dimensionless>>,
    /// Optional limits for each season
    seasonal_limits: IndexMap<Season, RangeInclusive<Dimensionless>>,
    /// Limits for each time slice (mandatory for all time slices)
    time_slice_limits: IndexMap<TimeSliceID, RangeInclusive<Dimensionless>>,
}

impl ActivityLimits {
    /// Create a new `ActivityLimits` with full availability for all time slices
    pub fn new_with_full_availability(time_slice_info: &TimeSliceInfo) -> Self {
        // Initialize time slice limits to full availability
        let mut ts_limits = IndexMap::new();
        for (ts_id, ts_length) in time_slice_info.iter() {
            ts_limits.insert(
                ts_id.clone(),
                Dimensionless(0.0)..=Dimensionless(ts_length.value()),
            );
        }

        ActivityLimits {
            annual_limit: None,
            seasonal_limits: IndexMap::new(),
            time_slice_limits: ts_limits,
        }
    }

    /// Create a new `ActivityLimits` from a map of limits for time slice selections
    ///
    /// The limits provided here may be for individual time slices, seasons, or the entire year.
    /// Provided limits must reflect the fraction of potential annual activity available in the
    /// given time slice selection. In other words, these are the absolute limits on activity per
    /// `1/capacity_to_activity` units of capacity.
    ///
    /// It is not mandatory to provide any limits; if no limits are provided, full availability
    /// will be assumed for all time slices (limited only by time slice lengths). However, if
    /// limits are provided for any individual time slices, they must be provided for ALL time
    /// slices. Similarly, if limits are provided for any seasons, they must be provided for ALL
    /// seasons.
    ///
    /// No calculations are done here to account for time slice lengths; this must be handled by the
    /// user when providing the limits. For example, a limit of 0..=0.1 for a time slice indicates
    /// that 10% of the potential annual activity can be undertaken in that time slice. If the time
    /// slice is 10% of the year, then this provides no additional constraint.
    ///
    /// Checks are done to ensure that provided limits are compatible with each other. For example,
    /// a limit of "..0.01" for "winter" would be incompatible with a limit of "0.2.." for
    /// "winter.day" (i.e. if activity must be >0.2 in "winter.night", then if cannot possibly be
    /// ≤0.01 in winter as a whole).
    pub fn new_from_limits(
        limits: &HashMap<TimeSliceSelection, RangeInclusive<Dimensionless>>,
        time_slice_info: &TimeSliceInfo,
    ) -> Result<Self> {
        let mut result = ActivityLimits::new_with_full_availability(time_slice_info);

        // Add time slice limits first
        let mut time_slices_added = IndexSet::new();
        for (ts_selection, limit) in limits {
            if let TimeSliceSelection::Single(ts_id) = ts_selection {
                result.add_time_slice_limit(ts_id.clone(), limit.clone());
                time_slices_added.insert(ts_id.clone());
            }
        }

        // Check that limits have been added for all or no time slices
        if !time_slices_added.is_empty() {
            let missing = time_slice_info
                .iter_ids()
                .filter(|ts_id| !time_slices_added.contains(*ts_id))
                .collect::<Vec<_>>();
            ensure!(
                missing.is_empty(),
                "Missing availability limits for time slices: [{}]. Please provide",
                missing.iter().join(", ")
            );
        }

        // Then add seasonal limits
        // Error will be raised if seasonal limits are incompatible with time slice limits
        let mut seasons_added = IndexSet::new();
        for (ts_selection, limit) in limits {
            if let TimeSliceSelection::Season(season) = ts_selection {
                result.add_seasonal_limit(season.clone(), limit.clone())?;
                seasons_added.insert(season.clone());
            }
        }

        // Check that limits have been added for all or no seasons
        if !seasons_added.is_empty() {
            let missing = time_slice_info
                .iter_seasons()
                .filter(|season| !seasons_added.contains(*season))
                .collect::<Vec<_>>();
            ensure!(
                missing.is_empty(),
                "Missing availability limits for seasons: [{}]. Please provide",
                missing.iter().join(", "),
            );
        }

        // Then add annual limit
        // Error will be raised if annual limit is incompatible with time slice/seasonal limits
        if let Some(limit) = limits.get(&TimeSliceSelection::Annual) {
            result.add_annual_limit(limit.clone())?;
        }

        Ok(result)
    }

    /// Add a limit for a specific time slice
    pub fn add_time_slice_limit(
        &mut self,
        ts_id: TimeSliceID,
        limit: RangeInclusive<Dimensionless>,
    ) {
        self.time_slice_limits.insert(ts_id, limit);
    }

    /// Add a limit for a specific season
    fn add_seasonal_limit(
        &mut self,
        season: Season,
        limit: RangeInclusive<Dimensionless>,
    ) -> Result<()> {
        // Get current limit for the season
        let current_limit = self.get_limit_for_season(&season);

        // Ensure that the new limit overlaps with the current limit
        // If not, it's impossible to satisfy both limits, so we must exit with an error
        ensure!(
            *limit.start() <= *current_limit.end() && *limit.end() >= *current_limit.start(),
            "Availability limit for season {season} clashes with time slice limits",
        );

        // Only insert the seasonal limit if it provides an extra level of constraint above the
        // existing time slice limits. This is to minimize the number of seasonal limits stored and
        // returned by `iter_limits()`, therefore preventing unnecessary constraints from being
        // added to the optimization model.
        if *limit.start() > *current_limit.start() || *limit.end() < *current_limit.end() {
            self.seasonal_limits.insert(season, limit);
        }

        Ok(())
    }

    /// Add an annual limit
    fn add_annual_limit(&mut self, limit: RangeInclusive<Dimensionless>) -> Result<()> {
        // Get current limit for the year
        let current_limit = self.get_limit_for_year();

        // Ensure that the new limit overlaps with the current limit
        // If not, it's impossible to satisfy both limits, so we must exit with an error
        ensure!(
            *limit.start() <= *current_limit.end() && *limit.end() >= *current_limit.start(),
            "Annual availability limit clashes with time slice/seasonal limits",
        );

        // Only insert the annual limit if it provides an extra level of constraint above the
        // existing time slice/seasonal limits. This prevents unnecessary constraints from being
        // stored and added to the optimization model.
        if *limit.start() > *current_limit.start() || *limit.end() < *current_limit.end() {
            self.annual_limit = Some(limit);
        }

        Ok(())
    }

    /// Get the limit for a given time slice selection
    pub fn get_limit(
        &self,
        time_slice_selection: &TimeSliceSelection,
    ) -> RangeInclusive<Dimensionless> {
        match time_slice_selection {
            TimeSliceSelection::Single(ts_id) => self.get_limit_for_time_slice(ts_id),
            TimeSliceSelection::Season(season) => self.get_limit_for_season(season),
            TimeSliceSelection::Annual => self.get_limit_for_year(),
        }
    }

    /// Get the limit for a given time slice
    pub fn get_limit_for_time_slice(
        &self,
        time_slice: &TimeSliceID,
    ) -> RangeInclusive<Dimensionless> {
        // Get limit for this specific time slice
        let ts_limit = self.time_slice_limits[time_slice].clone();
        let lower = *ts_limit.start();
        let mut upper = *ts_limit.end();

        // If there's a seasonal/annual limit, we must cap the time slice limit to ensure that it
        // doesn't exceed the upper bound of the season/year
        if let Some(seasonal_limit) = self.seasonal_limits.get(&time_slice.season) {
            upper = upper.min(*seasonal_limit.end());
        }
        if let Some(annual_limit) = &self.annual_limit {
            upper = upper.min(*annual_limit.end());
        }

        lower..=upper
    }

    /// Get the limit for a given season
    fn get_limit_for_season(&self, season: &Season) -> RangeInclusive<Dimensionless> {
        // Get sum of limits for all time slices in this season
        let mut lower = Dimensionless(0.0);
        let mut upper = Dimensionless(0.0);
        for (ts, limit) in &self.time_slice_limits {
            if &ts.season == season {
                lower += *limit.start();
                upper += *limit.end();
            }
        }

        // Bound this by the seasonal limit, if specified
        if let Some(seasonal_limit) = self.seasonal_limits.get(season) {
            lower = lower.max(*seasonal_limit.start());
            upper = upper.min(*seasonal_limit.end());
        }

        // If there's an annual limit, we must also cap the seasonal limit to ensure it doesn't
        // exceed the upper bound of the year
        if let Some(annual_limit) = &self.annual_limit {
            upper = upper.min(*annual_limit.end());
        }

        lower..=upper
    }

    /// Get the limit for the entire year
    fn get_limit_for_year(&self) -> RangeInclusive<Dimensionless> {
        // Get the sum of limits for all seasons
        let mut total_lower = Dimensionless(0.0);
        let mut total_upper = Dimensionless(0.0);
        let seasons = self
            .time_slice_limits
            .keys()
            .map(|ts_id| ts_id.season.clone())
            .unique();
        for season in seasons {
            let season_limit = self.get_limit_for_season(&season);
            total_lower += *season_limit.start();
            total_upper += *season_limit.end();
        }

        // Bound this by the annual limit, if specified
        if let Some(annual_limit) = &self.annual_limit {
            total_lower = total_lower.max(*annual_limit.start());
            total_upper = total_upper.min(*annual_limit.end());
        }

        total_lower..=total_upper
    }

    /// Iterate over all limits
    ///
    /// This first iterates over all individual time slice limits, followed by seasonal limits (if
    /// any), and finally the annual limit (if any).
    pub fn iter_limits(
        &self,
    ) -> impl Iterator<Item = (TimeSliceSelection, &RangeInclusive<Dimensionless>)> {
        // Iterate over all time slice limits
        let time_slice_limits = self
            .time_slice_limits
            .iter()
            .map(|(ts_id, limit)| (TimeSliceSelection::Single(ts_id.clone()), limit));

        // Then seasonal limits, if any
        let seasonal_limits = self
            .seasonal_limits
            .iter()
            .map(|(season, limit)| (TimeSliceSelection::Season(season.clone()), limit));

        // Then annual limit, if any
        let annual_limits = self
            .annual_limit
            .as_ref()
            .map(|limit| (TimeSliceSelection::Annual, limit));

        // Chain all limits together
        time_slice_limits
            .chain(seasonal_limits)
            .chain(annual_limits)
    }
}

/// Represents a maximum annual commodity coeff for a given process
#[derive(PartialEq, Debug, Clone)]
pub struct ProcessFlow {
    /// The commodity produced or consumed by this flow
    pub commodity: Rc<Commodity>,
    /// Maximum annual commodity flow quantity relative to other commodity flows.
    ///
    /// Positive value indicates flow out and negative value indicates flow in.
    pub coeff: FlowPerActivity,
    /// Identifies if a flow is fixed or flexible.
    pub kind: FlowType,
    /// Cost per unit flow.
    ///
    /// For example, cost per unit of natural gas produced. The user can apply it to any specified
    /// flow.
    pub cost: MoneyPerFlow,
}

impl ProcessFlow {
    /// Get the cost per unit flow for a given region, year, and time slice.
    ///
    /// Includes flow costs and levies/incentives, if any.
    pub fn get_total_cost_per_flow(
        &self,
        region_id: &RegionID,
        year: u32,
        time_slice: &TimeSliceID,
    ) -> MoneyPerFlow {
        self.cost + self.get_levy(region_id, year, time_slice)
    }

    /// Get the cost for this flow per unit of activity for a given region, year, and time slice.
    ///
    /// This includes cost per unit flow and levies/incentives, if any.
    pub fn get_total_cost_per_activity(
        &self,
        region_id: &RegionID,
        year: u32,
        time_slice: &TimeSliceID,
    ) -> MoneyPerActivity {
        let cost_per_unit = self.get_total_cost_per_flow(region_id, year, time_slice);
        self.coeff.abs() * cost_per_unit
    }

    /// Get the levy/incentive for this process flow with the given parameters, if any
    fn get_levy(&self, region_id: &RegionID, year: u32, time_slice: &TimeSliceID) -> MoneyPerFlow {
        match self.direction() {
            FlowDirection::Input => *self
                .commodity
                .levies_cons
                .get(&(region_id.clone(), year, time_slice.clone()))
                .unwrap_or(&MoneyPerFlow(0.0)),
            FlowDirection::Output => *self
                .commodity
                .levies_prod
                .get(&(region_id.clone(), year, time_slice.clone()))
                .unwrap_or(&MoneyPerFlow(0.0)),
            FlowDirection::Zero => MoneyPerFlow(0.0),
        }
    }

    /// Direction of the flow
    pub fn direction(&self) -> FlowDirection {
        match self.coeff {
            x if x < FlowPerActivity(0.0) => FlowDirection::Input,
            x if x > FlowPerActivity(0.0) => FlowDirection::Output,
            _ => FlowDirection::Zero,
        }
    }
}

/// Type of commodity flow (see [`ProcessFlow`])
#[derive(PartialEq, Default, Debug, Clone, DeserializeLabeledStringEnum)]
pub enum FlowType {
    /// The input to output flow ratio is fixed
    #[default]
    #[string = "fixed"]
    Fixed,
    /// The flow ratio can vary, subject to overall flow of a specified group of commodities whose
    /// input/output ratio must be as per user input data
    #[string = "flexible"]
    Flexible,
}

/// Direction of the flow (see [`ProcessFlow`])
#[derive(PartialEq, Debug)]
pub enum FlowDirection {
    /// The flow is an input (i.e., coeff < 0)
    Input,
    /// The flow is an output (i.e., coeff > 0)
    Output,
    /// The flow is zero, neither input nor output (i.e., coeff == 0)
    Zero,
}

/// Additional parameters for a process
#[derive(PartialEq, Clone, Debug)]
pub struct ProcessParameter {
    /// Overnight capital cost per unit capacity
    pub capital_cost: MoneyPerCapacity,
    /// Annual operating cost per unit capacity
    pub fixed_operating_cost: MoneyPerCapacityPerYear,
    /// Annual variable operating cost per unit activity
    pub variable_operating_cost: MoneyPerActivity,
    /// Lifetime in years of an asset created from this process
    pub lifetime: u32,
    /// Process-specific discount rate
    pub discount_rate: Dimensionless,
}

/// A constraint imposed on investments in the process
///
/// Constraints apply to a specific milestone year, and have been pre-scaled from annual limits
/// defined in the input data to account for the number of years since the previous milestone year.
#[derive(PartialEq, Debug, Clone)]
pub struct ProcessInvestmentConstraint {
    /// Addition constraint: Limit an agent can invest in the process, shared according to the
    /// agent's proportion of the process's primary commodity demand
    pub addition_limit: Option<Capacity>,
}

impl ProcessInvestmentConstraint {
    /// Calculate the effective addition limit
    ///
    /// For now, this just returns `addition_limit`, but in the future when we add growth
    /// limits and total capacity limits, this will have more complex logic which will depend on the
    /// current total capacity.
    pub fn get_addition_limit(&self) -> Option<Capacity> {
        self.addition_limit
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::commodity::{CommodityLevyMap, CommodityType, DemandMap, PricingStrategy};
    use crate::fixture::{assert_error, region_id, time_slice, time_slice_info2};
    use crate::time_slice::TimeSliceLevel;
    use crate::time_slice::TimeSliceSelection;
    use float_cmp::assert_approx_eq;
    use rstest::{fixture, rstest};
    use std::collections::HashMap;
    use std::rc::Rc;

    #[fixture]
    fn commodity_with_levy(region_id: RegionID, time_slice: TimeSliceID) -> Rc<Commodity> {
        let mut levies_prod = CommodityLevyMap::new();
        let mut levies_cons = CommodityLevyMap::new();

        // Add levy for the default region and time slice
        levies_prod.insert(
            (region_id.clone(), 2020, time_slice.clone()),
            MoneyPerFlow(10.0),
        );
        levies_cons.insert(
            (region_id.clone(), 2020, time_slice.clone()),
            MoneyPerFlow(-10.0),
        );
        // Add levy for a different region
        levies_prod.insert(("USA".into(), 2020, time_slice.clone()), MoneyPerFlow(5.0));
        levies_cons.insert(("USA".into(), 2020, time_slice.clone()), MoneyPerFlow(-5.0));
        // Add levy for a different year
        levies_prod.insert(
            (region_id.clone(), 2030, time_slice.clone()),
            MoneyPerFlow(7.0),
        );
        levies_cons.insert(
            (region_id.clone(), 2030, time_slice.clone()),
            MoneyPerFlow(-7.0),
        );
        // Add levy for a different time slice
        levies_prod.insert(
            (
                region_id.clone(),
                2020,
                TimeSliceID {
                    season: "summer".into(),
                    time_of_day: "day".into(),
                },
            ),
            MoneyPerFlow(3.0),
        );
        levies_cons.insert(
            (
                region_id.clone(),
                2020,
                TimeSliceID {
                    season: "summer".into(),
                    time_of_day: "day".into(),
                },
            ),
            MoneyPerFlow(-3.0),
        );

        Rc::new(Commodity {
            id: "test_commodity".into(),
            description: "Test commodity".into(),
            kind: CommodityType::ServiceDemand,
            time_slice_level: TimeSliceLevel::Annual,
            pricing_strategy: PricingStrategy::Shadow,
            levies_prod,
            levies_cons,
            demand: DemandMap::new(),
            units: "PJ".into(),
        })
    }

    #[fixture]
    fn commodity_with_consumption_levy(
        region_id: RegionID,
        time_slice: TimeSliceID,
    ) -> Rc<Commodity> {
        let mut levies = CommodityLevyMap::new();
        levies.insert((region_id, 2020, time_slice), MoneyPerFlow(10.0));

        Rc::new(Commodity {
            id: "test_commodity".into(),
            description: "Test commodity".into(),
            kind: CommodityType::ServiceDemand,
            time_slice_level: TimeSliceLevel::Annual,
            pricing_strategy: PricingStrategy::Shadow,
            levies_prod: CommodityLevyMap::new(),
            levies_cons: levies,
            demand: DemandMap::new(),
            units: "PJ".into(),
        })
    }

    #[fixture]
    fn commodity_with_production_levy(
        region_id: RegionID,
        time_slice: TimeSliceID,
    ) -> Rc<Commodity> {
        let mut levies = CommodityLevyMap::new();
        levies.insert((region_id, 2020, time_slice), MoneyPerFlow(10.0));

        Rc::new(Commodity {
            id: "test_commodity".into(),
            description: "Test commodity".into(),
            kind: CommodityType::ServiceDemand,
            time_slice_level: TimeSliceLevel::Annual,
            pricing_strategy: PricingStrategy::Shadow,
            levies_prod: levies,
            levies_cons: CommodityLevyMap::new(),
            demand: DemandMap::new(),
            units: "PJ".into(),
        })
    }

    #[fixture]
    fn commodity_with_incentive(region_id: RegionID, time_slice: TimeSliceID) -> Rc<Commodity> {
        let mut levies_prod = CommodityLevyMap::new();
        levies_prod.insert(
            (region_id.clone(), 2020, time_slice.clone()),
            MoneyPerFlow(-5.0),
        );
        let mut levies_cons = CommodityLevyMap::new();
        levies_cons.insert((region_id, 2020, time_slice), MoneyPerFlow(5.0));

        Rc::new(Commodity {
            id: "test_commodity".into(),
            description: "Test commodity".into(),
            kind: CommodityType::ServiceDemand,
            time_slice_level: TimeSliceLevel::Annual,
            pricing_strategy: PricingStrategy::Shadow,
            levies_prod,
            levies_cons,
            demand: DemandMap::new(),
            units: "PJ".into(),
        })
    }

    #[fixture]
    fn commodity_no_levies() -> Rc<Commodity> {
        Rc::new(Commodity {
            id: "test_commodity".into(),
            description: "Test commodity".into(),
            kind: CommodityType::ServiceDemand,
            time_slice_level: TimeSliceLevel::Annual,
            pricing_strategy: PricingStrategy::Shadow,
            levies_prod: CommodityLevyMap::new(),
            levies_cons: CommodityLevyMap::new(),
            demand: DemandMap::new(),
            units: "PJ".into(),
        })
    }

    #[fixture]
    fn flow_with_cost() -> ProcessFlow {
        ProcessFlow {
            commodity: Rc::new(Commodity {
                id: "test_commodity".into(),
                description: "Test commodity".into(),
                kind: CommodityType::ServiceDemand,
                time_slice_level: TimeSliceLevel::Annual,
                pricing_strategy: PricingStrategy::Shadow,
                levies_prod: CommodityLevyMap::new(),
                levies_cons: CommodityLevyMap::new(),
                demand: DemandMap::new(),
                units: "PJ".into(),
            }),
            coeff: FlowPerActivity(1.0),
            kind: FlowType::Fixed,
            cost: MoneyPerFlow(5.0),
        }
    }

    #[fixture]
    fn flow_with_cost_and_levy(region_id: RegionID, time_slice: TimeSliceID) -> ProcessFlow {
        let mut levies = CommodityLevyMap::new();
        levies.insert((region_id, 2020, time_slice), MoneyPerFlow(10.0));

        ProcessFlow {
            commodity: Rc::new(Commodity {
                id: "test_commodity".into(),
                description: "Test commodity".into(),
                kind: CommodityType::ServiceDemand,
                time_slice_level: TimeSliceLevel::Annual,
                pricing_strategy: PricingStrategy::Shadow,
                levies_prod: levies,
                levies_cons: CommodityLevyMap::new(),
                demand: DemandMap::new(),
                units: "PJ".into(),
            }),
            coeff: FlowPerActivity(1.0),
            kind: FlowType::Fixed,
            cost: MoneyPerFlow(5.0),
        }
    }

    #[fixture]
    fn flow_with_cost_and_incentive(region_id: RegionID, time_slice: TimeSliceID) -> ProcessFlow {
        let mut levies = CommodityLevyMap::new();
        levies.insert((region_id, 2020, time_slice), MoneyPerFlow(-3.0));

        ProcessFlow {
            commodity: Rc::new(Commodity {
                id: "test_commodity".into(),
                description: "Test commodity".into(),
                kind: CommodityType::ServiceDemand,
                time_slice_level: TimeSliceLevel::Annual,
                pricing_strategy: PricingStrategy::Shadow,
                levies_prod: levies,
                levies_cons: CommodityLevyMap::new(),
                demand: DemandMap::new(),
                units: "PJ".into(),
            }),
            coeff: FlowPerActivity(1.0),
            kind: FlowType::Fixed,
            cost: MoneyPerFlow(5.0),
        }
    }

    #[rstest]
    fn get_levy_no_levies(
        commodity_no_levies: Rc<Commodity>,
        region_id: RegionID,
        time_slice: TimeSliceID,
    ) {
        let flow = ProcessFlow {
            commodity: commodity_no_levies,
            coeff: FlowPerActivity(1.0),
            kind: FlowType::Fixed,
            cost: MoneyPerFlow(0.0),
        };

        assert_eq!(
            flow.get_levy(&region_id, 2020, &time_slice),
            MoneyPerFlow(0.0)
        );
    }

    #[rstest]
    fn get_levy_with_levy(
        commodity_with_levy: Rc<Commodity>,
        region_id: RegionID,
        time_slice: TimeSliceID,
    ) {
        let flow = ProcessFlow {
            commodity: commodity_with_levy,
            coeff: FlowPerActivity(1.0),
            kind: FlowType::Fixed,
            cost: MoneyPerFlow(0.0),
        };

        assert_eq!(
            flow.get_levy(&region_id, 2020, &time_slice),
            MoneyPerFlow(10.0)
        );
    }

    #[rstest]
    fn get_levy_with_incentive(
        commodity_with_incentive: Rc<Commodity>,
        region_id: RegionID,
        time_slice: TimeSliceID,
    ) {
        let flow = ProcessFlow {
            commodity: commodity_with_incentive,
            coeff: FlowPerActivity(1.0),
            kind: FlowType::Fixed,
            cost: MoneyPerFlow(0.0),
        };

        assert_eq!(
            flow.get_levy(&region_id, 2020, &time_slice),
            MoneyPerFlow(-5.0)
        );
    }

    #[rstest]
    fn get_levy_different_region(commodity_with_levy: Rc<Commodity>, time_slice: TimeSliceID) {
        let flow = ProcessFlow {
            commodity: commodity_with_levy,
            coeff: FlowPerActivity(1.0),
            kind: FlowType::Fixed,
            cost: MoneyPerFlow(0.0),
        };

        assert_eq!(
            flow.get_levy(&"USA".into(), 2020, &time_slice),
            MoneyPerFlow(5.0)
        );
    }

    #[rstest]
    fn get_levy_different_year(
        commodity_with_levy: Rc<Commodity>,
        region_id: RegionID,
        time_slice: TimeSliceID,
    ) {
        let flow = ProcessFlow {
            commodity: commodity_with_levy,
            coeff: FlowPerActivity(1.0),
            kind: FlowType::Fixed,
            cost: MoneyPerFlow(0.0),
        };

        assert_eq!(
            flow.get_levy(&region_id, 2030, &time_slice),
            MoneyPerFlow(7.0)
        );
    }

    #[rstest]
    fn get_levy_different_time_slice(commodity_with_levy: Rc<Commodity>, region_id: RegionID) {
        let flow = ProcessFlow {
            commodity: commodity_with_levy,
            coeff: FlowPerActivity(1.0),
            kind: FlowType::Fixed,
            cost: MoneyPerFlow(0.0),
        };

        let different_time_slice = TimeSliceID {
            season: "summer".into(),
            time_of_day: "day".into(),
        };

        assert_eq!(
            flow.get_levy(&region_id, 2020, &different_time_slice),
            MoneyPerFlow(3.0)
        );
    }

    #[rstest]
    fn get_levy_consumption_positive_coeff(
        commodity_with_consumption_levy: Rc<Commodity>,
        region_id: RegionID,
        time_slice: TimeSliceID,
    ) {
        let flow = ProcessFlow {
            commodity: commodity_with_consumption_levy,
            coeff: FlowPerActivity(1.0), // Positive coefficient means production
            kind: FlowType::Fixed,
            cost: MoneyPerFlow(0.0),
        };

        assert_eq!(
            flow.get_levy(&region_id, 2020, &time_slice),
            MoneyPerFlow(0.0)
        );
    }

    #[rstest]
    fn get_levy_consumption_negative_coeff(
        commodity_with_consumption_levy: Rc<Commodity>,
        region_id: RegionID,
        time_slice: TimeSliceID,
    ) {
        let flow = ProcessFlow {
            commodity: commodity_with_consumption_levy,
            coeff: FlowPerActivity(-1.0), // Negative coefficient means consumption
            kind: FlowType::Fixed,
            cost: MoneyPerFlow(0.0),
        };

        assert_eq!(
            flow.get_levy(&region_id, 2020, &time_slice),
            MoneyPerFlow(10.0)
        );
    }

    #[rstest]
    fn get_levy_production_positive_coeff(
        commodity_with_production_levy: Rc<Commodity>,
        region_id: RegionID,
        time_slice: TimeSliceID,
    ) {
        let flow = ProcessFlow {
            commodity: commodity_with_production_levy,
            coeff: FlowPerActivity(1.0), // Positive coefficient means production
            kind: FlowType::Fixed,
            cost: MoneyPerFlow(0.0),
        };

        assert_eq!(
            flow.get_levy(&region_id, 2020, &time_slice),
            MoneyPerFlow(10.0)
        );
    }

    #[rstest]
    fn get_levy_production_negative_coeff(
        commodity_with_production_levy: Rc<Commodity>,
        region_id: RegionID,
        time_slice: TimeSliceID,
    ) {
        let flow = ProcessFlow {
            commodity: commodity_with_production_levy,
            coeff: FlowPerActivity(-1.0), // Negative coefficient means consumption
            kind: FlowType::Fixed,
            cost: MoneyPerFlow(0.0),
        };

        assert_eq!(
            flow.get_levy(&region_id, 2020, &time_slice),
            MoneyPerFlow(0.0)
        );
    }

    #[rstest]
    fn get_total_cost_base_cost(
        flow_with_cost: ProcessFlow,
        region_id: RegionID,
        time_slice: TimeSliceID,
    ) {
        assert_eq!(
            flow_with_cost.get_total_cost_per_activity(&region_id, 2020, &time_slice),
            MoneyPerActivity(5.0)
        );
    }

    #[rstest]
    fn get_total_cost_with_levy(
        flow_with_cost_and_levy: ProcessFlow,
        region_id: RegionID,
        time_slice: TimeSliceID,
    ) {
        assert_eq!(
            flow_with_cost_and_levy.get_total_cost_per_activity(&region_id, 2020, &time_slice),
            MoneyPerActivity(15.0)
        );
    }

    #[rstest]
    fn get_total_cost_with_incentive(
        flow_with_cost_and_incentive: ProcessFlow,
        region_id: RegionID,
        time_slice: TimeSliceID,
    ) {
        assert_eq!(
            flow_with_cost_and_incentive.get_total_cost_per_activity(&region_id, 2020, &time_slice),
            MoneyPerActivity(2.0)
        );
    }

    #[rstest]
    fn get_total_cost_negative_coeff(
        mut flow_with_cost: ProcessFlow,
        region_id: RegionID,
        time_slice: TimeSliceID,
    ) {
        flow_with_cost.coeff = FlowPerActivity(-2.0);
        assert_eq!(
            flow_with_cost.get_total_cost_per_activity(&region_id, 2020, &time_slice),
            MoneyPerActivity(10.0)
        );
    }

    #[rstest]
    fn get_total_cost_zero_coeff(
        mut flow_with_cost: ProcessFlow,
        region_id: RegionID,
        time_slice: TimeSliceID,
    ) {
        flow_with_cost.coeff = FlowPerActivity(0.0);
        assert_eq!(
            flow_with_cost.get_total_cost_per_activity(&region_id, 2020, &time_slice),
            MoneyPerActivity(0.0)
        );
    }

    #[test]
    fn is_input_and_is_output() {
        let commodity = Rc::new(Commodity {
            id: "test_commodity".into(),
            description: "Test commodity".into(),
            kind: CommodityType::ServiceDemand,
            time_slice_level: TimeSliceLevel::Annual,
            pricing_strategy: PricingStrategy::Shadow,
            levies_prod: CommodityLevyMap::new(),
            levies_cons: CommodityLevyMap::new(),
            demand: DemandMap::new(),
            units: "PJ".into(),
        });

        let flow_in = ProcessFlow {
            commodity: Rc::clone(&commodity),
            coeff: FlowPerActivity(-1.0),
            kind: FlowType::Fixed,
            cost: MoneyPerFlow(0.0),
        };
        let flow_out = ProcessFlow {
            commodity: Rc::clone(&commodity),
            coeff: FlowPerActivity(1.0),
            kind: FlowType::Fixed,
            cost: MoneyPerFlow(0.0),
        };
        let flow_zero = ProcessFlow {
            commodity: Rc::clone(&commodity),
            coeff: FlowPerActivity(0.0),
            kind: FlowType::Fixed,
            cost: MoneyPerFlow(0.0),
        };

        assert!(flow_in.direction() == FlowDirection::Input);
        assert!(flow_out.direction() == FlowDirection::Output);
        assert!(flow_zero.direction() == FlowDirection::Zero);
    }

    #[rstest]
    fn new_with_full_availability(time_slice_info2: TimeSliceInfo) {
        let limits = ActivityLimits::new_with_full_availability(&time_slice_info2);

        // Each time slice from the info should be present in the limits
        for (ts_id, ts_len) in time_slice_info2.iter() {
            let l = limits.get_limit_for_time_slice(ts_id);
            // Lower bound should be zero and upper bound equal to time slice length
            assert_eq!(*l.start(), Dimensionless(0.0));
            assert_eq!(*l.end(), Dimensionless(ts_len.value()));
        }

        // Annual limit should be 0..1
        let annual_limit = limits.get_limit(&TimeSliceSelection::Annual);
        assert_approx_eq!(Dimensionless, *annual_limit.start(), Dimensionless(0.0));
        assert_approx_eq!(Dimensionless, *annual_limit.end(), Dimensionless(1.0));
    }

    #[rstest]
    fn new_from_limits_with_seasonal_limit_applied(time_slice_info2: TimeSliceInfo) {
        let mut limits = HashMap::new();

        // Set a seasonal upper limit that is stricter than the sum of time slices
        limits.insert(
            TimeSliceSelection::Season("winter".into()),
            Dimensionless(0.0)..=Dimensionless(0.01),
        );

        let result = ActivityLimits::new_from_limits(&limits, &time_slice_info2).unwrap();

        // Each time slice upper bound should be capped by the seasonal upper bound (0.01)
        for (ts_id, _ts_len) in time_slice_info2.iter() {
            let ts_limit = result.get_limit_for_time_slice(ts_id);
            assert_eq!(*ts_limit.end(), Dimensionless(0.01));
        }

        // The seasonal limit should reflect the given bound
        let season_limit = result.get_limit(&TimeSliceSelection::Season("winter".into()));
        assert_eq!(*season_limit.end(), Dimensionless(0.01));
    }

    #[rstest]
    fn new_from_limits_with_annual_limit_applied(time_slice_info2: TimeSliceInfo) {
        let mut limits = HashMap::new();

        // Set an annual upper limit that is stricter than the sum of time slices
        limits.insert(
            TimeSliceSelection::Annual,
            Dimensionless(0.0)..=Dimensionless(0.01),
        );

        let result = ActivityLimits::new_from_limits(&limits, &time_slice_info2).unwrap();

        // Each time slice upper bound should be capped by the annual upper bound (0.01)
        for (ts_id, _ts_len) in time_slice_info2.iter() {
            let ts_limit = result.get_limit_for_time_slice(ts_id);
            assert_eq!(*ts_limit.end(), Dimensionless(0.01));
        }

        // The seasonal limit should be capped by the annual upper bound (0.01)
        let season_limit = result.get_limit(&TimeSliceSelection::Season("winter".into()));
        assert_eq!(*season_limit.end(), Dimensionless(0.01));

        // The annual limit should reflect the given bound
        let annual_limit = result.get_limit(&TimeSliceSelection::Annual);
        assert_eq!(*annual_limit.end(), Dimensionless(0.01));
    }

    #[rstest]
    fn new_from_limits_missing_timeslices_error(time_slice_info2: TimeSliceInfo) {
        let mut limits = HashMap::new();

        // Add a single time slice limit but do not provide limits for all time slices
        let first_ts = time_slice_info2.iter().next().unwrap().0.clone();
        limits.insert(
            TimeSliceSelection::Single(first_ts),
            Dimensionless(0.0)..=Dimensionless(0.1),
        );

        assert_error!(
            ActivityLimits::new_from_limits(&limits, &time_slice_info2),
            "Missing availability limits for time slices: [winter.night]. Please provide"
        );
    }

    #[rstest]
    fn new_from_limits_incompatible_limits(time_slice_info2: TimeSliceInfo) {
        let mut limits = HashMap::new();

        // Time slice limits capping activity to 0.1 in each ts
        for (ts_id, _ts_len) in time_slice_info2.iter() {
            limits.insert(
                TimeSliceSelection::Single(ts_id.clone()),
                Dimensionless(0.0)..=Dimensionless(0.1),
            );
        }

        // Seasonal limit that is incompatible (lower limit above the sum of time slice upper limits)
        limits.insert(
            TimeSliceSelection::Season("winter".into()),
            Dimensionless(0.99)..=Dimensionless(1.0),
        );

        assert_error!(
            ActivityLimits::new_from_limits(&limits, &time_slice_info2),
            "Availability limit for season winter clashes with time slice limits"
        );
    }
}