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)); }
/// <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); }
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), })); }
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); }