Example #1
0
        public void TestNullArguments()
        {
            ChemicalFormula     formulaA = new ChemicalFormula("CO");
            IHasChemicalFormula ok       = null;

            Assert.AreEqual("item", Assert.Throws <ArgumentNullException>(() => { formulaA.Add(ok); }).ParamName);
            ChemicalFormula ok2 = null;

            Assert.AreEqual("formula", Assert.Throws <ArgumentNullException>(() => { formulaA.Add(ok2); }).ParamName);
            Assert.AreEqual("other", Assert.Throws <ArgumentNullException>(() => { new ChemicalFormula(ok2); }).ParamName);
            Element ok3 = null;

            Assert.AreEqual("element", Assert.Throws <ArgumentNullException>(() => { formulaA.AddPrincipalIsotopesOf(ok3, 0); }).ParamName);
            Assert.AreEqual("item", Assert.Throws <ArgumentNullException>(() => { formulaA.Remove(ok); }).ParamName);
            Assert.AreEqual("formula", Assert.Throws <ArgumentNullException>(() => { formulaA.Remove(ok2); }).ParamName);
            Assert.AreEqual("formula", Assert.Throws <ArgumentNullException>(() => { formulaA.IsSubsetOf(ok2); }).ParamName);
            Assert.AreEqual("formula", Assert.Throws <ArgumentNullException>(() => { formulaA.IsSupersetOf(ok2); }).ParamName);
            Assert.AreEqual("element", Assert.Throws <ArgumentNullException>(() => { formulaA.CountWithIsotopes(ok3); }).ParamName);
            Assert.AreEqual("element", Assert.Throws <ArgumentNullException>(() => { formulaA.CountSpecificIsotopes(ok3, 0); }).ParamName);
            Assert.IsFalse(formulaA.Equals(ok2));
            IEnumerable <IHasChemicalFormula> ok4 = null;

            Assert.AreEqual("formulas", Assert.Throws <ArgumentNullException>(() => { ChemicalFormula.Combine(ok4); }).ParamName);
            Assert.AreEqual("element", Assert.Throws <ArgumentNullException>(() => { PeriodicTable.Add(ok3); }).ParamName);

            Assert.AreEqual("formula", Assert.Throws <ArgumentNullException>(() => { new IsotopicDistribution(ok2); }).ParamName);

            IHasMass ok5 = null;

            Assert.AreEqual("objectWithMass", Assert.Throws <ArgumentNullException>(() => { ok5.ToMZ(0); }).ParamName);

            var ok7 = new PhysicalObjectWithChemicalFormula("C");
        }
        public void AddingExistingElementsTest()
        {
            var elementC = new Element("GGG", 66, 12.0106);

            PeriodicTable.Add(elementC);
            var elementC1 = new Element("GGG", 66, 12.0106);

            Assert.Throws <ArgumentException>(() => { PeriodicTable.Add(elementC1); }, "Element with symbol GGG already added!");
            var elementC2 = new Element("GGG2", 66, 12.0106);

            Assert.Throws <ArgumentException>(() => { PeriodicTable.Add(elementC2); }, "Element with atomic number 66 already added!");
        }
        public static void Load(string elementLocation)
        {
            using (StreamReader sr = new StreamReader(elementLocation))
            {
                // Read the stream to a string, and write the string to the console.
                String line = sr.ReadLine();
                while (!line.Contains("Atomic Number"))
                {
                    line = sr.ReadLine();
                }
                var     prevAtomicNumber = -1;
                Element element          = null;
                do
                {
                    int atomicNumber = Convert.ToInt32(Regex.Match(line, @"\d+").Value);

                    line = sr.ReadLine();
                    string atomicSymbol = Regex.Match(line, @"[A-Za-z]+$").Value;

                    line = sr.ReadLine();
                    int massNumber = Convert.ToInt32(Regex.Match(line, @"\d+").Value);

                    line = sr.ReadLine();
                    double atomicMass = Convert.ToDouble(Regex.Match(line, @"[\d\.]+").Value);

                    line = sr.ReadLine();
                    double abundance = -1;
                    if (Regex.Match(line, @"[\d\.]+").Success == true)
                    {
                        abundance = Convert.ToDouble(Regex.Match(line, @"[\d\.]+").Value);
                    }
                    else
                    {
                        line = sr.ReadLine();
                        line = sr.ReadLine();
                        line = sr.ReadLine();
                        line = sr.ReadLine();
                        continue;
                    }

                    line = sr.ReadLine();
                    double averageMass = -1;
                    if (Regex.Match(line, @"\[").Success == true)
                    {
                        double averageMass1 = Convert.ToDouble(Regex.Match(line, @"(?<=\[)[\d\.]+").Value);
                        var    kkajsdf      = Regex.Match(line, @"(?<=,)[\d\.]+").Value;
                        double averageMass2 = Convert.ToDouble(kkajsdf);
                        averageMass = (averageMass1 + averageMass2) / 2;
                    }
                    else
                    {
                        averageMass = Convert.ToDouble(Regex.Match(line, @"[\d\.]+").Value);
                    }

                    if (atomicNumber != prevAtomicNumber)
                    {
                        element = new Element(atomicSymbol, atomicNumber, averageMass);
                        PeriodicTable.Add(element);
                    }
                    if (element == null)
                    {
                        throw new InvalidDataException("Element is null, problem with the data");
                    }
                    element.AddIsotope(massNumber, atomicMass, abundance);

                    line             = sr.ReadLine();
                    line             = sr.ReadLine();
                    line             = sr.ReadLine();
                    prevAtomicNumber = atomicNumber;
                } while (line.Contains("Atomic Number"));
            }
        }
        public void AddingExistingElementsTest()
        {
            var elementC = new Element("GGG", 127, 12.0106);

            PeriodicTable.Add(elementC);
        }
Example #5
0
        public void SetUp()
        {
            var elementH = new Element("H", 1, 1.007975);

            PeriodicTable.Add(elementH);
            elementH.AddIsotope(1, 1.00782503223, 0.999885);
            elementH.AddIsotope(2, 2.01410177812, 0.000115);

            var elementC = new Element("C", 6, 12.0106);

            PeriodicTable.Add(elementC);
            elementC.AddIsotope(12, 12, 0.9893);
            elementC.AddIsotope(13, 13.00335483507, 0.0107);

            var elementN = new Element("N", 7, 14.006855);

            PeriodicTable.Add(elementN);
            elementN.AddIsotope(15, 15.00010889888, 0.00364);
            elementN.AddIsotope(14, 14.00307400443, 0.99636);

            var elementO = new Element("O", 8, 15.9994);

            PeriodicTable.Add(elementO);
            elementO.AddIsotope(16, 15.99491461957, 0.99757);
            elementO.AddIsotope(17, 16.99913175650, 0.00038);
            elementO.AddIsotope(18, 17.99915961286, 0.00205);

            var elementFe = new Element("Fe", 26, 55.845);

            PeriodicTable.Add(elementFe);
            elementFe.AddIsotope(54, 53.93960899, 0.05845);
            elementFe.AddIsotope(56, 55.93493633, 0.91754);
            elementFe.AddIsotope(57, 56.93539284, 0.02119);
            elementFe.AddIsotope(58, 57.93327443, 0.00282);

            var elementBr = new Element("Br", 35, 79.904);

            PeriodicTable.Add(elementBr);
            elementBr.AddIsotope(79, 78.9183376, 0.5069);
            elementBr.AddIsotope(81, 80.9162897, 0.4931);

            var elementCa = new Element("Ca", 20, 40.078);

            PeriodicTable.Add(elementCa);
            elementCa.AddIsotope(40, 39.962590863, 0.96941);
            elementCa.AddIsotope(42, 41.95861783, 0.00647);
            elementCa.AddIsotope(43, 42.95876644, 0.00135);
            elementCa.AddIsotope(44, 43.95548156, 0.02086);
            elementCa.AddIsotope(46, 45.9536890, 0.00004);
            elementCa.AddIsotope(48, 47.95252276, 0.00187);

            var elementS = new Element("S", 16, 32.0675);

            PeriodicTable.Add(elementS);
            elementS.AddIsotope(32, 31.9720711744, 0.9499);
            elementS.AddIsotope(33, 32.9714589098, 0.0075);
            elementS.AddIsotope(34, 33.967867004, 0.0425);
            elementS.AddIsotope(36, 35.96708071, 0.0001);

            var elementSe = new Element("Se", 34, 78.971);

            PeriodicTable.Add(elementSe);
            elementSe.AddIsotope(74, 73.922475934, 0.0089);
            elementSe.AddIsotope(76, 75.919213704, 0.0937);
            elementSe.AddIsotope(77, 76.919914154, 0.0763);
            elementSe.AddIsotope(78, 77.91730928, 0.2377);
            elementSe.AddIsotope(80, 79.9165218, 0.4961);
            elementSe.AddIsotope(82, 81.9166995, 0.0873);

            // Wrong average mass, for checking some chemical formula hashing
            var elementAl = new Element("Al", 13, 26.98153853);

            PeriodicTable.Add(elementAl);
            elementAl.AddIsotope(27, 26.98153853, 1);

            var elementZr = new Element("Zr", 40, 91.224);

            PeriodicTable.Add(elementZr);
            elementZr.AddIsotope(90, 89.9046977, 0.5145);
            elementZr.AddIsotope(91, 90.9056396, 0.1122);
            elementZr.AddIsotope(92, 91.9050347, 0.1715);
            elementZr.AddIsotope(94, 93.9063108, 0.1738);
            // Wrong abundance on purpose, for testing
            elementZr.AddIsotope(96, 95.9082714, 0.0279);
        }
Example #6
0
        public static void Load(string elementLocation)
        {
            using (StreamReader sr = new StreamReader(elementLocation))
            {
                string line = sr.ReadLine();
                while (!line.Contains("Atomic Number"))
                {
                    line = sr.ReadLine();
                }
                var     prevAtomicNumber = -1;
                Element element          = new Element("fake", prevAtomicNumber, -1);
                do
                {
                    int atomicNumber = Convert.ToInt32(Regex.Match(line, @"\d+").Value, CultureInfo.InvariantCulture);

                    line = sr.ReadLine();
                    string atomicSymbol = Regex.Match(line, @"[A-Za-z]+$").Value;

                    line = sr.ReadLine();
                    int massNumber = Convert.ToInt32(Regex.Match(line, @"\d+").Value, CultureInfo.InvariantCulture);

                    line = sr.ReadLine();
                    double atomicMass = Convert.ToDouble(Regex.Match(line, @"[\d\.]+").Value, CultureInfo.InvariantCulture);

                    line = sr.ReadLine();
                    double abundance;
                    if (Regex.Match(line, @"[\d\.]+").Success)
                    {
                        abundance = Convert.ToDouble(Regex.Match(line, @"[\d\.]+").Value, CultureInfo.InvariantCulture);
                    }
                    else
                    {
                        sr.ReadLine();
                        sr.ReadLine();
                        sr.ReadLine();
                        line = sr.ReadLine();
                        continue;
                    }

                    line = sr.ReadLine();
                    double averageMass;
                    if (Regex.Match(line, @"\[").Success)
                    {
                        double averageMass1 = Convert.ToDouble(Regex.Match(line, @"(?<=\[)[\d\.]+").Value, CultureInfo.InvariantCulture);
                        var    kkajsdf      = Regex.Match(line, @"(?<=,)[\d\.]+").Value;
                        double averageMass2 = Convert.ToDouble(kkajsdf, CultureInfo.InvariantCulture);
                        averageMass = (averageMass1 + averageMass2) / 2;
                    }
                    else
                    {
                        averageMass = Convert.ToDouble(Regex.Match(line, @"[\d\.]+").Value, CultureInfo.InvariantCulture);
                    }

                    if (atomicNumber != prevAtomicNumber)
                    {
                        element = new Element(atomicSymbol, atomicNumber, averageMass);
                        PeriodicTable.Add(element);
                    }

                    element.AddIsotope(massNumber, atomicMass, abundance);

                    sr.ReadLine();
                    sr.ReadLine();
                    line             = sr.ReadLine();
                    prevAtomicNumber = atomicNumber;
                } while (line.Contains("Atomic Number"));
            }
        }