Ejemplo n.º 1
0
 def test_import_h5(self, tmpdir):
     bio = BytesIO()
     grid1 = TimeGrid(123, 3, 13, 111)
     with h5py.File(bio, mode="w") as h5file:
         grid1.export_h5(h5file.create_group("/gr"))
     with h5py.File(bio, mode="r") as h5file:
         grid2 = TimeGrid.import_h5(h5file["/gr"])
     assert_dataclass_eq(grid1, grid2)
Ejemplo n.º 2
0
 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)
     )