Peano
Loading...
Searching...
No Matches
compare_particle_positions.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2
3"""!
4Compare whether initial states and states after 1 loop through box are identical
5"""
6
7import numpy as np
8import argparse
9import sys
10import peano4.api
12
13
14parser = argparse.ArgumentParser(
15 description="""
16 Compare whether the particles are at the correct position.
17 The assumption is that particles move with a constant velocity
18 across periodic boundaries.
19 """
20)
21parser.add_argument(
22 "-f",
23 "--pvd-file",
24 dest="pvdfile",
25 help=".pvd file to read in",
26 default="./particles.pvd",
27)
28parser.add_argument(
29 "-d",
30 "--ndim",
31 dest="ndim",
32 type=int,
33 help="Number of dimensions to work with",
34 required=True,
35)
36parser.add_argument(
37 "-et",
38 "--end-time",
39 dest="t_end",
40 type=float,
41 help="What end time to read in",
42 required=True,
43)
44parser.add_argument(
45 "-tol",
46 "--tolerance",
47 dest="tolerance",
48 type=float,
49 default=1.0e-5,
50 help="Threshold for comparison tolerance",
51)
52parser.add_argument(
53 "-bs",
54 "--boxsize",
55 dest="boxsize",
56 type=float,
57 default=1.0,
58 help="Box size in all dimensions.",
59)
60
61args = parser.parse_args()
62
63ndim = args.ndim
64comparison_tolerance = args.tolerance
65boxsize = args.boxsize
66end_time = args.t_end
67pvdfile = args.pvdfile
68
69
70reader_0 = ParticleVTUReader(pvdfile=pvdfile, snapshot_time=0.0)
71partData_0 = reader_0.load()
72
73reader_1 = ParticleVTUReader(pvdfile=pvdfile, snapshot_time=end_time)
74partData_1 = reader_1.load()
75
76actual_end_time = reader_1.get_snapshot_time()
77
78coords_0 = partData_0.x[:, :ndim]
79vels_0 = partData_0.v[:, :ndim]
80ids_0 = partData_0.partid
81sort0 = np.argsort(ids_0)
82coords_0 = coords_0[sort0]
83vels_0 = vels_0[sort0]
84ids_0 = ids_0[sort0]
85
86coords_expect = coords_0 + actual_end_time * vels_0
87for dim in range(ndim):
88 mask = coords_expect[:, dim] > boxsize
89 while mask.any():
90 coords_expect[mask, dim] -= boxsize
91 mask = coords_expect[:, dim] > boxsize
92
93 mask = coords_expect[:, dim] < 0.0
94 while mask.any():
95 coords_expect[mask, dim] += boxsize
96 mask = coords_expect[:, dim] < 0.0
97
98
99coords_1 = partData_1.x[:, :ndim]
100ids_1 = partData_1.partid
101sort1 = np.argsort(ids_1)
102coords_1 = coords_1[sort1]
103ids_1 = ids_1[sort1]
104
105
106if coords_0.shape != coords_1.shape:
107 print("Error: coordinate arrays are not of equal length.")
108 print(" coords_0.shape", coords_0.shape)
109 print(" coords_1.shape", coords_1.shape)
110 quit()
111
112
113errors = 0
114
115for i in range(ids_0.shape[0]):
116 for n in range(ndim):
117 diff = coords_expect[i, n] / coords_1[i, n] - 1.0
118 if abs(diff) > comparison_tolerance:
119 print("Error: Coordinates disagree.")
120 print(" Particle ID: ", ids_0[i], ids_1[i])
121 print(" Dimension: ", n)
122 print(" Diff: ", diff)
123 print(" Coords_0: ", coords_0[i])
124 print(" Coords_1: ", coords_1[i])
125 print(" Coords expect: ", coords_expect[i])
126 print("----------------------------")
127 errors += 1
128
129print("Finished with", errors, "errors.")
130
131sys.exit(errors)
Reads in particle fields from a single .vtu file, or all .vtu at a given snapshot time specified in t...