Skip to content

Commit

Permalink
Merge pull request #109 from gridap/pretty_printing
Browse files Browse the repository at this point in the history
Pretty printing for the types more exposed to the users
  • Loading branch information
fverdugo committed Sep 30, 2019
2 parents b4bfe78 + dd4c949 commit f86819b
Show file tree
Hide file tree
Showing 25 changed files with 285 additions and 4 deletions.
7 changes: 4 additions & 3 deletions src/CellValues/CellMaps.jl
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,14 @@ function test_iter_cell_map_with_index_result(
end

function show(io::IO,self::CellMap)
s = "CellMap object with object id $(objectid(self)) and concrete type:"
println(io,s)
print(io,typeof(self))
s = "$(nameof(typeof(self))) object"
print(io,s)
end

function show(io::IO,::MIME"text/plain",self::CellMap)
show(io,self)
print(io,":")
print(io,"\n length: $(length(self))")
end

end # module CellMaps
20 changes: 20 additions & 0 deletions src/FESpaces/FEFunctions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,24 @@ restrict(uh::FEFunction,trian::BoundaryTriangulation) = restrict(uh.cellfield,tr

restrict(uh::FEFunction,trian::SkeletonTriangulation) = restrict(uh.cellfield,trian)

# Pretty printing

import Base: show

function show(io::IO,self::FEFunction{D,Z,T,E}) where {D,Z,T,E}
print(io,"$(nameof(typeof(self))) object")
end

function show(io::IO,::MIME"text/plain",self::FEFunction{D,Z,T,E}) where {D,Z,T,E}
show(io,self)
print(io,":")
print(io,"\n physdim: $D")
print(io,"\n refdim: $Z")
print(io,"\n valuetype: $T")
print(io,"\n doftype: $E")
print(io,"\n nfree: $(length(free_dofs(self)))")
print(io,"\n ndiri: $(length(diri_dofs(self)))")
print(io,"\n ncells: $(length(self))")
end

end # module
21 changes: 21 additions & 0 deletions src/FESpaces/FEOperators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -364,4 +364,25 @@ function solve!(uh::FEFunctionLike,nls::NonLinearFESolver,op::FEOperator,cache::
FEFunction(U,x)
end

# Pretty printing

import Base: show

function show(io::IO,self::FEOperator)
print(io,"$(nameof(typeof(self))) object")
end

function show(io::IO,::MIME"text/plain",self::FEOperator)
show(io,self)
end

function show(io::IO,self::FESolver)
print(io,"$(nameof(typeof(self))) object")
end

function show(io::IO,::MIME"text/plain",self::FESolver)
show(io,self)
end


end # module FEOperators
16 changes: 16 additions & 0 deletions src/FESpaces/FESpaces.jl
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,22 @@ function test_fe_space(

end

# Pretty printing

import Base: show

function show(io::IO,self::FESpace{D,Z,T}) where {D,Z,T}
print(io,"$(nameof(typeof(self))) object")
end

function show(io::IO,::MIME"text/plain",self::FESpace{D,Z,T}) where {D,Z,T}
show(io,self)
print(io,":")
print(io,"\n physdim: $D")
print(io,"\n refdim: $Z")
print(io,"\n valuetype: $T")
end

# Helpers

"""
Expand Down
12 changes: 12 additions & 0 deletions src/FESpaces/FETerms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ function setup_cell_jacobian(t::AffineFETerm,uh,v,du)
setup_cell_matrix(t,v,du)
end

# Pretty printing

import Base: show

function show(io::IO,self::FETerm)
print(io,"$(nameof(typeof(self))) object")
end

function show(io::IO,::MIME"text/plain",self::FETerm)
show(io,self)
end

# Concrete implementations

struct AffineFETermFromIntegration <: AffineFETerm
Expand Down
19 changes: 19 additions & 0 deletions src/Geometry/DiscreteModels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,25 @@ function test_discrete_model(model::DiscreteModel{D},dim::Integer) where D
@test isa(trian,Triangulation)
end

# Pretty printing

import Base: show

function show(io::IO,self::DiscreteModel{D}) where D
print(io,"$(nameof(typeof(self))) object")
end

function show(io::IO,::MIME"text/plain",self::DiscreteModel{D}) where D
show(io,self)
print(io,":")
print(io,"\n celldim: $D")
labels = FaceLabels(self)
for d = 0:D
print(io,"\n $d-faces: $(length(labels_on_dim(labels,d)))")
end
print(io,"\n tags: $(ntags(labels))")
end

# Concrete implementations

struct DiscreteModelFromData{D} <: DiscreteModel{D}
Expand Down
19 changes: 19 additions & 0 deletions src/Geometry/FaceLabels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,23 @@ function add_tag_from_tags!(
add_tag_from_tags!(facelabels,name,tags)
end

# Pretty printing

import Base: show

function show(io::IO,self::FaceLabels)
print(io,"FaceLabels object")
end

function show(io::IO,::MIME"text/plain",labels::FaceLabels)
show(io,labels)
print(io,":")
D = length(labels.dim_to_nface_to_label)-1
for d = 0:D
print(io,"\n $d-faces: $(length(labels_on_dim(labels,d)))")
end
print(io,"\n tags: $(ntags(labels))")
end


end #module
17 changes: 17 additions & 0 deletions src/Geometry/Grids.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,23 @@ function test_grid(grid::Grid{D,Z},np,nc) where {D,Z}
test_triangulation(trian)
end

# Pretty printing

import Base: show

function show(io::IO,self::Grid{D,Z}) where {D,Z}
print(io,"$(nameof(typeof(self))) object")
end

function show(io::IO,::MIME"text/plain",grid::Grid)
show(io,grid)
print(io,":")
print(io,"\n pointdim: $(pointdim(grid))")
print(io,"\n celldim: $(celldim(grid))")
print(io,"\n npoints: $(npoints(grid))")
print(io,"\n ncells: $(ncells(grid))")
end

# Concrete implementation

struct GridFromData{D,Z} <: Grid{D,Z}
Expand Down
15 changes: 15 additions & 0 deletions src/Integration/CellQuadratures.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ weights(::CellQuadrature)::CellValues{Float64} = @abstractmethod

function CellQuadrature end

# Pretty printing

import Base: show

function show(io::IO,self::CellQuadrature{D}) where {D}
print(io,"CellQuadrature object")
end

function show(io::IO,::MIME"text/plain",quad::CellQuadrature{D}) where {D}
show(io,quad)
print(io,":")
print(io,"\n dim: $D")
print(io,"\n ncells: $(length(quad))")
end

# Concrete structs

"""
Expand Down
19 changes: 18 additions & 1 deletion src/Integration/NormalVectors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function NormalVector(desc::BoundaryDescriptor)
_normal_vector(jac_surf,polytopes,facet_to_lfacet)
end

length(nv::NormalVector) = length(jac_surf)
length(nv::NormalVector) = length(nv.jac_surf)

function evaluate(nv::NormalVector{Z}, q::CellPoints{Z}) where Z
jac_surf_q = evaluate(nv.jac_surf,q)
Expand Down Expand Up @@ -77,4 +77,21 @@ function _normal_vector(

end

#@fverdugo this code works does not work here, but works when placed
# in the tests
import Base: show

function show(io::IO,self::NormalVector{Z,D,J,R}) where {Z,D,J,R}
print(io,"$(nameof(typeof(self))){$Z,$D} object")
end

function show(io::IO,::MIME"text/plain",self::NormalVector{Z,D,J,R}) where {Z,D,J,R}
show(io,self)
print(io,":")
print(io,"\n physdim: $D")
print(io,"\n refdim: $Z")
print(io,"\n ncells: $(length(self))")
end


end # module
16 changes: 16 additions & 0 deletions src/Integration/Triangulations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,22 @@ function test_triangulation(trian::Triangulation{Z,D}) where {Z,D}
@test isa(reffes,CellValue{LagrangianRefFE{Z,Float64}})
end

# Pretty printing

import Base: show

function show(io::IO,self::Triangulation{Z,D}) where {Z,D}
print(io,"$(nameof(typeof(self))) object")
end

function show(io::IO,::MIME"text/plain",trian::Triangulation{Z,D}) where {Z,D}
show(io,trian)
print(io,":")
print(io,"\n physdim: $D")
print(io,"\n refdim: $Z")
print(io,"\n ncells: $(ncells(trian))")
end

# Factories

function CellField(trian::Triangulation,fun::Function)
Expand Down
18 changes: 18 additions & 0 deletions src/MultiField/MultiFEFunctions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,23 @@ function restrict(uh::MultiFEFunction,trian::SkeletonTriangulation)
[ restrict(ui.cellfield,trian) for ui in uh.fields ]
end

# Pretty printing

import Base: show

function show(io::IO,self::MultiFEFunction)
s = "MultiFEFunction object with $(length(self)) fields"
print(io,s)
end

function show(io::IO,::MIME"text/plain",self::MultiFEFunction)
show(io,self)
print(io,":")
print(io,"\n nfree (all fields): $(length(self.free_dofs_all_fields))")
for (i,U) in enumerate(self.fields)
print(io,"\nfield $i:\n")
show(io,"text/plain",U)
end
end

end # module MultiFEFunctions
32 changes: 32 additions & 0 deletions src/MultiField/MultiFESpaces.jl
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,36 @@ function _compute_offsets(U)
offsets
end

# Pretty printing

import Base: show

function show(io::IO,self::Vector{<:FESpace})
s = "Vector of FESpace objects with $(length(self)) fields"
print(io,s)
end

function show(io::IO,::MIME"text/plain",self::Vector{<:FESpace})
show(io,self)
print(io,":")
for (i,U) in enumerate(self)
print(io,"\nfield $i:\n")
show(io,"text/plain",U)
end
end

function show(io::IO,self::MultiFESpace)
s = "MultiFESpace object with $(length(self)) fields"
print(io,s)
end

function show(io::IO,::MIME"text/plain",self::MultiFESpace)
show(io,self)
print(io,":")
for (i,U) in enumerate(self.fespaces)
print(io,"\nfield $i:\n")
show(io,"text/plain",U)
end
end

end # module MultiFESpaces
4 changes: 4 additions & 0 deletions test/FESpacesTests/FEFunctionsTests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ ufun(x) = x[1]

uh = interpolate(fespace,ufun)

@test string(uh) == "FEFunction object"
sr = "FEFunction object:\n physdim: 2\n refdim: 2\n valuetype: Float64\n doftype: Float64\n nfree: 9\n ndiri: 16\n ncells: 16"
@test sprint(show,"text/plain",uh) == sr

@test isa(Triangulation(uh),Triangulation)

tags = [7,6]
Expand Down
8 changes: 8 additions & 0 deletions test/FESpacesTests/FEOperatorsTestsMixin.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,18 @@ assem = SparseMatrixAssembler(V,U)
# Define the FEOperator
op = LinearFEOperator(a,b,V,U,assem,trian,quad)

sr = "LinearFEOperator object"
@test string(op) == sr
@test sprint(show,"text/plain",op) == sr

# Define the FESolver
ls = BackslashSolver()
solver = LinearFESolver(ls)

sr = "LinearFESolver object"
@test string(solver) == sr
@test sprint(show,"text/plain",solver) == sr

# Solve!
uh = solve(solver,op)

Expand Down
4 changes: 4 additions & 0 deletions test/FESpacesTests/FESpacesTests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ order = 1
tags = [1,2,3,4]
fespace = H1ConformingFESpace(Float64,model,order,tags)

@test string(fespace) == "ConformingFESpace object"
s = "ConformingFESpace object:\n physdim: 2\n refdim: 2\n valuetype: Float64"
@test sprint(show,"text/plain",fespace) == s

trian = Triangulation(model)
quad = CellQuadrature(trian,degree=2)

Expand Down
4 changes: 4 additions & 0 deletions test/FESpacesTests/FETermsTests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ g(v) = inner(v,gfield)

t1 = AffineFETerm(a,l,trian,quad)

sr = "AffineFETermFromIntegration object"
@test string(t1) == sr
@test sprint(show,"text/plain",t1) == sr

cm = setup_cell_matrix(t1,v,du)
@test isa(cm,CellMatrix)
@test length(cm) == ncells(trian)
Expand Down
6 changes: 6 additions & 0 deletions test/GeometryTests/DiscreteModelsTests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ grids = Grid[grid0,grid1,grid2]

model = DiscreteModelFromData(grids,graph,facelabels)

@test string(model) == "DiscreteModelFromData object"

s = "DiscreteModelFromData object:\n celldim: 2\n 0-faces: 6\n 1-faces: 7\n 2-faces: 2\n tags: 2"

@test sprint(show,"text/plain",model) == s

test_discrete_model(model,2)

vgrid = Grid(model)
Expand Down
4 changes: 4 additions & 0 deletions test/GeometryTests/FaceLabelsTests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,8 @@ add_tag_from_tags!(labels,"label4",["label1","label2"])
@test labels_on_tag(labels,4) == [1, 2, 3, 4, 5, 6]
@test tag_from_name(labels,"label4") == 4

@test string(labels) == "FaceLabels object"
s = "FaceLabels object:\n 0-faces: 9\n 1-faces: 9\n tags: 4"
@test sprint(show,"text/plain",labels) == s

end # module

0 comments on commit f86819b

Please sign in to comment.