Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def runTest(self):
"""Check liquid target with existing simulation data"""
if not sys.version_info <= (2,7):
self.skipTest("Existing pickle file only works with Python 3")
self.logger.debug("\nSetting input file to 'single.in'\n")
input_file='single.in'
## The general options and target options that come from parsing the input file
self.logger.debug("Parsing inputs...\n")
options, tgt_opts = parse_inputs(input_file)
self.logger.debug("options:\n%s\n\ntgt_opts:\n%s\n\n" % (str(options), str(tgt_opts)))
forcefield = FF(options)
objective = Objective(options, tgt_opts, forcefield)
## The optimizer component of the project
self.logger.debug("Creating optimizer: ")
optimizer = Optimizer(options, objective, forcefield)
self.assertEqual(Optimizer, type(optimizer), msg="\nExpected forcebalance optimizer object")
self.logger.debug(str(optimizer) + "\n")
## Actually run the optimizer.
self.logger.debug("Done setting up! Running optimizer...\n")
result = optimizer.Run()
self.logger.debug("\nOptimizer finished. Final results:\n")
self.logger.debug(str(result) + '\n')
def setUp(self):
super(ForceBalanceTestCase,self).setUp()
os.chdir('studies/001_water_tutorial')
self.input_file='very_simple.in'
targets = tarfile.open('targets.tar.bz2','r')
targets.extractall()
targets.close()
self.options, self.tgt_opts = forcebalance.parser.parse_inputs(self.input_file)
self.options.update({'writechk':'checkfile.tmp'})
self.forcefield = forcebalance.forcefield.FF(self.options)
self.objective = forcebalance.objective.Objective(self.options, self.tgt_opts, self.forcefield)
try: self.optimizer = forcebalance.optimizer.Optimizer(self.options, self.objective, self.forcefield)
except: self.fail("\nCouldn't create optimizer")
def test_parse_inputs_yields_consistent_results(self):
"""Check parse_inputs() gives consistent results"""
output1 = forcebalance.parser.parse_inputs('test/files/very_simple.in')
output2 = forcebalance.parser.parse_inputs('test/files/very_simple.in')
self.assertEqual(output1,output2)
os.chdir('test/files')
output3 = forcebalance.parser.parse_inputs('very_simple.in')
output4 = forcebalance.parser.parse_inputs('very_simple.in')
self.assertEqual(output3,output4)
# directory change should lead to different result in output['root']
self.assertNotEqual(output1,output3)
# different parameters from the same file should yield different results
shutil.copyfile('0.energy_force.in', 'test.in')
output5 = forcebalance.parser.parse_inputs('test.in')
shutil.copyfile('1.netforce_torque.in','test.in')
output6 = forcebalance.parser.parse_inputs('test.in')
self.assertNotEqual(output5,output6)
os.remove('test.in')
self.assertEqual(output1,output2)
os.chdir('test/files')
output3 = forcebalance.parser.parse_inputs('very_simple.in')
output4 = forcebalance.parser.parse_inputs('very_simple.in')
self.assertEqual(output3,output4)
# directory change should lead to different result in output['root']
self.assertNotEqual(output1,output3)
# different parameters from the same file should yield different results
shutil.copyfile('0.energy_force.in', 'test.in')
output5 = forcebalance.parser.parse_inputs('test.in')
shutil.copyfile('1.netforce_torque.in','test.in')
output6 = forcebalance.parser.parse_inputs('test.in')
self.assertNotEqual(output5,output6)
os.remove('test.in')
def main():
""" Print out all of the options available to ForceBalance. """
options = None
tgt_opts = [None]
if len(sys.argv) == 2:
options, tgt_opts = parser.parse_inputs(sys.argv[1])
out = []
out.append("# ForceBalance input file generated by MakeInputFile.py")
out.append("# The octothorpe '#' is a comment symbol")
out.append("# There are two sections, the main options ($options) and the target options ($target)")
out.append("# A ForceBalance calculation will have one $options section and as one $target section per optimization target")
out.append("# The most important options are listed at the top; options are also roughly grouped by their application")
out.append("# Note: If the specified value is 'None' then the option will truly be set to None - not the string 'None'")
out.append("# Note: Section option types are more complicated and may require you to read the documentation")
out.append("# Note: Boolean option types require no value, the key being present implies 'True'")
out.append("# Note: List option types are specified using spaces as the delimiter - i.e. forcefield ff1.itp ff2.itp ; delete empty brackets before use [] ")
out.append("")
out += parser.printsection("$options",options,parser.gen_opts_types)
for tgt_opt in tgt_opts:
out.append("\n")
out += parser.printsection("$target",tgt_opt,parser.tgt_opts_types)
for line in out:
def recover(self):
## Determine the save file name.
base, ext = os.path.splitext(self.input_file)
if not base.endswith(".sav"):
savfnm = base+".sav"
else:
savfnm = base+ext
## Parse the save file for mvals, if exist.
if os.path.exists(savfnm):
soptions, stgt_opts = parse_inputs(savfnm)
if soptions['read_mvals'] and np.max(np.abs(self.mvals0)) != 0.0:
warn_press_key("Save file read_mvals will overwrite input file.\nInput file: %s\nSave file : %s\n" % (soptions['read_mvals'], self.mvals0))
self.mvals0 = np.array(soptions['read_mvals'])
self.read_mvals = np.array(soptions['read_mvals'])
maxrd = max([T.maxrd() for T in self.Objective.Targets])
if maxrd < 0: return
## This will be invoked if we quit RIGHT at the start of an iteration (i.e. jobs were launched but none were finished)
if len(set([T.maxrd() for T in self.Objective.Targets])) == 1 and any([((T.maxid() - T.maxrd()) > 0) for T in self.Objective.Targets]):
maxrd += 1
printcool("Continuing optimization from iteration %i\nThese targets will load data from disk:\n%s" % \
(maxrd, '\n'.join([T.name for T in self.Objective.Targets if T.maxrd() == maxrd])), color=4)
## If data exists in the temp-dir corresponding to the highest
## iteration number, read the data.
for T in self.Objective.Targets:
if T.maxrd() == maxrd:
""" Create instances of ForceBalance components and run the optimizer.
The triumvirate, trifecta, or trinity of components are:
- The force field
- The objective function
- The optimizer
Cipher: "All I gotta do here is pull this plug... and there you have to watch Apoc die"
Apoc: "TRINITY" *chunk*
The force field is a class defined in forcefield.py.
The objective function is a combination of target classes and a penalty function class.
The optimizer is a class defined in this file.
"""
try:
## The general options and target options that come from parsing the input file
options, tgt_opts = parse_inputs(input_file)
## Set the continue_ option.
if continue_: options['continue'] = True
## The force field component of the project
forcefield = FF(options)
## The objective function
objective = Objective(options, tgt_opts, forcefield)
## The optimizer component of the project
optimizer = Optimizer(options, objective, forcefield)
## Actually run the optimizer.
optimizer.Run()
except:
import traceback
traceback.print_exc()
if debug:
import pdb
pdb.post_mortem()