Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add compressed cell values #41

Merged
merged 14 commits into from
Jul 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
219 changes: 219 additions & 0 deletions src/CellValues/CompressedCellValues.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
module CompressedCellValues

using Gridap
using Gridap.CellValuesGallery
using Gridap.CellNumberApply: IndexCellNumberFromKernel, CellNumberFromKernel
using Gridap.CellArrayApply: IndexCellArrayFromKernel, CellArrayFromKernel
using Gridap.CellMaps: IterCellMapValue, IndexCellMapValue

export IterCompressedCellValue
export IndexCompressedCellValue
export CompressedCellValue
export CompressedCellArray
export CompressedCellMap

import Base: iterate
import Base: length
import Base: size
import Base: getindex

import Gridap: apply
import Gridap: reindex
import Gridap: evaluate
import Base: ==, ≈

struct IterCompressedCellValue{T,A} <: IterCellValue{T}
values::Vector{T}
ptrs::A

function IterCompressedCellValue(
values::Vector{T}, ptrs) where T
@assert hasmethod(iterate,Tuple{typeof(ptrs)})
@assert hasmethod(length,Tuple{typeof(ptrs)})
A = typeof(ptrs)
new{T,A}(values,ptrs)
end

end

@inline function iterate(cv::IterCompressedCellValue)
inext = iterate(cv.ptrs)
_iterate(cv,inext)
end

@inline function iterate(cv::IterCompressedCellValue,state)
inext = iterate(cv.ptrs,state)
_iterate(cv,inext)
end

@inline function _iterate(cv,inext)
if inext === nothing; return nothing; end
i, istate = inext
v = cv.values[i]
(v,istate)
end

length(cv::IterCompressedCellValue) = length(cv.ptrs)

struct IndexCompressedCellValue{T,A} <: IndexCellValue{T,1}
values::Vector{T}
ptrs::A

function IndexCompressedCellValue(
values::Vector{T}, ptrs::AbstractArray) where T
A = typeof(ptrs)
new{T,A}(values,ptrs)
end

end

function IndexCompressedCellValue(cv::ConstantCellValue)
values = [cv.value,]
ptrs = ConstantCellValue(1,length(cv))
IndexCompressedCellValue(values,ptrs)
end

function getindex(
cv::IndexCompressedCellValue{T,A}, i::Integer) where {T,A}
j = cv.ptrs[i]
cv.values[j]
end

size(cv::IndexCompressedCellValue) = (length(cv.ptrs),)

const CompressedCellValue{T} = Union{
IterCompressedCellValue{T},IndexCompressedCellValue{T}}

function CompressedCellValue(values::Vector{T},ptrs::AbstractArray) where T
IndexCompressedCellValue(values,ptrs)
end

function CompressedCellValue(values::Vector{T},ptrs) where T
IterCompressedCellValue(values,ptrs)
end

function (==)(a::CompressedCellValue,b::CompressedCellValue)
_eq_kernel(==,a,b)
end

function (≈)(a::CompressedCellValue,b::CompressedCellValue)
_eq_kernel(≈,a,b)
end

function _eq_kernel(op,a,b)
!(op(a.values,b.values)) && return false
!( a.ptrs == b.ptrs ) && return false
length(a) != length(b) && return false
return true
end

function apply(k::NumberKernel,v::Vararg{<:CompressedCellValue})
optim = _is_optimizable(v...)
_apply(Val(optim),k,v...)
end

function apply(k::ArrayKernel,v::Vararg{<:CompressedCellValue})
optim = _is_optimizable(v...)
_apply(Val(optim),k,v...)
end

function _is_optimizable(v...)
@assert length(v) > 0
v1, = v
all( [ _is_compatible_data(vi,v1) for vi in v] )
end

function _is_compatible_data(a,b)
if length(a.values) != length(b.values)
return false
end
if a.ptrs === b.ptrs
return true
end
if a.ptrs == b.ptrs
return true
end
false
end

function _apply(::Val{true},k,v...)
v1, = v
n = length(v1.values)
input_values = [ [ vi.values[i] for vi in v] for i in 1:n]
values = [ compute_value(k,vals...) for vals in input_values ]
CompressedCellValue(values,v1.ptrs)
end

function _apply(::Val{false},k::NumberKernel,v::Vararg{<:IndexCompressedCellValue})
IndexCellNumberFromKernel(k,v...)
end

function _apply(::Val{false},k::NumberKernel,v::Vararg{<:CompressedCellValue})
CellNumberFromKernel(k,v...)
end

function _apply(::Val{false},k::ArrayKernel,v::Vararg{<:IndexCompressedCellValue})
IndexCellArrayFromKernel(k,v...)
end

function _apply(::Val{false},k::ArrayKernel,v::Vararg{<:CompressedCellValue})
CellArrayFromKernel(k,v...)
end

const CompressedCellArray{T,N} = CompressedCellValue{<:AbstractArray{T,N}}

function CompressedCellArray(values::Vector{<:AbstractArray},ptrs)
CompressedCellValue(values,ptrs)
end

const CompressedCellMap{S,M,T,N} = CompressedCellValue{<:Map{S,M,T,N}}

function CompressedCellMap(values::Vector{<:Map},ptrs)
CompressedCellValue(values,ptrs)
end


function evaluate(cm::ConstantCellMap{S,M},ca::CompressedCellArray{<:S,M}) where {S,M}
@assert length(cm) == length(ca)
m = cm.value
rs = [ evaluate(m,a) for a in ca.values]
CompressedCellValue(rs,ca.ptrs)
end

function evaluate(cm::CompressedCellMap{S,M},ca::CompressedCellArray{<:S,M}) where {S,M}
@assert length(cm) == length(ca)
optim = _is_optimizable(cm,ca)
_evaluate(Val(optim),cm,ca)
end

function _evaluate(::Val{true},cm::CompressedCellMap,ca::CompressedCellValue)
n = length(cm.values)
input_values = [ ( cm.values[i], ca.values[i] ) for i in 1:n]
values = [ evaluate(mi,ai) for (mi,ai) in input_values ]
CompressedCellValue(values,cm.ptrs)
end

function _evaluate(::Val{false},cm::CellMap,ca::CellArray)
IterCellMapValue(cm,ca)
end

function _evaluate(::Val{false},cm::IndexCellMap,ca::IndexCellArray)
IndexCellMapValue(cm,ca)
end

function reindex(values::CompressedCellValue, indices::CellValue{<:IndexLike})
vals = values.values
ptrs = reindex(_ptrs(values.ptrs),indices)
CompressedCellValue(vals,ptrs)
end

function reindex(values::CompressedCellValue, indices::IndexCellValue{<:IndexLike})
vals = values.values
ptrs = reindex(_ptrs(values.ptrs),indices)
CompressedCellValue(vals,ptrs)
end

_ptrs(p::CellValue) = p
_ptrs(p::AbstractArray) = CellValueFromArray(p)

end # module
2 changes: 2 additions & 0 deletions src/CellValues/files.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,7 @@ include("FindLocalIndex.jl")
include("ConstantCellValues.jl")
@reexport using Gridap.ConstantCellValues

include("CompressedCellValues.jl")
@reexport using Gridap.CompressedCellValues


140 changes: 140 additions & 0 deletions test/CellValuesTests/CompressedCellValuesTests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
module CompressedCellValuesTests

using Test
using Gridap
using Gridap.CellValuesGallery
using ..MapsMocks

values = [10,20,31]
ptrs = [1,2,3,3,2,2]
r = values[ptrs]
cv = IterCompressedCellValue(values,ptrs)
test_iter_cell_value(cv,r)

cv = IndexCompressedCellValue(values,ptrs)
test_index_cell_value(cv,r)

l = 10
v = 3
ccv = ConstantCellValue(v,l)
cv = IndexCompressedCellValue(ccv)
test_index_cell_value(cv,fill(v,l))

@test cv == cv
@test cv ≈ cv

values = [10,20,31]
ptrs = [1,2,3,3,2,2]
cv = CompressedCellValue(values,ptrs)

cv2 = apply(-,cv)
@test isa(cv2,IndexCompressedCellValue)
r = -values[ptrs]
test_index_cell_value(cv2,r)

values1 = [10,20,31]
values2 = [11,21,51]
ptrs = [1,2,3,3,2,2]
cv1 = CompressedCellValue(values1,ptrs)
cv2 = CompressedCellValue(values2,ptrs)
cv3 = apply(-,cv1,cv2)
@test isa(cv3,IndexCompressedCellValue)
r = values1[ptrs] - values2[ptrs]
test_index_cell_value(cv3,r)

values1 = [10,20,31]
values2 = [11,21,51]
ptrs1 = [1,2,3,3,2,2]
ptrs2 = [1,1,3,2,3,2]
cv1 = CompressedCellValue(values1,ptrs1)
cv2 = CompressedCellValue(values2,ptrs2)
cv3 = apply(-,cv1,cv2)
@test ! isa(cv3,IndexCompressedCellValue)
r = values1[ptrs1] - values2[ptrs2]
test_index_cell_value(cv3,r)

values1 = [10,20,31]
values2 = [11,21,51]
ptrs1 = [1,2,3,3,2,2]
ptrs2 = [1,1,3,2,3,2]
cv1 = IterCompressedCellValue(values1,ptrs1)
cv2 = IterCompressedCellValue(values2,ptrs2)
cv3 = apply(-,cv1,cv2)
@test ! isa(cv3,IndexCompressedCellValue)
r = values1[ptrs1] - values2[ptrs2]
test_iter_cell_value(cv3,r)

values = [[10,30],[20,10,40],[31,]]
ptrs = [1,2,3,3,2,2]
cv = CompressedCellValue(values,ptrs)

cv2 = apply(-,cv,broadcast=true)
@test isa(cv2,IndexCompressedCellValue)
r = -values[ptrs]
test_index_cell_array(cv2,r)

values1 = [[10,33],[20,12,40],[71,]]
values2 = [[10,30],[20,10,40],[31,]]
ptrs = [1,2,3,3,2,2]
cv1 = CompressedCellValue(values1,ptrs)
cv2 = CompressedCellValue(values2,ptrs)
cv3 = apply(-,cv1,cv2,broadcast=true)
@test isa(cv3,IndexCompressedCellValue)
r = values1[ptrs] - values2[ptrs]
test_index_cell_array(cv3,r)

values1 = [[10,33,2],[20,12,40],[71,1,4]]
values2 = [[10,30,3],[20,10,40],[31,3,5]]
ptrs1 = [1,3,2,3,2,2]
ptrs2 = [1,2,3,3,2,2]
cv1 = CompressedCellValue(values1,ptrs1)
cv2 = CompressedCellValue(values2,ptrs2)
cv3 = apply(-,cv1,cv2,broadcast=true)
r = values1[ptrs1] - values2[ptrs2]
@test ! isa(cv3,IndexCompressedCellValue)
test_index_cell_array(cv3,r)


a = VectorValue(10,10)
b = VectorValue(15,20)
p1 = VectorValue(1,1)
p2 = VectorValue(2,2)
p3 = VectorValue(3,3)
p = [p1,p2,p3]
m = MockMap(a)

l = 5
cm = ConstantCellMap(m,l)
vals = [[p1,p3], [p1,p3,p2], ]
ptrs = [1,2,1,1,2]
ca = CompressedCellValue(vals,ptrs)
cv = evaluate(cm,ca)
@test isa(cv,CompressedCellValue)
rvals = [ evaluate(m,pj) for pj in vals ]
cr = rvals[ptrs]
test_index_cell_map_with_index_arg(cm,ca,cr)

m1 = MockMap(a)
m2 = MockMap(b)
mvals = [m1,m2]
pvals = [[p1,p3], [p1,p3,p2], ]
ptrs = [1,2,1,1,2]
cm = CompressedCellMap(mvals,ptrs)
ca = CompressedCellArray(pvals,ptrs)
cv = evaluate(cm,ca)
@test isa(cv,CompressedCellValue)
rvals = [ evaluate(mi,ai) for (mi,ai) in zip(mvals,pvals) ]
cr = CompressedCellValue(rvals,ptrs)
test_index_cell_map_with_index_arg(cm,ca,cr)

values = [10,20,31]
ptrs = [1,2,3,3,2,2]
inds = [2,1,4]
cv = CompressedCellValue(values,ptrs)
indices = CellValueFromArray(inds)
cv2 = reindex(cv,indices)
@test isa(cv2,CompressedCellValue)
r = values[ptrs[inds]]
test_index_cell_value(cv2,r)

end # module
1 change: 1 addition & 0 deletions test/CellValuesTests/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ include("MapsMocks.jl")

@testset "ConstantCellValues" begin include("ConstantCellValuesTests.jl") end

@testset "CompressedCellValues" begin include("CompressedCellValuesTests.jl") end

end # module