A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
spectrum-ideal-phy-test.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011 CTTC
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Nicola Baldo <nbaldo@cttc.es>
18 */
19
20#include <ns3/adhoc-aloha-noack-ideal-phy-helper.h>
21#include <ns3/config.h>
22#include <ns3/data-rate.h>
23#include <ns3/friis-spectrum-propagation-loss.h>
24#include <ns3/ism-spectrum-value-helper.h>
25#include <ns3/log.h>
26#include <ns3/math.h>
27#include <ns3/mobility-helper.h>
28#include <ns3/object.h>
29#include <ns3/packet-socket-address.h>
30#include <ns3/packet-socket-client.h>
31#include <ns3/packet-socket-helper.h>
32#include <ns3/packet.h>
33#include <ns3/propagation-delay-model.h>
34#include <ns3/ptr.h>
35#include <ns3/simulator.h>
36#include <ns3/single-model-spectrum-channel.h>
37#include <ns3/spectrum-analyzer.h>
38#include <ns3/spectrum-error-model.h>
39#include <ns3/spectrum-helper.h>
40#include <ns3/spectrum-interference.h>
41#include <ns3/spectrum-model-300kHz-300GHz-log.h>
42#include <ns3/spectrum-model-ism2400MHz-res1MHz.h>
43#include <ns3/string.h>
44#include <ns3/test.h>
45#include <ns3/uinteger.h>
46#include <ns3/waveform-generator.h>
47
48#include <iomanip>
49#include <iostream>
50#include <string>
51
52using namespace ns3;
53
54NS_LOG_COMPONENT_DEFINE("SpectrumIdealPhyTest");
55
56static uint64_t g_rxBytes;
57static double g_bandwidth = 20e6; // Hz
58
59void
60PhyRxEndOkTrace(std::string context, Ptr<const Packet> p)
61{
62 g_rxBytes += p->GetSize();
63}
64
71{
72 public:
80 SpectrumIdealPhyTestCase(double snrLinear,
81 uint64_t phyRate,
82 bool rateIsAchievable,
83 std::string channelType);
85
86 private:
87 void DoRun() override;
95 static std::string Name(std::string channelType, double snrLinear, uint64_t phyRate);
96
97 double m_snrLinear;
98 uint64_t m_phyRate;
100 std::string m_channelType;
101};
102
103std::string
104SpectrumIdealPhyTestCase::Name(std::string channelType, double snrLinear, uint64_t phyRate)
105{
106 std::ostringstream oss;
107 oss << channelType << " snr = " << snrLinear << " (linear), "
108 << " phyRate = " << phyRate << " bps";
109 return oss.str();
110}
111
113 uint64_t phyRate,
114 bool rateIsAchievable,
115 std::string channelType)
116 : TestCase(Name(channelType, snrLinear, phyRate)),
117 m_snrLinear(snrLinear),
118 m_phyRate(phyRate),
119 m_rateIsAchievable(rateIsAchievable),
120 m_channelType(channelType)
121{
122}
123
125{
126}
127
128void
130{
132 double txPowerW = 0.1;
133 // for the noise, we use the Power Spectral Density of thermal noise
134 // at room temperature. The value of the PSD will be constant over the band of interest.
135 const double k = 1.381e-23; // Boltzmann's constant
136 const double T = 290; // temperature in Kelvin
137 double noisePsdValue = k * T; // W/Hz
138 double lossLinear = (txPowerW) / (m_snrLinear * noisePsdValue * g_bandwidth);
139 double lossDb = 10 * std::log10(lossLinear);
140 uint64_t phyRate = m_phyRate; // bps
141 uint32_t pktSize = 50; // bytes
142
143 uint32_t numPkts = 200; // desired number of packets in the
144 // test. Directly related with the accuracy
145 // of the measurement.
146
147 double testDuration = (numPkts * pktSize * 8.0) / phyRate;
148 NS_LOG_INFO("test duration = " << std::fixed << testDuration);
149
151 c.Create(2);
152
153 MobilityHelper mobility;
154 Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator>();
155 positionAlloc->Add(Vector(0.0, 0.0, 0.0));
156 positionAlloc->Add(Vector(5.0, 0.0, 0.0));
157 mobility.SetPositionAllocator(positionAlloc);
158 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
159
160 mobility.Install(c);
161
162 SpectrumChannelHelper channelHelper;
163 channelHelper.SetChannel(m_channelType);
164 channelHelper.SetPropagationDelay("ns3::ConstantSpeedPropagationDelayModel");
165 Ptr<MatrixPropagationLossModel> propLoss = CreateObject<MatrixPropagationLossModel>();
166 propLoss->SetLoss(c.Get(0)->GetObject<MobilityModel>(),
167 c.Get(1)->GetObject<MobilityModel>(),
168 lossDb,
169 true);
170 channelHelper.AddPropagationLoss(propLoss);
171 Ptr<SpectrumChannel> channel = channelHelper.Create();
172
174
175 uint32_t channelNumber = 1;
176 Ptr<SpectrumValue> txPsd = sf.CreateTxPowerSpectralDensity(txPowerW, channelNumber);
177
178 Ptr<SpectrumValue> noisePsd = sf.CreateConstant(noisePsdValue);
179
181 deviceHelper.SetChannel(channel);
182 deviceHelper.SetTxPowerSpectralDensity(txPsd);
183 deviceHelper.SetNoisePowerSpectralDensity(noisePsd);
184 deviceHelper.SetPhyAttribute("Rate", DataRateValue(DataRate(phyRate)));
185 NetDeviceContainer devices = deviceHelper.Install(c);
186
187 PacketSocketHelper packetSocket;
188 packetSocket.Install(c);
189
190 PacketSocketAddress socket;
191 socket.SetSingleDevice(devices.Get(0)->GetIfIndex());
192 socket.SetPhysicalAddress(devices.Get(1)->GetAddress());
193 socket.SetProtocol(1);
194
195 Ptr<PacketSocketClient> client = CreateObject<PacketSocketClient>();
196 client->SetRemote(socket);
197 client->SetAttribute("Interval",
198 TimeValue(Seconds(double(pktSize * 8) / (1.2 * double(phyRate)))));
199 client->SetAttribute("PacketSize", UintegerValue(pktSize));
200 client->SetAttribute("MaxPackets", UintegerValue(0));
201 client->SetStartTime(Seconds(0.0));
202 client->SetStopTime(Seconds(testDuration));
203 c.Get(0)->AddApplication(client);
204
205 Config::Connect("/NodeList/*/DeviceList/*/Phy/RxEndOk", MakeCallback(&PhyRxEndOkTrace));
206
207 g_rxBytes = 0;
208 Simulator::Stop(Seconds(testDuration + 0.000000001));
210 double throughputBps = (g_rxBytes * 8.0) / testDuration;
211
212 std::clog.unsetf(std::ios_base::floatfield);
213
215 {
216 NS_TEST_ASSERT_MSG_EQ_TOL(throughputBps,
217 m_phyRate,
218 m_phyRate * 0.01,
219 "throughput does not match PHY rate");
220 }
221 else
222 {
223 NS_TEST_ASSERT_MSG_EQ(throughputBps,
224 0.0,
225 "PHY rate is not achievable but throughput is non-zero");
226 }
227
229}
230
237{
238 public:
240};
241
243 : TestSuite("spectrum-ideal-phy", Type::SYSTEM)
244{
245 NS_LOG_INFO("creating SpectrumIdealPhyTestSuite");
246
247 for (double snr = 0.01; snr <= 10; snr *= 2)
248 {
249 double achievableRate = g_bandwidth * log2(1 + snr);
251 static_cast<uint64_t>(achievableRate * 0.1),
252 true,
253 "ns3::SingleModelSpectrumChannel"),
254 TestCase::Duration::QUICK);
256 static_cast<uint64_t>(achievableRate * 0.5),
257 true,
258 "ns3::SingleModelSpectrumChannel"),
259 TestCase::Duration::QUICK);
261 static_cast<uint64_t>(achievableRate * 0.95),
262 true,
263 "ns3::SingleModelSpectrumChannel"),
264 TestCase::Duration::QUICK);
266 static_cast<uint64_t>(achievableRate * 1.05),
267 false,
268 "ns3::SingleModelSpectrumChannel"),
269 TestCase::Duration::QUICK);
271 static_cast<uint64_t>(achievableRate * 2),
272 false,
273 "ns3::SingleModelSpectrumChannel"),
274 TestCase::Duration::QUICK);
276 static_cast<uint64_t>(achievableRate * 4),
277 false,
278 "ns3::SingleModelSpectrumChannel"),
279 TestCase::Duration::QUICK);
280 }
281 for (double snr = 0.01; snr <= 10; snr *= 10)
282 {
283 double achievableRate = g_bandwidth * log2(1 + snr);
285 static_cast<uint64_t>(achievableRate * 0.1),
286 true,
287 "ns3::MultiModelSpectrumChannel"),
288 TestCase::Duration::QUICK);
290 static_cast<uint64_t>(achievableRate * 0.5),
291 true,
292 "ns3::MultiModelSpectrumChannel"),
293 TestCase::Duration::QUICK);
295 static_cast<uint64_t>(achievableRate * 0.95),
296 true,
297 "ns3::MultiModelSpectrumChannel"),
298 TestCase::Duration::QUICK);
300 static_cast<uint64_t>(achievableRate * 1.05),
301 false,
302 "ns3::MultiModelSpectrumChannel"),
303 TestCase::Duration::QUICK);
305 static_cast<uint64_t>(achievableRate * 2),
306 false,
307 "ns3::MultiModelSpectrumChannel"),
308 TestCase::Duration::QUICK);
310 static_cast<uint64_t>(achievableRate * 4),
311 false,
312 "ns3::MultiModelSpectrumChannel"),
313 TestCase::Duration::QUICK);
314 }
315}
316
Ideal Spectrum PHY Test.
static std::string Name(std::string channelType, double snrLinear, uint64_t phyRate)
Get the test name.
bool m_rateIsAchievable
Check if the rate is achievable.
uint64_t m_phyRate
PHY rate (bps)
void DoRun() override
Implementation to actually run this TestCase.
std::string m_channelType
Channel type.
SpectrumIdealPhyTestCase(double snrLinear, uint64_t phyRate, bool rateIsAchievable, std::string channelType)
Constructor.
Ideal Spectrum PHY TestSuite.
void SetPhyAttribute(std::string name, const AttributeValue &v)
void SetTxPowerSpectralDensity(Ptr< SpectrumValue > txPsd)
void SetNoisePowerSpectralDensity(Ptr< SpectrumValue > noisePsd)
void SetChannel(Ptr< SpectrumChannel > channel)
set the SpectrumChannel that will be used by SpectrumPhy instances created by this helper
NetDeviceContainer Install(NodeContainer c) const
Class for representing data rates.
Definition: data-rate.h:89
AttributeValue implementation for DataRate.
Definition: data-rate.h:296
Helper class used to assign positions and mobility models to nodes.
Keep track of the current position and velocity of an object.
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
uint32_t AddApplication(Ptr< Application > application)
Associate an Application to this Node.
Definition: node.cc:169
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition: object.h:522
an address for a packet socket
void SetProtocol(uint16_t protocol)
Set the protocol.
void SetPhysicalAddress(const Address address)
Set the destination address.
void SetSingleDevice(uint32_t device)
Set the address to match only a specified NetDevice.
Give ns3::PacketSocket powers to ns3::Node.
void Install(Ptr< Node > node) const
Aggregate an instance of a ns3::PacketSocketFactory onto the provided node.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
Setup a SpectrumChannel.
Ptr< SpectrumChannel > Create() const
void AddPropagationLoss(std::string name, Ts &&... args)
void SetPropagationDelay(std::string name, Ts &&... args)
void SetChannel(std::string type, Ts &&... args)
Implements Wifi SpectrumValue for the 2.4 GHz ISM band only, with a 5 MHz spectrum resolution.
virtual Ptr< SpectrumValue > CreateConstant(double psd)
Creates a SpectrumValue instance with a constant value for all frequencies.
virtual Ptr< SpectrumValue > CreateTxPowerSpectralDensity(double txPower, uint8_t channel)
Creates a SpectrumValue instance that represents the TX Power Spectral Density of a wifi device corre...
encapsulates test code
Definition: test.h:1061
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1268
Type
Type of test.
Definition: test.h:1275
AttributeValue implementation for Time.
Definition: nstime.h:1413
Hold an unsigned integer type.
Definition: uinteger.h:45
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:978
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:275
#define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
Definition: test.h:145
#define NS_TEST_ASSERT_MSG_EQ_TOL(actual, limit, tol, msg)
Test that actual and expected (limit) values are equal to plus or minus some tolerance and report and...
Definition: test.h:338
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:704
void PhyRxEndOkTrace(std::string context, Ptr< const Packet > p)
static SpectrumIdealPhyTestSuite g_spectrumIdealPhyTestSuite
Static variable for test initialization.
static uint64_t g_rxBytes
static double g_bandwidth
static std::string Name(std::string str, uint32_t totalStreamSize, uint32_t sourceWriteSize, uint32_t serverReadSize, uint32_t serverWriteSize, uint32_t sourceReadSize, bool useIpv6)
Definition: tcp-test.cc:166
uint32_t pktSize
packet size used for the simulation (in bytes)