openmc-dev / openmc

OpenMC Monte Carlo Code
https://docs.openmc.org
Other
772 stars 502 forks source link

get_microxs_and_flux fails with dagmc geometry #2940

Closed shimwell closed 7 months ago

shimwell commented 7 months ago

Bug Description

When running openmc.deplete.get_microxs_and_flux with a geometry that uses dagmc the dagmc h5m can't be found by the get_microxs_and_flux. I believe this is because get_microxs_and_flux runs in a tmp directory by default.

 Reading cross sections XML file...
 ERROR: Geometry DAGMC file 'dagmc.h5m' does not exist!
Traceback (most recent call last):
  File "/home/j/neutronics-workshop/tasks/task_18_CAD_shut_down_dose_rate/test.py", line 31, in <module>
    flux_in_each_voxel, micro_xs = openmc.deplete.get_microxs_and_flux(
  File "/home/j/openmc/openmc/deplete/microxs.py", line 134, in get_microxs_and_flux
    statepoint_path = model.run(**run_kwargs)
  File "/home/j/openmc/openmc/model/model.py", line 716, in run
    openmc.run(particles, threads, geometry_debug, restart_file,
  File "/home/j/openmc/openmc/executor.py", line 314, in run
    _run(args, output, cwd)
  File "/home/j/openmc/openmc/executor.py", line 125, in _run
    raise RuntimeError(error_msg)
RuntimeError: Geometry DAGMC file 'dagmc.h5m' does not exist!

Steps to Reproduce

import openmc
import openmc.deplete
import os

openmc.config['chain_file'] = '/home/j/chain-endf-b8.0.xml'
openmc.config['cross_sections'] = '/home/j/endf-b8.0-hdf5/endfb-viii.0-hdf5/cross_sections.xml'

my_material = openmc.Material(name='mat1', material_id=1)
my_material.add_nuclide("H1", 1, percent_type="ao")
my_material.set_density("g/cm3", 0.001)
my_materials = openmc.Materials([my_material])

universe = openmc.DAGMCUniverse("dagmc.h5m").bounded_universe()
my_geometry = openmc.Geometry(universe)

umesh = openmc.UnstructuredMesh("umesh.h5m", library="moab")

my_settings = openmc.Settings()
my_settings.batches = 5
my_settings.particles = 5000
my_settings.run_mode = "fixed source"

my_source = openmc.IndependentSource()
my_source.space = openmc.stats.Point(my_geometry.bounding_box.center)
my_source.energy = openmc.stats.Discrete([14e6], [1])
my_settings.source = my_source

model = openmc.model.Model(my_geometry, my_materials, my_settings)

flux_in_each_voxel, micro_xs = openmc.deplete.get_microxs_and_flux(
    model=model,
    domains=umesh,
    energies=[0, 30e6], # one energy bin from 0 to 30MeV
    chain_file=openmc.config['chain_file'],
    # needed otherwise the dagmc file is not found in the temp dir that mico runs
    # run_kwargs={'cwd':os.path.dirname(__file__)},
    nuclides=my_material.get_nuclides()
)

Alternative work around

we can add a key word to the get_microxs_and_flux method call run_kwargs={'cwd':os.path.dirname(__file__)},

Environment

openmc dev (updated yesterday)

eepeterson commented 7 months ago

@shimwell you can also just call Path.resolve on the h5m file name and it should get exported to model.xml in a way that works in the temp directory with no changes to run_kwargs

shimwell commented 7 months ago

Good point Ethan

adding resolve and changing these two lines

universe = openmc.DAGMCUniverse("dagmc.h5m").bounded_universe()
umesh = openmc.UnstructuredMesh("umesh.h5m", library="moab")

to this

from pathlib import Path
universe = openmc.DAGMCUniverse(Path("dagmc.h5m").resolve()).bounded_universe()
umesh = openmc.UnstructuredMesh(Path("umesh.h5m").resolve(), library="moab")

Solves the issue 🎉

I had a look into the get_microxs_and_flux and I think it would be difficult to make the function work for relative paths. So I think resolve is the best solution we will get.

I shall close this now but if anyone wants to reopen that is also fine.

shimwell commented 7 months ago

We could sneak in a check when the DAGMCUniverse and UnstructuredMesh objects are made. The check could be something like this ...

if Path(filename).is_relative()
    warnings.warn(f"path to DAGMC file {filename} is relative. It is recommended to use absolute / resolved paths when creating DAGMCUniverse to avoid potential errors when openmc runs using temporary directories")

What do people think is it worth adding a check like this to DAGMCUniverse and UnstructuredMesh?