Exemple #1
0
        protected void OpenSpecieDB(string filename)
        {
            try
            {
                string db = File.ReadAllText(filename);
                if (!db.Contains("[Species]"))
                {
                    Log.Message(
                        "[Species] token not found in database file",
                        MessageType.Error,
                        new MessageSource(filename));
                    return;
                }
                int elementCount = 0, specieCount = 0;
                for (Match m = s_ElementRegex.Match(db); m.Success; m = m.NextMatch())
                {
                    Element elem = new Element();
                    elem.AtmoicNo = int.Parse(m.Groups["AtmNo"].Value, CultureInfo.InvariantCulture);
                    elem.AtomicMass = double.Parse(m.Groups["AtmWt"].Value, CultureInfo.InvariantCulture);
                    elem.Name = elem.Symbol = m.Groups["Sym"].Value;
                    if (Element.AddElement(elem))
                        elementCount++;
                }

                List<int> groupIndices = new List<int>();
                Dictionary<int, string> groupNames = new Dictionary<int, string>();
                for (Match m = s_GroupRegex.Match(db); m.Success; m = m.NextMatch())
                {
                    int i = int.Parse(m.Groups["Index"].Value, CultureInfo.InvariantCulture);
                    groupIndices.Add(i);
                    groupNames.Add(i, m.Groups["Name"].Value);
                    if (lstSpecies.Groups[m.Groups["Name"].Value] == null)
                        lstSpecies.Groups.Add(m.Groups["Name"].Value, m.Groups["Name"].Value);
                }
                groupIndices.Sort();


                for (Match m = s_CompoundRegex.Match(db); m.Success; m = m.NextMatch())
                {
                    Compound comp = new Compound();
                    comp.Name = m.Groups["Name"].Value;
                    comp.Symbol = m.Groups["Sym"].Value;
                    comp.Index = int.Parse(m.Groups["Index"].Value, CultureInfo.InvariantCulture);
                    int HfOK, CpOK;
                    int.TryParse(m.Groups["HfOK"].Value, out HfOK);
                    int.TryParse(m.Groups["CpOK"].Value, out CpOK);
                    comp.HeatOK = HfOK != 0 && CpOK != 0;
                    int j = 0;
                    while (j < groupIndices.Count && groupIndices[j] < comp.Index)
                        j++;
                    if (j != 0)
                        j--;
                    if (groupNames.Count > 0)//To stop the program from simply crashing.
                        comp.Annotation = groupNames[groupIndices[j]]; 

                    if (m.Groups["Phase"].Value == "Solid")
                        comp.Phase = Phases.Solid;
                    else if (m.Groups["Phase"].Value == "Liquid")
                        comp.Phase = Phases.Liquid;
                    else if (m.Groups["Phase"].Value == "Vapour")
                        comp.Phase = Phases.Gas;
                    for (int i = 0; i < m.Groups["ElementSym"].Captures.Count; i++)
                    {
                        string sym = m.Groups["ElementSym"].Captures[i].Value;
                        if (CreateElems && !Element.ElementList.ContainsKey(sym))
                        {
                            Element.AddElement(new Element(sym
                                , -1
                                , double.NaN));
                            Log.Message("Element '" + sym + "' not found in element database (Found in compound '" + comp.Name + "')",
                                MessageType.Warning,
                                new MessageSource(filename));
                        }
                        comp.Elements.Add(Element.ElementList[sym],
                                          Fraction.ToFraction(m.Groups["ElemCount"].Captures[i].Value));
                    }

                    if (Compound.AddCompound(comp))
                    {
                        specieCount++;
                    }
                }
                txtFilter_TextChanged(this, new EventArgs());
                Log.Message(elementCount.ToString() + " Elements and " + specieCount.ToString() + " Species loaded.",
                    MessageType.Note,
                    new MessageSource(filename));
                TreeNode newNode = new TreeNode(filename);
                //newNode.ContextMenuStrip = menuDatabaseFile;
                treeFiles.Nodes["SpecieDB"].Nodes.Add(newNode);
                treeFiles.Nodes["SpecieDB"].Expand();
                regKey.SetValue("Last Database", filename);
                UpdateLastPath(Path.GetDirectoryName(filename));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Open database", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Log.Message(ex.Message, MessageType.Error);
            }
            DoDatabaseChanged();
        }
 public void AddCompound(Compound c)
 {
   if (m_Cache.Contains(c))
     return;
   m_Cache.Add(c);
   if (!m_bListOk)
   {
     if (!string.IsNullOrEmpty(m_RawString.Trim()))
       m_RawString += m_RawString.TrimEnd('\r', '\n', ',', ' ') + ", " + c.Symbol;
     else
       m_RawString = c.Symbol;
   }
   FireChanged();
 }
 public void RemoveCompound(Compound c)
 {
   if (!m_Cache.Contains(c))
     return;
   while (m_Cache.Contains(c))
     m_Cache.Remove(c);
   if (!m_bListOk)
   {
     List<string> temp = new List<string>(m_RawString.Split(new char[] { ',', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries));
     for (int i = temp.Count - 1; i >= 0; i--)
       if (temp[i].Trim() == c.Symbol)
         temp.RemoveAt(i);
     StringBuilder sb = new StringBuilder(temp.Count);
     for (int i = 0; i < temp.Count; i++)
       if (i < temp.Count - 1)
         sb.Append(temp[i] + ", ");
       else
         sb.Append(temp[i]);
     m_RawString = sb.ToString();
   }
   FireChanged();
 }
 protected int GetCompoundIndex(List<Compound> orderedComps, Dictionary<Compound, Fraction> Comps, Compound comp)
 {
   int i = 0;
   string tempCompString = "";
   while (i < orderedComps.Count)
   {
     if (orderedComps[i] == comp)
       break;
     tempCompString += Comps[orderedComps[i]] + " ";
     tempCompString += orderedComps[i] + " + ";
     i++;
   }
   return tempCompString.Length;
 }
 public static bool AddCompound(Compound comp)
 {
   try
   {
     CompoundList.Add(comp.Symbol, comp);
     return true;
   }
   catch (Exception ex)
   {
     if (SilentAddFail)
       return false;
     else
       throw ex;
   }
 }
 public void DoDatabaseChanged()
 {
   //Extent:
   if (m_Extent.Specie != null && !Compound.CompoundList.ContainsKey(m_Extent.Specie.Symbol))
   {
     Program.Log.Message("Extent specie not found in new database", MessageType.Error);
     m_Extent.Specie = null;
   }
   //HOR:
   if (CustomHeatOfReaction && !Compound.CompoundList.ContainsKey(m_HeatOfReactionSpecie.Symbol))
   {
     Program.Log.Message("Heat of reaction specie not found in new database", MessageType.Error);
     m_HeatOfReactionSpecie = null;
     FireChanged();
   }
   //Spaces to force a reparse:
   ParseProducts(GetProductsString() + " ");
   ParseReactants(GetReactantsString() + " ");
 }
 public int ReactantIndex(Compound c)
 {
   return GetCompoundIndex(m_OrderedReactants, m_Reactants, c);
 }
 public int ProductIndex(Compound c)
 {
   return GetCompoundIndex(m_OrderedProducts, m_Products, c);
 }
    public void AddProduct(Compound compound, Fraction fraction, int location)
    {
      if (!m_bProductsOK)
      {
        throw new Exception("Attempt to add reactant when product string is unparseable");
      }
      if (m_Products.ContainsKey(compound))
      {
        throw new Exception("Attempt to add product that is already in the list of products");
      }
      //Determine where we want to add the new product:
      int i = GetInsertionIndex(m_OrderedProducts, m_Products, location);
      m_OrderedProducts.Insert(i, compound);
      m_Products.Add(compound, 1.0);

      FireChanged();
      if (ProductsChanged != null)
        ProductsChanged(this, new EventArgs());
    }
Exemple #10
0
 /// <summary>
 /// Changes the tag of this reaction's LVI to point to the backup reaction. Also returns the backup reaction, with itself as a backup.
 /// </summary>
 public SimpleReaction Revert()
 {
   this.m_bInitialised = false;
   this.SetRegex(s_GeneralReactionRegex.Match(m_Original.ToSaveString(true)), new MessageSource(""), "");
   this.Enabled = m_Original.Enabled;
   if (!m_Original.CustomHeatOfReaction)
   {
     this.m_HeatOfReactionSpecie = null;
     this.m_dHeatOfReaction = double.NaN;
   }
   m_bChanged = false;
   UpdateStatus(this, new EventArgs());
   this.m_bInitialised = true;
   return this;
 }