-
Notifications
You must be signed in to change notification settings - Fork 20
Custom python script caller for write #1033
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| import numpy as np | ||
| import tohil | ||
| from objarray_numpy_tools import objarray_to_nparray,nparray_to_objarray | ||
| from pathlib import Path | ||
|
|
||
| #to create functions and variables for all tcl available ones | ||
| tcl=tohil.import_tcl() | ||
|
|
||
|
|
||
| def _build_output_mdpa_path(gid_folder_path): | ||
| gid_folder=Path(gid_folder_path) | ||
|
|
||
| # In GiD, ModelName can arrive without the .gid suffix. | ||
| if (not gid_folder.is_dir()) and (gid_folder.suffix.lower() != ".gid"): | ||
| gid_folder_with_suffix=Path(f"{gid_folder}.gid") | ||
| if gid_folder_with_suffix.is_dir(): | ||
| gid_folder=gid_folder_with_suffix | ||
|
|
||
| case_name=gid_folder.stem | ||
| return gid_folder / f"{case_name}.mdpa" | ||
|
|
||
|
|
||
| def _normalize_node_xyzs(node_xyzs_original): | ||
| node_xyzs_array=objarray_to_nparray(node_xyzs_original) | ||
| flat=np.asarray(node_xyzs_array, dtype=object).reshape(-1) | ||
|
|
||
| # Sometimes tohil returns a single string with all coordinates. | ||
| if flat.size == 1 and isinstance(flat[0], str): | ||
| tokens=flat[0].replace(",", " ").split() | ||
| flat_numeric=np.asarray(tokens, dtype=float) | ||
| else: | ||
| flat_numeric=np.asarray(flat, dtype=float) | ||
|
|
||
| if flat_numeric.size % 3 != 0: | ||
| raise ValueError(f"Invalid flattened coordinates length: {flat_numeric.size}") | ||
|
|
||
| return flat_numeric.reshape((-1, 3)) | ||
|
|
||
|
|
||
| def my_meshio_write_mesh2(filename): | ||
| output_mdpa_path=_build_output_mdpa_path(filename) | ||
| output_mdpa_path.parent.mkdir(parents=True, exist_ok=True) | ||
|
|
||
| # Gets the nodes in the GID Mesh | ||
| info_nodes=tuple(tcl.GiD_Info('mesh','nodes','-array2')) | ||
| node_ids_original,node_xyzs=info_nodes | ||
| node_ids=objarray_to_nparray(node_ids_original) | ||
| node_xyzs_matrix=_normalize_node_xyzs(node_xyzs) | ||
| # tcl.W(node_ids) | ||
| # tcl.W(node_xyzs) | ||
|
|
||
| for element_type in ['line','triangle','quadrilateral','tetrahedra','pyramid','prism','hexahedra']: | ||
| info_elements=tuple(tcl.GiD_Info('mesh','elements',element_type,'-array2')) | ||
| if (len(info_elements)): | ||
| elements_data=info_elements[0] | ||
| element_type_ret,element_ids_original,connectivities_original,materials=elements_data | ||
| element_ids=objarray_to_nparray(element_ids_original) | ||
| connectivities=objarray_to_nparray(connectivities_original) | ||
| # tcl.W(element_type_ret) | ||
| # tcl.W(element_ids) | ||
|
|
||
| group_names=tcl.GiD_Groups("list") | ||
| for group_name in group_names: | ||
| group_name=str(group_name) # convert from tohil.tclobj to Python str | ||
| # get nodes of the group (returns "" when empty, so convert first then check length) | ||
| group_node_ids=objarray_to_nparray(tcl.GiD_EntitiesGroups("get", group_name, "nodes")) | ||
|
|
||
|
|
||
| # get elements of the group (returns "" when empty, so convert first then check length) | ||
| group_element_ids=objarray_to_nparray(tcl.GiD_EntitiesGroups("get", group_name, "elements")) | ||
|
|
||
| with open(output_mdpa_path, "w", encoding="utf-8") as mdpa_file: | ||
| mdpa_file.write(f"# Nodes count: {len(node_ids)}\n") | ||
| mdpa_file.write("Begin Nodes\n") | ||
| # GiD returns flattened coords: x1 y1 z1 x2 y2 z2 ... | ||
| for node_id, (x,y,z) in zip(node_ids, node_xyzs_matrix): | ||
| mdpa_file.write(f" {node_id} {x} {y} {z}\n") | ||
| mdpa_file.write("End Nodes\n") | ||
|
|
||
|
|
||
|
|
||
| return 0 | ||
|
|
||
| # main | ||
| def start(filename): | ||
| # tcl.W("pollo") | ||
| return my_meshio_write_mesh2(filename) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.