mosdef-hub / foyer

A package for atom-typing as well as applying and disseminating forcefields
https://foyer.mosdef.org
MIT License
118 stars 77 forks source link

xml_writer.py usage #464

Closed chemistry2010 closed 2 years ago

chemistry2010 commented 2 years ago

Dear foyer team,

I am trying to use "xml_writer.py" to write foyer xml file from a parametrized structure. I am testing this feature on a test molecule, i.e. n-butane. Below are the steps I follow to achieve the goal: ........... import parmed as pmd from foyer import Forcefield from foyer.xml_writer import write_foyer

mol = pmd.load_file('butane.mol2', structure=True) ff = Forcefield(forcefield_files='./oplsaa.xml') mol_ff = ff.apply(mol,verbose=True)

xml_file = mol_ff.write_foyer(filename='relevant_params.xml',forcefield='./oplsaa.xml') ...........

The foyer itself is doing a great job for atom typing, but when I try to write foyer xml file from a parametrized n-butane it gives me the following error:

Traceback (most recent call last): File "/path_to_folder/foyer_opls_test/foyer_atom_typing.py", line 24, in xml_file = mol_ff.write_foyer(filename='relevant_params.xml',forcefield='./oplsaa.xml') File "/path_to_folder/.conda/envs/foyer/lib/python3.9/site-packages/foyer/xml_writer.py", line 78, in write_foyer _write_atoms(self, root, self.atoms, forcefield, unique) File "/path_to_folder/.conda/envs/foyer/lib/python3.9/site-packages/foyer/xml_writer.py", line 106, in _write_atoms nonbonded.set("lj14scale", str(_infer_lj14scale(self, combining_rule))) File "/path_to_folder/.conda/envs/foyer/lib/python3.9/site-packages/foyer/xml_writer.py", line 475, in _infer_lj14scale raise ValueError( ValueError: Unexpected 1-4 sigma value found in adj <NonbondedException; <Atom C [2]; In RES 0> and <Atom H [4]; In RES 0>, type=<NonbondedExceptionType; rmin=3.3203, epsilon=0.0222, chgscale=0.5000>>. Expected 2.958039891549808and found 3.0. This estimate was made assuming a combining rule of lorentz

I thought that it should directly write the XML file similar to the "oplsaa.xml" with force field parameters, in this case, only relevant for n-butane. I would appreciate it so much if you can help me to resolve this issue and can tell me how to properly use the "write_foyer" function ?

Thanks a lot in advance!

umesh-timalsina commented 2 years ago

Hello @chemistry2010, thank you for the issue.

Instead of using the XML reference in write_foyer, you should use the proper forcefield reference. Example usage with mbuild's ethane molecule is given below:

>>> import foyer
Warning: importing 'simtk.openmm' is deprecated.  Import 'openmm' instead.
>>> import mbuild as mb
>>> oplsaa = foyer.forcefields.load_OPLSAA()
>>> ethane = mb.lib.molecules.Ethane()
>>> pmd_ethane = oplsaa.apply(ethane)
/home/umesh/krow/mosdef/foyer/foyer/forcefield.py:348: UserWarning: Parameters have not been assigned to all impropers. Total system impropers: 8, Parameterized impropers: 0. Note that if your system contains torsions of Ryckaert-Bellemans functional form, all of these torsions are processed as propers
  warnings.warn(msg)
>>> pmd_ethane.write_foyer('ethane_params.xml', forcefield=oplsaa)
>>> 

This is necessary because, the SMARTS information cannot be directly saved in a parmed structure or its atomtype. For the implementation details please refer to the following link.

chemistry2010 commented 2 years ago

Dear @umesh-timalsina, thank you for your quick answer !

Now everything is working !