Repository: watts-dev/watts (language: python)

Issue:
OpenMC required for tests
I'm over from JOSS looking at this repo to make sure everything checks out. I've looked over the docs quickly and everything looks really clean and understandable. I need to get the tests running on my laptop and I'm running into some issues that I'm hoping to get help with.

I've installed  `watts` from source, and when I run `pytest tests` I get the following (relevant snippet):
```
ImportError while importing test module '/tmp/watts/tests/test_plugin_openmc.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/lib/python3.8/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
tests/test_plugin_openmc.py:7: in <module>
    import openmc
E   ModuleNotFoundError: No module named 'openmc'
```

I'm using conda, so I followed the install instructions here `https://docs.openmc.org/en/stable/quickinstall.html` and installed from mamba, and even still I get the same error. I've checked with `python3 -c "import openmc; print(openmc.__version__)"` to confirm that openmc is indeed installed. Any advice on this is much appreciated.

Linking [https://github.com/openjournals/joss-reviews/issues/4735]


Discussion / hints:
@yadudoc The only thing I can think is that when you call `pytest`, it's picking it up from a different Python environment, not the conda environment that has OpenMC installed in it. Perhaps try running `python3 -m pytest tests` instead?
@paulromano That definitely helped. I'm getting new errors now and the error message and troubleshooting guide over on OpenMC seems to indicate that I'm missing the `OPENMC_CROSS_SECTIONS` env var and an XML file.

Have you considered dropping a mock XML file into tests and setting the required env var from conftests? 
Here's a snippet of the error:

```
tests/test_plugin_openmc.py FFF  
...
                error_msg = 'OpenMC aborted unexpectedly.'
            error_msg = ' '.join(error_msg.split())
    
>           raise RuntimeError(error_msg)
E           RuntimeError: No cross_sections.xml file was specified in materials.xml or in the OPENMC_CROSS_SECTIONS environment variable. OpenMC needs such a file to identify where to find data libraries. Please consult the user's guide at https://docs.openmc.org/ for information on how to set up data libraries.

/home/yadu/anaconda3/envs/watts/lib/python3.10/site-packages/openmc/executor.py:116: RuntimeError

```


Fix patch:
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index e4638559..4c1bd15f 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -38,4 +38,4 @@ jobs:
 
       - name: Test
         shell: bash
-        run: pytest tests
+        run: pytest -rs tests


Test patch:
diff --git a/tests/Am244.h5 b/tests/Am244.h5
new file mode 100644
index 00000000..d19edb04
Binary files /dev/null and b/tests/Am244.h5 differ
diff --git a/tests/test_plugin_openmc.py b/tests/test_plugin_openmc.py
index 54dc123d..18a6cce1 100644
--- a/tests/test_plugin_openmc.py
+++ b/tests/test_plugin_openmc.py
@@ -2,25 +2,35 @@
 # SPDX-License-Identifier: MIT
 
 from pathlib import Path
+import os
 
 import pytest
-import openmc
 import watts
 
+openmc = pytest.importorskip('openmc')
+
+
+@pytest.fixture(scope='module', autouse=True)
+def data_library():
+    """Create data library with only Am244"""
+    lib = openmc.data.DataLibrary()
+    lib.register_file(Path(__file__).with_name('Am244.h5'))
+    lib.export_to_xml()
+
+    # Temporarily set cross sections to the library just created
+    os.environ['OPENMC_CROSS_SECTIONS'] = str(Path.cwd() / 'cross_sections.xml')
+
 
 def build_openmc_model(params):
     model = openmc.model.Model()
 
-    pu_metal = openmc.Material()
-    pu_metal.set_density('sum')
-    pu_metal.add_nuclide('Pu239', 3.7047e-02)
-    pu_metal.add_nuclide('Pu240', 1.7512e-03)
-    pu_metal.add_nuclide('Pu241', 1.1674e-04)
-    pu_metal.add_element('Ga', 1.3752e-03)
-    model.materials.append(pu_metal)
+    am244 = openmc.Material()
+    am244.set_density('sum')
+    am244.add_nuclide('Am244', 0.02)
+    model.materials.append(am244)
 
     sph = openmc.Sphere(r=params['radius'], boundary_type='vacuum')
-    cell = openmc.Cell(fill=pu_metal, region=-sph)
+    cell = openmc.Cell(fill=am244, region=-sph)
     model.geometry = openmc.Geometry([cell])
 
     model.settings.batches = 50
@@ -39,12 +49,12 @@ def test_openmc_plugin():
     plugin = watts.PluginOpenMC(build_openmc_model)
     assert plugin.model_builder == build_openmc_model
 
-    params = watts.Parameters(radius=6.38)
+    params = watts.Parameters(radius=11.45)
     result = plugin(params, name="OpenMC run")
 
     # Sanity checks
     assert isinstance(result, watts.ResultsOpenMC)
-    assert result.parameters['radius'] == 6.38
+    assert result.parameters['radius'] == 11.45
     assert result.name == "OpenMC run"
     assert len(result.statepoints) == 5
     input_names = {p.name for p in result.inputs}
@@ -77,11 +87,11 @@ def test_openmc_plugin():
 
 
 @pytest.fixture
-def pu_model():
-    """Model of a 10 cm sphere of Pu239"""
+def am_model():
+    """Model of a 10 cm sphere of Am244"""
     model = openmc.model.Model()
     mat = openmc.Material()
-    mat.add_nuclide('Pu239', 1.0)
+    mat.add_nuclide('Am244', 1.0)
     mat.set_density('g/cm3', 10.0)
     sph = openmc.Sphere(r=10.0, boundary_type='vacuum')
     cell = openmc.Cell(fill=mat, region=-sph)
@@ -92,9 +102,9 @@ def pu_model():
     return model
 
 
-def test_extra_inputs(run_in_tmpdir, pu_model):
+def test_extra_inputs(run_in_tmpdir, am_model):
+    am_model.export_to_xml()
     # Export model to XML
-    pu_model.export_to_xml()
 
     # Use OpenMC plugin with extra inputs
     plugin = watts.PluginOpenMC(extra_inputs=['geometry.xml', 'materials.xml', 'settings.xml'])
@@ -105,14 +115,14 @@ def test_extra_inputs(run_in_tmpdir, pu_model):
     assert input_names == {'geometry.xml', 'materials.xml', 'settings.xml'}
 
 
-def test_arbitrary_function(run_in_tmpdir, pu_model):
+def test_arbitrary_function(run_in_tmpdir, am_model):
     # Add a plot to the model and export
     plot = openmc.Plot()
     plot.filename = 'watts_plot'
     plot.pixels = (100, 100)
     plot.width = (20.0, 20.0)
-    pu_model.plots.append(plot)
-    pu_model.export_to_xml()
+    am_model.plots.append(plot)
+    am_model.export_to_xml()
 
     # Use OpenMC plugin to run a geometry plot
     plugin = watts.PluginOpenMC(extra_inputs=Path.cwd().glob('*.xml'))