From 20877c61a1a4d6b9d26e73364c1a23b4dfb0b050 Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Fri, 1 Sep 2023 20:32:08 +0100 Subject: [PATCH] Adding helpful info to keyerror for StepResults.get_material (#2674) Co-authored-by: Paul Romano --- openmc/deplete/stepresult.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/openmc/deplete/stepresult.py b/openmc/deplete/stepresult.py index 9af6b5fee76..a3aacae41bb 100644 --- a/openmc/deplete/stepresult.py +++ b/openmc/deplete/stepresult.py @@ -214,11 +214,23 @@ def get_material(self, mat_id): ------- openmc.Material Equivalent material + + Raises + ------ + KeyError + If specified material ID is not found in the StepResult + """ with warnings.catch_warnings(): warnings.simplefilter('ignore', openmc.IDWarning) material = openmc.Material(material_id=int(mat_id)) - vol = self.volume[mat_id] + try: + vol = self.volume[mat_id] + except KeyError as e: + raise KeyError( + f'mat_id {mat_id} not found in StepResult. Available mat_id ' + f'values are {list(self.volume.keys())}' + ) from e for nuc, _ in sorted(self.index_nuc.items(), key=lambda x: x[1]): atoms = self[0, mat_id, nuc] if atoms < 0.0: