question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to override default charge assignment?

See original GitHub issue

I am currently attempting to parameterize the PF6- anion with the Sage force field:

from openff.toolkit.typing.engines.smirnoff import ForceField
from openff.toolkit.topology import Molecule, Topology

pf6_minus = "F[P-](F)(F)(F)(F)F"

forcefield = ForceField("openff_unconstrained-2.0.0.offxml")
ethanol = Molecule.from_smiles(pf6_minus)
topology = Topology.from_molecules(molecules=[ethanol])
system = forcefield.create_openmm_system(topology)

but I am getting an UnassignedMoleculeChargeException.

Warning: Unable to load toolkit 'OpenEye Toolkit'. The Open Force Field Toolkit does not require the OpenEye Toolkits, and can use RDKit/AmberTools instead. However, if you have a valid license for the OpenEye Toolkits, consider installing them for faster performance and additional file format support: https://docs.eyesopen.com/toolkits/python/quickstart-python/linuxosx.html OpenEye offers free Toolkit licenses for academics: https://www.eyesopen.com/academic-licensing
[13:59:24] UFFTYPER: Warning: hybridization set to SP3 for atom 1
[13:59:24] UFFTYPER: Unrecognized charge state for atom: 1
/Users/orioncohen/miniconda3/envs/openmm/lib/python3.9/site-packages/openff/toolkit/typing/engines/smirnoff/parameters.py:4155: Warning: No registered toolkits can provide the capability "assign_partial_charges" for args "()" and kwargs "{'molecule': Molecule with name '' and SMILES '[F][P-]([F])([F])([F])([F])[F]', 'partial_charge_method': 'am1bcc', 'use_conformers': None, 'strict_n_conformers': False, 'normalize_partial_charges': True, '_cls': <class 'openff.toolkit.topology.molecule.FrozenMolecule'>}"
Available toolkits are: [ToolkitWrapper around The RDKit version 2021.03.5, ToolkitWrapper around AmberTools version 21.0, ToolkitWrapper around Built-in Toolkit version None]
 ToolkitWrapper around The RDKit version 2021.03.5 <class 'openff.toolkit.utils.exceptions.ChargeMethodUnavailableError'> : partial_charge_method 'am1bcc' is not available from RDKitToolkitWrapper. Available charge methods are ['mmff94'] 
 ToolkitWrapper around AmberTools version 21.0 <class 'TypeError'> : 'NoneType' object is not iterable
 ToolkitWrapper around Built-in Toolkit version None <class 'openff.toolkit.utils.exceptions.ChargeMethodUnavailableError'> : Partial charge method "am1bcc"" is not supported by the Built-in toolkit. Available charge methods are ['zeros', 'formal_charge']

  warnings.warn(str(e), Warning)
Traceback (most recent call last):
  File "/Users/orioncohen/projects/lowT/openmm/setup/testing_pf6.py", line 9, in <module>
    system = forcefield.create_openmm_system(topology)
  File "/Users/orioncohen/miniconda3/envs/openmm/lib/python3.9/site-packages/openff/toolkit/typing/engines/smirnoff/forcefield.py", line 1348, in create_openmm_system
    parameter_handler.postprocess_system(system, topology, **kwargs)
  File "/Users/orioncohen/miniconda3/envs/openmm/lib/python3.9/site-packages/openff/toolkit/typing/engines/smirnoff/parameters.py", line 3944, in postprocess_system
    raise UnassignedMoleculeChargeException(msg)
openff.toolkit.typing.engines.smirnoff.parameters.UnassignedMoleculeChargeException: The following molecules did not have charges assigned by any ParameterHandler in the ForceField:
[F][P-]([F])([F])([F])([F])[F]

I have RESP fit charges for PF6-. Can I make ForceField use those parameters so it doesn’t throw an error?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:14 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
orionarchercommented, Nov 22, 2021

Update, I believe that I found the error. In my attempt to support arbitrary permutations of atom ordering, I made some accidental arbitrary permutations.

Iterating through Forces does follow the same Atom ordering as Topology and System.

Thank you for the help everyone and apologies for the mistake!

2reactions
mattwthompsoncommented, Nov 12, 2021

The quick-and-dirty approach is to assign your charges to the Moleucle object via its partial_charges setter and then instruct ForceField.create_openmm_system to use those parameters. Be aware this might be deprecated in the future since it muddies the water of what a force field is intended to do (#806).

import numpy as np
from openmm import NonbondedForce, unit

from openff.toolkit.topology import Molecule
from openff.toolkit.typing.engines.smirnoff import ForceField

pf6_minus = Molecule.from_smiles("F[P-](F)(F)(F)(F)F")

# Assign partial charges from a list-like object - replace with numbers from your favorite method
pf6_minus.partial_charges = np.asarray([0, -1, 0, 0, 0, 0, 0]) * unit.elementary_charge

forcefield = ForceField("openff_unconstrained-2.0.0.offxml")
topology = pf6_minus.to_topology()

system = forcefield.create_openmm_system(topology, charge_from_molecules=[pf6_minus])

# Inspect the openmm.System object to see which charges 
# actually ended up on each particle
for force in system.getForces():
    if type(force) == NonbondedForce:
        for i in range(force.getNumParticles()):
            print(force.getParticleParameters(i)[0])

A more programmatic approach is to modify the force field itself to include your pre-specified charges. This method (LibraryChargeType.from_molecule) creates a parameter whose SMIRKS pattern is unique to your molecule and also contains those charges.

import numpy as np
from openmm import NonbondedForce, unit

from openff.toolkit.topology import Molecule
from openff.toolkit.typing.engines.smirnoff import ForceField
from openff.toolkit.typing.engines.smirnoff.parameters import LibraryChargeHandler

pf6_minus = Molecule.from_smiles("F[P-](F)(F)(F)(F)F")

# Assign partial charges from a list-like object
pf6_minus.partial_charges = np.asarray([0, -1, 0, 0, 0, 0, 0]) * unit.elementary_charge

library_charge_type = LibraryChargeHandler.LibraryChargeType.from_molecule(pf6_minus)

forcefield = ForceField("openff_unconstrained-2.0.0.offxml")
forcefield["LibraryCharges"].add_parameter(parameter=library_charge_type)

topology = pf6_minus.to_topology()

system = forcefield.create_openmm_system(topology)

# Inspect the openmm.System object to see which charges
# actually ended up on each particle
for force in system.getForces():
    if type(force) == NonbondedForce:
        for i in range(force.getNumParticles()):
            print(force.getParticleParameters(i)[0])

Either approach should produce the same result:

$ python library_charges.py
0.0 e
-1.0 e
0.0 e
0.0 e
0.0 e
0.0 e
0.0 e

Interchange should be able to process these objects as well:

# copy everything up to `system = forcefield.create_openmm_system`
...
from openff.interchange.components.interchange import Interchange

out = Interchange.from_smirnoff(force_field=forcefield, topology=topology)
out.to_whatever("file.txt")  # GROMACS, Amber, LAMMPS, etc.

but there may be some bugs; please carefully validate anything that Interchange exports if you use it in research work.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Override Default Expense Account in iProcurement
Expectations are that No Charge Account should default and User need to enter manually and process the Purchase Requisition.
Read more >
How To Override BSV For Expense Purchasing
If you are dealing with an expense item, the charge account could default from your sub-inventory or organization or item expense account If...
Read more >
Charge Account not defaulting in PO distributions
Click on Assignment. Click on Purchase Order Information. 2.The Expense Charge Account Rules can be setup to enableoverride of one ormore ...
Read more >
Quick Reference Guide: Assign Costing Allocations
To Override Default Accounts on either an earning or the position: 5a. Use the Prompt Icon to select the FAS Account for the...
Read more >
R12 : Purchasing : How account generator defaults Accounts
While you cannot edit the accrual, budget, or variance accounts that the Account Generator constructs, you can override or specify the charge ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found