Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ecalc 1379 #567

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/libecalc/common/temporal_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,26 @@ def evaluate(
)
result[start_index:end_index] = evaluated_expression
return result

@staticmethod
def extrapolated(
temporal_expression: TemporalModel[Expression],
variables_map: VariablesMap,
) -> List[float]:
result = variables_map.false()
for period, expression in temporal_expression.items():
if Period.intersects(period, variables_map.period):
start_index, end_index = period.get_timestep_indices(variables_map.time_vector)
variables_map_for_this_period = variables_map.get_subset(start_index=start_index, end_index=end_index)
extrapolated_this_period = [
any(variables)
for variables in zip(
[False] * len(variables_map_for_this_period.time_vector),
*[
variables_map_for_this_period.variables_extrapolated[variable]
for variable in expression.variables
],
)
]
result[start_index:end_index] = extrapolated_this_period
return result
13 changes: 13 additions & 0 deletions src/libecalc/core/consumers/legacy_consumer/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,19 @@ def evaluate(
temporal_expression=TemporalModel(self._consumer_dto.regularity),
variables_map=variables_map,
)
# figure out if regularity has been extrapolated and override if it is extrapolated to 0
regularity_extrapolated = TemporalExpression.extrapolated(
temporal_expression=TemporalModel(self._consumer_dto.regularity),
variables_map=variables_map,
)
regularity = [
1
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will this also apply to timesteps at the end of the global time vector, if e.g. the input file with regularity ends at 2024, but the global time vector goes to 2026? Often, it is (or was) set explicitly to 0 in the last timestep (for different type of data, regularity, produiction etc), as an explicit end to make sure that extrapolation worked for the few remainign timesteps at the end - and in that case regularity should be 0, and not 1...right?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this is correct for missign timesteps in the beginning and in the middle of the global time vector, but not at the end. or perhaps those in the middle should be interpolated (as you do I think?)

if regularity_this_time_step == 0 and regularity_extrapolated_this_time_step
else regularity_this_time_step
for regularity_this_time_step, regularity_extrapolated_this_time_step in zip(
regularity, regularity_extrapolated
)
]

# NOTE! This function may not handle regularity 0
consumer_function_results = self.evaluate_consumer_temporal_model(
Expand Down
20 changes: 18 additions & 2 deletions src/libecalc/dto/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class VariablesMap(BaseModel):

time_vector: List[datetime] = Field(default_factory=list)
variables: Dict[str, List[Annotated[float, Field(allow_inf_nan=False)]]] = Field(default_factory=dict)
variables_extrapolated: Dict[str, List[Annotated[bool, Field(allow_inf_nan=False)]]] = Field(default_factory=dict)
variables_interpolated: Dict[str, List[Annotated[bool, Field(allow_inf_nan=False)]]] = Field(default_factory=dict)

@property
def period(self):
Expand All @@ -47,8 +49,19 @@ def length(self) -> int:

def get_subset(self, start_index: int = 0, end_index: int = -1) -> VariablesMap:
subset_time_vector = self.time_vector[start_index:end_index]
subset_dict = {ref: array[start_index:end_index] for ref, array in self.variables.items()}
return VariablesMap(variables=subset_dict, time_vector=subset_time_vector)
subset_variables = {ref: array[start_index:end_index] for ref, array in self.variables.items()}
subset_variables_extrapolated = {
ref: array[start_index:end_index] for ref, array in self.variables_extrapolated.items()
}
subset_variables_interpolated = {
ref: array[start_index:end_index] for ref, array in self.variables_interpolated.items()
}
return VariablesMap(
variables=subset_variables,
variables_extrapolated=subset_variables_extrapolated,
variables_interpolated=subset_variables_interpolated,
time_vector=subset_time_vector,
)

def get_subset_from_period(self, period: Period) -> VariablesMap:
start_index, end_index = period.get_timestep_indices(self.time_vector)
Expand All @@ -65,3 +78,6 @@ def get_subset_for_timestep(self, current_timestep: datetime) -> VariablesMap:

def zeros(self) -> List[float]:
return [0.0] * len(self.time_vector)

def false(self) -> List[bool]:
return [False] * len(self.time_vector)
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,40 @@ def all_energy_usage_models_variables():
datetime(2019, 1, 1, 0, 0),
datetime(2020, 1, 1, 0, 0),
]
return dto.VariablesMap(time_vector=time_vector, variables=variables)
variables_extrapolated = {
**{
"SIM1;OIL_PROD": [False, False, False, False],
"SIM1;WATER_PROD": [False, False, False, False],
"SIM1;GAS_PROD": [False, False, False, False],
"SIM1;WATER_INJ": [False, False, False, False],
"SIM1;GAS_LIFT": [False, False, False, False],
"SIM1;REGULARITY": [False, False, False, False],
"SIM1;POWERLOSS_CONSTANT": [False, False, False, False],
"FLARE;FLARE_RATE": [False, False, False, False],
"FLARE;METHANE_RATE": [False, False, False, False],
"$var.salt_water_injection": [False, False, False, False],
},
}
variables_interpolated = {
**{
"SIM1;OIL_PROD": [False, False, False, False],
"SIM1;WATER_PROD": [False, False, False, False],
"SIM1;GAS_PROD": [False, False, False, False],
"SIM1;WATER_INJ": [False, False, False, False],
"SIM1;GAS_LIFT": [False, False, False, False],
"SIM1;REGULARITY": [False, False, False, False],
"SIM1;POWERLOSS_CONSTANT": [False, False, False, False],
"FLARE;FLARE_RATE": [False, False, False, False],
"FLARE;METHANE_RATE": [False, False, False, False],
"$var.salt_water_injection": [False, False, False, False],
},
}
return dto.VariablesMap(
time_vector=time_vector,
variables=variables,
variables_extrapolated=variables_extrapolated,
variables_interpolated=variables_interpolated,
)


@pytest.fixture
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -656,5 +656,13 @@ def consumer_system_v2_dto() -> DTOCase:
"compressor1;rate": [0.0, 0.0, 0.0, 4000000.0],
"$var.compressor1": [0, 0, 0, 4000000],
},
variables_extrapolated={
"compressor1;rate": [False, False, False, False],
"$var.compressor1": [False, False, False, False],
},
variables_interpolated={
"compressor1;rate": [False, False, False, False],
"$var.compressor1": [False, False, False, False],
},
),
)
Original file line number Diff line number Diff line change
Expand Up @@ -292,5 +292,10 @@ def consumer_with_time_slots_models_dto(
)
],
),
variables=dto.VariablesMap(time_vector=time_vector, variables={"RATE": [5000] * number_of_years}),
variables=dto.VariablesMap(
time_vector=time_vector,
variables={"RATE": [5000] * number_of_years},
variables_extrapolated={"RATE": [False] * number_of_years},
variables_interpolated={"RATE": [False] * number_of_years},
),
)
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def _get_interpolation_kind(rate_interpolation_type: InterpolationType) -> str:

def _interpolate(
time_series: TimeSeries, time_vector: List[datetime], rate_interpolation_type: InterpolationType
) -> List[float]:
) -> Tuple[List[float], List[bool]]:
interpolation_kind = _get_interpolation_kind(
rate_interpolation_type=rate_interpolation_type,
)
Expand All @@ -56,15 +56,16 @@ def _interpolate(

interpolator = interp1d(x=setup_times, y=setup_y, kind=interpolation_kind)
target_times = [(time - start_time).total_seconds() for time in time_vector]
return list(interpolator(target_times))
value_is_interpolated = [value not in setup_times for value in target_times]
return list(interpolator(target_times)), value_is_interpolated


def fit_time_series_to_time_vector(
time_series: TimeSeries,
time_vector: List[datetime],
extrapolate_outside_defined_time_interval: bool,
interpolation_type: InterpolationType,
) -> List[float]:
) -> Tuple[List[float], List[bool], List[bool]]:
start, end = time_series.time_vector[0], time_series.time_vector[-1]
number_of_entries_before, entries_between, number_of_entries_after = _split_time_vector(
time_vector, start=start, end=end
Expand All @@ -76,12 +77,22 @@ def fit_time_series_to_time_vector(
extrapolation_after_value = 0.0

before_values = [0.0] * number_of_entries_before
between_values = _interpolate(
between_values, between_values_are_interpolated = _interpolate(
time_series=time_series, time_vector=entries_between, rate_interpolation_type=interpolation_type
)
after_values = [extrapolation_after_value] * number_of_entries_after

return [*before_values, *between_values, *after_values]
values_are_extrapolated = [
*[True] * number_of_entries_before,
*[False] * len(between_values),
*[True] * number_of_entries_after,
]
values_are_interpolated = [
*[False] * number_of_entries_before,
*between_values_are_interpolated,
*[False] * number_of_entries_after,
]
return [*before_values, *between_values, *after_values], values_are_extrapolated, values_are_interpolated


def _get_date_range(start: datetime, end: datetime, frequency: libecalc.common.time_utils.Frequency) -> Set[datetime]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ def _evaluate_variables(variables: Dict[str, YamlVariable], variables_map: Varia
for reference_id, variable in variables.items()
]
processed_variables = {**variables_map.variables}
processed_variables_extrapolated = {**variables_map.variables_extrapolated}
processed_variables_interpolated = {**variables_map.variables_interpolated}

did_process_variable = True
while len(variables_to_process) > 0 and did_process_variable:
Expand All @@ -80,6 +82,26 @@ def _evaluate_variables(variables: Dict[str, YamlVariable], variables_map: Varia
required_variable in processed_variables for required_variable in variable.required_variables
)
if is_required_variables_processed:
processed_variables_extrapolated[variable.reference_id] = [
any(required_variables)
for required_variables in zip(
[False] * len(variables_map.time_vector),
*[
processed_variables_extrapolated[required_variable]
for required_variable in variable.required_variables
],
)
]
processed_variables_interpolated[variable.reference_id] = [
any(required_variables)
for required_variables in zip(
[False] * len(variables_map.time_vector),
*[
processed_variables_interpolated[required_variable]
for required_variable in variable.required_variables
],
)
]
processed_variables[variable.reference_id] = variable.process(
variables=processed_variables,
time_vector=variables_map.time_vector,
Expand All @@ -103,7 +125,12 @@ def _evaluate_variables(variables: Dict[str, YamlVariable], variables_map: Varia
f"Missing references are {', '.join(missing_references)}"
)

return VariablesMap(variables=processed_variables, time_vector=variables_map.time_vector)
return VariablesMap(
variables=processed_variables,
variables_extrapolated=processed_variables_extrapolated,
variables_interpolated=processed_variables_interpolated,
time_vector=variables_map.time_vector,
)


def map_yaml_to_variables(
Expand All @@ -125,10 +152,16 @@ def map_yaml_to_variables(
)

variables = {}
variables_extrapolated = {}
variables_interpolated = {}
for timeseries_collection in timeseries_collections:
timeseries_list = timeseries_collection.time_series
for timeseries in timeseries_list:
variables[timeseries.reference_id] = fit_time_series_to_time_vector(
(
variables[timeseries.reference_id],
variables_extrapolated[timeseries.reference_id],
variables_interpolated[timeseries.reference_id],
) = fit_time_series_to_time_vector(
time_series=timeseries,
time_vector=global_time_vector,
extrapolate_outside_defined_time_interval=timeseries_collection.extrapolate_outside_defined_time_interval,
Expand All @@ -137,5 +170,10 @@ def map_yaml_to_variables(

return _evaluate_variables(
configuration.variables_raise_if_invalid,
variables_map=VariablesMap(variables=variables, time_vector=global_time_vector),
variables_map=VariablesMap(
variables=variables,
variables_interpolated=variables_interpolated,
variables_extrapolated=variables_extrapolated,
time_vector=global_time_vector,
),
)
Loading