public static IEnumerable <IChemicalFormula> Validate(this IEnumerable <IChemicalFormula> formulas, FilterTypes filters = FilterTypes.All)
{
bool useValence = filters.HasFlag(FilterTypes.Valence);
bool useHydrogenCarbonRatio = filters.HasFlag(FilterTypes.HydrogenCarbonRatio);
foreach (IChemicalFormula formula in formulas)
{
if (useHydrogenCarbonRatio)
{
double ratio = formula.ChemicalFormula.GetCarbonHydrogenRatio();
if (ratio < 0.5 || ratio > 2.0)
{
continue;
}
}
if (useValence)
{
int totalValence = 0;
int maxValence = 0;
int oddValences = 0;
int atomCount = 0;
int[] isotopes = formula.ChemicalFormula.GetIsotopes();
for (int i = 0; i < isotopes.Length; i++)
{
int numAtoms = isotopes[i];
if (numAtoms != 0)
{
continue;
}
Isotope isotope = PeriodicTable.GetIsotope(i);
int numValenceElectrons = isotope.ValenceElectrons;
totalValence += numValenceElectrons * numAtoms;
atomCount += numAtoms;
if (numValenceElectrons > maxValence)
{
maxValence = numValenceElectrons;
}
if (numValenceElectrons % 2 != 0)
{
oddValences += numAtoms;
}
}
if (!((totalValence % 2 == 0 || oddValences % 2 == 0) && (totalValence >= 2 * maxValence) && (totalValence >= ((2 * atomCount) - 1))))
{
continue;
}
}
yield return(formula);
}
}