4Compare smoothing lengths that Peano yields and what you expect them to be.
8from scipy.spatial
import KDTree
18parser = argparse.ArgumentParser(
20 Compare Smoothing Length Results With Expectations. This script assumes that
21 particles may have variable smoothing lengths.
23 This script has two main modes of usage, which are determined by the
24 provided initial conditions file via the `--ic-file` argument.
26 If the IC file is a hdf5 file, the script assumes that we're running a
27 unit test where swift2 is computing smoothing lengths from randompy sampled
28 particles. It will then look for a `particles-0.vtu` file in this directory
29 to compare results to.
31 If the IC file is a .vtu file, the script assumes that you want to verify a
32 swift2 snapshot. It will then compute smoothing lengths for the particle positions
33 given in the .vtu file, and compare them to the smoothing lengths from the same
41 help=
"Initial conditions file. Must be a .hdf5 or .vtu file.",
50 help=
"Number of neighbours to search for. You must specify either --eta or --neighbours",
58 help=
"Target resolution eta. You must specify either --eta or --neighbours",
65 help=
"Number of dimensions to work with",
74 help=
"Threshold for convergence of smoothing length",
81 default=
"quartic_spline",
82 help=
"SPH kernel function to use.",
83 choices=swift2.sphtools.sph_kernel_list,
86args = parser.parse_args()
87icfile = args.ic_filename
88h_tolerance = args.h_tol
90if kernel ==
"quartic_spline":
91 kernel =
"quartic_spline_vectorized"
97if eta
is None and nneigh
is None:
98 raise ValueError(
"You need to specify either --eta or --neighbours")
99if eta
is not None and nneigh
is not None:
100 raise ValueError(
"You need to specify either --eta or --neighbours, not both")
101if nneigh
is not None:
102 eta = swift2.sphtools.eta_from_number_of_neighbours(
103 nneigh, kernel=kernel, ndim=ndim
106 nneigh = swift2.sphtools.number_of_neighbours_from_eta(
107 eta, kernel=kernel, ndim=ndim
114comparison_tolerance = 10.0 * h_tolerance
115comparison_tolerance = max(comparison_tolerance, 1.0e-4)
118print(
" Running comparison")
119print(
" --- IC file: ", icfile)
120print(
" --- Dimensions: ", ndim)
121print(
" --- SPH Kernel: ", kernel)
122print(
" --- Target number of neighbours:", nneigh)
123print(
" --- Target eta: ", eta)
124print(
" --- h tolerance: ", h_tolerance)
125print(
" --- comparison tolerance: ", comparison_tolerance)
130if icfile.endswith(
".hdf5"):
132 vtufile =
"particles-1.vtu"
134 hfile = h5py.File(icfile,
"r")
135 gas = hfile[
"PartType0"]
136 coords = gas[
"Coordinates"][:]
137 coords = coords[:, :ndim]
138 ids = gas[
"ParticleIDs"][:]
141elif icfile.endswith(
".vtu"):
146 partData = reader.load()
147 coords = partData.x[:, :ndim]
148 ids = partData.partid
151 raise ValueError(
"Unrecognized file type {icfile}, looking for .vtu or .hdf5 file.")
154sort_ic = np.argsort(ids)
156coords = coords[sort_ic]
163distances, indexes = tree.query(coords, k=2 * nneigh + 10 * ndim)
164neighbours = coords[indexes]
166npart = coords.shape[0]
167sml_python = np.zeros((npart))
173 xp = neighbours[i, 0, :]
174 xn = neighbours[i, 1:, :]
177 h = swift2.sphtools.find_smoothing_length(
178 xp, xn, kernel=kernel, eta=eta, h_tolerance=h_tolerance, ndim=ndim, verbose=verb
183pool = multiprocessing.Pool()
184sml_python[:] = pool.map(sml_search, (i
for i
in range(npart)))
191partData = reader.load()
192sml_peano = partData.smoothingLength
193partIDs_peano = partData.partid
196sort_peano = np.argsort(partIDs_peano)
197sml_peano = sml_peano[sort_peano]
198partIDs_peano = partIDs_peano[sort_peano]
204npart = sml_peano.shape[0]
209 +
"particle ID = {0:4d} "
210 +
"h_peano = {1:.5e} "
211 +
"h_python = {2:.5e} "
215for i
in range(npart):
216 diff = 1.0 - sml_peano[i] / sml_python[i]
218 if abs(diff) > comparison_tolerance:
220 print(errorstr.format(
int(partIDs_peano[i]), sml_peano[i], sml_python[i], diff))
223print(f
"\nSummary run with {nneigh} neighbours:")
224print(f
"Finished with {errors} errors.")
226 print(
"Before you panic:")
227 print(
" - Are you using the correct kernel in this script?")
228 print(
" - Are you using the correct resolution eta in this script?")
229 print(
" - Are you hitting h_min/h_max limits?")
230 print(
"Other ToDo's left for this script: ")
231 print(
" - corrections for periodic wrapping ")
Reads in particle fields from a single .vtu file, or all .vtu at a given snapshot time specified in t...