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

Make multiplying with unit_SI optional #309

Open
wants to merge 8 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
30 changes: 21 additions & 9 deletions openpmd_viewer/openpmd_timeseries/data_reader/data_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def read_openPMD_params(self, iteration, extract_parameters=True):
self.series, iteration, extract_parameters)

def read_field_cartesian( self, iteration, field, coord, axis_labels,
slice_relative_position, slice_across ):
slice_relative_position, slice_across, units ):
"""
Extract a given field from an openPMD file in the openPMD format,
when the geometry is cartesian (1d, 2d or 3d).
Expand Down Expand Up @@ -172,6 +172,9 @@ def read_field_cartesian( self, iteration, field, coord, axis_labels,
0 : middle of the simulation box
1 : upper edge of the simulation box

units: string
Type of units to be used for data reading.

Returns
-------
A tuple with
Expand All @@ -183,14 +186,14 @@ def read_field_cartesian( self, iteration, field, coord, axis_labels,
filename = self.iteration_to_file[iteration]
return h5py_reader.read_field_cartesian(
filename, iteration, field, coord, axis_labels,
slice_relative_position, slice_across )
slice_relative_position, slice_across, units )
elif self.backend == 'openpmd-api':
return io_reader.read_field_cartesian(
self.series, iteration, field, coord, axis_labels,
slice_relative_position, slice_across )
slice_relative_position, slice_across, units )

def read_field_circ( self, iteration, field, coord, slice_relative_position,
slice_across, m=0, theta=0. ):
slice_across, units, m=0, theta=0. ):
"""
Extract a given field from an openPMD file in the openPMD format,
when the geometry is thetaMode
Expand Down Expand Up @@ -229,6 +232,9 @@ def read_field_circ( self, iteration, field, coord, slice_relative_position,
0 : middle of the simulation box
1 : upper edge of the simulation box

units: string
Type of units to be used for data reading.

Returns
-------
A tuple with
Expand All @@ -241,13 +247,14 @@ def read_field_circ( self, iteration, field, coord, slice_relative_position,
filename = self.iteration_to_file[iteration]
return h5py_reader.read_field_circ(
filename, iteration, field, coord, slice_relative_position,
slice_across, m, theta )
slice_across, units, m, theta )
elif self.backend == 'openpmd-api':
return io_reader.read_field_circ(
self.series, iteration, field, coord, slice_relative_position,
slice_across, m, theta )
slice_across, units, m, theta )

def read_species_data( self, iteration, species, record_comp, extensions):
def read_species_data( self, iteration, species, record_comp,
extensions, units):
"""
Extract a given species' record_comp

Expand All @@ -265,14 +272,19 @@ def read_species_data( self, iteration, species, record_comp, extensions):

extensions: list of strings
The extensions that the current OpenPMDTimeSeries complies with

units: string
Type of units to be used for data reading.
"""
if self.backend == 'h5py':
filename = self.iteration_to_file[iteration]
return h5py_reader.read_species_data(
filename, iteration, species, record_comp, extensions )
filename, iteration, species, record_comp,
extensions, units)
elif self.backend == 'openpmd-api':
return io_reader.read_species_data(
self.series, iteration, species, record_comp, extensions )
self.series, iteration, species, record_comp,
extensions, units)

def get_grid_parameters(self, iteration, avail_fields, metadata ):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@


def read_field_cartesian( filename, iteration, field, coord, axis_labels,
slice_relative_position, slice_across ):
slice_relative_position, slice_across, units ):
"""
Extract a given field from an HDF5 file in the openPMD format,
when the geometry is cartesian (1d, 2d or 3d).
Expand Down Expand Up @@ -53,6 +53,9 @@ def read_field_cartesian( filename, iteration, field, coord, axis_labels,
0 : middle of the simulation box
1 : upper edge of the simulation box

units: string
Type of units to be used for data reading.

Returns
-------
A tuple with
Expand Down Expand Up @@ -103,11 +106,11 @@ def read_field_cartesian( filename, iteration, field, coord, axis_labels,

axes = { i: axis_labels[i] for i in range(len(axis_labels)) }
# Extract data
F = get_data( dset, list_i_cell, list_slicing_index )
F = get_data( dset, units, list_i_cell, list_slicing_index )
info = FieldMetaInformation( axes, shape, grid_spacing, global_offset,
group.attrs['gridUnitSI'], dset.attrs['position'] )
else:
F = get_data( dset )
F = get_data( dset, units )
axes = { i: axis_labels[i] for i in range(len(axis_labels)) }
info = FieldMetaInformation( axes, F.shape,
group.attrs['gridSpacing'], group.attrs['gridGlobalOffset'],
Expand All @@ -119,7 +122,8 @@ def read_field_cartesian( filename, iteration, field, coord, axis_labels,


def read_field_circ( filename, iteration, field, coord,
slice_relative_position, slice_across, m=0, theta=0. ):
slice_relative_position, slice_across,
units, m=0, theta=0. ):
"""
Extract a given field from an HDF5 file in the openPMD format,
when the geometry is thetaMode
Expand Down Expand Up @@ -159,6 +163,9 @@ def read_field_circ( filename, iteration, field, coord,
0 : middle of the simulation box
1 : upper edge of the simulation box

units: string
Type of units to be used for data reading.

Returns
-------
A tuple with
Expand Down Expand Up @@ -188,7 +195,7 @@ def read_field_circ( filename, iteration, field, coord,
# Get cylindrical info
rmax = info.rmax
inv_dr = 1./info.dr
Fcirc = get_data( dset ) # (Extracts all modes)
Fcirc = get_data( dset, units ) # (Extracts all modes)
nr = Fcirc.shape[1]
if m == 'all':
modes = [ mode for mode in range(0, int(Nm / 2) + 1) ]
Expand Down Expand Up @@ -221,22 +228,22 @@ def read_field_circ( filename, iteration, field, coord,
mult_above_axis = np.array( mult_above_axis )
mult_below_axis = np.array( mult_below_axis )
# - Sum the modes
F = get_data( dset ) # (Extracts all modes)
F = get_data( dset, units ) # (Extracts all modes)
F_total[Nr:, :] = np.tensordot( mult_above_axis,
F, axes=(0, 0) )[:, :]
F_total[:Nr, :] = np.tensordot( mult_below_axis,
F, axes=(0, 0) )[::-1, :]
elif m == 0:
# Extract mode 0
F = get_data( dset, 0, 0 )
F = get_data( dset, units, 0, 0 )
F_total[Nr:, :] = F[:, :]
F_total[:Nr, :] = F[::-1, :]
else:
# Extract higher mode
cos = np.cos( m * theta )
sin = np.sin( m * theta )
F_cos = get_data( dset, 2 * m - 1, 0 )
F_sin = get_data( dset, 2 * m, 0 )
F_cos = get_data( dset, units, 2 * m - 1, 0 )
F_sin = get_data( dset, units, 2 * m, 0 )
F = cos * F_cos + sin * F_sin
F_total[Nr:, :] = F[:, :]
F_total[:Nr, :] = (-1) ** m * F[::-1, :]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
from .utilities import get_data, join_infile_path


def read_species_data(filename, iteration, species, record_comp, extensions):
def read_species_data(filename, iteration, species, record_comp,
extensions, units):
"""
Extract a given species' record_comp

Expand All @@ -36,6 +37,10 @@ def read_species_data(filename, iteration, species, record_comp, extensions):

extensions: list of strings
The extensions that the current OpenPMDTimeSeries complies with

units: string
Type of units to be used for data reading. Will convert ux uy uz to
narmalized units if 'SI_u'
AlexanderSinn marked this conversation as resolved.
Show resolved Hide resolved
"""
# Open the HDF5 file
dfile = h5py.File( filename, 'r' )
Expand Down Expand Up @@ -63,7 +68,8 @@ def read_species_data(filename, iteration, species, record_comp, extensions):
output_type = np.uint64
else:
output_type = np.float64
data = get_data( species_grp[ opmd_record_comp ], output_type=output_type )
data = get_data( species_grp[ opmd_record_comp ], units,
output_type=output_type)

# For ED-PIC: if the data is weighted for a full macroparticle,
# divide by the weight with the proper power
Expand All @@ -74,16 +80,17 @@ def read_species_data(filename, iteration, species, record_comp, extensions):
macro_weighted = record_dset.attrs['macroWeighted']
weighting_power = record_dset.attrs['weightingPower']
if (macro_weighted == 1) and (weighting_power != 0):
w = get_data( species_grp[ 'weighting' ] )
w = get_data( species_grp[ 'weighting' ], units )
data *= w ** (-weighting_power)

# - Return positions, with an offset
if record_comp in ['x', 'y', 'z']:
offset = get_data(species_grp['positionOffset/%s' % record_comp])
offset = get_data(species_grp['positionOffset/%s' % record_comp],
units)
data += offset
# - Return momentum in normalized units
elif record_comp in ['ux', 'uy', 'uz' ]:
m = get_data(species_grp['mass'])
elif record_comp in ['ux', 'uy', 'uz' ] and units == 'SI_u':
m = get_data(species_grp['mass'], units)
# Normalize only if the particle mass is non-zero
if np.all( m != 0 ):
norm_factor = 1. / (m * constants.c)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def is_scalar_record(record):
return(scalar)


def get_data(dset, i_slice=None, pos_slice=None, output_type=np.float64):
def get_data(dset, units, i_slice=None, pos_slice=None, output_type=np.float64):
"""
Extract the data from a (possibly constant) dataset
Slice the data according to the parameters i_slice and pos_slice
Expand All @@ -85,6 +85,10 @@ def get_data(dset, i_slice=None, pos_slice=None, output_type=np.float64):
dset: an h5py.Dataset or h5py.Group (when constant)
The object from which the data is extracted

units: string
Type of units to be used for data reading. Won't multiply data
with unit_SI when 'raw'

pos_slice: int or list of int, optional
Slice direction(s).
When None, no slicing is performed
Expand Down Expand Up @@ -140,9 +144,8 @@ def get_data(dset, i_slice=None, pos_slice=None, output_type=np.float64):
data = data.astype( output_type )
# Scale by the conversion factor
if output_type in [ np.float64, np.float32, np.float16 ]:
if dset.attrs['unitSI'] != 1.0:
data *= dset.attrs['unitSI']

if dset.attrs['unitSI'] != 1.0 and not units == 'raw':
data *= dset.attrs['unitSI']
return(data)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@


def read_field_cartesian( series, iteration, field_name, component_name,
axis_labels, slice_relative_position, slice_across ):
axis_labels, slice_relative_position, slice_across,
units):
"""
Extract a given field from a file in the openPMD format,
when the geometry is cartesian (1d, 2d or 3d).
Expand Down Expand Up @@ -52,6 +53,9 @@ def read_field_cartesian( series, iteration, field_name, component_name,
0 : middle of the simulation box
1 : upper edge of the simulation box

units: string
Type of units to be used for data reading.

Returns
-------
A tuple with
Expand Down Expand Up @@ -106,21 +110,23 @@ def read_field_cartesian( series, iteration, field_name, component_name,

axes = { i: axis_labels[i] for i in range(len(axis_labels)) }
# Extract data
F = get_data( series, component, list_i_cell, list_slicing_index )
F = get_data( series, component, units,
list_i_cell, list_slicing_index )
info = FieldMetaInformation( axes, shape, grid_spacing, global_offset,
grid_unit_SI, grid_position )
else:
F = get_data( series, component )
F = get_data( series, component, units)
axes = { i: axis_labels[i] for i in range(len(axis_labels)) }
info = FieldMetaInformation( axes, F.shape,
grid_spacing, global_offset,
grid_unit_SI, grid_position )

return F, info
return F, info


def read_field_circ( series, iteration, field_name, component_name,
slice_relative_position, slice_across, m=0, theta=0. ):
slice_relative_position, slice_across,
units, m=0, theta=0. ):
"""
Extract a given field from a file in the openPMD format,
when the geometry is thetaMode
Expand Down Expand Up @@ -160,6 +166,9 @@ def read_field_circ( series, iteration, field_name, component_name,
0 : middle of the simulation box
1 : upper edge of the simulation box

units: string
Type of units to be used for data reading.

Returns
-------
A tuple with
Expand Down Expand Up @@ -191,7 +200,7 @@ def read_field_circ( series, iteration, field_name, component_name,
# Get cylindrical info
rmax = info.rmax
inv_dr = 1./info.dr
Fcirc = get_data( series, component ) # (Extracts all modes)
Fcirc = get_data( series, component, units) # (Extracts all modes)
nr = Fcirc.shape[1]
if m == 'all':
modes = [ mode for mode in range(0, int(Nm / 2) + 1) ]
Expand Down Expand Up @@ -224,22 +233,22 @@ def read_field_circ( series, iteration, field_name, component_name,
mult_above_axis = np.array( mult_above_axis )
mult_below_axis = np.array( mult_below_axis )
# - Sum the modes
F = get_data( series, component ) # (Extracts all modes)
F = get_data( series, component, units ) # (Extracts all modes)
F_total[Nr:, :] = np.tensordot( mult_above_axis,
F, axes=(0, 0) )[:, :]
F_total[:Nr, :] = np.tensordot( mult_below_axis,
F, axes=(0, 0) )[::-1, :]
elif m == 0:
# Extract mode 0
F = get_data( series, component, 0, 0 )
F = get_data( series, component, units, 0, 0 )
F_total[Nr:, :] = F[:, :]
F_total[:Nr, :] = F[::-1, :]
else:
# Extract higher mode
cos = np.cos( m * theta )
sin = np.sin( m * theta )
F_cos = get_data( series, component, 2 * m - 1, 0 )
F_sin = get_data( series, component, 2 * m, 0 )
F_cos = get_data( series, component, units, 2 * m - 1, 0 )
F_sin = get_data( series, component, units, 2 * m, 0 )
F = cos * F_cos + sin * F_sin
F_total[Nr:, :] = F[:, :]
F_total[:Nr, :] = (-1) ** m * F[::-1, :]
Expand Down
Loading