openPMD / openPMD-api

:floppy_disk: C++ & Python API for Scientific I/O
https://openpmd-api.readthedocs.io
GNU Lesser General Public License v3.0
138 stars 51 forks source link

Writing particle weight data to h5 #1589

Closed kli-jfp closed 8 months ago

kli-jfp commented 8 months ago

Hi! I'm having trouble saving particle data, specifically weights, to a h5 file. I tried variations of the same code used in openPMD-api/examples/ /7_extended_write_serial.py,

electrons["weighting"] \
        .reset_dataset(Dataset(np.dtype("float32"), extent=[1])) \
        .make_constant(1.e-5)

but this gives an AttributeError: 'openpmd_api.openpmd_api_cxx.Record' object has no attribute 'reset_dataset'

This is the code I'm using. What is the correct way to store the weights (e.g. this h5 file is going to be used in a WarpX sim)?

Python:

import numpy as np
import openpmd_api as io

N = 5  # Number of particles

x = np.zeros(N, dtype=np.float32)
y = np.zeros(N, dtype=np.float32)
z = np.zeros(N, dtype=np.float32)

ux = np.zeros(N, dtype=np.float32)
uy = np.zeros(N, dtype=np.float32)
uz = np.zeros(N, dtype=np.float32)

w = cp.zeros(N, dtype=np.float32)

series = io.Series(
    "H5Files/protons_%05T.h5",
    io.Access.create)

i = series.iterations[0]

# record
electrons = i.particles["electrons"]

# record components
p_x = electrons["position"]["x"]
p_y = electrons["position"]["y"]
p_z = electrons["position"]["z"]

dataset = io.Dataset(
    x.dtype,
    x.shape)
p_x.reset_dataset(dataset)
p_y.reset_dataset(dataset)
p_z.reset_dataset(dataset)

p_x = x
p_y = y
p_z = z

# record components
u_x = electrons["momentum"]["x"]
u_y = electrons["momentum"]["y"]
u_z = electrons["momentum"]["z"]

dataset = io.Dataset(
    ux.dtype,
    ux.shape)
u_x.reset_dataset(dataset)
u_y.reset_dataset(dataset)
u_z.reset_dataset(dataset)

u_x = ux
u_y = uy
u_z = uz

# This section I don't know how to implement...
# This is an attempt
electrons["weighting"][io.Record_Component.SCALAR].store_chunk(w)

series.flush()
series.close()

Software Environment: I have installed openPMD-api with conda (ubuntu os) openpmd-api 0.15.1 pypi_0 pypi openpmd-viewer 1.6.0 pyhd8ed1ab_0 conda-forge

kli-jfp commented 8 months ago

p_w = electrons["weighting"][io.Record_Component.SCALAR]

dataset = io.Dataset(np.dtype("float32"), extent=[N])

p_w.reset_dataset(dataset)

p_w.store_chunk(w)

franzpoeschel commented 8 months ago

I think that you used the example from the dev branch on GitHub? Can you try if this version helps you: https://github.com/openPMD/openPMD-api/blob/0.15.2/examples/7_extended_write_serial.py:

    electrons["weighting"][SCALAR] \
        .reset_dataset(Dataset(np.dtype("float32"), extent=[1])) \
        .make_constant(1.e-5)

Specifying this [SCALAR] bit is no longer necessary on the dev branch, but it's required in the released versions.

@ax3l The documentation at https://openpmd-api.readthedocs.io/en/0.15.2/usage/examples.html links to the dev versions of the examples, regularly causing confusion when there are API additions reflected in them. Is there any way to have the links point to the correct release?