Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def submit_jobs(self, mvals, AGrad=False, AHess=False):
n=0
id_string = "%s_%i-%i" % (self.name, Counter(), n)
while os.path.exists('%s.out' % id_string):
n+=1
id_string = "%s_%i-%i" % (self.name, Counter(), n)
with open('forcebalance.p','w') as f: forcebalance.nifty.lp_dump((mvals, AGrad, AHess, id_string, self.r_options, self.r_tgt_opts, self.FF),f)
forcebalance.nifty.LinkFile(os.path.join(os.path.split(__file__)[0],"data","rtarget.py"),"rtarget.py")
forcebalance.nifty.LinkFile(os.path.join(self.root,"temp", self.name, "target.tar.bz2"),"%s.tar.bz2" % self.name)
wq = getWorkQueue()
logger.info("Sending target '%s' to work queue for remote evaluation\n" % self.name)
# input:
# forcebalance.p: pickled mvals, options, and forcefield
# rtarget.py: remote target evaluation script
# target.tar.bz2: tarred target
# output:
# objective.p: pickled objective function dictionary
# indicate.log: results of target.indicate() written to file
forcebalance.nifty.queue_up(wq, "python rtarget.py > %s.out 2>&1" % id_string,
["forcebalance.p", "rtarget.py", "%s.tar.bz2" % self.name],
if exception.errno != errno.EEXIST:
raise
# Goto subdir
os.chdir(str(pt.idnr))
# Link dir contents from target subdir to current temp directory.
for f in self.scripts:
LinkFile(os.path.join(self.root, self.tempdir, f),
os.path.join(os.getcwd(), f))
link_dir_contents(os.path.join(self.root, self.tgtdir,
str(pt.idnr)), os.getcwd())
# Dump the force field to a pickle file
lp_dump((self.FF, mvals, self.OptionDict, AGrad), 'forcebalance.p')
# Run the simulation chain for point.
cmdstr = ("%s python md_chain.py " % self.mdpfx +
" ".join(self.quantities) + " " +
"--engine %s " % self.engname +
"--length %d " % self.n_sim_chain +
"--name %s " % self.simpfx +
"--temperature %f " % pt.temperature +
"--pressure %f " % pt.pressure +
"--nequil %d " % self.eq_steps +
"--nsteps %d " % self.md_steps)
_exec(cmdstr, copy_stderr=True, outfnm='md_chain.out')
os.chdir('..')
Results['Hydration_Derivatives'] = Derivs_liq - Derivs_gas
# Code of the future!
# Don't know how to use it yet though.
# Engine.molecular_dynamics(**MDOpts)
# logger.info("MD simulation took %.3f seconds\n" % click())
# # Extract properties.
# Results = Engine.md_extract(OrderedDict([(i, {}) for i in Tgt.timeseries.keys()]))
# potential = properties['Potential']
# Calculate energy and dipole derivatives if needed.
# if AGrad:
# Results['derivatives'] = energy_derivatives(Engine, FF, mvals, h, pgrad, dipole='dipole' in Tgt.timeseries.keys())
# Dump results to file
logger.info("Writing final force field.\n")
pvals = FF.make(mvals)
logger.info("Writing all simulation data to disk.\n")
lp_dump(Results, 'md_result.p')
GEps0 = prefactor*(GD2/avg(V) - mBeta*covde(V)*D2/avg(V)**2)/T
Sep = printcool("Dielectric constant: % .4e +- %.4e\nAnalytic Derivative:" % (Eps0, Eps0_err))
FF.print_map(vals=GEps0)
if FDCheck:
GEps0_fd = property_derivatives(Liquid, FF, mvals, h, pgrad, kT, calc_eps0, {'d_':Dips,'v_':V})
Sep = printcool("Numerical Derivative:")
FF.print_map(vals=GEps0_fd)
Sep = printcool("Difference (Absolute, Fractional):")
absfrac = ["% .4e % .4e" % (i-j, (i-j)/j) for i,j in zip(GEps0,GEps0_fd)]
FF.print_map(vals=absfrac)
logger.info("Writing final force field.\n")
pvals = FF.make(mvals)
logger.info("Writing all simulation data to disk.\n")
lp_dump((Rhos, Volumes, Potentials, Energies, Dips, G, [GDx, GDy, GDz], mPotentials, mEnergies, mG, Rho_err, Hvap_err, Alpha_err, Kappa_err, Cp_err, Eps0_err, NMol),'npt_result.p')
FF.print_map(vals=GScd)
logger.info(Sep)
def calc_scd(b = None, **kwargs):
if b is None: b = np.ones(L,dtype=float)
if 's_' in kwargs:
s_ = kwargs['s_']
return bzavg(s_,b)
# calc_scd(None, **{'s_': Scds})
logger.info("Writing final force field.\n")
pvals = FF.make(mvals)
logger.info("Writing all simulation data to disk.\n")
lp_dump((Rhos, Volumes, Potentials, Energies, Dips, G, [GDx, GDy, GDz], Rho_err, Alpha_err, Kappa_err, Cp_err, Eps0_err, NMol, Als, Al_err, Scds, Scd_err, LKappa_err),'npt_result.p')
Tgt = forcebalance.objective.Implemented_Targets[tgt_opts['type']](options,tgt_opts,forcefield)
Tgt.read_objective = False
Tgt.read_indicate = False
Tgt.write_objective = False
Tgt.write_indicate = False
# The "active" parameters are determined by the master, written to disk and sent over.
Tgt.pgrad = pgrad
# Should the remote target be submitting jobs of its own??
Tgt.submit_jobs(mvals, AGrad = AGrad, AHess = AHess)
Ans = Tgt.meta_get(mvals, AGrad = AGrad, AHess = AHess)
forcebalance.nifty.lp_dump(Ans, 'objective.p') # or some other method of storing resulting objective
# also run target.indicate()
logger = forcebalance.output.getLogger("forcebalance")
logger.addHandler(forcebalance.output.RawFileHandler('indicate.log'))
Tgt.indicate()
print("\n")
Hvap_avg *= -1
GHvap *= -1
bar = printcool("Density: % .4f +- % .4f kg/m^3, Derivatives below" % (Rho_avg, Rho_err))
FF.print_map(vals=GRho)
print bar
print "Box energy:", np.mean(Energies)
print "Monomer energy:", np.mean(mEnergies)
bar = printcool("Enthalpy of Vaporization: % .4f +- %.4f kJ/mol, Derivatives below" % (Hvap_avg, Hvap_err))
FF.print_map(vals=GHvap)
print bar
# Print the final force field.
pvals = FF.make(mvals,False)
with open(os.path.join('npt_result.p'),'w') as f: lp_dump((np.mean(Rhos), Rho_err, GRho, Hvap_avg, Hvap_err, GHvap),f)
Engine = EngineClass(name=Sim.type, **Sim.EngOpts)
click() # Start timer.
# This line runs the condensed phase simulation.
Engine.molecular_dynamics(**Sim.MDOpts)
logger.info("MD simulation took %.3f seconds\n" % click())
# Extract properties.
Results = Engine.md_extract(OrderedDict([(i, {}) for i in Sim.timeseries.keys()]))
# Calculate energy and dipole derivatives if needed.
if AGrad:
Results['derivatives'] = energy_derivatives(Engine, FF, mvals, h, pgrad, dipole='dipole' in Sim.timeseries.keys())
# Dump results to file
logger.info("Writing final force field.\n")
pvals = FF.make(mvals)
logger.info("Writing all simulation data to disk.\n")
with wopen('md_result.p') as f:
lp_dump(Results, f)