Exemplo n.º 1
0
        public Dictionary <int, int> getColorsDictionary(Grain[,] grains, int x, int y, int left, int top, int botom, int right)
        {
            Grain[] neighborhood = new Grain[8];
            neighborhood[0] = grains[left, top];
            neighborhood[1] = grains[x, top];
            neighborhood[2] = grains[right, top];
            neighborhood[3] = grains[left, y];
            neighborhood[4] = grains[right, y];
            neighborhood[5] = grains[left, botom];
            neighborhood[6] = grains[x, botom];
            neighborhood[7] = grains[right, botom];
            //wylicza sasiedztwo moora i zwraca slownik kolorow

            Dictionary <int, int> colors = new Dictionary <int, int>();

            for (int i = 0; i < neighborhood.Length; i++)
            {
                if (neighborhood[i] != null && (neighborhood[i].stan == Grain.TYPE_GRAIN || neighborhood[i].stan == Grain.TYPE_OLD_GRAIN))
                {
                    int color = neighborhood[i].index;
                    if (colors.ContainsKey(color))
                    {
                        colors[color]++;
                    }
                    else
                    {
                        colors[color] = 1;
                    }
                }
            }
            return(colors);
        }
Exemplo n.º 2
0
        public bool isBoundary(Grain[,] grains, int width, int height, int xpom, int ypom)
        {
            int gora  = ypom - 1;
            int dol   = ypom + 1;
            int prawy = xpom + 1;
            int lewy  = xpom - 1;

            //perdiodyczny warunek brzegowy
            if (xpom == 0)
            {
                lewy = width - 1;
            }
            if (xpom == width - 1)
            {
                prawy = 0;
            }
            if (ypom == 0)
            {
                gora = height - 1;
            }
            if (ypom == height - 1)
            {
                dol = 0;
            }
            //liczy sasiedztwo po to zeby policzyc tablice kolorow
            //jesli sa w sasiedztwie dwa rozne kolory to jest granica
            return(this.getColorsDictionary(grains, xpom, ypom, lewy, gora, dol, prawy).Count >= 2);
        }
Exemplo n.º 3
0
        private void mc_proceedSingleCellMonteCarlo(Tuple <int, int> cell, Grain[,] space, bool recrystalization = false)
        {
            int    x              = cell.Item1;
            int    y              = cell.Item2;
            int    dim            = space.GetLength(0);
            double initial_energy = mc_getCellEnergy(x, y, space);
            var    previous_ID    = space[x, y].ID;

            mc_changeID(x, y, dim, space, recrystalization);
            double post_change_energy = mc_getCellEnergy(x, y, space);
            double energy_difference  = post_change_energy - initial_energy;

            //Console.WriteLine("Energy difference = {0}", energy_difference);
            if (energy_difference < 0)
            {
                // Keep new state, update color
                space[x, y].color = grain_ID_Color_dict[space[x, y].ID];
                var t = true;
            }
            else
            {
                // Revert change
                space[x, y].ID = previous_ID;
            }
        }
Exemplo n.º 4
0
    public static List <Grain> vonNeumannGrains(Grain[,] grains, int x, int y)
    {
        List <Grain> _Neighbours = new List <Grain>();

        int x_l = -200;
        int x_p = -200;
        int y_d = -200;
        int y_g = -200;

        if (BOUNDARY_CONDITION == BoundaryCondition.Periodic)
        {
            x_l = x - 1 < 0 ? SIZE_X - 1 : x - 1;
            x_p = x + 1 >= SIZE_X ? 0 : x + 1;
            y_d = y + 1 >= SIZE_Y ? 0 : y + 1;
            y_g = y - 1 < 0 ? SIZE_Y - 1 : y - 1;
        }
        else
        {
            x_l = x - 1 < 0 ? x : x - 1;
            x_p = x + 1 >= SIZE_X ? x : x + 1;
            y_d = y + 1 >= SIZE_Y ? y : y + 1;
            y_g = y - 1 < 0 ? y : y - 1;
        }

        _Neighbours.Add(grains[y, x_l]); // s_l
        _Neighbours.Add(grains[y, x_p]); // s_p
        _Neighbours.Add(grains[y_d, x]); // s_d
        _Neighbours.Add(grains[y_g, x]); // s_g


        return(_Neighbours);
    }
Exemplo n.º 5
0
        public static bool isPointOnGrainBorder(Tuple <int, int> point, Grain[,] grain_structure)
        {
            HashSet <int> neighbors_IDs = new HashSet <int>();

            for (int i = point.Item1 - 1; i <= point.Item1 + 1; ++i)
            {
                if (i < 0 || i > grain_structure.GetLength(0) - 1)
                {
                    continue;
                }
                for (int j = point.Item2 - 1; j <= point.Item2 + 1; ++j)
                {
                    if (j < 0 || j > grain_structure.GetLength(0) - 1)
                    {
                        continue;
                    }
                    neighbors_IDs.Add(grain_structure[i, j].ID);
                }
            }
            if (neighbors_IDs.Count > 1)
            {
                return(true);
            }

            return(false);
        }
Exemplo n.º 6
0
 private void Draw(Grain[,] grains, Grain[,] temp, int height)
 {
     Parallel.For(0, height, i =>
     {
         for (int j = 0; j < WidthSize; j++)
         {
             if (grains[i, j] != temp[i, j])
             {
                 lock (lockBrush)
                 {
                     //  using (Graphics graph = Graphics.FromImage(bmp))
                     {
                         try
                         {
                             SolidBrush brush = brushes[grains[i, j].ID];
                             bmp.SetPixel(j, i, brush.Color);
                             //graph.FillRectangle(brush, j, i, 1, 1);
                         }
                         catch (Exception error)
                         {
                             //   graph.Dispose();
                         }
                     }
                 }
             }
         }
     });
     paintArea.Invalidate();
 }
Exemplo n.º 7
0
        private double mc_getCellEnergy(int x, int y, Grain[,] space, double Jgb = 1.0)
        {
            int    dim = space.GetLength(0);
            int    number_of_neighbors_with_different_ID = mc_getNumberOfNeighborsWithDifferentID(x, y, dim, space);
            double energy = Jgb * number_of_neighbors_with_different_ID;

            return(energy);
        }
Exemplo n.º 8
0
    public Grain[,] Simulation(Grain[,] grains, Graphics g)
    {
        bool[,] recrystalized = new bool[SIZE_Y, SIZE_X];

        for (int y = 0; y < SIZE_Y; y++)
        {
            for (int x = 0; x < SIZE_X; x++)
            {
                recrystalized[y, x] = false;
            }
        }

        for (int y = 0; y < SIZE_Y; y++)
        {
            for (int x = 0; x < SIZE_X; x++)
            {
                List <Grain> neighbours = NeighbourhoodFactory.GetNeighboursGrains(grains, x, y);

                foreach (Grain neighbour in neighbours)
                {
                    if (lastRecrystalized[neighbour.Y, neighbour.X])
                    {
                        bool rescrystallize = true;

                        foreach (Grain neighbour1 in neighbours)
                        {
                            if (!neighbour1.Recrystallized && neighbour1.Density >= grains[y, x].Density)
                            {
                                rescrystallize = false;
                                break;
                            }
                        }

                        if (rescrystallize && !grains[y, x].Recrystallized)
                        {
                            recrystalized[y, x]  = true;
                            grains[y, x].Density = 0;
                            // grains[y, x].Recrystallized = true;
                            grains[y, x].DisplayRecrystallized(g, Colors.GetRecrystallizationColor());
                        }

                        break;
                    }
                }
            }
        }

        for (int y = 0; y < SIZE_Y; y++)
        {
            for (int x = 0; x < SIZE_X; x++)
            {
                currentlyRecrystalized[y, x] = recrystalized[y, x] ? true: currentlyRecrystalized[y, x];
            }
        }

        return(grains);
    }
Exemplo n.º 9
0
 public Algorithms(Grain[,] grains, Grain[,] temp, int ActualHeightOFLayers, NeighbourType type, BoundaryConditions bc, int LayerNumber)
 {
     this.grains = grains;
     this.temp   = temp;
     this.ActualHeightOfLayers = ActualHeightOFLayers;
     this.type         = type;
     ActualLayerNumber = LayerNumber;
     this.bc           = bc;
 }
 private List <Grain> TakeNeumannNeighbourhood(int i, int j, Grain[,] GrainsArray)
 {
     return(new List <Grain>
     {
         GrainsArray[i, j + 1],
         GrainsArray[i + 1, j],
         GrainsArray[i - 1, j],
         GrainsArray[i, j - 1],
     });
 }
Exemplo n.º 11
0
        private void mc_changeID(int x, int y, int dim, Grain[,] space, bool recrystalization = false)
        {
            HashSet <int> possible_IDs = mc_getNeighborsIDs(x, y, dim, space, recrystalization);

            if (possible_IDs.Count > 0)
            {
                int new_ID = possible_IDs.ElementAt(rand.Next(possible_IDs.Count));
                space[x, y].ID = new_ID;
            }
        }
 private List <Grain> TakeFurtherMooreNeighbourhood(int i, int j, Grain[,] GrainsArray)
 {
     return(new List <Grain>
     {
         GrainsArray[i - 1, j - 1],
         GrainsArray[i - 1, j + 1],
         GrainsArray[i + 1, j - 1],
         GrainsArray[i + 1, j + 1]
     });
 }
Exemplo n.º 13
0
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            MouseEventArgs me          = (MouseEventArgs)e;
            Point          coordinates = me.Location;
            int            x           = coordinates.X / CELL_SIZE;
            int            y           = coordinates.Y / CELL_SIZE;

            if (x >= SIZE_X || y >= SIZE_Y)
            {
                return;
            }



            if (!isPlaying)
            {
                if (grainGrowth.Tab[y, x].State == 0)
                {
                    grainGrowth.Tab[y, x].State = Colors.RandomColor();
                    grainGrowth.Tab[y, x].Display(g);
                }
                else
                {
                    grainGrowth.Tab[y, x].State = 0;
                    grainGrowth.Tab[y, x].Display(g);
                }
            }
            else
            {
                this.tab = new Grain[SIZE_Y, SIZE_X];

                for (int i = 0; i < SIZE_Y; i++)
                {
                    for (int j = 0; j < SIZE_X; j++)
                    {
                        this.tab[i, j] = grainGrowth.Tab[i, j].Copy();
                    }
                }

                if (grainGrowth.Tab[y, x].State == 0)
                {
                    this.tab[y, x].State = Colors.RandomColor();
                }
                else
                {
                    this.tab[y, x].State = 0;
                }

                clickedButton = true;
            }

            grainGrowth.Tab[y, x].DisplayEnergy(g);

            pictureBox1.Image = simulationBitmap;
        }
Exemplo n.º 14
0
 private void initGrainStructure(out Grain[,] g_s, int dim)
 {
     g_s = new Grain[dim, dim];
     for (int x = 0; x < dim; ++x)
     {
         for (int y = 0; y < dim; ++y)
         {
             g_s[x, y] = new Grain(0, 0, grain_ID_Color_dict[0]);
         }
     }
 }
Exemplo n.º 15
0
    public Recrystallization(Grain[,] grains)
    {
        for (int i = 0; i < SIZE_Y; i++)
        {
            for (int j = 0; j < SIZE_X; j++)
            {
                grains[i, j].Density        = 0;
                grains[i, j].Recrystallized = false;
            }
        }



        Colors.InitializeRecrystallizationColrs();

        ro                  = new List <double>();
        sigma               = new List <double>();
        checkedRo           = new List <double>();
        checkedRoFromGrians = new List <double>();

        currentlyRecrystalized = new bool[SIZE_Y, SIZE_X];
        lastRecrystalized      = new bool[SIZE_Y, SIZE_X];

        time = 0;

        double _ro    = (A / B) + (1 - (A / B)) * (Math.Exp(-B * time));
        double _sigma = sigma0 + value * A0 * B0 * Math.Sqrt(_ro);

        criticallRo = criticallRo / (SIZE_X * SIZE_Y);

        Console.WriteLine("Critical " + criticallRo);

        ro.Add(_ro);
        sigma.Add(_sigma);
        checkedRo.Add(0);
        checkedRoFromGrians.Add(_ro);


        for (int i = 0; i < SIZE_Y; i++)
        {
            for (int j = 0; j < SIZE_X; j++)
            {
                currentlyRecrystalized[i, j] = false;
                lastRecrystalized[i, j]      = false;

                List <int> neighbours = NeighbourhoodFactory.GetNeighboursMonteCarlo(grains, j, i);

                if (neighbours.Where(s => s != grains[i, j].State).ToList().Count > 0)
                {
                    grains[i, j].OnBorder = true;
                }
            }
        }
    }
Exemplo n.º 16
0
    public void Resize()
    {
        this.previousGrains = new Grain[SIZE_Y, SIZE_X];

        for (int i = 0; i < SIZE_Y; i++)
        {
            for (int j = 0; j < SIZE_X; j++)
            {
                this.previousGrains[i, j] = this.tab[i, j].Copy();
            }
        }
    }
Exemplo n.º 17
0
        private int mooreMethod(Grain[,] grainCointainer, int i, int j)
        {
            int up, down, left, right;

            up    = i - 1;
            down  = i + 1;
            left  = j - 1;
            right = j + 1;

            for (int k = 1; k < NucleonAmount + 1; k++)
            {
                total[k] = 0;

                if (grainCointainer[up, left].GrainId == k)
                {
                    total[k]++;
                }
                if (grainCointainer[up, j].GrainId == k)
                {
                    total[k]++;
                }
                if (grainCointainer[up, right].GrainId == k)
                {
                    total[k]++;
                }
                if (grainCointainer[i, left].GrainId == k)
                {
                    total[k]++;
                }
                if (grainCointainer[i, right].GrainId == k)
                {
                    total[k]++;
                }
                if (grainCointainer[down, left].GrainId == k)
                {
                    total[k]++;
                }
                if (grainCointainer[down, j].GrainId == k)
                {
                    total[k]++;
                }
                if (grainCointainer[down, right].GrainId == k)
                {
                    total[k]++;
                }
                if (total[k] > maximum)
                {
                    maximum = total[k];
                    id      = k;
                }
            }
            return(0);
        }
Exemplo n.º 18
0
        private List <Grain> TakeFurtherMooreNeighbourhood(int i, int j, Grain[,] structureArray)
        {
            var neighbourhood = new List <Grain>
            {
                structureArray[i - 1, j - 1],
                structureArray[i - 1, j + 1],
                structureArray[i + 1, j - 1],
                structureArray[i + 1, j + 1]
            };

            return(neighbourhood);
        }
Exemplo n.º 19
0
        private List <Grain> TakeNeumannNeighbourhood(int i, int j, Grain[,] structureArray)
        {
            var neighbourhood = new List <Grain>
            {
                structureArray[i, j + 1],
                structureArray[i + 1, j],
                structureArray[i - 1, j],
                structureArray[i, j - 1],
            };

            return(neighbourhood);
        }
Exemplo n.º 20
0
        private bool IsStateChangeAcceptable(Point point, Grain[,] structureArray, Grain newState)
        {
            var neighbourhood  = StructureHelpers.TakeMooreNeighbourhood(point.X, point.Y, structureArray);
            int previousEnergy = neighbourhood.Where(g => (g.Id != 0 && g.Id != structureArray[point.X, point.Y].Id)).Count();
            int newEnergy      = neighbourhood.Where(g => (g.Id != 0 && g.Id != newState.Id)).Count();

            if (newEnergy - previousEnergy <= 0)
            {
                return(true);
            }
            return(false);
        }
Exemplo n.º 21
0
    public void Simulate(Simulation grainGrowth, Graphics gB, Graphics g)
    {
        Grain[,] grains = grainGrowth.Tab;

        List <Grain> all = grains.OfType <Grain>().ToList();

        while (all.Count > 0)
        {
            if (BREAK_SIMULATION)
            {
                BREAK_SIMULATION = false;
                break;
            }



            int   index = random.Next(all.Count);
            Grain grain = all.ElementAt(index);
            all.RemoveAt(index);

            int stateBefore = grain.State;

            List <int> neighbours = NeighbourhoodFactory.GetNeighboursMonteCarlo(grains, grain.X, grain.Y);

            int energyBefore = CalculateEnergy(neighbours, stateBefore);

            grains[grain.Y, grain.X].Q = energyBefore;

            int stateAfter = (neighbours.ElementAt(random.Next(neighbours.Count)));

            int energyAfter = CalculateEnergy(neighbours, stateAfter);

            int deltaEnergy = energyAfter - energyBefore;
            if (deltaEnergy <= 0)
            {
                grains[grain.Y, grain.X].State = stateAfter;
                grain.Display(g, gB);
                grains[grain.Y, grain.X].Q = energyAfter;
            }
            else
            {
                float probability = (float)Math.Exp(-(deltaEnergy / this.KT)) * 100;
                float value       = (float)random.NextDouble() * 100;

                if (value <= probability)
                {
                    grains[grain.Y, grain.X].State = stateAfter;
                    grain.Display(g, gB);
                    grains[grain.Y, grain.X].Q = energyAfter;
                }
            }
        }
    }
 internal void initialize(Grain[,] grains, int w, int h)
 {
     for (int i = 0; i < w; i++)
     {
         for (int j = 0; j < h; j++)
         {
             if (grains[i, j] != null)
             {
                 grains[i, j].stan = Grain.TYPE_OLD_GRAIN;
             }
         }
     }
 }
Exemplo n.º 23
0
 public static void Import(int boardWidth, int boardHeight, Grain[,] grainsBoard)
 {
     using (System.IO.StreamWriter file = new System.IO.StreamWriter(@Path))
     {
         for (int i = 0; i < boardWidth - 1; i++)
         {
             for (int j = 0; j < boardHeight - 1; j++)
             {
                 file.WriteLine("{0} {1} {2} {3}", i, j, grainsBoard[i, j].Alive, ColorTranslator.ToHtml(grainsBoard[i, j].GetPenColor().Color));
             }
         }
     }
 }
Exemplo n.º 24
0
 public Grain[] getNeighborhood(Grain[,] grains, int x, int y, int left, int top, int bottom, int right)
 {
     Grain[] neighborhood = new Grain[8];
     neighborhood[0] = grains[left, top];
     neighborhood[1] = grains[x, top];
     neighborhood[2] = grains[right, top];
     neighborhood[3] = grains[left, y];
     neighborhood[4] = grains[right, y];
     neighborhood[5] = grains[left, bottom];
     neighborhood[6] = grains[x, bottom];
     neighborhood[7] = grains[right, bottom];
     return(neighborhood);
 }
Exemplo n.º 25
0
    public Simulation()
    {
        this.Tab            = new Grain[SIZE_Y, SIZE_X];
        this.previousGrains = new Grain[SIZE_Y, SIZE_X];

        for (int i = 0; i < SIZE_Y; i++)
        {
            for (int j = 0; j < SIZE_X; j++)
            {
                this.Tab[i, j]            = new Grain(j, i, 0);
                this.previousGrains[i, j] = this.Tab[i, j].Copy();
            }
        }
    }
Exemplo n.º 26
0
        private bool HasAnyRecrystalizedNeighbour(int i, int j, Grain[,] structureArray)
        {
            var neighbourhood          = StructureHelpers.TakeMooreNeighbourhood(i, j, structureArray);
            var recrystalizedNeighbour = neighbourhood.Where(g => (g.IsRecrystalized == true)).FirstOrDefault();

            if (recrystalizedNeighbour != null)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemplo n.º 27
0
 public static void Replace(Grain[,] grainTable, Grain[,] tempGrainTable, int xNumOfCells, int yNumOfCells)
 {
     for (int i = 0; i < xNumOfCells; i++)
     {
         for (int j = 0; j < yNumOfCells; j++)
         {
             grainTable[i, j].Rect.Fill   = tempGrainTable[i, j].Rect.Fill;
             grainTable[i, j].Rect.Stroke = tempGrainTable[i, j].Rect.Stroke;
             grainTable[i, j].Color       = tempGrainTable[i, j].Color;
             grainTable[i, j].State       = tempGrainTable[i, j].State;
             grainTable[i, j].id          = tempGrainTable[i, j].id;
         }
     }
 }
Exemplo n.º 28
0
 private bool IsAnyInclusion(Grain[,] tab)
 {
     for (var x = 0; x < XSize; x++)
     {
         for (var y = 0; y < YSize; y++)
         {
             if (tab[x, y].Value == 2)
             {
                 return(true);
             }
         }
     }
     return(false);
 }
Exemplo n.º 29
0
        public void createGrainArrays(int XSize, int YSize)
        {
            grainCointainer       = new Grain[YSize, XSize];
            grainCointainerSecond = new Grain[YSize, XSize];

            for (int i = 0; i < YSize; i++)
            {
                for (int j = 0; j < XSize; j++)
                {
                    grainCointainer[i, j]       = new Grain();
                    grainCointainerSecond[i, j] = new Grain();
                }
            }
        }
        public static List <Grain> TakeMooreNeighbourhood(int i, int j, Grain[,] structureArray)
        {
            var neighbourhood = new List <Grain>
            {
                structureArray[i - 1, j],
                structureArray[i + 1, j],
                structureArray[i, j - 1],
                structureArray[i, j + 1],
                structureArray[i - 1, j - 1],
                structureArray[i - 1, j + 1],
                structureArray[i + 1, j - 1],
                structureArray[i + 1, j + 1]
            };

            return(neighbourhood);
        }