"""This manages the reading of the HDF5 output files for pyro."""importimportlibimporth5pyimportpyro.mesh.boundaryasbndfrompyro.mesh.patchimportCartesian2d,CellCenterData2d,SphericalPolarfrompyro.particlesimportparticles
[docs]defread_bcs(f):"""read in the boundary condition record from the HDF5 file"""try:gb=f["BC"]exceptKeyError:returnNoneBCs={}fornameingb:BCs[name]=gb[name]returnBCs
[docs]defread(filename):"""read an HDF5 file and recreate the simulation object that holds the data and state of the simulation. """ifnotfilename.endswith(".h5"):filename+=".h5"withh5py.File(filename,"r")asf:# read the simulation information -- this only exists if the# file was created as a simulation objecttry:solver_name=f.attrs["solver"]problem_name=f.attrs["problem"]t=f.attrs["time"]n=f.attrs["nsteps"]exceptKeyError:# this was just a patch written outsolver_name=None# read in the grid info and create our gridgrid=f["grid"].attrstry:coord_type=grid["coord_type"]exceptKeyError:coord_type=0ifcoord_type==1:grid_class=SphericalPolarelse:grid_class=Cartesian2dmyg=grid_class(grid["nx"],grid["ny"],ng=grid["ng"],xmin=grid["xmin"],xmax=grid["xmax"],ymin=grid["ymin"],ymax=grid["ymax"])# sometimes problems define custom BCs -- at the moment, we# are going to assume that these always map to BC.user. We# need to read these in now, since the variable creation# requires it.custom_bcs=read_bcs(f)ifcustom_bcsisnotNone:ifsolver_namein["compressible_fv4","compressible_rk","compressible_sdc"]:bc_solver="compressible"else:bc_solver=solver_namebcmod=importlib.import_module(f"pyro.{bc_solver}.BC")forname,is_solidincustom_bcs.items():bnd.define_bc(name,bcmod.user,is_solid=is_solid)# read in the variable info -- start by getting the namesgs=f["state"]names=[]fornings:names.append(n)# create the CellCenterData2d objectmyd=CellCenterData2d(myg)forninnames:grp=gs[n]bc=bnd.BC(xlb=grp.attrs["xlb"],xrb=grp.attrs["xrb"],ylb=grp.attrs["ylb"],yrb=grp.attrs["yrb"])myd.register_var(n,bc)myd.create()# auxiliary dataforkinf["aux"].attrs:myd.set_aux(k,f["aux"].attrs[k])# restore the variable dataforninnames:grp=gs[n]data=grp["data"]v=myd.get_var(n)v.v()[:,:]=data[:,:]# restore the particle datatry:gparticles=f["particles"]particle_data=gparticles["particle_positions"]init_data=gparticles["init_particle_positions"]my_particles=particles.Particles(myd,None,len(particle_data),"array",particle_data,init_data)exceptKeyError:my_particles=Noneifsolver_nameisnotNone:solver=importlib.import_module(f"pyro.{solver_name}")sim=solver.Simulation(solver_name,problem_name,None,None)sim.n=nsim.cc_data=mydsim.cc_data.t=tsim.particles=my_particlessim.read_extras(f)# check if there are derived variables -- since we do a lot of# inheritance, we want to start with the deepest class and# work backwards through the inheritance until we find a derives# moduleformodin[cls.__module__forclsintype(sim).__mro__ifclsisnotobject]:try:derives=importlib.import_module(f"{mod.replace('simulation','derives')}")sim.cc_data.add_derived(derives.derive_primitives)exceptModuleNotFoundError:continueelse:breakifsolver_nameisnotNone:returnsimreturnmyd