Beispiel #1
0
 /// <summary>
 /// Initializes a new instance of the MolecularFormula.Item class.
 /// </summary>
 /// <param name="symbol">The symbol of the chemical element.</param>
 /// <param name="count">The atom count.</param>
 public Item(string symbol, int count = 1)
     : this(ChemicalElement.Get(symbol), count)
 {
 }
Beispiel #2
0
 /// <summary>
 /// Initializes a new instance of the Isotope class.
 /// </summary>
 /// <param name="atomicNumber">The atomic number (or proton number, symbol Z) of the chemical element. The number is one-based.</param>
 /// <param name="atomicMassNumber">The atomic mass number (total protons and neutrons).</param>
 /// <param name="atomicWeight">The atomic weight in dalton (unified atomic mass unit).</param>
 public Isotope(int atomicNumber, int atomicMassNumber, double atomicWeight)
     : this(ChemicalElement.Get(atomicNumber), atomicMassNumber)
 {
     AtomicWeight = atomicWeight;
 }
Beispiel #3
0
    /// <summary>
    /// Writes the specified chemistry periodic table, followed by the current line terminator, to the standard output stream.
    /// </summary>
    /// <param name="console">The console instance.</param>
    /// <param name="style">The style.</param>
    public static void WriteTable(StyleConsole console, ChemicalElementConsoleStyle style)
    {
        var col = new List <ConsoleText>();

        if (style == null)
        {
            style = new();
        }
        var symbolStyle = new ConsoleTextStyle
        {
            ForegroundConsoleColor = style.SymbolConsoleColor,
            ForegroundRgbColor     = style.SymbolRgbColor,
            BackgroundConsoleColor = style.BackgroundConsoleColor,
            BackgroundRgbColor     = style.BackgroundRgbColor
        };
        var numberStyle = new ConsoleTextStyle
        {
            ForegroundConsoleColor = style.AtomicNumberConsoleColor,
            ForegroundRgbColor     = style.AtomicNumberRgbColor,
            BackgroundConsoleColor = style.BackgroundConsoleColor,
            BackgroundRgbColor     = style.BackgroundRgbColor
        };
        var punctuationStyle = new ConsoleTextStyle
        {
            ForegroundConsoleColor = style.PunctuationConsoleColor,
            ForegroundRgbColor     = style.PunctuationRgbColor,
            BackgroundConsoleColor = style.BackgroundConsoleColor,
            BackgroundRgbColor     = style.BackgroundRgbColor
        };

        col.Add("1 ", punctuationStyle);
        AppendSymbol(col, ChemicalElement.H, symbolStyle);
        col.Add(' ', 64, punctuationStyle);
        AppendSymbol(col, ChemicalElement.He, symbolStyle);
        col.AddNewLine();
        col.Add("  ", punctuationStyle);
        AppendNumber(col, 1, numberStyle);
        col.Add(' ', 64, punctuationStyle);
        AppendNumber(col, 2, numberStyle);
        col.AddNewLine();
        col.AddNewLine();

        col.Add("2 ", punctuationStyle);
        AppendSymbol(col, ChemicalElement.Li, symbolStyle);
        AppendSymbol(col, ChemicalElement.Be, symbolStyle);
        col.Add(' ', 40, punctuationStyle);
        AppendSymbol(col, ChemicalElement.B, symbolStyle);
        AppendSymbol(col, ChemicalElement.C, symbolStyle);
        AppendSymbol(col, ChemicalElement.N, symbolStyle);
        AppendSymbol(col, ChemicalElement.O, symbolStyle);
        AppendSymbol(col, ChemicalElement.F, symbolStyle);
        AppendSymbol(col, ChemicalElement.Ne, symbolStyle);
        col.AddNewLine();
        col.Add("  ", punctuationStyle);
        AppendNumber(col, 3, numberStyle, 2);
        col.Add(' ', 40, punctuationStyle);
        AppendNumber(col, 5, numberStyle, 6);
        col.AddNewLine();
        col.AddNewLine();

        col.Add("3 ", punctuationStyle);
        AppendSymbol(col, ChemicalElement.Na, symbolStyle);
        AppendSymbol(col, ChemicalElement.Mg, symbolStyle);
        col.Add(' ', 40, punctuationStyle);
        AppendSymbol(col, ChemicalElement.Al, symbolStyle);
        AppendSymbol(col, ChemicalElement.Si, symbolStyle);
        AppendSymbol(col, ChemicalElement.P, symbolStyle);
        AppendSymbol(col, ChemicalElement.S, symbolStyle);
        AppendSymbol(col, ChemicalElement.Cl, symbolStyle);
        AppendSymbol(col, ChemicalElement.Ar, symbolStyle);
        col.AddNewLine();
        col.Add("  ", punctuationStyle);
        AppendNumber(col, 11, numberStyle, 2);
        col.Add(' ', 40, punctuationStyle);
        AppendNumber(col, 13, numberStyle, 6);
        col.AddNewLine();
        col.AddNewLine();

        col.Add("4 ", punctuationStyle);
        for (var i = 19; i <= 36; i++)
        {
            AppendSymbol(col, ChemicalElement.Get(i), symbolStyle);
        }

        col.AddNewLine();
        col.Add("  ", punctuationStyle);
        AppendNumber(col, 19, numberStyle, 18);
        col.AddNewLine();
        col.AddNewLine();

        col.Add("5 ", punctuationStyle);
        for (var i = 37; i <= 54; i++)
        {
            AppendSymbol(col, ChemicalElement.Get(i), symbolStyle);
        }

        col.AddNewLine();
        col.Add("  ", punctuationStyle);
        AppendNumber(col, 37, numberStyle, 18);
        col.AddNewLine();
        col.AddNewLine();

        col.Add("6 ", punctuationStyle);
        for (var i = 55; i < 58; i++)
        {
            AppendSymbol(col, ChemicalElement.Get(i), symbolStyle);
        }

        for (var i = 72; i <= 86; i++)
        {
            AppendSymbol(col, ChemicalElement.Get(i), symbolStyle);
        }

        col.AddNewLine();
        col.Add("  ", punctuationStyle);
        AppendNumber(col, 55, numberStyle, 2);
        col.Add("... ", punctuationStyle);
        AppendNumber(col, 72, numberStyle, 15);
        col.AddNewLine();
        col.AddNewLine();

        col.Add("7 ", punctuationStyle);
        for (var i = 87; i < 90; i++)
        {
            AppendSymbol(col, ChemicalElement.Get(i), symbolStyle);
        }

        for (var i = 104; i <= 118; i++)
        {
            AppendSymbol(col, ChemicalElement.Get(i), symbolStyle);
        }

        col.AddNewLine();
        col.Add("  ", punctuationStyle);
        AppendNumber(col, 87, numberStyle, 2);
        col.Add("... ", punctuationStyle);
        AppendNumber(col, 104, numberStyle, 15);
        col.AddNewLine();
        col.AddNewLine();

        col.Add(ChemicalElement.La.Symbol, symbolStyle);
        col.Add('-', 1, punctuationStyle);
        col.Add(ChemicalElement.Lu.Symbol, symbolStyle);
        col.Add("\t  ", punctuationStyle);
        for (var i = 57; i <= 71; i++)
        {
            AppendSymbol(col, ChemicalElement.Get(i), symbolStyle);
        }

        col.AddNewLine();
        col.Add("\t  ", punctuationStyle);
        AppendNumber(col, 57, numberStyle, 15);
        col.AddNewLine();
        col.AddNewLine();

        col.Add(ChemicalElement.Ac.Symbol, symbolStyle);
        col.Add('-', 1, punctuationStyle);
        col.Add(ChemicalElement.Lr.Symbol, symbolStyle);
        col.Add("\t  ");
        for (var i = 89; i <= 103; i++)
        {
            AppendSymbol(col, ChemicalElement.Get(i), symbolStyle);
        }

        col.AddNewLine();
        col.Add("\t  ", punctuationStyle);
        AppendNumber(col, 89, numberStyle, 15);
        col.AddNewLine();
        col.AddNewLine();

        if (ChemicalElement.Has(119) && ChemicalElement.Has(120))
        {
            col.Add("8 ", punctuationStyle);
            col.Add("119 ", numberStyle);
            col.Add('-', 1, punctuationStyle);
            col.Add(" 168 ", numberStyle);
            AppendSymbol(col, ChemicalElement.Get(119), symbolStyle);
            AppendSymbol(col, ChemicalElement.Get(120), symbolStyle);
            for (var i = 121; i < 131; i++)
            {
                if (ChemicalElement.Has(i))
                {
                    AppendSymbol(col, ChemicalElement.Get(i), symbolStyle);
                }
                else
                {
                    break;
                }
            }

            col.Add("...", punctuationStyle);
            col.AddNewLine();
        }
        else
        {
            col.Add("8 ", punctuationStyle);
            col.Add("119 ", numberStyle);
            col.Add('-', 1, punctuationStyle);
            col.Add(" 168 ", numberStyle);
            col.AddNewLine();
        }

        col.Add("9 ", punctuationStyle);
        col.Add("169 ", numberStyle);
        col.Add('-', 1, punctuationStyle);
        col.Add(" 218 ", numberStyle);
        col.AddNewLine();

        (console ?? StyleConsole.Default).Write(col);
    }
Beispiel #4
0
 /// <summary>
 /// Initializes a new instance of the Isotope class.
 /// </summary>
 /// <param name="atomicNumber">The atomic number (or proton number, symbol Z) of the chemical element. The number is one-based.</param>
 /// <param name="atomicMassNumber">The atomic mass number (total protons and neutrons).</param>
 public Isotope(int atomicNumber, int atomicMassNumber)
     : this(ChemicalElement.Get(atomicNumber), atomicMassNumber)
 {
 }
Beispiel #5
0
        public void TestElement()
        {
            Assert.IsNull(ChemicalElement.Get(0));
            Assert.IsNull(ChemicalElement.Get(-1));
            Assert.IsNull(ChemicalElement.Get("Nothing"));
            Assert.IsFalse(new ChemicalElement(-1, null, "Nothing", true).IsValid());
            Assert.IsFalse(ChemicalElement.IsValid("Nothing"));

            var h = ChemicalElement.Get(1);

            Assert.IsTrue(h.IsValid());
            Assert.AreEqual(1, h.AtomicNumber);
            Assert.AreEqual("H", h.Symbol);
            Assert.AreEqual("Hydrogen", h.EnglishName);
            Assert.AreEqual(1, h.Period);
            Assert.AreEqual(1, h.Group);
            Assert.AreEqual(0, h.IndexInPeriod);
            Assert.AreEqual("s", h.Block);
            Assert.AreEqual(h, ChemicalElement.Get("H"));
            Assert.AreEqual(h, ChemicalElement.H);
            Assert.AreEqual(h, new ChemicalElement(1, "H", "No.1", true));
            Assert.IsTrue(h.HasAtomicWeight);
            Assert.IsTrue(h.AtomicWeight >= 1);
            Assert.IsTrue(h.ToString().Contains(h.Symbol));
            Assert.IsTrue(h.IsNonMetallic);
            Assert.IsFalse(h.IsAlkaliMetal);
            Assert.IsFalse(h.IsAlkalineEarthMetal);
            Assert.IsFalse(h.IsChalcogen);
            Assert.IsFalse(h.IsHalogen);
            Assert.IsFalse(h.IsInBoronGroup);
            Assert.IsFalse(h.IsInCarbonGroup);
            Assert.IsFalse(h.IsInNitrogenGroup);
            Assert.IsFalse(h.IsInVIII);
            Assert.IsFalse(h.IsMetal);
            Assert.IsFalse(h.IsNoble);
            Assert.IsFalse(h.IsTransition);
            Assert.IsFalse(h.IsRadioelement());
            Assert.AreEqual("H", ((JsonObjectNode)h).TryGetStringValue("symbol"));
            var d = h.Isotope(2);

            Assert.AreEqual("D", d.ToString());
            Assert.AreEqual(h, d.Element);
            Assert.AreEqual(1, d.AtomicNumber);
            Assert.AreEqual(2, d.AtomicMassNumber);
            Assert.AreEqual(1, d.Neutrons);
            Assert.IsTrue(d.HasAtomicWeight);
            Assert.AreEqual(d, new Isotope(h, 2));
            Assert.IsFalse(h.Isotope(10).HasAtomicWeight);

            var c = ChemicalElement.Get(6);

            Assert.IsTrue(c.IsValid());
            Assert.IsFalse(c == h);
            Assert.IsTrue(c != h);
            Assert.IsTrue(c > h);
            Assert.IsTrue(c >= h);
            Assert.IsFalse(c < h);
            Assert.IsFalse(c <= h);
            Assert.AreNotEqual(h, c);
            Assert.AreEqual(6, c.AtomicNumber);
            Assert.AreEqual("C", c.Symbol);
            Assert.AreEqual("Carbon", c.EnglishName);
            Assert.AreEqual(2, c.Period);
            Assert.AreEqual(14, c.Group);
            Assert.AreEqual(3, c.IndexInPeriod);
            Assert.AreEqual("p", c.Block);
            Assert.AreEqual(c, ChemicalElement.Get("C"));
            Assert.AreEqual(c, ChemicalElement.C);
            Assert.AreEqual(c, new ChemicalElement(6, "C", "No.6", true));
            Assert.IsTrue(c.HasAtomicWeight);
            Assert.IsTrue(c.AtomicWeight >= 12);
            Assert.IsTrue(c.ToString().Contains(c.Symbol));
            Assert.AreEqual(6, ((JsonObjectNode)c).TryGetInt32Value("number"));
            var c12 = c.Isotope(12);

            Assert.AreNotEqual(h, c12);
            Assert.AreEqual(3, c12.ToString().Length);
            Assert.IsTrue(c12.ToString().EndsWith("C"));
            Assert.AreEqual(c, c12.Element);
            Assert.AreEqual(6, c12.AtomicNumber);
            Assert.AreEqual(12, c12.AtomicMassNumber);
            Assert.AreEqual(6, c12.Neutrons);
            Assert.IsTrue(c12.HasAtomicWeight);
            Assert.AreEqual(12, c12.AtomicWeight);
            Assert.AreEqual(c12, new Isotope(6, 12));

            var count = 0;

            foreach (var prop in typeof(ChemicalElement).GetProperties(BindingFlags.Static | BindingFlags.Public))
            {
                if (!prop.CanRead || prop.PropertyType != typeof(ChemicalElement))
                {
                    continue;
                }
                var ele = (ChemicalElement)prop.GetValue(null);
                Assert.IsNotNull(ele);
                Assert.AreEqual(prop.Name, ele.Symbol);
                Assert.AreEqual(ele, ChemicalElement.Get(ele.AtomicNumber));
                Assert.AreEqual(ele, ChemicalElement.Get(ele.Symbol));
                Assert.IsFalse(string.IsNullOrWhiteSpace(ele.Name));
                count++;
            }

            Assert.IsTrue(count >= 118);
            var usn = ChemicalElement.Get(170);

            Assert.AreNotEqual(h, usn);
            Assert.AreNotEqual(c, usn);
            Assert.AreEqual("Usn", usn.Symbol);
            Assert.AreEqual("Unseptnilium", usn.EnglishName);
            Assert.AreEqual(9, usn.Period);
            Assert.AreEqual(2, usn.Group);
            Assert.AreEqual(1, usn.IndexInPeriod);
            Assert.AreEqual("s", usn.Block);
            Assert.AreEqual(usn, ChemicalElement.Get("Usn"));
            Assert.IsFalse(usn.HasAtomicWeight);
            Assert.IsTrue(double.IsNaN(usn.AtomicWeight));
            Assert.IsTrue(usn.ToString().Contains(usn.Symbol));

            var bbb = ChemicalElement.Get(222);

            Assert.AreEqual("Bbb", bbb.Symbol);
            Assert.AreEqual("i", bbb.Block);

            var one = new ChemicalElement(17, "Seventeen", "A new sample element", true);

            Assert.IsTrue(one.IsValid());
            Assert.AreNotEqual(ChemicalElement.Get(17), one);
        }