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

📄 linklayermodel.java

📁 tinyos-2.x.rar
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/****************************************************************************
 *       
 * "Copyright (c) 2006 The University of Southern California"  
 * All rights reserved.   
 *       
 * Permission to use, copy, modify, and distribute all components of 
 * this software and its documentation for any purpose, without fee, 
 * and without written agreement is hereby granted, 
 * provided that the above copyright notice, the following two paragraphs 
 * and the author names appear in all copies of this software.  
 *     
 * NO REPRESENTATIONS ARE MADE ABOUT THE SUITABILITY OF THE SOFTWARE FOR ANY  
 * PURPOSE. IT IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.  
 *     
 * Neither the software developers, the Autonomous Network Research Group  
 * (ANRG), or USC, shall be liable for any damages suffered from using this  
 * software.   
 *     
 * Author:  Marco Zuniga, Avinash Sridharan
 * Director: Prof. Bhaskar Krishnamachari
 * Autonomous Networks Reseach Group, University of Southern California
 * http://ceng.usc.edu/~anrg
 * Contact: marcozun@usc.edu
 *
 * Date last modified: 2004/07/02 marcozun
 * Date last modified: 2006/02/05 asridhar
 *       
 *     
 * Description: This file contains the code that generates the
 * gains for all links in the network and noise floor values for
 * all nodes.
 *     
 ****************************************************************************/

package net.tinyos.sim;

import java.io.*;
import java.util.*;
import java.text.DecimalFormat;

/**
 * Stores channel, radio and topology parameters provided by user
 * through the configuration file.
 */

class InputVariables {

  // Channel parameters
  double n;  // path loss exponent
  double sigma; // standard deviation shadowing variance
  double  d0;  // reference distance
  double pld0; // power decay for reference distance d0
  // Radio parameters
  double pn;  // radio noise floor
  double wgn; // white gaussian noise 
  // Covariance Matrix for hardware variance
  double  s11;
  double  s12;
  double  s21;
  double  s22;
  // Topology parameters
  int  numNodes; // number of nodes
  int  top;  // topology option
  double grid;  // grid unit
  double Xterr;  // X dimension of Terrain
  double Yterr;  // Y dimension of Terrain
  String topFile; // file name with nodes' coordinates (user-defined)
  // data directly derived from configuration file
  double area;  // area of the terrain


  InputVariables() { // Constructor, loading default values
    n  = 3;
    sigma = 3;
    pld0 = 55;
    d0  = 1;
    pn  = -105;
    wgn  = 4;
    s11  = 3.7;
    s12  = -3.3;
    s21  = -3.3;
    s22  = 6.0;
    numNodes= 0;
    top  = 0;
    Xterr = 0;
    Yterr = 0;
    topFile = "";
    area  = 0;
  }

}

/**
 * Stores nodes' coordinates, link gains and noise floor values for a given topology 
 */

class OutputVariables {

  double[] nodePosX;  // X coordinate
  double[] nodePosY;  // Y coordinate
  double[] outputpowervar; // output power
  double[] noisefloor;  // noise floor
  double[][]  linkGain;  // link gain

  OutputVariables(int numNodes) { // Constructor

    nodePosX = new double[numNodes];
    nodePosY = new double[numNodes];
    outputpowervar = new double[numNodes];
    noisefloor  = new double[numNodes];
    linkGain = new double[numNodes][numNodes];
  }

}


/**
 * Simulates gains for all links of a specific topology, and noise
 * floor values for all nodes.  <p>The link gain between nodes A and B
 * is defined as the output power of A minus the pathloss between A
 * and B.  The user specifies the desired channel, radio and topology
 * parameters through a configuration file.  The configuration file is
 * provided as a command line argument: <tt>$ java LinkLayerModel
 * configurationFileName</tt>, and the link gains and noise floor
 * values are provided on a file called <tt>linkgain.out</tt>.
 */

public class LinkLayerModel  {

  public static void main (String args[]) {
    if (args.length != 1) {
      usage();
      return;
    }
    // variable that contains input parameters
    InputVariables inVar = new InputVariables();
    // parse configuration file and store parameters in inVar
    readFile  ( args[0], inVar );
    // if user defined topology (TOPOLOGY = 4), obtain number of nodes
    if (inVar.top == 4) {
      obtainNumNodes (inVar.topFile, inVar);
    }
    // variable that contains output data
    OutputVariables outVar = new OutputVariables( inVar.numNodes ); 
    // create topology
    System.out.print("Topology ...\t\t\t");
    obtainTopology ( inVar, outVar );
    System.out.println("done");
    // obtain ouput power and noise floor for all nodes
    System.out.print("Radio Pt and Pn ...\t\t");
    obtainRadioPtPn ( inVar, outVar );
    System.out.println("done");
    // obtain link gains 
    System.out.print("Links Gain .....\t\t");
    obtainLinkGain ( inVar, outVar);
    System.out.println("done");
    // print linkgain.out (link gains and noise floor) and topology.out (x/y coordinates)
    System.out.print("Printing Output File ...\t");
    printFile  ( inVar, outVar);
    System.out.println("done");
  }


  /**
   * Parses configuration file provided by user and stores specified
   * parameters
   *
   * @param inputFile configuration file containing channel, radio and
   * deployment parameters
   * @param  var   class that stores input parameters from configuration file
   * @return    true if file parsing was performed without errors
   */

  protected static boolean readFile (String inputFile, InputVariables var )
  {

    String thisLine;
    StringTokenizer st;

    // open configuration file
    try {
      FileInputStream fin =  new FileInputStream(inputFile);
      try {
        BufferedReader myInput = new BufferedReader(new InputStreamReader(fin));
        try {
          // parse the file
          while ((thisLine = myInput.readLine()) != null) {

            if ( !thisLine.equals("") && !thisLine.startsWith("%") ) {
              st = new StringTokenizer(thisLine, " =;\t");
              String key = st.nextToken();
              String value = st.nextToken();

              if ( key.equals("PATH_LOSS_EXPONENT")) {
                var.n = Double.valueOf(value).doubleValue();
                if (var.n < 0) {
                  System.out.println("Error: value of PATH_LOSS_EXPONENT must be positive");
                  System.exit(1);
                }
              }
              else if ( key.equals("SHADOWING_STANDARD_DEVIATION")) {
                var.sigma = Double.valueOf(value).doubleValue();
                if (var.sigma < 0) {
                  System.out.println("Error: value of SHADOWING_STANDARD_DEVIATION must be positive");
                  System.exit(1);
                }
              }
              else if ( key.equals("PL_D0")) {
                var.pld0 = Double.valueOf(value).doubleValue();
                if (var.pld0 < 0) {
                  System.out.println("Error: value of PL_D0 must be positive");
                  System.exit(1);
                }
              }
              else if ( key.equals("D0")) {
                var.d0 = Double.valueOf(value).doubleValue();
                if (var.d0 <= 0) {
                  System.out.println("Error: value of D0 must be greater than zero");
                  System.exit(1);
                }
              }
              else if ( key.equals("NOISE_FLOOR")) {
                var.pn = Double.valueOf(value).doubleValue();
              }
              else if ( key.equals("WHITE_GAUSSIAN_NOISE")) {
                var.wgn = Double.valueOf(value).doubleValue();
                if (var.wgn < 0) {
                  System.out.println("Error: value of WHITE_GAUSSIAN_NOISE must be greater equal than 0");
                  System.exit(1);
                }
              }
              else if ( key.equals("S11")) {
                var.s11 = Double.valueOf(value).doubleValue();
                if (var.s11 < 0) {
                  System.out.println("Error: value of S11 must be greater equal than 0");
                  System.exit(1);
                }
              }
              else if ( key.equals("S12")) {
                var.s12 = Double.valueOf(value).doubleValue();
              }
              else if ( key.equals("S21")) {
                var.s21 = Double.valueOf(value).doubleValue();
              }
              else if ( key.equals("S22")) {
                var.s22 = Double.valueOf(value).doubleValue();
                if (var.s22 < 0) {
                  System.out.println("Error: value of S22 must be greater equal than 0");
                  System.exit(1);
                }
              }
              else if ( key.equals("NUMBER_OF_NODES")) {
                var.numNodes = Integer.parseInt(value);
                if (var.numNodes <= 0) {
                  System.out.println("Error: value of NUMBER_OF_NODES must be positive");
                  System.exit(1);
                }
              } 
              else if ( key.equals("TOPOLOGY")) {
                var.top = Integer.parseInt(value);
                if ( (var.top < 1) | (var.top > 4) ) {
                  System.out.println("Error: value of TOPOLOGY must be between 1 and 4");
                  System.exit(1);
                }
              }
              else if ( key.equals("GRID_UNIT")) {
                var.grid = Double.valueOf(value).doubleValue();
              }
              else if ( key.equals("TOPOLOGY_FILE")) {
                var.topFile = value;
              }
              else if ( key.equals("TERRAIN_DIMENSIONS_X")) {
                var.Xterr = Double.valueOf(value).doubleValue();
                if (var.Xterr < 0) {
                  System.out.println("Error: value of TERRAIN_DIMENSIONS_X must be positive");
                  System.exit(1);
                }
              } 
              else if ( key.equals("TERRAIN_DIMENSIONS_Y")) {
                var.Yterr = Double.valueOf(value).doubleValue();
                if (var.Yterr < 0) {
                  System.out.println("Error: value of TERRAIN_DIMENSIONS_Y must be positive");
                  System.exit(1);
                }
                var.area = var.Xterr * var.Yterr;
              } 
              else {
                System.out.println("Error: undefined parameter " + key + ", please review your configuration file");
                System.exit(1);
              } 
            }
          } // end while loop
        }
        catch (Exception e) {
          System.out.println("Error1: " + e);
          System.exit(1);
        }

      } // end try
      catch (Exception e) {
        System.out.println("Error2: " + e);
        System.exit(1);
      }

    } // end try
    catch (Exception e) {
      System.out.println("Error Failed to Open file " + inputFile + e);
      System.exit(1);
    }

    return true;

  }

  /**
   * Obtain X and Y coordinates for all nodes. Different type of topologies are available 
   * (grid, uniform, random, user-defined)
   * 
   * @param inVar class that contains input parameters from configuration file
   * @param outVar class that stores x/y coordinates
   * @return true if X/Y coordinates are obtained without errors
   */

  protected static boolean obtainTopology( InputVariables  inVar, 
                                           OutputVariables outVar )
  {

    Random rand = new Random();
    int i, j;
    int sqrtNumNodes, nodesX;
    double cellArea, cellLength;
    double Xdist, Ydist, dist;
    boolean wrongPlacement;

    if (inVar.numNodes <= 0) {
      System.out.println("\nError: value of NUMBER_OF_NODES must be positive");
      System.exit(1);
    }

    switch (inVar.top) {
    case 1: // GRID
      if (inVar.grid < inVar.d0) {
        System.out.println("\nError: value of GRID_UNIT must be equal or greater than D0");
        System.exit(1);
      }
      sqrtNumNodes = (int) Math.sqrt(inVar.numNodes);
      if ( sqrtNumNodes != Math.sqrt(inVar.numNodes) ) {
        System.out.println ("\nError: on GRID topology, NUMBER_OF_NODES should be the square of a natural number");
        System.exit(1);
      }
      for (i = 0; i < inVar.numNodes; i = i+1) {
        outVar.nodePosX[i] = (i%sqrtNumNodes) * inVar.grid;
        outVar.nodePosY[i] = (i/sqrtNumNodes) * inVar.grid;
      }
      break;
    case 2: // UNIFORM
      sqrtNumNodes = (int) Math.sqrt(inVar.numNodes);
      if ( sqrtNumNodes != Math.sqrt(inVar.numNodes) ) {
        System.out.println ("\nError: on UNIFORM topology, NUMBER_OF_NODES should be the square of a natural number");
        System.exit(1);
      }
      if ( (inVar.Xterr <= 0) | (inVar.Yterr <= 0) ) {
        System.out.println("\nError: values of TERRAIN_DIMENSIONS must be positive");
        System.exit(1);
      }
      if ( inVar.Xterr != inVar.Yterr ) {
        System.out.println("\nError: values of TERRAIN_DIMENSIONS_X and TERRAIN_DIMENSIONS_Y must be equal");
        System.exit(1);
      }
      cellLength = Math.sqrt ( inVar.area / inVar.numNodes );
      nodesX = sqrtNumNodes; 
      if ( cellLength < (inVar.d0*1.4) ) {
        System.out.println ("\nError: on UNIFORM topology, density is too high, increase physical terrain");
        System.exit(1);
      }
      for (i = 0; i < inVar.numNodes; i = i+1) {

⌨️ 快捷键说明

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