ansys / pymechanical

Pythonic interface to Ansys Mechanical ™
https://mechanical.docs.pyansys.com/
MIT License
36 stars 18 forks source link

What is the source of the ID of the selected geometry? #651

Closed coconutLatte closed 6 months ago

coconutLatte commented 6 months ago

Hello! I'm doing an easy steady state thermal simulation analysis and record it to mechanical script. By this repo I chose the remote session way to run it on HPC cluster to play pre process and solve. It successed for me. What I want to get is how the selected geometry ID is converted from an .stp geometry file?

Here are the specific steps I did:

Version

Ansys Mechanical 2023R1

What I did

record the whole simulation

  1. import the .stp geometry file (a pan with a heating wire on the bottom);
  2. generate the mesh;
  3. add heat flow analysis of steady state thermal on the face where the heating wire is in contact with the bottom of the pot;
  4. add convert analysis of steady state thermal on the pot body;
  5. do the solution about temperature.

I recorded all the steps, and got the script like here

'''NOTE : All workflows will not be recorded, as recording is under development.'''

#region Context Menu Action
geometry_import_12 = DataModel.GetObjectById(12)
geometry_import_12.Import(r"C:\workspace\AnsysPanAPP\AnsysPanModel.stp") # Primary Source!
#endregion

#region UI Action
with Transaction(True):
    body_28 = DataModel.GetObjectById(28)
    body_28.Material = "47139dc2-7f90-4271-bb9e-b55e52970f4e"
#endregion

#region Context Menu Action
ExtAPI.Application.ActiveUnitSystem = MechanicalUnitSystem.StandardNMM
#endregion

#region Details View Action
mesh_14 = Model.Mesh
mesh_14.ElementSize = Quantity(1, "mm")
#endregion

#region Context Menu Action
mesh_14.GenerateMesh()
#endregion

#region Context Menu Action
sizing_39 = mesh_14.AddSizing()
#endregion

#region Details View Action
selection = ExtAPI.SelectionManager.CreateSelectionInfo(SelectionTypeEnum.GeometryEntities)
selection.Ids = [828]
sizing_39.Location = selection
#endregion

#region Details View Action
sizing_39.ElementSize = Quantity(0.5, "mm")
#endregion

#region Context Menu Action
analysis_20 = DataModel.GetObjectById(20)
heat_flow_42 = analysis_20.AddHeatFlow()
#endregion

#region Context Menu Action
convection_43 = analysis_20.AddConvection()
#endregion

#region Details View Action
selection = ExtAPI.SelectionManager.CreateSelectionInfo(SelectionTypeEnum.GeometryEntities)
selection.Ids = [796]
heat_flow_42.Location = selection
#endregion

#region Details View Action
heat_flow_42.Magnitude.Output.SetDiscreteValue(0, Quantity(500, "W"))
#endregion

#region Details View Action
selection = ExtAPI.SelectionManager.CreateSelectionInfo(SelectionTypeEnum.GeometryEntities)
selection.Ids = [477]
convection_43.Location = selection
#endregion

#region Details View Action
convection_43.FilmCoefficient.Output.SetDiscreteValue(0, Quantity(5e-4, "W mm^-1 mm^-1 C^-1"))
#endregion

#region Context Menu Action
solution_21 = DataModel.GetObjectById(21)
temperature_result_46 = solution_21.AddTemperature()
#endregion

convert it to PyMechanical way

I modify some of the script and add it to a simple workflow based on PyMechanical like this

import ansys.mechanical.core as pymechanical

wb_path = "/es01/home/zssoftware/Ansys/Ansys2023r1_gr/v231/aisol/.workbench"
print("wb_path", wb_path)
mechanical = pymechanical.launch_mechanical(exec_file=wb_path, verbose_mechanical=True, batch=True, loglevel="DEBUG", log_file=True, log_mechanical="pymechanical")
print("mechanical launch success: ", mechanical)

project_directory = mechanical.project_directory
print(f"project directory = {project_directory}")

try:
    # import geometry file
    GEO_IMPORT_SCRIPT = """
geo_import_format = Ansys.Mechanical.DataModel.Enums.GeometryImportPreference.Format.Automatic
geo_import_preferences = Ansys.ACT.Mechanical.Utilities.GeometryImportPreferences()
#geo_import_preferences.ProcessSurfaces = False
#geo_import_preferences.ProcessNamedSelections = True
#geo_import_preferences.NamedSelectionKey = "" 
geo_import = Model.GeometryImportGroup.AddGeometryImport()
geo_import.Import("{geo_path:s}", geo_import_format, geo_import_preferences)
"""
    mechanical.run_python_script(
      GEO_IMPORT_SCRIPT.format(geo_path="/es01/home/yuansuan/test_case/wanghao/ansys-231-pre-process/AnsysPanModel.stp")  
    )

    # mesh
    mechanical.run_python_script("""
mesh_14 = Model.Mesh
mesh_14.ElementSize = Quantity(1, "mm")
mesh_14.GenerateMesh()
sizing_39 = mesh_14.AddSizing()
selection = ExtAPI.SelectionManager.CreateSelectionInfo(SelectionTypeEnum.GeometryEntities)
selection.Ids = [828]
sizing_39.Location = selection
sizing_39.ElementSize = Quantity(0.5, "mm")
"""
    )

    # add steadyStateThermalAnalysis
    mechanical.run_python_script("""
state_thermal_analysis = Model.AddSteadyStateThermalAnalysis()
heat_flow_42 = state_thermal_analysis.AddHeatFlow()
selection = ExtAPI.SelectionManager.CreateSelectionInfo(SelectionTypeEnum.GeometryEntities)
selection.Ids = [796]
heat_flow_42.Location = selection
heat_flow_42.Magnitude.Output.SetDiscreteValue(0, Quantity(500, "W"))

convection_43 = state_thermal_analysis.AddConvection()
selection = ExtAPI.SelectionManager.CreateSelectionInfo(SelectionTypeEnum.GeometryEntities)
selection.Ids = [477]
convection_43.Location = selection
convection_43.FilmCoefficient.Output.SetDiscreteValue(0, Quantity(5e-4, "W mm^-1 mm^-1 C^-1"))

state_thermal_analysis.Solution.AddTemperature()
state_thermal_analysis.WriteInputFile("/es01/home/yuansuan/test_case/wanghao/ansys-231-pre-process/wanghao_test.dat")
""")
except Exception as e:
    print("An error occurred while running the Python script:", e)
finally:
    print("exit mechanical instance")
    mechanical.exit()

It runs good so that I can get the .dat file for my next step to do the solve.

Related infos

the pan looks like: image the raw pan.stp file: AnsysPanModel.zip

What I want to know

I want to know the source of selection id. Is it pointing at the geometry body(face / line or etc.)? Is there any way that I can get the id at the very begining? Or is the selection id could mapped to the information of .stp file? Or any idea about how could I get the seletion id in other ways?

Sorry for the long text to read and too many questions. It's really important for me. Hope for some help!

dipinknair commented 6 months ago

@coconutLatte, Please redirect your question to Ansys Developer Forum. For Mechanical. There are many posts there that would help you since this is related to Mechanical scripting rather than PyMechanical.

coconutLatte commented 6 months ago

@dipinknair Thanks! I would question in the link

pmaroneh commented 6 months ago

Hi @coconutLatte , as @dipinknair pointed out, the Ansys Developer Forum is better suited for these types of questions. In short, the recording will show the ids of the geometry because it has no other way of identifying the faces, edges, etc that you have scoped. However, it is needed to modify these, because from a user perspective there is no way to know the ids, and if the geometry changes the ids will change too. The most robust way is to use Named Selections. See this post as an example.