Skip to content
Snippets Groups Projects
Commit 532c8ed1 authored by Germain Clavier's avatar Germain Clavier
Browse files

Newest version of lmp_ave_post as I use them on therm115

parent 5f41614a
Branches
Tags
No related merge requests found
...@@ -9,6 +9,7 @@ and returns average and sem. ...@@ -9,6 +9,7 @@ and returns average and sem.
import argparse import argparse
import numpy as np import numpy as np
import sys import sys
import os
import logging import logging
import utilsscript import utilsscript
...@@ -32,7 +33,7 @@ def get_fields(infile, is_non_standard): ...@@ -32,7 +33,7 @@ def get_fields(infile, is_non_standard):
return fields return fields
def compute_mean(fields, infile): def compute_mean(fields, infile, stepmin, stepmax):
''' '''
Computes data ave and sem. Computes data ave and sem.
''' '''
...@@ -60,6 +61,11 @@ def compute_mean(fields, infile): ...@@ -60,6 +61,11 @@ def compute_mean(fields, infile):
if not line: if not line:
break break
step, nentries = int(line[0]), int(line[1]) step, nentries = int(line[0]), int(line[1])
keep = True
if stepmin and step < stepmin:
keep = False
if stepmax and step > stepmax:
break
if nrec % 1000 == 0: if nrec % 1000 == 0:
logging.info( logging.info(
"Reading record number {:<10d}, step {:<10.2f}." "Reading record number {:<10d}, step {:<10.2f}."
...@@ -72,10 +78,13 @@ def compute_mean(fields, infile): ...@@ -72,10 +78,13 @@ def compute_mean(fields, infile):
) )
sys.exit(1) sys.exit(1)
for i in range(nentries): for i in range(nentries):
if keep:
line = list(map(float, f.readline().split())) line = list(map(float, f.readline().split()))
for j, k in enumerate(fields): for j, k in enumerate(fields):
ave[i, j] += line[j] ave[i, j] += line[j]
nrec += 1 else:
f.readline()
nrec += keep
except EOFError: except EOFError:
break break
...@@ -92,6 +101,11 @@ def compute_mean(fields, infile): ...@@ -92,6 +101,11 @@ def compute_mean(fields, infile):
try: try:
line = line.split() line = line.split()
step, nentries = int(line[0]), int(line[1]) step, nentries = int(line[0]), int(line[1])
keep = True
if stepmin and step < stepmin:
keep = False
if stepmax and step > stepmax:
break
if nrec % 1000 == 0: if nrec % 1000 == 0:
logging.info( logging.info(
"Reading record number {:<10d}, step {:<10.2f}." "Reading record number {:<10d}, step {:<10.2f}."
...@@ -104,17 +118,19 @@ def compute_mean(fields, infile): ...@@ -104,17 +118,19 @@ def compute_mean(fields, infile):
) )
sys.exit(1) sys.exit(1)
for i in range(nentries): for i in range(nentries):
if keep:
line = list(map(float, f.readline().split())) line = list(map(float, f.readline().split()))
for j, k in enumerate(fields): for j, k in enumerate(fields):
sem[i, j] += (line[j] - ave[i, j])**2 sem[i, j] += (line[j] - ave[i, j])**2
nrec += 1 else:
f.readline()
nrec += keep
line = f.readline() line = f.readline()
if not line: if not line:
break break
except EOFError: except EOFError:
break break
sem = np.sqrt(sem/nrec) sem = np.sqrt(sem/nrec)
return ave, sem return ave, sem
...@@ -131,8 +147,8 @@ def write_output(outfile, fields, ave, sem): ...@@ -131,8 +147,8 @@ def write_output(outfile, fields, ave, sem):
] ]
logging.info("About to write output file.") logging.info("About to write output file.")
with open(outfile, 'w') as f: with open(outfile, 'w') as f:
line = " ".join(('#', *["{:^17}"]*len(fields), '\n')) line = " ".join(('#', *["{:^17}"]*len(fields)))
f.write(line.format(*fields)) f.write(''.join([line.format(*fields).rstrip(), '\n']))
for i, (a, s) in enumerate(zip(ave, sem)): for i, (a, s) in enumerate(zip(ave, sem)):
# I found no nice way of doing this... # I found no nice way of doing this...
elems = [] elems = []
...@@ -141,9 +157,9 @@ def write_output(outfile, fields, ave, sem): ...@@ -141,9 +157,9 @@ def write_output(outfile, fields, ave, sem):
if fi == 'Chunk': if fi == 'Chunk':
elems.append("{:^17d}".format(i+1)) elems.append("{:^17d}".format(i+1))
else: else:
elems.append("{:>8.3f} {:>8.3f}".format(a[j], 0.)) elems.append("{:>12.6f} {:>12.6f}".format(a[j], 0.))
else: else:
elems.append("{:>8.3f} {:>8.3f}".format(a[j], s[j])) elems.append("{:>12.6f} {:>12.6f}".format(a[j], s[j]))
line = " ".join((*elems, '\n')) line = " ".join((*elems, '\n'))
f.write(line) f.write(line)
logging.info("DONE!") logging.info("DONE!")
...@@ -177,6 +193,20 @@ def main(): ...@@ -177,6 +193,20 @@ def main():
default="output.mean", default="output.mean",
help="Output file name.", help="Output file name.",
) )
parser.add_argument(
"--smin",
dest="stepmin",
default=0,
type=int,
help="Minimum step to consider [default %s]"
)
parser.add_argument(
"--smax",
dest="stepmax",
default=0,
type=int,
help="Maximum step to consider, 0 for all [default %s]"
)
parser.add_argument( parser.add_argument(
"-n", "-n",
"--non-standard", "--non-standard",
...@@ -194,11 +224,17 @@ def main(): ...@@ -194,11 +224,17 @@ def main():
infile = args.infile infile = args.infile
outfile = args.outfile outfile = args.outfile
stepmin = args.stepmin
stepmax = args.stepmax
is_non_standard = args.non_standard is_non_standard = args.non_standard
if not os.path.isfile(infile):
print("Could not find file {}.".format(infile))
sys.exit()
fields = get_fields(infile, is_non_standard) fields = get_fields(infile, is_non_standard)
ave, sem = compute_mean(fields, infile) ave, sem = compute_mean(fields, infile, stepmin, stepmax)
write_output(outfile, fields, ave, sem) write_output(outfile, fields, ave, sem)
......
...@@ -105,7 +105,7 @@ def main(): ...@@ -105,7 +105,7 @@ def main():
logging.info("Found {} token".format(tok)) logging.info("Found {} token".format(tok))
infos = next(lines).split() infos = next(lines).split()
npoints = int(infos[1]) npoints = int(infos[1])
dummy = next(lines) next(lines)
for i in range(npoints): for i in range(npoints):
line = next(lines).split() line = next(lines).split()
r.append(float(line[1])) r.append(float(line[1]))
...@@ -116,7 +116,7 @@ def main(): ...@@ -116,7 +116,7 @@ def main():
force = np.array(force) force = np.array(force)
toplot.append([r, ener, force, tok]) toplot.append([r, ener, force, tok])
tok = [] tok = []
dummy = next(lines) next(lines)
except StopIteration: except StopIteration:
break break
for tp in toplot: for tp in toplot:
...@@ -127,6 +127,8 @@ def main(): ...@@ -127,6 +127,8 @@ def main():
for tp in toplot: for tp in toplot:
axes[0].plot(tp[0], tp[1], label=tp[3], linewidth=3) axes[0].plot(tp[0], tp[1], label=tp[3], linewidth=3)
axes[1].plot(tp[0], tp[2], label=tp[3], linewidth=3) axes[1].plot(tp[0], tp[2], label=tp[3], linewidth=3)
hmin, hmax = axes[1].get_xlim()
axes[1].hlines(0, hmin, hmax, color="black", linewidth=3, linestyles="dashdot")
if args.temp > 0: if args.temp > 0:
hmin, hmax = axes[0].get_xlim() hmin, hmax = axes[0].get_xlim()
...@@ -146,7 +148,7 @@ def main(): ...@@ -146,7 +148,7 @@ def main():
# Setting axes 0 # Setting axes 0
axes[0].set_title("Estimated energy", fontsize=fontsize) axes[0].set_title("Estimated energy", fontsize=fontsize)
axes[0].legend(frameon=False, fontsize=fontsize) # Fat font, no frame # axes[0].legend(frameon=False, fontsize=fontsize) # Fat font, no frame
axes[0].set_xlabel("r [A]", fontname=font, fontsize=fontsize) # ylabel name, font is Hack, size 30pts axes[0].set_xlabel("r [A]", fontname=font, fontsize=fontsize) # ylabel name, font is Hack, size 30pts
axes[0].set_ylabel("E [kcal/mol]", fontname=font, fontsize=fontsize) # ylabel name, font is Hack, size 30pts axes[0].set_ylabel("E [kcal/mol]", fontname=font, fontsize=fontsize) # ylabel name, font is Hack, size 30pts
axes[0].tick_params(axis='both', which='major', labelsize=fontsize) # Biggers ticks, bigger ticks label! axes[0].tick_params(axis='both', which='major', labelsize=fontsize) # Biggers ticks, bigger ticks label!
...@@ -178,4 +180,3 @@ if __name__ == "__main__": ...@@ -178,4 +180,3 @@ if __name__ == "__main__":
main() main()
except KeyboardInterrupt: except KeyboardInterrupt:
raise SystemExit("User interruption.") raise SystemExit("User interruption.")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment