scripts/gray/visual.py: degrade instead of failing on missing files

This commit is contained in:
Michele Guerini Rocco 2025-02-04 15:56:11 +01:00
parent 9442356797
commit 0ee7b683fd
Signed by: rnhmjoj
GPG Key ID: BFBAF4C975F76450

View File

@ -39,6 +39,20 @@ def cli_args() -> argparse.Namespace:
return parser.parse_args() return parser.parse_args()
def placeholder(ax: plt.Axes, text: str):
'''
Puts a placeholder text at the center of an axis
'''
left, width = .25, .5
bottom, height = .25, .5
right = left + width
top = bottom + height
ax.text(0.5*(left+right), 0.5*(top+bottom), text,
horizontalalignment='center',
verticalalignment='center')
def align_yaxis(*axes: [plt.Axes]): def align_yaxis(*axes: [plt.Axes]):
''' '''
Aligns the origins of two axes Aligns the origins of two axes
@ -116,6 +130,7 @@ def plot_poloidal(inputs: Path, outputs: Path, ax: plt.Axes):
ax.set_ylabel('$z$ / m') ax.set_ylabel('$z$ / m')
# load flux surfaces # load flux surfaces
try:
surfaces = gray.read_table(outputs / 'flux-surfaces.71.txt') surfaces = gray.read_table(outputs / 'flux-surfaces.71.txt')
surfaces = surfaces.reshape(-1, int(surfaces['i'].max())) surfaces = surfaces.reshape(-1, int(surfaces['i'].max()))
@ -140,6 +155,8 @@ def plot_poloidal(inputs: Path, outputs: Path, ax: plt.Axes):
fontsize=10, fmt={1: label}) fontsize=10, fmt={1: label})
ax.plot(np.nan, np.nan, '--', color='xkcd:ocean blue', ax.plot(np.nan, np.nan, '--', color='xkcd:ocean blue',
label='rational surfaces') label='rational surfaces')
except FileNotFoundError:
pass
# load limiter # load limiter
limiter = gray.get_limiter(conf, inputs) limiter = gray.get_limiter(conf, inputs)
@ -186,12 +203,15 @@ def plot_toroidal(inputs: Path, outputs: Path, ax: plt.Axes):
limiter = gray.get_limiter(conf, inputs) limiter = gray.get_limiter(conf, inputs)
# plot plasma boundary # plot plasma boundary
try:
surfaces = gray.read_table(outputs / 'flux-surfaces.71.txt') surfaces = gray.read_table(outputs / 'flux-surfaces.71.txt')
boundary = surfaces[np.isclose(surfaces['ψ_n'], 1, 1e-3)] boundary = surfaces[np.isclose(surfaces['ψ_n'], 1, 1e-3)]
# plot plasma boundary # plot plasma boundary
plot_circle(ax, radius=boundary['R'].min(), color='xkcd:slate gray') plot_circle(ax, radius=boundary['R'].min(), color='xkcd:slate gray')
plot_circle(ax, radius=boundary['R'].max(), color='xkcd:slate gray') plot_circle(ax, radius=boundary['R'].max(), color='xkcd:slate gray')
except FileNotFoundError:
pass
# plot limiter # plot limiter
if limiter[0].size > 0: if limiter[0].size > 0:
@ -374,23 +394,30 @@ def plot_inputs(inputs: Path, outputs: Path, axes: [plt.Axes]):
''' '''
from matplotlib.colors import TwoSlopeNorm from matplotlib.colors import TwoSlopeNorm
maps = gray.read_table(outputs / 'inputs-maps.72.txt') conf = gray.read_conf(inputs / 'gray.ini')
try:
maps = gray.read_table(outputs / 'inputs-maps.72.txt')
# filter valid points # filter valid points
maps = maps[maps['ψ_n'] > 0] maps = maps[maps['ψ_n'] > 0]
flux = maps['R'], maps['z'], maps['ψ_n'] flux = maps['R'], maps['z'], maps['ψ_n']
except FileNotFoundError:
maps = None
placeholder(axes['B'], 'inputs-maps.72.txt not found')
# contour levels # contour levels
ψ_max = maps['ψ_n'].max() if maps is not None else 1.2
inner_levels = np.linspace(0, 0.99, 8) inner_levels = np.linspace(0, 0.99, 8)
outer_levels = np.linspace(0.9991, maps['ψ_n'].max()*0.99, 6) outer_levels = np.linspace(0.9991, ψ_max*0.99, 6)
all_levels = np.concatenate([inner_levels, outer_levels]) all_levels = np.concatenate([inner_levels, outer_levels])
# contour style # contour style
norm = TwoSlopeNorm(vmin=0, vcenter=1, vmax=maps['ψ_n'].max()) norm = TwoSlopeNorm(vmin=0, vcenter=1, vmax=ψ_max)
cmap = plt.cm.RdYlGn_r cmap = plt.cm.RdYlGn_r
borders = dict(colors='xkcd:grey', linestyles='-', linewidths=0.8) borders = dict(colors='xkcd:grey', linestyles='-', linewidths=0.8)
# interpolated equilibrium # interpolated equilibrium
if maps is not None:
interp = axes['B'] interp = axes['B']
interp.set_title('poloidal flux', loc='right') interp.set_title('poloidal flux', loc='right')
interp.set_xlabel('$R$ / m') interp.set_xlabel('$R$ / m')
@ -401,7 +428,6 @@ def plot_inputs(inputs: Path, outputs: Path, axes: [plt.Axes]):
interp.plot(np.nan, np.nan, c='xkcd:slate gray', label='plasma boundary') interp.plot(np.nan, np.nan, c='xkcd:slate gray', label='plasma boundary')
# add limiter # add limiter
conf = gray.read_conf(inputs / 'gray.ini')
limiter = gray.get_limiter(conf, inputs) limiter = gray.get_limiter(conf, inputs)
interp.plot(*limiter, c='xkcd:black', label='limiter') interp.plot(*limiter, c='xkcd:black', label='limiter')
@ -418,14 +444,18 @@ def plot_inputs(inputs: Path, outputs: Path, axes: [plt.Axes]):
orig.plot(*eqdsk.limiter, c='xkcd:black') orig.plot(*eqdsk.limiter, c='xkcd:black')
orig.plot(*eqdsk.boundary, c='xkcd:slate gray') orig.plot(*eqdsk.boundary, c='xkcd:slate gray')
else: else:
orig.axis('off') placeholder(orig, 'not an EQDSK')
# colorbar # colorbar
bar = plt.colorbar(plt.cm.ScalarMappable(norm, cmap), cax=axes['C']) bar = plt.colorbar(plt.cm.ScalarMappable(norm, cmap), cax=axes['C'])
bar.set_label(label='normalised poloidal flux', labelpad=10) bar.set_label(label='normalised poloidal flux', labelpad=10)
# Plasma radial profiles # Plasma radial profiles
try:
profiles = gray.read_table(outputs / 'kinetic-profiles.55.txt') profiles = gray.read_table(outputs / 'kinetic-profiles.55.txt')
except FileNotFoundError:
placeholder(axes['D'], 'kinetic-profiles.55.txt not found')
return
axes['D'].set_title('plasma profiles', loc='right') axes['D'].set_title('plasma profiles', loc='right')
add_alt_axis(axes['D'], profiles['ρ_t'], profiles['ψ_n']**0.5, add_alt_axis(axes['D'], profiles['ρ_t'], profiles['ψ_n']**0.5,