Skip to content

Commit 9f6286e

Browse files
committed
several improvements:
- bump pre-commit - better show of FESpace, FEVector and FEMatrix - coffset function - first, last functions for blocks - submatrix function for FEMatrixBlock
1 parent 4828579 commit 9f6286e

4 files changed

Lines changed: 75 additions & 51 deletions

File tree

src/ExtendableFEMBase.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ export interpolator_matrix
117117
export FEVectorBlock, FEVector
118118
export dot, norm, norms
119119
export FEMatrixBlock, FEMatrix, _addnz
120-
export fill!, addblock!, addblock_matmul!, lrmatmul, mul!, add!, apply_penalties!
120+
export fill!, addblock!, addblock_matmul!, lrmatmul, mul!, add!, apply_penalties!, coffset
121121
export submatrix
122122

123123
export displace_mesh, displace_mesh!

src/fematrix.jl

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,14 @@ function Base.copy(FEMB::FEMatrixBlock{TvM, TiM, TvG, TiG, FETypeX, FETypeY, APT
2626
return FEMatrixBlock{TvM, TiM, TvG, TiG, FETypeX, FETypeY, APTX, APTY}(deepcopy(FEMB.name), copy(FEMB.FES), copy(FEMB.FESY), FEMB.offset, FEMB.offsetY, FEMB.last_index, FEMB.last_indexY, entries)
2727
end
2828

29-
function Base.show(io::IO, FEB::FEMatrixBlock; tol = 1.0e-14)
30-
@printf(io, "\n")
31-
for j in 1:(FEB.offset + 1):FEB.last_index
32-
for k in 1:(FEB.offsetY + 1):FEB.last_indexY
33-
if FEB.entries[j, k] > tol
34-
@printf(io, " +%.1e", FEB.entries[j, k])
35-
elseif FEB.entries[j, k] < -tol
36-
@printf(io, " %.1e", FEB.entries[j, k])
37-
else
38-
@printf(io, " ********")
39-
end
40-
end
41-
@printf(io, "\n")
42-
end
43-
return
29+
"""
30+
$(TYPEDSIGNATURES)
31+
32+
Custom `show` function for `FEMatrixBlock` that prints its coordinates and the name.
33+
"""
34+
function Base.show(io::IO, FEB::FEMatrixBlock)
35+
@printf(io, "[%d:%d,%d:%d]: %s", FEB.offset+1, FEB.last_index, FEB.offsetY+1, FEB.last_indexY, FEB.name)
36+
return nothing
4437
end
4538

4639

@@ -100,6 +93,9 @@ Base.getindex(FEF::FEMatrix{TvM, TiM, TvG, TiG, nbrow, nbcol, nbtotal}, i::Int,
10093
Base.getindex(FEB::FEMatrixBlock, i::Int, j::Int) = FEB.entries[FEB.offset + i, FEB.offsetY + j]
10194
Base.getindex(FEB::FEMatrixBlock, i::Any, j::Any) = FEB.entries[FEB.offset .+ i, FEB.offsetY .+ j]
10295
Base.setindex!(FEB::FEMatrixBlock, v, i::Int, j::Int) = setindex!(FEB.entries, v, FEB.offset + i, FEB.offsetY + j)
96+
Base.first(FEB::FEMatrixBlock) = (FEB.offset+1, FEB.offsetY+1)
97+
Base.last(FEB::FEMatrixBlock) = (FEB.last_index, FEB.last_indexY)
98+
10399

104100

105101
"""
@@ -147,14 +143,15 @@ Custom `show` function for `FEMatrix` that prints some information on its blocks
147143
function Base.show(io::IO, FEM::FEMatrix{TvM, TiM, TvG, TiG, nbrow, nbcol, nbtotal}) where {TvM, TiM, TvG, TiG, nbrow, nbcol, nbtotal}
148144
println(io, "\nFEMatrix information")
149145
println(io, "====================")
150-
println(io, " block | ndofsX | ndofsY | name (FETypeX, FETypeY) ")
146+
println(io, " block | starts | ends | size | name ")
151147
for j in 1:length(FEM)
152148
n = mod(j - 1, nbrow) + 1
153149
m = Int(ceil(j / nbrow))
154-
@printf(io, " [%d,%d] |", m, n)
155-
@printf(io, " %6d |", FEM[j].FES.ndofs)
156-
@printf(io, " %6d |", FEM[j].FESY.ndofs)
157-
@printf(io, " %s (%s, %s)\n", FEM[j].name, FEM[j].FES.name, FEM[j].FESY.name)
150+
@printf(io, " [%2d,%2d] |", m, n)
151+
@printf(io, " (%6d,%6d) |", FEM[j].offset+1, FEM[j].offsetY+1)
152+
@printf(io, " (%6d,%6d) |", FEM[j].last_index, FEM[j].last_indexY)
153+
@printf(io, " (%6d,%6d) |", FEM[j].FES.ndofs, FEM[j].FESY.ndofs)
154+
@printf(io, " %s \n", FEM[j].name)
158155
end
159156
return println(io, "\n nnzvals = $(length(FEM.entries.cscmatrix.nzval))")
160157
end
@@ -166,19 +163,19 @@ FEMatrix{TvM,TiM}(name::String, FES::FESpace{TvG,TiG,FETypeX,APTX}) where {TvG,T
166163
167164
Creates FEMatrix with one square block (FES,FES).
168165
"""
169-
function FEMatrix(FES::FESpace; name = "auto")
166+
function FEMatrix(FES::FESpace; name = :automatic)
170167
return FEMatrix{Float64, Int64}(FES; name = name)
171168
end
172-
function FEMatrix{TvM}(FES::FESpace; name = "auto") where {TvM}
169+
function FEMatrix{TvM}(FES::FESpace; name = :automatic) where {TvM}
173170
return FEMatrix{TvM, Int64}(FES; name = name)
174171
end
175-
function FEMatrix{TvM, TiM}(FES::FESpace{TvG, TiG, FETypeX, APTX}; name = "auto") where {TvM, TiM, TvG, TiG, FETypeX, APTX}
172+
function FEMatrix{TvM, TiM}(FES::FESpace{TvG, TiG, FETypeX, APTX}; name = :automatic) where {TvM, TiM, TvG, TiG, FETypeX, APTX}
176173
return FEMatrix{TvM, TiM}([FES], [FES]; name = name)
177174
end
178175

179176
"""
180177
````
181-
FEMatrix{TvM,TiM}(FESX, FESY; name = "auto")
178+
FEMatrix{TvM,TiM}(FESX, FESY; name = :automatic)
182179
````
183180
184181
Creates FEMatrix with one rectangular block (FESX,FESY) if FESX and FESY are single FESpaces, or
@@ -209,12 +206,12 @@ end
209206

210207
"""
211208
````
212-
FEMatrix{TvM,TiM}(FESX, FESY; name = "auto")
209+
FEMatrix{TvM,TiM}(FESX, FESY; name = :automatic)
213210
````
214211
215212
Creates an FEMatrix with blocks coressponding to the ndofs of FESX (rows) and FESY (columns).
216213
"""
217-
function FEMatrix{TvM, TiM}(FESX::Array{<:FESpace{TvG, TiG}, 1}, FESY::Array{<:FESpace{TvG, TiG}, 1}; entries = nothing, name = nothing, tags = nothing, tagsX = tags, tagsY = tagsX, npartitions = 1, kwargs...) where {TvM, TiM, TvG, TiG}
214+
function FEMatrix{TvM, TiM}(FESX::Array{<:FESpace{TvG, TiG}, 1}, FESY::Array{<:FESpace{TvG, TiG}, 1}; entries = nothing, name = :automatic, tags = nothing, tagsX = tags, tagsY = tagsX, npartitions = 1, kwargs...) where {TvM, TiM, TvG, TiG}
218215
ndofsX, ndofsY = 0, 0
219216
for j in 1:length(FESX)
220217
ndofsX += FESX[j].ndofs
@@ -232,7 +229,7 @@ function FEMatrix{TvM, TiM}(FESX::Array{<:FESpace{TvG, TiG}, 1}, FESY::Array{<:F
232229
@assert size(entries) == (ndofsX, ndofsY) "size of given entries not matching number of dofs in given FE space(s)"
233230
end
234231

235-
if name === nothing
232+
if name === :automatic || name === nothing
236233
name = ""
237234
end
238235

@@ -250,9 +247,9 @@ function FEMatrix{TvM, TiM}(FESX::Array{<:FESpace{TvG, TiG}, 1}, FESY::Array{<:F
250247
offsetY = 0
251248
for k in 1:length(FESY)
252249
if (tagsX !== nothing) && (tagsY !== nothing)
253-
blockname = name * " [$(tagsX[j]),$(tagsY[k])"
250+
blockname = name * " $(tagsX[j])[$(FESX[j].name)] x $(tagsY[k])[$(FESY[k].name)]"
254251
else
255-
blockname = name * " [$j,$k]"
252+
blockname = name * " $(FESX[j].name) x $(FESY[k].name)"
256253
end
257254
Blocks[(j - 1) * length(FESY) + k] =
258255
FEMatrixBlock{TvM, TiM, TvG, TiG, eltype(FESX[j]), eltype(FESY[k]), assemblytype(FESX[j]), assemblytype(FESY[k])}(blockname, FESX[j], FESY[k], offset, offsetY, offset + FESX[j].ndofs, offsetY + FESY[k].ndofs, entries)
@@ -641,3 +638,12 @@ function submatrix(A::AbstractExtendableSparseMatrixCSC{Tv, Ti}, srows, scols) w
641638
flush!(S)
642639
return S
643640
end
641+
642+
"""
643+
$(TYPEDSIGNATURES)
644+
645+
Returns the FEMatrixBlock as an ExtendableSparseMatrix
646+
"""
647+
function submatrix(A::FEMatrixBlock{Tv, Ti}) where {Tv, Ti}
648+
return submatrix(A.entries, A.offset+1:A.last_index, A.offsetY+1:A.last_indexY)
649+
end

src/fevector.jl

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,21 @@ function Base.copy(FEB::FEVectorBlock{T, Tv, Ti, FEType, APT}, entries) where {T
2222
return FEVectorBlock{T, Tv, Ti, FEType, APT}(deepcopy(FEB.name), copy(FEB.FES), FEB.offset, FEB.last_index, entries)
2323
end
2424

25+
"""
26+
$(TYPEDSIGNATURES)
27+
28+
Returns the number of components for the finite element in that block.
29+
"""
2530
get_ncomponents(FB::FEVectorBlock) = get_ncomponents(get_FEType(FB.FES))
2631

27-
function Base.show(io::IO, FEB::FEVectorBlock; tol = 1.0e-14)
28-
return @printf(io, "\n")
29-
#for j=1:FEB.offset+1:FEB.last_index
30-
# if FEB.entries[j] > tol
31-
# @printf(io, "[%d] = +%.3e", j, FEB.entries[j]);
32-
# elseif FEB.entries[j] < -tol
33-
# @printf(io, "[%d] %.3e", j, FEB.entries[j]);
34-
# else
35-
# @printf(io, "[%d] ********", j );
36-
# end
37-
# @printf(io, "\n");
38-
#end
32+
"""
33+
$(TYPEDSIGNATURES)
34+
35+
Custom `show` function for `FEVectorBlock` that prints some information and the view of that block.
36+
"""
37+
function Base.show(io::IO, ::MIME"text/plain", FEB::FEVectorBlock)
38+
@printf(io, "block %s [%d:%d] = ", FEB.name, FEB.offset+1, FEB.last_index)
39+
show(io, view(FEB))
3940
end
4041

4142
"""
@@ -57,6 +58,8 @@ function Base.copy(FEV::FEVector{T, Tv, Ti}) where {T, Tv, Ti}
5758
end
5859

5960
# overload stuff for AbstractArray{T,1} behaviour
61+
Base.IndexStyle(::Type{<:FEVector}) = LinearIndices
62+
Base.IndexStyle(::Type{<:FEVectorBlock}) = LinearIndices
6063
Base.getindex(FEF::FEVector{T, Tv, Ti}, tag) where {T, Tv, Ti} = FEF.FEVectorBlocks[findfirst(==(tag), FEF.tags)]
6164
Base.getindex(FEF::FEVector, i::Int) = FEF.FEVectorBlocks[i]
6265
Base.getindex(FEB::FEVectorBlock, i::Int) = FEB.entries[FEB.offset + i]
@@ -68,12 +71,14 @@ Base.setindex!(FEB::FEVectorBlock, v, i::AbstractArray) = (FEB.entries[FEB.offse
6871
Base.eltype(::FEVector{T}) where {T} = T
6972
Base.size(FEF::FEVector) = size(FEF.FEVectorBlocks)
7073
Base.size(FEB::FEVectorBlock) = FEB.last_index - FEB.offset
74+
Base.first(FEB::FEVectorBlock) = FEB.offset+1
75+
Base.last(FEB::FEVectorBlock) = FEB.last_index
7176

7277

7378
"""
7479
$(TYPEDEF)
7580
76-
returns a view of the part of the full FEVector that coressponds to the block.
81+
Returns a view of the part of the full FEVector that coressponds to the block.
7782
"""
7883
Base.view(FEB::FEVectorBlock) = view(FEB.entries, (FEB.offset + 1):FEB.last_index)
7984

@@ -88,7 +93,7 @@ end
8893
"""
8994
$(TYPEDEF)
9095
91-
returns a vector with the individual norms of all blocks
96+
Returns a vector with the individual norms of all blocks.
9297
"""
9398
function norms(FEV::FEVector{T}, p::Real = 2) where {T}
9499
norms = zeros(T, length(FEV))
@@ -191,10 +196,12 @@ Custom `show` function for `FEVector` that prints some information on its blocks
191196
function Base.show(io::IO, FEF::FEVector)
192197
println(io, "\nFEVector information")
193198
println(io, "====================")
194-
print(io, " block | ndofs \t| min / max \t| FEType \t\t (name/tag)")
199+
print(io, " block | starts | ends | length | min / max \t| FEType \t\t (name/tag)")
195200
for j in 1:length(FEF)
196-
@printf(io, "\n [%5d] | ", j)
197-
@printf(io, " %6d\t|", FEF[j].FES.ndofs)
201+
@printf(io, "\n [%5d] |", j)
202+
@printf(io, " %6d |", FEF[j].offset+1)
203+
@printf(io, " %6d |", FEF[j].last_index)
204+
@printf(io, " %6d |", FEF[j].FES.ndofs)
198205
ext = extrema(view(FEF[j]))
199206
@printf(io, " %.2e/%.2e \t|", ext[1], ext[2])
200207
if length(FEF.tags) >= j
@@ -260,7 +267,7 @@ end
260267
"""
261268
$(TYPEDSIGNATURES)
262269
263-
Scalar product between two FEVEctorBlocks
270+
Scalar product between two FEVEctorBlocks.
264271
"""
265272
function LinearAlgebra.dot(a::FEVectorBlock{T}, b::FEVectorBlock{T}) where {T}
266273
return dot(view(a), view(b))

src/finiteelements.jl

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,15 @@ ndofs(FES::FESpace) = FES.ndofs
9292
"""
9393
$(TYPEDSIGNATURES)
9494
95+
returns the offset between the degrees of freedom of each component
96+
(i.e. the number of scalar degrees of freedom that influence a component,
97+
vector-valued degrees of freedom are stored at the end).
98+
"""
99+
coffset(FES::FESpace) = FES.coffset
100+
101+
"""
102+
$(TYPEDSIGNATURES)
103+
95104
returns true if the finite element space is broken, false if not
96105
"""
97106
broken(FES::FESpace) = FES.broken
@@ -213,12 +222,14 @@ function Base.show(io::IO, FES::FESpace{Tv, Ti, FEType, APT}) where {Tv, Ti, FET
213222
println(io, "\nFESpace information")
214223
println(io, "===================")
215224
println(io, " name = $(FES.name)")
216-
println(io, " FEType = $FEType")
225+
println(io, " FEType = $FEType ($(FES.broken ? "$APT, broken" : "$APT"))")
217226
println(io, " FEClass = $(supertype(FEType))")
218-
println(io, " ndofs = $(FES.ndofs)\n")
227+
println(io, " ndofs = $(FES.ndofs) $(FES.coffset !== FES.ndofs ? "(coffset = $(FES.coffset))" : "")\n")
228+
println(io, " xgrid = $(FES.xgrid)")
229+
println(io, " dofgrid = $(FES.dofgrid !== FES.xgrid ? FES.dofgrid : "xgrid")")
219230
println(io, "")
220231
println(io, "DofMaps")
221-
println(io, "==========")
232+
println(io, "=======")
222233
for tuple in FES.dofmaps
223234
println(io, "> $(tuple[1])")
224235
end

0 commit comments

Comments
 (0)