Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/case read vars #960

Merged
merged 4 commits into from
Oct 3, 2022
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
80 changes: 71 additions & 9 deletions src/ansys/fluent/core/filereader/casereader.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,33 @@ def __init__(self, raw_data):
self.units = elem[1].strip()


class _CaseVariable:
def __init__(self, variables: dict, path: str = ""):
self._variables = variables
self._path = path

def __call__(self, name: str = ""):
if not name:
error_name = self._path[:-1] if self._path else self._path
raise RuntimeError(f"Invalid variable {error_name}")
return self._variables[name]

def __getattr__(self, name: str):
for orig, sub in (
("__q", "?"),
("__dot", "."),
("__plus", "+"),
("_", "-"),
):
name = name.replace(orig, sub)
try:
name = self._path + name
result = self._variables[name]
return lambda: result
except KeyError:
return _CaseVariable(self._variables, name + "/")


class CaseReader:
"""Class to read a Fluent case file.

Expand All @@ -77,6 +104,26 @@ class CaseReader:
Get the dimensionality of the case (2 or 3)
precision
Get the precision (1 or 2 for 1D of 2D)
rp_vars
Get dictionary of all RP vars
rp_var
Get specific RP var by name, either by providing
the Scheme name:
`reader.rp_var("rad/enable-netm?")`
or a pythonic version:
`reader.rp_var.rad.enable_netm__q()`
has_rp_var
Whether case has particular RP var
config_vars
Get dictionary of all RP vars
config_var
Get specific config var by name, either by providing
the Scheme name:
`reader.config_var("rp-3d?")`
or a pythonic version:
`reader.config_var.rp_3d__q()`
has_config_var
Whether case has particular config var
"""

def __init__(self, case_filepath: str = None, project_filepath: str = None):
Expand Down Expand Up @@ -119,8 +166,9 @@ def __init__(self, case_filepath: str = None, project_filepath: str = None):
except BaseException:
raise RuntimeError(f"Could not read case file {case_filepath}")

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

self._config_vars = {v[0]: v[1] for v in self._rp_vars["case-config"]}

def input_parameters(self) -> List[InputParameter]:
exprs = self._named_expressions()
Expand Down Expand Up @@ -149,20 +197,34 @@ def precision(self) -> int:
if attr[0] == "rp-double?":
return 2 if attr[1] is True else 1

def rp_vars(self) -> dict:
return self._rp_vars

@property
def rp_var(self):
return _CaseVariable(self._rp_vars)

def has_rp_var(self, name):
return name in self._rp_vars

def config_vars(self):
return self._config_vars

@property
def config_var(self):
return _CaseVariable(self._config_vars)

def has_config_var(self, name):
return name in self._config_vars

def _named_expressions(self):
return self._find_rp_var("named-expressions")

def _case_config(self):
return self._find_rp_var("case-config")

def _find_rp_var(self, name: str):
try:
return self._rp_var_cache[name]
except KeyError:
for var in self._rp_vars:
if type(var) == list and len(var) and var[0] == name:
self._rp_var_cache[name] = var[1]
return var[1]
return self._rp_vars[name]


def _get_processed_string(input_string: bytes) -> str:
Expand Down
31 changes: 26 additions & 5 deletions tests/test_casereader.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,16 @@ def call_casereader_static_mixer(
)


def test_casereader_static_mixer_h5():
call_casereader_static_mixer(
case_filepath=examples.download_file(
"Static_Mixer_Parameters.cas.h5", "pyfluent/static_mixer"
)
def static_mixer_file():
return examples.download_file(
"Static_Mixer_Parameters.cas.h5", "pyfluent/static_mixer"
)


def test_casereader_static_mixer_h5():
call_casereader_static_mixer(case_filepath=static_mixer_file())


def test_casereader_static_mixer_binary_cas():
call_casereader_static_mixer(
case_filepath=examples.download_file(
Expand Down Expand Up @@ -181,3 +183,22 @@ def test_case_reader_with_bad_data_to_be_skipped_and_input_parameters_labeled_di
},
),
)


def test_case_reader_get_rp_and_config_vars():
reader = CaseReader(case_filepath=static_mixer_file())
rp_vars = reader.rp_vars()
assert rp_vars
assert hasattr(rp_vars, "__getitem__")
config_vars = reader.config_vars()
assert config_vars
assert hasattr(config_vars, "__getitem__")
assert config_vars["rp-3d?"] is True
assert reader.config_var("rp-3d?") is True
assert reader.config_var.rp_3d__q() is True
assert reader.rp_var.smooth_mesh.niter() is 4
assert reader.rp_var.pressure.output_dpdt__q() is True
assert len(reader.rp_var.context.map_r17__plus()) == 53
assert reader.rp_var.defaults.pre_r19__dot0_early__q() is False
with pytest.raises(BaseException):
reader.rp_var.defaults.pre_r19__dot0_early()