def init_chk(filename): """Reads a checkpoint file and returns the data contained in it. Args: filename: A string giving the name of the checkpoint file to be read from. Returns: A Beads object, Cell object and Thermostat object as read from the checkpoint file. """ # reads configuration from a checkpoint file rfile = open(filename, "r") xmlchk = xml_parse_file(rfile) # Parses the file. from ipi.inputs.simulation import InputSimulation simchk = InputSimulation() simchk.parse(xmlchk.fields[0][1]) sim = simchk.fetch() if len(sim.syslist) > 1: warning("Restart from checkpoint with " + str(len(sim.syslist)) + " systems will fetch data from the first system.") rcell = sim.syslist[0].cell rbeads = sim.syslist[0].beads rmotion = sim.syslist[0].motion return (rbeads, rcell, rmotion)
def step(self, step=None): """Does one replay time step.""" self.ptime = 0.0 self.ttime = 0.0 self.qtime = -time.time() while True: self.rstep += 1 try: if self.intraj.mode == "xyz": for b in self.beads: myframe = read_file("xyz", self.rfile) myatoms = myframe["atoms"] mycell = myframe["cell"] myatoms.q *= unit_to_internal("length", self.intraj.units, 1.0) mycell.h *= unit_to_internal("length", self.intraj.units, 1.0) b.q[:] = myatoms.q self.cell.h[:] = mycell.h elif self.intraj.mode == "pdb": for b in self.beads: myatoms, mycell = read_file("pdb", self.rfile) myatoms.q *= unit_to_internal("length", self.intraj.units, 1.0) mycell.h *= unit_to_internal("length", self.intraj.units, 1.0) b.q[:] = myatoms.q self.cell.h[:] = mycell.h elif self.intraj.mode == "chk" or self.intraj.mode == "checkpoint": # TODO: Adapt the new `Simulation.load_from_xml`? # reads configuration from a checkpoint file xmlchk = xml_parse_file(self.rfile) # Parses the file. from ipi.inputs.simulation import InputSimulation simchk = InputSimulation() simchk.parse(xmlchk.fields[0][1]) mycell = simchk.cell.fetch() mybeads = simchk.beads.fetch() self.cell.h[:] = mycell.h self.beads.q[:] = mybeads.q softexit.trigger(" # Read single checkpoint") except EOFError: softexit.trigger(" # Finished reading re-run trajectory") if (step is None) or (self.rstep > step): break self.qtime += time.time()
def gleacf(path2iipi, path2ivvac, oprefix, action, nrows, stride, dparam): # opens & parses the i-pi input file ifile = open(path2iipi, "r") xmlrestart = io_xml.xml_parse_file(ifile) ifile.close() isimul = InputSimulation() isimul.parse(xmlrestart.fields[0][1]) simul = isimul.fetch() # parses the drift and diffusion matrices of the GLE thermostat. ttype = str(type(simul.syslist[0].motion.thermostat).__name__) kbT = float(simul.syslist[0].ensemble.temp) simul.syslist[0].motion.thermostat.temp = kbT if (ttype == "ThermoGLE"): Ap = simul.syslist[0].motion.thermostat.A * unit_to_internal( "time", dt[1], float(dt[0])) Cp = simul.syslist[0].motion.thermostat.C / kbT Dp = np.dot(Ap, Cp) + np.dot(Cp, Ap.T) elif (ttype == "ThermoLangevin"): Ap = np.asarray( [1.0 / simul.syslist[0].motion.thermostat.tau]).reshape( (1, 1)) * unit_to_internal("time", dt[1], float(dt[0])) Cp = np.asarray([1.0]).reshape((1, 1)) Dp = np.dot(Ap, Cp) + np.dot(Cp, Ap.T) # imports the vvac function. ivvac = input_vvac(path2ivvac, nrows, stride) ix = ivvac[:, 0] iy = ivvac[:, 1] # computes the vvac kernel print "# computing the kernel." ker = gleKernel(ix, Ap, Dp) # (de-)convolutes the spectrum if (action == "conv"): print "# printing the output spectrum." output_vvac((ix, np.dot(iy, ker.T)), oprefix, input_vvac(path2ivvac, nrows, 1)) elif (action == "deconv"): print "# deconvoluting the input spectrum." oy = ISRA(ix, ker, iy, dparam, oprefix)
def gleacf(path2iipi, path2ivvac, oprefix, action, nrows, stride, dparam): # opens & parses the i-pi input file ifile = open(path2iipi, "r") xmlrestart = io_xml.xml_parse_file(ifile) ifile.close() isimul = InputSimulation() isimul.parse(xmlrestart.fields[0][1]) simul = isimul.fetch() # parses the drift and diffusion matrices of the GLE thermostat. ttype = str(type(simul.syslist[0].motion.thermostat).__name__) kbT = float(simul.syslist[0].ensemble.temp) simul.syslist[0].motion.thermostat.temp = kbT if(ttype == "ThermoGLE"): Ap = simul.syslist[0].motion.thermostat.A * unit_to_internal("time", dt[1], float(dt[0])) Cp = simul.syslist[0].motion.thermostat.C / kbT Dp = np.dot(Ap, Cp) + np.dot(Cp, Ap.T) elif(ttype == "ThermoLangevin"): Ap = np.asarray([1.0 / simul.syslist[0].motion.thermostat.tau]).reshape((1, 1)) * unit_to_internal("time", dt[1], float(dt[0])) Cp = np.asarray([1.0]).reshape((1, 1)) Dp = np.dot(Ap, Cp) + np.dot(Cp, Ap.T) # imports the vvac function. ivvac = input_vvac(path2ivvac, nrows, stride) ix = ivvac[:, 0] iy = ivvac[:, 1] # computes the vvac kernel print "# computing the kernel." ker = gleKernel(ix, Ap, Dp) # (de-)convolutes the spectrum if(action == "conv"): print "# printing the output spectrum." output_vvac((ix, np.dot(iy, ker.T)), oprefix, input_vvac(path2ivvac, nrows, 1)) elif(action == "deconv"): print "# deconvoluting the input spectrum." oy = ISRA(ix, ker, iy, dparam, oprefix)
def step(self): """Does one simulation time step.""" self.ptime = self.ttime = 0 self.qtime = -time.time() try: if (self.intraj.mode == "xyz"): for b in self.beads: myatoms = read_xyz(self.rfile) myatoms.q *= unit_to_internal("length", self.intraj.units, 1.0) b.q[:] = myatoms.q elif (self.intraj.mode == "pdb"): for b in self.beads: myatoms, mycell = read_pdb(self.rfile) myatoms.q *= unit_to_internal("length", self.intraj.units, 1.0) mycell.h *= unit_to_internal("length", self.intraj.units, 1.0) b.q[:] = myatoms.q self.cell.h[:] = mycell.h elif (self.intraj.mode == "chk" or self.intraj.mode == "checkpoint"): # reads configuration from a checkpoint file xmlchk = xml_parse_file(self.rfile) # Parses the file. from ipi.inputs.simulation import InputSimulation simchk = InputSimulation() simchk.parse(xmlchk.fields[0][1]) mycell = simchk.cell.fetch() mybeads = simchk.beads.fetch() self.cell.h[:] = mycell.h self.beads.q[:] = mybeads.q softexit.trigger(" # Read single checkpoint") except EOFError: softexit.trigger(" # Finished reading re-run trajectory") self.qtime += time.time()
def init_chk(filename): """Reads an checkpoint file and returns the data contained in it. Args: filename: A string giving the name of the checkpoint file to be read from. Returns: A Beads object, Cell object and Thermostat object as read from the checkpoint file. """ # reads configuration from a checkpoint file rfile = open(filename, "r") xmlchk = xml_parse_file(rfile) # Parses the file. from ipi.inputs.simulation import InputSimulation simchk = InputSimulation() simchk.parse(xmlchk.fields[0][1]) rcell = simchk.cell.fetch() rbeads = simchk.beads.fetch() rthermo = simchk.ensemble.thermostat.fetch() return (rbeads, rcell, rthermo)
def init_chk(filename): """Reads an checkpoint file and returns the data contained in it. Args: filename: A string giving the name of the checkpoint file to be read from. Returns: A Beads object, Cell object and Thermostat object as read from the checkpoint file. """ # reads configuration from a checkpoint file rfile = open(filename,"r") xmlchk = xml_parse_file(rfile) # Parses the file. from ipi.inputs.simulation import InputSimulation simchk = InputSimulation() simchk.parse(xmlchk.fields[0][1]) rcell = simchk.cell.fetch() rbeads = simchk.beads.fetch() rthermo = simchk.ensemble.thermostat.fetch() return (rbeads, rcell, rthermo)
def step(self): """Does one simulation time step.""" self.ptime = self.ttime = 0 self.qtime = -time.time() try: if self.intraj.mode == "xyz": for b in self.beads: myatoms = read_xyz(self.rfile) myatoms.q *= unit_to_internal("length", self.intraj.units, 1.0) b.q[:] = myatoms.q elif self.intraj.mode == "pdb": for b in self.beads: myatoms, mycell = read_pdb(self.rfile) myatoms.q *= unit_to_internal("length", self.intraj.units, 1.0) mycell.h *= unit_to_internal("length", self.intraj.units, 1.0) b.q[:] = myatoms.q self.cell.h[:] = mycell.h elif self.intraj.mode == "chk" or self.intraj.mode == "checkpoint": # reads configuration from a checkpoint file xmlchk = xml_parse_file(self.rfile) # Parses the file. from ipi.inputs.simulation import InputSimulation simchk = InputSimulation() simchk.parse(xmlchk.fields[0][1]) mycell = simchk.cell.fetch() mybeads = simchk.beads.fetch() self.cell.h[:] = mycell.h self.beads.q[:] = mybeads.q softexit.trigger(" # Read single checkpoint") except EOFError: softexit.trigger(" # Finished reading re-run trajectory") self.qtime += time.time()
def step(self, step=None): """Does one replay time step.""" self.ptime = 0.0 self.ttime = 0.0 self.qtime = -time.time() # If wildcard is used, check that it is consistent with Nbeads wildcard_used = False if any(char in self.intraj.value for char in "*?[]"): wildcard_used = True if len(self.rfile) != len(self.beads): info( "Error: if a wildcard is used for replay, then " "the number of files should be equal to the number of beads.", verbosity.low, ) softexit.trigger(" # Error in replay input.") while True: self.rstep += 1 try: if self.intraj.mode == "xyz": for bindex, b in enumerate(self.beads): if wildcard_used: myframe = read_file("xyz", self.rfile[bindex]) else: myframe = read_file("xyz", self.rfile) myatoms = myframe["atoms"] mycell = myframe["cell"] myatoms.q *= unit_to_internal("length", self.intraj.units, 1.0) mycell.h *= unit_to_internal("length", self.intraj.units, 1.0) b.q[:] = myatoms.q elif self.intraj.mode == "pdb": for bindex, b in enumerate(self.beads): if wildcard_used: myatoms, mycell = read_file( "pdb", self.rfile[bindex]) else: myatoms, mycell = read_file("pdb", self.rfile) myatoms.q *= unit_to_internal("length", self.intraj.units, 1.0) mycell.h *= unit_to_internal("length", self.intraj.units, 1.0) b.q[:] = myatoms.q elif self.intraj.mode == "chk" or self.intraj.mode == "checkpoint": # TODO: Adapt the new `Simulation.load_from_xml`? # reads configuration from a checkpoint file xmlchk = xml_parse_file(self.rfile) # Parses the file. from ipi.inputs.simulation import InputSimulation simchk = InputSimulation() simchk.parse(xmlchk.fields[0][1]) mycell = simchk.cell.fetch() mybeads = simchk.beads.fetch() self.beads.q[:] = mybeads.q softexit.trigger(" # Read single checkpoint") # do not assign cell if it contains an invalid value (typically missing cell in the input) if mycell.V > 0: self.cell.h[:] = mycell.h except EOFError: softexit.trigger(" # Finished reading re-run trajectory") if (step is None) or (self.rstep > step): break self.qtime += time.time()
def main(inputfile, outdir="trim"): # opens & parses the input file ifile = open(inputfile, "r") xmlrestart = io_xml.xml_parse_file(ifile) # Parses the file. ifile.close() isimul = InputSimulation() isimul.parse(xmlrestart.fields[0][1]) simul = isimul.fetch() trimstep = isimul.step.fetch() os.makedirs(outdir) # reconstructs the list of the property and trajectory files that have been output # and that should be re-ordered lprop = [] # list of property files ltraj = [] # list of trajectory files nsys = len(simul.syslist) for o in simul.outtemplate: if type(o) is CheckpointOutput: # properties and trajectories are output per system pass elif type(o) is PropertyOutput: nprop = [] isys = 0 for s in simul.syslist: # create multiple copies if s.prefix != "": filename = s.prefix + "_" + o.filename else: filename = o.filename ofilename = outdir + "/" + filename nprop.append({"filename": filename, "ofilename": ofilename, "stride": o.stride, "ifile": open(filename, "r"), "ofile": open(ofilename, "w") }) isys += 1 lprop.append(nprop) elif type(o) is TrajectoryOutput: # trajectories are more complex, as some have per-bead output if getkey(o.what) in ["positions", "velocities", "forces", "extras"]: # multiple beads nbeads = simul.syslist[0].beads.nbeads for b in range(nbeads): ntraj = [] isys = 0 # zero-padded bead number padb = (("%0" + str(int(1 + np.floor(np.log(nbeads) / np.log(10)))) + "d") % (b)) for s in simul.syslist: if s.prefix != "": filename = s.prefix + "_" + o.filename else: filename = o.filename ofilename = outdir + "/" + filename if (o.ibead < 0 or o.ibead == b): if getkey(o.what) == "extras": filename = filename + "_" + padb ofilename = ofilename + "_" + padb else: filename = filename + "_" + padb + "." + o.format ofilename = ofilename + "_" + padb + "." + o.format ntraj.append({"filename": filename, "format": o.format, "ofilename": ofilename, "stride": o.stride, "ifile": open(filename, "r"), "ofile": open(ofilename, "w") }) isys += 1 if ntraj != []: ltraj.append(ntraj) else: ntraj = [] isys = 0 for s in simul.syslist: # create multiple copies if s.prefix != "": filename = s.prefix + "_" + o.filename else: filename = o.filename filename = filename + "." + o.format ofilename = outdir + "/" + filename ntraj.append({"filename": filename, "format": o.format, "ofilename": ofilename, "stride": o.stride, "ifile": open(filename, "r"), "ofile": open(ofilename, "w") }) isys += 1 ltraj.append(ntraj) ptfile = None wtefile = None if os.path.isfile("PARATEMP"): ptfile = open("PARATEMP", "r") optfile = open(outdir + "/PARATEMP", "w") if os.path.isfile("PARAWTE"): wtefile = open("PARAWTE", "r") owtefile = open(outdir + "/PARAWTE", "w") # now reads files one frame at a time, and re-direct output to the appropriate location for step in range(trimstep + 1): # reads one line from PARATEMP index file if not ptfile is None: line = ptfile.readline() optfile.write(line) if not wtefile is None: line = wtefile.readline() owtefile.write(line) try: for prop in lprop: for isys in range(nsys): sprop = prop[isys] if step % sprop["stride"] == 0: # property transfer iline = sprop["ifile"].readline() while iline[0] == "#": # fast forward if line is a comment prop[isys]["ofile"].write(iline) iline = sprop["ifile"].readline() prop[isys]["ofile"].write(iline) for traj in ltraj: for isys in range(nsys): straj = traj[isys] if step % straj["stride"] == 0: # property transfer # reads one frame from the input file ibuffer = [] if straj["format"] == "xyz": iline = straj["ifile"].readline() nat = int(iline) ibuffer.append(iline) ibuffer.append(straj["ifile"].readline()) for i in range(nat): ibuffer.append(straj["ifile"].readline()) traj[isys]["ofile"].write(''.join(ibuffer)) elif straj["format"] == "pdb": iline = straj["ifile"].readline() while (iline.strip() != "" and iline.strip() != "END"): ibuffer.append(iline) iline = straj["ifile"].readline() ibuffer.append(iline) traj[isys]["ofile"].write(''.join(ibuffer)) except EOFError: break
def main(inputfile, prefix="SRT_"): verbosity.level = "low" # opens & parses the input file ifile = open(inputfile, "r") xmlrestart = io_xml.xml_parse_file(ifile) # Parses the file. ifile.close() # ugly hack to remove ffplumed objects to avoid messing up with plumed output files newfields = [ f for f in xmlrestart.fields[0][1].fields if f[0] != "ffplumed" ] xmlrestart.fields[0][1].fields = newfields isimul = InputSimulation() isimul.parse(xmlrestart.fields[0][1]) simul = isimul.fetch() swapfile = "" if simul.smotion is None or (simul.smotion.mode != "remd" and simul.smotion.mode != "multi"): raise ValueError( "Simulation does not look like a parallel tempering one.") else: if simul.smotion.mode == "remd": swapfile = simul.smotion.swapfile else: for sm in simul.smotion.mlist: if sm.mode == "remd": if swapfile != "": raise ValueError( "I'm not equipped to deal with multiple REMD outputs, sorry" ) swapfile = sm.swapfile if swapfile == "": raise ValueError("Could not determine the REMD swapfile name. \ Sorry, you'll have to look carefully at your inputs.") # reconstructs the list of the property and trajectory files that have been output # and that should be re-ordered lprop = [] # list of property files ltraj = [] # list of trajectory files nsys = len(simul.syslist) for o in simul.outtemplate: o = deepcopy(o) # avoids overwriting the actual filename if simul.outtemplate.prefix != "": o.filename = simul.outtemplate.prefix + "." + o.filename if (type(o) is CheckpointOutput ): # properties and trajectories are output per system pass elif type(o) is PropertyOutput: nprop = [] isys = 0 for s in simul.syslist: # create multiple copies if s.prefix != "": filename = s.prefix + "_" + o.filename else: filename = o.filename ofilename = prefix + filename nprop.append({ "filename": filename, "ofilename": ofilename, "stride": o.stride, "ifile": open(filename, "r"), "ofile": open(ofilename, "w"), }) isys += 1 lprop.append(nprop) elif (type(o) is TrajectoryOutput ): # trajectories are more complex, as some have per-bead output if getkey(o.what) in [ "positions", "velocities", "forces", "extras", ]: # multiple beads nbeads = simul.syslist[0].beads.nbeads for b in range(nbeads): ntraj = [] isys = 0 # zero-padded bead number padb = ("%0" + str( int(1 + np.floor(np.log(nbeads) / np.log(10)))) + "d") % (b) for s in simul.syslist: if s.prefix != "": filename = s.prefix + "_" + o.filename else: filename = o.filename ofilename = prefix + filename if o.ibead < 0 or o.ibead == b: if getkey(o.what) == "extras": filename = filename + "_" + padb ofilename = ofilename + "_" + padb # Sets format of extras as None ntraj.append({ "filename": filename, "format": None, "ofilename": ofilename, "stride": o.stride, "ifile": open(filename, "r"), "ofile": open(ofilename, "w"), }) else: filename = filename + "_" + padb + "." + o.format ofilename = ofilename + "_" + padb + "." + o.format ntraj.append({ "filename": filename, "format": o.format, "ofilename": ofilename, "stride": o.stride, "ifile": open(filename, "r"), "ofile": open(ofilename, "w"), }) isys += 1 if ntraj != []: ltraj.append(ntraj) else: ntraj = [] isys = 0 for s in simul.syslist: # create multiple copies if s.prefix != "": filename = s.prefix + "_" + o.filename else: filename = o.filename filename = filename + "." + o.format ofilename = prefix + filename ntraj.append({ "filename": filename, "format": o.format, "ofilename": ofilename, "stride": o.stride, "ifile": open(filename, "r"), "ofile": open(ofilename, "w"), }) isys += 1 ltraj.append(ntraj) ptfile = open(simul.outtemplate.prefix + "." + swapfile, "r") # now reads files one frame at a time, # and re-direct output to the appropriate location line = ptfile.readline().split() irep = list(range(nsys)) # Could this be harmful? step = 0 while True: # reads one line from index file try: for prop in lprop: for isys in range(nsys): sprop = prop[isys] if step % sprop["stride"] == 0: # property transfer iline = sprop["ifile"].readline() if len(iline) == 0: raise EOFError # useful if line is blank while iline[ 0] == "#": # fast forward if line is a comment prop[irep[isys]]["ofile"].write(iline) iline = sprop["ifile"].readline() prop[irep[isys]]["ofile"].write(iline) for traj in ltraj: for isys in range(nsys): straj = traj[isys] if step % straj["stride"] == 0: # property transfer # reads one frame from the input file ibuffer = [] if straj["format"] is None: ibuffer.append(straj["ifile"].readline()) ibuffer.append(straj["ifile"].readline()) traj[irep[isys]]["ofile"].write("".join(ibuffer)) if straj["format"] == "xyz": iline = straj["ifile"].readline() nat = int(iline) ibuffer.append(iline) ibuffer.append(straj["ifile"].readline()) for i in range(nat): ibuffer.append(straj["ifile"].readline()) traj[irep[isys]]["ofile"].write("".join(ibuffer)) elif straj["format"] == "pdb": iline = straj["ifile"].readline() while iline.strip() != "" and iline.strip( ) != "END": ibuffer.append(iline) iline = straj["ifile"].readline() ibuffer.append(iline) traj[irep[isys]]["ofile"].write("".join(ibuffer)) except EOFError: break if len(line) > 0 and step == int(line[0]): irep = [int(i) for i in line[1:]] line = ptfile.readline() line = line.split() step += 1
def main(inputfile, prefix="PTW-", ttemp="300.0", skip="2000"): txtemp = ttemp ttemp = unit_to_internal("energy", "kelvin", float(ttemp)) skip = int(skip) # opens & parses the input file ifile = open(inputfile, "r") verbosity.level = "quiet" verbosity.lock = True xmlrestart = io_xml.xml_parse_file(ifile) # Parses the file. ifile.close() isimul = InputSimulation() isimul.parse(xmlrestart.fields[0][1]) verbosity.level = "quiet" banner() print( "# Printing out temperature re-weighing factors for a parallel tempering simulation" ) simul = isimul.fetch() if simul.mode != "paratemp": raise ValueError( "Simulation does not look like a parallel tempering one.") # reconstructs the list of the property and trajectory files that have been output # and that should be re-ordered lprop = [] # list of property files # ltraj = [] # list of trajectory files tlist = simul.paratemp.temp_list nsys = len(simul.syslist) for o in simul.outtemplate: if (type(o) is CheckpointOutput ): # properties and trajectories are output per system pass elif type(o) is PropertyOutput: nprop = [] isys = 0 for s in simul.syslist: # create multiple copies if s.prefix != "": filename = s.prefix + "_" + o.filename else: filename = o.filename ofilename = (prefix + (("%0" + str( int(1 + np.floor(np.log(len(simul.syslist)) / np.log(10)))) + "d") % (isys)) + "_" + o.filename) nprop.append({ "filename": filename, "ofilename": ofilename, "stride": o.stride, "ifile": open(filename, "r"), "ofile": None, }) isys += 1 lprop.append(nprop) ptfile = open("PARATEMP", "r") # these are variables used to compute the weighting factors tprops = [] vfields = [] refpots = [] vunits = [] nw = [] tw = [] tw2 = [] repot = re.compile(" ([0-9]*) *--> potential") reunit = re.compile("{(.*)}") # now reads files one frame at a time, and re-direct output to the appropriate location irep = np.zeros(nsys, int) while True: # reads one line from PARATEMP index file line = ptfile.readline() line = line.split() try: if len(line) == 0: raise EOFError step = int(line[0]) irep[:] = line[1:] wk = 0 for prop in lprop: for isys in range(nsys): sprop = prop[isys] if step % sprop["stride"] == 0: # property transfer iline = sprop["ifile"].readline() if len(iline) == 0: raise EOFError while iline[ 0] == "#": # fast forward if line is a comment # checks if we have one single file with potential energies rm = repot.search(iline) if not (rm is None) and not (prop in tprops): tprops.append(prop) for p in prop: p["ofile"] = open(p["ofilename"], "w") p["ofile"].write( "# column 1 --> ptlogweight: ln of re-weighing factor with target temperature %s K\n" % (txtemp)) vfields.append(int(rm.group(1)) - 1) refpots.append(np.zeros(nsys)) nw.append(np.zeros(nsys)) tw.append(np.zeros(nsys)) tw2.append(np.zeros(nsys)) rm = reunit.search(iline) if rm: vunits.append(rm.group(1)) else: vunits.append("atomic_unit") iline = sprop["ifile"].readline() if prop in tprops: # do temperature weighing pot = unit_to_internal( "energy", vunits[wk], float(iline.split()[vfields[wk]])) ir = irep[isys] if nw[wk][ir] == 0: refpots[wk][ ir] = pot # picks the first value as a reference potential to avoid insane absolute values of the logweight temp = tlist[ir] lw = (pot - refpots[wk][ir]) * (1 / temp - 1 / ttemp) if ( step > skip ): # computes trajectory weights avoiding the initial - possibly insane - values if nw[wk][ir] == 0: tw[wk][ir] = lw tw2[wk][ir] = lw else: tw[wk][ir] = logsumlog((tw[wk][ir], 1), (lw, 1))[0] tw2[wk][ir] = logsumlog((tw2[wk][ir], 1), (2 * lw, 1))[0] nw[wk][ir] += 1 prop[ir]["ofile"].write("%15.7e\n" % (lw)) if isys == nsys - 1: wk += 1 except EOFError: # print out trajectory weights based on PRSA 2011, assuming that observables and weights are weakly correlated wk = 0 fpw = open(prefix + "WEIGHTS", "w") fpw.write("# Global trajectory weights for temperature %s K\n" % (txtemp)) fpw.write( "# Please cite M. Ceriotti, G. A. Brain, O. Riordan, D.E. Manolopoulos, " + "The inefficiency of re-weighted sampling and the curse of system size in high-order path integration. " + "Proceedings of the Royal Society A, 468(2137), 2-17 (2011) \n" ) for prop in lprop: if prop in tprops: for ir in range(nsys): fpw.write("%s %15.7e \n" % ( prop[ir]["ofilename"], 1.0 / (np.exp(tw2[wk][ir] - 2 * tw[wk][ir]) * nw[wk][ir]), )) wk += 1 break
def get_A(path2iipi): """ Parses the i-PI input to read the relevant data files and returns the Helmholtz free energy obtained within the self consitent phonons (SCP) approximation. """ blockPrint() # Parses the i-PI input file. ifile = open(path2iipi, "r") xmlrestart = io_xml.xml_parse_file(ifile) ifile.close() # Initializes the simulation class. isimul = InputSimulation() isimul.parse(xmlrestart.fields[0][1]) simul = isimul.fetch() enablePrint() # Obtains various parameters and relevant filenames. prefix = simul.outtemplate.prefix + "." + simul.syslist[0].motion.prefix max_iter = simul.syslist[0].motion.max_iter batch_exp = simul.syslist[0].motion.batch_weight_exponent kbT = float(simul.syslist[0].ensemble.temp) beta = 1.0 / kbT # Checks for ASR type to skip # over zero modes. asr = simul.syslist[0].motion.asr if asr == "crystal": nz = 3 elif asr == "poly": nz = 6 iD_list = [] q_list = [] K_list = [] V_list = [] hw_list = [] x_list = [] v_list = [] for i in range(max_iter): try: # Imports the q, iD, x, f from the i^th SCP iteration. # "0" stands for the SCP trial Hamiltonian. # iD -> the covariance matrix of the canonical density in position representation. # q -> the position at minimum potential energy. # K -> the force constant matrix. # V -> the minimum potential energy. # hw -> the frequencies. # vH -> the ensemble average of the potential w.r.t the canonical density. # AH -> the free energy. iD_list.append(np.loadtxt(prefix + ".iD." + str(i))) q_list.append(np.loadtxt(prefix + ".q." + str(i))) K_list.append(np.loadtxt(prefix + ".K." + str(i))) V_list.append(np.loadtxt(prefix + ".V0." + str(i))) hw_list.append(np.loadtxt(prefix + ".w." + str(i))[nz:]) # x -> the samples drawn from the canonical density of the trial distribution. # v -> the potential enegry of the samples. x_list.append(np.loadtxt(prefix + ".x." + str(i))) v_list.append(np.loadtxt(prefix + ".v." + str(i))) except IOError: break print("# Finished Import") for i in range(max_iter): try: # Imports the q, iD, x, f from the i^th SCP iteration. iD0 = iD_list[i] q0 = q_list[i] K0 = K_list[i] V0 = V_list[i] hw0 = hw_list[i] betahw0 = beta * hw0 vH0 = np.sum(hw0 * np.cosh(betahw0 / 2.0) / np.sinh(betahw0 / 2.0) * 0.250) AH0 = np.sum(hw0 * 0.5 + kbT * np.log(1 - np.exp(-betahw0))) except IOError: break # Stores the potential /free energy of the initial harmonic trail Hamiltonian. if i == 0: A_harm = AH0 v_harm = vH0 V0_harm = V0 print(("%23s %23s %23s %23s %23s %23s %23s" % ( "# ITERATION", "A_SCP", "A_SCP-A_HARM", "ERROR", "E_SCP", "E_SCP-E_HARM", "ERROR", ))) # Initializes the average and the error in the difference between the physical # and the SCP potential. avg_dv = 0.0 err_dv = 0.0 norm = 0 # Inner loop over previous SCP steps for j in range(i + 1): try: # Imports the q, iD of the j^th trial Hamiltonian. # The idea is to reweight using samples drawn from the j <= i trial Hamiltonians. iD = iD_list[j] q = q_list[j] # x -> the samples drawn from the canonical density of the trial distribution. # v -> the potential enegry of the samples. x = x_list[j] v = v_list[j] except IOError: break # vh -> the harmonic component of the potential energy. # Note that the harmonic Hamiltonian is the i^th one # while the samples are drawn from the j^th one. vh = 0.5 * np.sum(np.dot(x - q0, K0.T) * (x - q0), axis=1) # Calculates the statistical weight of each sample. w = np.exp(-(0.50 * np.dot(iD0, (x - q0).T).T * (x - q0)).sum(axis=1) + (0.50 * np.dot(iD, (x - q).T).T * (x - q)).sum(axis=1)) V1 = np.sum(w) V2 = np.sum(w**2) # Calculates the average amd error (over the samples from the j^th batch of samples) # associated with the "anharmonic" component of the potential. avg_dv_j = np.nan_to_num(np.sum(w * (v - vh)) / V1) err_dv_j = np.nan_to_num( np.sum(w * (v - vh - avg_dv_j)**2) / (V1 - V2 / V1)) # Calculates the "batch" weight. c = np.nan_to_num(np.exp(-np.var(np.log(w))))**batch_exp # Accumulates the contribution to the average (and error) from the j^th batch. avg_dv += c * avg_dv_j err_dv += c**2 * err_dv_j norm += c avg_dv = np.nan_to_num(avg_dv / norm) err_dv = np.nan_to_num(err_dv / norm**2 / len(w)) # Calculates the SCP potential / free energy. A_scp = AH0 + avg_dv A_scp_err = np.sqrt(err_dv) E_scp = 2.0 * vH0 + avg_dv E_scp_err = np.sqrt(err_dv) # Calculates the SCP potential / free energy correction # w.r.t the initial trial Hamiltonian. A_scp_corr = A_scp - A_harm - V0_harm E_scp_corr = E_scp - 2.0 * v_harm - V0_harm print( ("%23d %23.8e %23.8e %23.8e %23.8e %23.8e %23.8e" % (i, A_scp, A_scp_corr, A_scp_err, E_scp, E_scp_corr, E_scp_err)))
def get_filesname(self, xml_path, olddir, newdir): """ The test results should be analyzed number by numbers. The idea is that the testing input should never change, then the files should be always the same. It would probably be better, anyway, to use the i-pi infrastructure to retrieve the right position of the data. In fact, this would work as a further testing. Args: olddir: The path used for the 'old_filename' in the returned dictionary. newdir: The path used for the 'new_filename' in the returned dictionary. Returns: lprop nprop """ # Avoid to print i-pi output devnull = open('/dev/null', 'w') oldstdout_fno = os.dup(sys.stdout.fileno()) os.dup2(devnull.fileno(), 1) # opens & parses the input file # get in the input file location so it can find other input files for initialization cwd = os.getcwd() iodir = os.path.dirname(os.path.realpath(xml_path)) os.chdir(iodir) # print "READING FILE FROM ", iodir # print " WHILE RUNNING IN ", cwd # print "I have changed directory to ", os.getcwd() ifile = open(xml_path, "r") xmlrestart = io_xml.xml_parse_file(ifile) # Parses the file. ifile.close() isimul = InputSimulation() isimul.parse(xmlrestart.fields[0][1]) simul = isimul.fetch() os.chdir(cwd) # reconstructs the list of the property and trajectory files lprop = [] # list of property files ltraj = [] # list of trajectory files for o in simul.outtemplate: # properties and trajectories are output per system if isinstance(o, CheckpointOutput): pass elif isinstance(o, PropertyOutput): nprop = [] isys = 0 for _ss in simul.syslist: # create multiple copies filename = _ss.prefix + o.filename nprop.append({"old_filename": os.path.join(olddir, filename), "new_filename": os.path.join(newdir, filename), "stride": o.stride, "properties": o.outlist, }) isys += 1 lprop.append(nprop) # trajectories are more complex, as some have per-bead output elif isinstance(o, TrajectoryOutput): if getkey(o.what) in ["positions", "velocities", "forces", "forces_sc", "extras"]: # multiple beads nbeads = simul.syslist[0].beads.nbeads for _bi in range(nbeads): ntraj = [] isys = 0 # zero-padded bead number padb = (("%0" + str(int(1 + np.floor(np.log(nbeads) / np.log(10)))) + "d") % (_bi)) for _ss in simul.syslist: if o.ibead < 0 or o.ibead == _bi: if getkey(o.what) == "extras": filename = _ss.prefix + o.filename + "_" + padb else: filename = _ss.prefix + o.filename + "_" + padb + \ "." + o.format ntraj.append({"old_filename": os.path.join(olddir, filename), "format": o.format, "new_filename": os.path.join(newdir, filename), "stride": o.stride, "what": o.what, }) isys += 1 if ntraj != []: ltraj.append(ntraj) else: ntraj = [] isys = 0 for _ss in simul.syslist: # create multiple copies filename = _ss.prefix + o.filename filename = filename + "." + o.format ntraj.append({"old_filename": os.path.join(olddir, filename), "new_filename": os.path.join(newdir, filename), "format": o.format, "stride": o.stride, }) isys += 1 ltraj.append(ntraj) os.dup2(oldstdout_fno, 1) return ltraj, lprop
def get_output_filenames(xml_path): """ Predicts ipi output filenames based on I-PI input file. Args: xml_path: absolute path to I-PI input file Returns: list of I-PI output files that will be generated running I-PI using this input TODO A more elegant solution. This function launches I-PI in the directory of the input file and dumps output to dev/null, which would make potential bugs hard to detect. """ # Avoid to print i-pi output devnull = open('/dev/null', 'w') oldstdout_fno = os.dup(sys.stdout.fileno()) os.dup2(devnull.fileno(), 1) xml_path = os.path.abspath(xml_path) os.chdir(os.path.dirname(xml_path)) # i-pi xml file parser ifile = open(xml_path, 'r') xmlrestart = io_xml.xml_parse_file(ifile) ifile.close() isimul = InputSimulation() isimul.parse(xmlrestart.fields[0][1]) simul = isimul.fetch() # reconstructs the list of the property and trajectory files lprop = [] # list of property files ltraj = [] # list of trajectory files for o in simul.outtemplate: # properties and trajectories are output per system if isinstance(o, CheckpointOutput): pass elif isinstance(o, PropertyOutput): for _ss in simul.syslist: # create multiple copies filename = o.filename if _ss.prefix != "": filename = _ss.prefix + "_" + filename lprop.append(filename) # trajectories are more complex, as some have per-bead output elif isinstance(o, TrajectoryOutput): if getkey(o.what) in [ 'positions', 'velocities', 'forces', 'forces_sc', 'extras' ]: # multiple beads nbeads = simul.syslist[0].beads.nbeads for _bi in range(nbeads): # zero-padded bead number padb = (('%0' + str( int(1 + np.floor(np.log(nbeads) / np.log(10)))) + 'd') % (_bi)) for _ss in simul.syslist: if (o.ibead < 0 and ((_bi % (-o.ibead) == 0))) or o.ibead == _bi: filename = o.filename if _ss.prefix != "": filename = _ss.prefix + "_" + filename if getkey(o.what) == 'extras': filename += "_" + padb else: filename += "_" + padb + "." + o.format ltraj.append(filename) else: for _ss in simul.syslist: # create multiple copies filename = o.filename if _ss.prefix != "": filename = _ss.prefix + "_" + filename filename += "." + o.format ltraj.append(filename) os.dup2(oldstdout_fno, 1) return (ltraj + lprop)
def main(inputfile, prefix="PTW-", ttemp="300.0", skip="2000"): txtemp = ttemp ttemp = unit_to_internal("energy", "kelvin", float(ttemp)) skip = int(skip) # opens & parses the input file ifile = open(inputfile, "r") verbosity.level = "quiet" verbosity.lock = True xmlrestart = io_xml.xml_parse_file(ifile) # Parses the file. ifile.close() isimul = InputSimulation() isimul.parse(xmlrestart.fields[0][1]) verbosity.level = "quiet" banner() print "# Printing out temperature re-weighing factors for a parallel tempering simulation" simul = isimul.fetch() if simul.mode != "paratemp": raise ValueError("Simulation does not look like a parallel tempering one.") # reconstructs the list of the property and trajectory files that have been output # and that should be re-ordered lprop = [] # list of property files ltraj = [] # list of trajectory files tlist = simul.paratemp.temp_list nsys = len(simul.syslist) for o in simul.outtemplate: if type(o) is CheckpointOutput: # properties and trajectories are output per system pass elif type(o) is PropertyOutput: nprop = [] isys = 0 for s in simul.syslist: # create multiple copies if s.prefix != "": filename = s.prefix + "_" + o.filename else: filename = o.filename ofilename = prefix + (("%0" + str(int(1 + np.floor(np.log(len(simul.syslist)) / np.log(10)))) + "d") % (isys)) + "_" + o.filename nprop.append({"filename": filename, "ofilename": ofilename, "stride": o.stride, "ifile": open(filename, "r"), "ofile": None }) isys += 1 lprop.append(nprop) ptfile = open("PARATEMP", "r") # these are variables used to compute the weighting factors tprops = [] vfields = [] refpots = [] vunits = [] nw = [] tw = [] tw2 = [] repot = re.compile(' ([0-9]*) *--> potential') reunit = re.compile('{(.*)}') # now reads files one frame at a time, and re-direct output to the appropriate location irep = np.zeros(nsys, int) while True: # reads one line from PARATEMP index file line = ptfile.readline() line = line.split() try: if len(line) == 0: raise EOFError step = int(line[0]) irep[:] = line[1:] wk = 0 for prop in lprop: for isys in range(nsys): sprop = prop[isys] if step % sprop["stride"] == 0: # property transfer iline = sprop["ifile"].readline() if len(iline) == 0: raise EOFError while iline[0] == "#": # fast forward if line is a comment # checks if we have one single file with potential energies rm = repot.search(iline) if not (rm is None) and not (prop in tprops): tprops.append(prop) for p in prop: p["ofile"] = open(p["ofilename"], "w") p["ofile"].write("# column 1 --> ptlogweight: ln of re-weighing factor with target temperature %s K\n" % (txtemp)) vfields.append(int(rm.group(1)) - 1) refpots.append(np.zeros(nsys)) nw.append(np.zeros(nsys)) tw.append(np.zeros(nsys)) tw2.append(np.zeros(nsys)) rm = reunit.search(iline) if rm: vunits.append(rm.group(1)) else: vunits.append("atomic_unit") iline = sprop["ifile"].readline() if prop in tprops: # do temperature weighing pot = unit_to_internal("energy", vunits[wk], float(iline.split()[vfields[wk]])) ir = irep[isys] if (nw[wk][ir] == 0): refpots[wk][ir] = pot # picks the first value as a reference potential to avoid insane absolute values of the logweight temp = tlist[ir] lw = (pot - refpots[wk][ir]) * (1 / temp - 1 / ttemp) if step > skip: # computes trajectory weights avoiding the initial - possibly insane - values if nw[wk][ir] == 0: tw[wk][ir] = lw tw2[wk][ir] = lw else: tw[wk][ir] = logsumlog((tw[wk][ir], 1), (lw, 1))[0] tw2[wk][ir] = logsumlog((tw2[wk][ir], 1), (2 * lw, 1))[0] nw[wk][ir] += 1 prop[ir]["ofile"].write("%15.7e\n" % (lw)) if isys == nsys - 1: wk += 1 except EOFError: # print out trajectory weights based on PRSA 2011, assuming that observables and weights are weakly correlated wk = 0 fpw = open(prefix + "WEIGHTS", "w") fpw.write("# Global trajectory weights for temperature %s K\n" % (txtemp)) fpw.write("# Please cite M. Ceriotti, G. A. Brain, O. Riordan, D.E. Manolopoulos, " + "The inefficiency of re-weighted sampling and the curse of system size in high-order path integration. " + "Proceedings of the Royal Society A, 468(2137), 2-17 (2011) \n") for prop in lprop: if prop in tprops: for ir in range(nsys): fpw.write("%s %15.7e \n" % (prop[ir]["ofilename"], 1.0 / (np.exp(tw2[wk][ir] - 2 * tw[wk][ir]) * nw[wk][ir]))) wk += 1 break
def gleacf(path2ixml, path2iA, path2iC, path2ifacf, oprefix, action, nrows, stride, tscale, dparam): if path2ixml != None and path2iA != None: raise Exception( "The drift and covariance matrices have to be provided either through the i-pi xml file or manually. Can not use both forms of input simultaneously. " ) elif path2ixml != None and path2iA == None: # opens & parses the i-pi input file ifile = open(path2ixml, "r") xmlrestart = io_xml.xml_parse_file(ifile) ifile.close() isimul = InputSimulation() isimul.parse(xmlrestart.fields[0][1]) simul = isimul.fetch() # parses the drift and diffusion matrices of the GLE thermostat. ttype = str(type(simul.syslist[0].motion.thermostat).__name__) P = float(simul.syslist[0].init.nbeads) kbT = float(simul.syslist[0].ensemble.temp) * P simul.syslist[0].motion.thermostat.temp = kbT if (ttype == "ThermoGLE"): Ap = simul.syslist[0].motion.thermostat.A * tscale Cp = simul.syslist[0].motion.thermostat.C / kbT Dp = np.dot(Ap, Cp) + np.dot(Cp, Ap.T) elif (ttype == "ThermoNMGLE"): Ap = simul.syslist[0].motion.thermostat.A[0] * tscale Cp = simul.syslist[0].motion.thermostat.C[0] / kbT Dp = np.dot(Ap, Cp) + np.dot(Cp, Ap.T) elif (ttype == "ThermoLangevin" or ttype == "ThermoPILE_L"): Ap = np.asarray([1.0 / simul.syslist[0].motion.thermostat.tau ]).reshape((1, 1)) * tscale Cp = np.asarray([1.0]).reshape((1, 1)) Dp = np.dot(Ap, Cp) + np.dot(Cp, Ap.T) else: raise Exception( "GLE thermostat not found. The allowed thermostats are gle, nm_gle, langevin and pile_l." ) elif path2ixml == None and path2iA != None: Ap = np.loadtxt(path2iA, dtype=float, ndmin=2) * tscale if path2iC != None: Cp = np.loadtxt(path2iC) else: Cp = np.eye(len(Ap)) Dp = np.dot(Ap, Cp) + np.dot(Cp, Ap.T) else: raise Exception( "The drift and covariance matrixes have to be provided either through the i-pi xml file or manually." ) # imports the facf function. ifacf = input_facf(path2ifacf, nrows, stride) ix = ifacf[:, 0] iy = ifacf[:, 1] # computes the facf kernel print "# computing the kernel." ker = gleKernel(ix, Ap, Dp) # (de-)convolutes the spectrum if (action == "conv"): print "# printing the output spectrum." output_facf((ix, np.dot(iy, ker.T)), oprefix, input_facf(path2ifacf, nrows, 1)) elif (action == "deconv"): print "# deconvoluting the input spectrum." oy = ISRA(ix, ker, iy, dparam, oprefix)
def main(inputfile, prefix="PT"): verbosity.level = "low" # opens & parses the input file ifile = open(inputfile, "r") xmlrestart = io_xml.xml_parse_file(ifile) # Parses the file. ifile.close() isimul = InputSimulation() isimul.parse(xmlrestart.fields[0][1]) simul = isimul.fetch() if simul.smotion.mode != "remd": raise ValueError("Simulation does not look like a parallel tempering one.") # reconstructs the list of the property and trajectory files that have been output # and that should be re-ordered lprop = [] # list of property files ltraj = [] # list of trajectory files nsys = len(simul.syslist) for o in simul.outtemplate: if type(o) is CheckpointOutput: # properties and trajectories are output per system pass elif type(o) is PropertyOutput: nprop = [] isys = 0 for s in simul.syslist: # create multiple copies if s.prefix != "": filename = s.prefix + "_" + o.filename else: filename = o.filename ofilename = prefix + str(isys) + "_" + o.filename nprop.append({"filename": filename, "ofilename": ofilename, "stride": o.stride, "ifile": open(filename, "r"), "ofile": open(ofilename, "w") }) isys += 1 lprop.append(nprop) elif type(o) is TrajectoryOutput: # trajectories are more complex, as some have per-bead output if getkey(o.what) in ["positions", "velocities", "forces", "extras"]: # multiple beads nbeads = simul.syslist[0].beads.nbeads for b in range(nbeads): ntraj = [] isys = 0 # zero-padded bead number padb = (("%0" + str(int(1 + np.floor(np.log(nbeads) / np.log(10)))) + "d") % (b)) for s in simul.syslist: if s.prefix != "": filename = s.prefix + "_" + o.filename else: filename = o.filename ofilename = prefix + str(isys) + "_" + o.filename if (o.ibead < 0 or o.ibead == b): if getkey(o.what) == "extras": filename = filename + "_" + padb ofilename = ofilename + "_" + padb else: filename = filename + "_" + padb + "." + o.format ofilename = ofilename + "_" + padb + "." + o.format ntraj.append({"filename": filename, "format": o.format, "ofilename": ofilename, "stride": o.stride, "ifile": open(filename, "r"), "ofile": open(ofilename, "w") }) isys += 1 if ntraj != []: ltraj.append(ntraj) else: ntraj = [] isys = 0 for s in simul.syslist: # create multiple copies if s.prefix != "": filename = s.prefix + "_" + o.filename else: filename = o.filename filename = filename + "." + o.format ofilename = prefix + str(isys) + "_" + o.filename + "." + o.format ntraj.append({"filename": filename, "format": o.format, "ofilename": ofilename, "stride": o.stride, "ifile": open(filename, "r"), "ofile": open(ofilename, "w") }) isys += 1 ltraj.append(ntraj) ptfile = open("PARATEMP", "r") # now reads files one frame at a time, and re-direct output to the appropriate location line = ptfile.readline().split() irep = range(nsys) # Could this be harmful? step = 0 while True: # reads one line from PARATEMP index file try: for prop in lprop: for isys in range(nsys): sprop = prop[isys] if step % sprop["stride"] == 0: # property transfer iline = sprop["ifile"].readline() if len(iline) == 0: raise EOFError # useful if line is blank while iline[0] == "#": # fast forward if line is a comment prop[irep[isys]]["ofile"].write(iline) iline = sprop["ifile"].readline() prop[irep[isys]]["ofile"].write(iline) for traj in ltraj: for isys in range(nsys): straj = traj[isys] if step % straj["stride"] == 0: # property transfer # reads one frame from the input file ibuffer = [] if straj["format"] == "xyz": iline = straj["ifile"].readline() nat = int(iline) ibuffer.append(iline) ibuffer.append(straj["ifile"].readline()) for i in range(nat): ibuffer.append(straj["ifile"].readline()) traj[irep[isys]]["ofile"].write(''.join(ibuffer)) elif straj["format"] == "pdb": iline = straj["ifile"].readline() while (iline.strip() != "" and iline.strip() != "END"): ibuffer.append(iline) iline = straj["ifile"].readline() ibuffer.append(iline) traj[irep[isys]]["ofile"].write(''.join(ibuffer)) except EOFError: break if len(line) > 0 and step == int(line[0]): irep = [int(i) for i in line[1:]] line = ptfile.readline() line = line.split() step += 1
def get_output_filenames(xml_path): """ Predicts ipi output filenames based on I-PI input file. Args: xml_path: absolute path to I-PI input file Returns: list of I-PI output files that will be generated running I-PI using this input TODO A more elegant solution. This function launches I-PI in the directory of the input file and dumps output to dev/null, which would make potential bugs hard to detect. """ # Avoid to print i-pi output devnull = open('/dev/null', 'w') oldstdout_fno = os.dup(sys.stdout.fileno()) os.dup2(devnull.fileno(), 1) xml_path = os.path.abspath(xml_path) os.chdir(os.path.dirname(xml_path)) # i-pi xml file parser ifile = open(xml_path, 'r') xmlrestart = io_xml.xml_parse_file(ifile) ifile.close() isimul = InputSimulation() isimul.parse(xmlrestart.fields[0][1]) simul = isimul.fetch() # reconstructs the list of the property and trajectory files lprop = [] # list of property files ltraj = [] # list of trajectory files for o in simul.outtemplate: # properties and trajectories are output per system if isinstance(o, CheckpointOutput): pass elif isinstance(o, PropertyOutput): for _ss in simul.syslist: # create multiple copies filename = o.filename if _ss.prefix != "": filename = _ss.prefix + "_" + filename lprop.append(filename) # trajectories are more complex, as some have per-bead output elif isinstance(o, TrajectoryOutput): if getkey(o.what) in ['positions', 'velocities', 'forces', 'forces_sc', 'extras']: # multiple beads nbeads = simul.syslist[0].beads.nbeads for _bi in range(nbeads): # zero-padded bead number padb = (('%0' + str(int(1 + np.floor(np.log(nbeads) / np.log(10)))) + 'd') % (_bi)) for _ss in simul.syslist: if (o.ibead < 0 and ((_bi % (-o.ibead) == 0))) or o.ibead == _bi: filename = o.filename if _ss.prefix != "": filename = _ss.prefix + "_" + filename if getkey(o.what) == 'extras': filename += "_" + padb else: filename += "_" + padb + "." + o.format ltraj.append(filename) else: for _ss in simul.syslist: # create multiple copies filename = o.filename if _ss.prefix != "": filename = _ss.prefix + "_" + filename filename += "." + o.format ltraj.append(filename) os.dup2(oldstdout_fno, 1) return (ltraj + lprop)
def get_np(path2iipi, bsize=20000, nskip=300, si=15.0, sf=-15.0, ns=10000): # opens & parses the i-pi input file ifile = open(path2iipi, "r") xmlrestart = io_xml.xml_parse_file(ifile) ifile.close() isimul = InputSimulation() isimul.parse(xmlrestart.fields[0][1]) simul = isimul.fetch() # parses the temperature, the number of beads, the number of atoms, the number of target species and their masses. T = float(simul.syslist[0].ensemble.temp) P = simul.syslist[0].beads.nbeads open_paths = simul.syslist[0].nm.open_paths[-1] m = simul.syslist[0].beads.m[open_paths] # initialises the data files. dq = np.zeros((bsize, 3), float) dqxgrid = np.linspace(si, sf, ns) dqygrid = np.linspace(si, sf, ns) dqzgrid = np.linspace(si, sf, ns) nplistx = [] nplisty = [] nplistz = [] # Read the end to end distances from file data_path = ( "/home/cuzzocre/source/i-pi-mc/examples/lammps/ice-nst/P32-T269/endtoend.data" ) delta = np.loadtxt(data_path) step = np.shape(delta)[0] n_block = int(step / bsize) for x in range(n_block): dq = delta[x * bsize:(x + 1) * bsize] hx = histo(np.concatenate((dq.T[0], -dq.T[0])), dqxgrid, kernel, 0, np.sqrt(T * P * m)) hy = histo(np.concatenate((dq.T[1], -dq.T[1])), dqygrid, kernel, 0, np.sqrt(T * P * m)) hz = histo(np.concatenate((dq.T[2], -dq.T[2])), dqzgrid, kernel, 0, np.sqrt(T * P * m)) # Defines the grid for momentum. pxi = -np.pi / (dqxgrid[1] - dqxgrid[0]) pxf = +np.pi / (dqxgrid[1] - dqxgrid[0]) pxstep = 2 * np.pi / np.abs(dqxgrid[-1] - dqxgrid[0]) pxgrid = np.linspace(pxi, pxf, ns) pyi = -np.pi / (dqygrid[1] - dqygrid[0]) pyf = +np.pi / (dqygrid[1] - dqygrid[0]) pystep = 2 * np.pi / np.abs(dqygrid[-1] - dqygrid[0]) pygrid = np.linspace(pyi, pyf, ns) pzi = -np.pi / (dqzgrid[1] - dqzgrid[0]) pzf = +np.pi / (dqzgrid[1] - dqzgrid[0]) pzstep = 2 * np.pi / np.abs(dqzgrid[-1] - dqzgrid[0]) pzgrid = np.linspace(pzi, pzf, ns) # Computes the Fourier transform of the end to end vector. npx = np.abs(np.fft.fftshift(np.fft.fft(hx))) npy = np.abs(np.fft.fftshift(np.fft.fft(hy))) npz = np.abs(np.fft.fftshift(np.fft.fft(hz))) nplistx.append(npx) nplisty.append(npy) nplistz.append(npz) avgnpx = np.mean(np.asarray(nplistx), axis=0) avgnpy = np.mean(np.asarray(nplisty), axis=0) avgnpz = np.mean(np.asarray(nplistz), axis=0) normx = np.sum(avgnpx) normy = np.sum(avgnpy) normz = np.sum(avgnpz) errnpx = np.std(np.asarray(nplistx), axis=0) / np.sqrt(n_block) / normx avgnpx = avgnpx / normx errnpy = np.std(np.asarray(nplisty), axis=0) / np.sqrt(n_block) / normy avgnpy = avgnpy / normy errnpz = np.std(np.asarray(nplistz), axis=0) / np.sqrt(n_block) / normz avgnpz = avgnpz / normz avgpsqnpx = pxgrid**2 * avgnpx / pxstep errpsqnpx = pxgrid**2 * errnpx / pxstep avgpsqnpy = pygrid**2 * avgnpy / pystep errpsqnpy = pygrid**2 * errnpy / pystep avgpsqnpz = pzgrid**2 * avgnpz / pzstep errpsqnpz = pzgrid**2 * errnpz / pzstep np.savetxt("np.data", np.c_[pxgrid, avgnpx, errnpx, avgnpy, errnpy, avgnpz, errnpz]) np.savetxt( "psq-np.data", np.c_[pxgrid, avgpsqnpx, errpsqnpx, avgpsqnpy, errpsqnpy, avgpsqnpz, errpsqnpz], ) psqmedx = 0.0 psqmed2x = 0.0 psqmedy = 0.0 psqmed2y = 0.0 psqmedz = 0.0 psqmed2z = 0.0 for i in range(n_block): psqmedx = psqmedx + np.dot(pxgrid**2, np.asarray(nplistx)[i, :]) / normx psqmed2x = (psqmed2x + (np.dot(pxgrid**2, np.asarray(nplistx)[i, :]) / normx)**2) psqmedy = psqmedy + np.dot(pygrid**2, np.asarray(nplisty)[i, :]) / normy psqmed2y = (psqmed2y + (np.dot(pygrid**2, np.asarray(nplisty)[i, :]) / normy)**2) psqmedz = psqmedz + np.dot(pzgrid**2, np.asarray(nplistz)[i, :]) / normz psqmed2z = (psqmed2z + (np.dot(pzgrid**2, np.asarray(nplistz)[i, :]) / normz)**2) print("number of blocks", n_block) print( "av_px^2", psqmedx / n_block, "sigmax", np.sqrt((psqmed2x / n_block) - (psqmedx / n_block)**2) / np.sqrt(n_block), ) print( "av_py^2", psqmedy / n_block, "sigmay", np.sqrt((psqmed2y / n_block) - (psqmedy / n_block)**2) / np.sqrt(n_block), ) print( "av_pz^2", psqmedz / n_block, "sigmaz", np.sqrt((psqmed2z / n_block) - (psqmedz / n_block)**2) / np.sqrt(n_block), )
def main(inputfile, outdir="trim"): # opens & parses the input file ifile = open(inputfile, "r") xmlrestart = io_xml.xml_parse_file(ifile) # Parses the file. ifile.close() isimul = InputSimulation() isimul.parse(xmlrestart.fields[0][1]) simul = isimul.fetch() trimstep = isimul.step.fetch() os.makedirs(outdir) # reconstructs the list of the property and trajectory files that have been output # and that should be re-ordered lprop = [] # list of property files ltraj = [] # list of trajectory files nsys = len(simul.syslist) for o in simul.outtemplate: o = deepcopy(o) # avoids overwriting the actual filename if simul.outtemplate.prefix != "": o.filename = simul.outtemplate.prefix + "." + o.filename if type(o) is CheckpointOutput: # properties and trajectories are output per system pass elif type(o) is PropertyOutput: nprop = [] isys = 0 for s in simul.syslist: # create multiple copies if s.prefix != "": filename = s.prefix + "_" + o.filename else: filename = o.filename ofilename = outdir + "/" + filename nprop.append({"filename": filename, "ofilename": ofilename, "stride": o.stride, "ifile": open(filename, "r"), "ofile": open(ofilename, "w") }) isys += 1 lprop.append(nprop) elif type(o) is TrajectoryOutput: # trajectories are more complex, as some have per-bead output if getkey(o.what) in ["positions", "velocities", "forces", "extras"]: # multiple beads nbeads = simul.syslist[0].beads.nbeads for b in range(nbeads): ntraj = [] isys = 0 # zero-padded bead number padb = (("%0" + str(int(1 + np.floor(np.log(nbeads) / np.log(10)))) + "d") % (b)) for s in simul.syslist: if s.prefix != "": filename = s.prefix + "_" + o.filename else: filename = o.filename ofilename = outdir + "/" + filename if (o.ibead < 0 or o.ibead == b): if getkey(o.what) == "extras": filename = filename + "_" + padb ofilename = ofilename + "_" + padb else: filename = filename + "_" + padb + "." + o.format ofilename = ofilename + "_" + padb + "." + o.format ntraj.append({"filename": filename, "format": o.format, "ofilename": ofilename, "stride": o.stride, "ifile": open(filename, "r"), "ofile": open(ofilename, "w") }) isys += 1 if ntraj != []: ltraj.append(ntraj) else: ntraj = [] isys = 0 for s in simul.syslist: # create multiple copies if s.prefix != "": filename = s.prefix + "_" + o.filename else: filename = o.filename filename = filename + "." + o.format ofilename = outdir + "/" + filename ntraj.append({"filename": filename, "format": o.format, "ofilename": ofilename, "stride": o.stride, "ifile": open(filename, "r"), "ofile": open(ofilename, "w") }) isys += 1 ltraj.append(ntraj) ptfile = None wtefile = None if os.path.isfile("PARATEMP"): ptfile = open("PARATEMP", "r") optfile = open(outdir + "/PARATEMP", "w") if os.path.isfile("PARAWTE"): wtefile = open("PARAWTE", "r") owtefile = open(outdir + "/PARAWTE", "w") # now reads files one frame at a time, and re-direct output to the appropriate location for step in range(trimstep + 1): # reads one line from PARATEMP index file if not ptfile is None: line = ptfile.readline() optfile.write(line) if not wtefile is None: line = wtefile.readline() owtefile.write(line) try: for prop in lprop: for isys in range(nsys): sprop = prop[isys] if step % sprop["stride"] == 0: # property transfer iline = sprop["ifile"].readline() while iline[0] == "#": # fast forward if line is a comment prop[isys]["ofile"].write(iline) iline = sprop["ifile"].readline() prop[isys]["ofile"].write(iline) for traj in ltraj: for isys in range(nsys): straj = traj[isys] if step % straj["stride"] == 0: # property transfer # reads one frame from the input file ibuffer = [] if straj["format"] == "xyz": iline = straj["ifile"].readline() nat = int(iline) ibuffer.append(iline) ibuffer.append(straj["ifile"].readline()) for i in range(nat): ibuffer.append(straj["ifile"].readline()) traj[isys]["ofile"].write(''.join(ibuffer)) elif straj["format"] == "pdb": iline = straj["ifile"].readline() while (iline.strip() != "" and iline.strip() != "END"): ibuffer.append(iline) iline = straj["ifile"].readline() ibuffer.append(iline) traj[isys]["ofile"].write(''.join(ibuffer)) except EOFError: break
def gleacf(path2ixml, path2iA, path2iC, path2ifacf, oprefix, action, nrows, stride, tscale, dparam): if path2ixml != None and path2iA != None: raise Exception("The drift and covariance matrices have to be provided either through the i-pi xml file or manually. Can not use both forms of input simultaneously. ") elif path2ixml != None and path2iA == None: # opens & parses the i-pi input file ifile = open(path2ixml, "r") xmlrestart = io_xml.xml_parse_file(ifile) ifile.close() isimul = InputSimulation() isimul.parse(xmlrestart.fields[0][1]) simul = isimul.fetch() # parses the drift and diffusion matrices of the GLE thermostat. ttype = str(type(simul.syslist[0].motion.thermostat).__name__) P = float(simul.syslist[0].init.nbeads) kbT = float(simul.syslist[0].ensemble.temp) * P simul.syslist[0].motion.thermostat.temp = kbT if(ttype == "ThermoGLE"): Ap = simul.syslist[0].motion.thermostat.A * tscale Cp = simul.syslist[0].motion.thermostat.C / kbT Dp = np.dot(Ap, Cp) + np.dot(Cp, Ap.T) elif(ttype == "ThermoNMGLE"): Ap = simul.syslist[0].motion.thermostat.A[0] * tscale Cp = simul.syslist[0].motion.thermostat.C[0] / kbT Dp = np.dot(Ap, Cp) + np.dot(Cp, Ap.T) elif(ttype == "ThermoLangevin" or ttype == "ThermoPILE_L"): Ap = np.asarray([1.0 / simul.syslist[0].motion.thermostat.tau]).reshape((1, 1)) * tscale Cp = np.asarray([1.0]).reshape((1, 1)) Dp = np.dot(Ap, Cp) + np.dot(Cp, Ap.T) else: raise Exception("GLE thermostat not found. The allowed thermostats are gle, nm_gle, langevin and pile_l.") elif path2ixml == None and path2iA != None: Ap = np.loadtxt(path2iA, dtype=float, ndmin=2) * tscale if path2iC != None: Cp = np.loadtxt(path2iC) else: Cp = np.eye(len(Ap)) Dp = np.dot(Ap, Cp) + np.dot(Cp, Ap.T) else: raise Exception("The drift and covariance matrixes have to be provided either through the i-pi xml file or manually.") # imports the facf function. ifacf = input_facf(path2ifacf, nrows, stride) ix = ifacf[:, 0] iy = ifacf[:, 1] # computes the facf kernel print "# computing the kernel." ker = gleKernel(ix, Ap, Dp) # (de-)convolutes the spectrum if(action == "conv"): print "# printing the output spectrum." output_facf((ix, np.dot(iy, ker.T)), oprefix, input_facf(path2ifacf, nrows, 1)) elif(action == "deconv"): print "# deconvoluting the input spectrum." oy = ISRA(ix, ker, iy, dparam, oprefix)
def get_np(path2iipi, bsize=20000, nskip=300, si=15.0, sf=-15.0, ns=10000): # opens & parses the i-pi input file ifile = open(path2iipi, "r") xmlrestart = io_xml.xml_parse_file(ifile) ifile.close() isimul = InputSimulation() isimul.parse(xmlrestart.fields[0][1]) simul = isimul.fetch() # parses the temperature, the number of beads, the number of atoms, the number of target species and their masses. T = float(simul.syslist[0].ensemble.temp) P = simul.syslist[0].beads.nbeads natoms = simul.syslist[0].beads.natoms open_paths = simul.syslist[0].nm.open_paths[-1] m = simul.syslist[0].beads.m[open_paths] # initialises the data files. data_1 = np.zeros((bsize, 3), float) data_2 = np.zeros((bsize, 3), float) dq = np.zeros((bsize, 3), float) dqxgrid = np.linspace(si, sf, ns) dqygrid = np.linspace(si, sf, ns) dqzgrid = np.linspace(si, sf, ns) nplistx = [] nplisty = [] nplistz = [] # Read the end to end distances from file data_path = '/home/cuzzocre/source/i-pi-mc/examples/lammps/ice-nst/P32-T269/endtoend.data' delta = np.loadtxt(data_path) step = np.shape(delta)[0] n_block = int(step / bsize) for x in xrange(n_block): dq = delta[x * bsize: (x + 1) * bsize] hx = histo(np.concatenate((dq.T[0], -dq.T[0])), dqxgrid, kernel, 0, np.sqrt(T * P * m)) hy = histo(np.concatenate((dq.T[1], -dq.T[1])), dqygrid, kernel, 0, np.sqrt(T * P * m)) hz = histo(np.concatenate((dq.T[2], -dq.T[2])), dqzgrid, kernel, 0, np.sqrt(T * P * m)) # Defines the grid for momentum. pxi = -np.pi / (dqxgrid[1] - dqxgrid[0]) pxf = +np.pi / (dqxgrid[1] - dqxgrid[0]) pxstep = 2 * np.pi / np.abs(dqxgrid[-1] - dqxgrid[0]) pxgrid = np.linspace(pxi, pxf, ns) pyi = -np.pi / (dqygrid[1] - dqygrid[0]) pyf = +np.pi / (dqygrid[1] - dqygrid[0]) pystep = 2 * np.pi / np.abs(dqygrid[-1] - dqygrid[0]) pygrid = np.linspace(pyi, pyf, ns) pzi = -np.pi / (dqzgrid[1] - dqzgrid[0]) pzf = +np.pi / (dqzgrid[1] - dqzgrid[0]) pzstep = 2 * np.pi / np.abs(dqzgrid[-1] - dqzgrid[0]) pzgrid = np.linspace(pzi, pzf, ns) # Computes the Fourier transform of the end to end vector. npx = np.abs(np.fft.fftshift(np.fft.fft(hx))) npy = np.abs(np.fft.fftshift(np.fft.fft(hy))) npz = np.abs(np.fft.fftshift(np.fft.fft(hz))) nplistx.append(npx) nplisty.append(npy) nplistz.append(npz) avgnpx = np.mean(np.asarray(nplistx), axis=0) avgnpy = np.mean(np.asarray(nplisty), axis=0) avgnpz = np.mean(np.asarray(nplistz), axis=0) normx = np.sum(avgnpx) normy = np.sum(avgnpy) normz = np.sum(avgnpz) errnpx = np.std(np.asarray(nplistx), axis=0) / np.sqrt(n_block) / normx avgnpx = avgnpx / normx errnpy = np.std(np.asarray(nplisty), axis=0) / np.sqrt(n_block) / normy avgnpy = avgnpy / normy errnpz = np.std(np.asarray(nplistz), axis=0) / np.sqrt(n_block) / normz avgnpz = avgnpz / normz avgpsqnpx = pxgrid**2 * avgnpx / pxstep errpsqnpx = pxgrid**2 * errnpx / pxstep avgpsqnpy = pygrid**2 * avgnpy / pystep errpsqnpy = pygrid**2 * errnpy / pystep avgpsqnpz = pzgrid**2 * avgnpz / pzstep errpsqnpz = pzgrid**2 * errnpz / pzstep np.savetxt("np.data", np.c_[pxgrid, avgnpx, errnpx, avgnpy, errnpy, avgnpz, errnpz]) np.savetxt("psq-np.data", np.c_[pxgrid, avgpsqnpx, errpsqnpx, avgpsqnpy, errpsqnpy, avgpsqnpz, errpsqnpz]) psqmedx = 0. psqmed2x = 0. psqmedy = 0. psqmed2y = 0. psqmedz = 0. psqmed2z = 0. for i in range(n_block): psqmedx = psqmedx + np.dot(pxgrid**2, np.asarray(nplistx)[i, :]) / normx psqmed2x = psqmed2x + (np.dot(pxgrid**2, np.asarray(nplistx)[i, :]) / normx)**2 psqmedy = psqmedy + np.dot(pygrid**2, np.asarray(nplisty)[i, :]) / normy psqmed2y = psqmed2y + (np.dot(pygrid**2, np.asarray(nplisty)[i, :]) / normy)**2 psqmedz = psqmedz + np.dot(pzgrid**2, np.asarray(nplistz)[i, :]) / normz psqmed2z = psqmed2z + (np.dot(pzgrid**2, np.asarray(nplistz)[i, :]) / normz)**2 print 'number of blocks', n_block print 'av_px^2', psqmedx / n_block, 'sigmax', np.sqrt((psqmed2x / n_block) - (psqmedx / n_block)**2) / np.sqrt(n_block) print 'av_py^2', psqmedy / n_block, 'sigmay', np.sqrt((psqmed2y / n_block) - (psqmedy / n_block)**2) / np.sqrt(n_block) print 'av_pz^2', psqmedz / n_block, 'sigmaz', np.sqrt((psqmed2z / n_block) - (psqmedz / n_block)**2) / np.sqrt(n_block)