Repository: openmc-dev/openmc

Issue:
High Temperature key error in from_njoy()
When using **openmc.data.IncidentNeutron.from_njoy()**, for high temperatures a python KeyError may arise. 

For what I can say the problem arises from the following sequence of actions performed by this routine (in file neutron.py): 

- Input temperature is taken in Kelvin. 

- Temperature is converted to MeV and written to an .ace file by **njoy**, rounded to something like 6 digits.

- Data from the .ace file is read in by OpenMC and temperature is converted from MeV to K and rounded to an int - for temperatures above approx. 1e5 K this is not the same as the temperature int from the input.
`data = cls.from_ace(lib.tables[0])`
`data.add_temperature_from_ace(table)`
This temperature is stored in data.temperatures.


- A key for the data table is created using the "new" temperature after reading and rounding - but at a later point OpenMC tries to access the data table with a key from an evaluation object which seems to hold the "old" temperature from the input (or maybe it has been converted back and forth with higher precision, so rounding does not have an impact for the numbers I tested)
`temp = "{}K".format(round(ev.target["temperature"]))`
`kerma = data.reactions[301].xs[temp]`

- Consequently, a python KeyError arises.

As a workaround, one can use **openmc.data.njoy.make_ace** and subsequently **openmc.data.IncidentNeutron.from_ace** (in this case, the data table is created and accessed with the same key from the .ace file). 

Nevertheless, I suppose that the observed behaviour is not intended, so you might want to have a look at that.


Discussion:
Thanks for reporting this @teutebergt! Should be an easy fix; I'll try to get a PR submitted shortly. However, do note that NJOY itself also has some issues with [high temperatures](https://github.com/njoy/NJOY2016/issues/222).

Resolving patch:
diff --git a/openmc/data/neutron.py b/openmc/data/neutron.py
--- a/openmc/data/neutron.py
+++ b/openmc/data/neutron.py
@@ -868,9 +868,8 @@ def get_file3_xs(ev, mt, E):
 
             heatr_evals = get_evaluations(kwargs["heatr"])
             heatr_local_evals = get_evaluations(kwargs["heatr"] + "_local")
-            for ev, ev_local in zip(heatr_evals, heatr_local_evals):
-                temp = "{}K".format(round(ev.target["temperature"]))
 
+            for ev, ev_local, temp in zip(heatr_evals, heatr_local_evals, data.temperatures):
                 # Get total KERMA (originally from ACE file) and energy grid
                 kerma = data.reactions[301].xs[temp]
                 E = kerma.x


Test patch:
diff --git a/tests/unit_tests/test_data_neutron.py b/tests/unit_tests/test_data_neutron.py
--- a/tests/unit_tests/test_data_neutron.py
+++ b/tests/unit_tests/test_data_neutron.py
@@ -531,3 +531,12 @@ def test_ace_table_types():
     assert TT.from_suffix('20t') == TT.THERMAL_SCATTERING
     with pytest.raises(ValueError):
         TT.from_suffix('z')
+
+
+@needs_njoy
+def test_high_temperature():
+    endf_data = os.environ['OPENMC_ENDF_DATA']
+    endf_file = os.path.join(endf_data, 'neutrons', 'n-001_H_001.endf')
+
+    # Ensure that from_njoy works when given a high temperature
+    openmc.data.IncidentNeutron.from_njoy(endf_file, temperatures=[123_456.0])