Random Number Generator and Seed Values#
NetSim includes protocol and traffic models which include stochastic behavior. Typical examples are a) WiFi node's random backoff after collisions, and b) packet error decision by comparing a random number chosen between 0 and 1 against the packet error probability.
NetSim uses an inbuilt prime modulus combined linear congruential Random Number Generator (RNG) to generate the randomness. It is a single stream RNG with a period of $\approx 2 \times 10^{18}$. Two seeds values are used to initialize the RNG. These seeds can be input from NetSim GUI or via the config file. Having the same set of seed values ensures that for a particular network configuration the same output results will be got, irrespective of the system or time at which the simulation is run. This ensures repeatability of experimentation.
Modifying the seed value will lead to the generation of a different stream of random numbers and thereby lead to a different sequence of events in NetSim. Therefore, all simulation results are dependent on the initial seeding of the RNG.
Confidence in simulation results and error bars#
Since NetSim's models include stochastic behavior, results are dependent on the initial seeding of the random number generator. Because a particular random seed selection can potentially result in an anomalous, or nonrepresentative behavior, it is important for each model configuration to be exercised with several random number seeds, to be able to determine standard or typical behavior.
The field of statistics provides methods for calculating confidence in an estimate, based on a trial or series of random trials. To calculate confidence intervals, users can do the following:

Run $N$ simulations for the same model configuration, with a different initial seed (for the random number generator) for each run.

For any output metric $X$, calculate the mean (average) $\overline{X}$ of the $N$ samples.
$$\overline{X} = \frac{1}{N}\left( X_{1} + X_{2} + \ldots + X_{N} \right)$$
 Calculate the standard deviation $\sigma$ of the $N$ samples.
$$\sigma = \sqrt{\left( \frac{1}{N  1} \right) \times \Sigma\ \left( X_{i}  \overline{X} \right)^{2}}$$
 Confidence interval limits can be expressed as
$$\Theta_{lower} = \overline{X}  z_{\alpha} \times \left( \frac{\sigma}{\sqrt{(N)}\ } \right),\ {\ \Theta}{upper} = \overline{X} + z \times \left( \frac{\sigma}{\sqrt{(N)}\ } \right)$$
This statement can be thought of as assigning a probability to the condition that the true mean $\mu$ is within a particular distance of the random sample $\overline{X}$, as shown below
$$Prob\ \left\lbrack \overline{X}  z_{\alpha} \times \left( \frac{\sigma}{\sqrt{(N)}\ } \right) < \mu < \ \overline{X} + z_{\alpha} \times \left( \frac{\sigma}{\sqrt{(N)}\ } \right) \right\rbrack = \alpha$$
Confidence level $(α)$  $z_α$ 

99%  2.575 
98%  2.327 
95%  1.960 
90%  1.645 
80%  1.282 
Table 10‑1:Value of $\mathbf{Z}_{\mathbf{\alpha}}\mathbf{\ }$ for different confidence intervals
 The above expression for confidence assumes the distribution of the output metric is Normal, which is true (from the Central Limit Theorem) if the number of runs $N \geq 30.$ If the number of repetitions is less than 30 then it is better to use the tstatistic based confidenceinterval (details can be found in standard statistics textbooks).
Error bars are plotted as a vertical bar centered at the mean and ending at the upper and lower confidence limits
Interfacing MATLAB with NetSim (Std/Pro versions)#
NetSim provides runtime interfacing with MATLAB so that users do not have to rewrite code in C for features that are already available in MATLAB and instead simply reuse MATLAB code. Lot of work related to machine learning, artificial intelligence and specialized mathematical algorithms which can be used for networking research, can be carried out using existing MATLAB code.
Figure 10‑1: Interfacing MATLAB with NetSim
This interfacing feature can be used to either replace an existing functionality in NetSim or to incorporate additional functionalities supported by MATLAB. Any existing command/function/algorithm in MATLAB or a MATLAB Mscript can be used.
In general, the following are done when a user interfaces NetSim to MATLAB:

Initialize a MATLAB engine process in parallel with NetSim,

Execute MATLAB workspace commands,

Pass parameters to MATLAB workspace,

Read parameters from MATLAB workspace,

Generate dynamic threedimensional plots,

Make calls to functions that are part of MATLAB Mscripts or .m > files, and

Terminate the MATLAB engine process at NetSim simulation end.
Guidelines to interface NetSim with MATLAB

Analyze what parameters the function or code in MATLAB expects as > input.

Identify the relevant variables in NetSim to be passed as input to > MATLAB.

Make calls from relevant places of NetSim source code to

Pass parameters from NetSim to MATLAB.

To read computed parameters from MATLAB workspace

Identify and update the appropriate simulation variables in NetSim.
NetSim offers two different methods to interact with MATLAB during runtime.

NetSimMATLAB Socket Interface

NetSimMATLAB COM Interface
The first is an inbuilt socket interface that offers simplified NetSim API's that can be used for interactions with MATLAB. The second method involves importing MATLAB libraries, including MATLAB headers, and using MATLAB Engine APIs for all interactions with MATLAB. The former is a lot simpler since no additional settings will be required in the source code project settings. The later involves multiple configuration steps but offers more flexibility.
NetSimMATLAB Socket Interface#
NetSimMATLAB Socket Interface was introduced in NetSim v13.0 to reduce the complexity involved in the NetSimMATLAB COM Interface.
NetSim provides several inbuilt APIs that can be called from the underlying protocol C source codes to interact with MATLAB. Following is some of the APIs with syntax and description:
NetSim API’s to interact with MATLAB  Description 

netsim_matlab_interface_configure(char* appPath)  
Starts a MATLAB engine process and adds the appPath to the top of the search path for current MATLAB session.  
netsim_matlab_interface_close();  Sends exit command to MATLAB to terminate the session. 
netsim_matlab_send_ascii_command(char* format, ...)  Sends commands, variables and values as string to MATLAB. 
netsim_matlab_get_value(char out, int outLen, char name, char* type)  Gets the value of a MATLAB variable as a string. Currently supports double data type only. 
Table 10‑2: MATLAB Engine API functions
These functions are defined as part of the NetSim_utility.h file which is part of the Include directory of NetSim Source Codes. This header can be included in the C files where these APIs are to be called from.
Prerequisites for MATLAB Socket Interfacing#
 An installed version of MATLAB R2021(a) or lower version in the same > system where NetSim is installed.
Note:

If the MATLAB is installed in different System in the same Network. Copy MATLAB_Interface.exe present in bin folder from installed directory (C:\Program Files\NetSim\Standard_v13_1\bin) to the system where MATLAB is installed.

Execute the below command in the command prompt where MATLAB_Interface.exe is placed followed by IP address of the system where the NetSim is installed.
Figure 10‑2: Command to start the MATLAB interface

Registration of MATLAB as a COM server by one of the following methods
 Start Command Prompt as administrator and execute the following command:
matlab regserver
Note: If you have multiple versions of MATLAB installed on your computer, the best practice is to run the matlab command from the matlabroot folder.
 Enter the following command in the command line of MATLAB version that you want to interface with NetSim:
matlab regserver
Implement Weibull Distribution of MATLAB without using .m file#
In this example we will replace the default Rayleigh Fading (part of the path loss calculation) used in NetSim, with a Fading Power calculated using the Weibull Distribution from MATLAB Socket Interfacing.
"This example uses 64bit version of NetSim setup with MATLAB Engine"
Procedure
 Create a MATLAB_Interface.c file inside the IEEE802_11 folder which can be found in the current workspace location of NetSim that you are running, and it would be something like "C:\Users\PC\Documents\NetSim\Workspaces\\<Yourdefaultworkspace>\src\Simulation\IEEE802_11". Write the following code inside MATLAB_Interface.c file.
/\*
\*
\* This is a simple program that illustrates how to call the MATLAB
\* Engine functions from NetSim C Code.
\*
\*/
#include\<windows.h\>
#include\<stdlib.h\
#include\<stdio.h\
#include\<string.h\
#include \"main.h\"
#include \"Stack.h\"
#include\"NetSim_utility.h\"
#include \"direct.h\"
char buf\[BUFSIZ\];
double status;
double\* result;
double fn_netsim_matlab_init()
{
netsim_matlab_interface_configure(\"\");
return 0;
}
double fn_netsim_matlab_run()
{
//write your own implementation here
int weibull_noncentrality = 1, weibull_scale = 2;
//use ProbDistUnivParam() function for matlab 2016 or lower
//sprintf_s(buf, BUFSIZ, \"h=ProbDistUnivParam(\'weibull\',\[%d
%d\])\",
//weibull_noncentrality, weibull_scale);//
//use makedist() function for matlab 2017 or Higher
sprintf_s(buf,BUFSIZ,\"h=makedist(\'weibull\',%d,%d)\",
weibull_noncentrality,
weibull_scale);
netsim_matlab_send_ascii_command(buf);
sprintf_s(buf,BUFSIZ,\"i=random(h)\");//
netsim_matlab_send_ascii_command(buf);
netsim_matlab_get_value(buf,BUFSIZ,\"i\",\"double\");
double result = atof(buf);
return result;
}
double fn_netsim_matlab_finish()
{
fprintf(stderr, \"\\nPress any key to close MATLAB\\n\");
\_getch();
netsim_matlab_interface_close();
return 0;
}
Figure 10‑3: Create a MATLAB_Interface.c inside the IEEE802_11 folder

Now open the code and configure Visual studio Software Platform to x64.

Right click on "IEEE802_11 Project" present in "Solution Explorer" window and select Add > Existing Item > select the MATLAB_Interface.c

MATLAB_Interface.c file contains the following API functions.
I. netsim_matlab_interface_configure() under fn_netsim_matlab_init()  waits to connect MATLAB after running MATLAB interface.exe i.e "Open MATLAB Interface" option in the NetSim UI.
II. netsim_matlab_send_ascii_command() and netsim_matlab_get_value() inside fn_netsim_matlab_run() relevant variables in NetSim to be passed as input to MATLAB and vice versa.
III. netsim_matlab_interface_close(); inside fn_netsim_matlab_finish()  Send the quit command to MATLAB to terminate the connection.
 In the Solution Explorer under IEEE802_11 project double click on the IEEE802_11.c file.
Figure 10‑4: Solution Explorer double click on the IEEE802_11.c file
 Add a call to fn_netsim_matlab_init(); inside the fn_NetSim_IEEE802_11_Init() function.
Figure 10‑5: Added a fn_netsim_matlab_init(); inside the fn_NetSim_IEEE802_11_Init() function
 Similarly add a call to fn_netsim_matlab_finish(); inside the fn_NetSim_IEEE802_11_
Finish() function.
Figure 10‑6: Added a fn_netsim_matlab_finish(); inside the fn_NetSim_IEEE802_11_Finish() function
 In the Solution Explorer under IEEE802_11 project double click on the IEEE802_11.h file. Add definitions of the following functions.
double fn_netsim_matlab_init();
double fn_netsim_matlab_run();
double fn_netsim_matlab_finish();
Figure 10‑7: Added Matlab definitions in IEEE802_11.h file

In the Solution Explorer under IEEE802_11 project double click on the IEE802_11_PHY.c file.

Inside fn_Netsim_IEEE802_11_PHYIn () function comment the lines,
dFadingPower =
propagation_calculate_fadingloss(propagationHandle,packet\>nTransmitterId,ifid,pstruEventDetails\>nDeviceId,
pstruEventDetails\>nInterfaceId);
 Make a call to the fn_netsim_matlab_run() function by adding the following line,
dFadingPower = fn_netsim_matlab_run();
Figure 10‑8: Call to the fn_netsim_matlab_run() function
Note: In MATLAB Socket Interfacing, compilation of MATLAB Engine is not required since the MATLAB APIs are already called in function.
 Now Right Click on IEEE802_11 project and select Rebuild.
Figure 10‑9: Rebuild IEEE802_11 project

A new libIEEE802.11.dll gets created in the binx64 folder of NetSim's current workspace path C:\Users\PC\Documents\NetSim\Workspaces\\<Your default workspace>\bin_x64> for 64bit. For more information, follow steps provided in Section 9.1 "Writing your own code".

Run NetSim in Administrative mode. Create a Network scenario involving IEEE802_11 say MANET, right click on the Adhoc link and select properties. Make sure that the Channel Characteristics is set to PathLoss and Fading and Shadowing. Perform the simulation.
Figure 10‑10: Wireless Link Properties Window in MANET
 NetSim Simulation console waits for MATLAB Interface process to connect.
Figure 10‑11: NetSim Simulation console waits for MATLAB Interface process to connect

If MATLAB is installed in the same system where NetSim is installed. MATLAB Interface process can be initiated directly from the design window of NetSim.

Go to "Options" in design window and select the Open MATLAB Interface option as shown below:
Figure 10‑12: Option to open MATLAB Interface
 Click on the OK button when the below window is displayed. This opens the MATLAB Session.
Figure 10‑13: NetSim MATLAB Interfacing warning message

After clicking on "OK", MATLAB interface will get initiated. MATLAB Command window will open after which the simulation NetSim starts to simulate. During the simulation communication between MATLAB and NetSim will established.

The dFading power value will be displayed in the MATLAB Console window as below. At simulation end the MATLAB Interface process will get terminated.
Figure 10‑14: Runtime MATLAB interfacing window with I value
Debug and understand communication between NetSim and MATLAB#
 In the Solution Explorer under IEEE802_11 project double click on MATLAB_Interface.c file and place a breakpoint inside the fn_netsim_matlab_run() function before the return statement.
Figure 10‑15: Place a breakpoint inside the fn_netsim_matlab_run() function

Rebuild the code.

Now run the NetSim Scenario. The simulation window stops for user interrupt.

In Visual studio, go to Debug =>Attach to Process.

From the list of Processes select NetSimCore.exe and click on Attach.
Figure 10‑16: Select NetSimCore.exe in Attach to Process Window

Now go to the Simulation window and press Enter. Simulation will pause at Waiting for matlab interface to connect, go to Design window/GUI > Options > Open MATLAB interface >click on OK

MATLAB Command Window will start and breakpoint in Visual Studio gets triggered.
Figure 10‑17: Breakpoint gets triggered in Visual Studio.
 Now place another breakpoint after the line dFadingPower = fn_netsim_matlab_run()
Figure 10‑18: Added Breakpoint under fn_netsim_matlab_run()
 Add the variable dFadingPower in IEEE802_11_Phy.c file, to watch. For this, right click on the variable dFadingPower and select "Add Watch" option. You will find a watch window containing the variable name and its value in the bottom left corner.
Figure 10‑19: Right Click on dFadingPower and Select "Add Watch" option
 Now when debugging (say by pressing F5 each time) you will find that the watch window displays the value of dFadingPower whenever the control reaches the recently set breakpoint. You will also find that the value of dFadingPower in the Visual Studio Watch window and the value of i in the MATLAB Console window appear to be similar.
Figure 10‑20: The Visual Studio Watch window and the value of i in the Runtime MATLAB window are similar
Implement Weibull Distribution of MATLAB in NetSim using .m file#
Procedure:
 Create the weibull_distribution.m file inside \<bin_x64 folder of where workspace is created>
C:\Users\PC\Documents\NetSim\Workspaces\\<Your default workspace>\bin_x64 for 64 bit.
The weibull_distribution.m file contains the following code:
function WLAN= weibull_distribution(noncentrality,scale)
%use ProbDistUnivParam() function for matlab 2016
> %h=ProbDistUnivParam(\'weibull\',\[noncentrality,scale\]);
>
> %use makedist() function for matlab 2017
>
> h=makedist(\'weibull\',noncentrality,scale);
>
> i=random(h,1);
>
> WLAN=i;
 Create matlab folder inside working workspace bin_x64 folder
Figure 10‑21: Bin folder of x64 of current workspace
 Place the weibull_distribution.m file inside matlab folder.
Figure 10‑22: Place Weibull_distribution.m file inside matlab folder
 You will have to create a MATLAB_Interface.c file in the IEEE802_11 folder similar to the previous example.The functions fn_netsim_matlab_init() and fn_netsim_matlab_finish() will remain the same. Modify the function fn_netsim_matlab_run() that is part of MATLAB_Interface.c which was used in the previous example as shown below:
double fn_netsim_matlab_run()
{
//write your own implementation here
int weibull_noncentrality = 1, weibull_scale = 2;
netsim_matlab_send_ascii_command(buf);
sprintf_s(buf,BUFSIZ, \"k=weibull_distribution(%d,%d)\",
weibull_noncentrality, weibull_scale);
netsim_matlab_send_ascii_command( buf);
netsim_matlab_get_value(buf, BUFSIZ, \"k\", \"double\");
double result = atof(buf);
return result;
}

Follow the similar steps 2 to 17 as explained in the section "Implement Weibull Distribution of MATLAB in NetSim without using .m file".

You will find that once the Simulation is run MATLAB Command Window starts to execute and dfading power value will be printed based on the weibull_distribution.m file.
Figure 10‑23: Runtime MATLAB interfacing window with k value
NetSimMATLAB COM Interface#
NetSimMATLAB COM Interface was introduced in NetSim v9. This allows runtime interaction between NetSim and MATLAB taking advantage of rich MATLAB Engine and matrix API's.
MATLAB functions can be called from NetSim\'s underlying protocol C source codes using MATLAB APIs. Following is some of the MATLAB Engine API functions that can be used from NetSim C source codes:
MATLAB Engine API Functions  Description 

Engine  Type for MATLAB engine 
engOpen  Start MATLAB engine session 
engOpenSingleUse  Start MATLAB engine session for single, nonshared use 
engClose  Quit MATLAB engine session 
engEvalString  Evaluate expression in string 
engGetVariable  Copy variable from MATLAB engine workspace 
engPutVariable  Put variable into MATLAB engine workspace 
engGetVisible  Determine visibility of MATLAB engine session 
engSetVisible  Show or hide MATLAB engine session 
engOutputBuffer  Specify buffer for MATLAB output 
Table 10‑3: MATLAB Engine API functions
In addition to these API's there are several C Matrix API's of MATLAB that can be used.
Prerequisites for NetSimMATLAB COM Interface#

MATLAB Interfacing requires an installed version of MATLAB. Engine API functions cannot be run on a machine that only has the MATLAB Runtime.

Both NetSim and MATLAB should use the same build; 64bit.

Make sure that the following path is added to the environment variable PATH:
\<Path where MATLAB is installed>\bin\win64

If the machine has more than one MATLAB installed, the directory for the target platform must be ahead of any other MATLAB directory (for instance, when compiling a 64bit application, the directory in the MATLAB 64bit installation must be the first one on the PATH).

Registration of MATLAB as a COM server by one of the following methods
 Start Command Prompt as administrator and execute the following command:
matlab regserver
Note:

If you have multiple versions of MATLAB installed on your computer, the best practice is to run the matlab command from the matlabroot folder.

Enter the following command in the command line of MATLAB version that you want to interface with NetSim:
matlab regserver
Implement Weibull Distribution of MATLAB without using .m file#
In this example we will replace the default Rayleigh Fading (part of the path loss calculation) used in NetSim, with a Fading Power calculated using the Weibull Distribution from MATLAB.
Note: This example uses 64bit version of NetSim and MATLAB.
Procedure:
 Create a MATLAB_Interface.c file inside the IEEE802_11 folder which can be found in the current workspace location of NetSim that you are running, and it would be something like "C:\Users\PC\Documents\NetSim\Workspaces\\<Your default workspace>\src\Simulation\
IEEE802_11" For more information on NetSim workspace refer Section 4 "Workspaces and Experiments". Write the following code inside the MATLAB_Interface.c file:
/\*
\*
\* This is a simple program that illustrates how to call the MATLAB
\* Engine functions from NetSim C Code.
\*
\*/
#include\<windows.h\
#include\<stdlib.h\
#include\<stdio.h\
#include\<string.h\
#include\"engine.h\"
#include\"mat.h\"
#include\"mex.h\"
#include\"main.h\"
char buf\[BUFSIZ\];
Engine \*ep;
int status;
mxArray \*h = NULL, \*i = NULL, \*j = NULL, \*k = NULL;
mxArray \*out;
double \*result;
double fn_netsim_matlab_init()
{
/\*
\* Start the MATLAB engine
\*/
fprintf(stderr, \"\\nPress any key to Initialize MATLAB\\n\");
\_getch();
if(!(ep = engOpen(NULL))) {
MessageBox((HWND)NULL, (LPCWSTR)\"Can\'t start MATLAB engine\",
(LPCWSTR) \"MATLAB_Interface.c\", MB_OK);
exit(1);
}
engEvalString(ep, \"desktop\");
return 0;
}
double fn_netsim_matlab_run()
{
//write your own implementation here
int weibull_noncentrality = 1, weibull_scale = 2;
engPutVariable(ep, \"h\", h);
//use ProbDistUnivParam() function for matlab 2016
/\*sprintf_s(buf, BUFSIZ, \"h=ProbDistUnivParam(\'weibull\',\[%d
%d\])\", weibull_noncentrality, weibull_scale);\*/
//use makedist() function for matlab 2017 or higher
sprintf_s(buf, BUFSIZ, \"h=makedist(\'weibull\',%d,%d)\",
weibull_noncentrality, weibull_scale);
status = engEvalString(ep, buf);
engPutVariable(ep, \"i\", i);
sprintf_s(buf, BUFSIZ, \"i=random(h,1)\");
status = engEvalString(ep, buf);
out = engGetVariable(ep, \"i\");
result = mxGetPr(out);
return \*result;
}
double fn_netsim_matlab_finish()
{
fprintf(stderr, \"\\nPress any key to close MATLAB\\n\");
\_getch();
status = engEvalString(ep, \"exit\");
return 0;
}
Figure 10‑24: Create a MATLAB_Interface.c inside the IEEE802_11 folder

Now open the code and you can configure Visual studio platform to x64 to build 64 bit Dll files.

Right click on "IEEE802_11 Project" present in "Solution Explorer" window and select Add à Existing Item and select the MATLAB_Interface.c file.

MATLAB_Interface.c file contains the following functions.
i. fn_netsim_matlab_init()  Opens the MATLAB Engine
ii. fn_netsim_matlab_run()  Communicates with MATLAB Command Window
iii. fn_netsim_matlab_finish()  Closes the MATLAB Engine
 In the Solution Explorer under IEEE802.11 project Double click on the IEEE802_11.c file
Figure 10‑25: Solution Explorer double click on the IEEE802_11.c file
 Add a call to fn_netsim_matlab_init(); inside the fn_NetSim_IEEE802_11_Init() function.
Figure 10‑26: Added a fn_netsim_matlab_init(); inside the fn_NetSim_IEEE802_11_Init() function
 Similarly add a call to fn_netsim_matlab_finish(); inside the fn_NetSim_IEEE802_11_
Finish() function.
Figure 10‑27: Added a fn_netsim_matlab_finish(); inside the fn_NetSim_IEEE802_11_Finish() function
 In the Solution Explorer under IEEE802_11 project Double click on the IEEE802_11.h file. Add definitions of the following functions.
double fn_netsim_matlab_init();
double fn_netsim_matlab_run();
double fn_netsim_matlab_finish();
Figure 10‑28: Added Matlab definitions in IEEE802_11.h file

In the Solution Explorer under IEEE802_11 project Double click on the IEEE802_11_PHY.c file

Inside fn_Netsim_IEEE802_11_PHYIn () function comment the lines,
dFadingPower = propagation_calculate_fadingloss(propagationHandle,
packet\>nTransmitterId,ifid,pstruEventDetails\>nDeviceId,
pstruEventDetails\>nInterfaceId);
 Make a call to the fn_netsim_matlab_run() function by adding the following line,
dFadingPower = fn_netsim_matlab_run();
Figure 10‑29: Call to the fn_netsim_matlab_run() function
 To compile a MATLAB engine application in the Microsoft Visual Studio (2019) environment, Right click on the IEEE802_11 project and select PROPERTIES in the solution explorer. Once this window has opened, make the following changes:
Figure 10‑30: Right click on the IEEE802_11 project and select Properties

Under C/C++ à General, add the following directory to the field ADDITIONAL INCLUDE DIRECTORIES:
\<Path where MATLAB is installed>\extern\include
NOTE: To determine path where MATLAB is installed, entering the following command in the MATLAB command prompt:
matlabroot
Figure 10‑31: MATLAB Command Prompt
Figure 10‑32: Determine MATLAB Path in Additional Include Directories
 Under C/C++ à Precompiled Headers, set PRECOMPILED HEADERS as \"Not Using Precompiled Headers\".
Figure 10‑33: Select Precompiled Header as "Not Using Precompiled Headers"
 Under Linker à General, add the directory to the field ADDITIONAL LIBRARY DIRECTORIES:
\<Path where MATLAB is installed>\extern\lib\win64\microsoft
Figure 10‑34: SetAdditional Library Directories to \<Path where MATLAB is installed>\extern\lib\win64\microsoft
 Under Configuration Properties àDebugging, Add the following Target path in the ENVIRONMENT: \<Path where MATLAB is installed>\bin\win64
Figure 10‑35: Set Environment to \<Path where MATLAB is installed>\bin\win64
 Under Linker à Input, add the following names to the field marked ADDITIONAL DEPENDENCIES: libeng.lib;libmx.lib;libmat.lib; and Click on Apply and OK.
Figure 10‑36: Set Additional Dependencies as libeng.lib;libmx.lib;libmat.lib;
 Now Right Click on IEEE802_11 project and select Rebuild.
Note:
 Before Rebuilding make sure you have access to IEEE802.11 folder, if the access denied while rebuilding go to
\"C:\Users\PC\Documents\NetSim\Workspaces\\<Yourdefaultworkspace>\src\Simulation\IEEE802_11\" path..
 Right click on IEEE802.11 folder, select on properties and deselect \"Read only\" attribute and click on apply.
Figure 10‑37: Rebuild IEEE802_11 project
 A new libIEEE802.11.dll gets created in the bin_x64 folder of NetSim's current workspace path
\<C:\Users\PC\Documents\NetSim\Workspaces\\<Your default workspace>\bin_x64>.
For more information, follow steps provided in Section 9.1 "Writing your own code".
 Run NetSim in Administrative mode. Create a Network scenario involving IEEE802_11 say MANET, right click on the Adhoc link and select properties. Make sure that the Channel Characteristics is set to PathLoss and Fading and Shadowing.
Figure 10‑38: Wireless Link Properties Window
 Perform Simulation. You will be observed to initiate the MATLAB in NetSim console window After clicking on enter, MATLAB command window starts and gets closed once the simulation is over.
Debug and understand communication between NetSim and MATLAB#
 In the Solution Explorer under IEEE802_11 project double click on MATLAB_Interface.c file and place a breakpoint inside the fn_netsim_matlab_run() function before the return statement as shown in below screenshot.
Figure 10‑39: Place a breakpoint inside the fn_netsim_matlab_run() function

Rebuild the code.

Now run the NetSim Scenario. The simulation window stops for user interrupt.

In Visual studio, go to Debug à Attach to Process.

From the list of Processes select NetSimCore.exe and click on Attach.
Figure 10‑40: Select NetSimCore.exe in Attach to Process Window

Now go to the Simulation window and press Enter.

MATLAB Command Window and MATLAB Desktop Window will start and breakpoint in Visual Studio gets triggered.
Figure 10‑41: Once Simulation Start and breakpoint gets triggered in Visual Studio
 Now when debugging (say, by pressing F5 each time) you will find the computation taking place in the MATLAB Workspace.
Figure 10‑42: MATLAB Workspace

This value of i obtained from MATLAB is used to calculate fading power instead of the already available models in NetSim.

Now place another breakpoint after the line dFadingPower = fn_netsim_matlab_run()
Figure 10‑43: Added Another breakpoint after the line dFadingPower = fn_netsim_matlab_run()
 Add the variable dFadingPower in IEEE802_11_Phy.c file, to watch. For this, right click on the variable dFadingPower and select "Add Watch" option. You will find a watch window containing the variable name and its value in the bottom left corner.
Figure 10‑44: Right Click on dFadingPower and Select "Add Watch" option
 Now when debugging (say by pressing F5 each time) you will find that the watch window displays the value of dFadingPower whenever the control reaches the recently set breakpoint. You will also find that the value of dFadingPower in the Visual Studio Watch window and the value of i in the MATLAB workspace window are similar.
Figure 10‑45: The Visual Studio Watch window and the value of i in the MATLAB workspace window are similar.
Implement Weibull Distribution of MATLAB in NetSim using .m file:#
Procedure:
 Create a file named weibull_distribution.m file inside \<Path where MATLAB is installed>. The weibull_distribution.m file contains the following code:
> function WLAN= weibull_distribution(noncentrality,scale)
>
> %use ProbDistUnivParam() function for matlab 2016
>
> %h=ProbDistUnivParam(\'weibull\',\[noncentrality,scale\]);
>
> %use makedist() function for matlab 2017
>
> h=makedist(\'weibull\',noncentrality,scale);
i=random(h,1);
> WLAN=i;
 Place this file in the MATLAB's default working directory. This will usually be MATLAB's root directory or the bin folder in MATLAB's installation path.
NOTE: To determine path where MATLAB is installed, entering the following command in the MATLAB command prompt:
matlabroot
Figure 10‑46: MATLAB Command Prompt
 Execute the below command in MATLAB command window to clear cache.
> *restoredefaultpath % This will remove any custom paths*
>
> *rehash toolboxcache*
>
> *savepath*
Figure 10‑47: MATLAB command window to clear cache
 You will have to create a MATLAB_Interface.c file in the IEEE802_11 folder similar to the previous example. The functions fn_netsim_matlab_init() and fn_netsim_matlab_finish() will remain the same. Modify the function fn_netsim_matlab_run() that is part of MATLAB_Interface.c which was used in the previous example as shown below:
> double fn_netsim_matlab_run()
>
> {
>
> //write your own implementation here
>
> int weibull_noncentrality = 1, weibull_scale = 2;
>
> engPutVariable(ep, \"h\", h);
>
> sprintf_s(buf,BUFSIZ, \"k=weibull_distribution(%d,%d)\",
> weibull_noncentrality, weibull_scale);
>
> status = engEvalString(ep, buf);
>
> out = engGetVariable(ep, \"k\");
>
> result = mxGetPr(out);
>
> return \*result;
>
> }

Follow steps 2 to 17 as explained in the section "Implement Weibull Distribution of MATLAB in NetSim without using .m file" above.

A call to the weibull_distribution () function inside the weibull_distribution.m file is made, and weibull_noncentrality and weibull_scale parameters are passed from NetSim.

Right Click on IEEE802_11 project and select Rebuild.

A new libIEEE802.11.dll will get created in the bin_x64 folder of NetSim's current workspace path \<C:\Users\PC\Documents\NetSim\Workspaces\\<Your default workspace>\bin_x64> for 64bit.
Open NetSim in Administrative mode. Create a Network scenario involving IEEE802_11 say MANET, right click on the properties, make sure that the Channel Characteristics is set to PathLoss and Fading and Shadowing.
 You will find that once the Simulation is run MATLAB Command Window starts and gets closed once the Simulation is over. You can also debug the code to understand the communication between NetSim and MATLAB as explained in the DEBUGGING section above.
Plot a histogram in MATLAB per a Weibull distribution (using .m file)#
Procedure:
 Create a file NETSIM_MATLAB.m file containing the following code, Place this file in the MATLAB's default working directory. This will usually be MATLAB's root directory or the bin folder in MATLAB's installation path.
Figure 10‑48: MATLAB's default working directory path
function WLAN=NETSIM_MATLAB(choice,varargin)
switch(choice)
case\'weibull\'
%use ProbDistUnivParam function for matlab 2016
%h=ProbDistUnivParam(\'weibull\',\[varargin{1},varargin{2}\]);
%use makedist function for matlab 2017
h=makedist(\'weibull\',varargin{1}, varargin{2});
logpath=strcat(varargin{3},\'\\plotvalues.txt\');
i=random(h,1);
fid = fopen(logpath,\'a+\');
fprintf(fid,\'%f\',i);
fprintf(fid,\'\\r\\n\');
fclose(\'all\');
WLAN=i;
case\'plothistogram\'
logpath=strcat(varargin{1},\'\\plotvalues.txt\');
fid=fopen(logpath);
mx=fscanf(fid,\'%f\');
hist(mx);
fclose(\'all\');
WLAN=0;
end
 Modify the function fn_netsim_matlab_run() that is part of MATLAB_Interface.c which was used in the previous example.
double fn_netsim_matlab_run(char\* arr)
{
//write your own implementation here
int weibull_noncentrality = 1, weibull_scale = 2;
if (strcmp(arr, \"weibull\") == 0)
{
engPutVariable(ep, \"h\", h);
//engPutVariable(ep, \"logpath=%s\", pszIOLogPath);
//sprintf_s(buf, BUFSIZ, \"logpath=%s\", pszIOLogPath);
//status = engEvalString(ep, buf);
sprintf_s(buf, BUFSIZ, \"h=NETSIM_MATLAB(\'weibull\',%d,%d,\'%s\')\",
weibull_noncentrality, weibull_scale, pszIOLogPath);
status = engEvalString(ep, buf);
out = engGetVariable(ep, \"h\");
result = mxGetPr(out);
return \*result;
}
else if (strcmp(arr, \"plothistogram\") == 0)
{
//engPutVariable(ep, \"logpath=%s\", pszIOLogPath);
sprintf(buf, \"NETSIM_MATLAB(\'plothistogram\',\'%s\')\", pszIOLogPath);
engEvalString(ep, buf);
status = engEvalString(ep, buf);
return 0;
}
else
return 0;
}
Follow steps 2 to 17 as explained in the section on "Implement weibull Distribution of MATLAB in NetSim without using .m file" above.

A call to the NetSim_MATLAB() function inside the NetSim_MATLAB.m file is made, for fading power calculation with parameters distribution('weibull'), weibull_noncentrality and weibull_scale parameters are passed from NetSim.

A call to the NetSim_MATLAB() function inside the NetSim_MATLAB.m file is made, for plotting histogram for the values generated by MATLAB.

Also add the following call to fn_netsim_matlab_run() function to plot the histogram before closing the MATLAB Engine.
Figure 10‑49: Added the parameter \"plothistogram\" in fn_netsim_matlab_run() function to get histograms
 Similarly in the call made to fn_netsim_matlab_run() function in IEEE802_11_Phy.c file add the parameter "weibull" as shown below.
Figure 10‑50: Added the parameter "weibull" in fn_netsim_matlab_run() function in IEEE802_11_Phy.c file
 Also modify the function definition of fn_netsim_matlab_run() function in IEEE802_11.h file as shown below:
Figure 10‑51: Modify the Function definition of fn_netsim_matlab_run() function in IEEE802_11.h file
 Right Click on IEEE802_11 project and select Rebuild will create a new libIEEE802.11. in the bin_x64 folder of NetSim's current workspace path
\< C:\Users\PC\Documents\NetSim\Workspaces\\<Your default workspace>\bin_x64 > for 64bit.

Open NetSim in Administrative mode. Create a Network scenario involving IEEE802_11 say MANET, Right click on Adhoc link or Wireless link. Make sure that the Channel Characteristics is set to PathLoss and Fading and Shadowing.

You will find that once the Simulation is run MATLAB Command Window starts and once the Simulation is over a histogram is displayed in MATLAB for the values that were generated using weibull distribution.
Figure 10‑52: Histogram plot is displayed in MATLAB
 The graph and the MATLAB windows get closed once you press any key.
You can also debug the code to understand the communication between NetSim and MATLAB as explained in the DEBUGGING section above.
Interfacing tail with NetSim#
What is a tail command?
The tail command is a commandline utility for outputting the last part of files given to it via standard input. It writes results to standard output. By default, tail returns the last ten lines of each file that it is given. It may also be used to follow a file in realtime and watch as new lines are written to it.
PART 1:
Tail options
 The following command is used to log the file.
tail \" path_to_file \" f
where f option is used to watch a file for changes with the tail command pass the f option. This will show the last ten lines of a file and will update when new lines are added. This is commonly used to watch log files in realtime. As new lines are written to the log the console will update will new lines.
 If users do not want the last ten lines of the file, then use the following command.
tail n 0 \" path_to_file \" f
where n option is used to show the last n number of lines.
 If you want to open more than 1 file then use the following command
tail n 0 \" path_to_file \" \" path_to_file \" f
PART 2:
Steps to log NetSim files using tail console.
Note: Before Executing below steps, User need to generate/Simulate a scenario of a network to which tail command is being executed.

Open command window from Install directory path of Netsim (\<C:\Program Files\NetSim\Pro_v13_1\bin) which contains tail.exe

Type the following command to open ospf_hello.log.txt file and press enter.
tail n 0 "\<NetSim_IOPath>\log\ospf_hello.log" f
For example,
tail n 0 \"C:\Users\Admin\AppData\Roaming\NetSim\pro_13.1\log\ospf_hello.log\" f
Figure 10‑53: Enter the ospf_hello.log.txt file path is Command Prompt
 Open solution file and add the following line in fn_NetSim_OSPF_Init() function in ospf.c file present inside OSPF project
Figure 10‑54: Add the following line in fn_NetSim_OSPF_Init() function in ospf.c file present inside OSPF project

Rebuild the project.

Upon rebuilding, libOSPF.dll will get created in the bin folder of NetSim's current workspace path \<C:\Users\PC\Documents\NetSim\Workspaces\\<Your default workspace>\bin_x64 > for 64bit

Create a scenario in NetSim as per the screenshot below and run simulation.
Figure 10‑55: Network Topology
 In the console window user would get a warning message shown in the below screenshot Figure 10‑56 (because of changed DLL) and then the simulation will pause for user input (because of _getch() added in the init function)
Figure 10‑56: Modified Project DLL Warning Message in NetSim Console

In Visual Studio, put break point inside all the functions in OSPF_Hello.c file present inside OSPF project.

Go to "Debug>Attach to Process" in Visual studio and attach to NetSimCore.exe.

Press enter in the command window. Then control goes to the project and stops at the break point in the source code as shown below Figure 10‑57.
Figure 10‑57: Control goes to the project and stops at the break point in the source code.

Once after pressing enter in command window, check the tail console to watch the ospf_hello.log would look like the following screenshot Figure 10‑57.

Below Screenshot shows that scheduling time of hello interval for each Routers connected in Network.
Figure 10‑58: Scheduling hello interval for Routers.
Figure 10‑59: Adding new neighbor and terminating process if it is 1way event
 Above Screenshot shows of adding neighbor to hello message, at Time 0.0101 ms, Router 5 interface 1 (11.4.1.2) received hello msg from neighbor 11.2.1.1.
Figure 10‑60: Adding Neighbor and Performing 2way Event if neighbor is present in Router's list.
Above Screenshot indicates the Router 2's interface 1 (11.2.1.2) has received hello message from neighbor 11.2.1.1, if neighbor is present in Router's list 2way event will be performed.

Similarly, users can debug the code and observe how the OSPF tables get filled.

Users can also open multiple files by using the command given in Part 1.
Adding Custom Performance Metrics#
NetSim allows users to add additional metrics tables to the Simulation Results window in addition to the default set of tables that are available at the end of any simulation. This can be achieved by editing the source codes of the respective protocol.
General format to add custom metrics in Result window:
Every protocol has a main C file which contains a Metrics () function. For E.g., TCP project will have a TCP.c file, UDP will have an UDP.c file etc. In the following example we have added a new table as part of TCP protocol. TCP.c file contains fn_NetSim_TCP_Metrics() function where code related to custom metrics is added as shown below:
_declspec(dllexport) int fn_NetSim_TCP_Metrics(PMETRICSWRITER metricsWriter)
{
//CUSTOM METRICS
> //Set table name
>
> PMETRICSNODE table = init_metrics_node(MetricsNode_Table, \"CUSTOM
> METRICS\", NULL);
>
> //set table headers
>
> add_table_heading(table, \"COLUMN_HEADER_1\", true, 0);
>
> add_table_heading(table, \"COLUMN_HEADER_2\", false, 0);
>
> //Add table data
>
> add_table_row_formatted(false, table, \"%s,%s,\",
> \"ROW_DATA1\",\"ROW_DATA2\");
>
> PMETRICSNODE menu =
> init_metrics_node(MetricsNode_Menu,\"CUSTOM_METRICS\", NULL);
>
> //Add table to menu
>
> add_node_to_menu(menu, table);
>
> //Write to Metrics file
>
> write_metrics_node(metricsWriter, WriterPosition_Current, NULL, menu);
>
> delete_metrics_node(menu);
>
> //CUSTOM METRICS
>
> return fn_NetSim_TCP_Metrics_F(metricsWriter);
>
> }
Figure 10‑61: Added Custom metrics table to the Simulation Results window
For illustration, an example regarding Wireless Sensor Network is provided. In this example, parameters such as Sensor Node Name, Residual Energy, State (On/Off) and turnoff time are tracked and added to a new table in the Simulation Results window.
Refer Section 9.1 on writing your own code, for more information.
After loading the source codes in Visual Studio, perform the following modifications:
Step 1: Copy the provided code at the top in 802_15_4.h file.
double NetSim_Off_Time[100]; //Supports upto Device ID 100. Array size can be increased for higher number of Devices/Device ID\'s
Step 2:
Add the header file in 802_15_4.c file.
#include \"../BatteryModel/BatteryModel.h\"
Step 3:
Copy the below code (in red colour) in 802_15_4.c file (inside fn_NetSim_Zigbee_Metrics() function)
/** This function write the metrics in metrics.txt */
\_declspec(dllexport) int
fn_NetSim_Zigbee_Metrics(PMETRICSWRITERmetricsWriter)
{
//CUSTOM METRICS
ptrIEEE802_15_4\_PHY_VAR phy;
ptrBATTERY battery;
char radiostate\[BUFSIZ\];
NETSIM_ID nDeviceCount = NETWORK\>nDeviceCount;
//Set table name
PMETRICSNODE table = init_metrics_node(MetricsNode_Table,
\"NODE_FAILURE_METRICS\", NULL);
//set table headers
add_table_heading(table, \"Node Name\", true, 0);
add_table_heading(table, \"Status(ON/OFF)\", true, 0);
add_table_heading(table, \"Residual_Energy (mJ)\", true, 0);
add_table_heading(table, \"Time  Turned OFF (microseconds)\",
false, 0);
for (int i = 1; i \<= nDeviceCount; i++)
{
sprintf(radiostate, \"ON\");
phy = WSN_PHY(i);
if (strcmp(DEVICE(i)\>type, \"SENSOR\"))
continue;
if (WSN_MAC(i)\>nNodeStatus == 5 \\
phy\>nRadioState==RX_OFF)
sprintf(radiostate, \"OFF\");
//Add table data
add_table_row_formatted(false, table, \"%s,%s,%.2lf,%.2lf,\",
DEVICE_NAME(i), radiostate,
battery_get_remaining_energy((ptrBATTERY)phy\>battery),
NetSim_Off_Time\[i\]);
}
PMETRICSNODE menu = init_metrics_node(MetricsNode_Menu,
\"CUSTOM_METRICS\", NULL);
add_node_to_menu(menu, table);
write_metrics_node(metricsWriter, WriterPosition_Current, NULL,
menu);
delete_metrics_node(menu);
//CUSTOM METRICS
return fn_NetSim_Zigbee_Metrics_F(metricsWriter);
}
**Step 4:**
Copy the below code (in red colour) at the end of ChangeRadioState.c
file.
if(isChange)
{
phy\>nOldState = nOldState;
phy\>nRadioState = nNewState;
}
else
{
phy\>nRadioState = RX_OFF;
WSN_MAC(nDeviceId)\>nNodeStatus = OFF;
> NetSim_Off_Time\[nDeviceId\] = ldEventTime;
}
return isChange;
}
Step 5:
Build DLL with the modified code and run a Wireless Sensor Network scenario. After Simulation, user will notice a new Performance metrics named "Custom Metrics" is added. The newly added NODE_FAILURE_METRICS table is shown below Figure 10‑62.
Figure 10‑62: Added Custom metrics table to the Simulation Results window
Simulation Time and its relation to Real Time (Wall clock)#
The notion of time in a simulation is not directly related to the actual time that it takes to run a simulation (as measured by a wallclock or the computer\'s own clock), but is a variable maintained by the simulation program. NetSim uses a virtual clock which ticks virtual time. Virtual time starts from zero progresses as a positive real number.
Time is as a global parameter. All components of the network share the same time throughout the simulation independently of where they are physically located or how they are logically connected to the network. There are not differences among the local time of the communication network components.
This virtual time is referred to as simulation time to clearly distinguish it from real (wallclock) time. NetSim is a discrete event simulator (DES), and in any DES, the progression of the model over simulation time is decomposed into individual events where change can take place. The flow of time is only between events and is not continuous. Therefore, simulation time is not allowed to progress during an event, but only between events. In fact, the simulation time is always equal to the time at which the current event occurs. Thus, simulation time can be viewed as a variable that \"jumps\" to track the time specified for each new event.
The answer to the question \"Will NetSim run for 10 seconds if Simulation time is set to 10 sec?\" is, the simulation may take more than 10 seconds (Wall clock) if the network scenario is very large and heavy traffic load. It may take a much shorter time (wall clock) for small networks with low traffic loads.
Note that when running in \"Emulation mode\" simulation time and wall clock will be exactly synchronized since it involves the transfer of real packets across the virtual network in NetSim.
In NetSim, the current simulation time can be got using pstruEventDetails>dEventTime
Adding Custom Plots#
NetSim's plot option can be used to obtain Link and application throughput plots, which can be accessed from the results dashboard after the simulation. In addition, TCP Congestion Window plots and Buffer occupancy plots can be obtained by enabling the respective option in the device properties.
Users can also log additional parameters with respect to time and get them plotted in NetSim results dashboard. Following are some of the API's which are part of NetSim_Plots.h file that can be used for this purpose:

fn_NetSim_Install_Metrics_Plot()  This function creates a plot log file and returns a value of type PNETSIMPLOT which can be stored in a pointer and be used for adding values using add_plot_data_formatted(). This function can generally be called at simulation start. This function can be called one time for each plot that is to be generated.
For Eg: If a plot is to be generated for each node. Then this function needs to be called the number of device times.

add_plot_data_formatted()  This function can be used to add values to the plot log created using the call to fn_NetSim_Install_Metrics_Plot(). This function needs to be called each time you want to add new values to the plot log file. The call should be made at appropriate section of code where the value being plotted changes with time.
Plotting SNR for each UEgNB pair in 5G NR#
SNR measured by UE's from each gNB can be logged and plotted as part of NetSim results window without having to use additional tools. Following is one such example where we log the SNR for each UEgNB pair and obtain plots at the end of the simulation.
Step 1: Open NetSim source code in NetSim current workspace. For more information, please refer section "4.12 How does a user open and modify source codes".
Step 2: Go to LTE_NR project through the solution explorer and open the LTE_NR.c file. In the function fn_NetSim_LTE_NR_Init(), modify code as shown below:
\_declspec(dllexport) int fn_NetSim_LTE_NR_Init()
{
//custom plot
int ret = fn_NetSim_LTE_NR_Init_F();
for (NETSIM_ID r = 0; r \< NETWORK\>nDeviceCount; r++)
{
for (NETSIM_ID rin = 0; rin \< DEVICE(r + 1)\>nNumOfInterface; rin++)
{
if (!isLTE_NRInterface(r + 1, rin + 1))
continue;
ptrLTENR_PROTODATA data = LTENR_PROTODATA_GET(r + 1, rin + 1);
switch (data\>deviceType)
{
case LTENR_DEVICETYPE_UE:
{
for (NETSIM_ID r1 = 0; r1 \< NETWORK\>nDeviceCount; r1++)
{
for (NETSIM_ID rin1 = 0; rin1 \< DEVICE(r1 + 1)\>nNumOfInterface;
rin1++)
{
if (!isLTE_NRInterface(r1 + 1, rin1 + 1))
continue;
ptrLTENR_PROTODATA data = LTENR_PROTODATA_GET(r1 + 1, rin1 + 1);
switch (data\>deviceType)
{
case LTENR_DEVICETYPE_GNB:
{
char heading\[BUFSIZ\], plotname\[BUFSIZ\];
sprintf(heading, \"UE\_%d_GNB\_%d_SNR\", r + 1, r1 + 1);
sprintf(plotname, \"plot_UE\_%d_GNB\_%d_SNR\", r + 1, r1 + 1);
fn_NetSim_Install_Metrics_Plot(Plot_Custom, \"LTE_NR SNR Plot\",
heading, \"SNR(dB)\", 1, plotname);
}
break;
default:
break;
}
}
}
break;
default:
break;
}
break;
}
}
}
return ret;
//custom plot
}
Step 3: In the file LTENR_GNBRRC.c go to the function fn_NetSim_LTENR_RRC_GENERATE_UE_MEASUREMENT_REPORT() and add the lines of code highlighted in red as shown below:
void fn_NetSIM_LTENR_RRC_GENERATE_UE_MEASUREMENT_REPORT()
{
NETSIM_ID d = pstruEventDetails\>nDeviceId;
NETSIM_ID in = pstruEventDetails\>nInterfaceId;
ptrLTENR_UERRC ueRRC = LTENR_UERRC_GET(d, in);
ptrLTENR_GNBRRC gnbRRC = LTENR_GNBRRC_GET(ueRRC\>SelectedCellID,
ueRRC\>SelectedCellIF);
ptrLTENR_RRC_UE_MEASUREMENT_REPORT report = NULL;
ptrLTENR_RRC_UE_MEASUREMENT_REPORT temp = NULL;
ptrLTENR_GNBPHY phy = NULL;
for (NETSIM_ID r = 0; r \< NETWORK\>nDeviceCount; r++)
{
for (NETSIM_ID rin = 0; rin \< DEVICE(r + 1)\>nNumOfInterface; rin++)
{
if (!isLTE_NRInterface(r + 1, rin + 1))
continue;
ptrLTENR_PROTODATA data = LTENR_PROTODATA_GET(r + 1, rin + 1);
switch (data\>deviceType)
{
case LTENR_DEVICETYPE_GNB:
temp = MEASUREMENT_REPORT_ALLOC();
temp\>ueID = d;
temp\>cellID = r + 1;
temp\>cellIF = rin + 1;
temp\>rs_type = RS_TYPE_SSB;
temp\>reportAmount = ReportAmount_r1;
temp\>reportInteval = gnbRRC\>ueMeasReportInterval;
phy = LTENR_GNBPHY_GET(r + 1, rin + 1);
temp\>sinr = LTENR_PHY_RRC_RSRP_SINR(r + 1, rin + 1, d, in);
//custom plot
char plotname\[BUFSIZ\];
sprintf(plotname, \"%s\\\\plot_UE\_%d_GNB\_%d_SNR.txt\",pszIOPath, d,
r + 1);
FILE\* fp = fopen(plotname, \"a+\");
if (fp)
{
fprintf(fp, \"%lf,%lf\\n\",pstruEventDetails\>dEventTime,temp\>sinr);
fclose(fp);
}
//custom plot
LIST_ADD_LAST((void\*\*)&report, temp);
break;
default:
break;
}
}
}
ptrLTENR_RRC_Hdr hdr = calloc(1, sizeof \* hdr);
hdr\>msg = report;
hdr\>msgType = LTENR_MSG_RRC_UE_MEASUREMENT_REPORT;
hdr\>SenderID = d;
hdr\>SenderIF = in;
fn_NetSIm_LTENR_RRC_ADD_HDR_INTO_PACKET(pstruEventDetails\>pPacket,
hdr, ueMEASID, LTENR_MSG_RRC_UE_MEASUREMENT_REPORT);
LTENR_CallPDCPOut();
}
Step 4: In LTENR.c add the following lines #include \"NetSim_Plot.h\".
Step 5: Save the changes and rightclick on the LTE_NR module in the solution explorer and select Rebuild.
Step 6: Upon a successful build, NetSim will automatically update the modified binaries in the respective binary folder.
Step 7: Now on running any simulation in LTE/5G NR networks, you will get individual SNR plots for each UEGNB/UEENB pair, in the NetSim Metrics window under Plots >LTE_NR SNR Plot shown below Figure 10‑63.
Figure 10‑63: LTE_NR SNR Plots in Result Window
Figure 10‑64: SNR Plot
The above results are based on the Handover in 5G NR Experiment which is part of NetSim v13.1 experiment manual. The plot shows how the SNR drops as UE 3 moves away from GNB 1.
Environment Variables in NetSim#

NETSIM_PACKET_FILTER = \<filter_string> //used by NetSim developers to debug. Emulator code to passes filter string to windivert. See windivert doc for more information.

NETSIM_EMULATOR_LOG = \<log_file_path> // Used by Real time sync function to log get event and add event. Used by NetSim developers to debug.

NETSIM_EMULATOR = 1 // Set by application dll or user to notify NetSim internal modules to run in emulation mode

NETSIM_CUSTOM_EMULATOR = 1 // To notify NetworkStack to not load emulation dll and to only do time sync.

NETSIM_SIM_AREA_X = \<int> // Area used by Mobility functions for movement of device. Set by config file parser or user.

NETSIM_SIM_AREA_Y = \<int> // Same as above

NETSIM_ERROR_MODE = 1 // if set then windows won\'t popup gui screen for error reporting on exception.

NETSIM_BREAK = \<int> // Event id at which simulation will break and wait for user input.
Equivalent to d command in CLI mode.

NETSIM_AUTO = \<int> // If set NetSim will not ask for keypress after simulation. //Useful to run batch simulations.

NETSIM_IO_PATH = \<path> // IO path of NetSim from where it will read Config file and write output file. Equivalent to IOPATH command in CLI mode.

NETSIM_MAP = 1 // Set by Networkstack to inform other modules that simulation is running per map view.

NETSIM_ADVANCE_METRIC // If set, NetSim provides a set of extra metrics.
In application metrics, you can see duplicate packets received.

NETSIM_CONFIG_NAME = \<FILE NAME> // Config file name. This file must present in IOPath. If not set default value is Configuration.netsim

NETSIM_NEG_ID = 1 // If set, then control packets will have negative id.

NETSIM_PACKET_DBG = 1 // If set, then Simulation engine will log the packet creation and freeing

NETSIM_MEMCHECK = 1 // If set, then simulation will enable memory check.

NETSIM_MEMCHECK_1 = x // Lower event id

NETSIM_MEMCHECK_2 = x // Upper event id
Best practices for running large scale simulations#
As we scale simulations, the number of events processed and the memory consumed increase. Simulation scale can be defined in terms of:

Number of Nodes

End nodes

Intermediate devices

Total traffic in the network

Number of traffic sources

Average generation rate per source

Simulation time
The simulators performance is additionally affected by:

Protocols running

Network Parameters such as Topology, Mobility, Wireless Channel etc.

Enabling/Disabling  Animation, Plots, Traces and Logs

External Interfacing  MATLAB, Wireshark, SUMO
NetSim GUI limitation on total number of Nodes is as follows:

NetSim Academic  100

NetSim Standard  500

NetSim Professional  No software limit
Recommended best practices for running large scale simulations are:

Run 64bit Build of NetSim

Use the latest Windows 10 Build.

Use a system running a highend processor with minimum 32 GB RAM

Disable Animation  NetSim writes one file per node and windows > limits the number of simultaneously opened files to 512.

Disable plots, traces and logs to speed up the simulation.

If plots are enabled NetSim writes one file for each link and for each application. Therefore, it is recommended users only select those links/applications for which they wish to plot output performance. Plots for all other applications/links should be disabled.

NetSim writes one packet trace and one event trace file per simulation. If users wish to open this file in MSExcel, please note Excel's limit of 1 Million rows.

Packet trace and Event trace can be disabled to speed up the simulation.

Running simulations via CLI mode will save memory.
Batch experimentation and automated simulations#
NetSim Batch Automation allows users to execute a series of simulations without manual intervention. Consider a requirement, where a user wishes to create and simulate hundreds of network scenarios and store and analyse the performance metrics of each simulation run. It is impossible to do this manually using the GUI. This requirement can be met using NetSim Batch Automation script which runs NetSim via CLI.
A related requirement of advanced simulation users is a multiparameter sweep. When you sweep one or more parameters, you change their values between simulation runs, and compare and analyze the performance metrics from each run.
The documentation and codes for batch experimentation script is available TETCOS  https://www.tetcos.com/fileexchange.html