Skip to content

Commit

Permalink
Use WeeklyProfileSize to match weekly profile length
Browse files Browse the repository at this point in the history
  • Loading branch information
s-simoncelli committed Nov 23, 2023
1 parent 27ccfdb commit 5a40dbd
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 20 deletions.
51 changes: 34 additions & 17 deletions pywr-core/src/parameters/profiles/weekly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,44 @@ use crate::timestep::Timestep;
use crate::PywrError;
use std::any::Any;

// A weekly profile can be 52 or 53 week long
enum WeeklyProfileSize {
FiftyTwo([f64; 52]),
FiftyThree([f64; 53]),
}

impl WeeklyProfileSize {
// Calculate the value for the current week
fn value(&self, date: &time::Date) -> f64 {
let current_day = date.ordinal() as usize;
let current_day_index = current_day - 1;

match self {
Self::FiftyTwo(values) => {
if current_day >= 364 {
values[51]
} else {
values[current_day_index / 7]
}
}
Self::FiftyThree(values) => {
if current_day >= 364 {
values[52]
} else {
values[current_day_index / 7]
}
}
}
}
}

pub struct WeeklyProfileParameter {
meta: ParameterMeta,
values: Vec<f64>,
values: WeeklyProfileSize,
}

impl WeeklyProfileParameter {
pub fn new(name: &str, values: Vec<f64>) -> Self {
pub fn new(name: &str, values: WeeklyProfileSize) -> Self {
if values.len() != 52 || values.len() != 53 {
panic!("52 or 53 values must be given for the weekly profile named {name}");
}
Expand All @@ -39,20 +70,6 @@ impl Parameter for WeeklyProfileParameter {
_state: &State,
_internal_state: &mut Option<Box<dyn Any + Send>>,
) -> Result<f64, PywrError> {
let current_day = timestep.date.ordinal();
let current_day_index = current_day - 1;

let week_index: u16;
if current_day >= 364 {
if self.values.len() == 52 {
week_index = 51
} else {
week_index = 52
}
} else {
week_index = current_day_index / 7
}

Ok(self.values[week_index as usize])
Ok(self.values.value(&timestep.date))
}
}
7 changes: 4 additions & 3 deletions pywr-schema/src/parameters/profiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use crate::parameters::{
};
use pywr_core::parameters::ParameterIndex;
use pywr_v1_schema::parameters::{
DailyProfileParameter as DailyProfileParameterV1, MonthInterpDay as MonthInterpDayV1,
DailyProfileParameter as DailyProfileParameterV1,
MonthInterpDay as MonthInterpDayV1,
MonthlyProfileParameter as MonthlyProfileParameterV1,
// WeeklyProfileParameter as WeeklyProfileParameterV1,
UniformDrawdownProfileParameter as UniformDrawdownProfileParameterV1,
Expand Down Expand Up @@ -243,7 +244,7 @@ impl WeeklyProfileParameter {
let values = &self.values.load(tables)?;
let p = pywr_core::parameters::WeeklyProfileParameter::new(
&self.meta.name,
values.try_into().expect(""),
values.try_into().expect("")
);
Ok(model.add_parameter(Box::new(p))?)
}
Expand Down Expand Up @@ -275,4 +276,4 @@ impl WeeklyProfileParameter {
// let p = Self { meta, values };
// Ok(p)
// }
// }
// }

0 comments on commit 5a40dbd

Please sign in to comment.