Skip to content

6.1 Simulations

Eric Breitbarth edited this page Apr 7, 2024 · 1 revision

Here, we show how to use the high-level CrackPy module ansys_simulation. To use this part of the CrackPy package ANSYS Mechanical is needed.

We start with the necessary imports

# Imports
import os
import time

from ansys.mapdl.core import launch_mapdl
import ansys.mapdl.core.errors

from crackpy.ansys_simulation import models
from crackpy.ansys_simulation.utils import delete_ansys_leftovers
from crackpy.structure_elements.material import Material

1. Compact tension (CT) specimen

First, we need to create the output path folder

OUTPUT_PATH = 'CT_Simulation_Output'
if not os.path.exists(OUTPUT_PATH):
    os.makedirs(OUTPUT_PATH)

Then, we specify the specimen and material parameters

# Parameters
FORCE = 10000
ALPHA = 0.5
WIDTH = 75
THICKNESS = 10
ESIZE = 0.5
REF_WINDOW = 8

# Material
material = Material(
    name='AA2024-T3',
    E=72000,
    nu_xy=0.33,
    sig_yield=350,
    plane_strain=False
)

In the main part of the simulation, we set the mesh and boundary conditions, run the simulation, and postprocess by plotting the results and saving the displacements and strains in a nodemap-file.

while True:

    try:
        # Start MAPDL
        mapdl = launch_mapdl(run_location=OUTPUT_PATH, nproc=5)

        # Initialize CT simulation
        ansys_simulation = models.CTSimulation(mapdl)

        ansys_simulation.set_parameters(
            material=material,
            width=WIDTH,
            thickness=THICKNESS,
            force=FORCE,
            alpha=ALPHA,
            esize=ESIZE,
            ref_window=REF_WINDOW
        )

        # Key points and Mesh
        ansys_simulation.set_key_points()
        ansys_simulation.set_mesh()

        # Boundary conditions
        ansys_simulation.set_boundary_conditions()

        # Solve
        ansys_simulation.solve()

        # Plot
        ansys_simulation.plot(yield_stress=material.sig_yield)

        # Export
        ansys_simulation.export_nodemap()

    # Catch exceptions
    except ansys.mapdl.core.errors.LockFileException:
        delete_ansys_leftovers(ansys_folder=OUTPUT_PATH)
        continue

    except ansys.mapdl.core.errors.MapdlExitedError:
        print('Mapdl Session Terminated. Retrying...')
        delete_ansys_leftovers(ansys_folder=OUTPUT_PATH)
        continue

    except OSError:
        print('OSError. Retrying...')
        continue

    finally:
        mapdl.exit()
        time.sleep(5)
        delete_ansys_leftovers(ansys_folder=OUTPUT_PATH)

    break

Note that the simulation is wrapped in a try-block to handle several exceptions which often arise when dealing with Ansys. Moreover, we use the function delete_ansys_leftovers to get rid of unnecessary overhead created by Ansys.

2. Middle tension (MT) specimen

For a middle tension specimen, the script looks very similar:

OUTPUT_PATH = 'MT_Simulation_Output'
if not os.path.exists(OUTPUT_PATH):
    os.makedirs(OUTPUT_PATH)

# Parameters
FORCE = 10000
ALPHA = 0.5
THETA = 0
WIDTH = 160
HEIGHT = 200
THICKNESS = 2
ESIZE = 0.5
REF_WINDOW = 12

# Material
material = Material(
    name='AA2024-T3',
    E=72000,
    nu_xy=0.33,
    sig_yield=350,
    plane_strain=False
)


while True:

    try:
        # Start MAPDL
        mapdl = launch_mapdl(run_location=OUTPUT_PATH, nproc=5)

        # Initialize MT simulation
        ansys_simulation = models.MTSimulation(mapdl)

        ansys_simulation.set_parameters(
            material=material,
            height=HEIGHT,
            width=WIDTH,
            thickness=THICKNESS,
            force=FORCE,
            alpha=ALPHA,
            theta=THETA,
            esize=ESIZE,
            ref_window=REF_WINDOW
        )

        # Key points and Mesh
        ansys_simulation.set_key_points()
        ansys_simulation.set_mesh()

        # Boundary conditions
        ansys_simulation.set_boundary_conditions()

        # Solve
        ansys_simulation.solve()

        # Plot
        ansys_simulation.plot(yield_stress=material.sig_yield)

        # Export nodal data into Nodemap-File
        ansys_simulation.export_nodemap()

    # Catch exceptions
    except ansys.mapdl.core.errors.LockFileException:
        delete_ansys_leftovers(ansys_folder=OUTPUT_PATH)
        continue

    except ansys.mapdl.core.errors.MapdlExitedError:
        print('Mapdl Session Terminated. Retrying...')
        continue

    except OSError:
        print('OSError. Retrying...')
        continue

    finally:
        mapdl.exit()
        time.sleep(5)
        delete_ansys_leftovers(ansys_folder=OUTPUT_PATH)

    break