Source code for postprocess_performance

# -*- coding: utf-8 -*-
"""
This file is part of PyFrac.

Created by Haseeb Zia on 04.03.18.
Copyright (c) ECOLE POLYTECHNIQUE FEDERALE DE LAUSANNE, Switzerland, Geo-Energy Laboratory, 2016-2020.
All rights reserved. See the LICENSE.TXT file for more details.
"""

import sys
if "win32" in sys.platform or "win64" in sys.platform:
    slash = "\\"
else:
    slash = "/"


import dill
import numpy as np
import logging
import re
import os

from properties import PlotProperties
from visualization import plot_variable_vs_time


[docs]def load_performance_data(address, sim_name='simulation'): """ This function loads the performance data in the given simulation. If no simulation name is provided, the most recent simulation will be loaded. Arguments: address (string): -- the disk address where the simulation results are saved sim_name (string): -- the name of the simulation returns: perf_data (list): -- the loaded performance data in the form of a list of IterationProperties objects.( \ see :py:class:`properties.IterationProperties` for details). """ log = logging.getLogger('PyFrac.load_performace_data') log.info("---loading performance data---\n") if address is None: address = '.' + slash + '_simulation_data_PyFrac' if address[-1] != slash: address = address + slash if re.match('\d+-\d+-\d+__\d+_\d+_\d+', sim_name[-20:]): sim_full_name = sim_name else: simulations = os.listdir(address) time_stamps = [] for i in simulations: if re.match(sim_name + '__\d+-\d+-\d+__\d+_\d+_\d+', i): time_stamps.append(i[-20:]) if len(time_stamps) == 0: raise ValueError('Simulation not found! The address might be incorrect.') Tmst_sorted = sorted(time_stamps) sim_full_name = sim_name + '__' + Tmst_sorted[-1] filename = address + sim_full_name + slash + 'perf_data.dat' try: with open(filename, 'rb') as inp: perf_data = dill.load(inp) except FileNotFoundError: raise ValueError("Performance data not found! Check if it's saving is enabled in simulation properties.") return perf_data
#-----------------------------------------------------------------------------------------------------------------------
[docs]def get_performance_variable(perf_data, iteration, variable): """ This function gets the required variable from the specified iteration. Arguments: perf_data (list): -- the loaded performance data in the form of a list of IterationProperties objects.(see :py:class:`properties.IterationProperties` for details). iteration (string): -- the type of iteration (see :py:class:`properties.IterationProperties` for details). variable (string): -- the name of the variable to be retrieved. returns: - var_list (list) -- the loaded variable. - time_list (list) -- the corresponding simulated times at which the variable was collected. - N_list (list) -- the corresponding number of elements in the fracture at the time step at which the \ variable was collected. """ var_list = [] time_list = [] N_list = [] def append_variable(Iteration, variable): var_list.append(getattr(Iteration, variable)) time_list.append(node.time) N_list.append(node.NumbOfElts) for i_node, node in enumerate(perf_data): if iteration == 'time step': append_variable(node, variable) else: for i_TS_attempt, TS_attempt in enumerate(node.attempts_data): if iteration == 'time step attempt': append_variable(TS_attempt, variable) else: for i_sameFP_inj, sameFP_inj in enumerate(TS_attempt.sameFront_data): if iteration == 'same front': append_variable(sameFP_inj, variable) else: for i_nonLinSolve, nonLinSolve_itr in enumerate(sameFP_inj.nonLinSolve_data): if iteration == 'nonlinear system solve': append_variable(nonLinSolve_itr, variable) else: for i_widthConstraint, widthConstraint_Itr in enumerate( nonLinSolve_itr.widthConstraintItr_data): if iteration == 'width constraint iteration': append_variable(widthConstraint_Itr, variable) else: for i_linearSolve, linearSolve_Itr in enumerate( widthConstraint_Itr.linearSolve_data): if iteration == 'linear system solve': append_variable(linearSolve_Itr, variable) for i_RKLSolve, RKLSolve_Itr in enumerate( widthConstraint_Itr.RKL_data): if iteration == 'RKL time step': append_variable(RKLSolve_Itr, variable) for i_extFP_inj, extFP_inj in enumerate(TS_attempt.extendedFront_data): if iteration == 'extended front': append_variable(extFP_inj, variable) else: for i_tipInv_itr, tipInv_itr in enumerate(extFP_inj.tipInv_data): if iteration == 'tip inversion': append_variable(tipInv_itr, variable) else: for i_brentq_itr, brentq_itr in enumerate(tipInv_itr.brentMethod_data): if iteration == 'Brent method': append_variable(brentq_itr, variable) for i_tipWidth_itr, tipWidth_itr in enumerate(extFP_inj.tipWidth_data): if iteration == 'tip width': append_variable(tipWidth_itr, variable) else: for i_brentq_itr, brentq_itr in enumerate(tipWidth_itr.brentMethod_data): if iteration == 'Brent method': append_variable(brentq_itr, variable) for i_nonLinSolve, nonLinSolve_itr in enumerate(extFP_inj.nonLinSolve_data): if iteration == 'nonlinear system solve': append_variable(nonLinSolve_itr, variable) else: for i_widthConstraint, widthConstraint_Itr in enumerate( nonLinSolve_itr.widthConstraintItr_data): if iteration == 'width constraint iteration': append_variable(widthConstraint_Itr, variable) else: for i_linearSolve, linearSolve_Itr in enumerate( widthConstraint_Itr.linearSolve_data): if iteration == 'linear system solve': append_variable(linearSolve_Itr, variable) for i_RKLSolve, RKLSolve_Itr in enumerate( widthConstraint_Itr.RKL_data): if iteration == 'RKL time step': append_variable(RKLSolve_Itr, variable) return var_list, time_list, N_list
#-----------------------------------------------------------------------------------------------------------------------
[docs]def plot_performance(address, variable, sim_name='simulation', fig=None, plot_prop=None, plot_vs_N=False): """ This function plot the performance variable from the given simulation. Arguments: address (string): -- the disk location where the results of the simulation were saved. variable (string): -- Currently, the following variables are supported: =============================== ================================================ variable meaning =============================== ================================================ 'time step attempts' the number of attempts taken for the time step 'fracture front iterations' fracture front iterations (including the fixed front iteration) 'tip inversion iterations' the iterations taken by the brentq method to converge while inverting \ the tip asymptote 'width constraint iterations' the iterations taken to converge on closed cells 'Picard iterations' the number of times the linear system is solved 'CPU time: time steps' the CPU time taken by each of the time steps 'CPU time: time step attempts' the CPU time taken by each of the time step attempt =============================== ================================================ sim_name(string): -- the name of the simulation. fig (Figure): -- a figure to superimpose on plot_prop (PlotProperties): -- a PlotProperties object plot_vs_N (bool): -- if True, a plot of the variable versus the number of cells will also be plotted. """ perf_data = load_performance_data(address, sim_name) if variable in ['time step attempts']: var_list, time_list, N_list = get_performance_variable(perf_data, 'time step', 'iterations') elif variable in ['fracture front iterations']: var_list, time_list, N_list = get_performance_variable(perf_data, 'time step attempt', 'iterations') elif variable in ['tip inversion iterations']: var_list, time_list, N_list = get_performance_variable(perf_data, 'Brent method', 'iterations') elif variable in ['width constraint iterations']: var_list, time_list, N_list = get_performance_variable(perf_data, 'nonlinear system solve', 'iterations') elif variable in ['Picard iterations']: var_list, time_list, N_list = get_performance_variable(perf_data, 'width constraint iteration', 'iterations') elif variable in ['RKL substeps']: var_list, time_list, N_list = get_performance_variable(perf_data, 'RKL time step', 'iterations') elif variable in ['CPU time: time steps']: t_start_list, time_list, N_list = get_performance_variable(perf_data, 'time step', 'CpuTime_start') del time_list, N_list t_end_list, time_list, N_list = get_performance_variable(perf_data, 'time step', 'CpuTime_end') var_list = [i - j for i, j in zip(t_end_list, t_start_list)] elif variable in ['CPU time: time step attempts']: t_start_list, time_list, N_list = get_performance_variable(perf_data, 'time step attempt', 'CpuTime_start') del time_list, N_list t_end_list, time_list, N_list = get_performance_variable(perf_data, 'time step attempt', 'CpuTime_end') var_list = [i - j for i, j in zip(t_end_list, t_start_list)] else: raise ValueError("Cannot recognize the required variable.") var_list_np = np.asarray(var_list) del var_list time_list_np = np.asarray(time_list) del time_list N_list_np = np.asarray(N_list) del N_list if plot_prop is None: plot_prop = PlotProperties(line_style='.') if plot_vs_N: fig = plot_variable_vs_time(N_list_np, var_list_np, fig=fig, plot_prop=plot_prop, label='fracture front iterations') ax = fig.get_axes()[0] ax.set_ylabel(variable) ax.set_xlabel('number of elements') else: fig = plot_variable_vs_time(time_list_np, var_list_np, fig=fig, plot_prop=plot_prop, label=variable) ax = fig.get_axes()[0] ax.set_ylabel(variable) ax.set_xlabel('time') return fig
#----------------------------------------------------------------------------------------------------------------------- #-----------------------------------------------------------------------------------------------------------------------