# A Simulation of Contact Graph Routing for Mars-Earth Data Communication
## 3 Algorithms of the Model  
We develop the program using MATLAB R2023b. The main program, procedures RoverSatelliteRouting and SatelliteAntennaRouting, are shown in Algorithms 1, 2, and 3, respectively. ```
1:Input:\(n,M,mxd\) the numbers of nodes and time intervals, and maximum length of data, resp. 2:Output:\(x\)
3:\(ids\leftarrow\) Empty array
4:access\(\leftarrow\) Open file\((accessmatrix)\)
5:\(ek,nek\leftarrow\)\(nchoosek\) and the length, respectively. 6:\(x\gets 2\times nek\times mxd\times M\) empty matrix
7:\(storsats\gets n\times M\) empty matrix
8:ids[1] = 0
9:for\(j=2\) to \(n-1\)do
10:\(ids[j]\gets ids[j-1]+(n-j+1)\)
11:endfor
12:\(data[i]\gets m\times 2^{k}\), where \(i=1,2,\ldots,M,m=randi([10,20])\) and \(k=randi([6,8])\) Generate data size for M minutes
13:for\(t=1\) to \(M\)do
14: Define \(rs[i,j],se[j,k],dly[j,k]\) for all \(i,j,k\leftarrow\) Open file\((ContactWindows)\)
15:if\(schedule=0\)then
16: Print \"all contact window is closed\"
17: Continue
18:endif
19:if\(data[t]>256\)then
20: Distribute a generated data into \(nd[t]\) packets @ 256, or \(dt[d,t],d=1,2,\ldots,nd[t]\)
21:endif
22:\(starts\leftarrow\) Choose randomly the sending rovers whether from PR or CR
23:Call Procedure RoverSatelliteRouting\((x,t,storsats,n_{d})\)
24:Call Procedure SatelliteAntennaRouting\((x,t,storsats,n_{d})\)
25:Call Procedure PlottingDirectedGrap\((x,t,n_{d})\)
26:endfor

Figure 1: Plot of the example. In Algorithm 1 Step 5, we use _nchoosek_ for evaluating Equation (1), where we look for the number of all possible edges/pairings between two nodes and their dimensions. Steps 8-11 define the variable _ids_ in Equation (3). Every time \(t\), the contact windows of each node are drawn from an external file named _ContactWindows_, which contains schedules such as in Table 4.

In Algorithm 2 Step 5, we sort the capacity of satellites, based on the access matrix and the contact windows, in descending order. The result is in vector \(strdcapsats\). The index of the satellite whose maximum capacity is _imxcap_, which is chosen as the relay satellite; see Step 10. If this satellite finally has full capacity, its index will be deleted from the variable _strdcapsat_; see Step 18. If the capacity is zero, it will begin from satellite 1, which is always zero. This phenomenon is used to check whether there is an index of open satellites still available in the vector \(strdcapsat\) or not; see Step 11. If there is an open connection between the chosen satellite and any DSN antenna, then the data will be stored in the satellite; see Step 16. In this procedure, the capacity of the satellite is always checked. If the sum of stored data sizes is greater than the capacity, the latest data sent to the satellite will be deleted. Satellite Odyssey has very limited capacity, so in case 4 of the simulation for one sol, the excess data can be seen as a loss. ```
1:Output:\(x,\)\(storsats\)
2:\(introve\gets 1:2\)
3:\(insat\gets 3:6\)
4:\(interth\gets 7:9\)
5:\(strdcapsats\leftarrow\) sort access
6:while\(d<n_{d}\)do
7: Increment \(d\gets d+1\)
8:\(i\leftarrowstarts[d]\)
9:if any \(rk[j,k]>0\)then
10:\(nxt=strdcapsats[i,imxcap]\)
11:if\(nxt\
eq 1\)then
12:if\(rs[i,nxt]>0\) and any \(se[nxt,k]>0,k=7,\ldots,9\)then
13: Check capacity of satellite-\(nxt\) with output \(accum\) and \(ok\)
14:if\(accum\leq C_{j}\)then
15:\(x[1,ids[i]+nxt-i,d,t]\gets dt[d,t],j=ids[i]+nxt-i\)
16:\(storsats[nxt,t+dly[j,k]]\gets dt[d,t]\)
17:else
18:\(strdcapsats[1:2,imxcap]\leftarrow[\ ]\)
19:\(imxcap\gets imxcap+1\) Increment the index of the sorted satellite
20:endif
21:\(imxcap\gets imxcap+1\) Increment the index of the sorted satellite
22:endif
23:else
24: Delete the newly arrived data
25:endif
26:endif
27:endif
28:endwhile
```
**Algorithm 2** Procedure RoverSatelliteRouting \((x,t,storsats,n_{d})\)

Procedure \(SatelliteAntennaRouting\) is written in Algorithm 3. This algorithm manages the deliveries of data at a delay time, and of data being stored in satellites due to the nonexistence of open contact windows to all antennas from the previous time.