📄 linklayermodel.java
字号:
outVar.nodePosX[i] = (i%nodesX) * cellLength + rand.nextDouble()*cellLength;
outVar.nodePosY[i] = (i/nodesX) * cellLength + rand.nextDouble()*cellLength;
wrongPlacement = true;
while ( wrongPlacement ) {
for (j = 0; j < i; j = j+1) {
Xdist = outVar.nodePosX[i] - outVar.nodePosX[j];
Ydist = outVar.nodePosY[i] - outVar.nodePosY[j];
// distance between a given pair of nodes
dist = Math.pow((Xdist*Xdist + Ydist*Ydist), 0.5);
if (dist < inVar.d0) {
outVar.nodePosX[i] = (i%nodesX) * cellLength + rand.nextDouble()*cellLength;
outVar.nodePosY[i] = (i/nodesX) * cellLength + rand.nextDouble()*cellLength;
wrongPlacement = true;
break;
}
}
if ( j == i ) {
wrongPlacement = false;
}
}
}
break;
case 3: // RANDOM
if ( (inVar.Xterr <= 0) | (inVar.Yterr <= 0) ) {
System.out.println("\nError: values of TERRAIN_DIMENSIONS must be positive");
System.exit(1);
}
cellLength = Math.sqrt ( inVar.area / inVar.numNodes );
if ( cellLength < (inVar.d0*1.4) ) {
System.out.println ("\nError: on RANDOM topology, density is too high, increase physical terrain");
System.exit(1);
}
for (i = 0; i < inVar.numNodes; i = i+1) {
outVar.nodePosX[i] = rand.nextDouble() * inVar.Xterr;
outVar.nodePosY[i] = rand.nextDouble() * inVar.Yterr;
wrongPlacement = true;
while ( wrongPlacement ) {
for (j = 0; j < i; j = j+1) {
Xdist = outVar.nodePosX[i] - outVar.nodePosX[j];
Ydist = outVar.nodePosY[i] - outVar.nodePosY[j];
// distance between a given pair of nodes
dist = Math.pow((Xdist*Xdist + Ydist*Ydist), 0.5);
if (dist < inVar.d0) {
outVar.nodePosX[i] = rand.nextDouble() * inVar.Xterr;
outVar.nodePosY[i] = rand.nextDouble() * inVar.Yterr;
wrongPlacement = true;
break;
}
}
if ( j == i ) {
wrongPlacement = false;
}
}
}
break;
case 4: // FILE (user-defined topology)
readTopologyFile(inVar.topFile, outVar);
correctTopology (inVar, outVar);
break;
default:
System.out.println("\nError: topology is not correct, please check TOPOLOGY in the configuration file");
System.exit(1);
}
return true;
}
/**
* Checks that user-defined topology does not have inter-node distances less than D0 meter,
* where D0 is the reference distance in the channel model (specified in configuration file)
*
* @param inVar class that stores input parameters from configuration file
* @param outVar class that stores link gains, noise floors and x/y coordinates
* @return true if x/y coordinates provided by user satisfy the condition that no internode distance is lesss than D0
*/
protected static boolean correctTopology ( InputVariables inVar,
OutputVariables outVar )
{
Random rand = new Random();
int i, j;
double Xdist, Ydist, dist, avgDecay;
for (i = 0; i < inVar.numNodes; i = i+1) {
for (j = i+1; j < inVar.numNodes; j = j+1 ) {
Xdist = outVar.nodePosX[i] - outVar.nodePosX[j];
Ydist = outVar.nodePosY[i] - outVar.nodePosY[j];
// distance between a given pair of nodes
dist = Math.pow((Xdist*Xdist + Ydist*Ydist), 0.5);
if (dist < inVar.d0) {
System.out.println("\nError: file " + inVar.topFile + " contains inter-node distances less than one.");
System.exit(1);
}
}
}
return true;
}
/**
* Obtains output power and noise floor for all nodes in the network
*
* @param inVar class that contains radio parameters
* @param outVar class that stores output powers and noise floors
* @return true if all output powers and noise floors were obtained correctly
*/
protected static boolean obtainRadioPtPn ( InputVariables inVar,
OutputVariables outVar )
{
Random rand = new Random();
int i, j;
double t11, t12, t21, t22;
double rn1, rn2;
t11 = 0;
t12 = 0;
t21 = 0;
t22 = 0;
if ( (inVar.s11 == 0) && (inVar.s22 == 0) ) { // symmetric links do nothing
}
else if ( (inVar.s11 == 0) && (inVar.s22 != 0) ) { // both S11 and S22 must be 0 for symmetric links
System.out.println("\nError: symmetric links require both, S11 and S22 to be 0, not only S11.");
System.exit(1);
}
else {
if ( (inVar.s12 != inVar.s21) ) { // check that S is symmetric
System.out.println("\nError: S12 and S21 must have the same value.");
System.exit(1);
}
if ( Math.abs(inVar.s12) > Math.sqrt(inVar.s11*inVar.s22) ) { // check that correlation is within [-1,1]
System.out.println("\nError: S12 (and S21) must be less than sqrt(S11xS22).");
System.exit(1);
}
t11 = Math.sqrt(inVar.s11);
t12 = inVar.s12/Math.sqrt(inVar.s11);
t21 = 0;
t22 = Math.sqrt( (inVar.s11*inVar.s22 - Math.pow( inVar.s12, 2)) / inVar.s11 );
}
for (i = 0; i < inVar.numNodes; i = i+1) {
rn1 = rand.nextGaussian();
rn2 = rand.nextGaussian();
outVar.noisefloor[i] = inVar.pn + t11 * rn1;
outVar.outputpowervar[i] = t12 * rn1 + t22 * rn2;
}
return true;
}
/**
* Obtains gain for all links in the network. The link gain between nodes A and B
* is defined as the output power of A minus the pathloss between A and B.
*
* @param inVar class that contains channel parameters from configuration file
* @param outVar class that stores link gains
* @return true if all link gains were obtained correctly
*/
protected static boolean obtainLinkGain ( InputVariables inVar,
OutputVariables outVar )
{
Random rand = new Random();
int i, j;
double Xdist, Ydist, dist, pathloss;
for (i = 0; i < inVar.numNodes; i = i+1) {
for (j = i+1; j < inVar.numNodes; j = j+1 ) {
Xdist = outVar.nodePosX[i] - outVar.nodePosX[j];
Ydist = outVar.nodePosY[i] - outVar.nodePosY[j];
// distance between a given pair of nodes
dist = Math.pow((Xdist*Xdist + Ydist*Ydist), 0.5);
// mean decay dependent on distance
pathloss = - inVar.pld0 - 10*inVar.n*(Math.log(dist/inVar.d0)/Math.log(10.0)) + ( rand.nextGaussian()*inVar.sigma );
// assymetric links are given by running two different
// R.V.s for each unidirectional link (output power variance).
outVar.linkGain[i][j] = outVar.outputpowervar[i] + pathloss;
outVar.linkGain[j][i] = outVar.outputpowervar[j] + pathloss;
}
}
return true;
}
/**
* Provides link gain and noise floor in file linkgain.out, and the
* X/Y coordinates in file topology.out.
*
* @param inVar class that contains input parameters from configuration file
* @param outVar class that stores link gains, noise floors and x/y coordinates
* @return true if files linkgain.out and topology.out were printed correctly
*/
protected static boolean printFile( InputVariables inVar,
OutputVariables outVar )
{
int i, j;
DecimalFormat posFormat = new DecimalFormat("##0.00");
/*
* Output file for xy coordinates.
*/
try{
FileOutputStream fout = new FileOutputStream("topology.out");
try {
PrintStream myOutput = new PrintStream(fout);
for (i = 0; i < inVar.numNodes; i = i+1) {
myOutput.print( i + "\t" + posFormat.format(outVar.nodePosX[i]) + "\t"+ posFormat.format(outVar.nodePosY[i]) + "\n");
}
}
catch (Exception e) {
System.out.println("\nError : Failed to open a print stream to the linkgain file" + e);
}
}
catch (Exception e) {
System.out.println("\nError : Failed to open the link gain file linkgains.out:" + e);
}
/*
* Output file for link gains.
*/
try{
FileOutputStream fout = new FileOutputStream("linkgain.out");
try {
PrintStream myOutput = new PrintStream(fout);
for (i = 0; i < inVar.numNodes; i = i+1) {
for (j = (i+1); j < inVar.numNodes; j = j+1 ) {
if ( i != j) {
myOutput.print( "gain\t" + i + "\t" + j + "\t" + posFormat.format(outVar.linkGain[i][j]) + "\n");
myOutput.print( "gain\t" + j + "\t" + i + "\t" + posFormat.format(outVar.linkGain[j][i]) + "\n");
}
}
}
for (i = 0; i < inVar.numNodes; i = i+1) {
myOutput.print( "noise\t" + i + "\t" + posFormat.format(outVar.noisefloor[i]) + "\t" + posFormat.format(inVar.wgn) + "\n");
}
}
catch (Exception e) {
System.out.println("\nError : Failed to open a print stream to the linkgain file" + e);
}
}
catch (Exception e) {
System.out.println("\nError : Failed to open the link gain file linkgains.out:" + e);
}
return true;
}
/**
* Obtains nodes coordinates for user-defined topology.
*
* @param inputTopoFile topology file provided by user
* @param outVar class that contains variables to store x/y coordinates of nodes
* @return true if x/y coordinates of user-defined topology file were read correctly.
*/
protected static boolean readTopologyFile ( String inputTopoFile,
OutputVariables outVar )
{
String thisLine;
StringTokenizer st;
int counter = 0;
try {
FileInputStream fin = new FileInputStream(inputTopoFile);
try {
BufferedReader myInput = new BufferedReader(new InputStreamReader(fin));
try {
while ((thisLine = myInput.readLine()) != null) {
if ( !thisLine.equals("") && !thisLine.startsWith("%") &&
!thisLine.startsWith(" ") ) {
st = new StringTokenizer(thisLine, " \t");
int node = Integer.parseInt(st.nextToken());
double x = Double.valueOf(st.nextToken()).doubleValue();
double y = Double.valueOf(st.nextToken()).doubleValue();
outVar.nodePosX[node] = x;
outVar.nodePosY[node] = y;
counter++;
}
}
} // end try
catch (Exception e) {
System.out.println("Error4: " + e);
System.exit(1);
}
} // end try
catch (Exception e) {
System.out.println("Error5: " + e);
System.exit(1);
}
} // end try
catch (Exception e) {
System.out.println("Error: Failed to Open TOPOLOGY_FILE " + inputTopoFile + e);
System.exit(1);
}
return true;
}
/**
* Obtains number of nodes in network when user defines the topology.
*
* @param inputTopoFile topology file provided by user
* @param inVar class that contains variable for number of nodes
* @return true if number of nodes from user-defined topology file were obtained correctly
*/
protected static boolean obtainNumNodes ( String inputTopoFile,
InputVariables inVar )
{
String thisLine;
StringTokenizer st;
int counter = 0;
try {
FileInputStream fin = new FileInputStream(inputTopoFile);
try {
BufferedReader myInput = new BufferedReader(new InputStreamReader(fin));
try {
while ((thisLine = myInput.readLine()) != null) {
if ( !thisLine.equals("") && !thisLine.startsWith("%") &&
!thisLine.startsWith(" ") ) {
counter++;
}
}
inVar.numNodes = counter;
} // end try
catch (Exception e) {
System.out.println("Error4: " + e);
System.exit(1);
}
} // end try
catch (Exception e) {
System.out.println("Error5: " + e);
System.exit(1);
}
} // end try
catch (Exception e) {
System.out.println("Error: Failed to Open TOPOLOGY_FILE " + inputTopoFile + e);
System.exit(1);
}
return true;
}
private static void usage() {
System.err.println("usage: net.tinyos.sim.LinkLayerModel <config file>");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -