-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfixed_rss.cc
More file actions
254 lines (212 loc) · 8.12 KB
/
Copy pathfixed_rss.cc
File metadata and controls
254 lines (212 loc) · 8.12 KB
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
#include <algorithm>
#include <cmath>
#include <fstream>
#include <numeric>
#include <string>
#include <vector>
#include "ns3/applications-module.h"
#include "ns3/core-module.h"
#include "ns3/flow-monitor-module.h"
#include "ns3/internet-module.h"
#include "ns3/mobility-module.h"
#include "ns3/network-module.h"
#include "ns3/propagation-module.h"
#include "ns3/spectrum-module.h"
#include "ns3/wifi-module.h"
using namespace ns3;
NS_LOG_COMPONENT_DEFINE("Wifi80211nAdhoc");
struct Result {
double distance;
double simTime;
double rssDbm;
double throughputMbps;
double rssStdDevDb;
};
static std::vector<double> g_rssiSamples;
static void PhyRxCallback(std::string, Ptr<const Packet>, uint16_t,
WifiTxVector, MpduInfo, SignalNoiseDbm snr,
uint16_t) {
g_rssiSamples.push_back(snr.signal);
}
static double ComputeMean(const std::vector<double> &values) {
if (values.empty()) {
return 0.0;
}
return std::accumulate(values.begin(), values.end(), 0.0) / values.size();
}
static double ComputeStdDev(const std::vector<double> &values, double mean) {
if (values.size() <= 1) {
return 0.0;
}
double sqSum =
std::inner_product(values.begin(), values.end(), values.begin(), 0.0);
double variance = (sqSum - values.size() * mean * mean) / (values.size() - 1);
variance = std::max(variance, 0.0);
return std::sqrt(variance);
}
static void ConfigurePropagationLoss(YansWifiChannelHelper &channelHelper,
const std::string &lossModel) {
if (lossModel == "FixedRssLossModel") {
channelHelper.AddPropagationLoss("ns3::FixedRssLossModel", "Rss",
DoubleValue(-40.0));
} else if (lossModel == "FriisPropagationLossModel") {
channelHelper.AddPropagationLoss("ns3::FriisPropagationLossModel",
"Frequency", DoubleValue(5e9));
} else if (lossModel == "ThreeLogDistancePropagationLossModel") {
channelHelper.AddPropagationLoss(
"ns3::ThreeLogDistancePropagationLossModel");
} else if (lossModel == "TwoRayGroundPropagationLossModel") {
channelHelper.AddPropagationLoss("ns3::TwoRayGroundPropagationLossModel",
"Frequency", DoubleValue(5e9),
"HeightAboveZ", DoubleValue(0.6));
} else if (lossModel == "Nakagami") {
channelHelper.AddPropagationLoss("ns3::NakagamiPropagationLossModel");
} else {
NS_FATAL_ERROR("Unknown lossModel: " << lossModel);
}
channelHelper.SetPropagationDelay("ns3::ConstantSpeedPropagationDelayModel");
}
static Result RunOnce(double distance, double simTime,
const std::string &lossModel) {
g_rssiSamples.clear();
const double txPowerDbm = 10.0;
const double antennaGainDb = 1.0;
const uint32_t payloadSize = 1450;
const double dataRate = 75e6;
const double interval = (payloadSize * 8) / dataRate;
const Time interPacketInterval = Seconds(interval);
NodeContainer nodes;
nodes.Create(2);
WifiHelper wifi;
wifi.SetStandard(WIFI_STANDARD_80211n);
wifi.SetRemoteStationManager("ns3::MinstrelHtWifiManager");
YansWifiPhyHelper phy;
YansWifiChannelHelper channelHelper;
ConfigurePropagationLoss(channelHelper, lossModel);
phy.SetChannel(channelHelper.Create());
phy.Set("ChannelSettings", StringValue("{0, 40, BAND_5GHZ, 0}"));
phy.Set("TxPowerStart", DoubleValue(txPowerDbm));
phy.Set("TxPowerEnd", DoubleValue(txPowerDbm));
phy.Set("TxGain", DoubleValue(antennaGainDb));
phy.Set("RxGain", DoubleValue(antennaGainDb));
WifiMacHelper mac;
mac.SetType("ns3::AdhocWifiMac");
NetDeviceContainer devices = wifi.Install(phy, mac, nodes);
Config::Connect("/NodeList/1/DeviceList/*/Phy/MonitorSnifferRx",
MakeCallback(&PhyRxCallback));
MobilityHelper mobility;
Ptr<ListPositionAllocator> positions = CreateObject<ListPositionAllocator>();
positions->Add(Vector(0.0, 0.0, 0.0));
positions->Add(Vector(distance, 0.0, 0.0));
mobility.SetPositionAllocator(positions);
mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
mobility.Install(nodes);
InternetStackHelper stack;
stack.Install(nodes);
Ptr<FlowMonitor> flowMonitor;
FlowMonitorHelper flowHelper;
flowMonitor = flowHelper.InstallAll();
Ipv4AddressHelper address;
address.SetBase("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer interfaces = address.Assign(devices);
const uint16_t port = 1337;
UdpServerHelper server(port);
ApplicationContainer serverApp = server.Install(nodes.Get(1));
serverApp.Start(Seconds(0.0));
serverApp.Stop(Seconds(simTime));
const uint32_t maxPackets =
(simTime > 1.0)
? (uint32_t)((dataRate / (payloadSize * 8.0)) * (simTime - 1))
: 0;
UdpClientHelper client(interfaces.GetAddress(1), port);
client.SetAttribute("PacketSize", UintegerValue(payloadSize));
client.SetAttribute("Interval", TimeValue(interPacketInterval));
client.SetAttribute("MaxPackets", UintegerValue(maxPackets));
ApplicationContainer clientApp = client.Install(nodes.Get(0));
clientApp.Start(Seconds(1.0));
clientApp.Stop(Seconds(simTime));
Simulator::Stop(Seconds(simTime + 0.1));
Simulator::Run();
Ptr<UdpServer> udpServer =
DynamicCast<UdpServer>(serverApp.Get(0)->GetObject<Application>());
uint64_t totalPacketsReceived = udpServer->GetReceived();
flowMonitor->CheckForLostPackets();
const std::map<FlowId, FlowMonitor::FlowStats> &stats =
flowMonitor->GetFlowStats();
double rssDbm = -200.0;
double rssStdDevDb = 0.0;
double throughputMbps = 0.0;
if (!g_rssiSamples.empty()) {
const double mean = ComputeMean(g_rssiSamples);
rssDbm = mean;
rssStdDevDb = ComputeStdDev(g_rssiSamples, mean);
}
if (!stats.empty() && simTime > 1.0) {
const FlowMonitor::FlowStats &flowStats = stats.begin()->second;
const double rxDuration = flowStats.timeLastRxPacket.GetSeconds() -
flowStats.timeFirstTxPacket.GetSeconds();
if (flowStats.rxPackets > 0 && rxDuration > 0) {
throughputMbps =
flowStats.rxPackets * payloadSize * 8.0 / rxDuration / 1000000.0;
}
}
if (throughputMbps == 0.0) {
rssDbm = -200.0;
rssStdDevDb = 0.0;
}
Simulator::Destroy();
return {distance, simTime, rssDbm, throughputMbps, rssStdDevDb};
}
int main(int argc, char *argv[]) {
std::string lossModel = "FriisPropagationLossModel";
std::string mode = "distance";
std::string csvPath = "output.csv";
CommandLine cmd(__FILE__);
cmd.AddValue("lossModel", "Propagation loss model to use", lossModel);
cmd.AddValue("mode", "Sweep mode: distance or runtime", mode);
cmd.AddValue("csv", "Path to output CSV file", csvPath);
cmd.Parse(argc, argv);
RngSeedManager::SetSeed(1);
RngSeedManager::SetRun(1);
std::ofstream file(csvPath, std::ios::trunc);
if (!file.is_open()) {
NS_FATAL_ERROR("Could not open output CSV file: " << csvPath);
}
file << "distance_m,sim_time_s,rss_dbm,throughput_mbps,rss_stddev_db\n";
if (mode == "distance") {
const double simTime = 10.0;
for (uint32_t distance = 1; distance <= 261; distance += 5) {
const Result r = RunOnce((double)distance, simTime, lossModel);
file << r.distance << "," << r.simTime << "," << r.rssDbm << ","
<< r.throughputMbps << "," << r.rssStdDevDb << "\n";
file.flush();
}
} else if (mode == "runtime") {
const double distance = 10.0;
struct TimeRange {
double end;
double step;
};
const std::vector<TimeRange> timeranges = {
{1.0, 0.01},
{2.5, 0.05},
{15.0, 0.5},
};
const double eps = 1e-9;
double simTime = 0.0;
for (const auto &range : timeranges) {
while (simTime <= range.end + eps) {
const Result r = RunOnce(distance, simTime, lossModel);
file << r.distance << "," << r.simTime << "," << r.rssDbm << ","
<< r.throughputMbps << "," << r.rssStdDevDb << "\n";
file.flush();
simTime += range.step;
}
}
} else {
NS_FATAL_ERROR("Unknown mode: " << mode
<< " (expected 'distance' or 'runtime')");
}
file.close();
return 0;
}