Skip to content

Commit

Permalink
Remove new_group_axis from space groups
Browse files Browse the repository at this point in the history
  • Loading branch information
APJansen committed Jun 20, 2023
1 parent 63c8816 commit 49fdbc5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 31 deletions.
50 changes: 25 additions & 25 deletions src/space_groups.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,20 @@ Each space group is defined by a point group and a way of using it to act on a g
"""
struct SpaceGroup{I}
point_group::PointGroup
new_group_axis::Int
end

const P1 = SpaceGroup{:P1}(C1, 1)
const P2 = SpaceGroup{:P2}(C2, 1)
const P2MM = SpaceGroup{:P2MM}(D2, 1)
const P4 = SpaceGroup{:P4}(C4, 1)
const P4M = SpaceGroup{:P4M}(D4, 1)
const P1 = SpaceGroup{:P1}(C1)
const P2 = SpaceGroup{:P2}(C2)
const P2MM = SpaceGroup{:P2MM}(D2)
const P4 = SpaceGroup{:P4}(C4)
const P4M = SpaceGroup{:P4M}(D4)

SpaceGroups = [P1, P2, P2MM, P4, P4M]


function (g::SpaceGroup)(x::AbstractArray, axes::AxesInfo)
x, axes = lift(x, axes, g.new_group_axis)
x = act_on_grid(g, x, axes)
function (g::SpaceGroup)(x::AbstractArray, axes::AxesInfo, new_group_axis::Int)
x, axes = lift(x, axes, new_group_axis)
x = act_on_grid(g, x, axes, new_group_axis)
# TODO: acting on group
x
end
Expand All @@ -31,49 +30,50 @@ Apply a space group to a grid.
- `g`: The group to apply.
- `x`: The grid to act on.
- `axes`: The meaning of the axes of the grid.
- `new_group_axis`: The axis on which group actions should be concatenated.
# Returns
The grid after the group action has been applied.
"""
function act_on_grid(g::SpaceGroup, x::AbstractArray, axes::AxesInfo) end
function act_on_grid(g::SpaceGroup, x::AbstractArray, axes::AxesInfo, new_group_axis::Int) end

function act_on_grid(g::SpaceGroup{:P1}, x::AbstractArray, axes::AxesInfo)
function act_on_grid(g::SpaceGroup{:P1}, x::AbstractArray, axes::AxesInfo, new_group_axis::Int)
x
end

function act_on_grid(g::SpaceGroup{:P2}, x::AbstractArray, axes::AxesInfo)
function act_on_grid(g::SpaceGroup{:P2}, x::AbstractArray, axes::AxesInfo, new_group_axis::Int)
x_rotated = reverse(x, dims = (axes.width, axes.height))
x = cat(x, x_rotated, dims = g.new_group_axis)
x = cat(x, x_rotated, dims = new_group_axis)
x
end

function act_on_grid(g::SpaceGroup{:P2MM}, x::AbstractArray, axes::AxesInfo)
x = cat(x, reverse(x, dims = axes.height), dims = g.new_group_axis)
x = cat(x, reverse(x, dims = axes.width), dims = g.new_group_axis)
function act_on_grid(g::SpaceGroup{:P2MM}, x::AbstractArray, axes::AxesInfo, new_group_axis::Int)
x = cat(x, reverse(x, dims = axes.height), dims = new_group_axis)
x = cat(x, reverse(x, dims = axes.width), dims = new_group_axis)
x
end

function act_on_grid(g::SpaceGroup{:P4}, x::AbstractArray, axes::AxesInfo)
x = cat(x, reverse(x, dims = (axes.width, axes.height)), dims = g.new_group_axis)
function act_on_grid(g::SpaceGroup{:P4}, x::AbstractArray, axes::AxesInfo, new_group_axis::Int)
x = cat(x, reverse(x, dims = (axes.width, axes.height)), dims = new_group_axis)
x = cat(
x,
reverse(swap_axes(x, axes.width, axes.height), dims = axes.height),
dims = g.new_group_axis,
dims = new_group_axis,
)
# put the elements in the right order
order = [1, 3, 2, 4]
indices = ntuple(a -> a == g.new_group_axis ? order : :, ndims(x))
indices = ntuple(a -> a == new_group_axis ? order : :, ndims(x))
x = x[indices...]
x
end

function act_on_grid(g::SpaceGroup{:P4M}, x::AbstractArray, axes::AxesInfo)
x = cat(x, reverse(x, dims = axes.width), dims = g.new_group_axis)
x = cat(x, reverse(x, dims = axes.height), dims = g.new_group_axis)
x = cat(x, swap_axes(x, axes.width, axes.height), dims = g.new_group_axis)
function act_on_grid(g::SpaceGroup{:P4M}, x::AbstractArray, axes::AxesInfo, new_group_axis::Int)
x = cat(x, reverse(x, dims = axes.width), dims = new_group_axis)
x = cat(x, reverse(x, dims = axes.height), dims = new_group_axis)
x = cat(x, swap_axes(x, axes.width, axes.height), dims = new_group_axis)
# put the elements in the right order
order = [1, 6, 4, 7, 2, 5, 3, 8]
indices = ntuple(a -> a == g.new_group_axis ? order : :, ndims(x))
indices = ntuple(a -> a == new_group_axis ? order : :, ndims(x))
x = x[indices...]
x
end
13 changes: 7 additions & 6 deletions test/actiontests.jl
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
function test_action_composition(space_group::SpaceGroup, x::AbstractArray, axes::AxesInfo)
new_group_axis = 1
point_group = space_group.point_group
g_x = space_group(x, axes)
g_x = space_group(x, axes, new_group_axis)

# This is when acting two times, first with the element at axis 1, then at axis 2
# here we need to take care of the axes explicitly
_, axes = lift(x, axes, space_group.new_group_axis)
# Now the h axis is action.new_group_axis and the g axis is action.new_group_axis + 1
h_on_g_x = space_group(g_x, axes)
_, axes = lift(x, axes, new_group_axis)
# Now the h axis is new_group_axis and the g axis is new_group_axis + 1
h_on_g_x = space_group(g_x, axes, new_group_axis)

# Now first compose the point group elements, then act once
# flattened list of compositions
h_compose_g = reshape(point_group.composition, :)
indices = ntuple(a -> a == space_group.new_group_axis ? h_compose_g : :, ndims(g_x))
indices = ntuple(a -> a == new_group_axis ? h_compose_g : :, ndims(g_x))
h_g_on_x = g_x[indices...]
# This has the actions of all 2 group element compositions on one axis,
# so we need to spread them over two axes again
h_g_on_x = reshape(h_g_on_x, size(h_on_g_x)...)

# Check if the two are equal
@test all(h_on_g_x .=== h_g_on_x)
end
end

0 comments on commit 49fdbc5

Please sign in to comment.