Skip to content

Extract specific energy flows from results #275

Description

@PRse4

How can I extract flows/flow rates from results of specific components?

import flixopt as fx
import pandas as pd
import numpy as np
import pathlib
from typing import Dict, List, Union
import plotly.graph_objects as go

if __name__ == '__main__':

    # Data import
    data_import = pd.read_csv(pathlib.Path('C:/import.csv'), index_col=0).sort_index()
    filtered_data = data_import['2024-01-01 00:00:00':'2024-01-01 23:45:00']

    # --- Experiment Options ---
    # Configure options for testing various parameters and behaviors
    check_penalty = False
    excess_penalty = None
    time_indices = None  # Define specific time steps for custom calculations, or use the entire series

    electricity_generation_pv= filtered_data['PV1[kW]'].to_numpy().astype(float) # eingabe  in kW
    electricity_generation_pv= np.where(electricity_generation_pv>0, electricity_generation_pv, 0) 

    timesteps = pd.date_range('2024-01-01', periods= len(electricity_generation_pv), freq= '15min')
    flow_system = fx.FlowSystem(timesteps)  # Create FlowSystem
    
    # --- Define Energy Buses ---
    # Represent node balances (inputs=outputs) for the different energy carriers (electricity, heat, gas) in the system
    flow_system.add_elements(fx.Bus('AC', excess_penalty_per_flow_hour= excess_penalty))

    # --- Define Effects ---
    # Specify effects related to costs, CO2 emissions, and primary energy consumption
    costs = fx.Effect('costs', '€', 'Kosten', is_standard=True, is_objective=True)

    # --- Define Components ---
    battery=fx.Storage(
        'battery',
        charging=fx.Flow('E_Batt_in',bus= 'AC', size= 2500), 
        discharging=fx.Flow('E_Batt_out',bus= 'AC', size=2500),
        capacity_in_flow_hours=fx.InvestParameters(
            fix_effects = 0, # Fixed investment costs if invested. (Attention: Annualize costs to chosen period!)
            specific_effects= 0, # Specific costs, e.g., in €/kW_nominal or €/m²_nominal. Example: {costs: 3, CO2: 0.3} with costs and CO2 representing an Object of class Effect (Attention: Annualize costs to chosen period!)
            optional=False,  # Forced investment
            #minimum_size=500, # Minimum size of 500 kWh
            #maximum_size=1e3,  # Maximum size of 1000 kWh
            fixed_size= 5000  # in kWh
        ),
        initial_charge_state=500 ,  # Initial charge state NOT RELATIVE und muss >= absolute_minimum_charge_state sein
        relative_minimum_charge_state=0.1,
        relative_maximum_charge_state=0.9,
        eta_charge=1,    
        eta_discharge=1,  
        relative_loss_per_hour= 0.00001,
        prevent_simultaneous_charge_and_discharge=True,  # Prevent simultaneous charge/discharge
    )
    
    pv_system=fx.Source('PV', source=fx.Flow(
        'E_el_PV_generation', 
        bus='AC',
        size=1, 
        fixed_relative_profile = electricity_generation_pv)
        )
      
    flow_system.add_elements(costs, battery, pv_system)
        
    # --- Solve Flowsystem ---
    calculation = fx.FullCalculation(
        name= 'Full', 
        flow_system= flow_system, 
        active_timesteps = time_indices
        )
    calculation.do_modeling()        

    calculation.solve(fx.solvers.HighsSolver(
        mip_gap=0.01,
        time_limit_seconds=1200, # timelimit in sec
        # threads = 8, # Number of threads to use.
        # extra_options= '' # Filename for saving the solver log.
        ))
    
    calculation.results.to_file()
    data_e_batt_soc = calculation.results.components['battery|flow_rate']

Activity

  1. FBumann commented on Jul 1, 2025

    @FBumann
    Member

    @PRse4 The Example doesnt run, as you reference a local .csv-file. Can you include it here or mimic the included data?

  2. FBumann commented on Jul 1, 2025

    @FBumann
    Member

    Alternatively, here is a altered example with the solution

    import flixopt as fx
    import pandas as pd
    import numpy as np
    import pathlib
    from typing import Dict, List, Union
    import plotly.graph_objects as go
    
    if __name__ == '__main__':
        timesteps = pd.date_range('2024-01-01', periods=100, freq='15min')
        flow_system = fx.FlowSystem(timesteps)  # Create FlowSystem
    
        electricity_generation_pv = np.sin(np.linspace(0, 2*np.pi, len(timesteps)))
    
        # --- Define Energy Buses ---
        # Represent node balances (inputs=outputs) for the different energy carriers (electricity, heat, gas) in the system
        flow_system.add_elements(fx.Bus('AC'))
    
        # --- Define Effects ---
        # Specify effects related to costs, CO2 emissions, and primary energy consumption
        costs = fx.Effect('costs', '€', 'Kosten', is_standard=True, is_objective=True)
    
        # --- Define Components ---
        battery = fx.Storage(
            'battery',
            charging=fx.Flow('E_Batt_in', bus='AC', size=2500),
            discharging=fx.Flow('E_Batt_out', bus='AC', size=2500),
            capacity_in_flow_hours=5000,
        )
    
        pv_system = fx.Source(
            'PV', 
                source=fx.Flow(
                'E_el_PV_generation',
                bus='AC',
                size=1,
                fixed_relative_profile=electricity_generation_pv)
        )
    
        flow_system.add_elements(costs, battery, pv_system)
    
        # --- Solve Flowsystem ---
        calculation = fx.FullCalculation(name='Full', flow_system=flow_system)
        calculation.do_modeling()
    
        calculation.solve(fx.solvers.HighsSolver(mip_gap=0.01, time_limit_seconds=1200))
    
        # Get solution of batter variables
        all_variable_solutions_of_battery = calculation.results.components['battery'].solution
    
        charging_flow_rate = calculation.results.solution['battery(E_Batt_in)|flow_rate']
        discharging_flow_rate = calculation.results.solution['battery(E_Batt_in)|flow_rate']
        charge_state = calculation.results.solution['battery|charge_state']
        
        # Convert to table
        df = all_variable_solutions_of_battery.to_pandas()
  3. PRse4 commented on Jul 1, 2025

    @PRse4
    Author

    thanks

  4. locked and limited conversation to collaborators on Jul 1, 2025
  5. converted this issue into a discussion #276 on Jul 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions