# Narrowband-Internet of Things Device-to-Device Simulation: An Open-Sourced Framework
## 3 NB-IoT D2D Framework, Model, and Simulation
### Latency-Energy Model
\(D_{rrj}\) represents the closed formula for latency:  
\[E_{ra_{j}}=(\ \xi\ P_{t_{j}}+P_{c})c_{j}\tau+P_{l}(D_{ra_{j}}-c_{j}\tau) \tag{16}\]  
\[E_{rar_{j}}=D_{rar_{j}}\ \times\ P_{l} \tag{17}\]  
\[D_{rr_{j}}=\sum_{l=1}^{N_{max}}P_{jl}(1-P_{j})^{l-1}(D_{rarj}+D_{raj}) \tag{18}\]  
\[E_{rr_{j}}=\sum_{l=1}^{N_{max}}P_{j}(1-P_{j})^{l-1}(E_{rar_{j}}+E_{ra_{j}}). \tag{19}\]  
The latency expected in the system is denoted by \(D_{raj}\) and \(D_{rarj}\) and corresponds to the transmitted requests and the RAR, respectively. In light of having \(cj\) of the order of repetition in the coverage class of \(c_{1}=1\), \(c_{2}=2\), the average length (denoted by \(\tau\)) of the request signaling is 10 ms. The equations for \(D_{raj}\) and \(D_{rrj}\) are as follows:  
Figure 14:0  
Figure 14:1  
\(Q\) is the number of requests that the server can handle in the queue. This value determines the average wait time before the incoming RA request is handled and is represented by \(D_{w}\) :  
Figure 14:2Figure 14:3  
In the downlink channel (NPDCCH), the service time is denoted by \(D_{t}\). The time for control frame sending is \(u\), and the time for class \(j\) transmission is \(D_{tj}=cj\ \times\ u\):  
Figure 14:4  
If the class of node is represented by \(j\) and the orthogonal preambles in every \(t\) seconds are \(M_{j}\) having a content of average \(N_{j}\) CH nodes, then the equation corresponds to:  
Figure 14:5  
\(P_{jRACH}\) is written as:  
Figure 14:6  
The service time in the distribution function for the system \((F_{1}(x))\) and the total sum in time for \(n>1\) nodes where the unit step function is represented by \(H(x)\):  
Figure 14:7  
Figure 14:8  
The message (RAR) received with a probability of \(P_{jRAR}\) within \(T_{th}\) shows the number of queue requests that potentially need to be served by \(K\), as follows:  
Figure 14:9  
The uplink channel (NPUSCH) is the queue system that looks into the requests that are allocated in fraction \(w\). The value of \(w\) can be calculated as follows:  
Figure 13:0  
The NPUSCH can be modeled as a Batch Poisson Process (BPP), which results in the modelled arrival of requests for service to the NPUSCH because of the reservation of the NPRACH periods in the whole system. The mean size of batch \(\mathcal{G}\) is calculated as follows:  
Figure 13:1  
The time taken by the uplink packet transmission goes along a general distribution, with \(S_{a}\) and \(S_{b}\) as the first two moments. Figure 13:2Figure 13:3  
The average transmission rate of the uplink data for class \(j\) is denoted by \(Rj\). Thus, CHs send and receive the corresponding field data using NPUSCH or NPDSCH according to the following equations:  
Figure 13:4  
Figure 13:5  
Figure 13:6  
The downlink shared channel (NPDSCH) is modeled as a system for handling the queue wherein the server responds to all of the request messages by visiting them one at a time. The time when the NPBCH, NPDCCH, NSSS, and NPSS are not scheduled is a fraction represented by \(y\):  
Figure 13:7  
The mean size of batch \(g\) can be modelled as BPPs that are sent for service request by a downlink to the queue of NPDSCH:  
Figure 13:8  
If a general distribution of \(m_{a}\) and \(m_{b}\) is followed by the length of the packet, the initial two moments having a distribution of packet duration of transmission are \(h_{a}\) and \(h_{b}\) as follows:  
Figure 13:9  
\[G_{u}=Sp_{u}N, \tag{13}\]0  
\(\mathfrak{R}_{j}^{2}\) is the average downlink rate of data from class \(j\). The latency in received data \(D_{rxj}\) can be calculated as follows:  
\[G_{u}=Sp_{u}N, \tag{13}\]1  
\[G_{u}=Sp_{u}N, \tag{13}\]2  
The total expected latency \(D_{uj,D_{dj}}\) for an uplink/downlink service request of class \(j\) (class 1 for normal path loss and class 2 for extreme path loss) depends on the communication time duration of the consecutive scheduling of NPRACH is \(t\). The time duration of two consecutive schedulings of NPDCCH is \(td\).