Source code for metafalcon.analysis.reset

#!/usr/bin/env python2

"""Reset dynamics output to a specified dynamics step in the dynamics-\*.log-file."""

import argparse
import os

[docs]def reset_log(step): """ Reset the `dynamics-*.log` file to the given step. Parameters ---------- step : int number of the last step to be kept in the log-file """ def is_in_log(step, logfile): with open(logfile) as f: for line in f: if "step: " in line: first = int(line.split()[1]) break with open(logfile) as f: for line in reversed(f.readlines()): if "step: " in line: last = int(line.split()[1]) break return (step >= first) and (step <= last) logfiles = [log for log in os.listdir(".") if ".log" in log and "dynamics-" in log] for logfile in logfiles: if is_in_log(step, logfile): log_ndx = int(logfile.replace(".", "-").split("-")[-2]) with open("dynamics-%i.log" % log_ndx) as f, open("dynamics-%i.log.1" % log_ndx, "w") as g: for line in f: if "step: %i" % (step+1) in line: break g.write(line) for logfile in logfiles: ndx = int(logfile.replace(".", "-").split("-")[-2]) if ndx > log_ndx: os.remove(logfile) os.system("mv dynamics-%i.log.1 dynamics-%i.log" % (log_ndx, log_ndx))
[docs]def cap_file(maxlines, filename): """ Cap a file after the specified number of lines. Parameters ---------- maxlines : int number of lines to be kept in the file filename : str name of the file to be capped """ if os.path.exists(filename): with open(filename) as f: lines = f.readlines()[:maxlines] with open(filename, "w") as g: g.writelines(lines)
[docs]def get_nat(): """ Get the number of atoms from the `dynamics-0.log` file. Returns ------- nat : int number of atoms """ nat = -1 with open("dynamics.in") as f: for line in f: if "COORD_BEGIN" in line: while "COORD_END" not in line: nat += 1 line = f.next() return nat
[docs]def cap_gradfile(maxgrad, filename): """ Cap a gradient file after the specified number of gradient steps. Parameters ---------- maxgrad : int number of gradient steps to be kept in the file filename : str name of the file to be capped """ if os.path.exists(filename): nat = get_nat() with open(filename) as f: lines = f.readlines()[:(maxgrad*(nat+1))] with open(filename, "w") as g: g.writelines(lines)
[docs]def reset_vg(step, extension=""): """ Reset the files `vg_centers.dat` and `vg_steps.dat` to the given step. Parameters ---------- step : int number of time steps to be kept extension : str filename extension of the metadynamics object """ with open("vg_centers%s.dat" % extension) as f: vgcenters = f.readlines() with open("vg_steps%s.dat" % extension) as f: vgsteps = f.readlines() with open("vg_centers%s.dat" % extension, "w") as f, open("vg_steps%s.dat" % extension, "w") as g: for vgstep, vgcenter in zip(vgsteps, vgcenters): if len(vgstep.split()) == 1 and int(vgstep) > step: continue f.write(vgcenter) g.write(vgstep)
[docs]def add_restart(): """Add restart option to the `meta-config.json` file.""" with open("dynamics.in", "a") as f: f.write("\nRESTART")
parser = argparse.ArgumentParser(description="Reset metadynamics folder to specific time \ step.") parser.add_argument("--step", type=int, default=0, help="step from which the dynamics should be restarted") if __name__ == "__main__": args = parser.parse_args() step = vars(args)["step"] reset_log(step) cap_file(step, "gap.dat") cap_file(step+1, "gap_unbiased.dat") cap_file(step, "gap_derivatives.dat") cap_file(step, "dgap.dat") cap_file(step, "coupling.dat") cap_file(step, "wiener.dat") reset_vg(step) if os.path.exists("vg_centers_offdiag.dat"): reset_vg(step, extension="_offdiag") if os.path.exists("original.wfu"): os.system("cp original.wfu temp.wfu") cap_gradfile(step, "4dj_dx.dat") cap_gradfile(step, "dgap_dx.dat") cap_gradfile(step, "dnewgap_dx.dat") add_restart()