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
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"appdirs>=1.4.0",
"pandas>=1.1.5",
"h5py>=3.8.0",
"lxml>=4.9.2",
"pyyaml",
]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
output_parameters = reader.output_parameters()
"""
import codecs
import glob
import gzip
import itertools
import os
from os.path import dirname
from pathlib import Path
from typing import List
import xml.etree.ElementTree as ET

import h5py
from lxml import etree

from ansys.fluent.core.solver.error_message import allowed_name_error_message

Expand Down Expand Up @@ -126,7 +127,7 @@ def __getattr__(self, name: str):
return _CaseVariable(self._variables, name + "/")


class Case:
class CaseFile:
"""Class to read a Fluent case file.

Methods
Expand Down Expand Up @@ -170,9 +171,17 @@ def __init__(self, case_filepath: str = None, project_filepath: str = None):
)
if project_filepath:
if Path(project_filepath).suffix in [".flprj", ".flprz"]:
case_filepath = _get_case_filepath(dirname(project_filepath))
project_dir = os.path.join(
dirname(project_filepath),
Path(project_filepath).name.split(".")[0] + ".cffdb",
)
case_filepath = Path(
project_dir + _get_case_filepath_from_flprj(project_filepath)
)
else:
raise RuntimeError("Please provide a valid fluent project file path")
raise FileNotFoundError(
"Please provide a valid fluent project file path"
)
try:
if "".join(Path(case_filepath).suffixes) == ".cas.h5":
file = h5py.File(case_filepath)
Expand All @@ -190,8 +199,8 @@ def __init__(self, case_filepath: str = None, project_filepath: str = None):
else:
raise RuntimeError()

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

except OSError:
error_message = (
Expand All @@ -200,8 +209,8 @@ def __init__(self, case_filepath: str = None, project_filepath: str = None):
)
raise RuntimeError(error_message)

except BaseException:
raise RuntimeError(f"Could not read case file {case_filepath}")
except BaseException as e:
raise RuntimeError(f"Could not read case file {case_filepath}") from e

self._rp_vars = {v[0]: v[1] for v in lispy.parse(rp_vars_str)[1]}

Expand Down Expand Up @@ -285,29 +294,9 @@ def _get_processed_string(input_string: bytes) -> str:
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]
def _get_case_filepath_from_flprj(flprj_file):
parser = etree.XMLParser(recover=True)
tree = ET.parse(flprj_file, parser)
root = tree.getroot()
folder_name = root.find("Metadata").find("CurrentSimulation").get("value")[5:-1]
return root.find(folder_name).find("Input").find("Case").find("Target").get("value")
8 changes: 6 additions & 2 deletions src/ansys/fluent/core/filereader/casereader.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Compatibility aliases
from .case import Case as CaseReader # noqa: F401
from .case import InputParameter, OutputParameter # noqa: F401
from .case_file import ( # noqa: F401
InputParameter,
OutputParameter,
_get_processed_string,
)
from .case_file import CaseFile as CaseReader # noqa: F401
42 changes: 32 additions & 10 deletions src/ansys/fluent/core/post_objects/post_objects_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,36 @@


class Container:
def __init__(
self, session, child, module, post_api_helper, local_surfaces_provider=None
):
"""Instantiate Plots, container of plot objects.
"""
Base class for containers, e.g. Plots, Graphics.

Parameters
Parameters
----------
session :
session : object
Session object.
container_type: object
Container type (e.g. Plots, Graphics)
module: object
Python module containing post definitions
post_api_helper: object
Provides helper APIs for post-processing
local_surfaces_provider : object, optional
Object providing local surfaces.
"""
session_state = child._sessions_state.get(session)
Object providing local surfaces so that user can access surfaces
created in other modules, such as PyVista. The default is ``None``.
"""

def __init__(
self,
session,
container_type,
module,
post_api_helper,
local_surfaces_provider=None,
):
session_state = container_type._sessions_state.get(session)
if not session_state:
session_state = self.__dict__
child._sessions_state[session] = session_state
container_type._sessions_state[session] = session_state
self.session = session
self._init_module(self, module, post_api_helper)
else:
Expand Down Expand Up @@ -56,6 +70,10 @@ class Plots(Container):
----------
session : obj
Session object.
module: object
Python module containing post definitions
post_api_helper: object
Provides helper APIs for post-processing
local_surfaces_provider : object, optional
Object providing local surfaces so that you can access surfaces
created in other modules, such as pyvista. The default is ``None``.
Expand Down Expand Up @@ -86,6 +104,10 @@ class Graphics(Container):
----------
session : obj
Session object.
module: object
Python module containing post definitions
post_api_helper: object
Provides helper APIs for post-processing
local_surfaces_provider : object, optional
Object providing local surfaces so that you can access surfaces
created in other modules, such as pyvista. The default is ``None``.
Expand Down
29 changes: 6 additions & 23 deletions tests/test_casereader.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
import pytest

from ansys.fluent.core import examples
from ansys.fluent.core.filereader.case import _get_processed_string
from ansys.fluent.core.filereader.casereader import CaseReader, InputParameter
from ansys.fluent.core.filereader.casereader import (
CaseReader,
InputParameter,
_get_processed_string,
)


def call_casereader(
Expand Down Expand Up @@ -120,12 +123,6 @@ def create_dir_structure_locally(copy_1: bool = False, copy_2: bool = False):
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 @@ -145,22 +142,8 @@ def test_casereader_with_both_project_and_case_file():
)


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):
with pytest.raises(FileNotFoundError):
call_casereader(project_filepath="project.flprx")


Expand Down