# CoSimulating Communication Networks and Electrical System for Performance Evaluation in Smart Grid
## 3 OOCoSim Design and Implementation
### OOCoSim Architecture and Implementation  
In this subsection, we describe the architecture and implementation of OOCoSim. The most important issue of OOCoSim implementation is how to interface both simulators with user-defined application function. To implement the interfacing functionality, OOCoSim employs ESD, EI, and ESM of OPNET and also COM Interface of OpenDSS. Figure 4 represents the overall architecture of OOCoSim. As we can see from the figure, the CoSim module is the most fundamental component of OOCoSim. It manages the operations of both simulators using the global scheduler and exchanges data among OPNET, OpenDSS, and the application model. The pre-defined functions of the external system access (ESA) library of OPNET are employed by the CoSim module to manage the operation of OPNET. Once OPNET simulation is activated by the CoSim module, the CoSim module and OPNET simulation communicate with each other via EIs. EIs are interfaces through which the CoSim module and OPNET simulation can access to the same information, and they are used to report network information to the CoSim module. ESM is a process model which performs protocol operations of application layer. The basic functionality of ESM is message communications. In OOCoSim, ESM is used to exchange relevant information with the CoSim module through EIs. Therefore, a supplementary state which employs EIs is added to the ESM. Specifically, OPNET collaborates with the CoSim module in the following manner. * First, OPNET simulation is invoked by the CoSim module through the functions defined in ESA, such as Esa_Main(), Esa_Init() and Esa_Load(). Figure 3: The example case of the scheduling mechanism. * Second, message communications of the application model are defined and performed by ESM. The protocol can be implemented with a finite state machine. * op_esys_interface_value_set() with the defined data type. * Fourth, the information is retrieved by the CoSim module through the callback handler which is defined for each EI. The information is passed to the application module. The CoSim module delivers information to OPNET and schedules an interrupt in the following manner. * The CoSim module can execute OPNET simulation until the specific simulation time through the function Esa_Execute_Until(). * When an event occurs in OPNET simulation, the CoSim module can interrupt OPNET simulation by using the function Esa_Interrupt(). * When CoSim module receives information targeting ESM by OpenDSS or the application model, it delivers information with/without an interrupt through the functions defined in ESA such as Esa_Interface_Value_Set() and Esa_Interface_Array_Set(). OpenDSS simulation is defined by a DSS script file. The DSS script file configures a distribution system which we want to simulate and runs a simulation. Once the simulation is invoked, we can access and change the values of simulation elements through the COM interface. Therefore, we implemented CoSim module to communicate with OpenDSS simulation via the COM interface. To externally drive OpenDSS simulation: (i) we have to create DSS files to define and configure a distribution system; then (ii) we invoke the simulation with the DSS files via the COM interface; and (iii) we set/get properties of circuit elements of OpenDSS simulation. In the CoSim module, we implemented some functions that handle OpenDSS with the COM interface by C++ languages and _IDispatch_ class of VC++, so that OOCoSim can employ OpenDSS. Specifically, OOCoSim uses IFext, ICircuit, and circuit elements (e.g., ILoad and IGenerator) interfaces, all of which are subclasses of _IDispatch_ class. The IFext interface is for an execution of OpenDSS commands such as compile and clear, the ICircuit interface lets the CoSim module retrieve circuit element interfaces, and the circuit element interfaces are used to set/get the properties of the circuit element.