Skip to content
Merged
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
31 changes: 20 additions & 11 deletions src/ansys/fluent/core/filereader/case_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ def __init__(self, raw_data):
self.name = v.strip('"')
elif k == "definition":
self.value = v.strip('"')
if "[" in self.value:
sep_index = self.value.index("[")
if not self.value[sep_index - 1] == " ":
self.value = "".join(
(self.value[:sep_index], " ", self.value[sep_index:])
)

@property
def units(self) -> str:
Expand Down Expand Up @@ -183,32 +189,35 @@ def __init__(self, case_filepath: str = None, project_filepath: str = None):
raise FileNotFoundError(
"Please provide a valid fluent project file path"
)

try:
if "".join(Path(case_filepath).suffixes) == ".cas.h5":
if Path(case_filepath).match("*.cas.h5"):
file = h5py.File(case_filepath)
settings = file["settings"]
rpvars = settings["Rampant Variables"][0]
rp_vars_str = rpvars.decode()
elif Path(case_filepath).suffix == ".cas":
elif Path(case_filepath).match("*.cas"):
with open(case_filepath, "rb") as file:
rp_vars_str = file.read()
rp_vars_str = _get_processed_string(rp_vars_str)
elif "".join(Path(case_filepath).suffixes) == ".cas.gz":
elif Path(case_filepath).match("*.cas.gz"):
with gzip.open(case_filepath, "rb") as file:
rp_vars_str = file.read()
rp_vars_str = _get_processed_string(rp_vars_str)
else:
raise RuntimeError()
error_message = (
"Could not read case file. "
"Only valid Case files (.h5, .cas, .cas.gz) can be read. "
)
raise RuntimeError(error_message)

except FileNotFoundError as e:
raise RuntimeError(f"The case file {case_filepath} cannot be found.") from e
raise FileNotFoundError(
f"The case file {case_filepath} cannot be found."
) from e

except OSError:
error_message = (
"Could not read case file. "
"Only valid Case files (.h5, .cas, .cas.gz) can be read. "
)
raise RuntimeError(error_message)
except OSError as e:
raise OSError(f"Error while reading case file {case_filepath}") from e

except BaseException as e:
raise RuntimeError(f"Could not read case file {case_filepath}") from e
Expand Down
9 changes: 8 additions & 1 deletion tests/test_casereader.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def test_processed_string():


def test_casereader_no_file():
with pytest.raises(RuntimeError):
with pytest.raises(FileNotFoundError):
call_casereader("no_file.cas.h5")


Expand Down Expand Up @@ -224,6 +224,13 @@ def test_case_reader_input_parameter():
assert momentum.numeric_value == 12.4
assert momentum.value == "12.4 [kg m s^-1]"

velocity = InputParameter(raw_data=(("name", "v"), ("definition", "2[m/s]")))

assert velocity.name == "v"
assert velocity.units == "m/s"
assert velocity.numeric_value == 2
assert velocity.value == "2 [m/s]"


def test_lispy_for_multiline_string():
assert lispy.parse('(define x "abc\ndef")') == ["define", "x", '"abc\ndef"']