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

Smilei allow real fields for envelope mode #289

Closed
wants to merge 9 commits into from
57 changes: 54 additions & 3 deletions postpic/particles/particles.py
Original file line number Diff line number Diff line change
Expand Up @@ -1192,7 +1192,7 @@ def __init__(self, sr, speciess, ids=None):
if ids is None:
self.ids = self._findids() # List of integers
else:
self.ids = np.asarray(ids, dtype=np.int)
self.ids = np.asarray(ids, dtype=np.int64)
# lookup dict used by collect
self._updatelookupdict()

Expand Down Expand Up @@ -1224,7 +1224,7 @@ def _findids(self):
ms = MultiSpecies(dr, *self.speciess, ignore_missing_species=True)
idsfound |= set(ms('id'))
del ms
return np.asarray(list(idsfound), dtype=np.int)
return np.asarray(list(idsfound), dtype=np.int64)

def __len__(self):
# counts the number of particles present
Expand All @@ -1243,7 +1243,7 @@ def _collectfromdump(self, dr, scalarfs):
scalars = np.zeros((len(scalarfs), len(ms)))
for i in range(len(scalarfs)):
scalars[i, :] = ms(scalarfs[i])
ids = np.asarray(ms('id'), dtype=np.int)
ids = np.asarray(ms('id'), dtype=np.int64)
del ms # close file to not exceed limit of max open files
return ids, scalars

Expand Down Expand Up @@ -1279,3 +1279,54 @@ def collect(self, *scalarfs):
particlelist[i].append(scalars[:, k])
ret = [np.asarray(p).T for p in particlelist]
return ret

'''
Exports the collected data to an hdf5-file. If the file with the same name
already exists, it will be deleted!

Parameters:
-----------
filename: whole name or path of the file which the data will be exported to.
*scalarfs: the scalarfunction(s) defining the particle property.
Id and weight will always be stored, so they do not have to be
specified here.

Returns:
-----------
The same as collect(): The list of particles with the defined properties.
The hdf5 file will have the structure:
- dset: Ids
- particles/Id/track: track data (ID, weight, scalarfs)
- particles/Id/weight: weights
'''
def exportToHdf5(self, filename, *scalarfs):

scalarfs = ("id", "weight") + scalarfs
print("The following scalars will be exported to {}: {}.".format(filename, scalarfs))
part_data = self.collect(*scalarfs)
print("Finished collecting electron data.")

timesteps_parts = []
for i in range(0, len(part_data)):
timesteps_parts.append(len(part_data[i][0]))

import h5py

try:
os.remove(filename)
except FileNotFoundError:
print("No file there to delete!")
f = h5py.File(filename, "w")
dset_ids = f.create_dataset("Ids", data=self.ids, dtype=np.int64)

for id in range(0, len(self.ids)):
f.create_dataset("particles/{}/track".format(int(self.ids[id])), data=part_data[id])
weight = part_data[id][1][0]
f.create_dataset("particles/{}/weight".format(int(self.ids[id])), data=weight)
if id % 1000 == 0:
print("Electron {} was saved to the hdf5-file.".format(id))

f.close()
print("File closed.")
return part_data

Loading