Exemplo n.º 1
0
  void setValue(VTime time, CellPosition pos, double value) {
    int res = currentTime.compareTo(time);
    if (res > 0) {
      System.err.println("non-increment timestamp found: " + time.toString());
      return;
    } else if (res < 0) {
      currentState = new CellState(currentState);
      stateList.add(currentState);

      currentTime = new VTime(time.toString());
      timeList.add(currentTime);
    }
    currentState.setValue(pos, value);
    if (!Double.isNaN(value)) this.setNumberWidth(Double.toString(value).length());
  }
Exemplo n.º 2
0
  public boolean loadInitialValue() {
    Vector v = null;
    CellPosition nt = null;
    int pos;
    double value;
    String data = null;

    clear();

    String iniPath = ini.GetFileName();
    pos = iniPath.lastIndexOf(File.separatorChar);
    if (pos >= 0) iniPath = iniPath.substring(0, pos + 1);
    else iniPath = "";

    if ((v = ini.get(modelName, "width")) != null) {
      cols = Integer.parseInt((String) v.elementAt(0));
      v = ini.get(modelName, "height");
      rows = Integer.parseInt((String) v.elementAt(0));
      dim = new CellPosition(2);
      dim.setValue(0, rows);
      dim.setValue(1, cols);
    } else {
      if ((v = ini.get(modelName, "dim")) != null) {
        dim = new CellPosition((String) v.elementAt(0));
        rows = dim.getValue(0);
        cols = dim.getValue(1);
      }
    }
    if (dim == null) {
      System.err.println("cannot find dimension of cell model " + modelName);
      return false;
    }

    // System.out.println(modelName+" dimension = "+dim.toString());
    nt = new CellPosition(dim.size());

    value = Double.NaN;
    if ((v = ini.get(modelName, "initialvalue")) != null) {
      value = Double.parseDouble((String) v.elementAt(0));
      this.setNumberWidth(Double.toString(value).length());
    }
    currentState = new CellState(dim, value);

    if ((v = ini.get(modelName, "initialrow")) != null) {
      for (int j = 0; j < v.size(); j++) {
        data = (String) v.elementAt(j);
        pos = data.indexOf(' ');
        int row = Integer.parseInt(data.substring(0, pos));
        if (row >= rows) {
          System.err.println(
              "The number of row for initialRow is out of range. It's "
                  + row
                  + " and must be in [ 0, "
                  + (rows - 1)
                  + "]");
          return false;
        }
        nt.setValue(0, row);
        StringTokenizer tokens = new StringTokenizer(data.substring(pos + 1));
        if (tokens.countTokens() < cols) {
          System.err.println(
              "Insuficient data for initialRow. Last row with "
                  + tokens.countTokens()
                  + " elements. Note: May be a middle row definition with less elements.");
          return false;
        }
        for (int col = 0; col < cols; col++) {
          try {
            value = Double.parseDouble(tokens.nextToken());
          } catch (Exception ex) {
            value = Double.NaN;
          }
          nt.setValue(1, col);
          currentState.setValue(nt, value);
          this.setNumberWidth(Double.toString(value).length());
        }
      }
    }

    if ((v = ini.get(modelName, "initialRowValue")) != null) {
      for (int j = 0; j < v.size(); j++) {
        data = (String) v.elementAt(j);
        pos = data.indexOf(' ');
        int row = Integer.parseInt(data.substring(0, pos));
        if (row >= rows) {
          System.err.println(
              "The number of row for initialRow is out of range. It's "
                  + row
                  + " and must be in [ 0, "
                  + (rows - 1)
                  + "]");
          return false;
        }
        data = data.substring(pos + 1).trim();
        if (data.length() == 0) {
          System.err.println(
              "Invalid initial row value for initialRowValue (must be a pair rowNumber rowValues)!");
          return false;
        }

        nt.setValue(0, row);
        if (data.length() != cols) {
          System.err.println(
              "The size of the rows for the initial values of the CoupledCell must be equal to the width value !");
          return false;
        }
        for (int col = 0; col < cols; col++) {
          char ch = data.charAt(col);
          value = (ch >= '0' && ch <= '9') ? ch - '0' : Double.NaN;
          nt.setValue(1, col);
          currentState.setValue(nt, value);
        }
      }
    }

    if ((v = ini.get(modelName, "initialCellsValue")) != null) {
      String filename = (String) v.elementAt(0);
      if (filename.length() > 0) {
        try {
          BufferedReader reader = new BufferedReader(new FileReader(iniPath + filename));

          while ((data = reader.readLine()) != null) {
            if (data.trim().length() > 0) {
              if ((pos = data.indexOf('=')) > 0) {
                nt.setValue(data.substring(0, pos));
                value = Double.NaN;
                try {
                  value = Double.parseDouble(data.substring(pos + 1).trim());
                  this.setNumberWidth(Double.toString(value).length());
                } catch (NumberFormatException e) {
                }

                currentState.setValue(nt, value);
              }
            }
          }
          reader.close();

        } catch (FileNotFoundException e) {
          System.err.println(
              "Can't open the file '" + filename + "' defined by the initialCellsValue clause");
          return false;
        } catch (IOException e) {
          System.err.println(e.getMessage());
          return false;
        }
      }
    }

    if ((v = ini.get(modelName, "initialMapValue")) != null) {
      String filename = (String) v.elementAt(0);
      if (filename.length() > 0) {
        try {
          BufferedReader reader = new BufferedReader(new FileReader(iniPath + filename));
          CellPosition.indexToPos(dim, 0, nt);
          boolean overflow = false;
          while ((data = reader.readLine()) != null && !overflow) {
            data = data.trim();
            if (data.length() > 0) {
              try {
                value = Double.parseDouble(data);
              } catch (NumberFormatException e) {
                value = Double.NaN;
              }
              currentState.setValue(nt, value);
              overflow = nt.next(dim);
            }
          }
          reader.close();

        } catch (FileNotFoundException e) {
          System.err.println(
              "Can't open the file '" + filename + "' defined by the initialCellsValue clause");
          return false;
        } catch (IOException e) {
          System.err.println(e.getMessage());
          return false;
        }
      }
    }

    currentTime = new VTime(0, 0, 0, 0);

    timeList.addElement(currentTime);
    stateList.addElement(currentState);

    return true;
  }
Exemplo n.º 3
0
 public void get(int index, VTime time, CellState state) {
   time.setTime((VTime) timeList.elementAt(index));
   state.setValue((CellState) stateList.elementAt(index));
 }