2.5. Python Script

"""
Purpose: Create Python Result objects to evaluate and plot 
         failure criteria for short fiber composites. 
Usage  : Execute this script in Ansys Mechanical
Version: 2023 R1

Copyright (C) 2021-2023 ANSYS Inc.  All Rights Reserved   
"""

# modify in case you want to create the plot objects
# for a different analysis, e.g. Model.Analyses[2]
solution = Model.Analyses[0].Solution

FAILURE_SCRIPT_Text="""
def post_started(sender, analysisIn):# Do not edit this line
    define_dpf_workflow(analysisIn)
    
def define_dpf_workflow(analysis):
    
    # failure type can be either "yield" or "ultimate"
    failure_type = %s
    
    # (optional) result_set_id
    # by default the failure criteria is computed for the last result set,
    result_set_id = None # e.g. result_set_id = 2
    
    # (optional) named_selection
    # string with the name of the Named Selection of interest
    # Note: You should enter the name of the MAPDL named selection,
    # not the name displayed in Mechanical. That is, all letters uppercase
    # and spaces replaced by underscore (e.g. "My Selection" becomes "MY_SELECTION").
    named_selection = None # e.g. named_selection = "CENTER"
    
    # current working directory, modify if your analysis is not AnalysisList[0]
    working_directory = analysis.WorkingDir
    
    _get_workflow(working_directory, failure_type, result_set_id, named_selection)
    
    
def _get_workflow(working_directory, failure_type, result_set_id, named_selection):
    
    import mech_dpf
    import Ans.DataProcessing as dpf
    import os
    
    _load_plugins()
    
    # define pins and data sources
    time_scoping_pin = 0
    mesh_scoping_pin = 1
    data_source_pin = 4
    stream_pin = 3

    engd_path = os.path.join(working_directory, "MatML.xml")
    rst_path = os.path.join(working_directory, "file.rst")
    dat_path = os.path.join(working_directory, "ds.dat")
    
    data_sources = dpf.DataSources()
    data_sources.AddFilePath(engd_path, "EngineeringData")
    data_sources.AddFilePath(dat_path, "dat")
    
    rst_data_source = dpf.DataSources()
    rst_data_source.SetResultFilePath(rst_path)

    rst_stream_provider = dpf.Operator("stream_provider")
    rst_stream_provider.Connect(data_source_pin, rst_data_source)
    
    sf_op = dpf.Operator("composite::short_fiber_failure_criterion_evaluator")
    sf_op.Connect(stream_pin, rst_stream_provider, 0)
    sf_op.Connect(data_source_pin, data_sources)
    sf_op.Connect(2, failure_type)

    if named_selection is not None:
        ns_op = dpf.operators.scoping.on_named_selection()   
        ns_op.inputs.streams_container.Connect(rst_stream_provider)  
        ns_op.inputs.requested_location.Connect('Elemental')
        ns_op.inputs.named_selection_name.Connect(named_selection) 
        sf_op.Connect(mesh_scoping_pin, ns_op)
        
    if result_set_id is not None:
        timeScop = dpf.Scoping()
        timeScop.Ids = [int(result_set_id)]
        sf_op.Connect(time_scoping_pin, timeScop)

    # extract failure values
    extract_field = dpf.operators.utility.extract_field()
    extract_field.inputs.fields_container.Connect(sf_op, 0)
    extract_field.inputs.indices.Connect([1]) # default is first field
    
    # define dpf workflow
    dpf_workflow = dpf.Workflow()
    dpf_workflow.Add(extract_field)
    dpf_workflow.SetOutputContour(extract_field)
    dpf_workflow.Record('wf_id', True)
    this.WorkflowId = dpf_workflow.GetRecordedId()
    
def _load_plugins():
    # utility function to load the dpf composites plugins
    import os
    import mech_dpf
    import Ans.DataProcessing as dpf
    
    version = Ansys.Utilities.ApplicationConfiguration.DefaultConfiguration.VersionInfo.VersionString
    dpf_root = os.path.join(os.environ["AWP_ROOT"+version], "dpf")
    composites_root = os.path.join(dpf_root, "plugins", "dpf_composites")
    dpf.DataProcessingCore.LoadLibrary('EngineeringData', os.path.join(dpf_root, 'bin', 'winx64', 'Ans.Dpf.EngineeringData.dll'), 'LoadOperators')
    dpf.DataProcessingCore.LoadLibrary('composites', os.path.join(composites_root, 'composite_operators.dll'), 'LoadOperators')
"""

ultimate_failure_plot = solution.AddPythonResult()
ultimate_failure_plot.Text = FAILURE_SCRIPT_Text % ('"ultimate"')
ultimate_failure_plot.Connect()
ultimate_failure_plot.Name = r"""Tsai-Hill Ultimate Failure"""

yield_failure_plot = solution.AddPythonResult()
yield_failure_plot.Text = FAILURE_SCRIPT_Text % ('"yield"')
yield_failure_plot.Connect()
yield_failure_plot.Name = r"""Hill Yield Failure"""