/* -*- 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 进行可视化仿真了,更直观地看到各个节点的分布情况。