Пример #1
1
  // ## operation writeChemkinSpecies(ReactionModel,SystemSnapshot)
  public static String writeChemkinSpecies(
      ReactionModel p_reactionModel, SystemSnapshot p_beginStatus) {
    // #[ operation writeChemkinSpecies(ReactionModel,SystemSnapshot)

    StringBuilder result = new StringBuilder();
    result.append("SPECIES\n");

    CoreEdgeReactionModel cerm = (CoreEdgeReactionModel) p_reactionModel;

    // write inert gas
    for (Iterator iter = p_beginStatus.getInertGas(); iter.hasNext(); ) {
      String name = (String) iter.next();
      result.append('\t' + name + '\n');
    }

    // write species
    for (Iterator iter = cerm.getSpecies(); iter.hasNext(); ) {
      Species spe = (Species) iter.next();
      result.append('\t' + spe.getChemkinName() + '\n');
    }

    result.append("END\n");

    return result.toString();

    // #]
  }
Пример #2
0
  public void populate(EvolutionState state, int thread) {
    // should we load individuals from a file? -- duplicates are permitted
    if (loadInds != null) {
      try {
        readSubpopulation(state, new LineNumberReader(new FileReader(loadInds)));
      } catch (IOException e) {
        state.output.fatal(
            "An IOException occurred when trying to read from the file "
                + loadInds
                + ".  The IOException was: \n"
                + e);
      }
    } else {
      Hashtable h = null;
      if (numDuplicateRetries >= 1) h = new Hashtable(individuals.length / 2); // seems reasonable

      for (int x = 0; x < individuals.length; x++) {
        for (int tries = 0; tries <= /* Yes, I see that*/ numDuplicateRetries; tries++) {
          individuals[x] = species.newIndividual(state, thread);

          if (numDuplicateRetries >= 1) {
            // check for duplicates
            Object o = h.get(individuals[x]);
            if (o == null) // found nothing, we're safe
            // hash it and go
            {
              h.put(individuals[x], individuals[x]);
              break;
            }
          }
        } // oh well, we tried to cut down the duplicates
      }
    }
  }
Пример #3
0
  public void setup(final EvolutionState state, final Parameter base) {
    Parameter def = defaultBase();

    int size;

    // do we load from a file?
    loadInds = state.parameters.getFile(base.push(P_FILE), null);

    // what species do we use?

    species =
        (Species)
            state.parameters.getInstanceForParameter(
                base.push(P_SPECIES), def.push(P_SPECIES), Species.class);
    species.setup(state, base.push(P_SPECIES));

    // how big should our subpopulation be?

    size = state.parameters.getInt(base.push(P_SUBPOPSIZE), def.push(P_SUBPOPSIZE), 1);
    if (size <= 0)
      state.output.fatal(
          "Subpopulation size must be an integer >= 1.\n",
          base.push(P_SUBPOPSIZE),
          def.push(P_SUBPOPSIZE));

    // How often do we retry if we find a duplicate?
    numDuplicateRetries = state.parameters.getInt(base.push(P_RETRIES), def.push(P_RETRIES), 0);
    if (numDuplicateRetries < 0)
      state.output.fatal(
          "The number of retries for duplicates must be an integer >= 0.\n",
          base.push(P_RETRIES),
          def.push(P_RETRIES));

    individuals = new Individual[size];
  }
Пример #4
0
  // ## operation generateSpeciesStatus(ReactionModel,ArrayList,ArrayList,ArrayList)
  private LinkedHashMap generateSpeciesStatus(
      ReactionModel p_reactionModel,
      ArrayList p_speciesChemkinName,
      ArrayList p_speciesConc,
      ArrayList p_speciesFlux) {
    // #[ operation generateSpeciesStatus(ReactionModel,ArrayList,ArrayList,ArrayList)
    int size = p_speciesChemkinName.size();
    if (size != p_speciesConc.size() || size != p_speciesFlux.size())
      throw new InvalidSpeciesStatusException();
    LinkedHashMap speStatus = new LinkedHashMap();
    for (int i = 0; i < size; i++) {
      String name = (String) p_speciesChemkinName.get(i);
      int ID = parseIDFromChemkinName(name);
      Species spe = SpeciesDictionary.getInstance().getSpeciesFromID(ID);
      double conc = ((Double) p_speciesConc.get(i)).doubleValue();
      double flux = ((Double) p_speciesFlux.get(i)).doubleValue();

      System.out.println(
          String.valueOf(spe.getID())
              + '\t'
              + spe.getName()
              + '\t'
              + String.valueOf(conc)
              + '\t'
              + String.valueOf(flux));

      if (conc < 0) {
        double aTol = ReactionModelGenerator.getAtol();
        // if (Math.abs(conc) < aTol) conc = 0;
        // else throw new NegativeConcentrationException("species " + spe.getName() + " has negative
        // conc: " + String.valueOf(conc));
        if (conc < -100.0 * aTol)
          throw new NegativeConcentrationException(
              "Species " + spe.getName() + " has negative concentration: " + String.valueOf(conc));
      }

      SpeciesStatus ss = new SpeciesStatus(spe, 1, conc, flux);
      speStatus.put(spe, ss);
    }
    return speStatus;

    // #]
  }
Пример #5
0
 /**
  * Reads a subpopulation in binary form, from the format generated by writeSubpopulation(...). If
  * the number of individuals is not identical, the individuals array will be deleted and replaced
  * with a new array, and a warning will be generated as individuals will have to be created using
  * newIndividual(...) rather than readIndividual(...)
  */
 public void readSubpopulation(final EvolutionState state, final DataInput dataInput)
     throws IOException {
   int numIndividuals = dataInput.readInt();
   if (numIndividuals != individuals.length) {
     state.output.warnOnce(
         "On reading subpopulation from binary stream, the subpopulation size was incorrect.\n"
             + "Had to resize and use newIndividual() instead of readIndividual().");
     individuals = new Individual[numIndividuals];
     for (int i = 0; i < individuals.length; i++)
       individuals[i] = species.newIndividual(state, dataInput);
   } else
     for (int i = 0; i < individuals.length; i++) individuals[i].readIndividual(state, dataInput);
 }
Пример #6
0
  /**
   * Reads a subpopulation from the format generated by printSubpopulation(....). If the number of
   * individuals is not identical, the individuals array will be deleted and replaced with a new
   * array, and a warning will be generated as individuals will have to be created using
   * newIndividual(...) rather than readIndividual(...).
   */
  public void readSubpopulation(final EvolutionState state, final LineNumberReader reader)
      throws IOException {
    // read in number of individuals and check to see if this appears to be a valid subpopulation
    int numIndividuals = Code.readIntegerWithPreamble(NUM_INDIVIDUALS_PREAMBLE, state, reader);

    // read in individuals
    if (numIndividuals != individuals.length) {
      state.output.warnOnce(
          "On reading subpopulation from text stream, the subpopulation size didn't match.\n"
              + "Had to resize and use newIndividual() instead of readIndividual().");
      individuals = new Individual[numIndividuals];
      for (int i = 0; i < individuals.length; i++) {
        int j = Code.readIntegerWithPreamble(INDIVIDUAL_INDEX_PREAMBLE, state, reader);
        // sanity check
        if (j != i)
          state.output.warnOnce(
              "On reading subpopulation from text stream, some individual indexes in the subpopulation did not match.");
        individuals[i] = species.newIndividual(state, reader);
      }
    } else
      for (int i = 0; i < individuals.length; i++) {
        int j = Code.readIntegerWithPreamble(INDIVIDUAL_INDEX_PREAMBLE, state, reader);
        // sanity check
        if (j != i)
          state.output.warnOnce(
              "On reading subpopulation from text stream, some individual indexes in the subpopulation did not match.");
        if (individuals[i] != null) individuals[i].readIndividual(state, reader);
        else {
          state.output.warnOnce(
              "On reading subpopulation from text stream, some of the preexisting subpopulation's slots were null.\n"
                  + "Had to use newIndividual() instead of readIndividual().  If you're starting an evolutionary run by reading an\n"
                  + "existing population from a file, this is expected -- ignore this message.");
          individuals[i] = species.newIndividual(state, reader);
        }
      }
  }
Пример #7
0
  // ## operation writeReactorInputFile(ReactionModel,ReactionTime,ReactionTime,SystemSnapshot)
  public boolean writeReactorInputFile(
      ReactionModel p_reactionModel,
      ReactionTime p_beginTime,
      ReactionTime p_endTime,
      SystemSnapshot p_beginStatus) {
    // #[ operation writeReactorInputFile(ReactionModel,ReactionTime,ReactionTime,SystemSnapshot)
    // construct "input" string
    String input = "<?xml version=\"1.0\" standalone=\"no\"?>" + "\n";

    String dir = System.getProperty("RMG.workingDirectory");
    if (!dir.endsWith("/")) dir += "/";
    String dtd = dir + "software/reactorModel/documentTypeDefinitions/reactorInput.dtd";
    input += "<!DOCTYPE reactorinput SYSTEM \"" + dtd + "\">" + "\n";

    input += "<reactorinput>" + "\n";
    input += "<header>" + "\n";
    input += "<title>Reactor Input File</title>" + "\n";
    input +=
        "<description>RMG-generated file used to call an external reactor model</description>"
            + "\n";
    input += "</header>" + "\n";
    input += "<inputvalues>" + "\n";
    input += "<integrationparameters>" + "\n";
    input += "<reactortype>" + reactorType + "</reactortype>" + "\n";
    input +=
        "<starttime units=\""
            + p_beginTime.getUnit()
            + "\">"
            + MathTool.formatDouble(p_beginTime.getTime(), 15, 6)
            + "</starttime>"
            + "\n";
    input +=
        "<endtime units=\""
            + p_endTime.getUnit()
            + "\">"
            + MathTool.formatDouble(p_endTime.getTime(), 15, 6)
            + "</endtime>"
            + "\n";
    //      input += "<starttime units=\"" + p_beginTime.unit + "\">" +
    // MathTool.formatDouble(p_beginTime.time,15,6) +  "</starttime>" + "\n";
    //      input += "<endtime units=\"" + p_endTime.unit + "\">" +
    // MathTool.formatDouble(p_endTime.time,15,6) +  "</endtime>" + "\n";
    input += "<rtol>" + rtol + "</rtol>" + "\n";
    input += "<atol>" + atol + "</atol>" + "\n";
    input += "</integrationparameters>" + "\n";
    input += "<chemistry>" + "\n";
    input += "</chemistry>" + "\n";
    input += "<systemstate>" + "\n";
    input +=
        "<temperature units=\"K\">"
            + MathTool.formatDouble(p_beginStatus.getTemperature().getK(), 15, 6)
            + "</temperature>"
            + "\n";
    input +=
        "<pressure units=\"Pa\">"
            + MathTool.formatDouble(p_beginStatus.getPressure().getPa(), 15, 6)
            + "</pressure>"
            + "\n";
    for (Iterator iter = p_beginStatus.getSpeciesStatus(); iter.hasNext(); ) {
      SpeciesStatus spcStatus = (SpeciesStatus) iter.next();
      Species thisSpecies = spcStatus.getSpecies();
      CoreEdgeReactionModel cerm = (CoreEdgeReactionModel) p_reactionModel;
      if (cerm.containsAsReactedSpecies(thisSpecies)) {
        String spcChemkinName = thisSpecies.getChemkinName();
        double concentration = spcStatus.getConcentration();
        input +=
            "<amount units=\"molPerCm3\" speciesid=\""
                + spcChemkinName
                + "\">"
                + concentration
                + "</amount>"
                + "\n";
      }
    }
    for (Iterator iter = p_beginStatus.getInertGas(); iter.hasNext(); ) {
      String name = (String) iter.next();
      double conc = p_beginStatus.getInertGas(name);
      if (conc != 0.0)
        input +=
            "<amount units=\"molPerCm3\" speciesid=\"" + name + "\">" + conc + "</amount>" + "\n";
    }
    input += "</systemstate>" + "\n";
    input += "</inputvalues>" + "\n";
    input += "</reactorinput>" + "\n";

    // write "input" string to file
    try {
      String file = "chemkin/reactorInput.xml";
      FileWriter fw = new FileWriter(file);
      fw.write(input);
      fw.close();
      return true;
    } catch (Exception e) {
      System.out.println("Error in writing reactorInput.xml!");
      System.out.println(e.getMessage());
      return false;
    }

    // #]
  }
Пример #8
0
  // ## operation writeChemkinThermo(ReactionModel)
  public static String writeChemkinThermo(ReactionModel p_reactionModel) {
    // #[ operation writeChemkinThermo(ReactionModel)
    /*
     String thermoHeader = "! neon added by pey (20/6/04) - used thermo for Ar\n";
    thermoHeader += "Ne                120186Ne  1               G  0300.00   5000.00  1000.00      1\n";
    thermoHeader += " 0.02500000E+02 0.00000000E+00 0.00000000E+00 0.00000000E+00 0.00000000E+00    2\n";
    thermoHeader += "-0.07453750E+04 0.04366001E+02 0.02500000E+02 0.00000000E+00 0.00000000E+00    3\n";
    thermoHeader += " 0.00000000E+00 0.00000000E+00-0.07453750E+04 0.04366001E+02                   4\n";
    thermoHeader += "N2                121286N   2               G  0300.00   5000.00  1000.00      1\n";
    thermoHeader += " 0.02926640e+02 0.01487977e-01-0.05684761e-05 0.01009704e-08-0.06753351e-13    2\n";
    thermoHeader += "-0.09227977e+04 0.05980528e+02 0.03298677e+02 0.01408240e-01-0.03963222e-04    3\n";
    thermoHeader += " 0.05641515e-07-0.02444855e-10-0.01020900e+05 0.03950372e+02                   4\n";
    thermoHeader += "Ar                120186Ar  1               G  0300.00   5000.00  1000.00      1\n";
    thermoHeader += " 0.02500000e+02 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00    2\n";
    thermoHeader += "-0.07453750e+04 0.04366001e+02 0.02500000e+02 0.00000000e+00 0.00000000e+00    3\n";
    thermoHeader += " 0.00000000e+00 0.00000000e+00-0.07453750e+04 0.04366001e+02                   4\n";
         */
    // #]
    String thermoHeader =
        "! The first four sets of polynomial coefficients (Ar, N2, Ne, He) are from         \n";
    thermoHeader +=
        "! THIRD MILLENIUM IDEAL GAS AND CONDENSED PHASE THERMOCHEMICAL DATABASE FOR     \n";
    thermoHeader +=
        "! COMBUSTION WITH UPDATES FROM ACTIVE THERMOCHENICAL TABLES                     \n";
    thermoHeader +=
        "! Authors: Alexander Burcat and Branko Ruscic                                   \n";
    thermoHeader +=
        "!                                                                               \n";
    thermoHeader +=
        "! The rest of the species are estimated by RMG (http://rmg.mit.edu/)            \n";
    // thermoHeader += "! Ar HF298=0.  REF=C.E. Moore 'Atomic Energy Levels' NSRDS-NBS 35 (1971)
    // p.211  \n";
    // thermoHeader += "! NASA Glen (former Lewis) Research Center   (1988)
    //    \n";
    thermoHeader +=
        "Ar                L 6/88Ar  1               G   200.000  6000.000 1000.        1\n";
    thermoHeader +=
        " 0.25000000E+01 0.00000000E+00 0.00000000E+00 0.00000000E+00 0.00000000E+00    2\n";
    thermoHeader +=
        "-0.74537500E+03 0.43796749E+01 0.25000000E+01 0.00000000E+00 0.00000000E+00    3\n";
    thermoHeader +=
        " 0.00000000E+00 0.00000000E+00-0.74537500E+03 0.43796749E+01                   4\n";
    // thermoHeader += "! N2  HF298= 0.0 KJ  REF=TSIV  Max Lst Sq Error Cp @ 6000 K 0.29%
    //    \n";
    thermoHeader +=
        "N2                G 8/02N   2               G   200.000  6000.000 1000.        1\n";
    thermoHeader +=
        " 2.95257637E+00 1.39690040E-03-4.92631603E-07 7.86010195E-11-4.60755204E-15    2\n";
    thermoHeader +=
        "-9.23948688E+02 5.87188762E+00 3.53100528E+00-1.23660988E-04-5.02999433E-07    3\n";
    thermoHeader +=
        " 2.43530612E-09-1.40881235E-12-1.04697628E+03 2.96747038E+00                   4\n";
    // thermoHeader += "!Ne    HF298= 0.0 KJ REF=McBride, Heimel, Ehlers & Gordon
    //    \n";
    // thermoHeader += "!                'Thermodynamic Properties to 6000 K...' NASA SP-3001
    // (1963)   \n";
    thermoHeader +=
        "Ne                L10/90Ne  1               G    200.0   6000.00  1000.0       1\n";
    thermoHeader +=
        " 0.25000000E 01 0.00000000E 00 0.00000000E 00 0.00000000E 00 0.00000000E 00    2\n";
    thermoHeader +=
        "-0.74537500E 03 0.33553227E 01 0.25000000E 01 0.00000000E 00 0.00000000E 00    3\n";
    thermoHeader +=
        " 0.00000000E 00 0.00000000E 00-0.74537498E 03 0.33553227E 01                   4\n";
    // thermoHeader += "7440-59-7
    //    \n";
    // thermoHeader += "He  HF298=0.0 KJ  REF=McBride, Heimel, Ehlers & Gordon "Thermodynamic
    // Properties\n";
    // thermoHeader += "to 6000K ..." NASA SP-3001 1963.
    //    \n";
    thermoHeader +=
        "He REF ELEMENT    L10/90HE 1.   0.   0.   0.G   200.000  6000.000  B   4.00260 1\n";
    thermoHeader +=
        " 2.50000000E+00 0.00000000E+00 0.00000000E+00 0.00000000E+00 0.00000000E+00    2\n";
    thermoHeader +=
        "-7.45375000E+02 9.28723974E-01 2.50000000E+00 0.00000000E+00 0.00000000E+00    3\n";
    thermoHeader +=
        " 0.00000000E+00 0.00000000E+00-7.45375000E+02 9.28723974E-01 0.00000000E+00    4\n\n";

    StringBuilder result = new StringBuilder();
    result.append("THERMO ALL\n");
    result.append("   300.000  1000.000  5000.000\n");
    result.append(thermoHeader);

    CoreEdgeReactionModel cerm = (CoreEdgeReactionModel) p_reactionModel;
    for (Iterator iter = cerm.getSpecies(); iter.hasNext(); ) {
      Species spe = (Species) iter.next();

      if (spe.getNasaThermoSource() != null) {
        result.append("!" + spe.getNasaThermoSource() + "\n");
      }
      result.append(spe.getNasaThermoData() + "\n");
    }
    result.append("END\n");

    // Added by Amrit for Richard's liquid phase chemkin code 05/21/2009
    result.append("\n");

    return result.toString();

    // #]
  }