A true stress-strain curve is often needed when we want to try a finite element analysis. Although there are some other methods to get an approximative curve from engineering stress-strain curve, the result is often above the true curve. And   the method from Liu Bing-yu is suitable for us to get a more accurate curve and my script refers to his article.

#!/usr/bin/env python
#ect.py
#Anaconda is recommended for users.
#The script is used to convert engineering stress-strain curve to true stress-strain curve.
#Author: Li Xin, a graduate student studying Materials Science & Engineering in USTB
#Email: lixin@xs.ustb.edu.cn
#Reference: Liu Bing-yu,one diagrammatic solution on true stress-strain curve --analysis for necking process
import os
import pandas as pd
import numpy as np
import tkinter as tk
import matplotlib.pyplot as plt
from tkinter import filedialog
def select_inputfile():
root = tk.Tk()
root.withdraw()
filepath = filedialog.askopenfilename(title=u'Input File',filetypes=[('csv file', '.csv')])
print('input path:',filepath)
return filepath
def select_savefile():
root = tk.Tk()
root.withdraw()
save_path = filedialog.asksaveasfilename(title=u'Save File',filetypes=[('csv file', '.csv')])+".csv"
print('save path:',save_path)
return save_path
print("**********ect.py**********")
print("a script to convert engineering stress-strain curve to true stress-strain curve")
print("**********ect.py**********")
#input
gauge_length = float(input())
print("Please input original size of cross-section(mm^2)")
size_cross_section = float(input())
print("Please input final size of cross-section(mm^2)")
final_cross_section = float(input())
print("Please input the data file of engineering stress(.csv)")
print("Format: | strain / % | stress / MPa | displacement / mm | ***No header is needed*** ")
path = select_inputfile()
stress = data_file["stress"]
strain = data_file["strain"]
displacement = data_file["displacement"]
#find the max stress
maxn = 0
num = 0
for i in range(0,len(stress)):
if stress[i] >= maxn:
maxn = stress[i]
num = i
print("Max Stress:" ,str(maxn),"Num", str(num))
print("Stran:",str(strain[num]),"Displacement:",str(displacement[num]))
#calculate Max stress size
max_size_cross_section = size_cross_section*gauge_length/(gauge_length+float(displacement[num]))
a = (final_cross_section - max_size_cross_section) / (float(displacement.iloc[-1]) - float(displacement[num]))
b = (max_size_cross_section * float(displacement.iloc[-1]) - float(displacement[num]) * final_cross_section) / (float(displacement.iloc[-1]) - float(displacement[num]))
print("Fitting curve:","Size =", a,"× Displacement +",b)
#calculate true strain
true_strain = []
for i in range(0,len(stress)):
true_strain.append(np.log(float(strain[i]) * 0.01 + 1) *100)
data_file["true_strain"] = true_strain
#calculate true stress of uniform deformation
true_stress = []
for i in range(0,num):
true_stress.append(float(stress[i])*(float(strain[i])*0.01+1))
for i in range(num,len(stress)):
true_stress.append(float(stress[i])*size_cross_section/(a*float(displacement[i])+b))
data_file["true_stress"] = true_stress
#output
print("Please select the directory where you want to output data:")
out_path = select_savefile()
data_file.to_csv(out_path,index = False, header = True)
#plot the diagram
plt.title("true stress-strain curve")
plt.xlabel("Strain / %")
plt.ylabel("Stress / MPa")
plt.plot(true_strain, true_stress)
plt.show()
print("Work done")
print("**********ect.py**********")
os.system('pause')