<jupyter_start><jupyter_text>Post processingThe purpose of this notebook is to present some of the ways `hydep` can be used to process its own results. At least those generated by the default `h5py` storage system.    **Note**    This notebook requires the output file generated by [the pincell example](pincell.ipynb)and `matplotlib`<jupyter_code>import numpy
from matplotlib import pyplot
from hydep.hdf import Processor
pincell = Processor("./pincell-results.h5")<jupyter_output><empty_output><jupyter_text>Basic accessThe `HdfProcessor` is designed to sit as a convenience layer above the `h5py` API. It can be thought of as a read-only dictionary into the underlying results with the following methods<jupyter_code>pincell.keys()
pincell["fluxes"]  # time step, material, group [single group for now]
pincell["time"].keys()
pincell.get("time/time")<jupyter_output><empty_output><jupyter_text>Our reduced order code simply returned the previous high-fidelity flux, meaning there will be no change from step zero to step 1, even though there are multiple "reduced-order" solutions. We can use the `time/highFidelity` mask to note where our high-fidelity solution was run, even though it may be obvious.<jupyter_code>pyplot.plot(
    pincell["time/time"],
    pincell["fluxes"][..., 0, 0], 
    "-o",
)
for t, f in zip(pincell["time/time"], pincell["time/highFidelity"]):
    if f:
        pyplot.axvline(t, color="gray", linestyle="--")
pyplot.xlabel("Time [s]")
pyplot.ylabel("Scalar flux [n/cm^2/s]");<jupyter_output><empty_output><jupyter_text>When examining $k$, each time step has the possibility to present a value and the associated standard deviation. Some reduced-order solvers, like the SFV interface, may not provide an estimate on $k$ and thus the array will contain some `nan` elements<jupyter_code>pincell["multiplicationFactor"]  # time step, (value, absolute uncertainty pair)
pincell["multiplicationFactor"][:, 0]<jupyter_output><empty_output><jupyter_text>Some indexing tricks can be performed to extract just the values of $k$ computed by the high-fidelity solution<jupyter_code>pincell["multiplicationFactor"][pincell["time/highFidelity"][:], :]
pincell["compositions"]  # (timestep, material, isotope)
pincell["isotopes/zais"]<jupyter_output><empty_output><jupyter_text>When storing strings in HDF files, they must be converted to character arrays and thus, upon slicing and selecting, must be decoded<jupyter_code>pincell["isotopes/names"][-5:]
list(b.decode() for b in pincell["isotopes/names"][-5:])<jupyter_output><empty_output><jupyter_text>Using the `zais` and `names` arrays, one could determine the indices that correspond to specific isotopes of interest. The `zais` vector is sorted, so `numpy.searchsorted` can work quite well<jupyter_code>ix = numpy.searchsorted(pincell["isotopes/zais"], 922350)
pyplot.plot(pincell["time/time"], pincell["compositions"][..., ix], "o")
pyplot.xlabel("Time [s]")
pyplot.ylabel("$^{235}$U atom density [atoms/b-cm]")<jupyter_output><empty_output><jupyter_text>This process can be applied for multiple isotopes as well    **Warning**If this approach is taken, note that the indices must be sorted in increasing order if indexing directly from the `h5py` dataset.<jupyter_code>ix = numpy.searchsorted(pincell["isotopes/zais"], [621490, 541350])
try:
    print(pincell["isotopes/names"][ix])
except TypeError as te:
    import traceback
    traceback.print_exc()<jupyter_output>Traceback (most recent call last):
  File "/home/drew/venv/hydep/lib/python3.8/site-packages/h5py/_hl/selections.py", line 85, in select
    int(a)
TypeError: only size-1 arrays can be converted to Python scalars

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-18-42138b97845a>", line 2, in <module>
    print(pincell["isotopes/names"][ix])
  File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
  File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
  File "/home/drew/venv/hydep/lib/python3.8/site-packages/h5py/_hl/dataset.py", line 553, in __getitem__
    selection = sel.select(self.shape, args, dsid=self.id)
  File "/home/drew/venv/hydep/lib/python3.8/site-packages/h5py/_hl/selections.py", line 90, in select
    sel[args]
  File "/home/drew/venv/hydep/lib/python3.8/site-packages/h5py/_hl/selections.py", line 367, in __getitem__
    raise TypeError("Indexing elements must be [...]<jupyter_text>We can instead read in the entire array and then use the index vector to select the isotopes of interest<jupyter_code>pyplot.plot(pincell["time/time"], pincell["compositions"][:, 0, :][..., ix])
pyplot.legend([n.decode() for n in pincell["isotopes/names"][:][ix]])
pyplot.xlabel("Time [s]")
pyplot.ylabel("Atom density [atoms/b-cm]");<jupyter_output><empty_output><jupyter_text>Convenience methodsThe processor has some default methods and attributes that should make processing easier. First, the time in days and string names of isotopes can be directly accessed from the processor<jupyter_code>pincell.days
pincell.names[-5:]<jupyter_output><empty_output><jupyter_text>Many of the datasets can also be accessed through attributes<jupyter_code>pincell.fluxes
pincell.compositions
pincell.keff
pincell.hfFlags<jupyter_output><empty_output><jupyter_text>When extracting $k$, the `getKeff` method can be used to automatically drop any values that originate from the reduced-order code. Additionally, the time returned is in days not in seconds<jupyter_code>d, k = pincell.getKeff(hfOnly=True)
pyplot.errorbar(d, k[:, 0], yerr=k[:, 1])<jupyter_output><empty_output><jupyter_text>`getDensities` can be used to extract atom densities for select isotopes, optionally at select time points as well. Since we have a 3D array with the material dimension having a single value, we can squeeze the data to be 2D<jupyter_code>adens = pincell.getDensities(names=["Xe135", "Sm149"], days=pincell.days[[0, -1]])
adens.shape
pyplot.plot(pincell.days[[0, 1]], adens.squeeze())
pyplot.xlabel("Time [d]")
pyplot.ylabel("Atom density [atoms/b-cm]")
pyplot.legend(["Xe135", "Sm149"]);<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11972059779077322, 'fraction_numerical': 0.015919428200129954, 'mean_word_length': 6.126157407407407, 'pattern_counts': {'":': 0, '<': 54, '<?xml version=': 0, '>': 54, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '321267', 'license': 'MIT', 'n_tokens_mistral': 2113, 'n_tokens_neox': 2019, 'n_words': 594, 'path': 'docs/examples/post-processing.ipynb', 'repo_name': 'CORE-GATECH-GROUP/hydep', 'size': '6156'}