-
Notifications
You must be signed in to change notification settings - Fork 7
/
simulate.py
executable file
·123 lines (110 loc) · 4.71 KB
/
simulate.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
#!/usr/bin/env python3
"""PTP Simulator
"""
import logging
import sys
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
import ptp.simulation
def get_parser():
parser = ArgumentParser(description="PTP Simulator",
formatter_class=ArgumentDefaultsHelpFormatter)
parser.add_argument('-N',
'--num-iter',
default=10,
type=int,
help='Number of iterations.')
parser.add_argument('-t',
'--sim-step',
default=1e-9,
type=float,
help='Simulation time step in seconds.')
parser.add_argument('-s',
'--save',
default=False,
action='store_true',
help='Save dataset generated by the PTP simulation.')
parser.add_argument('-f',
'--file',
default=None,
help='File containing simulation data to load.')
parser.add_argument('--verbose',
'-v',
action='count',
default=1,
help="Verbosity (logging) level")
sim_group = parser.add_argument_group('Simulation parameters')
sim_group.add_argument('--rtc-clk-freq',
default=125e6,
type=float,
help="RTC's driving clock frequency in Hz")
sim_group.add_argument('--rtc-resolution',
default=0,
type=float,
help="Fixed-point representation resolution in ns \
of the RTC's increment value")
sim_group.add_argument('--sync-rate',
default=16,
type=float,
help="Sync transmission rate in packets per second")
sim_group.add_argument('--freq-tolerance',
default=60,
type=float,
help="Slave RTC's frequency tolerance in ppb")
sim_group.add_argument('--freq-rw',
default=1e-18,
type=float,
help="Normalized variance of the slave RTC's \
random-walk in frequency (set 0 to disable this \
source of phase noise)")
sim_group.add_argument('--phase-rw',
default=1e-12,
type=float,
help="Normalized variance of the slave RTC's \
random-walk in phase, a.k.a. white noise in \
frequency (set 0 to disable this source of phase \
noise)")
sim_group.add_argument('--pdv-distr',
default='Gamma',
type=str,
choices=["Gamma", "Gaussian"],
help="Distribution of the PDV experienced by PTP \
messages in the simulation (Gamma or Gaussian)")
sim_group.add_argument('--gamma-shape',
default=None,
type=int,
help="Shape parameter of the Gamma distribution")
sim_group.add_argument('--gamma-scale',
default=None,
type=int,
help="Scale parameter of the Gamma distribution")
sim_group.add_argument('--no-ts-quantization',
default=False,
action='store_true',
help="Disables the quantization of the time scale")
return parser
def main():
args = get_parser().parse_args()
logging_level = 70 - (10 * args.verbose) if args.verbose > 0 else 0
logging.basicConfig(stream=sys.stderr, level=logging_level)
simulation = ptp.simulation.Simulation(
n_iter=args.num_iter,
sim_t_step=args.sim_step,
sync_period=(1 / args.sync_rate),
rtc_clk_freq=args.rtc_clk_freq,
rtc_resolution=args.rtc_resolution,
freq_tolerance=args.freq_tolerance,
freq_rw=args.freq_rw,
phase_rw=args.phase_rw,
pdv_distr=args.pdv_distr,
gamma_shape=args.gamma_shape,
gamma_scale=args.gamma_scale,
ts_quantization=(not args.no_ts_quantization))
if (args.file is not None):
simulation.load(args.file)
simulation.dump()
else:
simulation.run()
if (args.save):
simulation.save()
if __name__ == "__main__":
main()