Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
e186c21
Added functionality to read casefile from a project path
prmukherj Jul 27, 2022
720cae6
Added the related test cases
prmukherj Jul 27, 2022
5330a52
Added support for .flprz format
prmukherj Jul 28, 2022
45c5014
Corrected test location for test
prmukherj Jul 28, 2022
c02f720
Skipped failing test
prmukherj Jul 28, 2022
adb4043
Updated case-reader to handle project file + updated associated test …
prmukherj Jul 28, 2022
c2163e7
Merge branch 'main' into feat/project_as_case_filepath
prmukherj Jul 28, 2022
65f32b8
changed directory creation from os to pathlib
prmukherj Jul 29, 2022
fddb2fe
Merge branch 'feat/project_as_case_filepath' of https://github.com/py…
prmukherj Jul 29, 2022
11ccdbb
Updated the copy instruction
prmukherj Jul 29, 2022
806f50a
used / in place of join
prmukherj Jul 31, 2022
6ec3e3a
Skipped the test for reading case file from project
prmukherj Jul 31, 2022
ec39bcc
Added a TODO for the failing test
prmukherj Jul 31, 2022
617ba3f
Updated glob for linux support
prmukherj Jul 31, 2022
57436b6
Merge branch 'main' into feat/project_as_case_filepath
prmukherj Aug 7, 2022
9b89955
Update src/ansys/fluent/core/filereader/casereader.py
prmukherj Aug 9, 2022
733e142
Update src/ansys/fluent/core/filereader/casereader.py
prmukherj Aug 9, 2022
80ab100
Made project filepath into a separate argument
prmukherj Aug 10, 2022
6e5f1f2
Additional tests to cover failures
prmukherj Aug 10, 2022
7a074e4
Merge branch 'main' into feat/project_as_case_filepath
prmukherj Aug 10, 2022
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
43 changes: 41 additions & 2 deletions src/ansys/fluent/core/filereader/casereader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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]
87 changes: 75 additions & 12 deletions tests/test_casereader.py
Original file line number Diff line number Diff line change
@@ -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()

Expand Down Expand Up @@ -37,44 +43,79 @@ 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"
)
)


def test_casereader_binary_cas():
call_casereader(
examples.download_file(
case_filepath=examples.download_file(
"Static_Mixer_Parameters_legacy_binary.cas", "pyfluent/static_mixer"
)
)


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"
)
)


def test_casereader_text_cas():
call_casereader(
examples.download_file(
case_filepath=examples.download_file(
"Static_Mixer_Parameters_legacy_text.cas", "pyfluent/static_mixer"
)
)


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 ))")
Expand All @@ -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")