Simplifying multi-energy system co-simulations using energysim

The traditional methodology for conducting technical assessments of multi-energy systems involved using domain-specific modeling tools to focus on the energy sector of interest, while making simplifying assumptions about any coupled energy sector. This was acceptable since the interactions between energy domains were minimal. However, with the expectation of an increased adoption of energy conversion technologies (such as power to X (P2X) systems: power to heat, power to gas, etc.) in the future, and consequently higher interaction between various energy sectors and stronger dependence on one another, there is a need to update the current method for conducting technical assessments. This means taking into account not only the energy sector of interest, but also any dependent energy sectors, and the associated energy transformers (P2X). In this paper, we propose a co-simulation based approach to conduct simulation-based technical assessments of multi-energy systems, which allows us to couple domain specific modeling tools. We re-introduce the tool energysim to conduct the multi-energy system co-simulations. We motivate the importance of the proposed tool and compare it with other available tools. We highlight its main functionalities, and using a study case, we show how a multi-stakeholder, multi-energy system co-simulation can be set up and assessed.


Motivation and significance
The coupling of energy systems is an important driver towards a sustainable energy system.This recent trend in the coupling of various energy sectors has been driven primarily by the effort to decarbonize the energy system [1,2].As an added benefit, the energy conversion devices enabling this coupling such as power to heat, power to gas, power to mobility, etc. can also serve as sources of flexibility to the highly renewable future power systems [3,4].With increased interconnections, the interactions between various energy sectors are strongly coupled as well.We also refer to these coupled systems as multi-energy systems or MES.
To correctly assess MES, a holistic analysis of the system is required.It has been well noted in [5] that before an economic analysis can be conducted to determine a business case for an MES, it is essential to evaluate its technical feasibility.This involves the evaluation of various control strategies, assessment of operation and reliability of components and associated networks, determination of operational bottlenecks, etc.A modeling and simulation-based analysis, therefore, forms the first step towards the assessment of such integrated energy systems.
Simulation-based assessments are not new in energy system analysis.The dynamic and steady-state characteristics of different energy domains are unique.For example, the system dynamics for different energy carriers evolve at different time scales.For an electricity network, the changes in active power, and hence frequency, are immediately visible throughout the network.For a heat network, this is not the case: while pressure changes in the heat network are reflected throughout the network in seconds, the temperature dynamics across the network can take minutes and hours to reach a steady state.Consequently, to accurately consider these characteristics in analyzing a particular energy sector, state-of-the-art tools and solvers have been used.These tools have been developed using years of research and development to be able to accurately model the aforementioned unique characteristics of the energy domain in focus.Examples of such proprietary tools include PowerFactory for modeling electrical power system, Dymola/OpenModelica for modeling fluid system, MATLAB and Python-based APMonitor [6] for developing model-based control and PyTorch, [7] for developing data-driven controls.
When it comes to conducting a combined system study for an MES, there are two natural pathways.The first pathway is modeling the entire system in a single modeling environment, such as MATLAB, and then simulating it using a general-purpose solver.This is a time-consuming approach: it requires extensive knowledge on the part of the modeler to design different components of the MES.Because the MES is modeled in a single environment, it will be solved using a single, general-purpose solver.Such a solver may not be well suited to solving unique dynamics of subsystems within the MES, and therefore provide results that are not as accurate as with models and solvers developed using domain specific modeling tools.Additionally, simulating a large and complex multi domain model can require significant computing resources [8].
The second pathway is by dividing the MES into smaller subsystems (such as electrical subsystem, heat subsystem, gas subsystem, etc.), and leveraging software techniques to couple these subsystems.The subsystems can be modeled using domainspecific modeling tools, and solved using dedicated solvers (instead of general-purpose solvers) to obtain high accuracy results.This method is known as co-simulation, or coupled simulation [9].Co-simulation is a method that allows the coupling of models developed in various modeling environments by managing the time progression of the simulation and coordinating the data flow between subsystem models.By exchanging data (values of interest), such as process outputs, sensor measurements, etc. between subsystem models, dynamic interaction between the subsystems can be facilitated.Even though any data value can be shared, when creating an MES model, the exchanged variables are usually those which lay on the boundary of two energy carriers.For example mechanical power of steam turbine obtained from thermo-mechanical model (process output) of generator given as input to the rotor of the synchronous generator in the electrical power system model, or temperature of room obtained from a building thermal model (sensor measurement) given as input to a control system model, etc. Enabling this dynamic interaction allows for a holistic analysis of the system, where domain-specific characteristics are preserved while interacting with other energy domains.To reduce computational burden, techniques such as parallel and distributed computing can also be used [10] to speed up a co-simulation.As is noted in [11,12], operational model details have significant impact on results.Thus, by using domain-specific accurate models, potentially misleading simplifying assumptions are removed.Although, it must be noted that splitting larger systems into smaller subsystems and using co-simulation introduces some numerical challenges of its own, such as algebraic loops [9].
The main challenge in achieving this goal of a holistic system assessment is the development of a modular framework that allows the coupling of various subsystem models and an algorithm that manages overall simulation time progression and data exchange.This is where energysim steps in.energysim allows users to easily couple subsystem models and focus on high-level tasks in MES technical assessment studies such as subsystem model development, control algorithm development, case study definition, etc. rather than focusing on co-simulation specific tasks such as management of time progression in the co-simulation and data exchange between subsystem models.The availability of a simplified energy system co-simulator will allow increased insights into an MES setting by enabling a more collaborative modeling and simulation environment.
Previously, we used energysim in [13,14].In [13], we demonstrated how complex the model of the closed-cycle gas turbine was combined with the dynamic model of the electricity network for analyzing the impact of fuel supply change on electrical network frequency.In [14] we demonstrated how energysim can facilitate a multi-stakeholder analysis that involved concurrent simulation of detailed models of the electricity grid, electrolyzer, and control systems to correct forecasting errors of a nearby wind turbine.The version of energysim used for simulating use cases in the aforementioned articles has been majorly revamped.The current version (energysim (v2.1.7))provides more simulation adapters (explained in the next subsection) to couple other widely used energy modeling tools, allows access to the algorithm that coordinates time progression and data exchange which allows users to implement non-energy applications, uses HDF data format to store results, making it useful for very-high fidelity simulations generating considerable amounts of data.
energysim is developed in Python and is compatible with any version above v3.6 and can be easily installed with the python package manager (pypi).A use case has been provided in the main repository and a working example of the use case described in this paper has also been uploaded to the Code Ocean platform and is available as supplementary material.

Comparison with other tools
At the heart of any co-simulation tool lies an algorithm that manages time progression and data exchange.There already exist a few tools in literature such as Mosaik [15], MasterSim [16], MESCOS [17], Ptolemy II [18] to set up co-simulation.However, we believe, energysim offers several advantages to its users when compared to existing aforementioned tools.The first and foremost advantage is that it is developed in Python.Python is the most widely used language for scientific and general-purpose computing and has developed a huge user base, especially in the energy system community.Proprietary tools also frequently provide bindings to Python, which makes accessing them easy.This familiarity with the programming language allows energysim to be easily understandable and accessible to large audiences compared to tools which are developed in languages such as Java (Ptolemy II), C, C++ (MasterSim, MESCOS).Secondly, the structure of energysim is modular.This means that access to time progression and message exchange algorithm can be done via what we refer to as simulation adapters.Simulation adapters interface the simulation entity to energysim by defining four key function: init() , set_value() , get_value() , and step() .These functions enable energysim to initialize the simulator, set and get variable values at any time, and control simulation progress of the simulator in time respectively.To put it simply, these adapters provide a way for energysim to ''talk to simulators''.The tool already provides ready-made adapters to couple the most common tools used in the energy system modeling and simulation domain, however, creating new adapters can also be done easily.This is in contrast to other tools which require complex setup configurations (for ex.setting up Scenario API and Component API for each simulator with Mosaik) or can support only a single type of simulation entities (for ex.MasterSim supports only Functional Mock-up Units (FMUs)).

Main contribution
The main contribution of this work is the proposal of a new and improved co-simulation framework, energysim, that specifically addresses the needs of conducting technical simulations of complex MES.

Software architecture
energysim is classified as a ''hybrid-simulator''.It supports both quasi-static and continuous-time dynamic simulation (CTDS).We have not classified energysim as a discrete-time simulator because the term encompasses a broad range of simulation techniques, some of which energysim does not support, for example, event-based simulations, such as those involving communication network simulation.

Time progression and data exchange management
In energysim, there are two main time variables to define: macro-time step (for overall c-simulation) and micro-time step (for individual simulators).The data exchange between simulators occurs at fixed time intervals, known as the macro-time step.Between each macro-time step, individual simulators use an optional and unique micro-time step for solving their own model equations.This is essential for CTDS models to perform time integration for solving their model equations.The flowchart describing the co-simulation is shown in Fig. 1.
In between the macro-time steps, when no input data is available to the CTDS model, an interpolation method needs to be applied.Although there are quite a few techniques to implement the interpolation (constant, linear, polynomial [19]), within energysim, we have opted for a constant interpolation method.In this method, the inputs to the CTDS model are held constant at the value obtained at the last macro-time step.We are currently also testing new functionality that will allow users to select between different interpolations.

Software components
The core component of the energysim package is the world object.Once the world object is imported from energysim package, the user can instantiate it as shown in listing 1.
Listing 1: Instantiating world from energysim import world my_world = world ( s t a r t _ t i m e =0 , stop_time =23 * 3600 , logg ing = True , t_macro =60) my_world is the canvas on which simulators, simulator connections, and simulation options can be specified.The main parameters to be specified here are start_time , stop_time , logging , and t_macro .The parameter t_macro specifies the macro-time step and has a default value of 60 s.
Once my_world object is created, users can add simulators to it via the add_simulator() method as shown in listing 2. The add_simulator() method requires specification of sim_type , sim_name , sim_loc , and step_size .The parameter step_size specifies the micro-time step, unique to each simulator.These six parameters are shared for the specification of any simulator and are enough to execute a basic co-simulation.However, if additional parameters need to be specified, users can also do so.For example, a power flow model of an electric network (modeled using pandapower [20]) added in energysim using sim_type = 'powerflow' , by default, uses the AC power flow functionality of pandapower to solve the model (energysim does not contain a solver of its own).Therefore, an additional argument pf can be specified while adding the simulator so that if required, users can specify a different power flow options.For pandapower models, these include ''pf'' (default), ''dcpf'', ''opf'', and ''dcopf''.This is useful when, for example, an optimal controller based on electrical network power flow is needed.The example in the GitHub repository uses this functionality.There exist other similar simulator-specific options within energysim.A detailed list and description of these options are provided in the software documentation.
Some modeling languages allow exporting models as functional mock-up units (FMUs) according to the FMI standard [39].Models are packaged as a combination of XML files, binaries, and C-code and distributed as a ZIP file called FMU.The FMU contains encrypted model equations and optionally an associated solver.Model exchange via FMU is gaining wider adoption across the modeling and simulation community, with currently more than 150 tools supporting exporting models to FMUs.Within energysim, FMUs can be added by setting the value of the sim_type parameter as 'fmu' .
To ensure wider operability and integration to other software/python packages, we have also provided a way for users to interface their own simulator of choice with energysim by setting the sim_type parameter with 'external' .To add a userdefined simulator to energysim via user-developed simulation adapter, the to-be-coupled simulator must offer a ''play-andpause'' functionality.This means that energysim must be able to: • initialize the simulator, • get inputs from simulator when requested, • instruct the simulator to step forward in time, • request output values from the simulator, • pause the simulation while the simulator waits for instructions, and new inputs from energysim.
A detailed description of this method is available in the documentation.
Once the simulators are specified and added to my_world , users can specify the connections between the simulators as a Fig. 2.An example of an MES model and dummy interactions between subsystem models setup using energysim.Variables can be load active power, thermal power demand, gas demand, sent from Demand Profile simulator to EN, HN, and GN simulators.The EV simulator can exchange active power, reactive power, voltage value, charging status, etc. with the EN.The P2G simulator can exchange active power set-points with EN and gas flow rates and pressure with GN. python dictionary object, as shown.Then, the my_world ob- ject can then be simulated using the simulate() command.By default, the record_all parameter is set to True which instructs energysim to record each simulator's output values at every micro-time step.False leads to the recording of output only at macro-time step intervals.The results can be obtained by calling the command my_world.results() ,which returns a Python dictionary object with keys as sim_name and value as a pandas dataframe with time-stamped output values.If the to_csv option is set to True, then results are also exported as csv files.Parameter pbar toggles the simulation progress bar.Fig. 2 provides a graphical overview of an example of MES configured using energysim.

Software functionalities
Apart from the basic functionality to add simulators and simulate them, energysim offers a range of additional inbuilt functions to support the user in setting up multi-energy co-simulations.

Adding signals
In many cases, users may require a simple external input to their model.For example, a control system simulator needs a constant input value of True to indicate that the simulator is active, or a wind power plant simulator which may require a random number generated from a continuously updated probability distribution function as an input to account for the randomness of wind power production, etc.These inputs can be added as CSV simulators, FMU simulators, or first, be modeled in another modeling language and interfaced to energysim using the simulation adapters.However, such a process can be cumbersome.To simplify this process, we use ''signals''.Signals can provide the users with requested values and can be defined as python functions.Signals can be added to energysim using the add_signal()

Initialization
For CTDS models, correct initialization of system states is needed for accurate assessments.Different initial values will result in different dynamic behavior.To specify initial values to simulators, users can provide the initial values of parameters for the simulators as a dictionary with 'init' keyword.This dictionary can then be passed onto the my_world object through options method.Due to limited space, we have not included a listing for the same, but it can be found in the documentation.

Modifying data exchange
On many occasions, it becomes necessary to modify the output value of a particular simulator before it is provided to the other simulator.This situation is fairly common in multi-energy system simulations.Consider two simulators: a thermo-mechanical model of a combined heat and power system (CHP) and a steadystate power flow model of an electric network (EN).The power output from the CHP simulator is obtained in Watts (W).This power output of the CHP needs to be provided to the corresponding generator model in the EN.However, the generator in the EN accepts values only in megawatts (MW).One way to address the problem is to change the output values of the CHP in the model itself and recompile the model.However, this may not always be possible to do so (model may be encrypted).Therefore, energysim provides a method to apply linear modifications given by Eq. (1).
where y is the output of the simulator to be modified and y * is the modified output.By default, b is 0, therefore, users can provide a list with either just [a] or both [a, b].A dictionary entry to the options dictionary can be provided to energysim using the 'modify_signal' keyword.Both initialization and output modification is shown in listing 5.

Parameter sweep
Understanding the sensitivity of results on various simulation variables is an important part of technical assessments of MES.To perform repeated simulations with different initial conditions for the co-simulation in focus, users can make use of 'init' option shown in Section 2.2.2.By using a for-loop and providing different values of initial conditions, the sensitivity of results from the co-simulation can be obtained.A more sophisticated functionality is in the pipeline for the next release.

Topology plot
Using plot() method on my_world object, users can obtain a graph plot of the simulators, and connections between them.energysim uses python's NetworkX package to generate the topological plot of the multi-energy system based on the specified connections' dictionary.

Multi-stakeholder analysis
Consider an 18MW rated wind turbine connected to an electricity distribution grid.To mitigate its wind power fluctuations throughout the day, the wind turbine operator, WTO, forms a bilateral contract with a nearby 10MW industrial hydrogen electrolyzer to absorb its fluctuations.The electrolyzer's main goal is to produce hydrogen for its industrial process, and therefore, it can only modify its 10MW power set-point by ±5MW.Furthermore, the electrical grid operator needs to make sure that any sudden changes in load and generation setpoints for the electrolyzer and the wind turbine do not affect voltage stability in the distribution grid.We, therefore, need to conduct a technical analysis on the ability of the electrolyzer to adequately provide flexibility, while ensuring that electrolyzer production constraints are met, and the grid stability is not endangered.A central control system is designed which continuously receives information from various entities.These include: wind power forecast (P w,fc ) from WTO (added as csv simulator), hydrogen pressure and mass flow rate (p H2 , ṁH2 ) from electrolyzer (added as a fmu simulator), and actual wind power production of wind turbine (P w,a ) and grid bus voltages (V ) from pandapower network (added as powerflow simulator).Based on the received values, it calculates the electrolyzer power set point (P el,sp ) and dispatches it to the electrolyzer and grid.This setpoint is used by 1) the electrical grid to evaluate bus voltage based on the AC power flow solution, and 2) by electrolyzer to calculate hydrogen production rates.In the current setup, the controller also has an additional input for emergency control (u e ) (added as a signal simulator) to stop the electrolyzer operation in any case of an emergency.The system setup is shown in Fig. 3.
Listing 6 highlights the code to set up the co-simulation.The simulation takes exactly 6 min to execute on a dual-core Intel i5-6300U CPU @ 2.4 GHz running Ubuntu 20.04.Figs.4-6 shows the electrical network bus voltage, the hydrogen production rate obtained from the detailed electrolyzer model simulator, and the electrolyzer operating power set-points determined by the control system simulator respectively.

Impact
The need to take the proposed approach of coupled simulation towards energy system technical analyses is highlighted by the recent blackouts in Texas.Although the impact of extreme weather on power systems was assessed, the impact on natural gas supply and its equipment, which acts as a fuel source for power generation, was not considered simultaneously.This Fig. 3.The co-simulation setup for Section 3.1.The Data Files provide information regarding forecasted wind power to the control system.The Electrolyzer FMU provides information such as internal pressure and mass flow rates to the control system.The electrical network provides the voltage values at each bus node and the actual power output from the wind-powered plant.The controller uses this information to determine the operational power setpoint for the electrolyzer which it conveys to both the electrical power grid and the electrolyzer model.led to inaccurate assessments, leading to a catastrophic situation whereby millions of people lost access to electricity and heat.[40].
Using energysim, a combined assessment of energy sectors is possible, allowing users to get a better understanding of technical limitations in the interconnected system.The existence of such a tool facilitates interdisciplinary research efforts by bringing together models developed by experts in modeling tools of their choice.By making the technical assessment model agnostic, all subsystems can be modeled in desired detail and coupled with dedicated solvers to obtain a closer to reality system, and consequently accurate solutions.This is in contrast to existing domainspecific modeling and simulation tools, which either greatly simplify or completely ignore the interactions and dependence of energy sectors.
Using this tool, we expect the users to couple models of components and systems in different energy domains to derive critical insights and knowledge in designing and operating an integrated energy system.The ease of use will allow users to focus on tasks such as identification of operational bottlenecks, testing control strategies, the sensitivity of various factors on system behavior, checking technical feasibility of operating P2X devices on both the electricity and coupled sector simultaneously, etc.
Currently, energysim has enabled students and researchers at the Delft University of Technology to couple different types of energy systems for various studies.These include studying the impact of model fidelity on the availability of flexibility from P2X resources, development of machine learning models of integrated energy systems, and the evaluation of predictive control for flexibility coordination in multi-energy system setup.There has also been an interest in using energysim as a model for testing reinforcement learning-based control strategies.
Outside the intended user-group of energy system modeler and analysts, we can also expect energysim to be used in other applications requiring coupling of subsystem models to determine the evolution of system behavior over time.This could be enabled by using external simulators to access the time progression and data management algorithm of energysim.

Future work
energysim is a versatile tool for researchers to integrate various energy subsystem models to conduct holistic and comprehensive technical assessments.However, despite a range of available functionality, a few functionalities are still lacking, which are being actively worked on.These tasks have been identified to increase the adoption of energysim as well as to increase its capabilities as a multi-energy system co-simulator.Among these include the development of a method to conduct parameter sensitivity, the ability to specify different interpolations for data exchange between simulators, a method to parallelize simulation of individual simulators to increase computational efficiency and enable large-scale co-simulations, development of simulation adapters for other common software tools such as DigSILENT PowerFactory.Additionally, we intend to make available more case-study and examples as the energysim user group increases.

Conclusions
In this paper, we presented energysim, a multi-energy system co-simulation tool for coupling energy system models developed in different software tools.We have described in detail the main features and functionalities of the proposed software, illustrating its ease of use and versatility.We provided code listings to highlight the important features of the proposed tool: such as adding user-defined signals, initialization for conducting parameter sweeps, ability to perform output data modification, and generating topology plots for complex and simple MES.In the end, we demonstrated the use of energysim in a case study where detailed models of an electrolyzer, the electricity network, and a control system are used to assess the ability of the electrolyzer to act as a flexibility service provider to the wind plant generator.With the provided case study, it was shown how various stakeholders can come together to perform a holistic study and use preferred modeling tools and solvers in doing so, something which may not be possible with traditional monolithic simulation tools.

Declaration of competing interest
The authors declare that they have no known competing financial interests or personal relationships that could have appeared to influence the work reported in this paper.

Fig. 1 .
Fig.1.Flowchart depicting the co-simulation process.To start, the simulators are initialized and then, at each macro time step data is exchanged between the simulators.In between the macro-time steps, each simulator can use an individual micro-time step for simulation.

Listing 3 :
Finalizing simulation connections = { ' sim1 .o u t p u t _ v a r i a b l e 1 ' : ' sim2 .i n p u t _ v a r i a b l e 1 ' , ' sim3 .o u t p u t _ v a r i a b l e 2 ' : ' sim4 .i n p u t _ v a r i a b l e 2 ' , ' sim1 .o u t p u t _ v a r i a b l e 3 ' : ' sim2 .i n p u t _ v a r i a b l e 3 ' , } my_world .add_connections ( connections ) my_world .simulate ( pbar=True , r e c o r d _ a l l =True ) r e s u l t s = my_world .r e s u l t s ( to_csv = F a l s e )

Fig. 4 .
Fig. 4.Voltage at the bus where electrolyzer is connected to the electrical grid.This output comes from the electrical network simulator.

Fig. 5 .
Fig. 5. Production of hydrogen gas from the variable operation of electrolyzer.This output comes from the detailed electrolyzer model simulator.

Fig. 6 .
Fig.6.Electrolyzer operating power set point obtained.This output comes from control system simulator.

Table 1
List of common modeling languages used in energy system modeling and simulation community.
method.Examples of both time-dependent and independent signals are shown in listing 4.
8my_world .add_signal ( sim_name= ' c o n s t a n t _ s i g n a l ' , s i g n a l = t i d _ s i g n a l , s t e p _ s i z e =1)