Skip to content
Snippets Groups Projects
Commit 86be0681 authored by Antoine Cyril David Hoffmann's avatar Antoine Cyril David Hoffmann 🌱
Browse files

python script update

parent 2211fb85
No related branches found
No related tags found
No related merge requests found
......@@ -47,4 +47,48 @@ def load_grids(filename):
Ly = 2*np.pi/kygrid[1]
xgrid = np.linspace(-Lx/2,Lx/2,Nx)
ygrid = np.linspace(-Ly/2,Ly/2,Ny)
return xgrid,kxgrid,ygrid,kygrid,zgrid,pgrid,jgrid
\ No newline at end of file
return xgrid,kxgrid,ygrid,kygrid,zgrid,pgrid,jgrid
def load_group(filename,group):
data = {}
with h5py.File(filename, 'r') as file:
g_ = file[f"data/"+group]
for key in g_.keys():
name='data/'+group+'/'+key
data[key] = file.get(name)[:]
return data
def load_params(filename):
with h5py.File(filename, 'r') as file:
nml_str = file[f"files/STDIN.00"][0]
nml_str = nml_str.decode('utf-8')
params = read_namelist(nml_str)
return params
# Function to read all namelists from a file
def read_namelist(nml_str):
Nspecies = 1
all_namelists = {}
current_namelist = None
nml_str = nml_str.split('\n')
for line in nml_str:
line = line.split('!')
line = line[0]
if line.startswith('&'):
current_namelist = line[1:].strip()
if current_namelist == 'SPECIES':
current_namelist = current_namelist + "_" + str(Nspecies)
Nspecies = Nspecies + 1
all_namelists[current_namelist] = {}
elif line.startswith('/'):
current_namelist = None
elif current_namelist:
parts = line.split('=')
if len(parts) == 2:
key = parts[0].strip()
value = parts[1].strip().rstrip(',').strip("'").strip()
if tools.is_convertible_to_float(value):
all_namelists[current_namelist][key] = float(value)
else:
all_namelists[current_namelist][key] = value
return all_namelists
\ No newline at end of file
......@@ -12,7 +12,7 @@ import numpy as np
import sys
import tools
# Open the HDF5 file
#------ Open the HDF5 file
if __name__ == "__main__":
# No command-line argument provided, use default filename
filename = "outputs_00.h5"
......@@ -29,15 +29,31 @@ else:
print("Usage: python minimal_analysis.py [filename, int or string (opt)]")
sys.exit(1)
#------ Load data
# load the input parameters from the STDIN.00 file stored in the h5
params = loader.load_params(filename)
# time traces (heat and particle fluxes)
t0D, hflux_x = loader.load_data_0D(filename,'hflux_x')
t0D, pflux_x = loader.load_data_0D(filename,'pflux_x')
# 3D fields (phi and first gyromoment)
t3D, phi, tf = loader.load_data_3D_frame(filename,'phi',10000)
t3D, Ni00,tf = loader.load_data_3D_frame(filename,'Ni00',10000)
# grids of the simulation
x,kx,y,ky,z,p,j = loader.load_grids(filename)
# Examples to load data in dictionaries
# metric (gij, dBdi, Jacobian etc.)
metric = loader.load_group(filename,'metric')
# another way to load the time traces all at once
time_traces = loader.load_group(filename,'var0d')
#------ Process data
# Fourier transform into real space at constant plane
phi = tools.zkxky_to_xy_const_z(phi,-1)
Ni00= tools.zkxky_to_xy_const_z(Ni00,-1)
#------ Plot data
# Plot hflux_x and pflux_x against time
fig, axes = plt.subplots(2, 2, figsize=(10, 6))
# Plot hflux_x
......
......@@ -20,4 +20,22 @@ def closest_index(array, v):
# Find the index of the minimum difference
closest_index = np.argmin(absolute_diff)
return closest_index
\ No newline at end of file
return closest_index
def is_convertible_to_float(s):
try:
float(s)
return True
except ValueError:
return False
def numpy_array_to_list(d):
"""
Recursively convert NumPy arrays to lists within a dictionary.
"""
for key, value in d.items():
if isinstance(value, np.ndarray):
d[key] = value.tolist()
elif isinstance(value, dict):
d[key] = numpy_array_to_list(value)
return d
\ 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