Esempio n. 1
0
  void diffuse(HeatBugs heatbugs, int start, int end) {
    // locals are faster than instance variables
    final DoubleGrid2D _valgrid = heatbugs.valgrid;
    final double[][] _valgrid_field = heatbugs.valgrid.field;
    final double[][] _valgrid2_field = heatbugs.valgrid2.field;
    final int _gridHeight = _valgrid.getHeight();
    final double _evaporationRate = heatbugs.evaporationRate;
    final double _diffusionRate = heatbugs.diffusionRate;

    double average;

    double[] _past = _valgrid_field[_valgrid.stx(start - 1)];
    double[] _current = _valgrid_field[start];
    double[] _next;
    double[] _put;

    int yminus1;
    int yplus1;

    // for each x and y position
    for (int x = start; x < end; x++) {
      _next = _valgrid_field[_valgrid.stx(x + 1)];
      _put = _valgrid2_field[_valgrid.stx(x)];

      yminus1 = _valgrid.sty(-1); // initialized
      for (int y = 0; y < _gridHeight; y++) {
        // for each neighbor of that position
        // go across top
        yplus1 = _valgrid.sty(y + 1);
        average =
            (_past[yminus1]
                    + _past[y]
                    + _past[yplus1]
                    + _current[yminus1]
                    + _current[y]
                    + _current[yplus1]
                    + _next[yminus1]
                    + _next[y]
                    + _next[yplus1])
                / 9.0;

        // load the new value into HeatBugs.this.valgrid2
        _put[y] = _evaporationRate * (_current[y] + _diffusionRate * (average - _current[y]));

        // set y-1 to what y was "last time around"
        yminus1 = y;
      }

      // swap elements
      _past = _current;
      _current = _next;
    }
  }
  void diffuse(HexaBugs hexabugs, int start, int end) {
    // stolen from HeatBugs and modified for our own purposes
    // locals are faster than instance variables
    final DoubleGrid2D _valgrid = updateGrid;
    final double[][] _valgrid_field = updateGrid.field;
    final double[][] _valgrid2_field = tempGrid.field;
    final int _gridWidth = _valgrid.getWidth();
    final int _gridHeight = _valgrid.getHeight();
    final double _evaporationRate = evaporationRate;
    final double _diffusionRate = diffusionRate;

    double average;
    double[] _past = _valgrid_field[_valgrid.stx(start - 1)];
    double[] _current = _valgrid_field[start];
    double[] _next;
    double[] _put;

    int yminus1;
    int yplus1;

    // for each x and y position
    for (int x = start; x < end; x++) {
      int xplus1 = x + 1;
      if (xplus1 == _gridWidth) xplus1 = 0;
      _next = _valgrid_field[xplus1];
      _put = _valgrid2_field[x];
      boolean xmodulo2equals0 = x % 2 == 0;

      yminus1 = _gridHeight - 1; // initialized
      for (int y = 0; y < _gridHeight; y++) {
        // for each neighbor of that position
        // go across top
        yplus1 = y + 1;
        if (yplus1 == _gridHeight) yplus1 = 0;
        if (xmodulo2equals0) {
          average =
              (_current[y]
                      + // CURRENT
                      _past[yminus1]
                      + // UL
                      _next[yminus1]
                      + // UR
                      _past[y]
                      + // DL
                      _next[y]
                      + // DR
                      _current[yminus1]
                      + // UP
                      _current[yplus1] // DOWN
                  )
                  / 7.0;
        } else {
          average =
              (_current[y]
                      + // CURRENT
                      _past[y]
                      + // UL
                      _next[y]
                      + // UR
                      _past[yplus1]
                      + // DL
                      _next[yplus1]
                      + // DR
                      _current[yminus1]
                      + // UP
                      _current[yplus1] // DOWN
                  )
                  / 7.0;
        }

        // load the new value into HeatBugs.this.valgrid2
        _put[y] = _evaporationRate * (_current[y] + _diffusionRate * (average - _current[y]));

        // set y-1 to what y was "last time around"
        yminus1 = y;
      }

      // swap elements
      _past = _current;
      _current = _next;
    }
  }
Esempio n. 3
0
  // HEATMAPS
  public void start() {
    super.start();
    try {

      String foutDir =
          "/Users/swise/Dissertation/Colorado/finalLegionResults/nofire/processed/heatmaps";
      String dataDir =
          "/Users/swise/Dissertation/Colorado/finalLegionResults/nofire/heatmaps"; // -1";

      // go through each of the relevant files and read in the data

      File folder = new File(dataDir);
      File[] runFiles = folder.listFiles();

      HashMap<String, DoubleGrid2D> differentRuns = new HashMap<String, DoubleGrid2D>();

      for (File f : runFiles) {

        String filename = f.getName();
        if (!filename.endsWith(fileType)) // only specific kinds of files
        continue;

        // the group of runs to which this result belongs
        String runGroup = filename.substring(7, filename.lastIndexOf("_"));

        // Open the file as an input stream
        FileInputStream fstream;
        fstream = new FileInputStream(f.getAbsoluteFile());

        // Convert our input stream to a BufferedReader
        BufferedReader d = new BufferedReader(new InputStreamReader(fstream));

        String s = d.readLine(); // get rid of the header (MAYBE)

        DoubleGrid2D myGrid = differentRuns.get(runGroup);
        if (myGrid == null) {
          String[] bits = s.split("\t");
          myGrid = new DoubleGrid2D(Integer.parseInt(bits[0]), Integer.parseInt(bits[1]));
          differentRuns.put(runGroup, myGrid);
        }

        int row = 0;
        // read in the file line by line
        while ((s = d.readLine()) != null) {

          String[] bits = s.split("\t"); // split into columns
          int col = 0;
          for (String bit : bits) {
            int i = Integer.parseInt(bit);
            myGrid.field[row][col] += i;
            col++;
          }
          row++;
        }

        fstream.close();
      }

      for (String runGroup : differentRuns.keySet()) {

        // Open the file for output
        BufferedWriter w = new BufferedWriter(new FileWriter(foutDir + "/" + runGroup + ".txt"));

        DoubleGrid2D myGrid = differentRuns.get(runGroup);

        for (int row = 0; row < myGrid.getWidth(); row++) {
          for (int col = 0; col < myGrid.getHeight(); col++) {
            w.write(myGrid.field[row][col] + "\t");
          }
          w.newLine();
        }

        w.close();
      }

    } catch (Exception e) {
    }
  }