1
/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
3
* Copyright (c) 2010 CTTC
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License version 2 as
7
* published by the Free Software Foundation;
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
* Author: Nicola Baldo <nbaldo@cttc.es>
25
#include <ns3/core-module.h>
26
#include <ns3/network-module.h>
27
#include <ns3/spectrum-model-ism2400MHz-res1MHz.h>
28
#include <ns3/spectrum-model-300kHz-300GHz-log.h>
29
#include <ns3/wifi-spectrum-value-helper.h>
30
#include <ns3/single-model-spectrum-channel.h>
31
#include <ns3/waveform-generator.h>
32
#include <ns3/spectrum-analyzer.h>
36
#include <ns3/friis-spectrum-propagation-loss.h>
37
#include <ns3/propagation-delay-model.h>
38
#include <ns3/mobility-module.h>
39
#include <ns3/spectrum-helper.h>
40
#include <ns3/applications-module.h>
41
#include <ns3/adhoc-aloha-noack-ideal-phy-helper.h>
43
NS_LOG_COMPONENT_DEFINE ("TestAdhocOfdmAloha");
47
static bool g_verbose = false;
48
static uint64_t g_rxBytes;
51
PhyRxEndOkTrace (std::string context, Ptr<const Packet> p)
55
std::cout << context << " PHY RX END OK p:" << p << std::endl;
57
g_rxBytes += p->GetSize ();
61
int main (int argc, char** argv)
65
double txPowerW = 0.1;
66
uint64_t phyRate = 500000;
67
uint32_t pktSize = 1000;
68
double simDuration = 0.5;
69
cmd.AddValue ("verbose", "Print trace information if true", g_verbose);
70
cmd.AddValue ("lossDb", "link loss in dB", lossDb);
71
cmd.AddValue ("txPowerW", "txPower in Watts", txPowerW);
72
cmd.AddValue ("phyRate", "PHY rate in bps", phyRate);
73
cmd.AddValue ("pktSize", "packet size in bytes", pktSize);
74
cmd.AddValue ("simDuration", "duration of the simulation in seconds", simDuration);
75
cmd.Parse (argc, argv);
80
MobilityHelper mobility;
81
Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
82
positionAlloc->Add (Vector (0.0, 0.0, 0.0));
83
positionAlloc->Add (Vector (5.0, 0.0, 0.0));
84
mobility.SetPositionAllocator (positionAlloc);
85
mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
91
SpectrumChannelHelper channelHelper;
92
channelHelper.SetChannel ("ns3::MultiModelSpectrumChannel");
93
channelHelper.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel");
94
Ptr<MatrixPropagationLossModel> propLoss = CreateObject<MatrixPropagationLossModel> ();
95
propLoss->SetLoss (c.Get (0)->GetObject<MobilityModel> (), c.Get (1)->GetObject<MobilityModel> (), lossDb, true);
96
channelHelper.AddPropagationLoss (propLoss);
97
Ptr<SpectrumChannel> channel = channelHelper.Create ();
100
WifiSpectrumValue5MhzFactory sf;
102
uint32_t channelNumber = 1;
103
Ptr<SpectrumValue> txPsd = sf.CreateTxPowerSpectralDensity (txPowerW, channelNumber);
105
// for the noise, we use the Power Spectral Density of thermal noise
106
// at room temperature. The value of the PSD will be constant over the band of interest.
107
const double k = 1.381e-23; //Boltzmann's constant
108
const double T = 290; // temperature in Kelvin
109
double noisePsdValue = k * T; // watts per hertz
110
Ptr<SpectrumValue> noisePsd = sf.CreateConstant (noisePsdValue);
112
AdhocAlohaNoackIdealPhyHelper deviceHelper;
113
deviceHelper.SetChannel (channel);
114
deviceHelper.SetTxPowerSpectralDensity (txPsd);
115
deviceHelper.SetNoisePowerSpectralDensity (noisePsd);
116
deviceHelper.SetPhyAttribute ("Rate", DataRateValue (DataRate (phyRate)));
117
NetDeviceContainer devices = deviceHelper.Install (c);
119
PacketSocketHelper packetSocket;
120
packetSocket.Install (c);
122
PacketSocketAddress socket;
123
socket.SetSingleDevice (devices.Get (0)->GetIfIndex ());
124
socket.SetPhysicalAddress (devices.Get (1)->GetAddress ());
125
socket.SetProtocol (1);
127
OnOffHelper onoff ("ns3::PacketSocketFactory", Address (socket));
128
onoff.SetAttribute ("OnTime", RandomVariableValue (ConstantVariable (250)));
129
onoff.SetAttribute ("OffTime", RandomVariableValue (ConstantVariable (0)));
130
onoff.SetAttribute ("DataRate", DataRateValue (DataRate (2*phyRate)));
131
onoff.SetAttribute ("PacketSize", UintegerValue (pktSize));
133
ApplicationContainer apps = onoff.Install (c.Get (0));
134
apps.Start (Seconds (0.0));
135
apps.Stop (Seconds (simDuration));
137
Config::Connect ("/NodeList/*/DeviceList/*/Phy/RxEndOk", MakeCallback (&PhyRxEndOkTrace));
140
Simulator::Stop (Seconds (simDuration + 0.000001));
145
double throughputBps = (g_rxBytes * 8.0) / simDuration;
146
std::cout << "throughput: " << throughputBps << std::endl;
147
std::cout << "throughput: " << std::setw (20) << std::fixed << throughputBps << " bps" << std::endl;
148
std::cout << "phy rate : " << std::setw (20) << std::fixed << phyRate*1.0 << " bps" << std::endl;
149
double rxPowerW = txPowerW / (pow (10.0, lossDb/10.0));
150
double capacity = 20e6*log2 (1.0 + (rxPowerW/20.0e6)/noisePsdValue);
151
std::cout << "shannon capacity: " << std::setw (20) << std::fixed << capacity << " bps" << std::endl;
157
Simulator::Destroy ();