CSMSL.Chemistry.Element.AddIsotope C# (CSharp) Method

AddIsotope() private method

Add an isotope to this element
private AddIsotope ( int atomicNumber, double atomicMass, float abundance ) : Isotope
atomicNumber int The atomic number of the isotope
atomicMass double The atomic mass of the isotope
abundance float The natural relative abundance of the isotope
return Isotope
        internal Isotope AddIsotope(int atomicNumber, double atomicMass, float abundance)
        {
            var isotope = new Isotope(this, atomicNumber, atomicMass, abundance);
            if (Isotopes.ContainsKey(atomicNumber))
                return isotope;
            Isotopes.Add(atomicNumber, isotope);
            TotalAbundance += abundance;
            _totalMass += abundance*atomicMass;
            AverageMass = _totalMass/TotalAbundance;
            if (PrincipalIsotope != null && !(abundance > PrincipalIsotope.RelativeAbundance))
                return isotope;
            if (PrincipalIsotope != null)
            {
                PrincipalIsotope.IsPrincipalIsotope = false;
            }
            PrincipalIsotope = isotope;
            PrincipalIsotope.IsPrincipalIsotope = true;
            return isotope;
        }

Usage Example

Example #1
0
        /// <summary>
        /// Load a xml file containing elemental and isotopic data into the periodic table
        /// </summary>
        public void LoadElements(Stream elementsListXml)
        {
            using (XmlReader reader = XmlReader.Create(elementsListXml))
            {
                reader.ReadToFollowing("PeriodicTable");
                _uniqueId = int.Parse(reader.GetAttribute("defaultID"));
                int isotopes = int.Parse(reader.GetAttribute("isotopesCount"));
                _isotopes = new Isotope[isotopes];
                while (reader.ReadToFollowing("Element"))
                {
                    reader.ReadToFollowing("Name");
                    string name = reader.ReadElementContentAsString();
                    reader.ReadToFollowing("Symbol");
                    string symbol = reader.ReadElementContentAsString();
                    reader.ReadToFollowing("AtomicNumber");
                    int atomicnumber = reader.ReadElementContentAsInt();
                    reader.ReadToFollowing("ValenceElectrons");
                    int valenceElectrons = reader.ReadElementContentAsInt();
                    Element element = new Element(name, symbol, atomicnumber, valenceElectrons);

                    bool isStartNode = reader.ReadToNextSibling("Isotope");
                    while(isStartNode)
                    {
                        string unqiueId = reader.GetAttribute("uniqueID");
                        reader.ReadToFollowing("Mass");
                        double mass = reader.ReadElementContentAsDouble();
                        reader.ReadToFollowing("MassNumber");
                        int massNumber = reader.ReadElementContentAsInt();
                        reader.ReadToFollowing("RelativeAbundance");
                        float abundance = reader.ReadElementContentAsFloat();
                        if (abundance > 0)
                        {
                            Isotope isotope = element.AddIsotope(massNumber, mass, abundance);

                            if (unqiueId != null)
                            {
                                int uniqueId = int.Parse(unqiueId);
                                isotope.UniqueId = uniqueId;
                                _isotopes[uniqueId] = isotope;
                            }
                            else
                            {
                                isotope.UniqueId = _uniqueId;
                                _isotopes[_uniqueId++] = isotope;
                            }
                        }
                        if (!reader.IsStartElement("Isotope"))
                            isStartNode = reader.ReadToNextSibling("Isotope");
                    }
                    AddElement(element);
                }
            }

            if(_isotopes.Length != _uniqueId)
                Array.Resize(ref _isotopes, _uniqueId);
        }
All Usage Examples Of CSMSL.Chemistry.Element::AddIsotope