-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimulator.py
2045 lines (1795 loc) · 70.1 KB
/
Simulator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Simulator REST API for CCI-SWIFT ASCENT
Created: Jan 29, 2023
Author/s: Rishabh Rastogi ([email protected]), Ta Seen Reaz Niloy ([email protected])
Advised by Dr. Eric Burger ([email protected]) and Dr. Vijay K Shah ([email protected])
For SWIFT-ASCENT
"""
# !/usr/bin/env python
import cmath
import json
import math
import os
import pickle
import random
from pathlib import Path
from typing import Tuple
import matplotlib
import matplotlib.pyplot as plt
import base64
import io
import numpy as np
import pandas as pd
from Geometry3D import *
from flask import Flask, request, jsonify
from matplotlib.lines import Line2D
from scipy import optimize
from shapely import geometry
from tqdm import tqdm
from weather import get_weather
import warnings
from matplotlib.offsetbox import AnchoredText
warnings.filterwarnings("ignore")
matplotlib.use('Agg')
def get_weather_json(latitude, longitude):
weather_info = get_weather(f"{latitude},{longitude}")
if weather_info:
print('Temperature:', weather_info['main']['temp'])
print('Humidity:', weather_info['main']['humidity'])
print('Weather:', weather_info['weather'][0]['description'])
return weather_info
else:
print('Could not retrieve weather information for the given location')
def get_exclusion_zone_x_parameter(keys, x_pos_real):
for i, key in enumerate(keys):
if key > x_pos_real:
if i == 0:
return -1
else:
return i - 0.5
elif key == x_pos_real:
return i
return len(keys) + 1
def path_loss_UMi(BS_X, BS_Y, BS_Z, FSS_X, FSS_Y, FSS_Z, ctx):
##UMi
##LOS,SF=4:
buildings = ctx.buildings
saved_los = ctx.saved_los
##(10m<=d_2D)<=D_BP:
# RR - these variables needed as input
h = 5
d_2D = math.sqrt(
((FSS_X - BS_X) ** 2) + ((FSS_Y - BS_Y) ** 2) + ((FSS_Z - BS_Z) ** 2)
)
hBs = 10
fc = 12
d_3D = math.sqrt(hBs ** 2 + d_2D ** 2)
PL1umi = 32.4 + 21 * math.log10(d_3D) + 20 * math.log10(fc)
##(D_BP<=d_2D)<=5000m:
hBs = 10
hUT = 4.5
hE = 1
hBs1 = hBs - hE
hUT1 = hUT - hE
c = 3e8
D_BP = (4 * hBs1 * hUT1 * 12e9) / c
PL2umi = (
32.4
+ 40 * math.log10(d_3D)
+ 20 * math.log10(fc)
- 9.5 * math.log10((D_BP) ** 2 + (hBs - hUT) ** 2)
)
PLUMiLOS = 0
PL1umiNLOS = 0
# weather = get_weather_json(latitude=ctx.lat_FSS, longitude=ctx.lon_FSS)
# rain = weather["rain"]
# x = weather["rain"]["1h"] #rain_rate
rain = ctx.rain
x = ctx.rain_rate
if rain:
# x is the rain rate mm/h
P = -5.520 * 10 ** -12 * x ** 3 + 3.26 * 10 ** -9 * x ** 2 - 1.21 * x * 10 ** -7 - 6 * 10 ** -6 # av (considering vertical polarization)
Q = 8 * 10 ** -10 * x ** 3 - 4.552 * 10 ** -7 * x ** 2 - 3.03 * x * 10 ** -5 + 0.001 # bv (considering vertical polarization)
R = -5.71 * 10 ** -9 * x ** 3 + 6 * 10 ** -7 * x ** 2 + 8.707 * x * 10 ** -3 - 0.018 # cv (considering vertical polarization)
S = - 1.073 * 10 ** -7 * x ** 3 + 1.068 * 10 ** -4 * x ** 2 - 0.0598 * x + 0.0442 # dv (considering vertical polarization)
# Attenuation Factor due to rain
A = (P * (fc ** 3) + Q * (fc ** 2) + R * fc + S) / 1000 # (dB/m)
if 10 <= d_2D and d_2D <= D_BP:
PLUMiLOS = PL1umi + A
elif D_BP <= d_2D and d_2D <= 5000:
PLUMiLOS = PL2umi + A
else:
PLUMiLOS = 1 + A
##NLOS,SF=7.82:
PL1umiNLOS = (
35.3 * math.log10(d_3D) + 22.4 + 21.3 * math.log10(fc) - 0.3 * (hUT - 1.5)
) + A
else:
if 10 <= d_2D and d_2D <= D_BP:
PLUMiLOS = PL1umi
elif D_BP <= d_2D and d_2D <= 5000:
PLUMiLOS = PL2umi
else:
PLUMiLOS = 1
##NLOS,SF=7.82:
PL1umiNLOS = (
35.3 * math.log10(d_3D) + 22.4 + 21.3 * math.log10(fc) - 0.3 * (hUT - 1.5)
)
PLUMiNLOS = max(PLUMiLOS, PL1umiNLOS)
line_of_sight = True
for i in tqdm(range(len(buildings))):
for polygon in buildings[i].wall_polygons:
coordinates = ((BS_X, BS_Y, BS_Z), (FSS_X, FSS_Y, FSS_Z), *[(p.x, p.y, p.z) for p in polygon.points])
# polygon_hash = hash(polygon)
# if (bs_to_fss_segment_hash, polygon_hash) in saved_los:
if coordinates in saved_los:
if saved_los.get(coordinates):
path_loss_UMi = PLUMiNLOS
line_of_sight = False
else:
path_loss_UMi = PLUMiLOS
else:
bs_to_fss_segment = Segment(Point(BS_X, BS_Y, BS_Z), Point(FSS_X, FSS_Y, FSS_Z))
# bs_to_fss_segment_hash = hash(bs_to_fss_segment)
# polygon_hash = hash(polygon)
if intersection(bs_to_fss_segment, polygon) is not None:
path_loss_UMi = PLUMiNLOS
line_of_sight = False
saved_los[coordinates] = True
else:
path_loss_UMi = PLUMiLOS
saved_los[coordinates] = False
##realistic pathloss:
# bs_to_fss_segment = Segment(Point(BS_X, BS_Y, BS_Z), Point(FSS_X, FSS_Y, FSS_Z))
# for i in tqdm(range(len(buildings))):
# # for i in range(len(buildings)):
# for polygon in buildings[i].wall_polygons:
# if (hash(bs_to_fss_segment), hash(polygon)) in saved_los:
# if saved_los.get((hash(bs_to_fss_segment), hash(polygon))):
# path_loss_UMi = PLUMiNLOS
# line_of_sight = False
# else:
# path_loss_UMi = PLUMiLOS
# # line_of_sight = True
#
# # intersection(line, polygon): [Intersection], None
# elif intersection(bs_to_fss_segment, polygon) is not None:
# path_loss_UMi = PLUMiNLOS
# line_of_sight = False
# saved_los[(hash(bs_to_fss_segment), hash(polygon))] = True
# else:
# path_loss_UMi = PLUMiLOS
# # line_of_sight = True
# saved_los[(hash(bs_to_fss_segment), hash(polygon))] = False
# realisticpathlossmodified
# bs_to_fss_segment = Segment(Point(BS_X, BS_Y, BS_Z), Point(FSS_X, FSS_Y, FSS_Z))
# bs_to_fss_segment_hash = hash(bs_to_fss_segment)
#
# all_polygons = [(i, polygon) for i, building in enumerate(buildings) for polygon in building.wall_polygons]
#
# for i, polygon in tqdm(all_polygons):
# polygon_hash = hash(polygon)
# if (bs_to_fss_segment_hash, polygon_hash) in saved_los:
# if saved_los.get((bs_to_fss_segment_hash, polygon_hash)):
# path_loss_UMi = PLUMiNLOS
# line_of_sight = False
# else:
# path_loss_UMi = PLUMiLOS
# elif intersection(bs_to_fss_segment, polygon) is not None:
# path_loss_UMi = PLUMiNLOS
# line_of_sight = False
# saved_los[(bs_to_fss_segment_hash, polygon_hash)] = True
# else:
# path_loss_UMi = PLUMiLOS
# saved_los[(bs_to_fss_segment_hash, polygon_hash)] = False
return path_loss_UMi, d_2D, line_of_sight, saved_los
# #Loss Probability:
# if d_2D>18:
# PrLosUmi=(18/d_2D)+math.exp((-(d_2D/36))*(1-(18/d_2D)))
# else:
# PrLosUmi=1 ##d_2d<=18
# if PrLosUmi > random.random():
# path_loss_UMi = PLUMiLOS
# else:
# path_loss_UMi = PLUMiNLOS
##path_loss
# path_loss_UMi= PL2umi*PrLosUmi+PL1umiNLOS*(1-PrLosUmi)
# return path_loss_UMi, d_2D, line_of_sight
# In[ ]:
def path_loss_UMa(BS_X, BS_Y, BS_Z, FSS_X, FSS_Y, FSS_Z, ctx):
##LOS,SF=4:
##(10m<=d_2D)<=D_BP:
# RR - these variables needed as input
buildings = ctx.buildings
saved_los = ctx.saved_los
fc = 12
h = 5
d_2D = math.sqrt(((FSS_X - BS_X) ** 2) + (FSS_Y - BS_Y) ** 2) + (
(FSS_Z - BS_Z) ** 2
)
hBs = 25
d_3D = math.sqrt(hBs ** 2 + d_2D ** 2)
PL3uma = 28.0 + 22 * math.log10(d_3D) + 20 * math.log10(fc)
##(D_BP<=d_2D) <=5000m:
hUT = 4.5
hE = 1
hBs1 = hBs - hE
hUT1 = hUT - hE
c = 3 * 10 ** 8
D_BP = (4 * hBs1 * hUT1 * fc) / c
PL4uma = (
28.0
+ 40 * math.log10(d_3D)
+ 20 * math.log10(fc)
- 9 * math.log10((D_BP) ** 2 + (hBs - hUT) ** 2)
)
if 10 <= d_2D and d_2D <= D_BP:
PLUMALOS = PL3uma
elif D_BP <= d_2D and d_2D <= 5000:
PLUMALOS = PL4uma
else:
PLUMALOS = 1
##NLOS,SF=6:
PL1NLOSuma = (
13.54 + 39.08 * math.log10(d_3D) + 20 * math.log10(fc) - 0.6 * (hUT - 1.5)
)
PLUMANLOS = max(PLUMALOS, PL1NLOSuma)
line_of_sight = True
# realisticpathlossmodified
bs_to_fss_segment = Segment(Point(BS_X, BS_Y, BS_Z), Point(FSS_X, FSS_Y, FSS_Z))
bs_to_fss_segment_hash = hash(bs_to_fss_segment)
all_polygons = [(i, polygon) for i, building in enumerate(buildings) for polygon in building.wall_polygons]
for i, polygon in tqdm(all_polygons):
polygon_hash = hash(polygon)
if (bs_to_fss_segment_hash, polygon_hash) in saved_los:
if saved_los.get((bs_to_fss_segment_hash, polygon_hash)):
path_loss_UMa = PLUMANLOS
line_of_sight = False
else:
path_loss_UMa = PLUMALOS
elif intersection(bs_to_fss_segment, polygon) is not None:
path_loss_UMa = PLUMANLOS
line_of_sight = False
saved_los[(bs_to_fss_segment_hash, polygon_hash)] = True
else:
path_loss_UMa = PLUMALOS
saved_los[(bs_to_fss_segment_hash, polygon_hash)] = False
return path_loss_UMa, d_2D, line_of_sight
##NLOS,SF=7.8 (optional)
##PL_Optional=32.4+20*math.log(fc)+30*math.log(d_3D)
##Loss Probability:
# if d_2D >18:
# if hUT<=13:
# ChUT=0
# elif 13<hUT and hUT<=23:
# ChUT=((hUT-13)/10)**1.5
# PrLOSUma=((18/d_2D)+math.exp((-(d_2D/63))*(1-(18/d_2D))))*(1+ChUT*(5/4)*((d_2D/100)**3)*math.exp(-(d_2D/150)))
# else:##18m<d_2D:
# PrLOSUma=1
# if PrLOSUma > random.random():
# path_loss_UMa = PLUMALOS
# else:
# path_loss_UMa = PLUMANLOS
##Path Loss
# path_loss_UMa= PL4uma*PrLOSUma+PL1NLOSuma*(1-PrLOSUma)
# return path_loss_UMa, d_2D, line_of_sight
# In[ ]:
def path_loss_RMa(BS_X, BS_Y, BS_Z, FSS_X, FSS_Y, FSS_Z, ctx):
buildings = ctx.buildings
saved_los = ctx.saved_los
##LOS,SF=4(PL1),SF=6(PL2)
##10m<=d_2D<=d_BP:
# RR - these variables needed as input
fc = 12
h = 5
d_2D = math.sqrt(
((FSS_X - BS_X) ** 2) + ((FSS_Y - BS_Y) ** 2) + ((FSS_Z - BS_Z) ** 2)
)
hBs = 35
d_3D = math.sqrt(hBs ** 2 + d_2D ** 2)
PL1rma = (
20 * math.log10((40 * math.pi * d_3D * fc) / 3)
+ min(0.03 * h ** 1.72, 10) * math.log10(d_3D)
- min(0.044 * h ** 1.72, 14.77)
+ 0.002 * math.log10(h) * d_3D
)
##d_BP<=d_2D<=10km:
hUT = 4.5
c = 3 * 10 ** 8
d_BP = (2 * math.pi * hBs * hUT * fc) / c
PL2rma = PL1rma * (d_BP) + 40 * math.log10(d_3D / d_BP)
if 10 <= d_2D and d_2D <= d_BP:
PLRMALOS = PL1rma
elif d_BP <= d_2D and d_2D <= 10000:
PLRMALOS = PL2rma
else:
PLRMALOS = 1
##NLOS,SF=8:
W = 20
h = 5
PL1NLOSrma = (
161.04
- 7.11 * math.log10(W)
+ 7.5 * math.log10(h)
- (24.37 - 3.7 * (h / hBs) ** 2) * math.log10(hBs)
+ (43.42 - 3.1 * math.log10(hBs)) * (math.log10(d_3D) - 3)
+ 20 * math.log10(fc)
- (3.2 * (math.log10(11.75 * hUT)) ** 2 - 4.97)
)
PLRMANLOS = max(PLRMALOS, PL1NLOSrma)
line_of_sight = True
# realisticpathlossmodified
bs_to_fss_segment = Segment(Point(BS_X, BS_Y, BS_Z), Point(FSS_X, FSS_Y, FSS_Z))
bs_to_fss_segment_hash = hash(bs_to_fss_segment)
all_polygons = [(i, polygon) for i, building in enumerate(buildings) for polygon in building.wall_polygons]
for i, polygon in tqdm(all_polygons):
polygon_hash = hash(polygon)
if (bs_to_fss_segment_hash, polygon_hash) in saved_los:
if saved_los.get((bs_to_fss_segment_hash, polygon_hash)):
path_loss_RMa = PLRMANLOS
line_of_sight = False
else:
path_loss_RMa = PLRMALOS
elif intersection(bs_to_fss_segment, polygon) is not None:
path_loss_RMa = PLRMANLOS
line_of_sight = False
saved_los[(bs_to_fss_segment_hash, polygon_hash)] = True
else:
path_loss_RMa = PLRMALOS
saved_los[(bs_to_fss_segment_hash, polygon_hash)] = False
return path_loss_RMa, d_2D, line_of_sight
# ##realistic pathloss:
# bs_to_fss_segment = Segment(Point(BS_X, BS_Y, BS_Z), Point(FSS_X, FSS_Y, FSS_Z))
# for i in tqdm(range(len(buildings))):
# # for i in range(len(buildings)):
# for polygon in buildings[i].wall_polygons:
# if (hash(bs_to_fss_segment), hash(polygon)) in saved_los:
# if saved_los.get((hash(bs_to_fss_segment), hash(polygon))):
# path_loss_UMa = PLUMANLOS
# line_of_sight = False
# else:
# path_loss_UMa = PLUMALOS
# # line_of_sight = True
#
# elif intersection(bs_to_fss_segment, polygon) is not None:
# path_loss_UMa = PLUMANLOS
# line_of_sight = False
# saved_los[(hash(bs_to_fss_segment), hash(polygon))] = True
# else:
# path_loss_UMa = PLUMALOS
# # line_of_sight = True
# saved_los[(hash(bs_to_fss_segment), hash(polygon))] = False
# # realisticpathlossmodified
# bs_to_fss_segment = Segment(Point(BS_X, BS_Y, BS_Z), Point(FSS_X, FSS_Y, FSS_Z))
# bs_to_fss_segment_hash = hash(bs_to_fss_segment)
#
# all_polygons = [(i, polygon) for i, building in enumerate(buildings) for polygon in building.wall_polygons]
#
# for i, polygon in tqdm(all_polygons):
# polygon_hash = hash(polygon)
# if (bs_to_fss_segment_hash, polygon_hash) in saved_los:
# if saved_los.get((bs_to_fss_segment_hash, polygon_hash)):
# path_loss_UMi = PLUMiNLOS
# line_of_sight = False
# else:
# path_loss_UMi = PLUMiLOS
# elif intersection(bs_to_fss_segment, polygon) is not None:
# path_loss_UMi = PLUMiNLOS
# line_of_sight = False
# saved_los[(bs_to_fss_segment_hash, polygon_hash)] = True
# else:
# path_loss_UMi = PLUMiLOS
# saved_los[(bs_to_fss_segment_hash, polygon_hash)] = False
#
# return path_loss_UMi, d_2D, line_of_sight
#
# # ##Loss Probability:
# # if d_2D>10:
# # PrLOSrma=math.exp(-((d_2D-10)/1000))
#
# # ## 10m<d_2D:
# # else:
# # PrLOSrma=1
#
# # if PrLOSrma > random.random():
# # path_loss_RMa= PLRMALOS
#
# # else:
# # path_loss_RMa = PLRMANLOS
#
# ##PathLoss:
# # path_loss_RMa=PL2rma*PrLOSrma+PL1NLOSrma*(1-PrLOSrma)
# return path_loss_RMa, d_2D, line_of_sight
# In[ ]:
def Interface_UMi_1(
BS_X,
BS_Y,
BS_Z,
FSS_X,
FSS_Y,
FSS_Z,
FSS_phi,
pathloss_UMi,
theta_tilt,
phi_scan,
output=False,
):
LBodyLoss = 4
# LSpectralOverlap=10*math.log(10)
# theta_tilt, phi_scan = max_gain_5g_parameters(theta, phi)
x, y, z = BS_X - FSS_X, BS_Y - FSS_Y, (10 - 4.5)
theta_bs_es = math.degrees(math.atan(y / x)) % 360
phi_bs_es = math.degrees(math.sqrt(x ** 2 + y ** 2) / z) % 360
fss_phi_difference = abs(FSS_phi - phi_bs_es)
if output:
print("theta_bs_es:", theta_bs_es, "phi_bs_es:", phi_bs_es)
G_5G_R = gain_5g(theta_bs_es, phi_bs_es, theta_tilt, phi_scan)
G_Rx_5G = gain_fss_wbes_b(fss_phi_difference)
TXPower = -6.77
# LBuildingLoss=1
interface1 = TXPower + G_5G_R - pathloss_UMi - LBodyLoss + G_Rx_5G
return interface1, pathloss_UMi
def Interface_UMa_1(
BS_X,
BS_Y,
BS_Z,
FSS_X,
FSS_Y,
FSS_Z,
FSS_phi,
pathloss_UMa,
theta_tilt,
phi_scan,
output=False,
):
LBodyLoss = 4
# LSpectralOverlap=10*math.log(10)
x, y, z = BS_X - FSS_X, BS_Y - FSS_Y, BS_Z - FSS_Z
theta_bs_es = math.degrees(math.atan(y / x)) % 360
phi_bs_es = math.degrees(math.sqrt(x ** 2 + y ** 2) / z) % 360
fss_phi_difference = abs(FSS_phi - phi_bs_es)
# if output: print("theta_bs_es:", theta_bs_es, "phi_bs_es:", phi_bs_es)
G_5G_R = gain_5g(theta_bs_es, phi_bs_es, theta_tilt, phi_scan)
G_Rx_5G = gain_fss_wbes_b(fss_phi_difference)
TXPower = -6.77
# LBuildingLoss=1
interface2 = TXPower + G_5G_R - pathloss_UMa - LBodyLoss + G_Rx_5G
return interface2, pathloss_UMa
def Interface_RMa_1(
BS_X,
BS_Y,
BS_Z,
FSS_X,
FSS_Y,
FSS_Z,
FSS_phi,
pathloss_RMa,
theta_tilt,
phi_scan,
output=False,
):
LBodyLoss = 4
# LSpectralOverlap=10*math.log(10)
x, y, z = BS_X - FSS_X, BS_Y - FSS_Y, BS_Z - FSS_Z
theta_bs_es = math.degrees(math.atan(y / x)) % 360
phi_bs_es = math.degrees(math.sqrt(x ** 2 + y ** 2) / z) % 360
# if output: print("theta_bs_es:", theta_bs_es, "phi_bs_es:", phi_bs_es)
fss_phi_difference = abs(FSS_phi - phi_bs_es)
if output:
print("fss_phi_difference:", fss_phi_difference)
G_5G_R = gain_5g(theta_bs_es, phi_bs_es, theta_tilt, phi_scan)
G_Rx_5G = gain_fss_wbes_b(fss_phi_difference)
TXPower = -6.77
# LBuildingLoss=1
interface3 = TXPower + G_5G_R - pathloss_RMa - LBodyLoss + G_Rx_5G
return interface3, pathloss_RMa
def simulate(output=True, ctx=None):
FSS_X = np.array([])
FSS_Y = np.array([])
FSS_Z = np.array([])
FSS_CHANNELS = []
x = ctx.x
y = ctx.y
z = ctx.z
FSS_phi = ctx.FSS_phi
Noise_W = ctx.Noise_W
data_within_zone = ctx.data_within_zone
base_station_count = ctx.base_station_count
radius = ctx.radius
R = ctx.R
for i in range(1):
FSS_X = np.append(FSS_X, x)
FSS_Y = np.append(FSS_Y, y)
FSS_Z = np.append(FSS_Z, 4.5)
# 0 means not in use, 1 means in use
channel_status = [
random.randint(0, 1) for i in range(FSS_Channels.channel_count)
]
channels_used = np.array(
[i for i in range(FSS_Channels.channel_count) if channel_status[i] == 1]
)
FSS_CHANNELS.append(channels_used)
if output:
print(
"FSS Co-ordinates="
+ str(x)
+ ","
+ str(y)
+ ","
+ str(z)
+ ", channel: "
+ str(channels_used)
)
if output:
print(FSS_X, FSS_Y, FSS_Z, FSS_CHANNELS)
# Create base stations
BS_X = np.array([])
BS_Y = np.array([])
BS_Z = np.array([])
# Randomly select base stations
# base_station_indexes = random.sample(range(len(data_within_zone)), base_station_count)
# for i in base_station_indexes:
for i in range(base_station_count):
lat_BS, lon_BS = (
data_within_zone.iloc[i]["latitude"],
data_within_zone.iloc[i]["longitude"],
)
# bs1 = BS(radius, max_height=35, carr_freq=12e3, interference_type=None)
x_BS = R * math.cos(math.radians(lat_BS)) * math.cos(math.radians(lon_BS))
y_BS = R * math.cos(math.radians(lat_BS)) * math.sin(math.radians(lon_BS))
# z_BS = R * math.sin(math.radians(lat_BS))
z_BS = 10
x_FSS = ctx.x_FSS
y_FSS = ctx.y_FSS
bs_ue_min_radius = ctx.bs_ue_min_radius
bs_ue_max_radius = ctx.bs_ue_max_radius
BS_X = np.append(BS_X, x_BS - x_FSS)
BS_Y = np.append(BS_Y, y_BS - y_FSS)
BS_Z = np.append(BS_Z, 10)
if output:
print("Bs Co-ordinates=" + str(x_BS) + "," + str(y_BS) + "," + str(z_BS))
if output:
print(BS_X, BS_Y, BS_Z)
# Create user equipment
UE_X = np.array([])
UE_Y = np.array([])
UE_Z = np.array([])
UE_CHANNEL = np.array([])
for p in range(len(BS_X)):
for i in range(3):
# number of split regions
# i is the sector number
for j in range(10):
# number of UEs per region
# j is the number of the UE in one sector
bs_x, bs_y, bs_z = BS_X[p], BS_Y[p], BS_Z[p]
theta_bs_ue = random.uniform(120 * i, 120 * (i + 1))
# 0-120, 120-240, 240-360
radius_bs_ue = random.uniform(bs_ue_min_radius, bs_ue_max_radius)
x1 = bs_x + radius_bs_ue * math.cos(math.radians(theta_bs_ue))
y1 = bs_y + radius_bs_ue * math.sin(math.radians(theta_bs_ue))
UE_X = np.append(UE_X, x1)
UE_Y = np.append(UE_Y, y1)
UE_Z = np.append(UE_Z, 1.5)
maximum_UEs_per_channel = 4
if j > maximum_UEs_per_channel * BS_Channels.channel_count:
raise Exception(f"BS cannot support {j} UEs")
count = {i: 0 for i in range(1, BS_Channels.channel_count + 1)}
channel = random.randint(1, BS_Channels.channel_count)
while count[channel] >= maximum_UEs_per_channel:
channel = random.randint(1, BS_Channels.channel_count)
count[channel] += 1
UE_CHANNEL = np.append(UE_CHANNEL, channel)
if output:
print(
"UE Co-ordinates="
+ str(x1)
+ ","
+ str(y1)
+ ", channel: "
+ str(channel)
)
if output:
print(UE_X, UE_Y, UE_Z)
pathloss_UMa = np.empty([0])
pathloss_UMi = np.empty([0])
pathloss_RMa = np.empty([0])
distance_UMa = np.empty([0])
distance_UMi = np.empty([0])
distance_RMa = np.empty([0])
line_of_sight = np.empty([0])
for i in range(len(BS_X)):
for j in range(len(FSS_X)):
pathlossumi, distance, los_single, ctx.saved_los = path_loss_UMi(
BS_X[i], BS_Y[i], 10, FSS_X[j], FSS_Y[j], 4.5, ctx
)
distance = data_within_zone.iloc[i]["dist_from_FSS"]
pathlossuma = []
pathlossrma = []
# pathlossuma, distance, los_single = path_loss_UMa(
# BS_X[i], BS_Y[i], 25, FSS_X[j], FSS_Y[j], FSS_Z[j], ctx
# )
# pathlossrma, distance, los_single = path_loss_RMa(
# BS_X[i], BS_Y[i], 35, FSS_X[j], FSS_Y[j], FSS_Z[j], ctx
# )
if output:
print(
"pathloss umi:",
pathlossumi,
"uma:",
pathlossuma,
"rma:",
pathlossrma,
"for distance",
distance,
)
pathloss_UMa = np.append(pathloss_UMa, pathlossuma)
distance_UMa = np.append(distance_UMa, distance)
pathloss_UMi = np.append(pathloss_UMi, pathlossumi)
distance_UMi = np.append(distance_UMi, distance)
pathloss_RMa = np.append(pathloss_RMa, pathlossrma)
distance_RMa = np.append(distance_RMa, distance)
line_of_sight = np.append(line_of_sight, los_single)
if output:
print(pathloss_UMi, distance_UMi)
if output:
print(pathloss_UMa, distance_UMa)
if output:
print(pathloss_RMa, distance_RMa)
interface_UMi_W = np.empty([0])
interface_UMa_W = np.empty([0])
interface_RMa_W = np.empty([0])
for i in range(len(BS_X)):
interface_UMi_BS = np.empty([0])
interface_UMa_BS = np.empty([0])
interface_RMa_BS = np.empty([0])
for j in range(len(FSS_X)):
if output:
print(f"BS {i}, FSS {j}, pathloss {pathloss_UMi[i * len(FSS_X) + j]}")
for k in random.sample(range(len(UE_X)), 30):
# print(f"UE{k}")
# channel check
# if UE is using channel 1, the start is 12.2GHz and the end is 12.3GHz
bs_channel_start, bs_channel_end = BS_Channels.getChannelRange(
UE_CHANNEL[k]
)
bs_channel_range = range(
bs_channel_start, int(bs_channel_end + (5e6)), int(5e6)
)
interference_found = False
for fss_channel in FSS_CHANNELS[j]:
# if fss_channel >= 6:
fss_channel_start, fss_channel_end = FSS_Channels.getChannelRange(
fss_channel
)
fss_channel_range = range(
bs_channel_start, int(bs_channel_end + (5e6)), int(5e6)
)
bs_set = set(bs_channel_range)
if len(bs_set.intersection(fss_channel_range)):
interference_found = True
if not interference_found:
continue
for interference_type in ["UMi", "UMa", "RMa"]:
if interference_type == "UMi":
BS_Z = np.array([10 for i in range(len(BS_X))])
elif interference_type == "UMa":
BS_Z = np.array([25 for i in range(len(BS_X))])
elif interference_type == "RMa":
BS_Z = np.array([35 for i in range(len(BS_X))])
# bs_ue_x, bs_ue_y, bs_ue_z = BS_X-UE_X, BS_Y-UE_Y, BS_Z-UE_Z
bs_ue_x, bs_ue_y, bs_ue_z = (
UE_X[k] - BS_X[i],
UE_Y[k] - BS_Y[i],
UE_Z[k] - BS_Z[i],
)
theta_bs_ue = np.arctan(bs_ue_y / bs_ue_x)
phi_bs_ue = np.sqrt(bs_ue_x ** 2 + bs_ue_y ** 2) / bs_ue_z
theta_bs_ue = np.degrees(theta_bs_ue) % 360
phi_bs_ue = np.degrees(phi_bs_ue) % 360
if output:
print("theta_bs_ue:", theta_bs_ue, "phi_bs_ue:", phi_bs_ue)
theta_tilt, phi_scan = max_gain_5g_parameters(
theta_bs_ue, phi_bs_ue, ctx
)
theta_tilt = 10
if interference_type == "UMi":
interfaceumi, pathloss_UMi_x = Interface_UMi_1(
BS_X[i],
BS_Y[i],
BS_Z[i],
FSS_X[j],
FSS_Y[j],
FSS_Z[j],
FSS_phi['UMi'],
pathloss_UMi[i * len(FSS_X) + j],
theta_tilt,
phi_scan,
)
if output:
print("UE:", k, "/ interference umi:", interfaceumi, "/ pathloss:", pathlossumi)
# if output:
# print(
# "interference umi:",
# interfaceumi,
# "pathloss:",
# pathlossumi,
# )
interface_UMi_BS = np.append(interface_UMi_BS, interfaceumi)
# elif interference_type == "UMa":
# interfaceuma, pathloss_UMa_x = Interface_UMa_1(
# BS_X[i],
# BS_Y[i],
# BS_Z[i],
# FSS_X[j],
# FSS_Y[j],
# FSS_Z[j],
# FSS_phi["UMa"],
# pathloss_UMa[i * len(FSS_X) + j],
# theta_tilt,
# phi_scan,
# )
# if output:
# print(
# "interference uma:",
# interfaceuma,
# "pathloss:",
# pathlossuma,
# )
# interface_UMa_BS = np.append(interface_UMa_BS, interfaceuma)
# elif interference_type == "RMa":
# interfacerma, pathloss_RMa_x = Interface_RMa_1(
# BS_X[i],
# BS_Y[i],
# BS_Z[i],
# FSS_X[j],
# FSS_Y[j],
# FSS_Z[j],
# FSS_phi["RMa"],
# pathloss_RMa[i * len(FSS_X) + j],
# theta_tilt,
# phi_scan,
# )
# if output:
# print(
# "interference rma:",
# interfacerma,
# "pathloss:",
# pathlossrma,
# )
# interface_RMa_BS = np.append(interface_RMa_BS, interfacerma)
interface_UMi_W = np.append(
interface_UMi_W, np.sum(10 ** (interface_UMi_BS / 10))
)
interface_UMa_W = np.append(
interface_UMa_W, np.sum(10 ** (interface_UMa_BS / 10))
)
interface_RMa_W = np.append(
interface_RMa_W, np.sum(10 ** (interface_RMa_BS / 10))
)
if output:
print(interface_UMi_W, pathloss_UMi)
if output:
print(interface_UMa_W, pathloss_UMa)
if output:
print(interface_RMa_W, pathloss_RMa)
# I_N_UMi = np.array([interfaceumi-Noise for interfaceumi in interface_UMi])
I_N_UMi = interface_UMi_W / Noise_W
if output:
print("I/N UMi:", I_N_UMi)
I_N_UMa = interface_UMa_W / Noise_W
if output:
print("I/N UMa:", I_N_UMa)
I_N_RMa = interface_RMa_W / Noise_W
if output:
print("I/N RMa:", I_N_RMa)
return (
distance_RMa,
I_N_RMa,
distance_UMa,
I_N_UMa,
distance_UMi,
I_N_UMi,
line_of_sight,
ctx.saved_los,
)
def gain_antenna_element_horizontal(phi) -> float:
phi_3db = 80 # degrees
front_to_back_ratio = 30 # dB
return -min(12 * (phi / phi_3db) ** 2, front_to_back_ratio)
# antenna vertical pattern
def gain_antenna_element_vertical(theta) -> float:
theta_3db = 65 # degrees
side_lobe_level_limit = 30 # dB
return -min(12 * ((theta - 90) / theta_3db) ** 2, side_lobe_level_limit)
# antenna element gain of elevation and azimuth plane
def gain_antenna_element(theta, phi) -> float:
front_to_back_ratio = 30 # dB
antenna_gain_max = 8
return antenna_gain_max - min(
-(gain_antenna_element_horizontal(phi) + gain_antenna_element_vertical(theta)),
front_to_back_ratio,
)
def superposition(n, m, theta, phi, hspace, vspace) -> complex:
return cmath.exp(
complex(
0,
2
* math.pi
* (
(n - 1) * vspace * math.cos(math.radians(theta))
+ (m - 1)
* hspace
* math.sin(math.radians(theta))
* math.sin(math.radians(phi))
),
)
)
def weighting(n, m, theta_tilt, phi_scan, hspace, vspace, rows, cols) -> complex:
return cmath.exp(
complex(
0,
-2
* math.pi
* (
(n - 1) * vspace * math.sin(math.radians(theta_tilt))
+ (m - 1)
* hspace
* math.cos(math.radians(theta_tilt))
* math.sin(math.radians(phi_scan))
),
)
) / cmath.sqrt(rows * cols)
# returns theta_tilt and phi_scan which yield maximum antenna gain given theta and phi
def max_gain_5g_parameters(theta, phi, ctx, coarse=True, rounding_precision=0) -> tuple:
saved_tp = ctx.saved_tp
if coarse:
theta = round(theta, rounding_precision)
phi = round(phi, rounding_precision)
if (theta, phi) in saved_tp:
# print(f'match found for ({theta}, {phi}), using that')
return saved_tp.get((theta, phi))
# return max_parameters
# scipy's optimization can only find the minimum, so we pass a function which returns the negative of the weighting function
result = optimize.brute(
lambda x: -beam_pattern_5g(
theta, phi, x[0], x[1]
), # x[0] = theta_tilt, x[1] = phi_scan
# theta_tilt is between -90 and 90 degrees, phi_scan is between -180 and 180 degrees
ranges=[(-90, 90), (-180, 180)],
)
saved_tp[(theta, phi)] = tuple(x for x in result)
return saved_tp[(theta, phi)]
# a_A, the directional pattern from beam forming with an array of elements
def beam_pattern_5g(theta, phi, theta_tilt, phi_scan) -> float:
summation = 0
rows = 16 # Nv
cols = 16 # Nh
hspace = 0.5 # dh/λ
vspace = 0.5 # dv/λ
# weighting multiplied by the superposition vector
for n in range(1, rows + 1):
for m in range(1, cols + 1):
# print(f"{theta_tilt}, {phi_scan}, {abs(weighting(n, m, theta_tilt, phi_scan, hspace, vspace))}")
summation += weighting(