How to interpolate a solution?

Let’s imagine we have an initial shell represented by a fine mesh. We also have a field for which the values in each node are known.

For whatever reason, we want to represent the same field using a coarser mesh. How can we do it?

Let’s first start by importing all the modules we need.

[1]:
import numpy as np

from kokiy import CartShell
from kokiy.shell_utils import interpolate_solution_on_shell

Now, we create an initial shell represented by a fine mesh.

[2]:
n_trans = 12
n_longi = 24
zero = np.array([0.00, 0.12, -0.2])
u_max = np.array([0.00, 0.12, 0.2])
v_max = np.array([0.20, 0.12, -0.2])

init_shell = CartShell(n_trans, n_longi, zero, u_max, v_max)

For demonstration purposes, let’s use np.random to generate a random field.

[3]:
np.random.seed(1)  # repeteability

data = np.random.random(init_shell.shape)

To create a coarser mesh we rely on replicate (we could have followed the same steps as above, but using less points).

[4]:
n_trans = 6
n_longi = 12

new_shell = init_shell.replicate((n_trans, n_longi))

It is now time to perform the interpolation in the new mesh (notice we just have to adjust the variables accordingly to what cloud2cloud expects).

[5]:
init_mesh = init_shell.xyz.reshape(-1, 3)

coarser_data = interpolate_solution_on_shell(new_shell, init_mesh, data.reshape(-1))

Let’s take a look to what we’ve done using pyvista.

[7]:
pl.show()
../_images/how_to_interpolate_random_solution_14_0.png

If you have several variables in a dict or in a structured numpy array, use interpolate_solutions_on_shell instead (it will be faster than doing an external loop). Your numpy arrays may also contain time-dependent data (time must be the first variable).

Where else can I apply this?

Imagine instead of a structured fine mesh you have an unstructured mesh. Well, the conversion process is very similar (instead of an initial shell, you will already have a mesh).