diff --git a/examples/gpmd/test-energy.py b/examples/gpmd/test-energy.py index d7be33f..d661bea 100755 --- a/examples/gpmd/test-energy.py +++ b/examples/gpmd/test-energy.py @@ -1,4 +1,7 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 + +import argparse + def compare_MD(reference, current, reltol): """Compare MD energies. @@ -8,29 +11,24 @@ def compare_MD(reference, current, reltol): """ - import sys - #energy = re.compile("MD_data\s+([0-9eE.+-]+)\s+([0-9eE.+-]+)") - - fd = open(reference) - reference_energies = [] - for line in fd: - result = line.split() - reference_energies.append(float(result[0])) - fd.close() - - fd = open(current) - current_energies = [] - for line in fd: - result = line.split() - current_energies.append(float(result[0])) - fd.close() - - + with open(reference, encoding="utf-8") as fd: + reference_energies = [] + for line in fd: + result = line.split() + reference_energies.append(float(result[0])) + + with open(current, encoding="utf-8") as fd: + current_energies = [] + for line in fd: + result = line.split() + current_energies.append(float(result[0])) + if len(reference_energies) != len(current_energies): - raise Exception("[error] different number of MD steps\n" - + (" reference ran for %4d steps\n" % (len(reference_energies))) - + (" current ran for %4d steps\n" % (len(current_energies))) - + " can not compare") + raise Exception( + "[error] different number of MD steps\n" + + (" reference ran for %4d steps\n" % (len(reference_energies))) + + (" current ran for %4d steps\n" % (len(current_energies))) + + " can not compare") result = True for i in range(len(reference_energies)): @@ -41,29 +39,34 @@ def compare_MD(reference, current, reltol): print("failure in MD step %d" % (i+1)) result = False if not result: - raise Exception(("[error] when comparing '%s' with '%s'" % (reference, current)) - + "energies do not agree") + raise Exception( + ("[error] when comparing '%s' with '%s'" % (reference, current)) + + "energies do not agree") + + print("Energy test passed without failure ...") - print("Energy test passed without failure ...") def main(): """The main function. """ - import argparse, os, sys - - parser = argparse.ArgumentParser(description="""Script to compare MD results by using the total energy""") - parser.add_argument("--reference", - help="The reference output") - parser.add_argument("--current", - help="The current output") - parser.add_argument("--reltol", - help="Relative tolerance when comparing, default is %(default)s", - type=float, - default=1e-10) + parser = argparse.ArgumentParser(description="""Script to compare + MD results by using the total energy""") + parser.add_argument( + "--reference", + help="The reference output") + parser.add_argument( + "--current", + help="The current output") + parser.add_argument( + "--reltol", + help="Relative tolerance when comparing, default is %(default)s", + type=float, + default=1e-10) options = parser.parse_args() compare_MD(options.reference, options.current, options.reltol) + if __name__ == "__main__": main()