-
Notifications
You must be signed in to change notification settings - Fork 10
/
transformation_models.py
1069 lines (900 loc) · 34.6 KB
/
transformation_models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! -*- coding: utf-8 -*-
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from abc import abstractmethod
from scipy import integrate
from scipy.interpolate import splrep, splev, interp1d
from scipy.optimize import root
R = 8.314459
K = 273.15
def FahrenheitToCelsius(TF):
"""
Converts temperature in Fahrenheit to Celsius
"""
return (TF - 32.)*5./9.
def FC(**comp):
"""
Function that expresses the effects of the alloying elements on
on the kinetics of ferrite transformation
"""
C = comp.get('C', 0)
Mn = comp.get('Mn', 0)
Si = comp.get('Si', 0)
Ni = comp.get('Ni', 0)
Cr = comp.get('Cr', 0)
Mo = comp.get('Mo', 0)
return np.exp((1.0 + 6.31*C + 1.78*Mn + 0.31*Si + 1.12*Ni + 2.7*Cr + 4.06*Mo))
def PC(**comp):
"""
Function that expresses the effects of the alloying elements on
on the kinetics of pearlite transformation
"""
C = comp.get('C', 0)
Mn = comp.get('Mn', 0)
Si = comp.get('Si', 0)
Ni = comp.get('Ni', 0)
Cr = comp.get('Cr', 0)
Mo = comp.get('Mo', 0)
return np.exp(-4.25 + 4.12*C + 4.36*Mn + 0.44*Si + 1.71*Ni + 3.33*Cr + 5.19*np.sqrt(Mo))
def BC(**comp):
"""
Function that expresses the effects of the alloying elements on
on the kinetics of bainite transformation
"""
C = comp.get('C', 0)
Mn = comp.get('Mn', 0)
Ni = comp.get('Ni', 0)
Cr = comp.get('Cr', 0)
Mo = comp.get('Mo', 0)
return np.exp(-10.23 + 10.18*C + 0.85*Mn + 0.55*Ni + 0.9*Cr + 0.36*Mo)
def Ae1_Grange(**comp):
"""
Grange's equation for Ae1
"""
Mn = comp.get('Mn', 0)
Si = comp.get('Si', 0)
Ni = comp.get('Ni', 0)
Cr = comp.get('Cr', 0)
return FahrenheitToCelsius(1333 - 25*Mn + 40*Si - 26*Ni + 42*Cr)
def Ae3_Grange(**comp):
"""
Grange's equation for Ae3
"""
C = comp.get('C', 0)
Mn = comp.get('Mn', 0)
Si = comp.get('Si', 0)
Ni = comp.get('Ni', 0)
Cr = comp.get('Cr', 0)
return FahrenheitToCelsius(1570 - 323*C - 25*Mn + 80*Si - 32*Ni - 3*Cr)
def Ae1_Andrews(**comp):
"""
Andrews' equation for Ae1
"""
Mn = comp.get('Mn', 0)
Si = comp.get('Si', 0)
Ni = comp.get('Ni', 0)
Cr = comp.get('Cr', 0)
Mo = comp.get('Mo', 0)
W = comp.get('W', 0)
As = comp.get('As', 0)
return 723 - 16.9*Ni + 29.1*Si + 6.38*W - 10.7*Mn + 16.9*Cr + 290*As
def Ae3_Andrews(**comp):
"""
Andrews' equation for Ae3
"""
C = comp.get('C', 0)
Mn = comp.get('Mn', 0)
Si = comp.get('Si', 0)
Ni = comp.get('Ni', 0)
Cr = comp.get('Cr', 0)
Mo = comp.get('Mo', 0)
V = comp.get('V', 0)
W = comp.get('W', 0)
Cu = comp.get('Cu', 0)
# P = comp.get('P', 0)
# Al = comp.get('Al', 0)
# As = comp.get('As', 0)
# Ti = comp.get('Ti', 0)
return 910 - 203*np.sqrt(C) + 44.7*Si - 15.2*Ni + 31.5*Mo + 104*V + 13.1*W - \
30.0*Mn + 11.0*Cr + 20.0*Cu # - 700*P - 400*Al - 120*As - 400*Ti
def Bs_Li(**comp):
"""
Bainite start calculation from Li's work
"""
C = comp.get('C', 0)
Mn = comp.get('Mn', 0)
Ni = comp.get('Ni', 0)
Cr = comp.get('Cr', 0)
Mo = comp.get('Mo', 0)
return 637 - 58*C - 35*Mn - 15*Ni - 34*Cr - 41*Mo
def Bs_VanBohemen(**comp):
"""
Bainite start calculation from Van Bohemen's work
[1] S.M.C. van Bohemen, Mater. Sci. Technol. 28 (2012) 487–495.
"""
C = comp.get('C', 0)
Mn = comp.get('Mn', 0)
Si = comp.get('Si', 0)
Ni = comp.get('Ni', 0)
Cr = comp.get('Cr', 0)
Mo = comp.get('Mo', 0)
return 839 - (86*Mn + 23*Si + 67*Cr + 33*Ni + 75*Mo) - 270*(1 - np.exp(-1.33*C))
def Ms_Andrews(**comp):
"""
Andrews' equation for Ms
"""
C = comp.get('C', 0)
Mn = comp.get('Mn', 0)
Si = comp.get('Si', 0)
Ni = comp.get('Ni', 0)
Cr = comp.get('Cr', 0)
Mo = comp.get('Mo', 0)
Co = comp.get('Co', 0)
return 539 - 423*C - 30.4*Mn - 17.7*Ni - 12.1*Cr - 7.5*Mo + 10*Co - 7.5*Si
def alpha_martensite_VanBohemen(**comp):
"""
Martensite transformation rate constant
[1] S.M.C. van Bohemen, Mater. Sci. Technol. 28 (2012) 487–495.
"""
C = comp.get('C', 0)
Mn = comp.get('Mn', 0)
Si = comp.get('Si', 0)
Ni = comp.get('Ni', 0)
Cr = comp.get('Cr', 0)
Mo = comp.get('Mo', 0)
return 1e-3*(27.2 - (0.14*Mn + 0.21*Si + 0.11*Cr + 0.08*Ni + 0.05*Mo) - 19.8*(1-np.exp(-1.56*C)))
def Ms_VanBohemen(**comp):
"""
Martensite start temperature
[1] S.M.C. van Bohemen, Mater. Sci. Technol. 28 (2012) 487–495.
"""
C = comp.get('C', 0)
Mn = comp.get('Mn', 0)
Si = comp.get('Si', 0)
Ni = comp.get('Ni', 0)
Cr = comp.get('Cr', 0)
Mo = comp.get('Mo', 0)
return 565 - (31*Mn + 13*Si + 10*Cr + 18*Ni + 12*Mo) - 600*(1-np.exp(-0.96*C))
def Hv_martensite(phi700, **comp):
"""
Martensite Vickers hardness empirical equation
(Maynier et al.)
"""
C = comp.get('C', 0)
Mn = comp.get('Mn', 0)
Si = comp.get('Si', 0)
Ni = comp.get('Ni', 0)
Cr = comp.get('Cr', 0)
return 127 + 949*C + 27*Si + 11*Mn + 8*Ni + 16*Cr + 21*np.log10(phi700*3600)
def Hv_bainite(phi700, **comp):
"""
Bainite Vickers hardness empirical equation
(Maynier et al.)
"""
C = comp.get('C', 0)
Mn = comp.get('Mn', 0)
Si = comp.get('Si', 0)
Ni = comp.get('Ni', 0)
Cr = comp.get('Cr', 0)
Mo = comp.get('Mo', 0)
return -323 + 185*C + 330*Si + 153*Mn + 65*Ni + 144*Cr + 191*Mo + \
(89 + 53*C - 55*Si - 22*Mn - 10*Ni - 20*Cr - 33*Mo)*np.log10(phi700*3600)
def Hv_ferrite_pearlite(phi700, **comp):
"""
Ferrite + pearlite Vickers hardness empirical equation
(Maynier et al.)
"""
C = comp.get('C', 0)
Mn = comp.get('Mn', 0)
Si = comp.get('Si', 0)
Ni = comp.get('Ni', 0)
Cr = comp.get('Cr', 0)
Mo = comp.get('Mo', 0)
V = comp.get('V', 0)
return 42 + 223*C + 53*Si + 30*Mn + 12.6*Ni + 7*Cr + 19*Mo + \
(10 - 19*Si + 4*Ni + 8*Cr + 130*V)*np.log10(phi700*3600)
class Alloy:
"""
Alloy properties (composition in wt.% and prior austenite grain size)
"""
def __init__(self, gs, **w):
# Grain size
self.gs = gs
# Alloy composition
self.w = w
# Main elements
self.C = w.get('C', 0)
self.Mn = w.get('Mn', 0)
self.Si = w.get('Si', 0)
self.Ni = w.get('Ni', 0)
self.Cr = w.get('Cr', 0)
self.Mo = w.get('Mo', 0)
self.Co = w.get('Co', 0)
self.FC = FC(**w)
self.PC = PC(**w)
self.BC = BC(**w)
self.Ae3 = Ae3_Andrews(**w)
self.Ae1 = Ae1_Andrews(**w)
# self.Ae3 = Ae3_Grange(**w)
# self.Ae1 = Ae1_Grange(**w)
self.Bs = Bs_Li(**w)
# self.Ms = Ms_Andrews(**w)
# self.Bs = Bs_VanBohemen(**w)
self.Ms = Ms_VanBohemen(**w)
self.alpha_martensite = alpha_martensite_VanBohemen(**w)
# Hardness
self.Hv_martensite = lambda phi700: Hv_martensite(phi700, **w)
self.Hv_bainite = lambda phi700: Hv_bainite(phi700, **w)
self.Hv_ferrite_pearlite = lambda phi700: Hv_ferrite_pearlite(phi700, **w)
def format_composition(self, vmin=0):
fmt = []
for k, v in self.w.items():
if v > vmin:
fmt.append('{:g}{:}'.format(v, k))
fmt.insert(0, 'Fe') # assumes it is steel
fmt = '-'.join(fmt) + ' (wt.%)'
fmt += '\nASTM grain size {:}'.format(self.gs)
return fmt
class SigmoidalFunction(object):
"""
Abstract class for S(X) and I(X) functions. Once initialized,
calculates values of the function for a given [xmin, xmax]
interval and then creates a spline interpolator. The returned
values are calculated by the interpolator. This method has the
advantage of being able to process x as an array (or any other
kind of iterator)
"""
tck = None # tck spline knots, coefficients and degree
tck_inv = None # spline parameters of the inverse function
def __new__(cls, x):
"""
__new__ behaviour is modified to return the interpolated
value of the function
"""
if cls is SigmoidalFunction:
raise TypeError("Can't instantiate abstract class SigmoidalFunction")
# Check for compulsory subclass attributes
for var in ['xmin', 'xmax', 'ymin', 'ymax', 'n']:
if not hasattr(cls, var):
raise NotImplementedError(
'Class {} lacks required `{}` class attribute'.format(cls, var))
# This is were S(X) or I(X) is returned
return cls.val(x)
@staticmethod
def f(x):
"""
Function to be integrated
"""
pass
@classmethod
def val(cls, x):
"""
Evaluates SigmoidalFunction(x)
"""
if hasattr(x, '__iter__') and not isinstance(x, str):
x = np.array(x)
xmin = x[x > 0].min()
else:
xmin = x
xmin = min(cls.xmin, xmin)
# init spline if not initialized yet or if xmin is lower than lower bound
if xmin < cls.xmin or cls.tck is None:
cls.xmin = xmin
cls.init_spline()
return splev(x, cls.tck)
@classmethod
def inv(cls, y):
"""
Evaluates inverse function SigmoidalFunction^-1(y)
"""
if hasattr(y, '__iter__') and not isinstance(y, str):
y = np.array(y)
ymin, ymax = y.min(), y.max()
else:
ymin, ymax = y, y
if cls.tck_inv is None:
cls.init_spline()
if ymin < cls.ymin or ymax > cls.ymax:
print('Be careful! y value out of bounds [{:g}:{:g}]. '
'Returned value is an extrapolation'.format(cls.ymin, cls.ymax))
return splev(y, cls.tck_inv)
@classmethod
def init_spline(cls):
"""
Initializes spline
"""
X = np.linspace(cls.xmin, cls.xmax, cls.n)
Y = np.array([integrate.quad(cls.f, 0, x)[0] for x in X])
cls.ymin = Y.min()
cls.ymax = Y.max()
cls.tck = splrep(X, Y)
cls.tck_inv = splrep(Y, X)
class S(SigmoidalFunction):
n = 999
xmin = 0.001
xmax = 0.999
ymin = 0.02638507
ymax = 2.02537893
"""
S(X) function calculated using numerical integration and
spline interpolation
"""
@staticmethod
def f(x):
return 1./(x**(0.4*(1. - x))*(1. - x)**(0.4*x))
class I(SigmoidalFunction):
"""
I(X) function calculated using numerical integration and
spline interpolation
"""
n = 999
xmin = 0.001
xmax = 0.999
ymin = 0.29961765
ymax = 4.05928646
@staticmethod
def f(x):
return 1./(x**(2.*(1. - x)/3.)*(1. - x)**(2.*x/3.))
class PhaseTransformation(object):
"""
Abstract class for calculating kinetics of diffusional phase
transformations
"""
def __init__(self, alloy):
self.alloy = alloy
self.initialize()
# Check for compulsory object attributes
for var in ['comp_factor', 'Ts', 'Tf', 'Hv']:
if not hasattr(self, var):
raise NotImplementedError(
'Object {} lacks required `{}` attribute'.format(self, var))
@classmethod
def __init_subclass__(cls):
# Check for compulsory subclass attributes
for var in ['Q', 'n1', 'n2']:
if not hasattr(cls, var):
raise NotImplementedError(
'Class {} lacks required `{}` class attribute'.format(cls, var))
@abstractmethod
def initialize(self):
pass
def get_transformation_factor(self, T):
"""
Calculates the transformation factor for a given temperature T
Parameters
----------
T : float or iterable
Temperature. It can be provided as an array
Returns
-------
F : float or iterable
Transformation factor with same shape as T
"""
return self.comp_factor/(2**(self.n1*self.alloy.gs)*(self.Ts - T)**self.n2*np.exp(-self.Q/(R*(T + K))))
def get_transformation_time(self, T, f):
"""
Calculates the time necessary for the material to transform to a
fraction f at a temperature T
Parameters
----------
T : float or iterable
Temperature. It can be provided as an array
f : float or iterable
Transformed fraction. If iterable, has to have the same shape
as T
Returns
-------
t : float or iterable
Transformation time with same shape as T
"""
return S(f)*self.get_transformation_factor(T)
def get_transformation_temperature(self, Tini, Tfin, cooling_rate, f, dT=1.0):
"""
Calculates the temperature for the material to transform to a
fraction f during the cooling from Tini to Tfin at a cooling rate
cooling_rate
Parameters
----------
Tini : float
Initial temperature
Tfin : float
Final temperature
cooling_rate : float or iterable
Cooling rate(s). It can be provided as an array
f : float or iterable
Transformed fraction. If iterable, has to have the same shape
as cooling_rate
Returns
-------
T : float or iterable
Transformation temperature with same shape as cooling_rate
"""
dt = dT/np.array(cooling_rate)
nt = len(dt) if hasattr(dt, '__len__') else 1
T = np.arange(Tini, Tfin, -dT)
nucleation_time = np.full((nt, len(T)), 0, dtype=float)
filtr = T < self.Ts
nucleation_time[:, filtr] = np.outer(dt, 1./self.get_transformation_factor(T[filtr]))
nucleation_time = nucleation_time.cumsum(axis=1)
Tt = np.full(nt, np.nan, dtype=float) # Transformation temperature for a given fraction f
# Check for indices of nucleation_time larger than threshold S(f)
# First occurrence is the transformation temperature
Sf = S(f)
for i, n_time in enumerate(nucleation_time):
idx, = np.where(n_time >= Sf)
if len(idx) > 0:
Tt[i] = T[idx[0]]
return float(Tt) if nt == 1 else Tt
def get_transformed_fraction(self, t, T, n=1000):
"""
Calculates the transformed fraction for a given thermal cycle T(t)
Parameters
----------
t : iterable
Time
T : iterable
Temperatures at the instants of time t
n : int (optional)
Number of points at which the transformed fractions are
calculated
Default: 1000
Returns
-------
t, T, f : tuple
Tuple with arrays time, temperature, phase fraction evaluated
at n points
"""
if len(t) > 3:
# Fits T(t) by spline
def t2T(t_): return splev(t_, splrep(t, T))
else:
# Uses linear interpolator
t2T = interp1d(t, T)
# To ensure convergence of the algorithm, the T(t) thermal cycle is
# adjusted by a spline and the nucleation time is calculated by
# increments dt = (max(t) - min(t))/n
dt = (max(t) - min(t))/(n - 1)
t = np.linspace(min(t), max(t), n)
T = t2T(t)
nucleation_time = np.full(t.shape, 0, dtype=float)
f = np.full(T.shape, 0, dtype=float)
# Calculates nucleation time only for T lower than transformation
# start temperature and higher than Tf
filtr = (T < self.Ts) & (T > self.Tf)
if np.any(filtr):
nucleation_time[filtr] = dt/self.get_transformation_factor(T[filtr])
nucleation_time = nucleation_time.cumsum()
if T[0] < self.Ts:
# This is the factor corresponding to the transformed fraction at t[0]
nucleation_time += min(t)/self.get_transformation_factor(T[0])
# New filter: calculates f only for nucleation_time inside the bounds
# of S.inv(y)
filtr = (nucleation_time >= S.ymin) & (nucleation_time <= S.ymax)
if np.any(filtr):
f[filtr] = S.inv(nucleation_time[filtr])
f[nucleation_time < S.ymin] = 0
f[nucleation_time > S.ymax] = 1
return t, T, f
class Ferrite(PhaseTransformation):
"""
Austenite to ferrite phase transformation
"""
Q = 27500*4.184 # activation energy
n1 = 0.41 # exponential factor 1
n2 = 3 # exponential factor 2
def initialize(self):
self.comp_factor = self.alloy.FC # composition factor for calculating transformation time
self.Ts = self.alloy.Ae3 # transformation start temperature
self.Tf = self.alloy.Bs # transformation finish temperature
self.Hv = self.alloy.Hv_ferrite_pearlite
class Pearlite(PhaseTransformation):
"""
Austenite to pearlite phase transformation
"""
Q = 27500*4.184 # activation energy
n1 = 0.32 # exponential factor 1
n2 = 3 # exponential factor 2
def initialize(self):
self.comp_factor = self.alloy.PC # composition factor for calculating transformation time
self.Ts = self.alloy.Ae1 # transformation start temperature
self.Tf = self.alloy.Bs # transformation finish temperature
self.Hv = self.alloy.Hv_ferrite_pearlite
class Bainite(PhaseTransformation):
"""
Austenite to bainite phase transformation
"""
Q = 27500*4.184 # activation energy
n1 = 0.29 # exponential factor 1
n2 = 2 # exponential factor 2
def initialize(self):
self.comp_factor = self.alloy.BC # composition factor for calculating transformation time
self.Ts = self.alloy.Bs # transformation start temperature
self.Tf = self.alloy.Ms # transformation finish temperature
self.Hv = self.alloy.Hv_bainite
class Martensite:
"""
Athermal austenite to martensite transformation
"""
def __init__(self, alloy):
self.alloy = alloy
self.Ts = self.alloy.Ms
self.Hv = self.alloy.Hv_martensite
def get_transformed_fraction(self, t, T, n=1000):
"""
Calculates the transformed martensite fraction for a given thermal
cycle T(t) using the Koistinen-Marburger equation
Parameters
----------
t : iterable
Time
T : iterable
Temperatures at the instants of time t
n : int (optional)
Number of points at which the transformed fractions are
calculated
Default: 1000
Returns
-------
t, T, f : tuple
Tuple with arrays time, temperature, phase fraction evaluated
at n points
"""
if len(t) > 3:
# Fits T(t) by spline
def t2T(t_): return splev(t_, splrep(t, T))
else:
# Uses linear interpolator
t2T = interp1d(t, T)
t = np.linspace(min(t), max(t), n)
T = t2T(t)
f = np.full(T.shape, 0, dtype=float)
filtr = T < self.alloy.Ms
if np.any(filtr):
f[filtr] = 1 - np.exp(-self.alloy.alpha_martensite*(self.alloy.Ms - T[filtr]))
return t, T, f
class TransformationDiagrams:
"""
Transformation diagrams class
"""
colors_dict = dict(ferrite='#1f77b4', pearlite='#ff7f0e', bainite='#2ca02c',
martensite='#d62728', austenite='#9467bd')
columns_label_dict = dict(t='Time (s)', T=u'Temperature (°C)',
ferrite='Ferrite', pearlite='Pearlite', bainite='Bainite',
martensite='Martensite', austenite='Austenite')
def __init__(self, alloy):
self.alloy = alloy
self.ferrite = Ferrite(self.alloy)
self.pearlite = Pearlite(self.alloy)
self.bainite = Bainite(self.alloy)
self.martensite = Martensite(self.alloy)
self.df_TTT = None
self.df_CCT = None
def get_transformed_fraction(self, t, T, n=1000):
"""
Calculates transformation curves for a given T(t) thermal cycle
Parameters
----------
t : iterable
Time
T : iterable
Temperatures at the instants of time t
n : int (optional)
Number of points at which the transformed fractions are
calculated
Default: 1000
Returns
-------
f : pandas DataFrame
DataFrame containing the time, temperature, and phase fractions
of ferrite, pearlite, bainite, martensite, and austenite at n
points, and also the Vickers hardness for each data point
"""
# Uncorrected phase fractions
_, _, f_ferr = self.ferrite.get_transformed_fraction(t, T, n)
_, _, f_pear = self.pearlite.get_transformed_fraction(t, T, n)
_, _, f_bain = self.bainite.get_transformed_fraction(t, T, n)
t, T, f_mart = self.martensite.get_transformed_fraction(t, T, n)
f_ferr_inc = np.zeros(f_ferr.shape)
f_pear_inc = np.zeros(f_pear.shape)
f_bain_inc = np.zeros(f_bain.shape)
f_mart_inc = np.zeros(f_mart.shape)
f_ferr_inc[1:] = np.diff(f_ferr)
f_pear_inc[1:] = np.diff(f_pear)
f_bain_inc[1:] = np.diff(f_bain)
f_mart_inc[1:] = np.diff(f_mart)
f = pd.DataFrame(columns=['t', 'T', 'ferrite', 'pearlite',
'bainite', 'martensite', 'austenite'])
f['t'] = t
f['T'] = T
f.fillna(0, inplace=True)
f.loc[0, 'ferrite'] = f_ferr[0]
f.loc[0, 'pearlite'] = f_pear[0]
f.loc[0, 'bainite'] = f_bain[0]
f.loc[0, 'martensite'] = f_mart[0]
f.loc[0, 'austenite'] = 1. - f_ferr[0] - f_pear[0] - f_bain[0] - f_mart[0]
def f1(i, x, y, z, w):
if f_ferr[i] < 1:
return f.loc[i-1, 'ferrite'] + f_ferr_inc[i]*(1 - x - y - z - w)/(1 - f_ferr[i]) - x
else:
return f.loc[i-1, 'ferrite'] + f_ferr_inc[i]*(1 - y - z - w) - x
def f2(i, x, y, z, w):
if f_pear[i] < 1:
return f.loc[i-1, 'pearlite'] + f_pear_inc[i]*(1 - x - y - z - w)/(1 - f_pear[i]) - y
else:
return f.loc[i-1, 'pearlite'] + f_pear_inc[i]*(1 - x - z - w) - y
def f3(i, x, y, z, w): return f.loc[i-1, 'bainite'] + f_bain_inc[i]*(1 - x - y - w) - z
def f4(i, x, y, z, w): return f.loc[i-1, 'martensite'] + f_mart_inc[i]*(1 - x - y - z) - w
for i in range(len(f))[1:]:
x0 = [f.loc[i-1, 'ferrite'], f.loc[i-1, 'pearlite'],
f.loc[i-1, 'bainite'], f.loc[i-1, 'martensite']]
# Solves system of non-linear equations to get corrected phase fractions
res = root(lambda x: [f1(i, *x), f2(i, *x), f3(i, *x), f4(i, *x)], x0=x0)
f.loc[i, 'ferrite'] = res.x[0]
f.loc[i, 'pearlite'] = res.x[1]
f.loc[i, 'bainite'] = res.x[2]
f.loc[i, 'martensite'] = res.x[3]
f.loc[i, 'austenite'] = 1. - res.x.sum()
phi700 = None
try:
T2t = interp1d(T, t)
# Gets cooling rate at 700 oC
phi700 = 2./(T2t(699.) - T2t(701.))
if phi700 == 0:
phi700 = None
except ValueError:
# This might happen for isothermal heat treatments
pass
if phi700 is not None:
f['Hv'] = f['martensite']*self.martensite.Hv(phi700) + f['bainite']*self.bainite.Hv(phi700) + \
(f['ferrite'] + f['pearlite'])*self.ferrite.Hv(phi700)
else:
f['Hv'] = np.nan
return f.round(12)
def draw_thermal_cycle(self, ax, t, T, n=100, **kwargs):
"""
Draw thermal cycle (cooling curve) over AxesSubplot object
Parameters
----------
ax : AxesSubplot object
Axis where to draw the thermal cycle curve
t : iterable
Time
T : iterable
Temperatures at the instants of time t
n : int (optional)
Number of points at which the T(t) points are evaluated by
interpolation
Default: 100
Returns
-------
line : Line2D object
Line2D object corresponding to drawn curve
"""
if len(t) > 3:
# Fits T(t) by spline
def t2T(t_): return splev(t_, splrep(t, T))
else:
# Uses linear interpolator
t2T = interp1d(t, T)
t = np.linspace(min(t), max(t), n)
T = t2T(t)
kw = dict(color='k', ls='--')
kw.update(kwargs)
return ax.plot(t, T, **kw)
def TTT(self, fs=1e-2, ff=.99, ax=None, **kwargs):
"""
Plot TTT diagram
Parameters
----------
fs : float (optional)
Transformation start phase fraction
Default: 1e-2 (1%)
fs : float (optional)
Transformation finish phase fraction
Default: .99 (99%)
ax : AxesSubplot object (optional)
Axis where to plot the TTT curve. If None, then a new axis is
created
Default: None
**kwargs :
Optional arguments passed to ax.plot(*args, **kwargs)
Returns
-------
ax : AxesSubplot object
"""
if ax is None:
fig, ax = plt.subplots(figsize=(6, 6))
else:
fig = ax.get_figure()
# Ferrite
T = np.arange(self.alloy.Bs, self.alloy.Ae3)
ts = self.ferrite.get_transformation_time(T, fs) # start
tf = self.ferrite.get_transformation_time(T, ff) # finish
ax.plot(ts, T, color=self.colors_dict['ferrite'],
label='Ferrite {:g}%'.format(100*fs), **kwargs)
ax.plot(tf, T, color=self.colors_dict['ferrite'], ls='--',
label='Ferrite {:g}%'.format(100*ff), **kwargs)
df_ferrite = pd.DataFrame(dict(T_ferrite=T, ts_ferrite=ts, tf_ferrite=tf))
# Pearlite
T = np.arange(self.alloy.Bs, self.alloy.Ae1)
ts = self.pearlite.get_transformation_time(T, fs)
tf = self.pearlite.get_transformation_time(T, ff)
ax.plot(ts, T, color=self.colors_dict['pearlite'],
label='Pearlite {:g}%'.format(100*fs), **kwargs)
ax.plot(tf, T, color=self.colors_dict['pearlite'], ls='--',
label='Pearlite {:g}%'.format(100*ff), **kwargs)
df_pearlite = pd.DataFrame(dict(T_pearlite=T, ts_pearlite=ts, tf_pearlite=tf))
# Bainite
T = np.arange(self.alloy.Ms, self.alloy.Bs)
ts = self.bainite.get_transformation_time(T, fs)
tf = self.bainite.get_transformation_time(T, ff)
ax.plot(ts, T, color=self.colors_dict['bainite'],
label='Bainite {:g}%'.format(100*fs), **kwargs)
ax.plot(tf, T, color=self.colors_dict['bainite'], ls='--',
label='Bainite {:g}%'.format(100*ff), **kwargs)
df_bainite = pd.DataFrame(dict(T_bainite=T, ts_bainite=ts, tf_bainite=tf))
self.df_TTT = pd.concat([df_ferrite, df_pearlite, df_bainite], axis=1)
# Draws Ae1 and Ae3 lines
ax.axhline(self.alloy.Ae3, xmax=.1, color=self.colors_dict['ferrite'], ls=':')
ax.axhline(self.alloy.Ae1, xmax=.1, color=self.colors_dict['pearlite'], ls=':')
# Draws Bs and Ms lines
ax.axhline(self.alloy.Bs, color=self.colors_dict['bainite'], ls=':')
ax.axhline(self.alloy.Ms, color=self.colors_dict['martensite'])
ax.set_xscale('log')
ax.set_xlabel('Time (s)')
ax.set_ylabel(u'Temperature (°C)')
ax.set_title(self.alloy.format_composition())
xmin = ax.get_xlim()[0]
ax.text(xmin*1.5, self.alloy.Ae3, 'Ae3',
color=self.colors_dict['ferrite'], ha='left', va='bottom')
ax.text(xmin*1.5, self.alloy.Ae1, 'Ae1',
color=self.colors_dict['pearlite'], ha='left', va='bottom')
ax.text(xmin*1.5, self.alloy.Bs, 'Bs',
color=self.colors_dict['bainite'], ha='left', va='bottom')
ax.text(xmin*1.5, self.alloy.Ms, 'Ms',
color=self.colors_dict['martensite'], ha='left', va='bottom')
ax.legend(loc='upper center', ncol=3, bbox_to_anchor=(0.5, -.15))
fig.subplots_adjust(bottom=.2)
return ax
def CCT(self, Tini=900, fs=1e-2, ff=.99, phi_min=1e-4, phi_max=1e4, phi_steps=420, ax=None, **kwargs):
"""
Plot CCT diagram
Parameters
----------
fs : float (optional)
Transformation start phase fraction
Default: 1e-2 (1%)
fs : float (optional)
Transformation finish phase fraction
Default: .99 (99%)
ax : AxesSubplot object (optional)
Axis where to plot the TTT curve. If None, then a new axis is
created
Default: None
**kwargs :
Optional arguments passed to ax.plot(*args, **kwargs)
Returns
-------
ax : AxesSubplot object
"""
if ax is None:
fig, ax = plt.subplots(figsize=(6, 6))
else:
fig = ax.get_figure()
cooling_rates = 10**np.linspace(np.log10(phi_min), np.log10(phi_max), phi_steps)
draw_cooling = kwargs.get('draw_cooling', True)
# Ferrite
Ts = self.ferrite.get_transformation_temperature(
Tini, self.alloy.Bs, cooling_rates, fs) # start
Tf = self.ferrite.get_transformation_temperature(
Tini, self.alloy.Bs, cooling_rates, ff) # finish
ax.plot(Ts/cooling_rates, Ts,
color=self.colors_dict['ferrite'], label='Ferrite {:g}%'.format(100*fs), **kwargs)
ax.plot(Tf/cooling_rates, Tf, color=self.colors_dict['ferrite'],
ls='--', label='Ferrite {:g}%'.format(100*ff), **kwargs)
# Pearlite
Ts = self.pearlite.get_transformation_temperature(Tini, self.alloy.Bs, cooling_rates, fs)
Tf = self.pearlite.get_transformation_temperature(Tini, self.alloy.Bs, cooling_rates, ff)
ax.plot(Ts/cooling_rates, Ts, color=self.colors_dict['pearlite'],
label='Pearlite {:g}%'.format(100*fs), **kwargs)
ax.plot(Tf/cooling_rates, Tf, color=self.colors_dict['pearlite'],
ls='--', label='Pearlite {:g}%'.format(100*ff), **kwargs)
# Bainite
Ts = self.bainite.get_transformation_temperature(Tini, self.alloy.Ms, cooling_rates, fs)
Tf = self.bainite.get_transformation_temperature(Tini, self.alloy.Ms, cooling_rates, ff)
ax.plot(Ts/cooling_rates, Ts,
color=self.colors_dict['bainite'], label='Bainite {:g}%'.format(100*fs), **kwargs)
ax.plot(Tf/cooling_rates, Tf, color=self.colors_dict['bainite'],
ls='--', label='Bainite {:g}%'.format(100*ff), **kwargs)
ax.axhline(self.alloy.Ae3, xmax=.1, color=self.colors_dict['ferrite'], ls=':')
ax.axhline(self.alloy.Ae1, xmax=.1, color=self.colors_dict['pearlite'], ls=':')
ax.axhline(self.alloy.Bs, color=self.colors_dict['bainite'], ls=':')
ax.axhline(self.alloy.Ms, color=self.colors_dict['martensite'])
# Draw cooling curves
if draw_cooling:
for cooling_rate in cooling_rates[::10]:
T = np.linspace(Tini, 25, 100)
t = (Tini - T)/cooling_rate
kw = dict(lw=.5)
kw.update(kwargs)
ax.plot(t, T, 'k:', **kw)
ax.set_xscale('log')
ax.set_xlabel('Time (s)')
ax.set_ylabel(u'Temperature (°C)')
ax.set_title(self.alloy.format_composition())
xmin = ax.get_xlim()[0]
ax.text(xmin*1.5, self.alloy.Ae3, 'Ae3',
color=self.colors_dict['ferrite'], ha='left', va='bottom')
ax.text(xmin*1.5, self.alloy.Ae1, 'Ae1',
color=self.colors_dict['pearlite'], ha='left', va='bottom')
ax.text(xmin*1.5, self.alloy.Bs, 'Bs',
color=self.colors_dict['bainite'], ha='left', va='bottom')
ax.text(xmin*1.5, self.alloy.Ms, 'Ms',
color=self.colors_dict['martensite'], ha='left', va='bottom')
ax.legend(loc='upper center', ncol=3, bbox_to_anchor=(0.5, -.15))
fig.subplots_adjust(bottom=.2)
return ax
def plot_phase_fraction(self, t, T, n=1000, xaxis='t', ax=None, **kwargs):
"""
Plot phase fractions for a given thermal cycle T(t)