How to build a neighbor table for the Hexagonal lattice
Clash Royale CLAN TAG#URR8PPP
up vote
4
down vote
favorite
I want to consider a collection of vertices arranged in a finite Hexagonal lattice, with say $n_r$ rows and $n_x$ vertices per row, for a total of $N = n_xn_r$ vertices. The goal is to construct a neighbor table, which is a matrix $A$ with dimensionality $Ntimes N$. Each element $A_ij$ is one if there is a bond connecting vertices $i$ and $j$, and zero otherwise. This can also be seen as an adjacency matrix for a particular graph.
I can't think of a way to build this matrix that isn't frustrating to code as a function of $n_r$ and $n_x$, but I'm aware there are some lattice functions built into Mathematica. Maybe those can make the process more smooth if anyone has suggestions.
Also, to make things a little harder, I also want the option of including periodic boundary conditions. This just means if you walk off the finite lattice in a particular direction (say, off the left-hand side) you reappear on the opposite side of the lattice (in this case the right hand side). This means there are new connections that would look long-ranged in any planar representation of the graph, or you can think of it as putting the graph on a torus. Help with that case would be particularly appreciated.
graphs-and-networks lattices adjacency
add a comment |Â
up vote
4
down vote
favorite
I want to consider a collection of vertices arranged in a finite Hexagonal lattice, with say $n_r$ rows and $n_x$ vertices per row, for a total of $N = n_xn_r$ vertices. The goal is to construct a neighbor table, which is a matrix $A$ with dimensionality $Ntimes N$. Each element $A_ij$ is one if there is a bond connecting vertices $i$ and $j$, and zero otherwise. This can also be seen as an adjacency matrix for a particular graph.
I can't think of a way to build this matrix that isn't frustrating to code as a function of $n_r$ and $n_x$, but I'm aware there are some lattice functions built into Mathematica. Maybe those can make the process more smooth if anyone has suggestions.
Also, to make things a little harder, I also want the option of including periodic boundary conditions. This just means if you walk off the finite lattice in a particular direction (say, off the left-hand side) you reappear on the opposite side of the lattice (in this case the right hand side). This means there are new connections that would look long-ranged in any planar representation of the graph, or you can think of it as putting the graph on a torus. Help with that case would be particularly appreciated.
graphs-and-networks lattices adjacency
add a comment |Â
up vote
4
down vote
favorite
up vote
4
down vote
favorite
I want to consider a collection of vertices arranged in a finite Hexagonal lattice, with say $n_r$ rows and $n_x$ vertices per row, for a total of $N = n_xn_r$ vertices. The goal is to construct a neighbor table, which is a matrix $A$ with dimensionality $Ntimes N$. Each element $A_ij$ is one if there is a bond connecting vertices $i$ and $j$, and zero otherwise. This can also be seen as an adjacency matrix for a particular graph.
I can't think of a way to build this matrix that isn't frustrating to code as a function of $n_r$ and $n_x$, but I'm aware there are some lattice functions built into Mathematica. Maybe those can make the process more smooth if anyone has suggestions.
Also, to make things a little harder, I also want the option of including periodic boundary conditions. This just means if you walk off the finite lattice in a particular direction (say, off the left-hand side) you reappear on the opposite side of the lattice (in this case the right hand side). This means there are new connections that would look long-ranged in any planar representation of the graph, or you can think of it as putting the graph on a torus. Help with that case would be particularly appreciated.
graphs-and-networks lattices adjacency
I want to consider a collection of vertices arranged in a finite Hexagonal lattice, with say $n_r$ rows and $n_x$ vertices per row, for a total of $N = n_xn_r$ vertices. The goal is to construct a neighbor table, which is a matrix $A$ with dimensionality $Ntimes N$. Each element $A_ij$ is one if there is a bond connecting vertices $i$ and $j$, and zero otherwise. This can also be seen as an adjacency matrix for a particular graph.
I can't think of a way to build this matrix that isn't frustrating to code as a function of $n_r$ and $n_x$, but I'm aware there are some lattice functions built into Mathematica. Maybe those can make the process more smooth if anyone has suggestions.
Also, to make things a little harder, I also want the option of including periodic boundary conditions. This just means if you walk off the finite lattice in a particular direction (say, off the left-hand side) you reappear on the opposite side of the lattice (in this case the right hand side). This means there are new connections that would look long-ranged in any planar representation of the graph, or you can think of it as putting the graph on a torus. Help with that case would be particularly appreciated.
graphs-and-networks lattices adjacency
graphs-and-networks lattices adjacency
asked 3 hours ago
mflynn
1084
1084
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
4
down vote
accepted
I will take this opportunity to showcase the abilities of IGraph/M for lattice generation and mesh / graph / matrix conversions. IGraph/M thrives on user feedback, so if you find it useful, please take some time to write a few comments about you experiece. It will help me improve the package.
Non-periodic case
You can directly generate a (non-periodic) lattice with IGraph/M.
<< IGraphM`
mesh = IGLatticeMesh["Hexagonal", Polygon@CirclePoints[3, 6],
MeshCellLabel -> 2 -> "Index"]
The second argument of IGLatticeMesh
may be a region. This region will be filled with cells. In this case, I chose a big hexagon to be filled with small hexagonal cells.
The cell adjacency matrix:
am = IGMeshCellAdjacencyMatrix[mesh, 2]
"2" means 2-dimensional cells, i.e. little hexagons. "1" would mean edges and "0" points.
MatrixPlot[am]
If you need the graph,
graph = IGMeshCellAdjacencyGraph[mesh, 2,
VertexCoordinates -> Automatic]
Notice that this is actually a triangular connectivity, which could also be generated directly (in some shapes) with IGTriangularLattice
. Demo:
IGTriangularLattice[4], IGTriangularLattice[3, 5]
We could have use IGLatticeMesh
too:
IGLatticeMesh["Triangular", 3, 3]
Let's get the point-to-point connectivity now (instead of the cell-to-cell one):
IGMeshCellAdjacencyGraph[%, 0]
Periodic case
Now let us do the periodic case.
We start with a hex lattice arrange in an $ntimes m$ grid.
n, m = 5, 6;
mesh = IGLatticeMesh["Hexagonal", n, m, MeshCellLabel -> 2 -> "Index"]
Convert it to a graph. This time I will not preserve the vertex coordinates so that we can get a clearer layout after we make the lattice periodic.
graph = IGMeshCellAdjacencyGraph[mesh, 2, VertexLabels -> "Name"];
graph = VertexReplace[graph, 2, i_ :> i]
I have also converted the vertex names, which were of the form 2, index
(2 indicating two-dimensional mesh cells) to simply index
.
We add the extra edges needed for periodic boundary conditions.
extraEdges = DeleteDuplicates@Flatten@Table[
If[Mod[i, m] == 0, i <-> i - m + 1, i <-> Mod[i - 2 m + 1, m n, 1], ],
If[i <= m, i <-> i + m n - m, i <-> Mod[i + m n - m + 1, m n, 1], ],
i, m n
]
pgraph = EdgeAdd[graph, extraEdges]
Then we can get (or plot) the graph's adjacency matrix.
IGAdjacencyMatrixPlot[pgraph]
am = AdjacencyMatrix[graph]
Extra visualization: here's the graph in 3D with m,n = 10,20
:
(* remember to re-create graph and extraEdges after setting m,n *)
pgraph = Graph3D[EdgeAdd[graph, extraEdges], VertexLabels -> None]
An alternative solution for the periodic case
The adjacency relations of hexagonal cells form a triangular lattice. There is a function in IGraph/M for directly generating a triangular lattice graph, and it has an option to make it periodic:
IGTriangularLattice[5, 10]
IGTriangularLattice[5, 10, "Periodic" -> True]
Then you can just get the adjacency matrix again.
Note that the m,n
syntax in IGLatticeMesh
and IGTriangularLattice
do not have the exact same meaning—pay attention to the difference if you mix these approaches! The vertex labelling will also be different. Presumably at some point you will want to use the visualization of the hex lattice mesh to plot your results. Thus it is useful to be able to map back to mesh cell indices.
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
I will take this opportunity to showcase the abilities of IGraph/M for lattice generation and mesh / graph / matrix conversions. IGraph/M thrives on user feedback, so if you find it useful, please take some time to write a few comments about you experiece. It will help me improve the package.
Non-periodic case
You can directly generate a (non-periodic) lattice with IGraph/M.
<< IGraphM`
mesh = IGLatticeMesh["Hexagonal", Polygon@CirclePoints[3, 6],
MeshCellLabel -> 2 -> "Index"]
The second argument of IGLatticeMesh
may be a region. This region will be filled with cells. In this case, I chose a big hexagon to be filled with small hexagonal cells.
The cell adjacency matrix:
am = IGMeshCellAdjacencyMatrix[mesh, 2]
"2" means 2-dimensional cells, i.e. little hexagons. "1" would mean edges and "0" points.
MatrixPlot[am]
If you need the graph,
graph = IGMeshCellAdjacencyGraph[mesh, 2,
VertexCoordinates -> Automatic]
Notice that this is actually a triangular connectivity, which could also be generated directly (in some shapes) with IGTriangularLattice
. Demo:
IGTriangularLattice[4], IGTriangularLattice[3, 5]
We could have use IGLatticeMesh
too:
IGLatticeMesh["Triangular", 3, 3]
Let's get the point-to-point connectivity now (instead of the cell-to-cell one):
IGMeshCellAdjacencyGraph[%, 0]
Periodic case
Now let us do the periodic case.
We start with a hex lattice arrange in an $ntimes m$ grid.
n, m = 5, 6;
mesh = IGLatticeMesh["Hexagonal", n, m, MeshCellLabel -> 2 -> "Index"]
Convert it to a graph. This time I will not preserve the vertex coordinates so that we can get a clearer layout after we make the lattice periodic.
graph = IGMeshCellAdjacencyGraph[mesh, 2, VertexLabels -> "Name"];
graph = VertexReplace[graph, 2, i_ :> i]
I have also converted the vertex names, which were of the form 2, index
(2 indicating two-dimensional mesh cells) to simply index
.
We add the extra edges needed for periodic boundary conditions.
extraEdges = DeleteDuplicates@Flatten@Table[
If[Mod[i, m] == 0, i <-> i - m + 1, i <-> Mod[i - 2 m + 1, m n, 1], ],
If[i <= m, i <-> i + m n - m, i <-> Mod[i + m n - m + 1, m n, 1], ],
i, m n
]
pgraph = EdgeAdd[graph, extraEdges]
Then we can get (or plot) the graph's adjacency matrix.
IGAdjacencyMatrixPlot[pgraph]
am = AdjacencyMatrix[graph]
Extra visualization: here's the graph in 3D with m,n = 10,20
:
(* remember to re-create graph and extraEdges after setting m,n *)
pgraph = Graph3D[EdgeAdd[graph, extraEdges], VertexLabels -> None]
An alternative solution for the periodic case
The adjacency relations of hexagonal cells form a triangular lattice. There is a function in IGraph/M for directly generating a triangular lattice graph, and it has an option to make it periodic:
IGTriangularLattice[5, 10]
IGTriangularLattice[5, 10, "Periodic" -> True]
Then you can just get the adjacency matrix again.
Note that the m,n
syntax in IGLatticeMesh
and IGTriangularLattice
do not have the exact same meaning—pay attention to the difference if you mix these approaches! The vertex labelling will also be different. Presumably at some point you will want to use the visualization of the hex lattice mesh to plot your results. Thus it is useful to be able to map back to mesh cell indices.
add a comment |Â
up vote
4
down vote
accepted
I will take this opportunity to showcase the abilities of IGraph/M for lattice generation and mesh / graph / matrix conversions. IGraph/M thrives on user feedback, so if you find it useful, please take some time to write a few comments about you experiece. It will help me improve the package.
Non-periodic case
You can directly generate a (non-periodic) lattice with IGraph/M.
<< IGraphM`
mesh = IGLatticeMesh["Hexagonal", Polygon@CirclePoints[3, 6],
MeshCellLabel -> 2 -> "Index"]
The second argument of IGLatticeMesh
may be a region. This region will be filled with cells. In this case, I chose a big hexagon to be filled with small hexagonal cells.
The cell adjacency matrix:
am = IGMeshCellAdjacencyMatrix[mesh, 2]
"2" means 2-dimensional cells, i.e. little hexagons. "1" would mean edges and "0" points.
MatrixPlot[am]
If you need the graph,
graph = IGMeshCellAdjacencyGraph[mesh, 2,
VertexCoordinates -> Automatic]
Notice that this is actually a triangular connectivity, which could also be generated directly (in some shapes) with IGTriangularLattice
. Demo:
IGTriangularLattice[4], IGTriangularLattice[3, 5]
We could have use IGLatticeMesh
too:
IGLatticeMesh["Triangular", 3, 3]
Let's get the point-to-point connectivity now (instead of the cell-to-cell one):
IGMeshCellAdjacencyGraph[%, 0]
Periodic case
Now let us do the periodic case.
We start with a hex lattice arrange in an $ntimes m$ grid.
n, m = 5, 6;
mesh = IGLatticeMesh["Hexagonal", n, m, MeshCellLabel -> 2 -> "Index"]
Convert it to a graph. This time I will not preserve the vertex coordinates so that we can get a clearer layout after we make the lattice periodic.
graph = IGMeshCellAdjacencyGraph[mesh, 2, VertexLabels -> "Name"];
graph = VertexReplace[graph, 2, i_ :> i]
I have also converted the vertex names, which were of the form 2, index
(2 indicating two-dimensional mesh cells) to simply index
.
We add the extra edges needed for periodic boundary conditions.
extraEdges = DeleteDuplicates@Flatten@Table[
If[Mod[i, m] == 0, i <-> i - m + 1, i <-> Mod[i - 2 m + 1, m n, 1], ],
If[i <= m, i <-> i + m n - m, i <-> Mod[i + m n - m + 1, m n, 1], ],
i, m n
]
pgraph = EdgeAdd[graph, extraEdges]
Then we can get (or plot) the graph's adjacency matrix.
IGAdjacencyMatrixPlot[pgraph]
am = AdjacencyMatrix[graph]
Extra visualization: here's the graph in 3D with m,n = 10,20
:
(* remember to re-create graph and extraEdges after setting m,n *)
pgraph = Graph3D[EdgeAdd[graph, extraEdges], VertexLabels -> None]
An alternative solution for the periodic case
The adjacency relations of hexagonal cells form a triangular lattice. There is a function in IGraph/M for directly generating a triangular lattice graph, and it has an option to make it periodic:
IGTriangularLattice[5, 10]
IGTriangularLattice[5, 10, "Periodic" -> True]
Then you can just get the adjacency matrix again.
Note that the m,n
syntax in IGLatticeMesh
and IGTriangularLattice
do not have the exact same meaning—pay attention to the difference if you mix these approaches! The vertex labelling will also be different. Presumably at some point you will want to use the visualization of the hex lattice mesh to plot your results. Thus it is useful to be able to map back to mesh cell indices.
add a comment |Â
up vote
4
down vote
accepted
up vote
4
down vote
accepted
I will take this opportunity to showcase the abilities of IGraph/M for lattice generation and mesh / graph / matrix conversions. IGraph/M thrives on user feedback, so if you find it useful, please take some time to write a few comments about you experiece. It will help me improve the package.
Non-periodic case
You can directly generate a (non-periodic) lattice with IGraph/M.
<< IGraphM`
mesh = IGLatticeMesh["Hexagonal", Polygon@CirclePoints[3, 6],
MeshCellLabel -> 2 -> "Index"]
The second argument of IGLatticeMesh
may be a region. This region will be filled with cells. In this case, I chose a big hexagon to be filled with small hexagonal cells.
The cell adjacency matrix:
am = IGMeshCellAdjacencyMatrix[mesh, 2]
"2" means 2-dimensional cells, i.e. little hexagons. "1" would mean edges and "0" points.
MatrixPlot[am]
If you need the graph,
graph = IGMeshCellAdjacencyGraph[mesh, 2,
VertexCoordinates -> Automatic]
Notice that this is actually a triangular connectivity, which could also be generated directly (in some shapes) with IGTriangularLattice
. Demo:
IGTriangularLattice[4], IGTriangularLattice[3, 5]
We could have use IGLatticeMesh
too:
IGLatticeMesh["Triangular", 3, 3]
Let's get the point-to-point connectivity now (instead of the cell-to-cell one):
IGMeshCellAdjacencyGraph[%, 0]
Periodic case
Now let us do the periodic case.
We start with a hex lattice arrange in an $ntimes m$ grid.
n, m = 5, 6;
mesh = IGLatticeMesh["Hexagonal", n, m, MeshCellLabel -> 2 -> "Index"]
Convert it to a graph. This time I will not preserve the vertex coordinates so that we can get a clearer layout after we make the lattice periodic.
graph = IGMeshCellAdjacencyGraph[mesh, 2, VertexLabels -> "Name"];
graph = VertexReplace[graph, 2, i_ :> i]
I have also converted the vertex names, which were of the form 2, index
(2 indicating two-dimensional mesh cells) to simply index
.
We add the extra edges needed for periodic boundary conditions.
extraEdges = DeleteDuplicates@Flatten@Table[
If[Mod[i, m] == 0, i <-> i - m + 1, i <-> Mod[i - 2 m + 1, m n, 1], ],
If[i <= m, i <-> i + m n - m, i <-> Mod[i + m n - m + 1, m n, 1], ],
i, m n
]
pgraph = EdgeAdd[graph, extraEdges]
Then we can get (or plot) the graph's adjacency matrix.
IGAdjacencyMatrixPlot[pgraph]
am = AdjacencyMatrix[graph]
Extra visualization: here's the graph in 3D with m,n = 10,20
:
(* remember to re-create graph and extraEdges after setting m,n *)
pgraph = Graph3D[EdgeAdd[graph, extraEdges], VertexLabels -> None]
An alternative solution for the periodic case
The adjacency relations of hexagonal cells form a triangular lattice. There is a function in IGraph/M for directly generating a triangular lattice graph, and it has an option to make it periodic:
IGTriangularLattice[5, 10]
IGTriangularLattice[5, 10, "Periodic" -> True]
Then you can just get the adjacency matrix again.
Note that the m,n
syntax in IGLatticeMesh
and IGTriangularLattice
do not have the exact same meaning—pay attention to the difference if you mix these approaches! The vertex labelling will also be different. Presumably at some point you will want to use the visualization of the hex lattice mesh to plot your results. Thus it is useful to be able to map back to mesh cell indices.
I will take this opportunity to showcase the abilities of IGraph/M for lattice generation and mesh / graph / matrix conversions. IGraph/M thrives on user feedback, so if you find it useful, please take some time to write a few comments about you experiece. It will help me improve the package.
Non-periodic case
You can directly generate a (non-periodic) lattice with IGraph/M.
<< IGraphM`
mesh = IGLatticeMesh["Hexagonal", Polygon@CirclePoints[3, 6],
MeshCellLabel -> 2 -> "Index"]
The second argument of IGLatticeMesh
may be a region. This region will be filled with cells. In this case, I chose a big hexagon to be filled with small hexagonal cells.
The cell adjacency matrix:
am = IGMeshCellAdjacencyMatrix[mesh, 2]
"2" means 2-dimensional cells, i.e. little hexagons. "1" would mean edges and "0" points.
MatrixPlot[am]
If you need the graph,
graph = IGMeshCellAdjacencyGraph[mesh, 2,
VertexCoordinates -> Automatic]
Notice that this is actually a triangular connectivity, which could also be generated directly (in some shapes) with IGTriangularLattice
. Demo:
IGTriangularLattice[4], IGTriangularLattice[3, 5]
We could have use IGLatticeMesh
too:
IGLatticeMesh["Triangular", 3, 3]
Let's get the point-to-point connectivity now (instead of the cell-to-cell one):
IGMeshCellAdjacencyGraph[%, 0]
Periodic case
Now let us do the periodic case.
We start with a hex lattice arrange in an $ntimes m$ grid.
n, m = 5, 6;
mesh = IGLatticeMesh["Hexagonal", n, m, MeshCellLabel -> 2 -> "Index"]
Convert it to a graph. This time I will not preserve the vertex coordinates so that we can get a clearer layout after we make the lattice periodic.
graph = IGMeshCellAdjacencyGraph[mesh, 2, VertexLabels -> "Name"];
graph = VertexReplace[graph, 2, i_ :> i]
I have also converted the vertex names, which were of the form 2, index
(2 indicating two-dimensional mesh cells) to simply index
.
We add the extra edges needed for periodic boundary conditions.
extraEdges = DeleteDuplicates@Flatten@Table[
If[Mod[i, m] == 0, i <-> i - m + 1, i <-> Mod[i - 2 m + 1, m n, 1], ],
If[i <= m, i <-> i + m n - m, i <-> Mod[i + m n - m + 1, m n, 1], ],
i, m n
]
pgraph = EdgeAdd[graph, extraEdges]
Then we can get (or plot) the graph's adjacency matrix.
IGAdjacencyMatrixPlot[pgraph]
am = AdjacencyMatrix[graph]
Extra visualization: here's the graph in 3D with m,n = 10,20
:
(* remember to re-create graph and extraEdges after setting m,n *)
pgraph = Graph3D[EdgeAdd[graph, extraEdges], VertexLabels -> None]
An alternative solution for the periodic case
The adjacency relations of hexagonal cells form a triangular lattice. There is a function in IGraph/M for directly generating a triangular lattice graph, and it has an option to make it periodic:
IGTriangularLattice[5, 10]
IGTriangularLattice[5, 10, "Periodic" -> True]
Then you can just get the adjacency matrix again.
Note that the m,n
syntax in IGLatticeMesh
and IGTriangularLattice
do not have the exact same meaning—pay attention to the difference if you mix these approaches! The vertex labelling will also be different. Presumably at some point you will want to use the visualization of the hex lattice mesh to plot your results. Thus it is useful to be able to map back to mesh cell indices.
edited 15 mins ago
answered 57 mins ago
Szabolcs
155k13419909
155k13419909
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f184792%2fhow-to-build-a-neighbor-table-for-the-hexagonal-lattice%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password