How to export time history temperature result at a node as a csv file in batch in Mechanical?

Options
Rohith Patchigolla
Rohith Patchigolla Member, Moderator, Employee Posts: 115
First Answer First Anniversary Name Dropper Solution Developer Community of Practice Member

How to export time history temperature result at a node as a csv file in batch in Mechanical?

Answers

  • Rohith Patchigolla
    Rohith Patchigolla Member, Moderator, Employee Posts: 115
    First Answer First Anniversary Name Dropper Solution Developer Community of Practice Member
    Options

    Solution: Use Python Code object (added under Solution) with Target Callback --> "After Post"

    The below is the script in Python Code which exports time vs temperature data at a node (provided as an input) and exports this as a csv file in User Files directory of the workbench project.

    def after_post(this, solution):# Do not edit this line
        import mech_dpf
        import Ans.DataProcessing as dpf 
        import os
        import wbjn
        mech_dpf.setExtAPI(ExtAPI)
    
        dpn = wbjn.ExecuteCommand(ExtAPI,"returnValue(a+Parameters.GetActiveDesignPoint().Name)",a="DP")
        projectDir = wbjn.ExecuteCommand(ExtAPI,"returnValue(GetUserFilesDirectory())")
    
        #Input node number
        nodeId = 10264
    
        #Get the data source (i.e. result file)
        dataSource = dpf.DataSources(solution.Parent.ResultFileName)
    
        #Create result operator
        myResName = 'Probe_Temperature'
        myRes = dpf.operators.result.temperature()
    
        #Get the time data corresponding to result sets
        time_provider = dpf.operators.metadata.time_freq_provider()
        time_provider.inputs.data_sources.Connect(dataSource)
        numSets = time_provider.outputs.time_freq_support.GetData().NumberSets
        timeids = time_provider.outputs.time_freq_support.GetData().TimeFreqs.Data
    
        result_set_ids = []
        for i in range(numSets):
            result_set_ids.append(i+1)
    
        #Create time scoping operator
        time_scoping = dpf.Scoping()
        time_scoping.Location = dpf.locations.time_freq_sets
        time_scoping.Ids = result_set_ids
    
        #Provide Inputs
        myRes.inputs.data_sources.Connect(dataSource)
        myRes.inputs.time_scoping.Connect(time_scoping)
    
        #Get Outputs
        myRes_fields = myRes.outputs.fields_container.GetData()
        data = []
        #Loop over all available time points to get the displacement at the node
        for i in range(numSets):
            myRes_field = myRes_fields[i]
            data.append(myRes_field.GetEntityDataById(nodeId)[0])
    
        filePath = os.path.join(projectDir,dpn + '_' + myResName + '.csv')
        if os.path.isfile(filePath):
            os.remove(filePath)
        ctr = 0
        with open(filePath,'w') as f:
            f.write('Time Step,' + myResName + '\n')
            for d in data:
                f.write(str(timeids[ctr]) + ',' + str(d) + '\n')
                ctr+=1