/// <summary>
 /// In the minimal IMolecularFormula must contain all those IElement found in the
 /// minimal IMolecularFormula.
 /// </summary>
 /// <param name="formulaMax">A IMolecularFormula which contains the maximal representation of the Elements</param>
 /// <param name="formulaMin">A IMolecularFormula which contains the minimal representation of the Elements</param>
 /// <returns>True, if the correlation is valid</returns>
 private static bool ValidCorrelation(IMolecularFormula formulaMin, IMolecularFormula formulaMax)
 {
     foreach (var element in MolecularFormulaManipulator.Elements(formulaMin))
     {
         if (!MolecularFormulaManipulator.ContainsElement(formulaMax, element))
         {
             return(false);
         }
     }
     return(true);
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Validate the occurrence of this IMolecularFormula.
        /// </summary>
        /// <param name="formula">Parameter is the IMolecularFormula</param>
        /// <returns>An ArrayList containing 9 elements in the order described above</returns>
        public double Validate(IMolecularFormula formula)
        {
            Trace.TraceInformation("Start validation of ", formula);
            double isValid = 1.0;

            foreach (var element in MolecularFormulaManipulator.Elements(formula))
            {
                int occur = MolecularFormulaManipulator.GetElementCount(formula, element);
                if (occur > hashMap[element.Symbol])
                {
                    isValid = 0.0;
                    break;
                }
            }

            return(isValid);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Validate the occurrence of this <see cref="IMolecularFormula"/>.
        /// </summary>
        /// <param name="formula">Parameter is the <see cref="IMolecularFormula"/></param>
        /// <returns>An ArrayList containing 9 elements in the order described above</returns>
        public double Validate(IMolecularFormula formula)
        {
            Trace.TraceInformation("Start validation of ", formula);
            EnsureDefaultOccurElements(formula.Builder);

            double isValid = 1.0;

            foreach (var element in MolecularFormulaManipulator.Elements(formula))
            {
                int occur       = MolecularFormulaManipulator.GetElementCount(formula, element);
                var elemIsotope = formula.Builder.NewIsotope(element.Symbol);
                if ((occur < mfRange.GetIsotopeCountMin(elemIsotope)) || (occur > mfRange.GetIsotopeCountMax(elemIsotope)))
                {
                    isValid = 0.0;
                    break;
                }
            }

            return(isValid);
        }
        /// <summary>
        /// Remove all those IMolecularFormula which are not fit theirs IElement
        /// occurrence into a limits. The limits are given from formulaMax and formulaMin.
        /// In the minimal IMolecularFormula must contain all those IElement found in the
        /// minimal IMolecularFormula.
        /// </summary>
        /// <param name="formulaSet">IMolecularFormulaSet to look for</param>
        /// <param name="formulaMax">A IMolecularFormula which contains the maximal representation of the Elements</param>
        /// <param name="formulaMin">A IMolecularFormula which contains the minimal representation of the Elements</param>
        /// <returns>A IMolecularFormulaSet with only the IMolecularFormula which the IElements
        ///                         are into the correct occurrence</returns>
        public static IMolecularFormulaSet Remove(IMolecularFormulaSet formulaSet, IMolecularFormula formulaMin, IMolecularFormula formulaMax)
        {
            // prove the correlation between maximum and minimum molecularFormula
            if (!ValidCorrelation(formulaMin, formulaMax))
            {
                return(null);
            }

            var newFormulaSet = formulaSet.Builder.NewMolecularFormulaSet();

            foreach (var formula in formulaSet)
            {
                bool flagPass = true;

                // the formula must contain all element found into the formulaMin
                if (!ValidCorrelation(formula, formulaMin))
                {
                    continue;
                }

                foreach (var element in MolecularFormulaManipulator.Elements(formulaMin))
                {
                    int occur    = MolecularFormulaManipulator.GetElementCount(formula, element);
                    int occurMax = MolecularFormulaManipulator.GetElementCount(formulaMax, element);
                    int occurMin = MolecularFormulaManipulator.GetElementCount(formulaMin, element);

                    if (!(occurMin <= occur) || !(occur <= occurMax))
                    {
                        flagPass = false;
                        break;
                    }
                }
                if (flagPass) // stored if each IElement occurrence is into the limits
                {
                    newFormulaSet.Add(formula);
                }
            }
            return(newFormulaSet);
        }
Ejemplo n.º 5
0
        // Private procedures

        private void WriteCrystal(ICrystal crystal)
        {
            var title = crystal.Title;

            if (title != null && title.Trim().Length > 0)
            {
                Writeln($"TITL {title.Trim()}");
            }
            else
            {
                Writeln("TITL Produced with CDK (http://cdk.sf.net/)");
            }
            Vector3 a       = crystal.A;
            Vector3 b       = crystal.B;
            Vector3 c       = crystal.C;
            double  alength = a.Length();
            double  blength = b.Length();
            double  clength = c.Length();
            double  alpha   = Vectors.RadianToDegree(Vectors.Angle(b, c));
            double  beta    = Vectors.RadianToDegree(Vectors.Angle(a, c));
            double  gamma   = Vectors.RadianToDegree(Vectors.Angle(a, b));

            Write("CELL " + 1.54184.ToString("F5", NumberFormatInfo.InvariantInfo) + "   ");
            Write(alength.ToString("F5", NumberFormatInfo.InvariantInfo) + "  ");
            Write(blength.ToString("F5", NumberFormatInfo.InvariantInfo) + "  ");
            Write(clength.ToString("F5", NumberFormatInfo.InvariantInfo) + "  ");
            Write(alpha.ToString("F4", NumberFormatInfo.InvariantInfo) + "  ");
            Write(beta.ToString("F4", NumberFormatInfo.InvariantInfo) + "  ");
            Write(gamma.ToString("F4", NumberFormatInfo.InvariantInfo) + "  ");
            Writeln("ZERR " + ((double)crystal.Z).ToString("F5", NumberFormatInfo.InvariantInfo)
                    + "    0.01000  0.01000   0.01000   0.0100   0.0100   0.0100");
            string spaceGroup = crystal.SpaceGroup;

            if (string.Equals("P1", spaceGroup, StringComparison.Ordinal))
            {
                Writeln("LATT  -1");
            }
            else if (string.Equals("P 2_1 2_1 2_1", spaceGroup, StringComparison.Ordinal))
            {
                Writeln("LATT  -1");
                Writeln("SYMM  1/2+X   , 1/2-Y   ,    -Z");
                Writeln("SYMM     -X   , 1/2+Y   , 1/2-Z");
                Writeln("SYMM  1/2-X   ,    -Y   , 1/2+Z");
            }
            string            elemNames  = "";
            string            elemCounts = "";
            IMolecularFormula formula    = MolecularFormulaManipulator.GetMolecularFormula(crystal);
            var asortedElements          = MolecularFormulaManipulator.Elements(formula).ToReadOnlyList();

            foreach (var element in asortedElements)
            {
                string symbol = element.Symbol;
                elemNames += symbol + "    ".Substring(symbol.Length);
                string countS = MolecularFormulaManipulator.GetElementCount(formula, element).ToString(NumberFormatInfo.InvariantInfo);
                elemCounts += countS + "    ".Substring(countS.Length);
            }
            Writeln("SFAC  " + elemNames);
            Writeln("UNIT  " + elemCounts);
            /* write atoms */
            for (int i = 0; i < crystal.Atoms.Count; i++)
            {
                IAtom   atom      = crystal.Atoms[i];
                Vector3 cartCoord = atom.Point3D.Value;
                Vector3 fracCoord = CrystalGeometryTools.CartesianToFractional(a, b, c, cartCoord);
                string  symbol    = atom.Symbol;
                string  output    = symbol + (i + 1);
                Write(output);
                for (int j = 1; j < 5 - output.Length; j++)
                {
                    Write(" ");
                }
                Write("     ");
                string elemID = null;
                for (int elemidx = 0; elemidx < asortedElements.Count; elemidx++)
                {
                    var elem = asortedElements[elemidx];
                    if (elem.Symbol.Equals(symbol, StringComparison.Ordinal))
                    {
                        elemID = (elemidx + 1).ToString(NumberFormatInfo.InvariantInfo);
                        break;
                    }
                }
                Write(elemID);
                Write("    ".Substring(elemID.Length));
                Write(fracCoord.X.ToString("F5", NumberFormatInfo.InvariantInfo) + "   ");
                Write(fracCoord.Y.ToString("F5", NumberFormatInfo.InvariantInfo) + "   ");
                Writeln(fracCoord.Y.ToString("F5", NumberFormatInfo.InvariantInfo) + "    11.00000    0.05000");
            }
            Writeln("END");
        }