Make single output file for main and subgrids.

这个提交包含在:
Craig Warren
2020-02-13 13:23:11 +00:00
父节点 10e78f6ef5
当前提交 7e6434ede7
共有 3 个文件被更改,包括 249 次插入244 次删除

查看文件

@@ -98,90 +98,77 @@ __global__ void store_outputs(int NRX, int iteration, const int* __restrict__ rx
""")
def write_hdf5_outputfiles(outputfile, G):
if G.rxs:
write_hdf5_main_grid_outputfile(outputfile, G)
def write_hdf5_outputfile(outputfile, G):
"""Write an output file in HDF5 format.
Args:
outputfile (str): Name of the output file.
G (FDTDGrid): Parameters describing a grid in a model.
"""
# Check for any receivers in subgrids
sg_rxs = [True for sg in G.subgrids if sg.rxs]
if sg_rxs:
write_hdf5_sub_grid_outputfile(outputfile, G)
def write_hdf5_main_grid_outputfile(outputfile, G):
"""Write an output file in HDF5 format.
Args:
outputfile (str): Name of the output file.
G (FDTDGrid): Parameters describing a grid in a model.
"""
write_data(outputfile, G)
log.info(f'Written output file: {outputfile.name}')
def write_hdf5_sub_grid_outputfile(outputfile, G):
"""Write an output file in HDF5 format.
Args:
outputfile (str): Name of the output file.
G (FDTDGrid): Parameters describing a grid in a model.
"""
stem = outputfile.stem
suffix = outputfile.suffix
parent = outputfile.parent
for sg in G.subgrids:
# Create an outputfile for each subgrid
fp = stem + '_' + sg.name + suffix
fp = parent / Path(fp)
f = write_data(fp, sg)
# Write some additional meta data about the subgrid
f.attrs['is_os_sep'] = sg.is_os_sep
f.attrs['pml_separation'] = sg.pml_separation
f.attrs['subgrid_pml_thickness'] = sg.pmlthickness['x0']
f.attrs['filter'] = sg.filter
f.attrs['ratio'] = sg.ratio
f.attrs['interpolation'] = sg.interpolation
log.info(f'Written output file: {fp.name}')
def write_data(outputfile, G):
"""Write an output file in HDF5 format.
Args:
outputfile (str): Name of the output file.
G (FDTDGrid): Parameters describing a grid in a model.
Returns:
f (file object): File object.
"""
# Create output file and write top-level meta data
if G.rxs or sg_rxs:
f = h5py.File(outputfile, 'w')
f.attrs['gprMax'] = __version__
f.attrs['Title'] = G.title
f.attrs['Iterations'] = G.iterations
f.attrs['nx_ny_nz'] = (G.nx, G.ny, G.nz)
f.attrs['dx_dy_dz'] = (G.dx, G.dy, G.dz)
f.attrs['dt'] = G.dt
# Write meta data and data for main grid
if G.rxs:
write_grid(f, G)
# Write meta data and data for any subgrids
if sg_rxs:
for sg in G.subgrids:
grp = f.create_group('/subgrids/' + sg.name)
write_grid(grp, sg, is_subgrid=True)
if G.rxs or sg_rxs:
log.info(f'Written output file: {outputfile.name}')
def write_grid(basegrp, G, is_subgrid=False):
"""Write grid meta data and data to HDF5 group.
Args:
basegrp (dict): HDF5 group.
G (FDTDGrid): Parameters describing a grid in a model.
is_subgrid (bool): Is grid instance the main grid or a subgrid.
"""
# Write meta data for grid
basegrp.attrs['Iterations'] = G.iterations
basegrp.attrs['nx_ny_nz'] = (G.nx, G.ny, G.nz)
basegrp.attrs['dx_dy_dz'] = (G.dx, G.dy, G.dz)
basegrp.attrs['dt'] = G.dt
nsrc = len(G.voltagesources + G.hertziandipoles + G.magneticdipoles + G.transmissionlines)
f.attrs['nsrc'] = nsrc
f.attrs['nrx'] = len(G.rxs)
f.attrs['srcsteps'] = G.srcsteps
f.attrs['rxsteps'] = G.rxsteps
basegrp.attrs['nsrc'] = nsrc
basegrp.attrs['nrx'] = len(G.rxs)
basegrp.attrs['srcsteps'] = G.srcsteps
basegrp.attrs['rxsteps'] = G.rxsteps
if is_subgrid:
# Write additional meta data about subgrid
basegrp.attrs['is_os_sep'] = G.is_os_sep
basegrp.attrs['pml_separation'] = G.pml_separation
basegrp.attrs['subgrid_pml_thickness'] = G.pmlthickness['x0']
basegrp.attrs['filter'] = G.filter
basegrp.attrs['ratio'] = G.ratio
basegrp.attrs['interpolation'] = G.interpolation
# Create group for sources (except transmission lines); add type and positional data attributes
srclist = G.voltagesources + G.hertziandipoles + G.magneticdipoles
for srcindex, src in enumerate(srclist):
grp = f.create_group('/srcs/src' + str(srcindex + 1))
grp = basegrp.create_group('srcs/src' + str(srcindex + 1))
grp.attrs['Type'] = type(src).__name__
grp.attrs['Position'] = (src.xcoord * G.dx, src.ycoord * G.dy, src.zcoord * G.dz)
# Create group for transmission lines; add positional data, line resistance and
# line discretisation attributes; write arrays for line voltages and currents
for tlindex, tl in enumerate(G.transmissionlines):
grp = f.create_group('/tls/tl' + str(tlindex + 1))
grp = basegrp.create_group('tls/tl' + str(tlindex + 1))
grp.attrs['Position'] = (tl.xcoord * G.dx, tl.ycoord * G.dy, tl.zcoord * G.dz)
grp.attrs['Resistance'] = tl.resistance
grp.attrs['dl'] = tl.dl
@@ -189,17 +176,15 @@ def write_data(outputfile, G):
grp['Vinc'] = tl.Vinc
grp['Iinc'] = tl.Iinc
# Save total voltage and current
f['/tls/tl' + str(tlindex + 1) + '/Vtotal'] = tl.Vtotal
f['/tls/tl' + str(tlindex + 1) + '/Itotal'] = tl.Itotal
basegrp['tls/tl' + str(tlindex + 1) + '/Vtotal'] = tl.Vtotal
basegrp['tls/tl' + str(tlindex + 1) + '/Itotal'] = tl.Itotal
# Create group, add positional data and write field component arrays for receivers
for rxindex, rx in enumerate(G.rxs):
grp = f.create_group('/rxs/rx' + str(rxindex + 1))
grp = basegrp.create_group('rxs/rx' + str(rxindex + 1))
if rx.ID:
grp.attrs['Name'] = rx.ID
grp.attrs['Position'] = (rx.xcoord * G.dx, rx.ycoord * G.dy, rx.zcoord * G.dz)
for output in rx.outputs:
f['/rxs/rx' + str(rxindex + 1) + '/' + output] = rx.outputs[output]
return f
basegrp['rxs/rx' + str(rxindex + 1) + '/' + output] = rx.outputs[output]

查看文件

@@ -35,7 +35,7 @@ import gprMax.config as config
from .cython.yee_cell_build import build_electric_components
from .cython.yee_cell_build import build_magnetic_components
from .exceptions import GeneralError
from .fields_outputs import write_hdf5_outputfiles
from .fields_outputs import write_hdf5_outputfile
from .grid import dispersion_analysis
from .hash_cmds_file import parse_hash_commands
from .materials import Material
@@ -213,8 +213,8 @@ class ModelBuildRun:
to file(s).
"""
# Write an output file(s) in HDF5 format
write_hdf5_outputfiles(config.get_model_config().output_file_path_ext, self.G)
# Write an output file in HDF5 format
write_hdf5_outputfile(config.get_model_config().output_file_path_ext, self.G)
# Write any snapshots to file
if self.G.snapshots:

查看文件

@@ -45,17 +45,36 @@ def mpl_plot(filename, outputs=Rx.defaultoutputs, fft=False):
file = Path(filename)
# Open output file and read some attributes
# Open output file and read iterations
f = h5py.File(file, 'r')
nrx = f.attrs['nrx']
dt = f.attrs['dt']
iterations = f.attrs['Iterations']
time = np.linspace(0, (iterations - 1) * dt, num=iterations)
# Paths to grid(s) to traverse for outputs
paths = ['/']
# Check if any subgrids and add path(s)
is_subgrids = "/subgrids" in f
if is_subgrids:
paths = paths + ['/subgrids/' + path + '/' for path in f['/subgrids'].keys()]
# Get number of receivers in grid(s)
nrxs = []
for path in paths:
if f[path].attrs['nrx'] > 0:
nrxs.append(f[path].attrs['nrx'])
else:
paths.remove(path)
# Check there are any receivers
if nrx == 0:
if not paths:
raise CmdInputError(f'No receivers found in {file}')
# Loop through all grids
for path in paths:
iterations = f[path].attrs['Iterations']
nrx = f[path].attrs['nrx']
dt = f[path].attrs['dt']
time = np.linspace(0, (iterations - 1) * dt, num=iterations)
# Check for single output component when doing a FFT
if fft:
if not len(outputs) == 1:
@@ -63,8 +82,8 @@ def mpl_plot(filename, outputs=Rx.defaultoutputs, fft=False):
# New plot for each receiver
for rx in range(1, nrx + 1):
path = '/rxs/rx' + str(rx) + '/'
availableoutputs = list(f[path].keys())
rxpath = path + 'rxs/rx' + str(rx) + '/'
availableoutputs = list(f[rxpath].keys())
# If only a single output is required, create one subplot
if len(outputs) == 1:
@@ -82,7 +101,7 @@ def mpl_plot(filename, outputs=Rx.defaultoutputs, fft=False):
if output not in availableoutputs:
raise CmdInputError(f"{output} output requested to plot, but the available output for receiver 1 is {', '.join(availableoutputs)}")
outputdata = f[path + output][:] * polarity
outputdata = f[rxpath + output][:] * polarity
# Plotting if FFT required
if fft:
@@ -100,7 +119,8 @@ def mpl_plot(filename, outputs=Rx.defaultoutputs, fft=False):
pltrange = np.s_[0:pltrange]
# Plot time history of output component
fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, num='rx' + str(rx),
fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2,
num=rxpath + ' - ' + f[rxpath].attrs['Name'],
figsize=(20, 10), facecolor='w',
edgecolor='w')
line1 = ax1.plot(time, outputdata, 'r', lw=2, label=outputtext)
@@ -141,8 +161,8 @@ def mpl_plot(filename, outputs=Rx.defaultoutputs, fft=False):
else:
fig, ax = plt.subplots(subplot_kw=dict(xlabel='Time [s]',
ylabel=outputtext + ' field strength [V/m]'),
num='rx' + str(rx), figsize=(20, 10),
facecolor='w', edgecolor='w')
num=rxpath + ' - ' + f[rxpath].attrs['Name'],
figsize=(20, 10), facecolor='w', edgecolor='w')
line = ax.plot(time, outputdata, 'r', lw=2, label=outputtext)
ax.set_xlim([0, np.amax(time)])
# ax.set_ylim([-15, 20])
@@ -158,8 +178,8 @@ def mpl_plot(filename, outputs=Rx.defaultoutputs, fft=False):
# If multiple outputs required, create all nine subplots and populate only the specified ones
else:
fig, ax = plt.subplots(subplot_kw=dict(xlabel='Time [s]'),
num='rx' + str(rx), figsize=(20, 10),
facecolor='w', edgecolor='w')
num=rxpath + ' - ' + f[rxpath].attrs['Name'],
figsize=(20, 10), facecolor='w', edgecolor='w')
if len(outputs) == 9:
gs = gridspec.GridSpec(3, 3, hspace=0.3, wspace=0.3)
else:
@@ -179,7 +199,7 @@ def mpl_plot(filename, outputs=Rx.defaultoutputs, fft=False):
if output not in availableoutputs:
raise CmdInputError(f"Output(s) requested to plot: {', '.join(outputs)}, but available output(s) for receiver {rx} in the file: {', '.join(availableoutputs)}")
outputdata = f[path + output][:] * polarity
outputdata = f[rxpath + output][:] * polarity
if output == 'Ex':
ax = plt.subplot(gs[0, 0])