How can I perform a parametric analysis through PyMechanical?

Member, Employee Posts: 4
Ansys Employee First Comment Solution Developer Community of Practice Member
✭✭✭
edited November 2023 in Structures

Here is an example code to perform a parametric analysis through PyMechanical with parameters that are not parametrized by Mechanical itself.

Comments

  • Member, Employee Posts: 4
    Ansys Employee First Comment Solution Developer Community of Practice Member
    ✭✭✭
    edited September 2023

    Here is the code :

    1. from ansys.mechanical.core import launch_mechanical
    2. from pathlib import Path
    3.  
    4. ###############################################################################
    5. # Launch Mechanical
    6. # ~~~~~~~~~~~~~~~~~
    7. # Launch a new Mechanical session in batch, setting ``cleanup_on_exit`` to
    8. # ``False``. To close this Mechanical session when finished, this example
    9. # must call the ``mechanical.exit()`` method.
    10.  
    11. mechanical = launch_mechanical(batch=False, cleanup_on_exit=False)
    12. print(mechanical)
    13.  
    14. ###############################################################################
    15. # Define parameters
    16. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    17. P1 = [0.001, 0.005, 0.01, 0.05, 0.1] # Maximum Time Step
    18.  
    19. P2 = [3, 2.5, 2, 1.5, 1, 0.5, 0.25] # Face Sizing and Face Sizing 2
    20.  
    21. P3 = ["Linear"] #Element order
    22. P4 = ["PurePenalty"] #Contact formulation
    23. P5 = ["NodalNormalToTarget", "NodalProjectedNormalFromContact", "NodalDualShapeFunctionProjection"] # Detection Method
    24.  
    25. names = ['Max Time Step', 'Sizing', 'Order', 'Formulation', 'Detection']
    26. parameters = [P1,P2,P3,P4,P5]
    27. param = dict(zip(names, parameters))
    28.  
    29. for key, value in param.items():
    30.  
    31. length = (len(value))
    32. for j in range(0, length):
    33. print(value[j])
    34.  
    35. ###################################################################################
    36. # Open the Mechanical model
    37. # ~~~~~~~~~~~~~~~~~~
    38. # Run the Mechanical script to attach the geometry and set up and solve the
    39. # analysis.
    40.  
    41. file = r"D:\\Data\\project1\\SYS-99.mechdb"
    42. command = f'ExtAPI.DataModel.Project.Open("{file}")'
    43.  
    44. # RUN LINES TO PERFORM PRELIMINARY OPERATIONS
    45. # ~~~~~~~~~~~~~~~~~~
    46. mechanical.run_python_script(command)
    47.  
    48. command = """
    49. import json
    50.  
    51. #Scenario 1: Store main Tree Object items
    52. MODEL = Model
    53. GEOM = MODEL.Geometry
    54. COORDINATE_SYSTEMS = Model.CoordinateSystems
    55. MESH = Model.Mesh
    56. NAMED_SELECTIONS = Model.NamedSelections
    57. CONNECTIONS = Model.Connections
    58.  
    59. PARTS = GEOM.GetChildren(DataModelObjectCategory.Part,False)
    60. for part in PARTS:
    61. bodies = part.GetChildren(DataModelObjectCategory.Body, False)
    62. for body in bodies:
    63. if body.Name == "Part 1":
    64. PART1 = body
    65.  
    66. STAT_STRUC = DataModel.Project.Model.Analyses[0]
    67. ANALYSIS_SETTINGS = STAT_STRUC.AnalysisSettings
    68. STAT_STRUC_SOLUTION = STAT_STRUC.Solution"""
    69. output = mechanical.run_python_script(command)
    70. print(output)
    71.  
    72. # CHANGE THE PARAMETER
    73. # ~~~~~~~~~~~~~~~~~~
    74. if key == 'Max Time Step':
    75. command = """
    76. ANALYSIS_SETTINGS.Activate()
    77. ANALYSIS_SETTINGS.CurrentStepNumber=2
    78. ANALYSIS_SETTINGS.MaximumTimeStep=Quantity("{0:.3f} [sec]")
    79. """.format(value[j])
    80. if key == 'Sizing':
    81. command = """
    82. MESH.Children[1].ElementSize = Quantity({0:.1f}, "m")
    83. MESH.Children[2].ElementSize = Quantity({0:.1f}, "m")""".format(value[j])
    84. if key == 'Order':
    85. command = """
    86. MESH.ElementOrder = ElementOrder.{0:s}""".format(value[j])
    87. if key == 'Formulation':
    88. command = """
    89. CONNECTIONS.Children[0].Children[0].ContactFormulation = ContactFormulation.{0:s}
    90. CONNECTIONS.Children[0].Children[1].ContactFormulation = ContactFormulation.{0:s}""".format(value[j])
    91. if key == 'Detection':
    92. command = """
    93. CONNECTIONS.Children[0].Children[0].DetectionMethod = ContactDetectionPoint.{0:s}
    94. CONNECTIONS.Children[0].Children[1].DetectionMethod = ContactDetectionPoint.{0:s}""".format(value[j])
    95. print(command)
    96.  
    97. output = mechanical.run_python_script(command)
    98. print(output)
    99.  
    100. # UPDATE MESH AND SOLVE
    101. # ~~~~~~~~~~~~~~~~~~
    102. command = """
    103. # Solve Static Analysis
    104. MESH.GenerateMesh()
    105. STAT_STRUC = Model.Analyses[0]
    106. STAT_STRUC.Solution.Solve(True)"""
    107.  
    108. output = mechanical.run_python_script(command)
    109. print(output)

Welcome!

It looks like you're new here. Sign in or register to get started.