forked from Karql/grain-growth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SRXAlgorithm.cs
107 lines (89 loc) · 3.69 KB
/
SRXAlgorithm.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace grain_growth
{
public class SRXAlgorithm : AlgorithmBase
{
public void AddEnergy(double energy)
{
this.grid.ResetCurrentCellPosition();
do
{
if (this.IsNotRecrystalizedCellOnBoundary(this.grid.CurrentCell))
{
this.grid.CurrentCell.Energy = energy;
}
} while (this.grid.Next());
}
public void AddNucleations(int nucleationsNumber)
{
int[] notUsedIds = this.GetNotUsedIds().Take(nucleationsNumber).ToArray();
Cell[] cellsForNucleations = this.GetNotRecystalizedCellsOnBoundary().Take(nucleationsNumber).ToArray();
for (int i = 0; i < nucleationsNumber && i < notUsedIds.Length && i < cellsForNucleations.Length; ++i)
{
cellsForNucleations[i].ID = notUsedIds[i];
cellsForNucleations[i].NewID = notUsedIds[i];
cellsForNucleations[i].Recrystalized = true;
cellsForNucleations[i].Energy = 0;
}
}
public void Step()
{
foreach (Cell c in this.GetCellsToProcess())
{
Cell[] recrystalizedNeighboors = c.MoorNeighborhood.Where(i => i.ID > 1 && i.Recrystalized).ToArray();
//double e1 = c.MoorNeighborhood.Where(i => i.ID != c.ID).Sum(i => i.Energy) + c.Energy;
//int newId = recrystalizedNeighboors[RandomHelper.Next(recrystalizedNeighboors.Length)].ID;
//double e2 = c.MoorNeighborhood.Where(i => i.ID != newId).Sum(i => i.Energy);
double e1 = c.MoorNeighborhood.Count(i => i.ID != c.ID) + c.Energy;
int newId = recrystalizedNeighboors[RandomHelper.Next(recrystalizedNeighboors.Length)].ID;
double e2 = c.MoorNeighborhood.Count(i => i.ID != newId);
if (e2 - e1 <= 0)
{
c.ID = newId;
c.NewID = newId;
c.Recrystalized = true;
c.Energy = 0;
}
}
}
private IEnumerable<Cell> GetNotRecystalizedCellsOnBoundary()
{
List<Cell> cells = new List<Cell>();
this.grid.ResetCurrentCellPosition();
do
{
if (this.IsNotRecrystalizedCellOnBoundary(this.grid.CurrentCell))
{
cells.Add(this.grid.CurrentCell);
}
} while (this.grid.Next());
// Random order
return cells.OrderBy(i => RandomHelper.Next());
}
private bool IsNotRecrystalizedCellOnBoundary(Cell cell)
{
return cell.ID > 1 && !cell.Recrystalized && cell.MoorNeighborhood.Count(i => i.ID != cell.ID && !i.Recrystalized) > 0;
}
private IEnumerable<Cell> GetCellsToProcess()
{
List<Cell> cells = new List<Cell>();
this.grid.ResetCurrentCellPosition();
do
{
if (this.IsNotRecrystalizedCellWithRecrystalizedNeighboor(this.grid.CurrentCell))
{
cells.Add(this.grid.CurrentCell);
}
} while (this.grid.Next());
// Random order
return cells.OrderBy(i => RandomHelper.Next());
}
private bool IsNotRecrystalizedCellWithRecrystalizedNeighboor(Cell cell)
{
return cell.ID > 1 && !cell.Recrystalized && cell.MoorNeighborhood.Count(i => i.Recrystalized) > 0;
}
}
}