Skip to content

formula.Formula does not validate maximal charge against total atomic number. #2

@hanicinecm

Description

@hanicinecm

Problem:

Currently, Formula('He+42') happily executes, while the maximal charge should be 2:

In [1]: Formula('He+42').charge
Out[1]: 42

Suggested solution:

Ensure the total atomic number of Formula instance is greater or equal than the charge:

In [47]: formula = Formula('CH4+42')
    ...: total_atomic_number =  sum(formula.atom_stoich[atom.symbol] * atom.Z for atom in formula.atoms)
    ...: if total_atomic_number < formula.charge:
    ...:     raise FormulaParseError(f'Charge {formula.charge} if greater than the total atomic number {total_atomic_number}!')
    ...: 
    ...: 
---------------------------------------------------------------------------
FormulaParseError                         Traceback (most recent call last)
<ipython-input-47-b8fa6fbad41d> in <module>
      2 total_atomic_number =  sum(formula.atom_stoich[str(atom)] * atom.Z for atom in formula.atoms)
      3 if total_atomic_number < formula.charge:
----> 4     raise FormulaParseError(f'Charge {formula.charge} if greater than the total atomic number {total_atomic_number}!')
      5 

FormulaParseError: Charge 42 if greater than the total atomic number 10!

Problems with the suggested solution:

  • Some formulas do not define charge
  • The mapping between the Atom and Isotope instances from Formula.atoms and their atom_symbols from Formula.atom_stoich.keys() is not defined by atom.symbol, but rather appears to be built up through a series of if ... else cases. However they appear to match.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions