Skip to content
Snippets Groups Projects
Commit 498fa9ba authored by Ashwin Vishnu's avatar Ashwin Vishnu
Browse files

Added animate functionality for sw1l.output.spectra

parent 96760f74
No related branches found
No related tags found
No related merge requests found
......@@ -7,7 +7,7 @@
from fluidsim.util.util import pathdir_from_namedir, load_state_phys_file
class MoviesBase(object):
""" ..TODO: Clean up unnecessary default function arguments"""
def _set_path(self):
"""
Sets path attribute specifying the location of saved files.
......@@ -50,7 +50,7 @@
#self.sim.init_fields.get_state_from_simul(sim_temp)
self.sim = load_state_phys_file(self.path, t_approx=time)
return self._select_field()
return self._select_field(key_field=self._ani_key)
def _select_axis(self, xlabel='x', ylabel='y'):
"""
......@@ -70,7 +70,7 @@
raise NotImplementedError('_select_field function declaration missing')
def animate(self, numfig=None, nb_contours=10, frame_dt=300, file_dt=0.1, xmax=None, ymax=None, save_file=None):
def animate(self, key_field=None, numfig=None, nb_contours=10, frame_dt=300, file_dt=0.1, xmax=None, ymax=None, save_file=None):
"""
Load the key field from multiple save files and display as
an animated plot.
......@@ -89,7 +89,7 @@
'The MPI version of get_state_from_simul()\n'
'is not implemented.')
self._ani_init(numfig, nb_contours, file_dt, xmax, ymax)
self._ani_init(key_field, numfig, nb_contours, file_dt, xmax, ymax)
self._animation = animation.FuncAnimation(self._ani_fig, self._ani_update, self._ani_t,
interval=frame_dt, blit=False)
......@@ -116,9 +116,9 @@
class MoviesBase1D(MoviesBase):
def _ani_init(self, numfig, nb_contours, file_dt, xmax, ymax):
def _ani_init(self, key_field, numfig, nb_contours, file_dt, xmax, ymax):
"""
Initializes animated fig. and list of times of save files to load
.. FIXME: "Can't open attribute (Can't locate attribute: 'ny')"
Possible sources of error load_state_phys_file / info_solver
"""
......@@ -120,10 +120,11 @@
"""
Initializes animated fig. and list of times of save files to load
.. FIXME: "Can't open attribute (Can't locate attribute: 'ny')"
Possible sources of error load_state_phys_file / info_solver
"""
self._ani_fig, self._ani_ax = plt.subplots()
self._ani_key = key_field
self._ani_fig, self._ani_ax = plt.subplots(num=numfig)
self._ani_line, = self._ani_ax.plot([], [])
self._ani_t = []
self._set_font()
self._set_path()
......@@ -126,7 +127,7 @@
self._ani_line, = self._ani_ax.plot([], [])
self._ani_t = []
self._set_font()
self._set_path()
ax = self._ani_ax
ax.set_xlim(0, xmax)
......@@ -131,6 +132,6 @@
ax = self._ani_ax
ax.set_xlim(0, xmax)
ax.set_ylim(-ymax, ymax)
ax.set_ylim(10e-16, ymax)
tmax = int(self.sim.time_stepping.t)
self._ani_t = list(np.arange(0, tmax+file_dt, file_dt))
......@@ -139,6 +140,5 @@
"""
Loads contour data and updates figure
"""
x = self._select_axis()
with stdout_redirected():
y, key_field = self._ani_get_field(time)
......@@ -143,5 +143,6 @@
with stdout_redirected():
y, key_field = self._ani_get_field(time)
x = self._select_axis()
self._ani_line.set_data(x, y)
title = (key_field +
......@@ -145,7 +146,7 @@
self._ani_line.set_data(x, y)
title = (key_field +
', t = {0:.3f}, '.format(self.sim.time_stepping.t) +
', t = {0:.3f}, '.format(time) +
self.output.name_solver +
', nh = {0:d}'.format(self.params.oper.nx))
self._ani_ax.set_title(title)
......@@ -157,6 +158,7 @@
x = self.oper.xs
except AttributeError:
x = self.oper.x_seq
self._ani_ax.set_xlabel(xlabel, fontdict=self.font)
self._ani_ax.set_ylabel(ylabel, fontdict=self.font)
return x
......@@ -164,7 +166,7 @@
class MoviesBase2D(MoviesBase):
def _ani_init(self, numfig, nb_contours, file_dt, xmax=None, ymax=None):
def _ani_init(self, key_field, numfig, nb_contours, file_dt, xmax=None, ymax=None):
"""
Initializes animated fig. and list of times of save files to load
"""
......@@ -168,6 +170,7 @@
"""
Initializes animated fig. and list of times of save files to load
"""
self._ani_key = key_field
self._ani_nbc = nb_contours
self._ani_t = []
self._set_font()
......@@ -205,7 +208,7 @@
# self._ani_fig.colorbar(contours)
self._ani_fig.contours = contours
title = (key_field +
', t = {0:.3f}, '.format(self.sim.time_stepping.t) +
', t = {0:.3f}, '.format(time) +
self.output.name_solver +
', nh = {0:d}'.format(self.params.oper.nx))
......
......@@ -5,5 +5,4 @@
from fluiddyn.util import mpi
from .base import SpecificOutput
......@@ -9,3 +8,4 @@
from .base import SpecificOutput
from .movies import MoviesBase1D
......@@ -10,6 +10,6 @@
class Spectra(SpecificOutput):
class Spectra(SpecificOutput, MoviesBase1D):
"""Used for the saving of spectra.
"""
......@@ -180,3 +180,28 @@
def plot2d(self):
pass
def _ani_init(self, key_field, numfig, nb_contours, file_dt, xmax, ymax):
""".. TODO: Needs more generalization for all _ani functions replaced by inheritance;"""
if xmax is None:
xmax = self.oper.khE[-1:][0]
if ymax is None:
ymax = 1.0
super(Spectra, self)._ani_init(key_field, numfig, nb_contours, file_dt, xmax, ymax)
def _ani_get_field(self, time):
raise NotImplementedError('_ani_get_field function declaration missing')
def _select_field(self, field=None, key_field=None):
raise NotImplementedError('_select_field function declaration missing')
def _select_axis(self, xlabel='kh'):
# x = self.oper.khE
f = h5py.File(self.path_file2D, 'r')
x = f['khE'][...]
self._ani_ax.set_xlabel(xlabel, fontdict=self.font)
self._ani_ax.set_ylabel(self._ani_key, fontdict=self.font)
self._ani_ax.set_yscale('log')
return x
......@@ -126,10 +126,6 @@
print('you need to implement the ploting '
'of the spectra for this case')
def plot1d(self, tmin=0, tmax=1000, delta_t=2,
coef_compensate=3):
......@@ -226,10 +222,6 @@
ax1.plot(kh, 0.01*kh**(-5./3)*coef_norm, 'k--', linewidth=1)
def plot2d(self, tmin=0, tmax=1000, delta_t=2,
coef_compensate=3):
......@@ -349,3 +341,30 @@
ax1.plot(kh, kh**(-3)*coef_norm, 'k--', linewidth=1)
ax1.plot(kh, 0.01*kh**(-5./3)*coef_norm, 'k-.', linewidth=1)
def _ani_get_field(self, time):
f = h5py.File(self.path_file2D, 'r')
dset_times = f['times']
times = dset_times[...]
it = np.argmin(abs(times-time))
y = self._select_field(h5file=f, key_field=self._ani_key, it=it)
y[abs(y) < 10e-16] = 0
return y, self._ani_key
def _select_field(self, h5file=None, key_field=None, it=None):
if key_field is 'EK' or key_field is None:
self._ani_key = 'EK'
y = h5file['spectrum2D_EK'][it]
elif key_field is 'EA':
y = h5file['spectrum2D_EA'][it]
elif key_field is 'EKr':
y = h5file['spectrum2D_EKr'][it]
elif key_field is 'EKd':
y = h5file['spectrum2D_EK'][it] - h5file['spectrum2D_EKr'][it]
else:
raise KeyError('Unknown key ',key_field)
return y
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment