Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- warnings.filterwarnings('ignore')
- from ipywidgets import widgets, Output
- from IPython.display import display
- from IPython.html.widgets import *
- from IPython.display import clear_output
- import numpy as np
- import matplotlib.pyplot as plt
- from matplotlib.pyplot import figure as fig
- %matplotlib inline
- sub = ''
- mode = ''
- D0 = 20 #Initial grain size [μm]
- K0 = 10**10 #Kinetic cosntant [μm^2/s]
- n = 0.5 #Time exponent
- Q = 221000 #Activation energy for grain growth [J/mol]
- R = 8.314 #Ideal gas constant [J/(mol*K)]
- HR = 0.1 #float(input('What is the heating rate?')) #Heating rate [K/s]
- Tiso = 1473 #float(input('What is the isothermal heat treating temperature? (in Kelvin)')) #Isothermal heat treatment temperature [K]
- tiso = 50 #float(input('How long was the material keept at this temperature? (in seconds)')) #The time for the isothermal heat treatment temperature [s]
- CR = 1 #float(input('What is the cooling rate?'))#Cooling rate [K/s]
- N = 100 #The number of steps the integration will be done in
- t1 = (Tiso-298)/HR #The first time value for reaching the isothermal heat treatment temperature [s]
- dt1 = t1/N #The time increment for t1 [s]
- T_hr = [] #List of temperatures during the heating phase [K]
- th=0 #The time values for T_hr
- T_cr=[] #List of temperatures during the cooling phase [K]
- for i in range(N): #This for loop makes the list of temperatures during the heating phase
- T_hr.append(298 + HR*th)
- th += dt1
- tc = 0 #Time valyes for T_cr
- tcool = (Tiso-298)/CR #The time it takes for the material to cool down [s]
- dt2 = tcool/N #The time increment for tcool
- for i in range(N): #This loop makes the list of temperatures during the cooling phase
- T_cr.append(Tiso -tc*CR)
- tc += dt2
- sum1 = 0 #Sum of the integral during the heating phase [s]
- for i in range(N): #This loop calculates the integral numerically
- sum1 += np.exp(-Q/(R*T_hr[i]))*dt1 - np.exp(-Q/(R*298))*dt1
- #print(sum1)
- sum2 = 0 #Sum of the integral during the cooling phase [s]
- for i in range(N): #This loop calculates the integral numerically
- sum2 += np.exp(-Q/(R*T_cr[i]))*dt2 - np.exp(-Q/(R*298))*dt2
- D = (D0**(1/n) + (K0*sum1) + (K0*np.exp(-Q/(R*Tiso))*tiso) + (K0*sum2))**n #The grain size [μm]
- print(K0*sum1)
- #print(K0*np.exp(-Q/(R*Tiso))*tiso)
- #print(K0*sum2)
- print(D)
- def main(mode): #Function for plotting different cases of grain growth
- D0 = 20
- K0 = 10**10
- n = 0.5
- Q = 221000
- R = 8.314
- HR = 0.1 #float(input('What is the heating rate?'))
- Tiso = 1473 #float(input('What is the isothermal heat treating temperature? (in Kelvin)'))
- tiso = 50 #float(input('How long was the material keept at this temperature? (in seconds)'))
- CR = 1 #float(input('What is the cooling rate?'))
- N = 100
- t1 = (Tiso-298)/HR
- dt1 = t1/N
- T_hr = []
- th=0
- # Varying heating rate
- if mode == 'Varying HR':
- x1 = [] #List of x-values in the plot, which will be the heating rate
- y1 = [] #List of y-values in the plot, which will be the grain size
- for HR in range(10,200): #For loop which makes the list of heating rates and grain sizes
- x1.append(HR*0.01)
- t1 = (Tiso-298)/(HR)
- dt1 = t1/100
- th=0
- for i in range(100):
- T_hr.append(298 + HR*th)
- th += dt1
- sum1 = 0
- for i in range(100):
- sum1 += np.exp(-Q/(R*T_hr[i]))*dt1
- tc = 0
- tcool = (Tiso-298)/CR
- sum2 = 0
- dt2 = tcool/N
- for i in range(N):
- T_cr.append(Tiso -tc*CR)
- tc += dt2
- for i in range(N):
- sum2 += np.exp(-Q/(R*T_cr[i]))*dt2 - np.exp(-Q/(R*298))*dt2
- D = (D0**(1/n) + K0*sum1 + K0*np.exp(-Q/(R*Tiso))*tiso + K0*sum2)**n
- y1.append(D)
- plt.plot(x1,y1) #Plotting of the values
- plt.title('Varying heating rate')
- plt.xlabel('Heating rate [K/s]')
- plt.ylabel('Grain size [micrometer]')
- plt.show()
- #Varying heating rate
- elif mode == 'Varying CR':
- x2 = [] #List of cooling rates
- y2 = [] #List of grain sizes
- for CR in range(10,200):
- x2.append(CR*0.01)
- tc = 0
- tcool = (Tiso-298)/CR
- dt2 = tcool/N
- for i in range(N):
- T_cr.append(Tiso -tc*CR)
- tc += dt2
- sum2 = 0
- t1 = (Tiso-298)/(HR)
- dt1 = t1/100
- T_hr = []
- th=0
- for i in range(100):
- T_hr.append(298 + HR*th)
- th += dt1
- sum1 = 0
- for i in range(100):
- sum1 += np.exp(-Q/(R*T_hr[i]))*dt1
- for i in range(N):
- sum2 += np.exp(-Q/(R*T_cr[i]))*dt2 - np.exp(-Q/(R*298))*dt2
- D = (D0**(1/n) + K0*sum1 + K0*np.exp(-Q/(R*Tiso))*tiso + K0*sum2)**n
- y2.append(D)
- plt.plot(x2,y2)
- plt.title('Varying cooling rate')
- plt.xlabel('Cooling rate [K/s]')
- plt.ylabel('Grain size [micrometer]')
- plt.show()
- #Varying Tiso
- elif mode == 'Varying Tiso':
- x3 = [] #List of isothermal temperatures
- y3 = [] #List of grain sizes
- for Tiso in range(1173,1673):
- x3.append(Tiso)
- N = 100
- t1 = (Tiso-298)/HR
- dt1 = t1/N
- T_hr = []
- th=0
- for i in range(N):
- T_hr.append(298 + HR*th)
- th += dt1
- tc = 0
- tcool = (Tiso-298)/CR
- dt2 = tcool/N
- for i in range(N):
- T_cr.append(Tiso -tc*CR)
- tc += dt2
- sum2 = 0
- for i in range(N):
- sum2 += np.exp(-Q/(R*T_cr[i]))*dt2 - np.exp(-Q/(R*298))*dt2
- sum1 = 0
- for i in range(N):
- sum1 += np.exp(-Q/(R*T_hr[i]))*dt1 - np.exp(-Q/(R*298))*dt1
- D = (D0**(1/n) + K0*sum1 + K0*np.exp(-Q/(R*Tiso))*tiso + K0*sum2)**n
- y3.append(D)
- plt.plot(x3,y3)
- plt.title('Varying Tiso')
- plt.xlabel('Tiso')
- plt.ylabel('Grain size [micrometer]')
- plt.show()
- #HR = 0.1 #float(input('What is the heating rate?'))
- #Tiso = 1473 #float(input('What is the isothermal heat treating temperature? (in Kelvin)'))
- #tiso = 50 #float(input('How long was the material keept at this temperature? (in seconds)'))
- #CR = 1
- #varying tiso
- elif mode == 'Varying tiso':
- x4= [] #List of isothermal time values
- y4 = [] #List of grain sizes
- for tiso in range(1,100):
- x4.append(tiso)
- for i in range(N):
- T_hr.append(298 + HR*th)
- th += dt1
- tc = 0
- tcool = (Tiso-298)/CR
- dt2 = tcool/N
- for i in range(N):
- T_cr.append(Tiso -tc*CR)
- tc += dt2
- sum1 = 0
- for i in range(N):
- sum1 += np.exp(-Q/(R*T_hr[i]))*dt1 - np.exp(-Q/(R*298))*dt1
- #print(sum1)
- sum2 = 0
- for i in range(N):
- sum2 += np.exp(-Q/(R*T_cr[i]))*dt2 - np.exp(-Q/(R*298))*dt2
- D = (D0**(1/n) + K0*sum1 + K0*np.exp(-Q/(R*Tiso))*tiso + K0*sum2)**n
- y4.append(D)
- plt.plot(x4,y4)
- plt.title('Varying tiso')
- plt.xlabel('tiso')
- plt.ylabel('Grain size [micrometer]')
- plt.show()
- else:
- print('Unknown mode') #If the mode input isn't recognised, this will be printed
- def sub_task_chooser(sub): #This is a part of the code for making buttons
- #The values for each button is assigned here
- if sub == 1:
- mode = 'Varying HR'
- if sub == 2:
- mode = 'Varying CR'
- if sub == 3:
- mode = 'Varying Tiso'
- if sub == 4:
- mode = 'Varying tiso'
- main(mode)
- sub_menu = widgets.ToggleButtons(
- # {'names':corresponding values,}
- options={'Varying HR':1, 'Varying CR':2, 'Varying Tiso':3, 'Varying tiso':4},
- value = 2, #default value
- description='Choose case:', #name
- button_style='info', #blue buttons
- )
- interact(sub_task_chooser, sub = sub_menu)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement