A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
adhoc-aloha-ideal-phy-matrix-propagation-loss-model.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010 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/applications-module.h>
22#include <ns3/core-module.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/mobility-module.h>
27#include <ns3/network-module.h>
28#include <ns3/propagation-delay-model.h>
29#include <ns3/single-model-spectrum-channel.h>
30#include <ns3/spectrum-analyzer.h>
31#include <ns3/spectrum-helper.h>
32#include <ns3/spectrum-model-300kHz-300GHz-log.h>
33#include <ns3/spectrum-model-ism2400MHz-res1MHz.h>
34#include <ns3/waveform-generator.h>
35
36#include <iomanip>
37#include <iostream>
38#include <string>
39
40using namespace ns3;
41
42NS_LOG_COMPONENT_DEFINE("TestAdhocOfdmAloha");
43
44static bool g_verbose = false;
45static uint64_t g_rxBytes;
46
53void
54PhyRxEndOkTrace(std::string context, Ptr<const Packet> p)
55{
56 if (g_verbose)
57 {
58 std::cout << context << " PHY RX END OK p:" << p << std::endl;
59 }
60 g_rxBytes += p->GetSize();
61}
62
72{
73 public:
82 void UpdatePathloss(std::string context,
85 double lossDb);
86
91 void Print();
92
93 private:
94 std::map<uint32_t, std::map<uint32_t, double>> m_pathlossMap;
95};
96
97void
99 Ptr<const SpectrumPhy> txPhyConst,
100 Ptr<const SpectrumPhy> rxPhyConst,
101 double lossDb)
102{
103 Ptr<SpectrumPhy> txPhy = ConstCast<SpectrumPhy>(txPhyConst);
104 Ptr<SpectrumPhy> rxPhy = ConstCast<SpectrumPhy>(rxPhyConst);
105 uint32_t txNodeId = txPhy->GetMobility()->GetObject<Node>()->GetId();
106 uint32_t rxNodeId = rxPhy->GetMobility()->GetObject<Node>()->GetId();
107 m_pathlossMap[txNodeId][rxNodeId] = lossDb;
108}
109
110void
112{
113 for (std::map<uint32_t, std::map<uint32_t, double>>::const_iterator txit =
114 m_pathlossMap.begin();
115 txit != m_pathlossMap.end();
116 ++txit)
117 {
118 for (std::map<uint32_t, double>::const_iterator rxit = txit->second.begin();
119 rxit != txit->second.end();
120 ++rxit)
121 {
122 std::cout << txit->first << " --> " << rxit->first << " : " << rxit->second << " dB"
123 << std::endl;
124 }
125 }
126}
127
128int
129main(int argc, char** argv)
130{
131 CommandLine cmd(__FILE__);
132 double lossDb = 130;
133 double txPowerW = 0.1;
134 uint64_t phyRate = 500000;
135 uint32_t pktSize = 1000;
136 double simDuration = 0.5;
137 std::string channelType("ns3::SingleModelSpectrumChannel");
138 cmd.AddValue("verbose", "Print trace information if true", g_verbose);
139 cmd.AddValue("lossDb", "link loss in dB", lossDb);
140 cmd.AddValue("txPowerW", "txPower in Watts", txPowerW);
141 cmd.AddValue("phyRate", "PHY rate in bps", phyRate);
142 cmd.AddValue("pktSize", "packet size in bytes", pktSize);
143 cmd.AddValue("simDuration", "duration of the simulation in seconds", simDuration);
144 cmd.AddValue("channelType", "which SpectrumChannel implementation to be used", channelType);
145 cmd.Parse(argc, argv);
146
148 c.Create(2);
149
150 MobilityHelper mobility;
151 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
152 mobility.Install(c);
153 // the actual positions are irrelevant, since we use MatrixPropagationLossModel
154
155 SpectrumChannelHelper channelHelper;
156 channelHelper.SetChannel(channelType);
157 channelHelper.SetPropagationDelay("ns3::ConstantSpeedPropagationDelayModel");
158 Ptr<MatrixPropagationLossModel> propLoss = CreateObject<MatrixPropagationLossModel>();
159 propLoss->SetLoss(c.Get(0)->GetObject<MobilityModel>(),
160 c.Get(1)->GetObject<MobilityModel>(),
161 lossDb,
162 true);
163 channelHelper.AddPropagationLoss(propLoss);
164 Ptr<SpectrumChannel> channel = channelHelper.Create();
165
167
168 uint32_t channelNumber = 1;
169 Ptr<SpectrumValue> txPsd = sf.CreateTxPowerSpectralDensity(txPowerW, channelNumber);
170
171 // for the noise, we use the Power Spectral Density of thermal noise
172 // at room temperature. The value of the PSD will be constant over the band of interest.
173 const double k = 1.381e-23; // Boltzmann's constant
174 const double T = 290; // temperature in Kelvin
175 double noisePsdValue = k * T; // watts per hertz
176 Ptr<SpectrumValue> noisePsd = sf.CreateConstant(noisePsdValue);
177
179 deviceHelper.SetChannel(channel);
180 deviceHelper.SetTxPowerSpectralDensity(txPsd);
181 deviceHelper.SetNoisePowerSpectralDensity(noisePsd);
182 deviceHelper.SetPhyAttribute("Rate", DataRateValue(DataRate(phyRate)));
183 NetDeviceContainer devices = deviceHelper.Install(c);
184
185 PacketSocketHelper packetSocket;
186 packetSocket.Install(c);
187
188 PacketSocketAddress socket;
189 socket.SetSingleDevice(devices.Get(0)->GetIfIndex());
190 socket.SetPhysicalAddress(devices.Get(1)->GetAddress());
191 socket.SetProtocol(1);
192
193 OnOffHelper onoff("ns3::PacketSocketFactory", Address(socket));
194 onoff.SetConstantRate(DataRate(2 * phyRate));
195 onoff.SetAttribute("PacketSize", UintegerValue(pktSize));
196
197 ApplicationContainer apps = onoff.Install(c.Get(0));
198 apps.Start(Seconds(0.0));
199 apps.Stop(Seconds(simDuration));
200
201 Config::Connect("/NodeList/*/DeviceList/*/Phy/RxEndOk", MakeCallback(&PhyRxEndOkTrace));
202
203 GlobalPathlossDatabase globalPathlossDatabase;
204 Config::Connect("/ChannelList/*/$ns3::SpectrumChannel/PathLoss",
205 MakeCallback(&GlobalPathlossDatabase::UpdatePathloss, &globalPathlossDatabase));
206
207 g_rxBytes = 0;
208 Simulator::Stop(Seconds(simDuration + 0.000001));
210
211 if (g_verbose)
212 {
213 globalPathlossDatabase.Print();
214
215 double throughputBps = (g_rxBytes * 8.0) / simDuration;
216 std::cout << "throughput: " << throughputBps << std::endl;
217 std::cout << "throughput: " << std::setw(20) << std::fixed << throughputBps << " bps"
218 << std::endl;
219 std::cout << "phy rate : " << std::setw(20) << std::fixed << phyRate * 1.0 << " bps"
220 << std::endl;
221 double rxPowerW = txPowerW / (std::pow(10.0, lossDb / 10.0));
222 double capacity = 20e6 * log2(1.0 + (rxPowerW / 20.0e6) / noisePsdValue);
223 std::cout << "shannon capacity: " << std::setw(20) << std::fixed << capacity << " bps"
224 << std::endl;
225 }
226
228 return 0;
229}
static bool g_verbose
True if verbose output.
void PhyRxEndOkTrace(std::string context, Ptr< const Packet > p)
Trace for PHY Rx successful end.
static uint64_t g_rxBytes
Rx bytes counter.
Store the last pathloss value for each TX-RX pair.
void Print()
print the stored pathloss values to standard output
void UpdatePathloss(std::string context, Ptr< const SpectrumPhy > txPhy, Ptr< const SpectrumPhy > rxPhy, double lossDb)
update the pathloss value
std::map< uint32_t, std::map< uint32_t, double > > m_pathlossMap
Path loss map.
a polymophic address class
Definition: address.h:100
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
holds a vector of ns3::Application pointers.
void Start(Time start) const
Start all of the Applications in this container at the start time given as a parameter.
void Stop(Time stop) const
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
Parse command-line arguments.
Definition: command-line.h:232
Class for representing data rates.
Definition: data-rate.h:89
AttributeValue implementation for DataRate.
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.
A network Node.
Definition: node.h:56
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition: object.h:471
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:44
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:78
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:140
static void Run()
Run the simulation.
Definition: simulator.cc:176
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:184
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...
Hold an unsigned integer type.
Definition: uinteger.h:45
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:975
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1336
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:702
uint32_t pktSize
packet size used for the simulation (in bytes)