⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 col-line.c

📁 Service Location Protocol
💻 C
📖 第 1 页 / 共 2 页
字号:
/*******************************************************************************   File Name: col-line.c*   Purpose: column mobility model (nodes move parallel to direction of travel)*   Author: Jeff Boleng*   Date Created:**   Copyright (C) 2004  Toilers Research Group -- Colorado School of Mines**   Please see COPYRIGHT.TXT and LICENSE.TXT for copyright and license*   details.**   Bug fixes:**   Sept. 16th, 2004*   - reported by Rick Martin*   - Code fixed by Rick Martin*   - Verified by Feng Sun*   Fixed the maxX, maxY problem in ns2 when X destination value equals to*   the maximum X value of the simulation range.*******************************************************************************/ #include <stdlib.h>#include <stdio.h>#include <time.h>#include <math.h>#define rand48#define true  1#define false 0#define zero 0.000001#define infinity HUGE_VALdouble startTime = 1000.0;//double startTime = 0.0;#define minX 0.00001#define minY 0.00001/*See bug fix Sept. 16th, 2004 */#define ZERO 0.000000000001double PiOverTwo;double getRand(){  #ifdef rand48    return drand48();  #else    return (double)rand()/(double)RAND_MAX;  #endif}double *xLocRefPt, *yLocRefPt;double *xDestRefPt, *yDestRefPt;double minXlocal, maxXlocal, minYlocal, maxYlocal;double nodeSep=0.0;double maxX=0.0, maxY=0.0;double newX, newY, dist, speed;double *xLocNode, *yLocNode;double *nextEventNode;double *nextEventRefPt;double *startEventRefPt;double *speedRefPt;char output;int nodeIDstart = 0;void shortMove(int which, double when){  /* the group reference point is not done moving, so pick a new move for this node     relative to it's reference point's current location */  minXlocal = xLocRefPt[which]-nodeSep;  if (minXlocal < minX) minXlocal = minX;  maxXlocal = xLocRefPt[which]+nodeSep;  if (maxXlocal >= maxX) maxXlocal = maxX-ZERO;  minYlocal = yLocRefPt[which]-nodeSep;  if (minYlocal < minY) minYlocal = minY;  maxYlocal = yLocRefPt[which]+nodeSep;  if (maxYlocal >= maxY) maxYlocal = maxY-ZERO;  newX = minXlocal + getRand()*(maxXlocal - minXlocal);  newY = minYlocal + getRand()*(maxYlocal - minYlocal);  if (newX < minX) newX = minX;  if (newX >= maxX) newX = maxX-ZERO;  if (newY < minY) newY = minY;  if (newY >= maxY) newY = maxY-ZERO;  /* move node's at twice the speed of the reference point? */  /* speed = 2.0*speedRefPt[which]; */  speed = speedRefPt[which];  if (when != startTime)  {    if (output == 'N')    {      fprintf(stdout, "$ns_ at %.12f \"$node_(%d) setdest %.12f %.12f %.12f\"\n",                       nextEventNode[which], which+nodeIDstart, newX, newY, speed);    } else if (output == 'G') {      /* fprintf(stdout, "#ONE\n"); */      fprintf(stdout, "%d %.10f %.12f %.12f\n",                        which+nodeIDstart, nextEventNode[which], newX, newY);      /* fprintf(stdout, "%.12f %.12f # node %d at %.10f speed=%.10f\n", newX, newY,                        which+nodeIDstart, nextEventNode[which], speed); */    }  }  dist = sqrt((newX-xLocNode[which])*(newX-xLocNode[which])+              (newY-yLocNode[which])*(newY-yLocNode[which]));  xLocNode[which] = newX;  yLocNode[which] = newY;  nextEventNode[which] += dist/speed;  /* be sure to set ref pt dest and ref pt loc correctly      - ref pt dest is final reference point destination      - ref pt loc is the future location of ref pt at the next event for node */  if (when >= nextEventRefPt[which])  {    xLocRefPt[which] = xDestRefPt[which];    yLocRefPt[which] = yDestRefPt[which];  } else {    double xDelta = xDestRefPt[which] - xLocRefPt[which];    double yDelta = yDestRefPt[which] - yLocRefPt[which];    double travelPercent = (nextEventNode[which] - startEventRefPt[which]) /                            (nextEventRefPt[which] - startEventRefPt[which]);    xLocRefPt[which] += travelPercent*xDelta;    yLocRefPt[which] += travelPercent*yDelta;  }  return;}int main(int argc, char *argv[]){  int g, n, i, mult;  int numGroups=0;  int nextNode=0;  int nodesPerGroup=0;  double xSep=0.0, ySep=0.0;  double refPtSep=0.0;  double endTime=0.0, lowest;  double speedMean=0.0, speedDelta=0.0;  double pauseMean=0.0, pauseDelta=0.0;  double pauseTime = 0.0;  int *movingNode;  int *movingRefPt;  double speedLow, pauseLow;  double speedRange, pauseRange;  double direction=0.0;  PiOverTwo = acos(0.0);  if ((argc == 13) || (argc == 14))  {    if (argc == 13)    {      nodeIDstart = 0;    } else {      nodeIDstart = atoi(argv[13]);    }    numGroups      = atoi(argv[1]);    nodesPerGroup  = atoi(argv[2]);    refPtSep       = atof(argv[3]);    nodeSep        = atof(argv[4]);    maxX           = atof(argv[5]);    maxY           = atof(argv[6]);    endTime        = atof(argv[7]);    speedMean      = atof(argv[8]);    speedDelta     = atof(argv[9]);    pauseMean      = atof(argv[10]);    pauseDelta     = atof(argv[11]);    output         = argv[12][0];    fprintf(stdout, "#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");    fprintf(stdout, "#\tnumGroups      = %6d\n", numGroups);    fprintf(stdout, "#\tnodesPerGroup  = %6d\n", nodesPerGroup);    fprintf(stdout, "#\trefPtSep       = %9.2f\n", refPtSep);    fprintf(stdout, "#\tnodeSep        = %9.2f\n", nodeSep);    fprintf(stdout, "#\tmaxX           = %9.2f\n", maxX);    fprintf(stdout, "#\tmaxY           = %9.2f\n", maxY);    fprintf(stdout, "#\tendTime        = %9.2f\n", endTime);    fprintf(stdout, "#\tspeedMean      = %9.2f\n", speedMean);    fprintf(stdout, "#\tspeedDelta     = %9.2f\n", speedDelta);    fprintf(stdout, "#\tpauseMean      = %9.2f\n", pauseMean);    fprintf(stdout, "#\tpauseDelta     = %9.2f\n", pauseDelta);    fprintf(stdout, "#\toutput         = %6c\n", output);    fprintf(stdout, "#\tnodeIDstart    = %6d\n", nodeIDstart);    fprintf(stdout, "#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n");  } else {    fprintf(stdout, "Usage:  col-line <number of groups>\n");    fprintf(stdout, "            <nodes per group>\n");    fprintf(stdout, "            <ref. pt. separation>\n");    fprintf(stdout, "            <node separation from ref. pt.>\n");    fprintf(stdout, "            <max-x> <max-y> <end time>\n");    fprintf(stdout, "            <speed mean> <speed delta>\n");    fprintf(stdout, "            <pause time> <pause time delta>\n");    fprintf(stdout, "            <'N' or 'G'>\n");    fprintf(stdout, "            'N' implies NS2 mobility file\n");    fprintf(stdout, "            'G' implies gnuplot path file\n");    fprintf(stdout, "            [<node ID start> default 0]\n");    return -1;  }  endTime += startTime;  if (output == 'N')  {    fprintf(stdout, "# output format is NS2\n");  } else if (output == 'G') {    fprintf(stdout, "# output format is gnuplot\n");    fprintf(stdout, "set xrange[0:%.12f]\n", maxX);    fprintf(stdout, "set yrange[0:%.12f]\n", maxY);//    if (numGroups != 1)//    {//      fprintf(stderr, "Gnuplot output is only possible with one mobile group.\n");//      return -1;//    } else {      fprintf(stdout, "set multiplot\n");      fprintf(stdout, "plot \'-\' using 3:4 notitle with linespoints\n");//    }  } else {    fprintf(stderr, "Unknown output type requested\n");    return -1;  }  #ifdef rand48    srand48((int)time(NULL));  #else    srand((int)time(NULL));  #endif  speedLow = speedMean-speedDelta;  pauseLow = pauseMean-pauseDelta;  speedRange = 2*speedDelta;  pauseRange = 2*pauseDelta;  movingNode = (int*)malloc(sizeof(int)*numGroups*nodesPerGroup);  xLocNode = (double*)malloc(sizeof(double)*numGroups*nodesPerGroup);  yLocNode = (double*)malloc(sizeof(double)*numGroups*nodesPerGroup);  nextEventNode = (double*)malloc(sizeof(double)*numGroups*nodesPerGroup);  movingRefPt = (int*)malloc(sizeof(int)*numGroups*nodesPerGroup);  speedRefPt = (double*)malloc(sizeof(double)*numGroups*nodesPerGroup);  xLocRefPt = (double*)malloc(sizeof(double)*numGroups*nodesPerGroup);  yLocRefPt = (double*)malloc(sizeof(double)*numGroups*nodesPerGroup);  xDestRefPt = (double*)malloc(sizeof(double)*numGroups*nodesPerGroup);  yDestRefPt = (double*)malloc(sizeof(double)*numGroups*nodesPerGroup);  nextEventRefPt = (double*)malloc(sizeof(double)*numGroups*nodesPerGroup);  startEventRefPt = (double*)malloc(sizeof(double)*numGroups*nodesPerGroup);  fprintf(stdout, "#\tInitial positions:\n");  fprintf(stdout, "#\t  (node initial positions are also initial reference point positions)\n");  for (g=0; g<numGroups; g++)  {     int horizontal = (getRand()>=0.5)?true:false;    fprintf(stdout,"#\tHorizontal is %s.\n", (horizontal)?"true":"false");    //mult = (getRand()>=0.5)?-1:1;    mult = 1;    fprintf(stdout,"#\tMult is %d.\n", mult);    /* node 0 in each group is also the group reference point */    /* it can be placed in a totally random fashion */    /* each other point in the group must be placed in reference to it */    movingNode[g*nodesPerGroup] = movingRefPt[g*nodesPerGroup] = (getRand()>=0.5)?true:false;    //movingNode[g*nodesPerGroup] = movingRefPt[g*nodesPerGroup] = false;    xLocNode[g*nodesPerGroup] = xLocRefPt[g*nodesPerGroup] = getRand()*maxX;    yLocNode[g*nodesPerGroup] = yLocRefPt[g*nodesPerGroup] = getRand()*maxY;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -