📄 randdir.c
字号:
/******************************************************************************* File Name: randdir.c* Purpose: random direction mobility model* 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.******************************************************************************/ #include <stdlib.h>#include <stdio.h>#include <time.h>#include <math.h>#include <unistd.h>#undef rand48#undef DEBUG#define true 1#define false 0#define ZERO 0.00001double Pi;double twoPi;double PiOverTwo;double PiOverFour;double threePiOverTwo;double minX = 0.0;double minY = 0.0;double maxX = 0.0;double maxY = 0.0;double calcNewPosition(double x, double y, double v, double d, double *newX, double *newY);int main(int argc, char *argv[]){ int i; int numNodes=0; int nextNode=0; double endTime=0.0, lowest; double speedMean=0.0, speedDelta=0.0; double pauseMean=0.0, pauseDelta=0.0; double travelTime=0.0; int *moving; double *nextEvent; double *xLoc, *yLoc; double newX, newY, speed, direction; double speedLow; double speedRange; double pauseLow; double pauseRange; char output; int noPause; Pi = 2.0 * asin(1.0); twoPi = 2.0 * Pi; PiOverTwo = Pi/2.0; PiOverFour = Pi/4.0; threePiOverTwo = (3.0/2.0) * Pi; if (argc == 10) { numNodes = atoi(argv[1]); maxX = atof(argv[2]); maxY = atof(argv[3]); endTime = atof(argv[4]); speedMean = atof(argv[5]); speedDelta = atof(argv[6]); pauseMean = atof(argv[7]); pauseDelta = atof(argv[8]); output = (char)argv[9][0]; fprintf(stdout, "#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); fprintf(stdout, "#\tnumNodes = %6d\n", numNodes); 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, "#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n"); } else { fprintf(stdout, "Usage: randdir <number of nodes>\n"); fprintf(stdout, " <max-x> <max-y> <end time>\n"); fprintf(stdout, " <speed mean> <speed delta>\n"); fprintf(stdout, " <pause mean> <pause delta>\n"); fprintf(stdout, " <'N' or 'G'>\n"); fprintf(stdout, " 'N' implies NS2 mobility file\n"); fprintf(stdout, " 'G' implies gnuplot path file\n"); return -1; } if (output == 'N') { fprintf(stdout, "# output format is NS2\n"); } else if (output == 'G') { fprintf(stdout, "# output format is gnuplot\n"); if (numNodes != 1) { fprintf(stderr, "Gnuplot output is only possible with one mobile node.\n"); return -1; } else { fprintf(stdout, "plot \'-\' notitle with linespoints\n"); } } else { fprintf(stderr, "Unknown output type requested\n"); return -1; } #ifdef rand48 srand48((int)time(NULL)+getpid()); #else srand((int)time(NULL)+getpid()); #endif speedLow = speedMean-speedDelta; speedRange = 2*speedDelta; pauseLow = pauseMean-pauseDelta; pauseRange = 2*pauseDelta; if((pauseMean==0.0)&&(pauseDelta==0.0)) noPause=1; else noPause=0; moving = (int*)malloc(sizeof(int)*numNodes); nextEvent = (double*)malloc(sizeof(double)*numNodes); xLoc = (double*)malloc(sizeof(double)*numNodes); yLoc = (double*)malloc(sizeof(double)*numNodes); if (output == 'N') { fprintf(stdout, "#\tInitial positions:\n"); } for (i=0; i<numNodes; i++) { #ifdef rand48 moving[i] = (drand48()>=0.5)?true:false; xLoc[i] = drand48()*maxX; yLoc[i] = drand48()*maxY; #else moving[i] = (rand()>=(RAND_MAX/2))?true:false; /* fprintf(stdout, "$node_(%d) is ", i); if (moving[i]) { fprintf(stderr, "moving\n"); } else { fprintf(stderr, "not moving\n"); } */ xLoc[i] = ((double)rand()/(double)RAND_MAX)*maxX; yLoc[i] = ((double)rand()/(double)RAND_MAX)*maxY; #endif if (xLoc[i] >= maxX) { xLoc[i] = maxX - ZERO; } else if (xLoc[i] <= minX) { xLoc[i] = minX + ZERO; } if (yLoc[i] >= maxY) { yLoc[i] = maxY - ZERO; } else if (yLoc[i] <= minY) { yLoc[i] = minY + ZERO; } if (output == 'N') { fprintf(stdout, "$node_(%d) set X_ %.12f\n", i, xLoc[i]); fprintf(stdout, "$node_(%d) set Y_ %.12f\n", i, yLoc[i]); fprintf(stdout, "$node_(%d) set Z_ %.12f\n", i, 0.0); } else if (output == 'G') { xLoc[i] = maxX/2.0; yLoc[i] = maxY/2.0; fprintf(stdout, "%.12f %.12f\n", xLoc[i], yLoc[i]); } nextEvent[i] = 0.0; } if (output == 'N') { fprintf(stdout, "\n\n#\tMovements:\n"); } lowest = endTime; /* initialize high so all starting movements are scheduled and output */ while (lowest <= endTime) { if ((moving[nextNode]) && (!noPause)) { moving[nextNode] = false; speed = 0.0; if (output == 'N') { fprintf(stdout, "$ns_ at %.12f \"$node_(%d) setdest %.12f %.12f %.12f\"\n", nextEvent[nextNode], nextNode, xLoc[nextNode], yLoc[nextNode], speed); } #ifdef rand48 nextEvent[nextNode] += drand48()*pauseRange + pauseLow; #else nextEvent[nextNode] += ((double)rand()/(double)RAND_MAX)*pauseRange + pauseLow; #endif } else /* not moving */ { moving[nextNode] = true; #ifdef rand48 speed = drand48()*speedRange + speedLow; direction = drand48()*twoPi; #else speed = ((double)rand()/(double)RAND_MAX)*speedRange + speedLow; direction = ((double)rand()/(double)RAND_MAX)*twoPi; #endif travelTime = calcNewPosition(xLoc[nextNode], yLoc[nextNode], speed, direction, &newX, &newY); if (newX >= maxX) { newX = maxX - ZERO; } else if (newX <= minX) { newX = minX + ZERO; } if (newY >= maxY) { newY = maxY - ZERO; } else if (newY <= minY) { newY = minY + ZERO; } if (output == 'N') { fprintf(stdout, "$ns_ at %.10f \"$node_(%d) setdest %.10f %.10f %.10f\"\n", nextEvent[nextNode], nextNode, newX, newY, speed); } else if (output == 'G') { fprintf(stdout, "%.12f %.12f # node %d at %.10f speed=%.10f\n", newX, newY, nextNode, nextEvent[nextNode], speed); } xLoc[nextNode] = newX; yLoc[nextNode] = newY; nextEvent[nextNode] += travelTime; } /* if (moving[nextNode]) */ /* find new lowest */ lowest = endTime + 1.0; for (i=0; i<numNodes; i++) { if (nextEvent[i] < lowest) { lowest = nextEvent[i]; nextNode = i; } /* if (nextEvent[i] <= lowest) */ } /* for (i=0; i<numNodes; i++) */ } /* while (lowest <= endTime) */ if (output == 'N') { fprintf(stdout, "\n\n\n"); } else if (output == 'G') { fprintf(stdout, "e\n\n"); } free(nextEvent); free(xLoc); free(yLoc); return 0;}double calcNewPosition(double x, double y, double v, double d, double *newX, double *newY){ double xTime, yTime; double time = 0.0; if ((d >= 0.0) && (d < PiOverTwo)) { xTime = fabs(maxX-x)/fabs(v*cos(d)); yTime = fabs(maxY-y)/fabs(v*sin(d)); if (xTime < yTime) { /* hit the side first */ *newX = maxX; *newY = y + v*sin(d)*xTime; time = xTime; } else { /* hit top first */ *newX = x + v*cos(d)*yTime; *newY = maxY; time = yTime; } } else if ((d >= PiOverTwo) && (d < Pi)) { xTime = fabs(minX-x)/fabs(v*cos(d)); yTime = fabs(maxY-y)/fabs(v*sin(d)); if (xTime < yTime) { /* hit the side first */ *newX = minX; *newY = y + v*sin(d)*xTime; time = xTime; } else { /* hit top first */ *newX = x + v*cos(d)*yTime; *newY = maxY; time = yTime; } } else if ((d >= Pi) && (d < threePiOverTwo)) { xTime = fabs(minX-x)/fabs(v*cos(d)); yTime = fabs(minY-y)/fabs(v*sin(d)); if (xTime < yTime) { /* hit the side first */ *newX = minX; *newY = y + v*sin(d)*xTime; time = xTime; } else { /* hit top first */ *newX = x + v*cos(d)*yTime; *newY = minY; time = yTime; } } else if ((d >= threePiOverTwo) && (d <= twoPi)) { xTime = fabs(maxX-x)/fabs(v*cos(d)); yTime = fabs(minY-y)/fabs(v*sin(d)); if (xTime < yTime) { /* hit the side first */ *newX = maxX; *newY = y + v*sin(d)*xTime; time = xTime; } else { /* hit top first */ *newX = x + v*cos(d)*yTime; *newY = minY; time = yTime; } } return time;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -