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 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
6 changes: 3 additions & 3 deletions boutdata/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -695,9 +695,9 @@ def __init__(
comments = []
if inline_comment is not None:
parent_section.inline_comments[sectionname] = inline_comment
parent_section._comment_whitespace[
sectionname
] = comment_whitespace
parent_section._comment_whitespace[sectionname] = (
comment_whitespace
)
else:
# A key=value pair

Expand Down
1 change: 1 addition & 0 deletions boutdata/gen_surface.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Flux surface generator for tokamak grid files

"""

from __future__ import print_function

import numpy as np
Expand Down
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
31 changes: 27 additions & 4 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,6 +1048,9 @@ def change_grid(
# Read information from a restart file
with DataFile(file_list[0]) as f:
for var in copy_vars:
if var not in f:
print(f"{var} not present - skipping")
continue
copy_data[var] = f[var]

# Get a list of variables
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