-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgg.apls
100 lines (93 loc) · 9.66 KB
/
gg.apls
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
#!/usr/local/bin/dyalogscript MAXWS=16G
⍝ grain growth cellular automata model [email protected] 2023-2024
⎕IO←0 ⋄ ⎕RL←1 ⋄ config←'gg.json5'
:Namespace EBSD ⍝ Read and write ang files
Read←{ ⍝ read ang file
(h l)←((2-'#'=⊃¨)⊆⊢)⊃⎕NGET⍵1 ⍝ get (hash) comments and data
h,⍥⊂⎕CSV⍠2⊢('(^\s+)|(\s+$)' '\s+'⎕R'' ','⊢l)'N'2 0 ⍝ format data and read as csv
}
Crop←{⍝ crop defined region
(a b x y)←⍺ ⋄ (x y)←1e99@(=∘0)x y ⍝ default limits are 0 0 1e99 1e99
((((a∘≤∧x∘>)4∘⊃)∧((b∘≤∧y∘>)3∘⊃))⍵)∘/¨⍵ ⍝ select points between limits
}
Write←{⍵⊣⍵1⎕NPUT⍨{⍺,⍵⎕CSV⍠2⍠'Separator' ' '⊢'' 'N'}/⍺} ⍝ write comments and data to ang file
Orientations←{ ⍝ get orientations
⍺←0 ⋄ nx←⌊0.5+1+((⌈/-⌊/)x)÷⌈/|2-/x←4⊃⍵ ⋄ ny←⌊0.5+(≢x)÷nx ⍝ find size
g←∪ea←(a←○⍺÷180)(⌊0.5+÷⍨)⍣(⍺>0)↓⍉↑(○2)|3↑⍵ ⍝ unique euler angles
(a×⍣(⍺>0)⊢g)(nx ny⍴g⍳ea) ⍝ return angles and indices
}
IQ←5∘⊃ ⋄ CI←6∘⊃ ⍝ image quality and confidence index
:EndNamespace
:Namespace Euler ⍝ Orientations and misorientations
c←↑(0 1 2 3)(1 0 3 2)(2 3 0 1)(3 2 1 0) ⍝ product components ⎕IO=0
u←↑(1 ¯1 ¯1 ¯1)(1 1 1 ¯1)(1 ¯1 1 1)(1 1 ¯1 1) ⍝ product unit factors
QP←+/u×⊣(×⍤1)(⊂c)⌷⊢ ⋄ QC←×∘(1,-3⍴1) ⋄ QD←+.× ⍝ product, conjugate and dot product
RD←180÷⍨○ ⋄ UV←×∘(÷(÷2)*⍨+.×⍨)⍨ ⋄ (x y z)←=∘⊂⍨⍳3 ⍝ degrees, unitary vector and unit vectors
QA←((⊂2○⊢),(⊂1○⊢)∘.×∘UV{⍺←z ⋄ ⍺})∘(÷∘2) ⋄ QAD←QA∘RD ⍝ quaternion from axis-angle
QE←⊃{⍺←z x z ⋄ ⍺}QP.QA⊢ ⋄ QED←QE∘RD ⍝ quaternion from Euler angles (zxz)
cs ← ⊂1 0 0 0 ⍝ cubic symmetry: identity
cs,←,(1 0 0)(0 1 0)(0 0 1)∘.QAD 90 180 270 ⍝ 4-fold around <001>
cs,←,(1 1 1)(¯1 1 1)(1 ¯1 1)(1 1 ¯1)∘.QAD 120 240 ⍝ 3-fold around <111>
cs,←,(1 1 0)(1 0 1)(0 1 1)(1 ¯1 0)(¯1 0 1)(0 1 ¯1)∘.QAD 180 ⍝ 2-fold around <110>
MC←⊃2ׯ2○1⌊cs⌈.(|QD)∘⊂QC⍤QP∘QC ⋄ ML←⌊○⍤⊣÷⍨180×⊢ ⍝ misorientation (cubic symmetry) and level
M←{⍺←0.5 ⍝ namespace to calculate misorientations
IM←⌈(⊢+2÷⍨⊣×1-⍨⊣)⌊ ⋄ CM←{(c←⎕NS⍬).m←¯1⍴⍨1+⍵IM.-1 2 ⋄ c} ⍝ index and cache of misorientations
_M_ ← {(c s)←⍺⍺ ⍵⍵ ⍝ memoization
(a w)←(⊂,d←⍺≠⍵)/∘,¨⍺ ⍵ ⋄ 0=≢a:d ⍝ return zeros if no different pairs
n←(≠,i)∧s.m[i←a IM w]<0 ⋄ 0=≢p←n/⍥,i:s.m[i]@⊢d ⍝ return calculated if all done
s.m[p]←ML⊃a MC⍥{↓⍉↑c[n/⍵]}w ⋄ s.m[i]@⊢d ⍝ calculate and return
}
m←⎕NS'MC' 'IM' ⋄ m.ML←⍺∘ML ⋄ m.L←⌊÷∘⍺ ⍝ namespace with curried ML
m.D←(⍺÷2)+⍺∘× ⋄ w←↓⍉↑⍵ ⋄ m.M←w _M_(CM≢w) ⋄ m ⍝ degrees and misorientation functions
}
:EndNamespace
_GG_←{ ⍝ Grain Growth
(d r c(w1 w2))←⍺⍺.(dx Rp c w) ⋄ gf←0 1,⍺⍺.Gf ⋄ Out←⍺⍺.Out
q←⍵ ⋄ x←⍺⍺.tmax ⋄ f←4⍴⊂≠⍨v←q≥0 ⍝ orientations, total time and fractions
g←(⍺⍺.G0×⊢×1-⍟)@(0∘<)¨1⌊h÷⍨⍳1+h←⍵⍵.L⍺⍺.hagb ⍝ grain boundary energy
m←(⍺⍺.M0×*-⍺⍺.Qg÷8.314×⍺⍺.T)×1-*-⍺⍺.A×⍺⍺.n*⍨h÷⍨⍳1+h ⍝ grain boundary mobility
K←(,(÷2)*⍨∘.+⍨2*⍨⍳3)⌷⍨∘⊂1+⊢+3×⊣ ⋄ _C_←{1-c×0 1⍺⍺¨⊂¯1(⊢∧⍵⍵)L⍵} ⍝ directional constant and neighbour drag
_P←{_←⍺⍺ ⍺⎕NPUT⍨⊂↓(t←'-'@('¯'∘=)14 ¯6⍕⍵) ⋄ t} ⍝ write to file and return text
_←1(w2 _P)⍉↑(⍵⍵.D⍳1+h)g m ⋄ (g m)×←d*2 ¯4 ⋄ gh←4×⊢/g ⍝ write boundary energies and mobilities
D←h⌊⍵⍵.M ⋄ N←¯1 1(⊖¨,⌽¨)⊂ ⋄ L←(⍵⍵.L ⍺⍺.lagb)∘> ⍝ disorientation, neighbours, low angle
R←{ ⍝ rate of transformation
rs←1⊖rn←m[on←⍵D⊢n←¯1⊖⍵] ⋄ re←1⌽rw←m[ow←⍵D⊢w←¯1⌽⍵] ⍝ set rates to mobilities
(nw we)←n(1⌽⍵)D¨⊂w ⋄ (ns sw)←n w D¨⊂1⊖⍵ ⍝ diagonal and opposite disorientations
(lnw lsw lwe)←L nw sw we ⋄ rw×←(lnw+lsw)K lwe ⍝ multiply by directional constant (w)
lne←1⌽¯1⊖lsw ⋄ lns←L ns ⋄ rn×←(lne+lnw)K lns ⍝ multiply by directional constant (n)
lse←1⌽1⊖lnw ⋄ rs×←(lse+lsw)K lns ⋄ re×←(lse+lne)K lwe ⍝ multiply by directional constant (s,e)
gn←gf⌷⍨⊂4-4⌊lns+lnw+lne+lsw+lse+lwe ⍝ multiple points factor
ge←gw←gs←gn←gn×gh@(~⍺⍨)g[on](⊣+(1⊖⊣)+⊢+1⌽⊢)g[ow] ⍝ current boundary energy of each cell
gse←1⌽1⊖gnw←g[nw] ⋄ gne←1⌽¯1⊖gsw←g[sw] ⍝ diagonal boundary energies
gn-←⊃gnw gne+.×c←⌽_C_⊖ow ⋄ gs-←⊃gsw gse+.×1⊖¨c ⍝ energy after transformation (n,s)
gw-←⊃gnw gsw+.×c←⊖_C_⌽on ⋄ ge-←⊃gne gse+.×1⌽¨c ⍝ energy after transformation (w,e)
0⌈(N⍺)×rn rs rw re×(gn gs-⊂g[ns]),gw ge-⊂g[we] ⍝ rate of non-valid cells is zero
}
X←(⊢×1+r×?⍤≠⍨)⍣(r≠0){⊃((N,⊂)⍵)+.×(⍳5)=⊂⊃∘⍒⍤1⍺⍺↑[0]⍺,⊂=⍨⍵} ⍝ select neighbour to transform into
O←d×{2×(⍵÷○1)*÷2}∘{(⍳2)×.((≢,⍵){⍺⍺÷1⌈+/,⍵≠1⌽[⍺]⍵})⊂⍵} ⍝ grain diameter
P←{⍺←2 ⋄ (q t)←⍵ ⋄ ⍺(w1 _P)1 2⍴t(O q)} ⍝ write time and diameter to file
⎕←1 P q 0 ⋄ -@{~v}⊃⍺⍺.tmin{
(q t)←⍵ ⋄ x≤t+∆t←⍺⍺⌈÷⎕CT⌈⌈/∊(1-f)÷⍨r←v R q:{⎕←P⍵ ⋄ ⍵}q x ⍝ find time increment and exit if done
f{(n s w e)←⍵ ⋄ ⍵×(⍵<1)∨⍵≥(¯1⊖s)(1⊖n)(¯1⌽e)(1⌽w)}⍤+←∆t×r ⍝ set current transformation fractions
⎕←P(q←f X q)(t+∆t) ⋄ f×←~(⊂v∨←b)∨1 ¯1(⊖¨,⌽¨)⊂b←⊃∨/f≥1 ⍝ transform and reset fractions
_←(t+∆t)Out b q ⋄ ∇q(t+∆t) ⍝ run Out function and repeat
}|q 0
}
Sim←{
_←{⎕←P'' ⋄ ⍵.Out←{⍺≥t+tout: t⊢←⍺⊣⍺##.W⊃⌽⍵ ⋄ ⍬} ⋄ (m q)←⍺⍺ ⍵ ⋄ _←⍵.tmax W⍵_GG_ m⊢0 W q ⋄ ⎕←P''}
C←{Euler.(⍵.dm M↓⍉↑(1 1 1 QAD 63.8)(4⍴0))((×⍨⍵.input)>+.×⍨¨(⍳2⍴s)+0.5(⊣-⊣×⊢)s←2+⌈2×⍵.input)}_
R←{(x y n)←(⊢,×/)⍣(2=≢⍵.input)⊢⍵.input ⋄ (⍵.dm Euler.{⍺M UV⊃QP/(↓∘.=⍨⍳3)QA¨↓○2×?3⍵⍴0}n)(?x y⍴n)}_
P←{⍵,⍨' ',⍨⍣(⍵≢'')⊢'-- ::'@(1+3×1+⍳5)∊⍕¨(⊃,100+1∘↓)6↑1⎕DT⎕TS} ⍝ date, time and argument
j←(⎕JSON⍠'Dialect' 'JSON5')⊃⎕NGET⍵ ⋄ ⎕←P'< ',⍵ ⍝ read configuration from json5 file
(d f c o)←j.(da input(¯4↑crop)out) ⋄ j.t←0 ⋄ #.⎕RL←j.seed ⍝ set parameters, time and random seed
j.w←(⊂o),¨'time_diameter.txt' 'dis_gamma_M.txt' ⍝ output files
W←o{⍵⊣(⊂↓⍕⍵)⎕NPUT(⍺⍺,'.txt',⍨1↓0 4⍕⍺)1} ⋄ D←{1=≢f: C⍵ ⋄ R⍵} ⍝ output function for predefined inputs
2|⎕DR f: W{⍵.Out←⍺⍺{⍺≥t+tout: t⊢←⍺⊣⍺ ⍺⍺⊃⌽⍵ ⋄ ⍬} ⋄ D⍵}j ⍝ predefined input
⎕←P'< ',f,' ',⍕≢⊃⊃⌽(h e)←EBSD.Read f ⋄ e←c EBSD.Crop⍣(0∨.≠c)⊢e ⍝ read and crop ang file
(g q)←d EBSD.Orientations e ⋄ m←j.dm Euler.(M∘QE)↓⍉↑g ⍝ orientations and misorientations
q←-@{(⍴q)⍴⊃j.(ci iq)∨.>EBSD.(CI,⍥⊂IQ)e}q ⍝ valid cells
W←{e[7+⍳2]←(-∘1,⊢)⊂,q>0 ⋄ e[⍳3]←↓⍉↑g[,|q] ⋄ ⍵⊣h e EBSD.Write⍵} ⍝ write ebsd file with new orientations
j.Out←{(b ##.q)←⍵ ⋄ ##.e[9]⌈←⊂10×,b ⋄ ⍺≥t+tout: t⊢←⍺⊣##.W out,'.ang',⍨1↓0 4⍕⍺ ⋄ ⍬} ⍝ write output
⎕←P'> ',(W o,'0.ang'),' ',⍕≢⊃e ⋄ q←j _GG_ m⊢q ⋄ ⎕←P'> ',W o,'1.ang' ⍝ RUN! and write results
}
Sim¨a⊣⍣(0<≢a←1↓2⎕NQ#'GetCommandLineArgs')⊆config