-
Notifications
You must be signed in to change notification settings - Fork 85
/
fcompare_particles.py
55 lines (48 loc) · 1.73 KB
/
fcompare_particles.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import numpy as np
import argparse
from pathlib import Path
import pandas as pd
import amrex_particle
from amrex_particle import AmrexParticleFile
def main():
"""Compare particle files"""
parser = argparse.ArgumentParser(description="A comparison tool for particles")
parser.add_argument("f0", help="A particle directory", type=str)
parser.add_argument("f1", help="A particle directory", type=str)
parser.add_argument(
"-a",
"--abs_tol",
help="Absolute tolerance (default is 0)",
type=float,
default=0.0,
)
parser.add_argument(
"-r",
"--rel_tol",
help="Relative tolerance (default is 0)",
type=float,
default=0.0,
)
args = parser.parse_args()
assert Path(args.f0).is_dir()
assert Path(args.f1).is_dir()
p0f = AmrexParticleFile(Path(args.f0) / "particles")
p1f = AmrexParticleFile(Path(args.f1) / "particles")
p0df = p0f()
p1df = p1f()
assert np.abs(p0f.info["time"] - p1f.info["time"]) <= args.abs_tol
assert p0df.shape == p1df.shape
p0df.sort_values(by=["uid"], inplace=True, kind="stable", ignore_index=True)
p1df.sort_values(by=["uid"], inplace=True, kind="stable", ignore_index=True)
adiff = np.sqrt(np.square(p0df - p1df).sum(axis=0))
rdiff = np.sqrt(np.square(p0df - p1df).sum(axis=0)) / np.sqrt(
np.square(p0df).sum(axis=0)
)
adiff = adiff.to_frame(name="absolute_error")
rdiff = rdiff.to_frame(name="relative_error")
diff = pd.concat([adiff, rdiff], axis=1).fillna(value={"relative_error": 0.0})
print(diff)
assert (diff["absolute_error"] <= args.abs_tol).all()
assert (diff["relative_error"] <= args.rel_tol).all()
if __name__ == "__main__":
main()