diff --git a/src/ansys/fluent/core/filereader/case_file.py b/src/ansys/fluent/core/filereader/case_file.py index e867195bf9ab..6d7aa827b791 100644 --- a/src/ansys/fluent/core/filereader/case_file.py +++ b/src/ansys/fluent/core/filereader/case_file.py @@ -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: @@ -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 diff --git a/tests/test_casereader.py b/tests/test_casereader.py index acb1e64092a8..1ad411a88ce8 100644 --- a/tests/test_casereader.py +++ b/tests/test_casereader.py @@ -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") @@ -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"']