From 5dc07cfdc0d748cc39861f657fcff268eaf1ac1d Mon Sep 17 00:00:00 2001 From: Jan Malec Date: Wed, 16 Oct 2024 12:44:47 +0200 Subject: [PATCH] Fix path handeling for thermal ace generAtion --- openmc/data/njoy.py | 28 +++++++++++++++++++++------- openmc/data/thermal.py | 14 +++++++++++--- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/openmc/data/njoy.py b/openmc/data/njoy.py index 1bf44891ef4..c4de8874a90 100644 --- a/openmc/data/njoy.py +++ b/openmc/data/njoy.py @@ -400,7 +400,7 @@ def make_ace(filename, temperatures=None, acer=True, xsdir=None, if acer: ace = (output_dir / "ace") if acer is True else Path(acer) xsdir = (ace.parent / "xsdir") if xsdir is None else xsdir - with ace.open('w') as ace_file, xsdir.open('w') as xsdir_file: + with open(ace, 'w') as ace_file, open(xsdir, 'w') as xsdir_file: for temperature in temperatures: # Get contents of ACE file text = (output_dir / f"ace_{temperature:.1f}").read_text() @@ -589,16 +589,30 @@ def make_ace_thermal(filename, filename_thermal, temperatures=None, commands += 'stop\n' run(commands, tapein, tapeout, **kwargs) - ace = output_dir / ace + # Determine the path for the final ACE and xsdir files + # If 'ace' is an absolute path, use it directly + # If 'ace' is a relative path, place it in the current directory + if not os.path.isabs(ace): + ace = os.path.abspath(ace) + + # Determine xsdir path similarly + if xsdir is None: + xsdir = os.path.join(os.path.dirname(ace), 'xsdir') + else: + if not os.path.isabs(xsdir): + xsdir = os.path.abspath(xsdir) + xsdir = (ace.parent / "xsdir") if xsdir is None else Path(xsdir) - with ace.open('w') as ace_file, xsdir.open('w') as xsdir_file: + with open(ace, 'w') as ace_file, open(xsdir, 'w') as xsdir_file: # Concatenate ACE and xsdir files together for temperature in temperatures: - ace_in = output_dir / f"ace_{temperature:.1f}" - ace_file.write(ace_in.read_text()) + ace_in = os.path.join(output_dir, f"ace_{temperature:.1f}") + with open(ace_in, 'r') as f: + ace_file.write(f.read()) - xsdir_in = output_dir / f"xsdir_{temperature:.1f}" - xsdir_file.write(xsdir_in.read_text()) + xsdir_in = os.path.join(output_dir, f"xsdir_{temperature:.1f}") + with open(xsdir_in, 'r') as f: + xsdir_file.write(f.read()) # Remove ACE/xsdir files for each temperature for temperature in temperatures: diff --git a/openmc/data/thermal.py b/openmc/data/thermal.py index 6df171d3a4f..8ca80328cbb 100644 --- a/openmc/data/thermal.py +++ b/openmc/data/thermal.py @@ -839,13 +839,22 @@ def from_njoy(cls, filename, filename_thermal, temperatures=None, with tempfile.TemporaryDirectory() as tmpdir: # Run NJOY to create an ACE library kwargs.setdefault('output_dir', tmpdir) - kwargs.setdefault('ace', os.path.join(kwargs['output_dir'], 'ace')) + + # Get the ace filename from kwargs or default to 'ace' + ace_filename = kwargs.get('ace', 'ace') + kwargs['ace'] = ace_filename + kwargs['evaluation'] = evaluation kwargs['evaluation_thermal'] = evaluation_thermal make_ace_thermal(filename, filename_thermal, temperatures, **kwargs) + # Read the ACE file from the correct path + ace_path = kwargs['ace'] + if not os.path.isabs(ace_path): + ace_path = os.path.abspath(ace_path) + + lib = Library(ace_path) # Create instance from ACE tables within library - lib = Library(kwargs['ace']) name = kwargs.get('table_name') data = cls.from_ace(lib.tables[0], name=name) for table in lib.tables[1:]: @@ -867,7 +876,6 @@ def from_njoy(cls, filename, filename_thermal, temperatures=None, if isinstance(rx_endf.xs[t], (IncoherentElastic, Sum)): rx.xs[t] = rx_endf.xs[t] rx.distribution[t] = rx_endf.distribution[t] - return data @classmethod