How to export Min/Max temp time history at multiple named selections as a csv?

Rohith Patchigolla
Rohith Patchigolla Member, Moderator, Employee Posts: 174
100 Comments Photogenic Ansys Employee 5 Likes
✭✭✭✭

How to export Min or Max temperature time history (same as Tabular Data) at multiple named selections as a csv from Mechanical thermal analysis?

Answers

  • Rohith Patchigolla
    Rohith Patchigolla Member, Moderator, Employee Posts: 174
    100 Comments Photogenic Ansys Employee 5 Likes
    ✭✭✭✭
    edited August 28

    Below script exports the Max or Min time history as csv for multiple named selections (say T1 and T2) in the analysis solver directory.

    import mech_dpf
    import Ans.DataProcessing as dpf
    import csv
    import os
    import time
    startTime = time.time()
    
    
    mech_dpf.setExtAPI(ExtAPI)
    analysis = DataModel.AnalysisList[0]
    
    # Specify the file name for Max and Min data
    filenameMax = "output_max.csv"
    filepath = analysis.WorkingDir
    fullpathMax = os.path.join(filepath,filenameMax)
    
    filenameMin = "output_min.csv"
    filepath = analysis.WorkingDir
    fullpathMin = os.path.join(filepath,filenameMin)
    
    dataSource = dpf.DataSources(analysis.ResultFileName)
    
    time_freq_support = dpf.operators.metadata.time_freq_provider(data_sources = dataSource)
    n_sets = time_freq_support.outputs.time_freq_support.GetData().NumberSets
    timeScop = dpf.Scoping()
    timeScop.Ids = range(1, n_sets + 1)
    
    NamedSelNames = ['T1', 'T2', 'T3', 'T4', 'T5', 'T6', 'T7', 'T8', 'T9', 'T10', 'T11', 'T12', 'T13', 'T14', 'T15', 'T16', 'T17', 'T18', 'T19', 'T20', 'T21', 'T22', 'T23', 'T24', 'T25', 'T26', 'T27', 'T28', 'T29', 'T30', 'T31', 'T32', 'T33', 'T34']
    fullDataMax = []
    fullDataMin = []
    for nsName in NamedSelNames:
        ns_op = dpf.operators.scoping.on_named_selection(data_sources = dataSource, requested_location = 'Nodal', named_selection_name = nsName)
        temp_op = dpf.operators.result.temperature(data_sources = dataSource, time_scoping = timeScop, mesh_scoping = ns_op.outputs.mesh_scoping)
        temp_fc = temp_op.outputs.fields_container.GetData()
        minTemps = []
        maxTemps = []
    
        min_max_fc_op = dpf.operators.min_max.min_max_fc() # operator instantiation
        min_max_fc_op.inputs.fields_container.Connect(temp_fc)
        minTemps = min_max_fc_op.outputs.field_min.GetData().Data
        maxTemps = min_max_fc_op.outputs.field_max.GetData().Data
    
        fullDataMax.append(maxTemps)
        fullDataMin.append(minTemps)
    
    fullDataMax = list(zip(*fullDataMax))
    fullDataMin = list(zip(*fullDataMin))
    
    # Writing to csv file
    with open(fullpathMax, mode='w') as file:
        writer = csv.writer(file, lineterminator='\n')
        writer.writerow(NamedSelNames)
        for row in fullDataMax:
            writer.writerow(row)
    
    with open(fullpathMin, mode='w') as file:
        writer = csv.writer(file, lineterminator='\n')
        writer.writerow(NamedSelNames)
        for row in fullDataMin:
            writer.writerow(row)
    endTime = time.time()
    TotalTime = endTime - startTime
    ExtAPI.Log.WriteMessage("Total Time taken for export is " + str(TotalTime) + " Sec")