First part of the post process script. Error and periodicity

This commit is contained in:
Aloma Blanch 2020-07-16 19:41:59 -05:00
commit d6416a4977
2 changed files with 129 additions and 0 deletions

78
functions.py Normal file
View File

@ -0,0 +1,78 @@
#!/usr/bin/env python
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as mtick
from tkinter import Tk
from tkinter.filedialog import askopenfilename, asksaveasfile, askdirectory
import pandas as pd
import tkinter as tk
from scipy.signal import find_peaks
def error_plot(folder,t_step,r_criteria,save):
# Load iterations and residual error
histor = folder + '/histor.dat'
input = open(histor, 'r')
output = open(folder + "/histor_better.dat", 'w')
output.writelines(line.strip() +'\n' for line in input)
input.close()
output.close()
error_info = pd.read_csv(folder + "/histor_better.dat", sep=' ', header=None, usecols=(0,1,2))
# Select only last iteration of residual error
error=[]
for n in range(1,error_info.shape[0]):
if (error_info[0][n]>error_info[0][n-1]):
error.append(error_info[2][n-1])
time = np.linspace(start = t_step, stop = len(error)*t_step, num = len(error))
# Plots of interest
# Liniar Scale
plt.figure()
plt.plot(time,error)
plt.plot(time,r_criteria*np.ones(len(error)),'r')
plt.ylabel('Residual error')
plt.xlabel('Time steps')
plt.title('Last nonlinear residual error for each time step')
plt.grid(True)
if save: plt.savefig(plt_folder + case + '_Last_nonlin_res_error.pdf')
# Semilog scale
plt.figure()
plt.semilogy(time,error)
plt.semilogy(time,r_criteria*np.ones(len(error)),'r:')
plt.ylabel('Residual error')
plt.xlabel('Time steps')
plt.title('Log - Last nonlinear residual error for each time step')
plt.grid(True)
if save: plt.savefig(plt_folder + case + '_Log_Last_nonlin_res_error.pdf')
def periodicity(project,folder,dt,T_cyc,n_cyc):
pressure = np.loadtxt(folder+'/PHistRCR.dat',skiprows=2,)
time = np.linspace(0,T_cyc*n_cyc,round(T_cyc/dt*n_cyc))
peak_P = []
peak_P_pos = []
Nc = round(T_cyc/dt)
for i in range(0,n_cyc):
peak_P.append(np.amax(pressure[i*Nc:Nc*(i+1),-1])/1333.22)
peak_P_pos.append(np.where(pressure[i*Nc:Nc*(i+1),-1] == np.amax(pressure[i*Nc:Nc*(i+1),-1]))[0][0]+Nc*i)
peak_Pdiff = [peak_P[n]-peak_P[n-1] for n in range(1,len(peak_P))]
peak_Pdiff = list(map(abs, peak_Pdiff))
fig, ax = plt.subplots()
ax.plot(time,pressure[:,-1]/1333.22)
ax.plot(time[peak_P_pos], peak_P,'ro',label='Cylce pike')
ax.set(xlabel='time [s]', ylabel='Pressure [mmHg]',
title='Pressure @ last outlet')
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.legend(loc=0)
plt.show()
if (peak_Pdiff[-1]<=1): print('The numerical simulation \'{0}\' has achieve periodicity!\nSystolic Blood Pressure (SBP):\nsecond-last cycle = {1:.2f} mmHg,\nlast cycle = {2:.2f} mmHg,\n\u0394mmHg = {3:.2f} mmHg'.format(project,peak_P[-2],peak_P[-1],peak_Pdiff[-1]))

51
main.py Normal file
View File

@ -0,0 +1,51 @@
#!/usr/bin/env python
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as mtick
from tkinter import Tk
from tkinter.filedialog import askopenfilename, asksaveasfile, askdirectory
import pandas as pd
import tkinter as tk
import os.path
from scipy.signal import find_peaks_cwt
from scipy import signal
import statistics
from functions import error_plot, periodicity
# Selct dir
Tk().withdraw()
folder = askdirectory()
project_folder = os.path.dirname(folder)
project = os.path.basename(project_folder)
# Input parameters
T_cyc = 0.477
n_cyc = 5
# Read important parameters from - solver.inp file
mylines = [] # Declare an empty list named mylines.
with open (project_folder + '/solver.inp', 'rt') as myfile: # Open lorem.txt for reading text data.
for myline in myfile: # For each line, stored as myline,
mylines.append(myline) # add its contents to mylines.
# print(mylines)
# Number of Timesteps - idx 3
# Idea: remove the text to extract the number, the text part will be the same no matter the simulation
N_ts = int(mylines[3][20:-1])
# Time Step Size - idx 4
dt = float(mylines[4][16:-1])
# Residual criteria - idx 4
rc = float(mylines[26][18:-1])
# Cehcking convergency and periodicity
error_plot(folder,dt,rc,False)
periodicity(project,folder,dt,T_cyc,n_cyc)