Export Displacements

Goal

This script demonstrates how to export displacements to a text file once the simulation is solved. Output of the script is an ASCII file with node number, XYZ location and UX/UY/UZ values over the entire model.

Code

def after_post(this, solution):# Do not edit this line
    """
    Called after post processing.
    Keyword Arguments : 
        this -- this python object
        solution -- Solution
    """
    # This script shows how to export displacements in a text file once a simulation has been run
    # Output of the script is an ASCII file with node number, XYZ location and UX/UY/UZ values over 
    # the entire model
    
    # The script is working with 'after_post' or 'after_solve' callback
    # You may place the Python code either under the analysis or under solution

    # We are using dpf (Data Processing Framework) for results extraction
    import mech_dpf
    import Ans.DataProcessing as dpf
    import os
    
    solution=ExtAPI.DataModel.GetObjectsByName('Solution')[0]
    
    # Get analysis and retrieve working directory
    analysis=solution.Parent
    rstFile = os.path.join(analysis.WorkingDir,'file.rst')
    
    # Define rst file as source of data for DPF
    dataSource = dpf.DataSources(rstFile)
    
    # Now create displacement operator and connect to datasource
    displacements = dpf.operators.result.displacement()
    displacements.inputs.data_sources.Connect(dataSource)
    
    # We assume a static analysis with one load step, retrieve actual deformation data from rst
    disp=displacements.outputs.fields_container.GetData()[0]
    
    # Get Mesh information to retrieve node coordinates
    meshData=ExtAPI.DataModel.Project.Model.Analyses[0].MeshData
    nodeIds=disp.ScopingIds # Get node list from results
    
    # Loop over nodes and write to txt file in user_files directory from project
    outFileName=os.path.join(analysis.WorkingDir,'..\\..\\..\\user_files\\exported_disp.txt')
    
    f_out=open(outFileName,'w')
    f_out.write('# Node \t X\tY\tZ\tUX\tUY\tUZ\n')
    for nid in sorted(nodeIds):
        node=meshData.NodeById(nid) # Node data for nid
        nodeDisp=disp.GetEntityDataById(nid) # displacement from node nid
        f_out.write(str(nid)+'\t'+str(node.X)+'\t'+str(node.Y)+'\t'+str(node.Z)+
        '\t'+str(nodeDisp[0])+'\t'+str(nodeDisp[1])+'\t'+str(nodeDisp[2])+'\n')
    f_out.close()