Skip to content

Commit

Permalink
Simplified Assembler interface
Browse files Browse the repository at this point in the history
  • Loading branch information
fverdugo committed May 7, 2020
1 parent 9fcaca4 commit 5f4f63b
Show file tree
Hide file tree
Showing 12 changed files with 296 additions and 256 deletions.
8 changes: 4 additions & 4 deletions src/FESpaces/AffineFEOperators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ function AffineFEOperator(trial::FESpace,test::FESpace,assem::Assembler,terms::A

uhd = zero(trial)

matvecdata, matdata, vecdata = collect_cell_matrix_and_vector(uhd,u,v,terms)
A,b = assemble_matrix_and_vector(assem,matvecdata,matdata,vecdata)
data = collect_cell_matrix_and_vector(uhd,u,v,terms)
A,b = assemble_matrix_and_vector(assem,data)

#matdata = collect_cell_matrix(u,v,terms)
#vecdata = collect_cell_vector(uhd,v,terms)
#A = assemble_matrix(assem,matdata...)
#b = assemble_vector(assem,vecdata...)
#A = assemble_matrix(assem,matdata)
#b = assemble_vector(assem,vecdata)

AffineFEOperator(trial,test,A,b)
end
Expand Down
209 changes: 88 additions & 121 deletions src/FESpaces/Assemblers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,158 +61,115 @@ end

"""
"""
function allocate_matrix(a::Assembler,cellidsrows,cellidscols)
function allocate_matrix(a::Assembler,matdata)
@abstractmethod
end

function allocate_matrix(a::Assembler,cellmat,cellidsrows,cellidscols)
allocate_matrix(a,cellidsrows,cellidscols)
end

"""
"""
function allocate_vector(a::Assembler,cellidsrows)
function allocate_vector(a::Assembler,vecdata)
@abstractmethod
end

function allocate_vector(a::Assembler,cellvec,cellidsrows)
allocate_vector(a,cellidsrows)
end

"""
"""
function allocate_matrix_and_vector(a::Assembler,matvecdata,matdata,vecdata)
matrows, matcols, vecrows = _rearange_cell_ids(matvecdata,matdata,vecdata)
A = allocate_matrix(a,matrows,matcols)
b = allocate_vector(a,vecrows)
(A,b)
end

function allocate_matrix_and_vector(a::Assembler,matvecdata)
matdata = ([],[],[])
vecdata = ([],[])
allocate_matrix_and_vector(a,matvecdata,matdata,vecdata)
function allocate_matrix_and_vector(a::Assembler,data)
@abstractmethod
end

"""
"""
function assemble_matrix!(A,a::Assembler,cellmat,cellidsrows,cellidscols)
function assemble_matrix!(A,a::Assembler,matdata)
@abstractmethod
end

"""
"""
function assemble_matrix_add!(
mat,a::Assembler, term_to_cellmat, term_to_cellidsrows, term_to_cellidscols)
function assemble_matrix_add!(mat,a::Assembler, matdata)
@abstractmethod
end

"""
"""
function assemble_vector!(b,a::Assembler,cellvec,cellids)
function assemble_vector!(b,a::Assembler,vecdata)
@abstractmethod
end

"""
"""
function assemble_vector_add!(b,a::Assembler,term_to_cellvec,term_to_cellidsrows)
function assemble_vector_add!(b,a::Assembler,vecdata)
@abstractmethod
end

"""
"""
function assemble_matrix_and_vector!(A,b,a::Assembler,matvecdata,matdata,vecdata)
function assemble_matrix_and_vector!(A,b,a::Assembler, data)
@abstractmethod
end

function assemble_matrix_and_vector_add!(A,b,a::Assembler, matvecdata, matdata, vecdata)
function assemble_matrix_and_vector_add!(A,b,a::Assembler, data)
@abstractmethod
end

function assemble_matrix_and_vector!(A,b,a::Assembler,matvecdata)
matdata = ([],[],[])
vecdata = ([],[])
assemble_matrix_and_vector!(A,b,a,matvecdata,matdata,vecdata)
end

"""
"""
function assemble_matrix(a::Assembler,cellmat,cellidsrows,cellidscols)
A = allocate_matrix(a,cellidsrows,cellidscols)
assemble_matrix!(A,a,cellmat,cellidsrows,cellidscols)
function assemble_matrix(a::Assembler,matdata)
A = allocate_matrix(a,matdata)
assemble_matrix!(A,a,matdata)
A
end

"""
"""
function assemble_vector(a::Assembler,cellvec,cellids)
b = allocate_vector(a,cellids)
assemble_vector!(b,a,cellvec,cellids)
function assemble_vector(a::Assembler,vecdata)
b = allocate_vector(a,vecdata)
assemble_vector!(b,a,vecdata)
b
end

"""
"""
function assemble_matrix_and_vector(a::Assembler,matvecdata,matdata,vecdata)
A, b = allocate_matrix_and_vector(a,matvecdata,matdata,vecdata)
assemble_matrix_and_vector!(A,b,a,matvecdata,matdata,vecdata)
function assemble_matrix_and_vector(a::Assembler,data)
A, b = allocate_matrix_and_vector(a,data)
assemble_matrix_and_vector!(A,b,a,data)
(A, b)
end

function assemble_matrix_and_vector(a::Assembler,matvecdata)
matdata = ([],[],[])
vecdata = ([],[])
assemble_matrix_and_vector(a,matvecdata,matdata,vecdata)
end

function _rearange_cell_ids(matvecdata,matdata,vecdata)

matvec1, rows1, cols1 = matvecdata
mat2, rows2, cols2 = matdata
vec3, rows3 = vecdata

matrows = vcat(rows1,rows2)
matcols = vcat(cols1,cols2)
vecrows = vcat(rows1,rows3)

(matrows, matcols, vecrows)
end

"""
"""
function test_assembler(a::Assembler,matvecdata,matdata,vecdata)
function test_assembler(a::Assembler,matdata,vecdata,data)
trial_fesp = get_trial(a)
test_fesp = get_test(a)
A = allocate_matrix(a,matdata...)
A = allocate_matrix(a,matdata)
@test num_free_dofs(trial_fesp) == size(A,2)
@test num_free_dofs(test_fesp) == size(A,1)
assemble_matrix!(A,a,matdata...)
assemble_matrix_add!(A,a,matdata...)
A = assemble_matrix(a,matdata...)
assemble_matrix!(A,a,matdata)
assemble_matrix_add!(A,a,matdata)
A = assemble_matrix(a,matdata)
@test num_free_dofs(trial_fesp) == size(A,2)
@test num_free_dofs(test_fesp) == size(A,1)
b = allocate_vector(a,vecdata...)
b = allocate_vector(a,vecdata)
@test num_free_dofs(test_fesp) == length(b)
assemble_vector!(b,a,vecdata...)
assemble_vector_add!(b,a,vecdata...)
b = assemble_vector(a,vecdata...)
assemble_vector!(b,a,vecdata)
assemble_vector_add!(b,a,vecdata)
b = assemble_vector(a,vecdata)
@test num_free_dofs(test_fesp) == length(b)
A, b = allocate_matrix_and_vector(a,matvecdata,matdata,vecdata)
assemble_matrix_and_vector!(A,b,a,matvecdata,matdata,vecdata)
assemble_matrix_and_vector_add!(A,b,a,matvecdata,matdata,vecdata)
A, b = allocate_matrix_and_vector(a,data)
assemble_matrix_and_vector!(A,b,a,data)
assemble_matrix_and_vector_add!(A,b,a,data)
@test num_free_dofs(trial_fesp) == size(A,2)
@test num_free_dofs(test_fesp) == size(A,1)
@test num_free_dofs(test_fesp) == length(b)
A, b = assemble_matrix_and_vector(a,matvecdata,matdata,vecdata)
A, b = assemble_matrix_and_vector(a,data)
@test num_free_dofs(trial_fesp) == size(A,2)
@test num_free_dofs(test_fesp) == size(A,1)
@test num_free_dofs(test_fesp) == length(b)
strategy = get_assembly_strategy(a)
@test isa(strategy,AssemblyStrategy)
end

# This is an extended interface that only make sense for assemblers that build sparse matrices
# (e.g. not for matrix free assemblers)
# This is an extended interface that only make sense for assemblers that build (sequential) sparse matrices
# (e.g. not for matrix free assemblers or for distributed assemblers)

"""
"""
Expand All @@ -230,112 +187,122 @@ function get_vector_type(a::SparseMatrixAssembler)
@abstractmethod
end

function allocate_vector(a::SparseMatrixAssembler,term_to_cellidsrows)
n = num_free_dofs(a.test)
function allocate_vector(a::SparseMatrixAssembler,vecdata)
n = num_free_dofs(get_test(a))
allocate_vector(get_vector_type(a),n)
end

function assemble_vector!(b,a::SparseMatrixAssembler,term_to_cellvec,term_to_cellidsrows)
function assemble_vector!(b,a::SparseMatrixAssembler,vecdata)
fill_entries!(b,zero(eltype(b)))
assemble_vector_add!(b,a,term_to_cellvec,term_to_cellidsrows)
assemble_vector_add!(b,a,vecdata)
end

"""
"""
function count_matrix_nnz_coo(a::SparseMatrixAssembler,term_to_cellidsrows, term_to_cellidscols)
function count_matrix_nnz_coo(a::SparseMatrixAssembler,vecdata)
@abstractmethod
end

function count_matrix_nnz_coo(a::SparseMatrixAssembler,term_to_cellmat,term_to_cellidsrows, term_to_cellidscols)
count_matrix_nnz_coo(a,term_to_cellidsrows, term_to_cellidscols)
"""
"""
function count_matrix_and_vector_nnz_coo(a::SparseMatrixAssembler,data)
@abstractmethod
end

"""
"""
function fill_matrix_coo_symbolic!(I,J,a::SparseMatrixAssembler,term_to_cellidsrows, term_to_cellidscols)
function fill_matrix_coo_symbolic!(I,J,a::SparseMatrixAssembler,matdata,n=0)
@abstractmethod
end

function fill_matrix_coo_symbolic!(I,J,a::SparseMatrixAssembler,term_to_cellmat,term_to_cellidsrows, term_to_cellidscols)
fill_matrix_coo_symbolic!(I,J,a,term_to_cellidsrows, term_to_cellidscols)
function fill_matrix_and_vector_coo_symbolic!(I,J,a::SparseMatrixAssembler,data,n=0)
@abstractmethod
end

function allocate_matrix(a::SparseMatrixAssembler, term_to_cellidsrows, term_to_cellidscols)
n = count_matrix_nnz_coo(a,term_to_cellidsrows,term_to_cellidscols)
function allocate_matrix(a::SparseMatrixAssembler,matdata)
n = count_matrix_nnz_coo(a,matdata)
I,J,V = allocate_coo_vectors(get_matrix_type(a),n)
fill_matrix_coo_symbolic!(I,J,a,term_to_cellidsrows,term_to_cellidscols)
m = num_free_dofs(a.test)
n = num_free_dofs(a.trial)
fill_matrix_coo_symbolic!(I,J,a,matdata)
m = num_free_dofs(get_test(a))
n = num_free_dofs(get_trial(a))
finalize_coo!(get_matrix_type(a),I,J,V,m,n)
sparse_from_coo(get_matrix_type(a),I,J,V,m,n)
end

function assemble_matrix!(
mat,a::SparseMatrixAssembler, term_to_cellmat, term_to_cellidsrows, term_to_cellidscols)
function assemble_matrix!(mat,a::SparseMatrixAssembler,matdata)
z = zero(eltype(mat))
fill_entries!(mat,z)
assemble_matrix_add!(mat,a,term_to_cellmat,term_to_cellidsrows,term_to_cellidscols)
assemble_matrix_add!(mat,a,matdata)
end

"""
"""
function fill_matrix_coo_numeric!(
I,J,V,a::SparseMatrixAssembler,term_to_cellmat,term_to_cellidsrows, term_to_cellidscols,n=0)
function fill_matrix_coo_numeric!(I,J,V,a::SparseMatrixAssembler,matdata,n=0)
@abstractmethod
end

function assemble_matrix(
a::SparseMatrixAssembler, term_to_cellmat, term_to_cellidsrows, term_to_cellidscols)
function assemble_matrix(a::SparseMatrixAssembler,matdata)

n = count_matrix_nnz_coo(a,term_to_cellidsrows,term_to_cellidscols)
n = count_matrix_nnz_coo(a,matdata)
I,J,V = allocate_coo_vectors(get_matrix_type(a),n)

nmax = fill_matrix_coo_numeric!(I,J,V,a,term_to_cellmat,term_to_cellidsrows,term_to_cellidscols)
resize!(I,nmax)
resize!(J,nmax)
resize!(V,nmax)
fill_matrix_coo_numeric!(I,J,V,a,matdata)

m = num_free_dofs(a.test)
n = num_free_dofs(a.trial)
m = num_free_dofs(get_test(a))
n = num_free_dofs(get_trial(a))
finalize_coo!(get_matrix_type(a),I,J,V,m,n)
sparse_from_coo(get_matrix_type(a),I,J,V,m,n)
end

function assemble_matrix_and_vector!(A,b,a::SparseMatrixAssembler, matvecdata, matdata, vecdata)

function allocate_matrix_and_vector(a::SparseMatrixAssembler,data)

n = count_matrix_and_vector_nnz_coo(a,data)

I,J,V = allocate_coo_vectors(get_matrix_type(a),n)
fill_matrix_and_vector_coo_symbolic!(I,J,a,data)
m = num_free_dofs(get_test(a))
n = num_free_dofs(get_trial(a))
finalize_coo!(get_matrix_type(a),I,J,V,m,n)
A = sparse_from_coo(get_matrix_type(a),I,J,V,m,n)

b = allocate_vector(get_vector_type(a),m)

A,b
end

function assemble_matrix_and_vector!(A,b,a::SparseMatrixAssembler, data)
fill_entries!(A,zero(eltype(A)))
fill_entries!(b,zero(eltype(b)))
assemble_matrix_and_vector_add!(A,b,a,matvecdata, matdata, vecdata)
assemble_matrix_and_vector_add!(A,b,a,data)
A, b
end

"""
"""
function fill_matrix_and_vector_coo_numeric!(I,J,V,b,a::SparseMatrixAssembler,matvecdata, matdata, vecdata,n=0)
function fill_matrix_and_vector_coo_numeric!(I,J,V,b,a::SparseMatrixAssembler,data,n=0)
@abstractmethod
end

function assemble_matrix_and_vector(a::SparseMatrixAssembler, matvecdata, matdata, vecdata)
function assemble_matrix_and_vector(a::SparseMatrixAssembler, data)

term_to_cellidsrows, term_to_cellidscols, = _rearange_cell_ids(matvecdata,matdata,vecdata)
n = count_matrix_nnz_coo(a,term_to_cellidsrows,term_to_cellidscols)
n = count_matrix_and_vector_nnz_coo(a,data)
I,J,V = allocate_coo_vectors(get_matrix_type(a),n)
b = allocate_vector(a,vecdata...)
n = num_free_dofs(get_test(a))
b = allocate_vector(get_vector_type(a),n)

nmax = fill_matrix_and_vector_coo_numeric!(I,J,V,b,a, matvecdata, matdata, vecdata)
resize!(I,nmax)
resize!(J,nmax)
resize!(V,nmax)
fill_matrix_and_vector_coo_numeric!(I,J,V,b,a,data)

m = num_free_dofs(a.test)
n = num_free_dofs(a.trial)
m = num_free_dofs(get_test(a))
n = num_free_dofs(get_trial(a))
finalize_coo!(get_matrix_type(a),I,J,V,m,n)
A = sparse_from_coo(get_matrix_type(a),I,J,V,m,n)

A, b
end

function test_sparse_matrix_assembler(a::SparseMatrixAssembler,matvecdata,matdata,vecdata)
test_assembler(a,matvecdata,matdata,vecdata)
function test_sparse_matrix_assembler(a::SparseMatrixAssembler,matdata,vecdata,data)
test_assembler(a,matdata,vecdata,data)
_ = get_matrix_type(a)
_ = get_vector_type(a)
end
Expand Down
Loading

0 comments on commit 5f4f63b

Please sign in to comment.