# Smart Grid Testing Management Platform (SGTMP) +
## 3 Implementing SGTMP
### The Mosaik Co-Simulation Framework  
Mosaik is an open-source co-simulation framework written primarily in Python [20; 21]. It aims to allow for the connection of multiple independent SG simulation entities into one centralized simulation grid. In this simulation grid, Mosaik takes care of all of the data exchange between the simulation entities and ensures simulator time-frame synchronization. Time-frame synchronization is a crucial feature for all hardware-in-the-loop (HIL) testing frameworks, as all simulation entities can run with  
Figure 6: Activity diagram of a test run in SGTMP.  
variable update rate ranging from once every few milliseconds to minutes or hours [42]. By ensuring time-frame synchronization, all simulation entities can send and receive data from each other without having to worry about the liveness of the received data. This behavior of Mosaik is classified as discrete-event simulation [11], since each simulator runs in its process and receives events signaling the arrival of new data or a request to send data elsewhere.  
Mosaik provides a way to connect several independent simulators into one simulation by providing a single API for the user-supplied simulators (Figure 7). A simulator handles input/output sharing via the low/high-level API. Each simulator can have multiple models, and each model can have multiple instances. An example of a simulator can be a program that manages the runtime of several models or an application that interfaces with one or several hardware components. An example of a model can be software simulation of a PV panel given a set of input parameters or the individual hardware components. For some simulation models, allowing various model creations is not possible (e.g., if there is only one hardware component available and concurrent access is forbidden). For purely software-based simulators, care must be taken to ensure the given simulator can handle all of the parallel model processing.  
One of the key advantages of Mosaik is its ability to integrate almost any simulator into the simulation by implementing the Mosaik API for each simulator. If the user implements the API for their simulator, the simulator could be almost anything, even a gateway to a completely different simulation framework such as Simulink ([[https://www.mathworks.com/products/simulink.html](https://www.mathworks.com/products/simulink.html)]([https://www.mathworks.com/products/simulink.html](https://www.mathworks.com/products/simulink.html))). Thus in practice, it is possible to combine several simulation frameworks using Mosaik as the primary data exchange framework. For example, in [PERSON] et al. [53], the possibility of having self-contained Simulink simulators of photovoltaic panels is discussed along with methods for integration of the Simulink simulation model into Mosaik, where power grid entities can be simulated, either by native Mosaik simulators or with user-defined simulators.  
The Mosaik framework provides a conceptual abstraction of connecting different simulators, which allows the user to view all of the simulators as directly connected (Figure 8, logical data flow). However, due to the need to ensure that data and individual simulation time frames are synchronized, data must be sent to the central Mosaik server, which takes care of distributing it to the designated model at the appropriate time. Thus, the real model inside the framework is different (Figure 8, actual data flow). This approach allows transparent synchronization of both the time frames and data to be exchanged.  
Figure 7: Architectural view of the Mosaik Framework. Reproduced with permission from: [PERSON] et al. [52]