def multiply_param_with_variable(params, results, param_name, var_name):
    def get_label(k):
        if isinstance(k, tuple):
            return tuple(map(str, k))
        return str(k)

    parameter = select_from_dict(params, param_name)

    variable = select_from_dict(results, var_name)

    intersection = processing.convert_keys_to_strings(parameter).keys()\
                   & processing.convert_keys_to_strings(variable).keys()

    product = {}
    for k, var in variable.items():
        if get_label(k) in intersection:
            par = processing.convert_keys_to_strings(parameter)[get_label(k)]

            if isinstance(par, pd.Series):
                par.index = var.index

            prod = var * par
            product.update({k: prod})

    return product
예제 #2
0
 def test_nodes_with_none_exclusion_old_name(self):
     param_results = processing.parameter_as_dict(self.es,
                                                  exclude_none=True)
     param_results = processing.convert_keys_to_strings(param_results,
                                                        keep_none_type=True)
     assert_series_equal(
         param_results[('storage', None)]['scalars'],
         pandas.Series({
             'balanced': True,
             'initial_storage_level': 0,
             'invest_relation_input_capacity': 1 / 6,
             'invest_relation_output_capacity': 1 / 6,
             'investment_ep_costs': 0.4,
             'investment_existing': 0,
             'investment_maximum': float('inf'),
             'investment_minimum': 0,
             'label': 'storage',
             'inflow_conversion_factor': 1,
             'loss_rate': 0,
             'max_storage_level': 1,
             'min_storage_level': 0,
             'outflow_conversion_factor': 0.8,
         }))
     assert_frame_equal(param_results[('storage', None)]['sequences'],
                        pandas.DataFrame())
예제 #3
0
def node(results, node, multiindex=False):
    """
    Obtain results for a single node e.g. a Bus or Component.

    Either a node or its label string can be passed.
    Results are written into a dictionary which is keyed by 'scalars' and
    'sequences' holding respective data in a pandas Series and DataFrame.
    """
    # convert to keys if only a string is passed
    if type(node) is str:
        results = convert_keys_to_strings(results)

    filtered = {}

    # create a series with tuples as index labels for scalars
    scalars = {k: v['scalars'] for k, v in results.items()
               if node in k and not v['scalars'].empty}
    if scalars:
        # aggregate data
        filtered['scalars'] = pd.concat(scalars.values(), axis=0)
        # assign index values
        idx = {k: [c for c in v['scalars'].index]
               for k, v in results.items()
               if node in k and not v['scalars'].empty}
        idx = [tuple((k, m) for m in v) for k, v in idx.items()]
        idx = [i for sublist in idx for i in sublist]
        filtered['scalars'].index = idx
        filtered['scalars'].sort_index(axis=0, inplace=True)

        if multiindex:
            idx = pd.MultiIndex.from_tuples(
                [tuple([row[0][0], row[0][1], row[1]])
                 for row in filtered['scalars'].index])
            idx.set_names(['from', 'to', 'type'], inplace=True)
            filtered['scalars'].index = idx

    # create a dataframe with tuples as column labels for sequences
    sequences = {k: v['sequences'] for k, v in results.items()
                 if node in k and not v['sequences'].empty}
    if sequences:
        # aggregate data
        filtered['sequences'] = pd.concat(sequences.values(), axis=1)
        # assign column names
        cols = {k: [c for c in v['sequences'].columns]
                for k, v in results.items()
                if node in k and not v['sequences'].empty}
        cols = [tuple((k, m) for m in v) for k, v in cols.items()]
        cols = [c for sublist in cols for c in sublist]
        filtered['sequences'].columns = cols
        filtered['sequences'].sort_index(axis=1, inplace=True)

        if multiindex:
            idx = pd.MultiIndex.from_tuples(
                [tuple([col[0][0], col[0][1], col[1]])
                 for col in filtered['sequences'].columns])
            idx.set_names(['from', 'to', 'type'], inplace=True)
            filtered['sequences'].columns = idx

    return filtered
예제 #4
0
def run_model(params, wind_invest=False, pv_invest=False, storage_invest=False):
    logging.info('Initialize the energy system')
    energysystem = solph.EnergySystem(timeindex=date_time_index)
    Node.registry = energysystem
    logging.info('Create oemof objects')
    bgas = solph.Bus(label="natural_gas")
    bel = solph.Bus(label="electricity")

    solph.Sink(label='excess_bel', inputs={bel: solph.Flow()})

    solph.Source(label='rgas', outputs={bgas: solph.Flow(nominal_value=params['rgas_nom_val'],
                                                         summed_max=1)})

    solph.Source(label='wind', outputs={bel: solph.Flow(
            actual_value=data['wind'], nominal_value=params['wind_nom_val'], fixed=True)})

    solph.Source(label='pv', outputs={bel: solph.Flow(
           actual_value=data['pv'], nominal_value=params['pv_nom_val'], fixed=True)})

    solph.Sink(label='demand', inputs={bel: solph.Flow(
        actual_value=data['demand_el'], fixed=True, nominal_value=1)})

    solph.Transformer(
        label="pp_gas",
        inputs={bgas: solph.Flow()},
        outputs={bel: solph.Flow(nominal_value=10e10, variable_costs=50)},
        conversion_factors={bel: 0.58})


    logging.info('Optimise the energy system')

    om = solph.Model(energysystem)

    logging.info('Solve the optimization problem')
    om.solve(solver='cbc')

    energysystem.results['main'] = processing.convert_keys_to_strings(processing.results(om))
    energysystem.results['param'] = processing.convert_keys_to_strings(processing.param_results(energysystem))


    return energysystem, om
예제 #5
0
 def test_nodes_with_none_exclusion_old_name(self):
     param_results = processing.param_results(self.es, exclude_none=True)
     param_results = processing.convert_keys_to_strings(param_results)
     assert_series_equal(
         param_results[('storage', 'None')]['scalars'],
         pandas.Series({
             'initial_capacity': 0,
             'invest_relation_input_capacity': 1 / 6,
             'invest_relation_output_capacity': 1 / 6,
             'investment_ep_costs': 0.4,
             'investment_existing': 0,
             'investment_maximum': float('inf'),
             'investment_minimum': 0,
             'label': 'storage',
             'capacity_loss': 0,
             'capacity_max': 1,
             'capacity_min': 0,
             'inflow_conversion_factor': 1,
             'outflow_conversion_factor': 0.8,
         }))
     assert_frame_equal(param_results[('storage', 'None')]['sequences'],
                        pandas.DataFrame())
예제 #6
0
파일: views.py 프로젝트: vessenar/oemof
def node(results, node, multiindex=False, keep_none_type=False):
    """
    Obtain results for a single node e.g. a Bus or Component.

    Either a node or its label string can be passed.
    Results are written into a dictionary which is keyed by 'scalars' and
    'sequences' holding respective data in a pandas Series and DataFrame.
    """
    def replace_none(col_list, reverse=False):
        replacement = ((None, NONE_REPLACEMENT_STR) if reverse else
                       (NONE_REPLACEMENT_STR, None))
        changed_col_list = [((replacement[0] if n1 is replacement[1] else n1,
                              replacement[0] if n2 is replacement[1] else n2),
                             f) for (n1, n2), f in col_list]
        return changed_col_list

    # convert to keys if only a string is passed
    if type(node) is str:
        results = convert_keys_to_strings(results, keep_none_type)

    filtered = {}

    # create a series with tuples as index labels for scalars
    scalars = {
        k: v['scalars']
        for k, v in results.items() if node in k and not v['scalars'].empty
    }
    if scalars:
        # aggregate data
        filtered['scalars'] = pd.concat(scalars.values(), axis=0)
        # assign index values
        idx = {
            k: [c for c in v['scalars'].index]
            for k, v in results.items() if node in k and not v['scalars'].empty
        }
        idx = [tuple((k, m) for m in v) for k, v in idx.items()]
        idx = [i for sublist in idx for i in sublist]
        filtered['scalars'].index = idx

        # Sort index
        # (if Nones are present, they have to be replaced while sorting)
        if keep_none_type:
            filtered['scalars'].index = replace_none(
                filtered['scalars'].index.tolist())
        filtered['scalars'].sort_index(axis=0, inplace=True)
        if keep_none_type:
            filtered['scalars'].index = replace_none(
                filtered['scalars'].index.tolist(), True)

        if multiindex:
            idx = pd.MultiIndex.from_tuples([
                tuple([row[0][0], row[0][1], row[1]])
                for row in filtered['scalars'].index
            ])
            idx.set_names(['from', 'to', 'type'], inplace=True)
            filtered['scalars'].index = idx

    # create a dataframe with tuples as column labels for sequences
    sequences = {
        k: v['sequences']
        for k, v in results.items() if node in k and not v['sequences'].empty
    }
    if sequences:
        # aggregate data
        filtered['sequences'] = pd.concat(sequences.values(), axis=1)
        # assign column names
        cols = {
            k: [c for c in v['sequences'].columns]
            for k, v in results.items()
            if node in k and not v['sequences'].empty
        }
        cols = [tuple((k, m) for m in v) for k, v in cols.items()]
        cols = [c for sublist in cols for c in sublist]
        filtered['sequences'].columns = replace_none(cols)
        filtered['sequences'].sort_index(axis=1, inplace=True)
        filtered['sequences'].columns = replace_none(
            filtered['sequences'].columns, True)

        if multiindex:
            idx = pd.MultiIndex.from_tuples([
                tuple([col[0][0], col[0][1], col[1]])
                for col in filtered['sequences'].columns
            ])
            idx.set_names(['from', 'to', 'type'], inplace=True)
            filtered['sequences'].columns = idx

    return filtered
def run_model(params, wind_invest=False, pv_invest=False, storage_invest=False):
    logging.info('Initialize the energy system')
    energysystem = solph.EnergySystem(timeindex=date_time_index)
    Node.registry = energysystem
    logging.info('Create oemof objects')
    bgas = solph.Bus(label="natural_gas")
    bel = solph.Bus(label="electricity")

    solph.Sink(label='excess_bel', inputs={bel: solph.Flow()})

    solph.Source(label='rgas', outputs={bgas: solph.Flow(nominal_value=params['rgas_nom_val'],
                                                         summed_max=1)})

    if wind_invest == True:
        solph.Source(label='wind', outputs={bel: solph.Flow(
            actual_value=data['wind'], fixed=True,
            investment=solph.Investment(ep_costs=params['epc_wind']))})
    else:
        solph.Source(label='wind', outputs={bel: solph.Flow(
            actual_value=data['wind'], nominal_value=params['wind_nom_val'], fixed=True)})

    if pv_invest == True:
        pv = solph.Source(label='pv', outputs={bel: solph.Flow(
            actual_value=data['pv'], fixed=True,
            investment=solph.Investment(ep_costs=params['epc_pv']))})
    else:
        solph.Source(label='pv', outputs={bel: solph.Flow(
           actual_value=data['pv'], nominal_value=params['pv_nom_val'], fixed=True)})

    solph.Sink(label='demand', inputs={bel: solph.Flow(
        actual_value=data['demand_el'], fixed=True, nominal_value=1)})

    solph.Transformer(
        label="pp_gas",
        inputs={bgas: solph.Flow()},
        outputs={bel: solph.Flow(nominal_value=10e10, variable_costs=50)},
        conversion_factors={bel: 0.58})


    storage = solph.components.GenericStorage(
        label='storage',
        inputs={bel: solph.Flow(variable_costs=10e10)},
        outputs={bel: solph.Flow(variable_costs=10e10)},
        capacity_loss=0.00, initial_capacity=0,
        nominal_input_capacity_ratio=1/6,
        nominal_output_capacity_ratio=1/6,
        inflow_conversion_factor=1, outflow_conversion_factor=0.8,
        investment=solph.Investment(ep_costs=params['epc_storage']),
    )

    logging.info('Optimise the energy system')

    om = solph.Model(energysystem)
    test_var = 2
    print(test_var)

    logging.info('Solve the optimization problem')
    om.solve(solver='cbc')

    string_results = processing.convert_keys_to_strings(processing.results(om))
    electricity_results = views.node(string_results, 'electricity')
    param_dict = processing.convert_keys_to_strings(processing.parameter_as_dict(energysystem))
    param_dict_scalars = {key: value['scalars'] for (key,value) in param_dict.items()}

    print(string_results.keys())
    print(string_results[('wind','electricity')]['scalars']['invest'])
    print(string_results[('pv','electricity')]['scalars']['invest'])
예제 #8
0
파일: oemof_results.py 프로젝트: nesnoj/WAM
def store_results(session, input_data, result_data):
    """
    Stores inputs and results from oemof into DB

    For each in entry in scalars and sequences of both input- and result-data
    an OemofScalar or OemofSequence is build and connected to an OemofData
    object representing either input or result data. At last, both OemofData
    objects are connected to OemofInputResult object and resulting index is
    returned.

    Parameters
    ----------
    session: sqlalchemy.session
        SQLAlchemy session build via sqlalchemy.orm.sessionmaker
    input_data: dict
        Output of oemof.outputlib.processing.param_results with nodes as str
        (use oemof.outputlib.processing.convert_keys_to_str if necessary)
    result_data: dict
        Output of oemof.outputlib.processing.param_results with nodes as str
        (use oemof.outputlib.processing.convert_keys_to_str if necessary)

    Returns
    -------
    int: Index of created OemofInputResult entry
    """
    # Check if nodes are strings:
    if not isinstance(next(iter(input_data)), str):
        input_data = convert_keys_to_strings(input_data)
    if not isinstance(next(iter(result_data)), str):
        result_data = convert_keys_to_strings(result_data)

    input_result = OemofInputResult()
    for input_result_attr, data in (('input', input_data), ('result',
                                                            result_data)):
        scalars = []
        sequences = []
        for (from_node, to_node), sc_sq_dict in data.items():
            for key, value in sc_sq_dict['scalars'].items():
                scalars.append(
                    OemofScalar(from_node=from_node,
                                to_node=to_node,
                                attribute=key,
                                value=value,
                                type=type(value).__name__))
            session.add_all(scalars)
            for key, series in sc_sq_dict['sequences'].items():
                list_type = 'list'
                if isinstance(series, pandas.Series):
                    series = series.values.tolist()
                    list_type = 'series'
                sequences.append(
                    OemofSequence(from_node=from_node,
                                  to_node=to_node,
                                  attribute=key,
                                  value=series,
                                  type=list_type))
            session.add_all(sequences)
        oemof_data = OemofData()
        oemof_data.scalars = scalars
        oemof_data.sequences = sequences
        setattr(input_result, input_result_attr, oemof_data)
    session.add(input_result)
    session.flush()
    result_id = input_result.input_result_id
    transaction.commit()
    return result_id