~ubuntu-branches/ubuntu/quantal/ns3/quantal

« back to all changes in this revision

Viewing changes to ns-3.12.1/src/spectrum/examples/adhoc-aloha-ideal-phy-matrix-propagation-loss-model.cc

  • Committer: Package Import Robot
  • Author(s): YunQiang Su, Aron Xu, YunQiang Su, Upstream
  • Date: 2012-01-06 00:35:42 UTC
  • mfrom: (10.1.5 sid)
  • Revision ID: package-import@ubuntu.com-20120106003542-vcn5g03mhapm991h
Tags: 3.13+dfsg-1
[ Aron Xu ]:
        add tag binary and binary-indep, 
  for not build doc when --binary-arch (Closes: #654493).
[ YunQiang Su ]
        add waf 1.5/1.6 source to debian directory, 
  and build waf from there (Closes: #642217).
[ Upstream ]
  Successfully link with --as-needed option (Closes: #642225).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2
 
/*
3
 
 * Copyright (c) 2010 CTTC
4
 
 *
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;
8
 
 *
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.
13
 
 *
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
17
 
 *
18
 
 * Author: Nicola Baldo <nbaldo@cttc.es>
19
 
 */
20
 
 
21
 
 
22
 
 
23
 
#include <iostream>
24
 
 
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>
33
 
#include <ns3/log.h>
34
 
#include <string>
35
 
#include <iomanip>
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>
42
 
 
43
 
NS_LOG_COMPONENT_DEFINE ("TestAdhocOfdmAloha");
44
 
 
45
 
using namespace ns3;
46
 
 
47
 
static bool g_verbose = false;
48
 
static uint64_t g_rxBytes;
49
 
 
50
 
void
51
 
PhyRxEndOkTrace (std::string context, Ptr<const Packet> p)
52
 
{
53
 
  if (g_verbose)
54
 
    {
55
 
      std::cout << context << " PHY RX END OK p:" << p << std::endl;
56
 
    }
57
 
  g_rxBytes += p->GetSize ();
58
 
}
59
 
 
60
 
 
61
 
int main (int argc, char** argv)
62
 
{
63
 
  CommandLine cmd;
64
 
  double lossDb = 150;
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);
76
 
 
77
 
  NodeContainer c;
78
 
  c.Create (2);
79
 
 
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");
86
 
 
87
 
 
88
 
  mobility.Install (c);
89
 
 
90
 
 
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 ();
98
 
 
99
 
 
100
 
  WifiSpectrumValue5MhzFactory sf;
101
 
 
102
 
  uint32_t channelNumber = 1;
103
 
  Ptr<SpectrumValue> txPsd =  sf.CreateTxPowerSpectralDensity (txPowerW, channelNumber);
104
 
 
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);
111
 
 
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);
118
 
 
119
 
  PacketSocketHelper packetSocket;
120
 
  packetSocket.Install (c);
121
 
 
122
 
  PacketSocketAddress socket;
123
 
  socket.SetSingleDevice (devices.Get (0)->GetIfIndex ());
124
 
  socket.SetPhysicalAddress (devices.Get (1)->GetAddress ());
125
 
  socket.SetProtocol (1);
126
 
 
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));
132
 
 
133
 
  ApplicationContainer apps = onoff.Install (c.Get (0));
134
 
  apps.Start (Seconds (0.0));
135
 
  apps.Stop (Seconds (simDuration));
136
 
 
137
 
  Config::Connect ("/NodeList/*/DeviceList/*/Phy/RxEndOk", MakeCallback (&PhyRxEndOkTrace));
138
 
 
139
 
  g_rxBytes = 0;
140
 
  Simulator::Stop (Seconds (simDuration + 0.000001));
141
 
  Simulator::Run ();
142
 
 
143
 
  if (g_verbose)
144
 
    {
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; 
152
 
 
153
 
    }
154
 
 
155
 
 
156
 
 
157
 
  Simulator::Destroy ();
158
 
  return 0;
159
 
}