Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
hanicinecm opened this issue Sep 1, 2021 · 0 comments

Comments

@hanicinecm
Copy link
Collaborator

hanicinecm commented Sep 1, 2021

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant