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
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
def get_result_array(backend_model, model_data): all_variables = { i.name: get_var(backend_model, i.name) for i in backend_model.component_objects() if isinstance(i, po.base.var.IndexedVar) } # 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() } results = reorganise_dataset_dimensions(xr.Dataset(all_variables)) if all_params: additional_inputs = reorganise_dataset_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
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_dataset_dimensions(xr.Dataset(all_params))
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))
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)