Skip to content

Commit cdad2fb

Browse files
committed
CoupledDofsRestriction now handles multiple matrices correctly
1 parent a90a4f2 commit cdad2fb

File tree

4 files changed

+38
-14
lines changed

4 files changed

+38
-14
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# CHANGES
22

3+
## v1.6.0
4+
5+
### Added
6+
7+
- `CoupledDofsRestriction` accepts now an array of multiple coupling matrices.
8+
9+
### Fixed
10+
11+
- Periodic coupling with multiple redundant coupling matrices are now reduced to full rank to make the system uniquely solvable.
12+
313
## v1.5.0
414

515
### Added

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ExtendableFEM"
22
uuid = "a722555e-65e0-4074-a036-ca7ce79a4aed"
3-
version = "1.5.0"
3+
version = "1.6.0"
44
authors = ["Christian Merdon <merdon@wias-berlin.de>", "Patrick Jaap <patrick.jaap@wias-berlin.de>"]
55

66
[deps]

src/ExtendableFEM.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ using LinearSolve: LinearSolve, LinearProblem, UMFPACKFactorization, deleteat!,
7070
init, solve
7171
using Printf: Printf, @printf, @sprintf
7272
using SparseArrays: SparseArrays, AbstractSparseArray, SparseMatrixCSC, findnz, nnz,
73-
nzrange, rowvals, sparse, SparseVector, spzeros
73+
nzrange, rowvals, sparse, SparseVector, spzeros, qr, rank
7474
using StaticArrays: @MArray
7575
using SparseDiffTools: SparseDiffTools, ForwardColorJacCache,
7676
forwarddiff_color_jacobian!, matrix_colors
Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
struct CoupledDofsRestriction{TM} <: AbstractRestriction
2-
coupling_matrix::TM
2+
coupling_matrices::Vector{TM}
33
parameters::Dict{Symbol, Any}
44
end
55

66

77
"""
88
CoupledDofsRestriction(matrix::AbstractMatrix)
99
10-
Creates an restriction that couples dofs together.
10+
Creates a restriction that couples dofs together.
1111
1212
The coupling is stored in a CSC Matrix `matrix`, s.t.,
1313
@@ -16,27 +16,41 @@ end
1616
The matrix can be obtained from, e.g., `get_periodic_coupling_matrix`.
1717
"""
1818
function CoupledDofsRestriction(matrix::AbstractMatrix)
19-
return CoupledDofsRestriction(matrix, Dict{Symbol, Any}(:name => "CoupledDofsRestriction"))
19+
return CoupledDofsRestriction([matrix], Dict{Symbol, Any}(:name => "CoupledDofsRestriction"))
2020
end
2121

2222

23-
function assemble!(R::CoupledDofsRestriction, sol, SC; kwargs...)
23+
"""
24+
CoupledDofsRestriction(matrices::Vector{AM}) where {AM <: AbstractMatrix}
2425
25-
# extract all col indices
26-
_, J, _ = findnz(R.coupling_matrix)
26+
Creates a `CoupledDofsRestriction` from multiple given coupling matrices.
27+
"""
28+
function CoupledDofsRestriction(matrices::Vector{AM}) where {AM <: AbstractMatrix}
29+
return CoupledDofsRestriction(matrices, Dict{Symbol, Any}(:name => "CoupledDofsRestriction"))
30+
end
2731

28-
# remove duplicates
29-
unique_cols = unique(J)
3032

33+
function assemble!(R::CoupledDofsRestriction, sol, SC; kwargs...)
34+
35+
# extract all col indices and remove duplicates
3136
# subtract diagonal and shrink matrix to non-empty cols
32-
B = (R.coupling_matrix - LinearAlgebra.I)[:, unique_cols]
37+
Bs = [ (matrix - LinearAlgebra.I)[:, unique(findnz(matrix)[2])] for matrix in R.coupling_matrices ]
38+
39+
# combine all into one matrix
40+
B = hcat(Bs...)
41+
42+
# eliminate redundant cols by QR:
43+
qr_result = qr(B)
44+
45+
# pick minimal number of cols that are rank preserving
46+
cols_of_interest = qr_result.pcol[1:rank(qr_result)]
47+
B = B[:, cols_of_interest]
3348

3449
R.parameters[:matrix] = B
35-
R.parameters[:rhs] = Zeros(length(unique_cols))
50+
R.parameters[:rhs] = Zeros(size(B, 2))
3651

3752
# fixed dofs are all active rows of B
38-
I, _, _ = findnz(B)
39-
R.parameters[:fixed_dofs] = unique(I)
53+
R.parameters[:fixed_dofs] = unique(findnz(B)[1])
4054

4155
return nothing
4256
end

0 commit comments

Comments
 (0)