Skip to content

Commit

Permalink
Fix script
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas Bock <[email protected]>
  • Loading branch information
nicolasbock committed Sep 22, 2023
1 parent 51fad53 commit f5c266c
Showing 1 changed file with 40 additions and 37 deletions.
77 changes: 40 additions & 37 deletions examples/gpmd/test-energy.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#!/usr/bin/env python
#!/usr/bin/env python3

import argparse


def compare_MD(reference, current, reltol):
"""Compare MD energies.
Expand All @@ -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)):
Expand All @@ -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()

0 comments on commit f5c266c

Please sign in to comment.