diff --git a/src/ansys/fluent/core/filereader/casereader.py b/src/ansys/fluent/core/filereader/casereader.py index 13fe19d636ee..88b270752247 100644 --- a/src/ansys/fluent/core/filereader/casereader.py +++ b/src/ansys/fluent/core/filereader/casereader.py @@ -15,7 +15,10 @@ output_parameters = reader.output_parameters() """ import codecs +import glob import gzip +import itertools +from os.path import dirname from pathlib import Path from typing import List @@ -76,8 +79,16 @@ class CaseReader: Get the precision (1 or 2 for 1D of 2D) """ - def __init__(self, case_filepath: str): - + def __init__(self, case_filepath: str = None, project_filepath: str = None): + if case_filepath and project_filepath: + raise RuntimeError( + "Please enter either the case file path or the project file path" + ) + if project_filepath: + if Path(project_filepath).suffix in [".flprj", ".flprz"]: + case_filepath = _get_case_filepath(dirname(project_filepath)) + else: + raise RuntimeError("Please provide a valid fluent project file path") try: if "".join(Path(case_filepath).suffixes) == ".cas.h5": file = h5py.File(case_filepath) @@ -170,3 +181,31 @@ def _get_processed_string(input_string: bytes) -> str: rp_vars_str = codecs.decode(input_string, errors="ignore") string_identifier = "(37 (" return string_identifier + rp_vars_str.split(string_identifier)[1] + + +def _get_case_filepath(project_dir_path: str) -> str: + """Gets case file path within the provided project directory path. + + Parameters + ---------- + project_dir_path : str + The directory containing the case file + + Returns + ------- + case file path (str) + """ + file_list = list( + itertools.chain( + *( + glob.glob(project_dir_path + r"/**/**-Solve/*.%s" % ext) + for ext in ["cas", "cas.h5", "cas.gz"] + ) + ) + ) + if len(file_list) < 1: + raise RuntimeError(f"No case files are present in: {project_dir_path}") + elif len(file_list) > 1: + raise RuntimeError(f"More than one case file is present in: {project_dir_path}") + else: + return file_list[0] diff --git a/tests/test_casereader.py b/tests/test_casereader.py index 91a21d5995be..43df43e53ce0 100644 --- a/tests/test_casereader.py +++ b/tests/test_casereader.py @@ -1,10 +1,16 @@ +from os.path import dirname, join +import pathlib +import shutil + +import pytest + from ansys.fluent.core import examples from ansys.fluent.core.filereader.casereader import CaseReader, _get_processed_string -def call_casereader(case_filepath: str): +def call_casereader(case_filepath: str = None, project_filepath: str = None): - reader = CaseReader(case_filepath=case_filepath) + reader = CaseReader(case_filepath=case_filepath, project_filepath=project_filepath) input_parameters = reader.input_parameters() @@ -37,7 +43,7 @@ def call_casereader(case_filepath: str): def test_casereader_h5(): call_casereader( - examples.download_file( + case_filepath=examples.download_file( "Static_Mixer_Parameters.cas.h5", "pyfluent/static_mixer" ) ) @@ -45,7 +51,7 @@ def test_casereader_h5(): def test_casereader_binary_cas(): call_casereader( - examples.download_file( + case_filepath=examples.download_file( "Static_Mixer_Parameters_legacy_binary.cas", "pyfluent/static_mixer" ) ) @@ -53,7 +59,7 @@ def test_casereader_binary_cas(): def test_casereader_binary_gz(): call_casereader( - examples.download_file( + case_filepath=examples.download_file( "Static_Mixer_Parameters_legacy_binary.cas.gz", "pyfluent/static_mixer" ) ) @@ -61,7 +67,7 @@ def test_casereader_binary_gz(): def test_casereader_text_cas(): call_casereader( - examples.download_file( + case_filepath=examples.download_file( "Static_Mixer_Parameters_legacy_text.cas", "pyfluent/static_mixer" ) ) @@ -69,12 +75,47 @@ def test_casereader_text_cas(): def test_casereader_text_gz(): call_casereader( - examples.download_file( + case_filepath=examples.download_file( "Static_Mixer_Parameters_legacy_text.cas.gz", "pyfluent/static_mixer" ) ) +def create_dir_structure_locally(copy_1: bool = False, copy_2: bool = False): + # Copying from and then creating the entire directory structure locally + case_file_dir = ( + "Static_Mixer_Parameter_project_file/" + "Static_Mixer_Parameters.cffdb/Static_Mixer_Parameters-Solve" + ) + case_filepath = examples.download_file( + "Static_Mixer_Parameters.cas.h5", "pyfluent/static_mixer/" + case_file_dir + ) + prj_dir = join(dirname(case_filepath), case_file_dir) + pathlib.Path(prj_dir).mkdir(parents=True, exist_ok=True) + if copy_1: + shutil.copy2(case_filepath, prj_dir) + if copy_2: + case_filepath_2 = examples.download_file( + "Static_Mixer_Parameters_legacy_binary.cas.gz", "pyfluent/static_mixer" + ) + shutil.copy2(case_filepath_2, prj_dir) + prj_file_dir = "Static_Mixer_Parameter_project_file" + prj_file = r"Static_Mixer_Parameters.flprj" + prj_filepath = examples.download_file( + prj_file, "pyfluent/static_mixer/" + prj_file_dir + ) + prj_file_dir = join(dirname(prj_filepath), prj_file_dir) + shutil.copy2(prj_filepath, prj_file_dir) + + return join(prj_file_dir, prj_file) + + +def test_casereader_h5_for_project_directory(): + project_filepath = create_dir_structure_locally(copy_1=True) + call_casereader(project_filepath=project_filepath) + shutil.rmtree(dirname(project_filepath)) + + def test_processed_string(): assert ( _get_processed_string(b"Hello! World (37 ( Get this part of the string ))") @@ -83,9 +124,31 @@ def test_processed_string(): def test_casereader_no_file(): - throws = False - try: + with pytest.raises(RuntimeError): call_casereader("no_file.cas.h5") - except RuntimeError: - throws = True - assert throws + + +def test_casereader_with_both_project_and_case_file(): + with pytest.raises(RuntimeError): + call_casereader( + case_filepath="case_file.cas.h5", project_filepath="project.flprj" + ) + + +def test_casereader_for_project_directory_no_case_file(): + project_filepath = create_dir_structure_locally() + with pytest.raises(RuntimeError): + call_casereader(project_filepath=project_filepath) + shutil.rmtree(dirname(project_filepath)) + + +def test_casereader_for_project_directory_dual_case_file(): + project_filepath = create_dir_structure_locally(copy_1=True, copy_2=True) + with pytest.raises(RuntimeError): + call_casereader(project_filepath=project_filepath) + shutil.rmtree(dirname(project_filepath)) + + +def test_casereader_for_project_directory_invalid_project_file(): + with pytest.raises(RuntimeError): + call_casereader(project_filepath="project.flprx")