Exemple #1
0
 private void _initKnownIonCompounds()
 {
     KnownIonCompounds.Add(new Compound(new AtomStack[]
     {
         new AtomStack(PeriodicTable.GetElement("Na")),
         new AtomStack(PeriodicTable.GetElement("Cl"))
     }), new Tuple <int, int>(1, 1));
     KnownIonCompounds.Add(new Compound(new AtomStack[]
     {
         new AtomStack(PeriodicTable.GetElement("Na")),
         new AtomStack(PeriodicTable.GetElement("O"))
     }), new Tuple <int, int>(1, 2));
     KnownIonCompounds.Add(new Compound(new AtomStack[]
     {
         new AtomStack(PeriodicTable.GetElement("Ca")),
         new AtomStack(PeriodicTable.GetElement("F"), 2)
     }), new Tuple <int, int>(2, 1));
     KnownIonCompounds.Add(new Compound(new AtomStack[]
     {
         new AtomStack(PeriodicTable.GetElement("Na"), 2),
         new AtomStack(PeriodicTable.GetElement("O"))
     }), new Tuple <int, int>(1, 2));
     KnownIonCompounds.Add(new Compound(new AtomStack[]
     {
         new AtomStack(PeriodicTable.GetElement("Na")),
         new AtomStack(PeriodicTable.GetElement("F"))
     }), new Tuple <int, int>(1, 2));
     KnownIonCompounds.Add(new Compound(new AtomStack[]
     {
         new AtomStack(PeriodicTable.GetElement("Ca")),
         new AtomStack(PeriodicTable.GetElement("Cl"), 2)
     }), new Tuple <int, int>(1, 2));
 }
        /// <summary>
        /// Produces the Hill Notation of the chemical formula
        /// </summary>
        private string GetHillNotation()
        {
            StringBuilder s = new StringBuilder();

            // Find carbons
            if (Elements.ContainsKey(PeriodicTable.GetElement(Constants.CarbonAtomicNumber)))
            {
                s.Append("C" + (Elements[PeriodicTable.GetElement(Constants.CarbonAtomicNumber)] == 1 ? "" : "" + Elements[PeriodicTable.GetElement(Constants.CarbonAtomicNumber)]));
            }

            // Find carbon isotopes
            foreach (var i in PeriodicTable.GetElement(Constants.CarbonAtomicNumber).Isotopes)
            {
                if (Isotopes.ContainsKey(i))
                {
                    s.Append("C{" + i.MassNumber + "}" + (Isotopes[i] == 1 ? "" : "" + Isotopes[i]));
                }
            }

            // Find hydrogens
            if (Elements.ContainsKey(PeriodicTable.GetElement(Constants.HydrogenAtomicNumber)))
            {
                s.Append("H" + (Elements[PeriodicTable.GetElement(Constants.HydrogenAtomicNumber)] == 1 ? "" : "" + Elements[PeriodicTable.GetElement(Constants.HydrogenAtomicNumber)]));
            }

            // Find hydrogen isotopes
            foreach (var i in PeriodicTable.GetElement(Constants.HydrogenAtomicNumber).Isotopes)
            {
                if (Isotopes.ContainsKey(i))
                {
                    s.Append("H{" + i.MassNumber + "}" + (Isotopes[i] == 1 ? "" : "" + Isotopes[i]));
                }
            }

            List <string> otherParts = new List <string>();

            // Find other elements
            foreach (var i in Elements)
            {
                if (i.Key != PeriodicTable.GetElement(Constants.CarbonAtomicNumber) && i.Key != PeriodicTable.GetElement(Constants.HydrogenAtomicNumber))
                {
                    otherParts.Add(i.Key.AtomicSymbol + (i.Value == 1 ? "" : "" + i.Value));
                }
            }

            // Find other isotopes
            foreach (var i in Isotopes)
            {
                if (i.Key.Element != PeriodicTable.GetElement(Constants.CarbonAtomicNumber) && i.Key.Element != PeriodicTable.GetElement(Constants.HydrogenAtomicNumber))
                {
                    otherParts.Add(i.Key.Element.AtomicSymbol + "{" + i.Key.MassNumber + "}" + (i.Value == 1 ? "" : "" + i.Value));
                }
            }

            otherParts.Sort();
            return(s + string.Join("", otherParts));
        }
Exemple #3
0
        public Knowledge()
        {
            var h = new Element("H", 1, 1.00784, 1, 1);

            h.AddIsotope(1, 1.00783, 0.9985);
            h.AddIsotope(2, 2.014, 0.00115);
            var o  = new Element("O", 8, 15.99, 16, 2);
            var ca = new Element("Ca", 20, 40.078, 2, 4);

            ca.AddIsotope(40, 39.96, 0.97);
            var c = new Element("C", 6, 12.0106, 14, 2);

            c.AddIsotope(12, 12, 0.9893);
            c.AddIsotope(13, 13.00335, 0.0107);
            var p  = new Element("P", 15, 30.9738, 15, 3);
            var na = new Element("Na", 11, 22.9898, 1, 3);
            var n  = new Element("N", 7, 14.007, 15, 2);

            n.AddIsotope(14, 14.003, 0.97);
            var s = new Element("S", 16, 32.06, 16, 3);

            s.AddIsotope(32, 31.97207, 0.95);
            var al = new Element("Al", 13, 26.9816, 13, 3);
            var fe = new Element("Fe", 26, 55.845, 8, 4);
            var cl = new Element("Cl", 17, 35.45, 17, 3);
            var si = new Element("Si", 14, 28.085, 14, 3);
            var k  = new Element("K", 19, 39.0983, 1, 4);

            k.AddIsotope(40, 39.963, 0.0017);
            var cu = new Element("Cu", 29, 63.546, 11, 4);
            var f  = new Element("F", 9, 18.9984, 17, 2);

            PeriodicTable.Add(h);
            PeriodicTable.Add(o);
            PeriodicTable.Add(ca);
            PeriodicTable.Add(c);
            PeriodicTable.Add(p);
            PeriodicTable.Add(na);
            PeriodicTable.Add(n);
            PeriodicTable.Add(s);
            PeriodicTable.Add(al);
            PeriodicTable.Add(fe);
            fe.AddIsotope(54, 53.9993, 0.05845);
            PeriodicTable.Add(cl);
            PeriodicTable.Add(si);
            PeriodicTable.Add(k);
            PeriodicTable.Add(cu);
            PeriodicTable.Add(f);
            _initKnownCompounds();
            _initKnownIonCompounds();
        }
        private void tbSearch_TextInput(object sender, TextChangedEventArgs e)
        {
            DataContext = App.ViewModel;
            ObservableCollection <Element> elements        = ((PeriodicTable)DataContext).Items;
            ObservableCollection <Element> updatedelements = new ObservableCollection <Element>();

            foreach (Element atom in elements)
            {
                if ((atom.Name.ToLower().Contains(tbSearch.Text.ToLower()) || atom.Symbol.ToLower().Contains(tbSearch.Text.ToLower())) || tbSearch.Text == "Search")
                {
                    updatedelements.Add(atom);
                }
            }
            PeriodicTable newTable = new PeriodicTable();

            newTable.Items = updatedelements;
            DataContext    = newTable;
        }
Exemple #5
0
        /// <summary>
        /// Parses a string representation of chemical formula and adds the elements
        /// to this chemical formula
        /// </summary>
        /// <param name="formula">the Chemical Formula to parse</param>
        private void ParseFormulaAndAddElements(string formula)
        {
            if (string.IsNullOrEmpty(formula))
            {
                return;
            }

            if (!ValidateFormulaRegex.IsMatch(formula))
            {
                throw new FormatException("Input string for chemical formula was in an incorrect format");
            }

            foreach (Match match in FormulaRegex.Matches(formula))
            {
                string chemsym = match.Groups[1].Value; // Group 1: Chemical Symbol

                Element element = PeriodicTable.GetElement(chemsym);

                int sign = match.Groups[3].Success ? // Group 3 (optional): Negative Sign
                           -1 :
                           1;

                int numofelem = match.Groups[4].Success ? // Group 4 (optional): Number of Elements
                                int.Parse(match.Groups[4].Value, CultureInfo.InvariantCulture) :
                                1;

                if (match.Groups[2].Success) // Group 2 (optional): Isotope Mass Number
                {
                    // Adding isotope!
                    Add(element[int.Parse(match.Groups[2].Value, CultureInfo.InvariantCulture)], sign * numofelem);
                }
                else
                {
                    // Adding element!
                    Add(element, numofelem * sign);
                }
            }
        }
        /// <summary>
        /// Parses a string representation of chemical formula and adds the elements
        /// to this chemical formula.
        /// Use brackets for isotopes (C6H12N2O -> C{13}6H12N2O)
        /// </summary>
        /// <param name="formula">the Chemical Formula to parse</param>
        public static ChemicalFormula ParseFormula(string formula)
        {
            ChemicalFormula f = new ChemicalFormula();

            if (!ValidateFormulaRegex.IsMatch(formula))
            {
                throw new MzLibException("Input string for chemical formula was in an incorrect format: " + formula);
            }

            foreach (Match match in FormulaRegex.Matches(formula))
            {
                string chemsym = match.Groups[1].Value; // Group 1: Chemical Symbol

                Element element = PeriodicTable.GetElement(chemsym);

                int sign = match.Groups[3].Success ? // Group 3 (optional): Negative Sign
                           -1 :
                           1;

                int numofelem = match.Groups[4].Success ? // Group 4 (optional): Number of Elements
                                int.Parse(match.Groups[4].Value, CultureInfo.InvariantCulture) :
                                1;

                if (match.Groups[2].Success) // Group 2 (optional): Isotope Mass Number
                {
                    // Adding isotope!
                    f.Add(element[int.Parse(match.Groups[2].Value, CultureInfo.InvariantCulture)], sign * numofelem);
                }
                else
                {
                    // Adding element!
                    f.Add(element, numofelem * sign);
                }
            }
            return(f);
        }
Exemple #7
0
 private void _initKnownCompounds()
 {
     KnownCompounds.Add(new Compound(new AtomStack[]
     {
         new AtomStack(PeriodicTable.GetElement("H"), 2),
         new AtomStack(PeriodicTable.GetElement("O"))
     }));
     KnownCompounds.Add(new Compound(new AtomStack[]
     {
         new AtomStack(PeriodicTable.GetElement("Ca")),
         new AtomStack(PeriodicTable.GetElement("O"))
     }));
     KnownCompounds.Add(new Compound(new AtomStack[]
     {
         new AtomStack(PeriodicTable.GetElement("P"), 2),
         new AtomStack(PeriodicTable.GetElement("O"), 5)
     }));
     KnownCompounds.Add(new Compound(new AtomStack[]
     {
         new AtomStack(PeriodicTable.GetElement("Na")),
         new AtomStack(PeriodicTable.GetElement("O")),
         new AtomStack(PeriodicTable.GetElement("H"))
     }));
     KnownCompounds.Add(new Compound(new AtomStack[]
     {
         new AtomStack(PeriodicTable.GetElement("H")),
         new AtomStack(PeriodicTable.GetElement("N")),
         new AtomStack(PeriodicTable.GetElement("O"), 3)
     }));
     KnownCompounds.Add(new Compound(new AtomStack[]
     {
         new AtomStack(PeriodicTable.GetElement("H"), 2),
         new AtomStack(PeriodicTable.GetElement("S")),
         new AtomStack(PeriodicTable.GetElement("S"), 4)
     }));
     KnownCompounds.Add(new Compound(new AtomStack[]
     {
         new AtomStack(PeriodicTable.GetElement("H"), 3),
         new AtomStack(PeriodicTable.GetElement("P")),
         new AtomStack(PeriodicTable.GetElement("O"), 4),
     }));
     KnownCompounds.Add(new Compound(new AtomStack[]
     {
         new AtomStack(PeriodicTable.GetElement("H")),
         new AtomStack(PeriodicTable.GetElement("Cl"))
     }));
     KnownCompounds.Add(new Compound(new AtomStack[]
     {
         new AtomStack(PeriodicTable.GetElement("H")),
         new AtomStack(PeriodicTable.GetElement("C")),
         new AtomStack(PeriodicTable.GetElement("N"))
     }));
     KnownCompounds.Add(new Compound(new AtomStack[]
     {
         new AtomStack(PeriodicTable.GetElement("N")),
         new AtomStack(PeriodicTable.GetElement("H"), 3),
     }));
     KnownCompounds.Add(new Compound(new AtomStack[]
     {
         new AtomStack(PeriodicTable.GetElement("Si"), 3),
         new AtomStack(PeriodicTable.GetElement("N"), 4)
     }));
     KnownCompounds.Add(new Compound(new AtomStack[]
     {
         new AtomStack(PeriodicTable.GetElement("C")),
         new AtomStack(PeriodicTable.GetElement("O"), 2)
     }));
     KnownCompounds.Add(new Compound(new AtomStack[]
     {
         new AtomStack(PeriodicTable.GetElement("Na")),
         new AtomStack(PeriodicTable.GetElement("Cl"))
     }));
     KnownCompounds.Add(new Compound(new AtomStack[]
     {
         new AtomStack(PeriodicTable.GetElement("K")),
         new AtomStack(PeriodicTable.GetElement("N")),
         new AtomStack(PeriodicTable.GetElement("O"), 3),
     })); KnownCompounds.Add(new Compound(new AtomStack[]
     {
         new AtomStack(PeriodicTable.GetElement("Cu")),
         new AtomStack(PeriodicTable.GetElement("S")),
         new AtomStack(PeriodicTable.GetElement("O"), 4),
     }));
 }
Exemple #8
0
        static void Main(string[] args)
        {
            Configuration.Load();
            Chemist chemist = new Chemist("Антон");
            var     h       = new Atom(PeriodicTable.GetElement("H"));
            var     o       = new Atom(PeriodicTable.GetElement("O"));
            var     c       = new Atom(PeriodicTable.GetElement("C"));
            //Позволяет использовать тип с большей глубиной наследования, чем задано изначально.
            //Экземпляр IEnumerable<Derived> (IEnumerable(Of Derived) в Visual Basic) можно присвоить переменной типа IEnumerable<Base>
            AtomicCollection <IAtomic> iAtomics = new AtomicCollection <IAtomic>()
            {
                h, o, c
            };
            AtomicCollection <Atom> atoms = new AtomicCollection <Atom>()
            {
                h, o, c
            };

            void DoSomething(IEnumerable <IAtomic> ienumerable)
            {
                foreach (var element in ienumerable)
                {
                    Console.WriteLine(element);
                }
            }

            DoSomething(iAtomics.Concat(atoms));

            //Позволяет использовать более универсальный тип (с меньшей глубиной наследования), чем заданный изначально.
            //Экземпляр Action<Base> (Action(Of Base) в Visual Basic) можно присвоить переменной типа Action<Derived>.
            Action <IAtomic> action      = Console.WriteLine;
            Action <Atom>    atom_action = action;

            action(h);
            atom_action(h);


            CompoundExperiment compoundExperiment = new CompoundExperiment(1);
            Compound           ho = new Compound(new AtomStack[]
            {
                PeriodicTable.GetElement("H"),
                PeriodicTable.GetElement("O")
            });

//            AtomicCollection<IAtomic> collection = new AtomicCollection<IAtomic>(){ho};
//            Console.WriteLine(collection.Contains(new Atom(PeriodicTable.GetElement("H"))));
            compoundExperiment.AtomicCollection.Add(ho);
            compoundExperiment.AtomicCollection.Add(new Atom(PeriodicTable.GetElement("C")));
            compoundExperiment.AtomicCollection.Add(new Atom(PeriodicTable.GetElement("H")));
            compoundExperiment.AtomicCollection.Add(new Atom(PeriodicTable.GetElement("O")));
            compoundExperiment.AtomicCollection.Add(new Atom(PeriodicTable.GetElement("O")));
            IonExperiment ionExperiment = new IonExperiment(1);

            //ionExperiment.AtomicCollection.Add(new Atom(PeriodicTable.GetElement("Na")));
            //ionExperiment.AtomicCollection.Add(new Atom(PeriodicTable.GetElement("O")));
            //ionExperiment.AtomicCollection.Add(new Atom(PeriodicTable.GetElement("H")));
            ionExperiment.AtomicCollection.Add(new Atom(PeriodicTable.GetElement("Ca")));
            ionExperiment.AtomicCollection.Add(new Atom(PeriodicTable.GetElement("F")));
            ionExperiment.AtomicCollection.Add(new Atom(PeriodicTable.GetElement("F")));
            IsotopeExperiment isotopeExperiment = new IsotopeExperiment(1);

            isotopeExperiment.AtomicCollection.Add(new Atom(PeriodicTable.GetElement("Na")));
            chemist.DoExperiment(compoundExperiment);
            chemist.DoExperiment(ionExperiment);
            chemist.DoExperiment(isotopeExperiment);
            var      compoundExperiment2 = new CompoundExperiment(2);
            Compound h3 = new Compound(new AtomStack[]
            {
                new AtomStack(PeriodicTable.GetElement("H"), 3),
            });

            compoundExperiment2.AtomicCollection.Add(h3);
            compoundExperiment2.AtomicCollection.Add(new Atom(PeriodicTable.GetElement("N")));
            compoundExperiment2.AtomicCollection.Add(new Atom(PeriodicTable.GetElement("O")));
            compoundExperiment2.AtomicCollection.Add(new Atom(PeriodicTable.GetElement("Si")));
            chemist.DoExperiment(compoundExperiment2);
        }
        private void calcStuff(String text)
        {
            bool           parIn         = false;
            List <Element> inParElements = new List <Element>();

            for (int i = 0; i < text.Length; i++)
            {
                bool boolbreak = false;
                if (text.ToCharArray()[i] == '(')
                {
                    parIn    = true;
                    formula += '(';
                }
                else if (text.ToCharArray()[i] == ')')
                {
                    parIn    = false;
                    formula += ')';
                    int    t      = 1;
                    bool   chance = true;
                    string number = "";
                    do
                    {
                        if (i + t < text.Length && Char.IsDigit(text, i + t))
                        {
                            number += text.Substring(i + t, 1);
                            t++;
                            chance = true;
                        }
                        else
                        {
                            chance = false;
                        }
                    } while (chance);
                    if (number != "")
                    {
                        int times = Int32.Parse(number);
                        for (int q = 0; q < inParElements.Count; q++)
                        {
                            inParElements[q].Count = inParElements[q].Count * times;
                        }
                        formula += PeriodicTable.convertSubscript(number.ToString());
                    }
                    foreach (Element elem in inParElements)
                    {
                        equation.Add(elem);
                    }
                    inParElements = new List <Element>();
                }

                else if (Char.IsUpper(text, i))
                {
                    if (i + 1 < text.Length)
                    {
                        if (Char.IsLower(text, i + 1))
                        {
                            string letter = text.Substring(i, 2);
                            for (int j = 0; j < elements.Count; j++)
                            {
                                if (elements[j].Symbol == letter)
                                {
                                    Element currentElement = new Element(elements[j]);
                                    int     t      = 2;
                                    bool    chance = true;
                                    string  number = "";
                                    formula += letter;

                                    do
                                    {
                                        if (i + t < text.Length && Char.IsDigit(text, i + t))
                                        {
                                            number += text.Substring(i + t, 1);
                                            t++;
                                            chance = true;
                                        }
                                        else
                                        {
                                            chance = false;
                                        }
                                    } while (chance);
                                    if (number != "")
                                    {
                                        currentElement.Count = Int32.Parse(number);
                                        formula += PeriodicTable.convertSubscript(number.ToString());
                                    }
                                    if (parIn)
                                    {
                                        inParElements.Add(currentElement);
                                    }
                                    else
                                    {
                                        equation.Add(currentElement);
                                    }
                                }
                            }
                        }
                        else
                        {
                            boolbreak = true;
                        }
                    }
                    else
                    {
                        boolbreak = true;
                    }
                    if (Char.IsUpper(text, i) && boolbreak)
                    {
                        string letter = text.Substring(i, 1);
                        for (int j = 0; j < elements.Count; j++)
                        {
                            if (elements[j].Symbol == letter)
                            {
                                Element currentElement = new Element(elements[j]);
                                int     t      = 1;
                                bool    chance = true;
                                string  number = "";
                                formula += letter;
                                //OutputMoles.Text = text.Length + " " + i + t;
                                do
                                {
                                    if (i + t < text.Length && Char.IsDigit(text, i + t))
                                    {
                                        number += text.Substring(i + t, 1);
                                        t++;
                                        chance = true;
                                    }
                                    else
                                    {
                                        chance = false;
                                    }
                                } while (chance);
                                if (number != "")
                                {
                                    currentElement.Count = Int32.Parse(number);
                                    formula += PeriodicTable.convertSubscript(number.ToString());
                                }
                                if (parIn)
                                {
                                    inParElements.Add(currentElement);
                                }
                                else
                                {
                                    equation.Add(currentElement);
                                }
                            }
                        }
                    }
                }
            }
        }