ForceBalance API  1.3
Automated optimization of force fields and empirical potentials
abinitio_internal.py
Go to the documentation of this file.
1 """ @package forcebalance.abinitio_internal Internal implementation of energy matching (for TIP3P water only)
2 
3 @author Lee-Ping Wang
4 @date 04/2012
5 """
6 from __future__ import division
7 
8 from builtins import range
9 import os
10 from forcebalance import BaseReader
11 from forcebalance.abinitio import AbInitio
12 from forcebalance.forcefield import FF
13 import numpy as np
14 import sys
15 import pickle
16 import shutil
17 import itertools
18 
19 class AbInitio_Internal(AbInitio):
20 
21  """Subclass of Target for force and energy matching
22  using an internal implementation. Implements the prepare and
23  energy_force_driver methods. The get method is in the superclass.
24 
25  The purpose of this class is to provide an extremely simple test
26  case that does not require the user to install any external
27  software. It only runs with one of the included sample test
28  calculations (internal_tip3p), and the objective function is
29  energy matching.
30 
31  @warning This class is only intended to work with a very specific
32  test case (internal_tip3p). This is because the topology and
33  ordering of the atoms is hard-coded (12 water molecules with 3
34  atoms each).
35 
36  @warning This class does energy matching only (no forces)
37 
38  """
39 
40  def __init__(self,options,tgt_opts,forcefield):
41 
43  self.loop_over_snapshots = True
44 
45  self.coords = "all.gro"
46 
47  super(AbInitio_Internal,self).__init__(options,tgt_opts,forcefield)
48 
49  def energy_force_driver_all(self):
50  """ Here we actually compute the interactions and return the
51  energies and forces. I verified this to give the same answer
52  as GROMACS. """
53 
54  M = []
55  # Loop through the snapshots
56  ThisFF = FF({'forcefield':['tip3p.xml'], 'ffdir':'', 'priors':{}},verbose=False)
57  r_0 = ThisFF.pvals0[ThisFF.map['HarmonicBondForce.Bond/length/OW.HW']] * 10
58  k_ij = ThisFF.pvals0[ThisFF.map['HarmonicBondForce.Bond/k/OW.HW']]
59  t_0 = ThisFF.pvals0[ThisFF.map['HarmonicAngleForce.Angle/angle/HW.OW.HW']] * 180 / np.pi
60  k_ijk = ThisFF.pvals0[ThisFF.map['HarmonicAngleForce.Angle/k/HW.OW.HW']]
61  q_o = ThisFF.pvals0[ThisFF.map['NonbondedForce.Atom/charge/tip3p-O']]
62  q_h = ThisFF.pvals0[ThisFF.map['NonbondedForce.Atom/charge/tip3p-H']]
63  sig = ThisFF.pvals0[ThisFF.map['NonbondedForce.Atom/sigma/tip3p-O']]
64  eps = ThisFF.pvals0[ThisFF.map['NonbondedForce.Atom/epsilon/tip3p-O']]
65  facel = 1389.35410
66 
67  for I in range(self.ns):
68  xyz = self.mol.xyzs[I]
69  Bond_Energy = 0.0
70  Angle_Energy = 0.0
71  VdW_Energy = 0.0
72  Coulomb_Energy = 0.0
73  for i in range(0,len(xyz),3):
74  o = i
75  h1 = i+1
76  h2 = i+2
77  # First O-H bond.
78  r_1 = xyz[h1] - xyz[o]
79  r_1n = np.linalg.norm(r_1)
80  Bond_Energy += 0.5 * k_ij * ((r_1n - r_0) / 10)**2
81  # Second O-H bond.
82  r_2 = xyz[h2] - xyz[o]
83  r_2n = np.linalg.norm(r_2)
84  Bond_Energy += 0.5 * k_ij * ((r_2n - r_0) / 10)**2
85  # Angle.
86  theta = np.arccos(np.dot(r_1, r_2) / (r_1n * r_2n)) * 180 / np.pi
87  Angle_Energy += 0.5 * k_ijk * ((theta - t_0) * np.pi / 180)**2
88  for j in range(0, i, 3):
89  oo = j
90  hh1 = j+1
91  hh2 = j+2
92  # Lennard-Jones interaction.
93  r_o_oo = np.linalg.norm(xyz[oo] - xyz[o]) / 10
94  sroo = sig / r_o_oo
95  VdW_Energy += 4*eps*(sroo**12 - sroo**6)
96  # Coulomb interaction.
97  for k, l in itertools.product(*[[i,i+1,i+2],[j,j+1,j+2]]):
98  q1 = q_o if (k % 3 == 0) else q_h
99  q2 = q_o if (l % 3 == 0) else q_h
100  Coulomb_Energy += q1*q2 / np.linalg.norm(xyz[k]-xyz[l])
101  Coulomb_Energy *= facel
102  Energy = Bond_Energy + Angle_Energy + VdW_Energy + Coulomb_Energy
103  Force = list(np.zeros(3*len(xyz)))
104  M.append(np.array([Energy] + Force))
105  return M
def __init__(self, options, tgt_opts, forcefield)
coords
Name of the trajectory, we need this BEFORE initializing the SuperClass.
def energy_force_driver_all(self)
Here we actually compute the interactions and return the energies and forces.
Ab-initio fitting module (energies, forces, resp).
loop_over_snapshots
LPW 2018-02-11: This is set to True if the target calculates a single-point property over several exi...
Force field module.
Subclass of Target for force and energy matching using an internal implementation.