コード例 #1
0
 def test_export_h5(self):
     f = BytesIO()
     p1 = ParticleSource()
     with h5py.File(f, mode="w") as h5file:
         p1.export_h5(h5file.create_group(p1.name))
     with h5py.File(f, mode="r") as h5file:
         p2 = ParticleSource.import_h5(h5file[p1.name])
     assert_dataclass_eq(p1, p2)
コード例 #2
0
ファイル: reader.py プロジェクト: fizmat/ef_python
 def import_from_h5(h5file: h5py.File) -> Simulation:
     fields = [Field.import_h5(g) for g in h5file['ExternalFields'].values()]
     sources = [ParticleSource.import_h5(g) for g in h5file['ParticleSources'].values()]
     particles = [ParticleArray.import_h5(g) for g in h5file['ParticleSources'].values()]
     # cupy max has no `initial` argument
     max_id = int(max([(p.ids.get() if hasattr(p.ids, 'get') else p.ids).max(initial=-1) for p in particles], default=-1))
     g = h5file['SpatialMesh']
     mesh = MeshGrid.import_h5(g)
     charge = Reader.array_class(mesh, (), np.reshape(g['charge_density'], mesh.n_nodes))
     potential = Reader.array_class(mesh, (), np.reshape(g['potential'], mesh.n_nodes))
     field = FieldOnGrid('spatial_mesh', 'electric', Reader.array_class(mesh, 3, np.moveaxis(
         np.array([np.reshape(g[f'electric_field_{c}'], mesh.n_nodes) for c in 'xyz']),
         0, -1)))
     return Simulation(
         time_grid=TimeGrid.import_h5(h5file['TimeGrid']),
         mesh=mesh, charge_density=charge, potential=potential, electric_field=field,
         inner_regions=[InnerRegion.import_h5(g) for g in h5file['InnerRegions'].values()],
         electric_fields=[f for f in fields if f.electric_or_magnetic == 'electric'],
         magnetic_fields=[f for f in fields if f.electric_or_magnetic == 'magnetic'],
         particle_interaction_model=Model[
             h5file['ParticleInteractionModel'].attrs['particle_interaction_model'].decode('utf8')
         ],
         particle_sources=sources, particle_arrays=particles, particle_tracker=ParticleTracker(max_id)
     )