Skip to content
Snippets Groups Projects
Commit f972588b authored by calpe's avatar calpe
Browse files

Save array spatio_fft in time.

parent 3e8ba6bb
No related branches found
No related tags found
No related merge requests found
......@@ -9,8 +9,6 @@
:members:
:private-members:
#TODO: key_quantity == linear eigenmode a
"""
from __future__ import division, print_function
......@@ -50,7 +48,7 @@
"it_start": 10,
"nb_times_compute": 100,
"coef_decimate": 10,
"key_quantity": "ux",
"overlap_arrays": 0.5,
},
)
......@@ -66,5 +64,4 @@
# Parameters
self.nb_times_compute = pspatiotemp_spectra.nb_times_compute
self.coef_decimate = pspatiotemp_spectra.coef_decimate
self.key_quantity = pspatiotemp_spectra.key_quantity
self.it_last_run = pspatiotemp_spectra.it_start
......@@ -70,4 +67,5 @@
self.it_last_run = pspatiotemp_spectra.it_start
self.overlap_arrays = pspatiotemp_spectra.overlap_arrays
n0 = len(
list(range(0, output.sim.oper.shapeX_loc[0], self.coef_decimate))
......@@ -85,6 +83,8 @@
# 3D array
self.spatio_temp = np.empty(
[self.nb_times_compute, n0, n1 // 2 + 1], dtype=complex)
self.spatio_temp_olap = np.empty(
[self.nb_times_compute, n0, n1 // 2 + 1], dtype=complex)
# Array omegas
deltat = self.sim.time_stepping.deltat
......@@ -96,6 +96,8 @@
0, self.delta_omega * (nt // 2 + 1), self.delta_omega)
self.nb_times_in_spatio_temp = 0
self.nb_times_in_spatio_temp_olap = - int(
self.overlap_arrays * self.nb_times_compute)
def _init_files(self, dict_arrays_1time=None):
# we can not do anything when this function is called.
......@@ -115,7 +117,18 @@
self.t_last_save = self.sim.time_stepping.t
def _write_array_to_file(self, array, name="spatio_temp"):
"""Writes array to file self.path_file"""
array_to_file = {name: array}
if not os.path.exists(self.path_file):
self._init_files2(array_to_file)
else:
self._add_dict_arrays_to_file(
self.path_file, array_to_file
)
def _online_save(self):
"""Computes and saves the values at one time."""
itsim = int(self.sim.time_stepping.t / self.sim.time_stepping.deltat)
periods_save = self.sim.params.output.periods_save.spatio_temporal_spectra
......@@ -118,7 +131,8 @@
def _online_save(self):
"""Computes and saves the values at one time."""
itsim = int(self.sim.time_stepping.t / self.sim.time_stepping.deltat)
periods_save = self.sim.params.output.periods_save.spatio_temporal_spectra
nb_times_compute = self.nb_times_compute
if itsim - self.it_last_run >= periods_save - 1:
self.it_last_run = itsim
......@@ -122,5 +136,5 @@
if itsim - self.it_last_run >= periods_save - 1:
self.it_last_run = itsim
field = self.sim.state.state_phys.get_var(self.key_quantity)
field = self.sim.state.compute("ap_fft")
field_decimate = field[::self.coef_decimate, ::self.coef_decimate]
......@@ -126,5 +140,3 @@
field_decimate = field[::self.coef_decimate, ::self.coef_decimate]
field_fft = self.oper_fft2.fft2d(field_decimate)
self.spatio_temp[self.nb_times_in_spatio_temp, :, :] = field_fft
self.nb_times_in_spatio_temp += 1
self.spatio_temp[self.nb_times_in_spatio_temp, :, :] = field_decimate
......@@ -130,5 +142,9 @@
if self.nb_times_in_spatio_temp == self.nb_times_compute:
self.nb_times_in_spatio_temp = 0
self.t_last_save = self.sim.time_stepping.t
# Check start overlapped array
nb_times_olap = self.nb_times_in_spatio_temp_olap
condition = nb_times_olap >= 0 and nb_times_olap < nb_times_compute
if condition:
field = self.sim.state.compute("ap_fft")
field_decimate = field[::self.coef_decimate, ::self.coef_decimate]
self.spatio_temp_olap[nb_times_olap, :, :] = field_decimate
......@@ -134,6 +150,5 @@
for i, value in enumerate(self.hamming):
self.spatio_temp[i, :, :] = value * self.spatio_temp[i, :, :]
self.spatio_fft = self.oper_fft1.fft(self.spatio_temp)
# Add index
self.nb_times_in_spatio_temp += 1
self.nb_times_in_spatio_temp_olap += 1
......@@ -139,4 +154,6 @@
if mpi.rank == 0:
spatio_temp_spectra = {"spatio_temp_spectra": self.spatio_fft}
# Check and write overlapped file
if self.nb_times_in_spatio_temp_olap == nb_times_compute:
self.nb_times_in_spatio_temp_olap = 0
self._write_array_to_file(self.spatio_temp_olap)
......@@ -142,11 +159,9 @@
if not os.path.exists(self.path_file):
self._init_files2(spatio_temp_spectra)
else:
self._add_dict_arrays_to_file(
self.path_file, spatio_temp_spectra
)
# Check and write file
if self.nb_times_in_spatio_temp == nb_times_compute:
self.nb_times_in_spatio_temp = 0
self._write_array_to_file(self.spatio_temp)
def load(self):
"""Loads spatio temporal fft from file. """
with h5py.File(self.path_file, "r") as f:
......@@ -149,7 +164,7 @@
def load(self):
"""Loads spatio temporal fft from file. """
with h5py.File(self.path_file, "r") as f:
spatio_temporal_fft = f["spatio_temp_spectra"].value
return spatio_temporal_fft
spatio_temp = f["spatio_temp"].value
return spatio_temp
......@@ -155,7 +170,7 @@
def _compute_energy_from_spatio_temporal_fft(self, spatio_temporal_fft):
"""
Compute energy at each fourier mode from spatio temporal fft.
"""
return (1 / 2.) * np.abs(spatio_temporal_fft) ** 2
# def _compute_energy_from_spatio_temporal_fft(self, spatio_temporal_fft):
# """
# Compute energy at each fourier mode from spatio temporal fft.
# """
# return (1 / 2.) * np.abs(spatio_temporal_fft) ** 2
......@@ -161,6 +176,6 @@
def plot_frequency_spectra(self):
""" Plots the frequency spectra F(\omega). """
# Load data from file
spatio_temporal_fft = self.load()
# def plot_frequency_spectra(self):
# """ Plots the frequency spectra F(\omega). """
# # Load data from file
# spatio_temporal_fft = self.load()
......@@ -166,4 +181,4 @@
# Average over all spatio_temporal_fft 3d arrays
spatio_temporal_fft = spatio_temporal_fft.mean(axis=0)
# # Average over all spatio_temporal_fft 3d arrays
# spatio_temporal_fft = spatio_temporal_fft.mean(axis=0)
......@@ -169,5 +184,5 @@
# Compute energy from spatio_temporal_fft
energy_fft = self._compute_energy_from_spatio_temporal_fft(
spatio_temporal_fft)
# # Compute energy from spatio_temporal_fft
# energy_fft = self._compute_energy_from_spatio_temporal_fft(
# spatio_temporal_fft)
......@@ -173,4 +188,4 @@
omegas = np.arange(
0, self.delta_omega * (self.nb_times_compute// 2 + 1), self.delta_omega)
# omegas = np.arange(
# 0, self.delta_omega * (self.nb_times_compute// 2 + 1), self.delta_omega)
......@@ -176,3 +191,3 @@
E_omega = (1 / self.delta_omega) * energy_fft.sum(1).sum(1)
# E_omega = (1 / self.delta_omega) * energy_fft.sum(1).sum(1)
......@@ -178,8 +193,8 @@
# Plot
fig, ax = plt.subplots()
ax.set_xlabel("$\omega$")
ax.set_ylabel(r"$F(\omega)$")
ax.set_title(r"$E(\omega)$")
ax.loglog(omegas, E_omega)
# # Plot
# fig, ax = plt.subplots()
# ax.set_xlabel("$\omega$")
# ax.set_ylabel(r"$F(\omega)$")
# ax.set_title(r"$E(\omega)$")
# ax.loglog(omegas, E_omega)
......@@ -185,3 +200,3 @@
plt.show()
# plt.show()
......@@ -187,6 +202,6 @@
def plot_omega_kx(self):
""" Plots the frequency spectra F(\omega). """
# Load data from file
spatio_temporal_fft = self.load()
# def plot_omega_kx(self):
# """ Plots the frequency spectra F(\omega). """
# # Load data from file
# spatio_temporal_fft = self.load()
......@@ -192,4 +207,4 @@
# Average over all spatio_temporal_fft 3d arrays
spatio_temporal_fft = spatio_temporal_fft.mean(axis=0)
# # Average over all spatio_temporal_fft 3d arrays
# spatio_temporal_fft = spatio_temporal_fft.mean(axis=0)
......@@ -195,5 +210,5 @@
# Compute energy from spatio_temporal_fft
energy_fft = self._compute_energy_from_spatio_temporal_fft(
spatio_temporal_fft)
# # Compute energy from spatio_temporal_fft
# energy_fft = self._compute_energy_from_spatio_temporal_fft(
# spatio_temporal_fft)
......@@ -199,5 +214,5 @@
delta_kx = 2 * pi / self.oper.Lx
nx_decimate = len(
list(range(0, self.sim.oper.shapeX_loc[1], self.coef_decimate)))
# delta_kx = 2 * pi / self.oper.Lx
# nx_decimate = len(
# list(range(0, self.sim.oper.shapeX_loc[1], self.coef_decimate)))
......@@ -203,5 +218,5 @@
kxs = np.arange(0, delta_kx * (nx_decimate // 2 + 1), delta_kx)
omegas = np.arange(
0, self.delta_omega * (self.nb_times_compute// 2 + 1), self.delta_omega)
# kxs = np.arange(0, delta_kx * (nx_decimate // 2 + 1), delta_kx)
# omegas = np.arange(
# 0, self.delta_omega * (self.nb_times_compute// 2 + 1), self.delta_omega)
......@@ -207,3 +222,3 @@
E_omega_kx = (1 / self.delta_omega) * (1 / delta_kx) * energy_fft.sum(1)
# E_omega_kx = (1 / self.delta_omega) * (1 / delta_kx) * energy_fft.sum(1)
......@@ -209,4 +224,4 @@
# Plot
kx_grid, omega_grid = np.meshgrid(kxs, omegas)
# # Plot
# kx_grid, omega_grid = np.meshgrid(kxs, omegas)
......@@ -212,7 +227,7 @@
fig, ax = plt.subplots()
ax.set_xlabel("$\omega$")
ax.set_ylabel("$k_x$")
ax.set_title(r"$E(\omega, k_x)$")
ax.pcolor(omega_grid, kx_grid, E_omega_kx)
# fig, ax = plt.subplots()
# ax.set_xlabel("$\omega$")
# ax.set_ylabel("$k_x$")
# ax.set_title(r"$E(\omega, k_x)$")
# ax.pcolor(omega_grid, kx_grid, E_omega_kx)
......@@ -218,2 +233,2 @@
plt.show()
# plt.show()
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