# Co-simulation Using TCP/IP

Inter-Process Communication (IPC) co-simulation allows co-simulation to run on the same machine, as well as between different machines/platforms.

IPC co-simulation uses TCP sockets to communicate between the two processes that run solvers. Because TCP protocol is platform independent, this communication type allows communication between different platforms.

## Supported Methods for MATLAB/Simulink-Related Co-simulation Using TCP/IP

Two IPC co-simulation modes are supported:
• Simulink Coder/Real-Time Workshop (RTW) IPC co-simulation.

Two additional DLLs are needed for both IPC co-simulations: nuclient.dll and nuserver.dll. These libraries are used to establish the server-client communication.

For the RTW DLL, there is no change for the existing RTW DLL for it to be loaded. For Simulink IPC co-simulation, the mex DLL needed can be either mscosim or mscosimipc, which is the name entered for the S-Function block.

Note that for a co-simulation with an RTW DLL, MotionSolve drives the simulation (MotionSolve is called first). For a Simulink IPC co-simulation, Simulink is launched and called first. The process that brokers the communication (the TCP/IP daemon) is running on the machine to be called by the driving process.

## Required Model Changes

The following model changes are required for the two different ways to use TCP/IP communication: RTW co-simulation and Simulink co-simulation.
RTW
For RTW co-simulation, add the attribute ip_address inside the Control_PlantInput and Control_PlantOutput elements. This attribute contains a string that indicates the IP address (204.235.26.47 or localhost, for example) where the MotionSolve solver will run.
For Simulink IPC co-simulation, there is no change to MotionSolve XML model. For the Simulink model, change the name of the S-Function from mscosim into mscosimipc, or keep the same.

Additionally, you need to specify the IP address of the machine where MotionSolve will be running. To do this, define either a MATLAB variable or the environment variable MS_SERVER_IP_ADDRESS in the MATLAB workspace (for example, MS_SERVER_IP_ADDRESS = 204.235.26.47 or setenv (MS_SERVER_IP_ADDRESS, 204.235.26.47) ).

If you use this method, you will need to specify that each time you start MATLAB, since the variable will not persist after closing MATLAB. If you want to define the IP address only once and will not change it later on, you can define a system-level environmental variable, MS_SERVER_IP_ADDRESS, that points to an IP address where MotionSolve is running. If this environment variable is not found at the time of the IPC co-simulation run, locachost will be used as the default for the IP address.

## Run an RTW IPC Co-simulation

1. Change an RTW XML model into an IPC model by adding the ip_address attribute to Control_PlantInput and Control_PlantOutput, as described in Co-simulation Using TCP/IP.
2. On the machine where the XML model resides, issue a normal MotionSolve run command as: msolve xml_name mrf_name
Note: mbd_d.exe will also work instead of msolve.exe.
3. On the machine with the RTW DLL, issue the following command:.
msdaemon 22903 < ip_address string

For example, 204.235.26.47>

1. ip_address points to the machine where MotionSolve is running.
2. msdaemon.exe and nuclient.dll are the required files on this machine, and may be copied from another installation of the same platform.
3. msdaemon.exe and RTW DLL must be in the search path on that machine.
4. Set the environment variable NUSOL_DLL_DIR to point to the folder where msdaemon.exe and nuclient.dll reside.
5. The required msdaemon.exe and nuclient.dll files are located in the MotionSolve installation directory:

<install_dir>\hwsolvers\motionsolve\bin\<platform>

For example, C:\Program Files\Altair\2019.1\hwsolvers\motionsolve\bin\win64

The co-simulation should now run to completion.

## Run a Simulink IPC Co-simulation

1. Change a Simulink model into an IPC model by:
1. Changing the S-Function name to mscosim/mscosimipc.
2. Define MS_SERVER_IP_ADDRESS either inside a MATLAB workspace or as environmental variable, which are described above.
2. On the machine where the MotionSolve XML model resides, issue the following command: msdaemon 22903.
This process loads nuserver DLL and later on loads nusolver DLL to run MotionSolve model. The name of XML model to be run is defined in the Simulink model.
3. On the machine where Simulink model resides, launch the Simulink model and run the simulation.
The co-simulation should now run to completion.
Note: The ip_address attribute to Control_PlantInput and Control_PlantOutput is not used by Simulink IPC co-simulation.
If the parameter ip_address points to a local machine, which is localhost, you only need to issue the command to start the process that drives the co-simulation ( MotionSolve for RTW IPC co-simulation; Simulink for Simulink IPC co-simulation), and not start a separate msdaemon process, which is done automatically.