Skip to content

Commit

Permalink
Merge pull request #3 from Renumics/feature/script_as_param
Browse files Browse the repository at this point in the history
add: customized scrip for ansa
  • Loading branch information
Renumics authored Feb 21, 2023
2 parents ca6a596 + 38127b4 commit 0a06d9c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ Quickstart

<p align="center">
<a href="https://github.com/renumics/mesh2vec"><img src="https://img.shields.io/github/license/renumics/mesh2vec" height="20"/></a>
<a href="https://github.com/renumics/mesh2vec"><img src="https://img.shields.io/pypi/pyversions/renumics-mesh2vec" height="20"/></a>
<a href="https://github.com/renumics/mesh2vec"><img src="https://img.shields.io/pypi/wheel/renumics-mesh2vec" height="20"/></a>
<a href="https://github.com/renumics/mesh2vec"><img src="https://img.shields.io/pypi/pyversions/mesh2vec" height="20"/></a>
<a href="https://github.com/renumics/mesh2vec"><img src="https://img.shields.io/pypi/wheel/mesh2vec" height="20"/></a>
</p>
<h3 align="center">
<a href="https://renumics.github.io/mesh2vec/"><b>Latest Documentation</b></a>
Expand Down
12 changes: 12 additions & 0 deletions docs/source/customize_ansa_script.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Customize Ansa Script
=======================
.. _customize_ansa_script:

You can modify the original ansa data extraction script in :py:meth:`mesh2vec.mesh2vec_cae.Mesh2VecCae.from_ansa_shell` and
:py:meth:`mesh2vec.mesh2vec_cae.Mesh2VecCae.add_features_from_ansa` to include more features.
Make sure you provide also all required fields for nodes and elements (see original script below).

Original Ansa Script as Template:
----------------------------------
.. literalinclude:: ../../mesh2vec/templates/ansa.py
:encoding: latin-1
1 change: 1 addition & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ to use ANSA depended features like shell and feature import.

api
generated_examples/index
customize_ansa_script



Expand Down
27 changes: 23 additions & 4 deletions mesh2vec/mesh2vec_cae.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ def from_ansa_shell(
partid: str = "",
json_mesh_file: Optional[Path] = None,
ansa_executable: Optional[Path] = None,
ansa_script: Optional[Path] = None,
verbose: bool = False,
) -> "Mesh2VecCae":
"""
Expand All @@ -140,6 +141,9 @@ def from_ansa_shell(
Path to ANSA executable can also be provided in environment var: ANSA_EXECUTABLE
You can use a customized script to include more features ansa_script
(see :ref:`Customize Ansa script<customize_ansa_script>`)
Example:
>>> from pathlib import Path
Expand All @@ -154,7 +158,7 @@ def from_ansa_shell(
os.environ["ANSA_EXECUTABLE"] = str(ansa_executable)

elements, nodes = Mesh2VecCae._read_ansafile(
ansafile, json_mesh_file, verbose=verbose, partid=partid
ansafile, json_mesh_file, verbose=verbose, partid=partid, ansa_script=ansa_script
)
mesh = CaeShellMesh.from_ansa_json(elements, nodes)

Expand Down Expand Up @@ -230,6 +234,7 @@ def add_features_from_ansa(
ansafile: Optional[Path],
json_mesh_file: Optional[Path] = None,
ansa_executable: Optional[Path] = None,
ansa_script: Optional[Path] = None,
verbose: bool = False,
) -> None:
"""
Expand All @@ -238,6 +243,10 @@ def add_features_from_ansa(
Path to ANSA executable can also be provided in environment var: ANSA_EXECUTABLE
You can use a customized script to include more features ansa_script
(see :ref:`Customize Ansa script<customize_ansa_script>`)
``features`` is a subset of:
* aspect: The aspect ratio of each element (ansafile is required)
Expand Down Expand Up @@ -277,7 +286,7 @@ def add_features_from_ansa(

if any(feature in okay_ansa for feature in features):
elements, nodes = Mesh2VecCae._read_ansafile(
ansafile, json_mesh_file, verbose=verbose
ansafile, json_mesh_file, verbose=verbose, ansa_script=ansa_script
)
mesh = CaeShellMesh.from_ansa_json(elements, nodes)

Expand Down Expand Up @@ -663,10 +672,20 @@ def _read_ansafile(
json_mesh_file: Optional[Path],
verbose: bool,
partid: str = "",
ansa_script: Optional[Path] = None,
) -> Tuple[List[Dict], List[Dict]]:
src_folder = os.path.abspath(os.path.dirname(__file__))
ansa_path = os.getenv("ANSA_EXECUTABLE")

if ansa_script is not None:
if not ansa_script.exists():
raise FileNotFoundError(
f"Ansa Script was not found at '{ansa_script}' from current "
f"directory '{os.getcwd() }'"
)
else:
src_folder = os.path.abspath(os.path.dirname(__file__))
ansa_script = f"{src_folder}/templates/ansa.py"

with TemporaryDirectory() as tmp_folder:
if json_mesh_file is None:
output_path = Path(tmp_folder) / "tmp.json"
Expand All @@ -685,7 +704,7 @@ def _read_ansafile(
"-b",
"-foregr",
"-execpy",
f"load_script: '{src_folder}/templates/make_hg.py'",
f"load_script: '{ansa_script}",
"-execpy",
f"make_hg('{ansafile}', '{output_path}', '{partid}')",
]
Expand Down

0 comments on commit 0a06d9c

Please sign in to comment.