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

PR improvements: math components in CalliopeMath, small fixes #665

Merged
merged 2 commits into from
Aug 19, 2024
Merged
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
11 changes: 2 additions & 9 deletions src/calliope/backend/backend_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from calliope.backend import helper_functions, parsing
from calliope.exceptions import warn as model_warn
from calliope.io import load_config
from calliope.preprocess import CalliopeMath
from calliope.preprocess.model_math import ORDERED_COMPONENTS_T, CalliopeMath
from calliope.util.schema import (
MODEL_SCHEMA,
extract_from_schema,
Expand All @@ -44,13 +44,6 @@
from calliope.exceptions import BackendError

T = TypeVar("T")
ORDERED_COMPONENTS_T = Literal[
"variables",
"global_expressions",
"constraints",
"piecewise_constraints",
"objectives",
]
ALL_COMPONENTS_T = Literal["parameters", ORDERED_COMPONENTS_T]


Expand Down Expand Up @@ -539,7 +532,7 @@ def _raise_error_on_preexistence(self, key: str, obj_type: ALL_COMPONENTS_T):

Args:
key (str): Backend object name
obj_type (Literal["variables", "constraints", "objectives", "parameters", "expressions"]): Object type.
obj_type (ALL_COMPONENTS_T): Object type.

Raises:
BackendError: if `key` already exists in the backend model
Expand Down
5 changes: 3 additions & 2 deletions src/calliope/backend/parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -850,12 +850,13 @@ def generate_top_level_where_array(
return where

def raise_caught_errors(self):
"""If there are any parsing errors, pipe them to the ModelError bullet point list generator."""
"""Pipe parsing errors to the ModelError bullet point list generator."""
if not self._is_valid:
exceptions.print_warnings_and_raise_errors(
errors={f"{self.name}": self._errors},
during=(
"math string parsing (marker indicates where parsing stopped, but may not point to the root cause of the issue)"
"math string parsing (marker indicates where parsing stopped, "
"but may not point to the root cause of the issue)"
),
bullet=self._ERR_BULLET,
)
18 changes: 10 additions & 8 deletions src/calliope/preprocess/model_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import importlib.resources
import logging
import typing
from copy import deepcopy
from pathlib import Path

Expand All @@ -11,6 +12,13 @@
from calliope.util.tools import relative_path

LOGGER = logging.getLogger(__name__)
ORDERED_COMPONENTS_T = typing.Literal[
"variables",
"global_expressions",
"constraints",
"piecewise_constraints",
"objectives",
]


class CalliopeMath:
Expand All @@ -33,13 +41,7 @@ def __init__(
"""
self.history: list[str] = []
self.data: AttrDict = AttrDict(
{
"variables": {},
"global_expressions": {},
"constraints": {},
"piecewise_constraints": {},
"objectives": {},
}
{name: {} for name in typing.get_args(ORDERED_COMPONENTS_T)}
)

for math in math_to_add:
Expand Down Expand Up @@ -127,7 +129,7 @@ def _add_pre_defined_file(self, filename: str) -> None:
self._add_file(f / f"{filename}.yaml", filename)

def _add_user_defined_file(
self, relative_filepath: str | Path, model_def_path: str | Path
self, relative_filepath: str | Path, model_def_path: str | Path | None
) -> None:
"""Add user-defined Calliope math, relative to the model definition path.

Expand Down