Skip to content

Commit

Permalink
Merge pull request #10 from maslankam/model-refactoring
Browse files Browse the repository at this point in the history
Next version
  • Loading branch information
maslankam authored Nov 30, 2019
2 parents f481053 + 6eeba35 commit db2531c
Show file tree
Hide file tree
Showing 57 changed files with 3,190 additions and 697 deletions.
12 changes: 12 additions & 0 deletions ConsoleApp1/ConsoleApp1.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Model\Model.csproj" />
</ItemGroup>

</Project>
126 changes: 126 additions & 0 deletions ConsoleApp1/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
using System;
using Model;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApp1
{



class Program
{
private static CelluralAutomaton _automaton;
private static int _spaceSize;
private static int _grainsCount;
private static int _inclusionsCount;
private static int _minRadius;
private static int _maxRadius;
private static ITransitionRule _transition;
private static INeighbourhood _neighbourhood;
private static IBoundaryCondition _boundary;
private static bool _isAutomatonGenerated;
private static bool _isSaved;

static void Main(string[] args)
{



ITransitionRule transition = new GrainGrowthRule();
IBoundaryCondition boundary = new PeriodicBoundary();
INeighbourhood neighbourhood = new VonNeumanNeighbourhood(boundary);
_automaton = new CelluralAutomaton(500, 40, 30, 1, 5, transition, neighbourhood, boundary);

_spaceSize = 500;
_grainsCount = 20;
_inclusionsCount = 0;
_minRadius = 1;
_minRadius = 5;
_transition = new GrainGrowthRule();
_boundary = new AbsorbingBoundary();
_neighbourhood = new VonNeumanNeighbourhood(_boundary);


var doc = new XDocument(new XElement("Document"));

var widowVariables = new XElement("WindowVariables");
widowVariables.Add(new XAttribute("SpaceSize", _spaceSize));
widowVariables.Add(new XAttribute("GrainsCount", _grainsCount));
widowVariables.Add(new XAttribute("InclusionsCount", _inclusionsCount));
widowVariables.Add(new XAttribute("MinRadius", _minRadius));
widowVariables.Add(new XAttribute("MaxRadius", _inclusionsCount));
widowVariables.Add(new XAttribute("Transition", _transition.GetType()));
widowVariables.Add(new XAttribute("Neighbourhood", _neighbourhood.GetType()));
widowVariables.Add(new XAttribute("Boundary", _boundary.GetType()));
widowVariables.Add(new XAttribute("isGenerated", _isAutomatonGenerated));
widowVariables.Add(new XAttribute("isSaved", _isSaved));
doc.Root.Add(widowVariables);

var grains = new XElement("Grains");
foreach (var grain in _automaton.Grains)
{
var grainXmlElement = new XElement("Grain");

var id = new XAttribute("Id", grain.Id);
grainXmlElement.Add(id);

var phase = new XAttribute("P", grain.Phase);
grainXmlElement.Add(phase);
grainXmlElement.Add(new XAttribute("A", grain.Color.A));
grainXmlElement.Add(new XAttribute("R", grain.Color.R));
grainXmlElement.Add(new XAttribute("G", grain.Color.G));
grainXmlElement.Add(new XAttribute("B", grain.Color.B));

grains.Add(grainXmlElement);
}
doc.Root.Add(grains);

var inclusions = new XElement("Inclusions");
foreach (var inclusion in _automaton.Inclusions)
{
var inclusionXmlElement = new XElement("Grain");

var id = new XAttribute("Id", inclusion.Id);
inclusionXmlElement.Add(id);

var phase = new XAttribute("P", inclusion.Phase);
inclusionXmlElement.Add(phase);
inclusionXmlElement.Add(new XAttribute("rad", inclusion.Radius));
inclusionXmlElement.Add(new XAttribute("A", inclusion.Color.A));
inclusionXmlElement.Add(new XAttribute("R", inclusion.Color.R));
inclusionXmlElement.Add(new XAttribute("G", inclusion.Color.G));
inclusionXmlElement.Add(new XAttribute("B", inclusion.Color.B));

inclusions.Add(inclusionXmlElement);
}

doc.Root.Add(inclusions);


var cells = new XElement("Cells");
for (int i = 0; i < _automaton.Space.GetXLength(); i++)
{
var row = new XElement("Row");
row.Add(new XAttribute("x", i));
for (int j = 0; j < _automaton.Space.GetYLength(); j++)
{
//Console.WriteLine($"{i},{j}");
var cell = _automaton.Space.GetCell(i, j);

var c = new XElement("c");
c.Add(new XAttribute("y", j));
c.Add(new XAttribute("p", cell?.Phase?.ToString() ?? ""));
c.Add(new XAttribute("i", cell?.MicroelementMembership?.Id.ToString() ?? ""));

row.Add(c);
}
cells.Add(row);
}
doc.Root.Add(cells);

//Console.WriteLine("Saving");
doc.Save(@"C:\Users\mikim\Desktop\Multiscale Modeling\ConsoleApp1\Nowy dokument tekstowy.xml");
}
}
}
22 changes: 0 additions & 22 deletions Model/AbsorbingBoundary.cs

This file was deleted.

33 changes: 33 additions & 0 deletions Model/Boundary/AbsorbingBoundary.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;

namespace Model{


public class AbsorbingBoundary : IBoundaryCondition
{
public string Name
{
get
{
return this.ToString();
}
}

// AbsorbingBoundary always return null Cell
/// |nl|nl|nl|nl|nl|
/// |nl|00|01|02|nl|
/// |nl|10|11|12|nl|
/// |nl|20|21|22|nl|
/// |nl|nl|nl|nl|nl|
public Cell GetBoundaryNeighbour(CelluralSpace space, int x, int y, BoundaryDirection direction) {
return null;
}

public override string ToString()
{
return "AbsorbingBoundary";
}
}


}
15 changes: 15 additions & 0 deletions Model/Boundary/IBoundaryCondition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;

namespace Model{

public enum BoundaryDirection{
N, NE, E, SE , S, SW, W, NW
}

public interface IBoundaryCondition
{
public Cell GetBoundaryNeighbour(CelluralSpace space, int x, int y, BoundaryDirection direction);
}


}
116 changes: 116 additions & 0 deletions Model/Boundary/PeriodicBoundary.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
using System;

namespace Model
{


public class PeriodicBoundary : IBoundaryCondition
{
public string Name
{
get
{
return this.ToString();
}
}

// AbsorbingBoundary always return null Cell
/// y ->
///x |22|20|21|22|20|
///| |02|00|01|02|00|
///v |12|10|11|12|10|
/// |22|20|21|22|20|
/// |02|00|01|02|00|
public Cell GetBoundaryNeighbour(CelluralSpace space, int x, int y, BoundaryDirection direction)
{
switch (direction)
{
case BoundaryDirection.N: return space.GetCell( space.GetXLength() - 1, y);
case BoundaryDirection.NE:
{
if (y == space.GetYLength() - 1)
{
if(x == 0)
{
return space.GetCell(space.GetXLength() - 1, 0);
}
else
{
return space.GetCell(x - 1, 0);
}
}
else
{
return space.GetCell(space.GetXLength() - 1, y + 1);
}
}
case BoundaryDirection.E: return space.GetCell( x, 0);
case BoundaryDirection.SE:
{
if (x == space.GetXLength() - 1)
{
if (y == space.GetYLength() - 1)
{
return space.GetCell(0, 0);
}
else
{
return space.GetCell(0, y + 1);
}
}
else
{
return space.GetCell(x + 1, 0);
}
}
case BoundaryDirection.S: return space.GetCell( 0, y);
case BoundaryDirection.SW:
{
if (x == space.GetYLength() - 1)
{
if (y == 0)
{
return space.GetCell(0, 2);
}
else
{
return space.GetCell(0, y - 1);
}
}
else
{
return space.GetCell(x + 1, space.GetYLength() - 1);
}
}
case BoundaryDirection.W: return space.GetCell( x, space.GetYLength() - 1);
case BoundaryDirection.NW:
{
if (y == 0)
{
if (x == 0)
{
return space.GetCell(2, 2);
}
else
{
return space.GetCell(x - 1, space.GetYLength() - 1);
}
}
else
{
return space.GetCell(space.GetXLength() - 1, y - 1);
}
}
default: throw new ArgumentException("Wrong direction");
}
}

public override string ToString()
{
return "PeriodicBoundary";
}

}


}
14 changes: 12 additions & 2 deletions Model/Cell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,22 @@
namespace Model{
public class Cell
{
public Grain GrainMembership;
public int? Phase
{
get{return MicroelementMembership?.Phase ?? null;}
private set{}
}
public Microelement MicroelementMembership { get; set; }

public Cell()
{
this.GrainMembership = null;
MicroelementMembership = null;
}
public Cell(Microelement microelement)
{
MicroelementMembership = microelement;
}

}


Expand Down
Loading

0 comments on commit db2531c

Please sign in to comment.