Skip to content

Commit

Permalink
Merge pull request #241 from leeping/add_two_openmm_test
Browse files Browse the repository at this point in the history
Code updates to enable TestLiquid_OpenMM
  • Loading branch information
leeping authored Sep 20, 2021
2 parents 73c0b46 + f6ab5e5 commit cd68805
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 21 deletions.
3 changes: 1 addition & 2 deletions src/liquid.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ def prepare_temp_directory(self):

def read_data(self):
# Read the 'data.csv' file. The file should contain guidelines.
with open(os.path.join(self.tgtdir,'data.csv'),'rU') as f: R0 = list(csv.reader(f))
with open(os.path.join(self.tgtdir,'data.csv'),'r') as f: R0 = list(csv.reader(f))
# All comments are erased.
R1 = [[sub('#.*$','',word) for word in line] for line in R0 if len(line[0]) > 0 and line[0][0] != "#"]
# All empty lines are deleted and words are converted to lowercase.
Expand Down Expand Up @@ -727,7 +727,6 @@ def get_normal(self, mvals, AGrad=True, AHess=True):
@return property_results
"""

unpack = lp_load('forcebalance.p')
mvals1 = unpack[1]
if len(mvals) > 0 and (np.max(np.abs(mvals1 - mvals)) > 1e-3):
Expand Down
4 changes: 2 additions & 2 deletions src/target.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ def meta_get(self, mvals, AGrad=False, AHess=False, customdir=None):
def submit_jobs(self, mvals, AGrad=False, AHess=False):
return

def stage(self, mvals, AGrad=False, AHess=False, customdir=None, firstIteration=False):
def stage(self, mvals, AGrad=False, AHess=False, use_iterdir=True, customdir=None, firstIteration=False):
"""
Stages the directory for the target, and then launches Work Queue processes if any.
Expand All @@ -614,7 +614,7 @@ def stage(self, mvals, AGrad=False, AHess=False, customdir=None, firstIteration=
cwd = os.getcwd()

absgetdir = os.path.join(self.root,self.tempdir)
if Counter() is not None:
if use_iterdir and Counter() is not None:
## Not expecting more than ten thousand iterations
iterdir = "iter_%04i" % Counter()
absgetdir = os.path.join(absgetdir,iterdir)
Expand Down
10 changes: 6 additions & 4 deletions src/tests/test_openmmio.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,28 @@
"""
The testing functions for this class are located in test_target.py.
"""

class TestLiquid_OpenMM(TargetTests):
def setup_method(self, method):
pytest.skip("Needs optimizing to reduce runtime")
super(TestLiquid_OpenMM, self).setup_method(method)
self.check_grad_fd = False
# settings specific to this target
self.options.update({
'jobtype': 'NEWTON',
'forcefield': ['dms.xml']})

self.tgt_opt.update({'type':'LIQUID_OPENMM',
'name':'dms-liquid'})
'name':'dms-liquid', 'liquid_eq_steps':100, 'liquid_md_steps':200, 'gas_eq_steps':100, 'gas_md_steps':200})

self.ff = forcebalance.forcefield.FF(self.options)

self.ffname = self.options['forcefield'][0][:-3]
self.filetype = self.options['forcefield'][0][-3:]
self.mvals = [.5]*self.ff.np
self.mvals = np.array([.5]*self.ff.np)

self.target = forcebalance.openmmio.Liquid_OpenMM(self.options, self.tgt_opt, self.ff)
self.target.stage(self.mvals)
self.target.stage(self.mvals, AGrad=True, use_iterdir=False)


def teardown_method(self):
shutil.rmtree('temp')
Expand Down
27 changes: 14 additions & 13 deletions src/tests/test_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def setup_method(self, method):
self.tgt_opt = forcebalance.parser.tgt_opts_defaults.copy()
self.ff = None # Forcefield this target is fitting
self.options.update({'root': os.path.join(os.getcwd(), 'files')})
self.check_grad_fd = True # Whether to check gradient vs. finite difference. Set to False for liquid targets.

os.chdir(self.options['root'])

Expand All @@ -25,7 +26,6 @@ def test_get_function(self):
#os.chdir(self.target.tempdir)
os.chdir('temp/%s' % self.tgt_opt['name'])


self.logger.debug("Evaluating objective function for target...\n")
objective = self.target.get(self.mvals)
self.target.indicate()
Expand Down Expand Up @@ -76,16 +76,17 @@ def test_get_agrad(self):
assert G.any() # with AGrad=True, G should not be [0]
g = numpy.zeros(self.ff.np)

print(">ASSERT objective['G'] approximately matches finite difference calculations\n")
for p in range(self.ff.np):
mvals_lo = self.mvals[:]
mvals_hi = self.mvals[:]
mvals_lo[p]-=(self.mvals[p]/200.)
mvals_hi[p]+=(self.mvals[p]/200.)

Xlo = self.target.get(mvals_lo)['X']
Xhi = self.target.get(mvals_hi)['X']
g[p] = (Xhi-Xlo)/(self.mvals[p]/100.)
assert abs(g[p]-G[p]) < X*.01 +1e-7

if self.check_grad_fd:
print(">ASSERT objective['G'] approximately matches finite difference calculations\n")
for p in range(self.ff.np):
mvals_lo = self.mvals[:]
mvals_hi = self.mvals[:]
mvals_lo[p]-=(self.mvals[p]/200.)
mvals_hi[p]+=(self.mvals[p]/200.)

Xlo = self.target.get(mvals_lo)['X']
Xhi = self.target.get(mvals_hi)['X']
g[p] = (Xhi-Xlo)/(self.mvals[p]/100.)
assert abs(g[p]-G[p]) < X*.01 +1e-7

os.chdir('../..')

0 comments on commit cd68805

Please sign in to comment.