Skip to content

Store a mesh k-d tree for mesh variable interpolation #318

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions src/underworld3/discretisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,9 @@ def nuke_coords_and_rebuild(self):

self.dm.copyDS(self.dm_hierarchy[-1])

# create a kd-tree to store mesh coordinates
self._mesh_kdtree = uw.kdtree.KDTree(self.data)

return

@timing.routine_timer_decorator
Expand Down Expand Up @@ -2118,12 +2121,8 @@ def rbf_interpolate(self, new_coords, meth=0, p=2, verbose=False, nnn=None, rubb
with self.mesh.access(self):
D = self.data.copy()

if verbose and uw.mpi.rank == 0:
print("Building K-D tree", flush=True)

mesh_kdt = uw.kdtree.KDTree(self.coords)
mesh_kdt = self.mesh._mesh_kdtree
values = mesh_kdt.rbf_interpolator_local(new_coords, D, nnn, p=p, verbose=verbose)
del mesh_kdt

return values

Expand Down Expand Up @@ -2240,10 +2239,9 @@ def write(
@timing.routine_timer_decorator
def read_timestep(
self,
data_filename,
data_name,
index,
outputPath="",
outputPath:str,
verbose=False,
):
"""
Expand All @@ -2259,8 +2257,7 @@ def read_timestep(
# mesh.write_timestep( "test", meshUpdates=False, meshVars=[X], outputPath="", index=0)
# swarm.write_timestep("test", "swarm", swarmVars=[var], outputPath="", index=0)

output_base_name = os.path.join(outputPath, data_filename)
data_file = output_base_name + f".mesh.{data_name}.{index:05}.h5"
data_file = outputPath + f"_step_{index:05}.h5"

# check if data_file exists
if os.path.isfile(os.path.abspath(data_file)):
Expand All @@ -2285,7 +2282,7 @@ def field_from_checkpoint(

h5f = h5py.File(data_file)
D = h5f["fields"][data_name][()].reshape(-1, self.shape[1])
X = h5f["fields"]["coordinates"][()].reshape(-1, self.mesh.dim)
X = h5f["geometry"]["vertices"][()].reshape(-1, self.mesh.dim)

h5f.close()

Expand Down
2 changes: 1 addition & 1 deletion src/underworld3/kdtree.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def rbf_interpolator_local_from_kdtree(
# can decompose weighting vecotrs as IDW is a linear relationship
# build normalise weight vectors and multiply that with known data
epsilon = 1e-12
weights = 1 / np.pow( epsilon+distance_n[:], p)
weights = 1.0 / np.power(epsilon+distance_n[:], p)
n_weights = (weights.T / np.sum(weights, axis=1)).T
kdata = data[closest_n[:]]

Expand Down