NS-3 仿真学习之main-grid-topology.cc 分析

/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
 * Copyright (c) 2006,2007 INRIA
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation;
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/mobility-module.h"

using namespace ns3;

int main(int argc, char *argv[]) {
	CommandLine cmd(__FILE__);
	cmd.Parse(argc, argv);

	NodeContainer nodes;

	// create an array of empty nodes for testing purposes
	nodes.Create(120);

	MobilityHelper mobility;
	// setup the grid itself: objects are laid out
	// started from (-100,-100) with 20 objects per row,
	// the x interval between each object is 5 meters
	// and the y interval between each object is 20 meters
	mobility.SetPositionAllocator("ns3::GridPositionAllocator", "MinX",
			DoubleValue(-100.0), "MinY", DoubleValue(-100.0), "DeltaX",
			DoubleValue(5.0), "DeltaY", DoubleValue(20.0), "GridWidth",
			UintegerValue(20), "LayoutType", StringValue("RowFirst"));
	// each object will be attached a static position.
	// i.e., once set by the "position allocator", the
	// position will never change.
	mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");

	// finalize the setup by attaching to each object
	// in the input array a position and initializing
	// this position with the calculated coordinates.
	mobility.Install(nodes);

	// iterate our nodes and print their position.
	for (NodeContainer::Iterator j = nodes.Begin(); j != nodes.End(); ++j) {
		Ptr<Node> object = *j;
		Ptr<MobilityModel> position = object->GetObject<MobilityModel>();
		NS_ASSERT(position != 0);
		Vector pos = position->GetPosition();
		std::cout << "x=" << pos.x << ", y=" << pos.y << ", z=" << pos.z
				<< std::endl;
	}
	Simulator::Run();
	Simulator::Destroy();
	return 0;
}

源码来自NS-3 3.35 调试sr/mobility/examples/main-grid-topology.cc 

该源码建了120个空节点,按行列排列,分成6行,每行20个节点。节点间x轴方向间隔距离为5米,y轴方向间隔距离为20米。未做其他开发。
该例子原始源码中无Simulator::Run();语句,只在控制台打印出了各个节点的坐标,可加上该语句,便可通过--vis 进行可视化仿真了,更直观地看到各个节点的分布情况。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值