Skip to content
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

Improve grid interpolation for restart files #87

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
1 change: 0 additions & 1 deletion boutdata/mayavi2.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ def aligned_points(grid, nz=1, period=1.0, maxshift=0.4):


def create_grid(grid, data, period=1):

s = np.shape(data)

nx = grid["nx"]
Expand Down
1 change: 0 additions & 1 deletion boutdata/mms.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,6 @@ def write(self, nx, ny, output, MXG=2):
("sinty", self.sinty),
("zShift", self.zShift),
]:

# Note: This is slow, and could be improved using something like lambdify
values = zeros([ngx, ngy])
for i, x in enumerate(xarr):
Expand Down
33 changes: 28 additions & 5 deletions boutdata/restart.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ def is_pow2(x):

# Open the restart file in read mode and create the new file
with DataFile(f) as old, DataFile(new_f, write=True, create=True) as new:

# Find the dimension
for var in old.list():
# Read the data
Expand Down Expand Up @@ -223,7 +222,6 @@ def is_pow2(x):

# Find 3D variables
if old.ndims(var) == 3:

# Asynchronous call (locks first at .get())
jobs.append(
pool.apply_async(
Expand Down Expand Up @@ -974,6 +972,7 @@ def change_grid(
output=".",
interpolator="nearest",
show=False,
floor=True,
):
"""
Convert a set of restart files from one grid to another
Expand All @@ -992,9 +991,11 @@ def change_grid(
output : str, optional
Directory where output restart files will be written
interpolator : str, optional
Interpolation method to use. Options are 'nearest', 'CloughTocher', 'RBF'
Interpolation method to use. Options are 'nearest', 'CloughTocher', 'RBF', 'linear'
show : bool, optional
Display the interpolated fields using Matplotlib
floor : bool, optional
Apply floor to interpolated densities and pressures

"""

Expand Down Expand Up @@ -1047,7 +1048,10 @@ def change_grid(
# Read information from a restart file
with DataFile(file_list[0]) as f:
for var in copy_vars:
copy_data[var] = f[var]
try:
copy_data[var] = f[var]
except:
print("Failed to copy variable '{}'".format(var))
bendudson marked this conversation as resolved.
Show resolved Hide resolved

# Get a list of variables
varnames = f.list()
Expand Down Expand Up @@ -1109,14 +1113,22 @@ def extrapolate_yguards(data2d, myg):
from_data.flatten(),
neighbors=50,
)

elif interpolator == "nearest":
# Nearest neighbour. Tends to be robust
from scipy.interpolate import NearestNDInterpolator

interp = NearestNDInterpolator(
list(zip(from_Rxy.flatten(), from_Zxy.flatten())), from_data.flatten()
)
elif interpolator == "linear":
# Linear interpolator
from scipy.interpolate import LinearNDInterpolator

interp = LinearNDInterpolator(
list(zip(from_Rxy.flatten(), from_Zxy.flatten())),
from_data.flatten(),
fill_value=0.0,
)
else:
raise ValueError("Invalid interpolator")

Expand All @@ -1132,6 +1144,17 @@ def extrapolate_yguards(data2d, myg):
np.amax(to_data),
)
)

if floor:
if var[0] == "N" and var[1] != "V":
# A density
to_data = np.clip(to_data, 1e-5, None)
print("\t\t floor -> {}:{}".format(np.amin(to_data), np.amax(to_data)))
elif var[0] == "P":
# Pressure
to_data = np.clip(to_data, 1e-8, None)
print("\t\t floor -> {}:{}".format(np.amin(to_data), np.amax(to_data)))

if show:
import matplotlib.pyplot as plt

Expand Down