Skip to content

Commit 6a978c8

Browse files
committed
Adding Custom Torsion
This adds the custom torsion force reading the expression, parameters, and atoms from the XML from SMOG2
1 parent a657341 commit 6a978c8

File tree

3 files changed

+104
-2
lines changed

3 files changed

+104
-2
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ OpenSMOG/Test_Custom_Dihedral/testODdih/testODdih.gro
2121
OpenSMOG/Test_Custom_Dihedral/testODdih/testODdih.ndx
2222
OpenSMOG/Test_Custom_Dihedral/testODdih/testODdih.top
2323
OpenSMOG/Test_Custom_Dihedral/testODdih/testODdih.xml
24+
*.ipynb

OpenSMOG/OpenSMOG.py

+76
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,15 @@ def _splitForces_nb(self):
463463
forces[nb_data[0][n]] = [nb_data[1][n], nb_data[2][n], nb_data[3][n]]
464464
self.nonbond = forces
465465

466+
def _splitForces_dihedrals(self):
467+
#Custom Dihedrals
468+
dihedrals_data=self.data['dihedrals']
469+
n_forces = len(dihedrals_data[3])
470+
forces = {}
471+
for n in range(n_forces):
472+
forces[dihedrals_data[3][n]] = [dihedrals_data[0][n], dihedrals_data[1][n], dihedrals_data[2][n]]
473+
self.dihedrals = forces
474+
466475
def _customSmogForce(self, name, data):
467476
#first set the equation
468477
contacts_ff = CustomBondForce(data[0])
@@ -488,6 +497,31 @@ def _customSmogForce(self, name, data):
488497
contacts_ff.setForceGroup(self.forceCount)
489498
self.forceCount +=1
490499

500+
def _customSmogForce_cd(self, name, data):
501+
#first set the equation
502+
dihedrals_ff = CustomTorsionForce(data[0])
503+
504+
#second set the number of variable
505+
for pars in data[1]:
506+
dihedrals_ff.addPerTorsionParameter(pars)
507+
508+
#third, apply the bonds from each pair of atoms and the related variables.
509+
pars = [pars for pars in data[1]]
510+
511+
for iteraction in data[2]:
512+
atom_index_i = int(iteraction['i'])-1
513+
atom_index_j = int(iteraction['j'])-1
514+
atom_index_k = int(iteraction['k'])-1
515+
atom_index_l = int(iteraction['l'])-1
516+
parameters = [float(iteraction[k]) for k in pars]
517+
518+
dihedrals_ff.addTorsion(atom_index_i, atom_index_j, atom_index_k, atom_index_l, parameters)
519+
520+
self.forcesDict[name] = dihedrals_ff
521+
dihedrals_ff.setForceGroup(self.forceCount)
522+
self.forceCount +=1
523+
524+
491525
def _customSmogForce_nb(self, name, data):
492526
#first set the equation
493527
nonbond_ff = CustomNonbondedForce(data[0])
@@ -710,6 +744,41 @@ def import_xml2OpenSMOG(file_xml):
710744
xml_data['nonbond']=[NonBond_Num,NBExpression,NBExpressionParameters,NBParameters]
711745
else:
712746
self.nonbond_present=False
747+
748+
749+
## Custom Dihedrals
750+
CDForce_Names=[]
751+
CDExpression=[]
752+
CDParameters=[]
753+
ijkl=[]
754+
755+
self.dihedrals_present=False
756+
if root.find('dihedrals') == None:
757+
print('''
758+
Only dihedrals contribution in the top file will be added
759+
''')
760+
else:
761+
self.dihedrals_present=True
762+
dihedrals_xml=root.find('dihedrals')
763+
for i in range(len(dihedrals_xml)):
764+
for name in dihedrals_xml[i].iter('dihedrals_type'):
765+
CDForce_Names.append(name.attrib['name'])
766+
767+
for expr in dihedrals_xml[i].iter('expression'):
768+
CDExpression.append(expr.attrib['expr'])
769+
770+
CDinternal_Param=[]
771+
for par in dihedrals_xml[i].iter('parameter'):
772+
CDinternal_Param.append(par.text)
773+
CDParameters.append(CDinternal_Param)
774+
775+
internal_ijkl=[]
776+
for atoms_ijkl in dihedrals_xml[i].iter('interaction'):
777+
internal_ijkl.append(atoms_ijkl.attrib)
778+
ijkl.append(internal_ijkl)
779+
780+
xml_data['dihedrals']=[CDExpression,CDParameters,ijkl,CDForce_Names]
781+
713782
return xml_data
714783

715784
if not (self.forceApplied):
@@ -725,6 +794,13 @@ def import_xml2OpenSMOG(file_xml):
725794
print("Creating Contacts force {:} from xml file".format(force))
726795
self._customSmogForce(force, self.contacts[force])
727796
self.system.addForce(self.forcesDict[force])
797+
798+
if self.dihedrals_present==True:
799+
self._splitForces_dihedrals()
800+
for force in self.dihedrals:
801+
print("Creating Dihedrals force {:} from xml file".format(force))
802+
self._customSmogForce_cd(force, self.dihedrals[force])
803+
self.system.addForce(self.forcesDict[force])
728804

729805
if self.nonbond_present==True:
730806
self._splitForces_nb()

OpenSMOG/share/OpenSMOG.xsd

+27-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
<?xml version="1.0" encoding="ISO-8859-1"?>
22
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
33
<!-- INTERACTION TYPE -->
4-
<xs:complexType name="interactionType">
4+
<xs:complexType name="contactInteractionType">
55
<xs:attribute name="i" type="xs:integer" use="required"/>
66
<xs:attribute name="j" type="xs:integer" use="required"/>
77
<xs:anyAttribute processContents="skip"/>
88
</xs:complexType>
9+
<xs:complexType name="dihedralInteractionType">
10+
<xs:attribute name="i" type="xs:integer" use="required"/>
11+
<xs:attribute name="j" type="xs:integer" use="required"/>
12+
<xs:attribute name="k" type="xs:integer" use="required"/>
13+
<xs:attribute name="l" type="xs:integer" use="required"/>
14+
<xs:anyAttribute processContents="skip"/>
15+
</xs:complexType>
16+
917
<!-- nonbond_param TYPE -->
1018
<xs:complexType name="nonbondparamType">
1119
<xs:attribute name="type1" type="xs:string" use="required"/>
@@ -34,10 +42,20 @@
3442
<xs:sequence>
3543
<xs:element name="expression" type="expressionType" minOccurs="1" maxOccurs="unbounded"/>
3644
<xs:element name="parameter" type="parameterType" minOccurs="1" maxOccurs="unbounded"/>
37-
<xs:element name="interaction" type="interactionType" minOccurs="1" maxOccurs="unbounded"/>
45+
<xs:element name="interaction" type="contactInteractionType" minOccurs="1" maxOccurs="unbounded"/>
3846
</xs:sequence>
3947
<xs:attribute name="name" type="xs:string" use="required"/>
4048
</xs:complexType>
49+
<!-- DIHEDRAL POTENTIAL TYPE -->
50+
<xs:complexType name="dihedralpotType">
51+
<xs:sequence>
52+
<xs:element name="expression" type="expressionType" minOccurs="1" maxOccurs="unbounded"/>
53+
<xs:element name="parameter" type="parameterType" minOccurs="1" maxOccurs="unbounded"/>
54+
<xs:element name="interaction" type="dihedralInteractionType" minOccurs="1" maxOccurs="unbounded"/>
55+
</xs:sequence>
56+
<xs:attribute name="name" type="xs:string" use="required"/>
57+
</xs:complexType>
58+
4159
<!-- CONTACTS TYPE -->
4260
<xs:complexType name="contactsType">
4361
<xs:sequence>
@@ -55,6 +73,12 @@
5573
<xs:element name="constant" type="constantType" maxOccurs="unbounded"/>
5674
</xs:sequence>
5775
</xs:complexType>
76+
<!-- DIHEDRALS TYPE -->
77+
<xs:complexType name="dihedralsType">
78+
<xs:sequence>
79+
<xs:element name="dihedrals_type" type="dihedralpotType" maxOccurs="unbounded"/>
80+
</xs:sequence>
81+
</xs:complexType>
5882
<!-- NB TYPE -->
5983
<xs:complexType name="nbType">
6084
<xs:sequence>
@@ -74,6 +98,7 @@
7498
<xs:complexType name="OpenSMOGType">
7599
<xs:all>
76100
<xs:element name="contacts" type="contactsType" minOccurs="0" maxOccurs="1"/>
101+
<xs:element name="dihedrals" type="dihedralsType" minOccurs="0" maxOccurs="1"/>
77102
<xs:element name="constants" type="constantsType" minOccurs="0" maxOccurs="1"/>
78103
<xs:element name="nonbond" type="nonbondType" minOccurs="0" maxOccurs="1"/>
79104
</xs:all>

0 commit comments

Comments
 (0)