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

Wrote additional high level API assembly functions #652

Merged
merged 6 commits into from
Sep 6, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- Inheritance relationship for DiscreteModelPortion. Since PR [#645](https://github.com/gridap/Gridap.jl/pull/645).

### Added
- Additional high level API assembly functions. Since PR [#652](https://github.com/gridap/Gridap.jl/pull/652).

## [0.16.4] - 2021-08-17

### Added
Expand Down
44 changes: 44 additions & 0 deletions src/FESpaces/Assemblers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -270,44 +270,88 @@ function assemble_matrix(f::Function,a::Assembler,U::FESpace,V::FESpace)
assemble_matrix(a,collect_cell_matrix(U,V,f(u,v)))
end

function assemble_matrix!(f::Function,A::AbstractMatrix,a::Assembler,U::FESpace,V::FESpace)
v = get_fe_basis(V)
u = get_trial_fe_basis(U)
assemble_matrix!(A,a,collect_cell_matrix(U,V,f(u,v)))
end

function assemble_vector(f::Function,a::Assembler,V::FESpace)
v = get_fe_basis(V)
assemble_vector(a,collect_cell_vector(V,f(v)))
end

function assemble_vector!(f::Function,b::AbstractVector,a::Assembler,V::FESpace)
v = get_fe_basis(V)
assemble_vector!(b,a,collect_cell_vector(V,f(v)))
end

function assemble_matrix_and_vector(f::Function,b::Function,a::Assembler,U::FESpace,V::FESpace)
v = get_fe_basis(V)
u = get_trial_fe_basis(U)
assemble_matrix_and_vector(a,collect_cell_matrix_and_vector(U,V,f(u,v),b(v)))
end

function assemble_matrix_and_vector!(f::Function,b::Function,M::AbstractMatrix,r::AbstractVector,a::Assembler,U::FESpace,V::FESpace)
v = get_fe_basis(V)
u = get_trial_fe_basis(U)
assemble_matrix_and_vector!(M,r,a,collect_cell_matrix_and_vector(U,V,f(u,v),b(v)))
end

function assemble_matrix(f,a::Assembler,U::FESpace,V::FESpace)
assemble_matrix(a,collect_cell_matrix(U,V,f))
end

function assemble_matrix!(A::AbstractMatrix,f,a::Assembler,U::FESpace,V::FESpace)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function argument first here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

f is not a function, isnt?

assemble_matrix!(A,a,collect_cell_matrix(U,V,f))
end

function assemble_vector(f,a::Assembler,V::FESpace)
assemble_vector(a,collect_cell_vector(V,f))
end

function assemble_vector!(b::AbstractVector,f,a::Assembler,V::FESpace)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

f is not a function, isnt?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

f is a callable object, usually a Function.

to be consistent we have 2 main options:

  • add missing ::Function everywhere in this file, where we accept a function
  • remove all ::Function anotations to allow generic callable objects

2 is more general, but 1 is less disruptive wrt what we have now and also works since the weak form is always defined via functions.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still dont understand why f it is function/callable object in this particular function. See, e.g.,

function collect_cell_vector(test::FESpace,a::DomainContribution)
  w = []
  r = []
  for trian in get_domains(a)
    cell_vec = get_contribution(a,trian)
    @assert ndims(eltype(cell_vec)) == 1
    cell_vec_r = attach_constraints_rows(test,cell_vec,trian)
    rows = get_cell_dof_ids(test,trian)
    push!(w,compress_contributions(cell_vec_r,trian))
    push!(r,compress_ids(rows,trian))
  end
  (w,r)
end

a is a callable object?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. It is not a function. it is an object for which collect_cell_vector and related functions are defined.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, in any case I had to put the f and b args first in all function signatures to let all tests pass ...

assemble_vector!(b,a,collect_cell_vector(V,f))
end

function assemble_matrix_and_vector(f,b,a::Assembler,U::FESpace,V::FESpace)
assemble_matrix_and_vector(a,collect_cell_matrix_and_vector(U,V,f,b))
end

function assemble_matrix_and_vector!(M,r,f,b,a::Assembler,U::FESpace,V::FESpace)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here (and type-annotate array inputs)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops ... I forgot this one

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

f is not a function, isnt?

assemble_matrix_and_vector!(M,r,a,collect_cell_matrix_and_vector(U,V,f,b))
end

function assemble_matrix(f,U::FESpace,V::FESpace)
a = SparseMatrixAssembler(U,V)
assemble_matrix(f,a,U,V)
end

function assemble_matrix!(A,f,U::FESpace,V::FESpace)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here

a = SparseMatrixAssembler(U,V)
assemble_matrix!(A,f,a,U,V)
end

function assemble_vector(f,V::FESpace)
a = SparseMatrixAssembler(V,V)
assemble_vector(f,a,V)
end

function assemble_vector!(b,f,V::FESpace)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here

a = SparseMatrixAssembler(V,V)
assemble_vector!(b,f,a,V)
end

function assemble_matrix_and_vector(f,b,U::FESpace,V::FESpace)
a = SparseMatrixAssembler(U,V)
assemble_matrix_and_vector(f,b,a,U,V)
end

function assemble_matrix_and_vector!(M,r,f,b,U::FESpace,V::FESpace)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here

a = SparseMatrixAssembler(U,V)
assemble_matrix_and_vector!(M,r,f,b,a,U,V)
end

# Abstract interface for computing the data to be sent to the assembler

function collect_cell_matrix(trial::FESpace,test::FESpace,mat_contributions)
Expand Down
41 changes: 34 additions & 7 deletions test/FESpacesTests/AssemblersTests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,40 @@ A,b = assemble_matrix_and_vector(assem,data)
x = A\b
uh = FEFunction(U,x)

A = assemble_matrix(a,U,V)
b = assemble_vector(ℓ,V)
A,b = assemble_matrix_and_vector(a,ℓ,U,V)

A = assemble_matrix(a(du,dv),U,V)
b = assemble_vector(ℓ(dv),V)
A,b = assemble_matrix_and_vector(a(du,dv),ℓ(dv),U,V)
A1 = assemble_matrix(a,U,V)
b1 = assemble_vector(ℓ,V)
A2,b2 = assemble_matrix_and_vector(a,ℓ,U,V)

A12 = copy(A1); A12[1,1]=rand()
b12 = copy(b1); b12[1]=rand()
A22 = copy(A2); A22[1,1]=rand()
b22 = copy(b2); b22[1]=rand()

tol = 1.e-14
assemble_matrix!(a,A12,U,V)
assemble_vector!(ℓ,b12,V)
assemble_matrix_and_vector!(a,ℓ,A22,b22,U,V)
@test norm(A12-A1) < tol
@test norm(b12-b1) < tol
@test norm(A22-A2) < tol
@test norm(b22-b2) < tol

A1 = assemble_matrix(a(du,dv),U,V)
b1 = assemble_vector(ℓ(dv),V)
A2,b2 = assemble_matrix_and_vector(a(du,dv),ℓ(dv),U,V)

A12 = copy(A1); A12[1,1]=rand()
b12 = copy(b1); b12[1]=rand()
A22 = copy(A2); A22[1,1]=rand()
b22 = copy(b2); b22[1]=rand()

assemble_matrix!(A12,a(du,dv),U,V)
assemble_vector!(b12,ℓ(dv),V)
assemble_matrix_and_vector!(A22,b22,a(du,dv),ℓ(dv),U,V)
@test norm(A12-A1) < tol
@test norm(b12-b1) < tol
@test norm(A22-A2) < tol
@test norm(b22-b2) < tol

V = TestFESpace(
model,
Expand Down