Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 46 additions & 4 deletions src/openmc_mcnp_adapter/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from copy import deepcopy
from math import pi
import re
import os

import numpy as np

Expand All @@ -24,6 +25,15 @@
""", re.VERBOSE
)

_READ_RE = re.compile(r"""
\s*read # Keyword
\s.*?file # Everything up to filename
\s*=\s* # = sign (required) with optional spaces
(\S+) # The file name is anything without whitespace
.* # Anything else until end-of-line
""", re.IGNORECASE | re.VERBOSE
)
Comment on lines +28 to +35

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not going to be perfectly robust in that it may be overly generous with what counts as a read, but I think it's probably robust enough.


_CELL1_RE = re.compile(r'\s*(\d+)\s+(\d+)([ \t0-9:#().dDeE\+-]+)\s*(.*)')
_CELL2_RE = re.compile(r'\s*(\d+)\s+like\s+(\d+)\s+but\s*(.*)')
_CELL_FILL_RE = re.compile(r'\s*(\d+)\s*(?:\((.*)\))?')
Expand Down Expand Up @@ -252,22 +262,51 @@ def parse_data(section):
return data


def split_mcnp(filename):
"""Split MCNP file into three strings, one for each block
def read_file(filename):
"""Recursively read the MCNP input file and files referenced by READ cards

READ card keywords other than FILE are ignored.

Parameters
----------
filename : str
Path to MCNP file

Returns
-------
str
Text of the MCNP input file

"""
directory = os.path.dirname(os.path.abspath(filename))
with open(filename, 'r') as fh:
text = fh.read()
for match in tuple(_READ_RE.finditer(text)):
card = match[0].strip()
target = os.path.join(directory, match[1])
if not os.path.isfile(target):
errstr = f"In card {repr(card)}, failed to find: {target}"
raise FileNotFoundError(errstr)
subtext = read_file(target)
text = text.replace(card, subtext)
return text


def split_mcnp(text):
"""Split MCNP file into three strings, one for each block

Parameters
----------
text : str
Text of the MCNP input file

Returns
-------
list of str
List containing one string for each block

"""
# Find beginning of cell section
text = open(filename, 'r').read()
m = re.search(r'^[ \t]*(\d+)[ \t]+', text, flags=re.MULTILINE)
text = text[m.start():]
return re.split('\n[ \t]*\n', text)
Expand Down Expand Up @@ -331,8 +370,11 @@ def parse(filename):
Dictionary containing data-block information, including materials

"""
# Read the text of the file and any referenced files into memory
text = read_file(filename)

# Split file into main three sections (cells, surfaces, data)
sections = split_mcnp(filename)
sections = split_mcnp(text)

# Sanitize lines (remove comments, continuation lines, etc.)
cell_section = sanitize(sections[0])
Expand Down
7 changes: 7 additions & 0 deletions tests/inputs/testRead.imcnp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Testing read card
C this is a comment
read echo file=testReadTarget.imcnp encode

1 so 0.5

mode n
1 change: 1 addition & 0 deletions tests/inputs/testReadRecursive.imcnp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2 0 +1 $ lowest level
9 changes: 9 additions & 0 deletions tests/inputs/testReadReference.imcnp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Testing read card
C this is a comment
1 0 -1
2 0 +1 $ lowest level
c

1 so 0.5

mode n
3 changes: 3 additions & 0 deletions tests/inputs/testReadTarget.imcnp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
1 0 -1
read file=testReadRecursive.imcnp
c
29 changes: 29 additions & 0 deletions tests/test_read_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import os
import textwrap
import pytest

from openmc_mcnp_adapter import mcnp_str_to_model, mcnp_to_model
from openmc_mcnp_adapter.parse import read_file

here = os.path.dirname(os.path.abspath(__file__))
input_dir = os.path.join(here, "inputs")


def test_read_not_found():
deck = textwrap.dedent("""
title
c The next line points to an invalid file
read file=/badfile.path
""")
with pytest.raises(FileNotFoundError):
mcnp_str_to_model(deck)


def test_read_recursive():
reference = read_file(os.path.join(input_dir, "testReadReference.imcnp"))
trial = read_file(os.path.join(input_dir, "testRead.imcnp"))
assert trial == reference


def test_recursive_mcnp_to_model():
mcnp_to_model(os.path.join(input_dir, "testRead.imcnp"))