Overview of postprocessing capabilities in Ansys

Options
Javier Vique
Javier Vique Member, Employee Posts: 76
First Answer First Anniversary Name Dropper 5 Likes

In civil engineering, APDL has been traditionally used for both building up the models and postprocessing. Nowadays, many engineers are building their models in Mechanical, but they still use APDL snippet commands for getting some output data into txt files that are used afterwards in in-house tools. Which are the alternatives to this workflow?

Tagged:

Best Answer

  • Javier Vique
    Javier Vique Member, Employee Posts: 76
    First Answer First Anniversary Name Dropper 5 Likes
    Answer ✓
    Options

    The model of a 3D frame modeled by beam elements shown below is used as demo for giving an overview of the different options for postprocessing with Ansys. The final target of the user would be making an output of SMISC1 (axial force on i node) and SMISC2 (y bending on i node) on specific named selections (NS1 & NS2) for the last substep of the only available step. This output will be used in an in-house tool made in Excel.

    APDL snippet commands:
    *VWRITE commands are used for getting the output. Here the script:

    /POST1
    
    set,last
    
    *get,elem_min,elem,,num,mind           !Min element in model
    *get,elem_max,elem,,num,maxd           !Max element in model
    
    etable,etaxial,smisc,1
    etable,etbendingY,smisc,2
    
    *dim,aaxial,array,elem_max          
    *vget,aaxial(1,1),elem,elem_min,etab,etaxial
    
    *dim,abendingY,array,elem_max          
    *vget,abendingY(1,1),elem,elem_min,etab,etbendingY
    
    
    *do,i,1,%ARG1%
    CMSEL,S,NS_%i%
    *GET,ELEMCOUNT,ELEM,0,COUNT            !Elements in named selection
    *DIM,EARRAY,ARRA,ELEMCOUNT,1           !Elements array
    *VGET,EARRAY(1,1),ELEM,,ELIST          !Elements list
    *dim,emask,array,elem_max              !Mask to select elements in named selection
    *vget,emask(1),elem,,esel
    
    ALLSEL
    
    *cfopen,'Beams_NS_%i%','txt'
    *vlen,1
    *vwrite,'Beam','Axial','BendingY'
     (A11,X,A15,X,A15)
    
    
    *vmask,emask(1)
    *dim,results_ns%i%,array,ELEMCOUNT,2    
    *vfun,results_ns%i%(1,1),comp,aaxial(1,1)
    *vmask,emask(1)
    *vfun,results_ns%i%(1,2),comp,abendingY(1,1)  
    
    *vwrite,EARRAY(1,1),results_ns%i%(1,1),results_ns%i%(1,2)
     (F11.0,X,F15.3,X,F15.3)
    *cfclos
    *enddo
    

    Mechanical scripting:
    Two user-defined results (UDR) are created and they are evaluated for both named selections, making use of ExportToTextFile method. Here the script:

    import os
    
    folderpath = r'here_my_path'
    
    Solution = ExtAPI.DataModel.Project.Model.Analyses[0].Solution
    NamedSelections = ExtAPI.DataModel.GetObjectsByType(DataModelObjectCategory.NamedSelection)
    
    AxialSol = Solution.AddUserDefinedResult()
    AxialSol.Expression = 'SMISC1'
    BendingYSol = Solution.AddUserDefinedResult()
    BendingYSol.Expression = 'SMISC2'
    
    for NS in NamedSelections:
        axial_name = NS.Name + '_axial.txt'
        bendy_name = NS.Name + '_bendingY.txt'
        filename1 = os.path.join(folderpath,axial_name)
        filename2 = os.path.join(folderpath,bendy_name)
        AxialSol.ClearGeneratedData()
        AxialSol.Location = NS
        AxialSol.EvaluateAllResults()
        AxialSol.ExportToTextFile(filename1)
        BendingYSol.ClearGeneratedData()
        BendingYSol.Location = NS
        BendingYSol.EvaluateAllResults()
        BendingYSol.ExportToTextFile(filename2)
    

    An alternative, which would be more efficient, is using Result Reader and BEAM_AXIAL_F & BEAM_BENDING_M. Additionally, this would allow to make the export directly to Excel, but please be aware that the output is slightly different, since this output gives 2 values per element (node i and node j), so it is giving SMISC1-14 and SMISC2-15, not just SMISC1 & SMISC2. For details about result reader, please have a look at our user manual:
    https://ansyshelp.ansys.com/account/secured?returnurl=/Views/Secured/corp/v232/en/act_cust_mech/Postprocess_externalResultRetrieval.html?q=result reader

    Data Postprocessing Framework:
    DPF allows us to make this export, bringing the data into Excel. Here the script:

    ## Here an Excel is open following solution available in Dev Forum:
    # https://discuss.ansys.com/discussion/308/write-information-to-excel-example-on-mesh-quality
    import clr
    clr.AddReferenceByName('Microsoft.Office.Interop.Excel')
    from Microsoft.Office.Interop import Excel
    excel = Excel.ApplicationClass()
    excel.Visible = True # makes the Excel application visible to the user
    excel.ScreenUpdating = True     # Enables screen refreshing
    # opening a workbook
    filename = r"here_your_path\Book1.xlsx" #Specify existing Excel document
    workbook = excel.Workbooks.Open(filename)
    # adding a worksheet
    ws = workbook.Worksheets.Add() # Add a new worksheet and change its name
    ws.Name = "MyNewSheet"
    
    ws.Cells(1,1).Value = "Named Selection"
    ws.Cells(1,2).Value = "Element"
    ws.Cells(1,3).Value = "Axial"
    ws.Cells(1,4).Value = "BendingY"
    i = 2
    
    ## Here mech-dpf is used to get SMISC1 & SMISC2
    import os
    import mech_dpf
    import Ans.DataProcessing as dpf
    
    folderpath = r'D:\01.Support\Salesforce\00033923\DPF_results'
    
    analysis=Model.Analyses[0]
    dataSource = dpf.DataSources(analysis.ResultFileName)
    
    model=dpf.Model(dataSource)
    
    named_selections = ['NS_1','NS_2']
    
    op_smisc = dpf.operators.result.mapdl.smisc()
    op_smisc.inputs.data_sources.Connect(dataSource)
    op_smisc.inputs.item_index.Connect(1)
    smisc1 = op_smisc.outputs.fields_container.GetData()
    
    op_smisc.inputs.item_index.Connect(2)
    smisc2 = op_smisc.outputs.fields_container.GetData()
    
    for j in named_selections:
        ns = model.GetNamedSelection(j)
        for elemid in ns.Ids:
            ws.Cells(i,1).Value = j
            ws.Cells(i,2).Value = elemid
            ws.Cells(i,3).Value = smisc1[0].GetEntityDataById(elemid)[0]
            ws.Cells(i,4).Value = smisc2[0].GetEntityDataById(elemid)[0]
            i = i + 1
    
    print('Script has finished, review your Excel file')
    

    For big models, there might be some alternatives to GetEntityDataById(elemid) to get results faster.

    Below the verification for element 100: