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

clean up harmonic example and ignore files with 1_ext suffix #308

Merged
merged 11 commits into from
Jul 20, 2023
4 changes: 3 additions & 1 deletion doc/source/api/data_sources.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ Data sources
ShortFiberCompositesFiles
CompositeDataSources
get_composite_files_from_workbench_result_folder
get_composites_data_sources
composite_files_from_workbench_harmonic_analysis
get_composites_data_sources

9 changes: 3 additions & 6 deletions examples/010_harmonic_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

.. note::

This example is work in progress. Please open a Github issue if something is unclear.
When using a workbench project, use :func:`.composite_files_from_workbench_harmonic_analysis`
to obtain the input files.
janvonrickenbach marked this conversation as resolved.
Show resolved Hide resolved

This example shows how to evaluate failure criteria for a harmonic simulation.
It shows how to create a phase sweep to compute the maximum IRF in the frequency-phase
Expand Down Expand Up @@ -38,13 +39,9 @@
from ansys.dpf.composites.server_helpers import connect_to_or_start_server
from ansys.dpf.composites.unit_system import get_unit_system

# Todo: Discuss influence of damping factor
# Todo: Currently the CompositeDefinition file from the modal analysis needs
# to be copied to the harmonic response folder (including the Setup folder)

# %%
# Start a DPF server and copy the example files into the current working directory.
server = connect_to_or_start_server(port=50052)
server = connect_to_or_start_server()
composite_files_on_server = get_continuous_fiber_example_files(server, "harmonic")

# %%
Expand Down
80 changes: 77 additions & 3 deletions src/ansys/dpf/composites/data_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"ShortFiberCompositesFiles",
"CompositeDataSources",
"get_composite_files_from_workbench_result_folder",
"composite_files_from_workbench_harmonic_analysis",
"get_composites_data_sources",
"get_short_fiber_composites_data_sources",
)
Expand All @@ -22,6 +23,7 @@
_SHELL_COMPOSITE_DEFINITIONS_PREFIX = "ACPCompositeDefinitions"
_SETUP_FOLDER_PREFIX = "Setup"
_H5_SUFFIX = ".h5"
_1_EXT_SUFFIX = ".1_ext"
_MATML_FILENAME = "MatML.xml"
_RST_SUFFIX = ".rst"
_RST_PREFIX = "file"
Expand Down Expand Up @@ -169,16 +171,28 @@ def _is_matml_file(path: pathlib.Path) -> bool:
return path.name == _MATML_FILENAME and path.is_file()


def _has_1_ext_suffix(path: pathlib.Path) -> bool:
janvonrickenbach marked this conversation as resolved.
Show resolved Hide resolved
"""Check if the path has exactly 2 suffixes and the first one is equal to 1_ext.

Example: CompositeDefinitions.1_ext.h5
"""
if len(path.suffixes) == 2:
return path.suffixes[0] == _1_EXT_SUFFIX
return False


def _is_composite_definition_file(path: pathlib.Path) -> bool:
is_composite_def = path.name.startswith(_SHELL_COMPOSITE_DEFINITIONS_PREFIX)
return path.suffix == _H5_SUFFIX and path.is_file() and is_composite_def
no_ext_suffix = not _has_1_ext_suffix(path)
return path.suffix == _H5_SUFFIX and path.is_file() and is_composite_def and no_ext_suffix


def _is_solid_model_composite_definition_file(path: pathlib.Path) -> bool:
is_h5 = path.suffix == _H5_SUFFIX
has_1_ext_suffix = _has_1_ext_suffix(path)
is_file = path.is_file()
is_def = path.name.startswith(_SOLID_COMPOSITE_DEFINITIONS_PREFIX)
return is_h5 and is_file and is_def
return is_h5 and is_file and is_def and not has_1_ext_suffix


def _get_file_paths_with_predicate(
Expand Down Expand Up @@ -261,6 +275,65 @@ def get_composite_definitions_files(
composite_files.composite[key] = definition_files


def composite_files_from_workbench_harmonic_analysis(
result_folder_modal: _PATH, result_folder_harmonic: _PATH
) -> ContinuousFiberCompositesFiles:
"""Get a ``ContinuousFiberCompositesFiles`` object for a harmonic analysis.

Parameters
----------
result_folder_modal :
Result folder of the modal solution.
In the Modal System, Right-click the **solution** item in the Ansys Mechanical tree
and select **Open Solver Files Directory** to obtain the result folder.
result_folder_harmonic :
Result folder of the Harmonic Response solution.
In the Harmonic Response System,
Right-click the **solution** item in the Ansys Mechanical tree
janvonrickenbach marked this conversation as resolved.
Show resolved Hide resolved
and select **Open Solver Files Directory** to obtain the result folder.

"""
result_folder_path_harmonic = pathlib.Path(result_folder_harmonic)
result_folder_path_modal = pathlib.Path(result_folder_modal)

setup_folders_modal = [
folder_path
for folder_path in result_folder_path_modal.iterdir()
if folder_path.is_dir() and folder_path.name.startswith(_SETUP_FOLDER_PREFIX)
]

rst_paths = _get_file_paths_with_predicate(
_is_rst_file,
result_folder_path_harmonic,
)

if len(rst_paths) == 0:
raise RuntimeError(
f"Expected at least one rst file. Found {rst_paths}."
f" Available files in folder: {os.listdir(result_folder_path_harmonic)}"
)

matml_path = _get_single_file_path_with_predicate(
_is_matml_file,
result_folder_path_harmonic,
"matml",
)

assert matml_path is not None
assert rst_paths is not None

continuous_fiber_composite_files = ContinuousFiberCompositesFiles(
rst=[rst_path.resolve() for rst_path in rst_paths],
composite={},
engineering_data=matml_path.resolve(),
)

for setup_folder in setup_folders_modal:
_add_composite_definitons_from_setup_folder(setup_folder, continuous_fiber_composite_files)

return continuous_fiber_composite_files


def get_composite_files_from_workbench_result_folder(
result_folder: _PATH, ensure_composite_definitions_found: bool = True
) -> ContinuousFiberCompositesFiles:
Expand Down Expand Up @@ -386,7 +459,8 @@ def get_composite_files_from_workbench_result_folder(
raise RuntimeError(
"No composite definitions found. Set "
"ensure_composite_definitions_found argument"
" to False to skip this check."
" to False to skip this check. Note: Use the function"
" composite_files_from_workbench_harmonic_analysis if this is a harmonic analysis."
)

return continuous_fiber_composite_files
Expand Down
Binary file not shown.
Binary file not shown.
Loading