idaholab / MontePy

MontePy is the most user friendly Python library (API) to read, edit, and write MCNP input files.
https://www.montepy.org/
MIT License
30 stars 6 forks source link

'Read' input can have a 404 error #404

Open MicahGale opened 5 months ago

MicahGale commented 5 months ago

TODO: more research.

I was working a model that probably worked in MCNP. It seems like the paths are relative to the cwd and not the input file.

MicahGale commented 4 months ago

Here's a MWE:

The file tree:

|- in
|  |- foo.imcnp
| - cell
|  |- bar.imcnp

Where foo.imcnp has:

header
read file=cell/bar.imcnp

The from the root directory

problem = montepy.read_input("in/foo.imcnp") 

leads to:

---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
Cell In[2], line 1
----> 1 montepy.read_input("in/foo.imcnp")

File ~/mambaforge/lib/python3.10/site-packages/montepy/input_parser/input_reader.py:29, in read_input(input_file, mcnp_version, replace)
     27 problem = mcnp_problem.MCNP_Problem(input_file)
     28 problem.mcnp_version = mcnp_version
---> 29 problem.parse_input(replace=replace)
     30 return problem

File ~/mambaforge/lib/python3.10/site-packages/montepy/mcnp_problem.py:255, in MCNP_Problem.parse_input(self, check_input, replace)
    246 OBJ_MATCHER = {
    247     block_type.BlockType.CELL: (Cell, self._cells),
    248     block_type.BlockType.SURFACE: (
   (...)
    252     block_type.BlockType.DATA: (parse_data, self._data_inputs),
    253 }
    254 try:
--> 255     for i, input in enumerate(
    256         input_syntax_reader.read_input_syntax(
    257             self._input_file, self.mcnp_version, replace=replace
    258         )
    259     ):
    260         self._original_inputs.append(input)
    261         if i == 0 and isinstance(input, mcnp_input.Message):

File ~/mambaforge/lib/python3.10/site-packages/montepy/input_parser/input_syntax_reader.py:42, in read_input_syntax(input_file, mcnp_version, replace)
     40 with input_file.open("r", replace=replace) as fh:
     41     yield from read_front_matters(fh, mcnp_version)
---> 42     yield from read_data(fh, mcnp_version)

File ~/mambaforge/lib/python3.10/site-packages/montepy/input_parser/input_syntax_reader.py:214, in read_data(fh, mcnp_version, block_type, recursion)
    212 block_type, file_name, parent = reading_queue.popleft()
    213 new_wrapper = MCNP_InputFile(os.path.join(path, file_name), parent)
--> 214 with new_wrapper.open("r") as sub_fh:
    215     new_wrapper = MCNP_InputFile(file_name, parent)
    216     for input in read_data(sub_fh, mcnp_version, block_type, True):

File ~/mambaforge/lib/python3.10/site-packages/montepy/input_parser/input_file.py:93, in MCNP_InputFile.open(self, mode, encoding, replace)
     91         encoding = None
     92 self._mode = mode
---> 93 self._fh = open(self.path, mode, encoding=encoding)
     94 return self

FileNotFoundError: [Errno 2] No such file or directory: 'in/cell/bar.imcnp'
MicahGale commented 4 months ago

So far 5.13.10.1.1 of the 6.3 manual and 3.1 of the 6.2 manual don't provide really any guidance on how filename is interpreted.

MicahGale commented 3 months ago

The more I think about it; like how web servers work, etc. the relative paths should be relative to the root of execution.