예제 #1
0
def get_result_array(backend_model, model_data):
    """
    From a Pyomo model object, extract decision variable data and return it as
    an xarray Dataset. Any rogue input parameters that are constructed inside
    the backend (instead of being passed by calliope.Model().inputs) are also
    added to calliope.Model()._model_data in-place.
    """
    all_variables = {
        i.name: get_var(backend_model, i.name)
        for i in backend_model.component_objects()
        if isinstance(i, po.base.Var)
    }

    # Get any parameters that did not appear in the user's model.inputs Dataset
    all_params = {
        i.name: get_var(backend_model, i.name)
        for i in backend_model.component_objects()
        if isinstance(i, po.base.param.IndexedParam) and i.name not in
        model_data.data_vars.keys() and "objective_" not in i.name
    }

    results = reorganise_xarray_dimensions(xr.Dataset(all_variables))

    if all_params:
        additional_inputs = reorganise_xarray_dimensions(
            xr.Dataset(all_params))
        for var in additional_inputs.data_vars:
            additional_inputs[var].attrs["is_result"] = 0
        model_data.update(additional_inputs)

    return results
예제 #2
0
 def test_fail_reorganise_dimensions(self):
     with pytest.raises(TypeError) as excinfo:
         dataset.reorganise_xarray_dimensions(
             ["timesteps", "nodes", "techs", "costs"])
     assert check_error_or_warning(
         excinfo,
         "Must provide either xarray Dataset or DataArray to be reorganised"
     )
예제 #3
0
 def test_fail_reorganise_dimensions(self):
     with pytest.raises(TypeError) as excinfo:
         dataset.reorganise_xarray_dimensions(
             ['timesteps', 'loc_techs_bar', 'costs'])
     assert check_error_or_warning(
         excinfo,
         'Must provide either xarray Dataset or DataArray to be reorganised'
     )
예제 #4
0
파일: model.py 프로젝트: FraSanvit/calliope
def get_result_array(backend_model, model_data):
    """
    From a Pyomo model object, extract decision variable data and return it as
    an xarray Dataset. Any rogue input parameters that are constructed inside
    the backend (instead of being passed by calliope.Model().inputs) are also
    added to calliope.Model()._model_data in-place.
    """
    subsets_config = AttrDict.from_yaml_string(model_data.attrs["subsets"])

    def _get_dim_order(foreach):
        return tuple([i for i in model_data.dims.keys() if i in foreach])

    all_variables = {
        i.name: get_var(
            backend_model,
            i.name,
            dims=_get_dim_order(subsets_config.variables[i.name].foreach),
        )
        for i in backend_model.component_objects(ctype=po.Var)
    }
    # Add in expressions, which are combinations of variables (e.g. costs)
    all_variables.update({
        i.name: get_var(
            backend_model,
            i.name,
            dims=_get_dim_order(subsets_config.expressions[i.name].foreach),
            expr=True,
        )
        for i in backend_model.component_objects(ctype=po.Expression)
    })

    # Get any parameters that did not appear in the user's model.inputs Dataset
    all_params = {
        i.name: get_var(backend_model, i.name, expr=True)
        for i in backend_model.component_objects(
            ctype=po.base.param.IndexedParam)
        if i.name not in model_data.data_vars.keys()
        and "objective_" not in i.name
    }

    results = string_to_datetime(
        backend_model, reorganise_xarray_dimensions(xr.Dataset(all_variables)))

    if all_params:
        additional_inputs = reorganise_xarray_dimensions(
            xr.Dataset(all_params))
        for var in additional_inputs.data_vars:
            additional_inputs[var].attrs["is_result"] = 0
        model_data.update(additional_inputs)
        model_data = string_to_datetime(backend_model, model_data)
    results = string_to_datetime(backend_model, results)

    return results
예제 #5
0
파일: time.py 프로젝트: awelsz/calliope
def final_timedimension_processing(model_data):

    # Final checking of the data
    model_data, final_check_comments, warns, errors = checks.check_model_data(
        model_data)
    exceptions.print_warnings_and_raise_errors(warnings=warns, errors=errors)

    model_data = reorganise_xarray_dimensions(model_data)
    model_data = add_max_demand_timesteps(model_data)

    return model_data
예제 #6
0
def access_pyomo_model_inputs(backend_model):
    """
    If the user wishes to inspect the parameter values used as inputs in the backend
    model, they can access a new Dataset of all the backend model inputs, including
    defaults applied where the user did not specify anything for a loc::tech
    """

    all_params = {
        i.name: get_var(backend_model, i.name, sparse=True)
        for i in backend_model.component_objects()
        if isinstance(i, po.base.param.IndexedParam)
    }

    return reorganise_xarray_dimensions(xr.Dataset(all_params))
예제 #7
0
def create_valid_subset(model_data, name, config):
    """
    Returns the subset for which a given constraint, variable or
    expression is valid, based on the given configuration. See `config/subsets.yaml` for
    the configuration definitions.

    Parameters
    ----------

    model_data : xarray.Dataset (calliope.Model._model_data)
    name : str
        Name of the constraint, variable or expression
    config : dict
        Configuration for the constraint, variable or expression

    Returns
    -------
    valid_subset : pandas.MultiIndex

    """

    # Start with a mask that is True where the tech exists at a node (across all timesteps and for a each carrier and cost, where appropriate)
    imask = _imask_foreach(model_data, config.foreach)
    if imask is False:  # i.e. not all of 'foreach' are in model_data
        return None
    # Add "where" info as imasks
    where_array = config.get_key("where", default=[])
    if where_array:
        imask = _imask_where(model_data, name, where_array, imask, "and_")

    # Add imask based on subsets
    imask = _subset_imask(name, config, imask)

    # Only build and return imask if there are some non-zero elements
    if isinstance(imask, xr.DataArray) and imask.sum() != 0:
        # Squeeze out any unwanted dimensions
        if len(imask.dims) > len(config.foreach):
            imask = imask.sum(
                [i for i in imask.dims if i not in config.foreach]) > 0
        # We have a problem if we have too few dimensions at this point...
        if len(imask.dims) < len(config.foreach):
            raise ValueError(f"Missing dimension(s) in imask for set {name}")

        valid_subset = _get_valid_subset(
            reorganise_xarray_dimensions(imask).astype(bool))

        return valid_subset

    else:
        return None
예제 #8
0
def access_pyomo_model_inputs(backend_model):
    """
    If the user wishes to inspect the parameter values used as inputs in the backend
    model, they can access a new Dataset of all the backend model inputs, including
    defaults applied where the user did not specify anything for a loc::tech
    """
    # TODO: update replace with 'removeprefix' once using py 3.9+
    all_params = {
        i.name.replace("calliope_", ""): get_var(
            backend_model,
            i.name,
            sparse=True,
        )
        for i in backend_model.component_objects(ctype=po.Param)
        if i.is_indexed()
    }
    inputs = xr.Dataset(all_params)
    inputs = string_to_datetime(backend_model, inputs)

    return reorganise_xarray_dimensions(inputs)
예제 #9
0
 def test_reorganise_dataarray_dimensions(self, example_dataarray):
     reorganised_dataset = dataset.reorganise_xarray_dimensions(
         example_dataarray)
     assert reorganised_dataset.dims == ("costs", "nodes", "techs",
                                         "timesteps")
예제 #10
0
 def test_reorganise_dataset_dimensions(self, example_dataset):
     reorganised_dataset = dataset.reorganise_xarray_dimensions(
         example_dataset)
     dataset_dims = [i for i in reorganised_dataset.dims.keys()]
     assert dataset_dims == ["costs", "nodes", "techs", "timesteps"]
예제 #11
0
 def test_reorganise_dataarray_dimensions(self, example_dataarray):
     reorganised_dataset = dataset.reorganise_xarray_dimensions(
         example_dataarray)
     assert reorganised_dataset.dims == ('costs', 'loc_techs_bar',
                                         'timesteps')
예제 #12
0
 def test_reorganise_dataset_dimensions(self, example_dataset):
     reorganised_dataset = dataset.reorganise_xarray_dimensions(
         example_dataset)
     dataset_dims = [i for i in reorganised_dataset.dims.keys()]
     assert dataset_dims == ['costs', 'loc_techs_bar', 'timesteps']
예제 #13
0
 def _clean_model_data(self):
     self.model_data = dataset.reorganise_xarray_dimensions(self.model_data)
     self._add_var_attrs()
     self._update_dtypes()
     self._check_data()