コード例 #1
1
ファイル: Chemkin.java プロジェクト: ecintron/RMG-Java
  // ## 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
ファイル: Chemkin.java プロジェクト: ecintron/RMG-Java
  // ## 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;
    }

    // #]
  }
コード例 #3
0
ファイル: Chemkin.java プロジェクト: ecintron/RMG-Java
  // ## operation writeChemkinReactions(ReactionModel)
  public static String writeChemkinPdepReactions(
      ReactionModel p_reactionModel, SystemSnapshot p_beginStatus) {
    // #[ operation writeChemkinReactions(ReactionModel)

    StringBuilder result = new StringBuilder();
    //      result.append("REACTIONS	KCAL/MOLE\n");

    String reactionHeader = "";

    String units4Ea = ArrheniusKinetics.getEaUnits();
    if (units4Ea.equals("cal/mol")) reactionHeader = "CAL/MOL\t";
    else if (units4Ea.equals("kcal/mol")) reactionHeader = "KCAL/MOL\t";
    else if (units4Ea.equals("J/mol")) reactionHeader = "JOULES/MOL\t";
    else if (units4Ea.equals("kJ/mol")) reactionHeader = "KJOULES/MOL\t";
    else if (units4Ea.equals("Kelvins")) reactionHeader = "KELVINS\t";

    String units4A = ArrheniusKinetics.getAUnits();
    if (units4A.equals("moles")) reactionHeader += "MOLES\n";
    else if (units4A.equals("molecules")) reactionHeader += "MOLECULES\n";

    result.append("REACTIONS\t" + reactionHeader);

    LinkedList pDepList = new LinkedList();
    LinkedList nonPDepList = new LinkedList();
    LinkedList duplicates = new LinkedList();

    CoreEdgeReactionModel cerm = (CoreEdgeReactionModel) p_reactionModel;
    // first get troe and thirdbodyreactions
    for (Iterator iter = cerm.getReactionSet().iterator(); iter.hasNext(); ) {
      Reaction r = (Reaction) iter.next();
      /*
       * 1Jul2009-MRH:
       * 	Added extra set of parenthesis.  Before, if the rxn was reverse but an instance of
       * 		TROEReaction, it would also be added to the pDepList, resulting in RMG reporting
       * 		both rxns (forward and reverse) in the chem.inp file, w/o a DUP tag.  Furthermore,
       * 		both rxns were given the same set of Arrhenius parameters.  Running this in
       * 		Chemkin-v4.1.1 resulted in an error.
       */
      if (r.isForward()
          && (r instanceof ThirdBodyReaction
              || r instanceof TROEReaction
              || r instanceof LindemannReaction)) {
        pDepList.add(r);
      }
    }

    for (Iterator iter = PDepNetwork.getNetworks().iterator(); iter.hasNext(); ) {
      PDepNetwork pdn = (PDepNetwork) iter.next();
      for (ListIterator pdniter = pdn.getNetReactions().listIterator(); pdniter.hasNext(); ) {
        PDepReaction rxn = (PDepReaction) pdniter.next();
        if (cerm.categorizeReaction(rxn) != 1) continue;

        // check if this reaction is not already in the list and also check if this reaction has a
        // reverse reaction
        // which is already present in the list.
        if (rxn.getReverseReaction() == null) rxn.generateReverseReaction();

        if (!rxn.reactantEqualsProduct()
            && !pDepList.contains(rxn)
            && !pDepList.contains(rxn.getReverseReaction())) {
          pDepList.add(rxn);
        }
      }
    }
    LinkedList removeReactions = new LinkedList();
    for (Iterator iter = p_reactionModel.getReactionSet().iterator(); iter.hasNext(); ) {
      Reaction r = (Reaction) iter.next();

      boolean presentInPDep = false;
      if (r.isForward()
          && !(r instanceof ThirdBodyReaction)
          && !(r instanceof TROEReaction)
          && !(r instanceof LindemannReaction)) {
        Iterator r_iter = pDepList.iterator();
        while (r_iter.hasNext()) {
          Reaction pDepr = (Reaction) r_iter.next();
          if (pDepr.equals(r)) {
            //      				removeReactions.add(pDepr);
            //      				duplicates.add(pDepr);
            //      				if (!r.hasAdditionalKinetics()){
            //      					duplicates.add(r);
            //      					presentInPDep = true;
            //      				}
            presentInPDep = true;
            nonPDepList.add(r);
          }
        }
        if (!presentInPDep) nonPDepList.add(r);
      }
    }

    for (Iterator iter = removeReactions.iterator(); iter.hasNext(); ) {
      Reaction r = (Reaction) iter.next();
      pDepList.remove(r);
    }

    for (Iterator iter = pDepList.iterator(); iter.hasNext(); ) {
      Reaction r = (Reaction) iter.next();
      // 6Jul2009-MRH:
      //	Pass both system temperature and pressure to function toChemkinString.
      //		The only PDepKineticsModel that uses the passed pressure is RATE
      result.append(
          r.toChemkinString(p_beginStatus.getTemperature(), p_beginStatus.getPressure())
              + "\n"); // 10/26/07 gmagoon: eliminating use of Global.temperature; **** I use
                       // beginStatus here, which may or may not be appropriate
      // result.append(r.toChemkinString(Global.temperature)+"\n");
    }
    for (Iterator iter = nonPDepList.iterator(); iter.hasNext(); ) {
      Reaction r = (Reaction) iter.next();
      result.append(
          r.toChemkinString(p_beginStatus.getTemperature(), p_beginStatus.getPressure()) + "\n");
      // result.append(r.toChemkinString(Global.temperature)+"\n");
    }
    for (Iterator iter = duplicates.iterator(); iter.hasNext(); ) {
      Reaction r = (Reaction) iter.next();
      result.append(
          r.toChemkinString(p_beginStatus.getTemperature(), p_beginStatus.getPressure())
              + "\n\tDUP\n");
      // result.append(r.toChemkinString(Global.temperature)+"\n\tDUP\n");
    }

    result.append("END\n");

    return result.toString();

    // #]
  }
コード例 #4
0
ファイル: Chemkin.java プロジェクト: ecintron/RMG-Java
  // ## operation readReactorOutputFile(ReactionModel)
  public SystemSnapshot readReactorOutputFile(ReactionModel p_reactionModel) {
    // #[ operation readReactorOutputFile(ReactionModel)
    try {
      // open output file and build the DOM tree
      String dir = System.getProperty("RMG.workingDirectory");
      String filename = "chemkin/reactorOutput.xml";
      File inputFile = new File(filename);

      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      factory.setValidating(true); // validate the document with the DTD
      factory.setIgnoringElementContentWhitespace(true); // ignore whitespace
      DocumentBuilder builder = factory.newDocumentBuilder();
      Document doc = builder.parse(inputFile);

      // get root element and its children
      Element root = doc.getDocumentElement();
      NodeList rootchildren = root.getChildNodes();

      // header is rootchildren.item(0)

      // get return message and check for successful run
      Element returnmessageElement = (Element) rootchildren.item(1);
      Text returnmessageText = (Text) returnmessageElement.getFirstChild();
      String returnmessage = returnmessageText.toString();
      returnmessage = returnmessage.trim();
      if (!returnmessage.contains("SUCCESSFULLY COMPLETED RUN.")) {
        System.out.println("External reactor model failed!");
        System.out.println("Reactor model error message: " + returnmessage);
        System.exit(0);
      }

      // get outputvalues element and its children
      Element outputvaluesElement = (Element) rootchildren.item(2);
      NodeList children = outputvaluesElement.getChildNodes();

      // get time
      Element timeElement = (Element) children.item(0);
      Text timeText = (Text) timeElement.getFirstChild();
      double time = Double.parseDouble(timeText.getData());
      String timeUnits = timeElement.getAttribute("units");

      // get systemstate element and its children
      Element systemstateElement = (Element) children.item(1);
      NodeList states = systemstateElement.getChildNodes();

      // get temperature and its units
      Element temperatureElement = (Element) states.item(0);
      String tempUnits = temperatureElement.getAttribute("units");
      Text temperatureText = (Text) temperatureElement.getFirstChild();
      double temp = Double.parseDouble(temperatureText.getData());
      Temperature T = new Temperature(temp, tempUnits);

      // get pressure and its units
      Element pressureElement = (Element) states.item(1);
      String presUnits = pressureElement.getAttribute("units");
      Text pressureText = (Text) pressureElement.getFirstChild();
      double pres = Double.parseDouble(pressureText.getData());
      Pressure P = new Pressure(pres, presUnits);

      // get species amounts (e.g. concentrations)
      ArrayList speciesIDs = new ArrayList();
      ArrayList amounts = new ArrayList();
      ArrayList fluxes = new ArrayList();
      String amountUnits = null;
      String fluxUnits = null;

      // loop thru all the species
      // begin at i=2, since T and P take already the first two position of states
      int nSpe = (states.getLength() - 2) / 2;
      int index = 0;
      LinkedHashMap inertGas = new LinkedHashMap();
      for (int i = 2; i < nSpe + 2; i++) {
        // get amount element and the units
        Element amountElement = (Element) states.item(i);
        amountUnits = amountElement.getAttribute("units");

        Element fluxElement = (Element) states.item(i + nSpe);
        fluxUnits = fluxElement.getAttribute("units");

        // get speciesid and store in an array list
        String thisSpeciesID = amountElement.getAttribute("speciesid");

        // get amount (e.g. concentraion) and store in an array list
        Text amountText = (Text) amountElement.getFirstChild();
        double thisAmount = Double.parseDouble(amountText.getData());
        if (thisAmount < 0) {
          double aTol = ReactionModelGenerator.getAtol();
          // if (Math.abs(thisAmount) < aTol) thisAmount = 0;
          // else throw new NegativeConcentrationException("Negative concentration in
          // reactorOutput.xml: " + thisSpeciesID);
          if (thisAmount < -100.0 * aTol)
            throw new NegativeConcentrationException(
                "Species "
                    + thisSpeciesID
                    + " has negative concentration: "
                    + String.valueOf(thisAmount));
        }

        // get amount (e.g. concentraion) and store in an array list
        Text fluxText = (Text) fluxElement.getFirstChild();
        double thisFlux = Double.parseDouble(fluxText.getData());

        if (thisSpeciesID.compareToIgnoreCase("N2") == 0
            || thisSpeciesID.compareToIgnoreCase("Ne") == 0
            || thisSpeciesID.compareToIgnoreCase("Ar") == 0) {
          inertGas.put(thisSpeciesID, new Double(thisAmount));
        } else {
          speciesIDs.add(index, thisSpeciesID);
          amounts.add(index, new Double(thisAmount));
          fluxes.add(index, new Double(thisFlux));
          index++;
        }
      }

      // print results for debugging purposes
      /**
       * System.out.println(returnmessage); System.out.println("Temp = " + temp + " " + tempUnits);
       * System.out.println("Pres = " + pres + " " + presUnits); for (int i = 0; i < amounts.size();
       * i++) { System.out.println(speciesIDs.get(i) + " " + amounts.get(i) + " " + amountUnits); }
       */
      ReactionTime rt = new ReactionTime(time, timeUnits);
      LinkedHashMap speStatus = generateSpeciesStatus(p_reactionModel, speciesIDs, amounts, fluxes);
      SystemSnapshot ss = new SystemSnapshot(rt, speStatus, T, P);
      ss.inertGas = inertGas;
      return ss;
    } catch (Exception e) {
      System.out.println("Error reading reactor model output: " + e.getMessage());
      System.exit(0);
      return null;
    }

    // #]
  }