给出一个基于ns-3的Propagation Loss Models传播模型性能仿真的代码
时间: 2024-02-15 21:05:18 浏览: 47
好的,这里我提供一个基于ns-3的Propagation Loss Models传播模型性能仿真的代码示例,其中使用了Log-Distance Path Loss Model传播模型。
```c++
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/mobility-module.h"
#include "ns3/wifi-module.h"
#include "ns3/internet-module.h"
#include "ns3/applications-module.h"
#include "ns3/netanim-module.h"
#include "ns3/flow-monitor-module.h"
#include "ns3/propagation-loss-model.h"
#include "ns3/log-distance-propagation-loss-model.h"
using namespace ns3;
NS_LOG_COMPONENT_DEFINE ("WifiPropagationLoss");
int main (int argc, char *argv[])
{
LogComponentEnable ("WifiPropagationLoss", LOG_LEVEL_INFO);
// Create nodes
NodeContainer nodes;
nodes.Create (2);
// Create mobility model
MobilityHelper mobility;
mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
"MinX", DoubleValue (0.0),
"MinY", DoubleValue (0.0),
"DeltaX", DoubleValue (5.0),
"DeltaY", DoubleValue (5.0),
"GridWidth", UintegerValue (3),
"LayoutType", StringValue ("RowFirst"));
mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
mobility.Install (nodes);
// Create propagation loss model
Ptr<LogDistancePropagationLossModel> loss = CreateObject<LogDistancePropagationLossModel> ();
loss->SetReference (1.0); // reference distance
loss->SetExponent (2.0); // path loss exponent
loss->SetFrequency (2.4e9); // signal frequency
// Create wifi devices and install them to the nodes
WifiHelper wifi;
wifi.SetStandard (WIFI_PHY_STANDARD_80211b);
wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
"DataMode", StringValue ("DsssRate1Mbps"),
"ControlMode", StringValue ("DsssRate1Mbps"));
WifiMacHelper mac;
mac.SetType ("ns3::AdhocWifiMac");
NetDeviceContainer devices = wifi.Install (mac, nodes);
devices.Get (0)->GetObject<WifiNetDevice> ()->SetPropagationLossModel (loss);
devices.Get (1)->GetObject<WifiNetDevice> ()->SetPropagationLossModel (loss);
// Install internet stack
InternetStackHelper stack;
stack.Install (nodes);
// Assign IP addresses
Ipv4AddressHelper address;
address.SetBase ("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer interfaces = address.Assign (devices);
// Create UDP echo server
UdpEchoServerHelper echoServer (9);
ApplicationContainer serverApps = echoServer.Install (nodes.Get (1));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
// Create UDP echo client and send packets
UdpEchoClientHelper echoClient (interfaces.GetAddress (1), 9);
echoClient.SetAttribute ("MaxPackets", UintegerValue (10));
echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
echoClient.SetAttribute ("PacketSize", UintegerValue (1024));
ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));
// Enable pcap tracing
wifi.EnablePcapAll ("wifi-propagation-loss");
// Create animation
AnimationInterface anim ("wifi-propagation-loss.xml");
anim.SetConstantPosition (nodes.Get (0), 1.0, 1.0);
anim.SetConstantPosition (nodes.Get (1), 2.0, 2.0);
// Enable flow monitor
Ptr<FlowMonitor> flowMonitor;
FlowMonitorHelper flowHelper;
flowMonitor = flowHelper.InstallAll ();
// Run simulation
Simulator::Stop (Seconds (11.0));
Simulator::Run ();
// Print flow monitor statistics
flowMonitor->CheckForLostPackets ();
Ptr<Ipv4FlowClassifier> classifier = DynamicCast<Ipv4FlowClassifier> (flowHelper.GetClassifier ());
std::map<FlowId, FlowMonitor::FlowStats> stats = flowMonitor->GetFlowStats ();
for (std::map<FlowId, FlowMonitor::FlowStats>::const_iterator i = stats.begin (); i != stats.end (); ++i)
{
Ipv4FlowClassifier::FiveTuple tuple = classifier->FindFlow (i->first);
NS_LOG_INFO ("Flow " << i->first << " (" << tuple.sourceAddress << " -> " << tuple.destinationAddress << ")");
NS_LOG_INFO (" Tx Bytes: " << i->second.txBytes);
NS_LOG_INFO (" Tx Packets: " << i->second.txPackets);
NS_LOG_INFO (" Rx Bytes: " << i->second.rxBytes);
NS_LOG_INFO (" Rx Packets: " << i->second.rxPackets);
NS_LOG_INFO (" Throughput: " << i->second.rxBytes * 8.0 / (i->second.timeLastRxPacket.GetSeconds () - i->second.timeFirstTxPacket.GetSeconds ()) / 1024 / 1024 << " Mbps");
}
// Cleanup
Simulator::Destroy ();
return 0;
}
```
以上代码中,我们创建了两个节点,并在节点上安装了Wifi设备和传播模型。然后我们创建了一个UDP echo server和一个UDP echo client,并在仿真过程中发送了10个数据包。同时,我们使用了NetAnim工具生成了一个动画,并使用FlowMonitor统计了网络吞吐量等性能指标。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)