Calculating a KPI and Exporting into a Graph

This is an example of a run(scenario) script that loads coarse-graining (cg) dataset and based on two results computes the value of KPI for each timestep and creates a graph in the scenario with the temporal evolution of the KPI. The graph is saved into the scenario, the raw data into a csv file and the graphe is exported into a interactive HTML file.

'''This scripts loads the cg_dataset of a scenario. For each timestep in the dataset, it computes a KPI value 
    that is equal to the average magnitude of the velocity of the regions with Solid Fraction > 0.2. A graph is created 
    with the temporal evolution of the KPI and data saved into the scenario and a CSV file, and the graph exported into a HTML file'''

import iota

def run(scenario):

    ## Settings for the results ##
    mesh_name = 'Simulation_Domain'                           #Name of the mesh that contains the result

    res1_name = 'Solid Fraction'                              #Name of the result 1 to operate with
    res1_analysis = 'none'                                    #Name of the analysis of the res1
    res1_threshold = 0.2                                      #Threshold value of result res1

    res2_name = 'Velocity'                                    #Name of the result 2 to operate with
    res2_analysis = 'none'                                    #Name of the analysis of the res2
    ########################################

    #### Settings for graph saving ######
    Name_graph = 'Mean VeloMag(solidFraction larger 0.2)'                 #Name for the graph for the database
    Title_graph = 'Mean Velocity Magnitude for SolidFraction > 0.2'       #Title to shown in the graph
    xtitle = 'Time (s)'                                                   #Title for x-axis 
    ytitle = 'KPI Mean Velocity [m/s]'                                    #Tile for y-axis
    legend = ['Mean Velocity']                                            #Name for the graph legend
    graph_file = '{}/graphs/{}'.format(scenario.directory,Name_graph)     #Output name and path for exporting graph
    x_data = []                                                           #To store the x-data
    y_data = []                                                           #To store the y-data  
    ########################################     

    cg_dataset = scenario.get_cg_dataset()                    #Get the coarse-graining dataset of the scenario
    cg_data = cg.data()                                       #Load the data of the coarse-graining dataset
    ListOfTimesteps = cg_data.timesteps()                     #Get the number of timesteps of the dataset

    for cont, ts in enumerate(ListOfTimesteps):                                             #For each step in the dataset
        res1_values = cg_data.get_result(cont, mesh_name, res1_name, res1_analysis)         #Get the values of the Solid Fraction
        res2_values = cg_data.get_result(cont, mesh_name, res2_name, res2_analysis)         #Get the values of the Velocity [VX,VY,VZ]
        res2_magnitude = res2_values.modulus()                               #Compute magnitude of the velocity
        KPI_current = res2_magnitude[res1_values > res1_threshold].mean()    #Compute mean of velocity magnitude values if solid fraction > threshold

        y_data.append(KPI_current)                                           #Append the value of KPI to the y-values 
        x_data.append(float(ts))                                             #Append the time value of the step to the x-values 

    graph = scenario.add_graph(name=Name_graph, title=Title_graph, xdata=x_data, ydata=[y_data], 
        xtitle=xtitle, ytitle=ytitle, legend=legend)        #Save the graph data to database (including raw data to csv file)
    graph.export_html(graph_file)                                #Export the graph into html format

Last updated

Was this helpful?