Skip to content

Commit

Permalink
Add proper show for InitialCondition and geometries (#695)
Browse files Browse the repository at this point in the history
* add proper show

* fix tests

* fix

* fix doc tests

* implement suggestions

* fix

---------

Co-authored-by: LasNikas <[email protected]>
  • Loading branch information
LasNikas and LasNikas authored Jan 14, 2025
1 parent be1f695 commit 7352afb
Show file tree
Hide file tree
Showing 13 changed files with 195 additions and 7 deletions.
28 changes: 27 additions & 1 deletion src/general/initial_condition.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,13 @@ initial_condition = InitialCondition(; coordinates, velocity, mass, density)
initial_condition = InitialCondition(; coordinates, velocity=x -> 2x, mass=1.0, density=1000.0)
# output
InitialCondition{Float64}(-1.0, [0.0 1.0 1.0; 0.0 0.0 1.0], [0.0 2.0 2.0; 0.0 0.0 2.0], [1.0, 1.0, 1.0], [1000.0, 1000.0, 1000.0], [0.0, 0.0, 0.0])
┌──────────────────────────────────────────────────────────────────────────────────────────────────┐
│ InitialCondition{Float64} │
│ ═════════════════════════ │
│ #dimensions: ……………………………………………… 2 │
│ #particles: ………………………………………………… 3 │
│ particle spacing: ………………………………… -1.0 │
└──────────────────────────────────────────────────────────────────────────────────────────────────┘
```
"""
struct InitialCondition{ELTYPE}
Expand Down Expand Up @@ -183,6 +189,26 @@ struct InitialCondition{ELTYPE}
end
end

function Base.show(io::IO, ic::InitialCondition)
@nospecialize ic # reduce precompilation time

print(io, "InitialCondition{$(eltype(ic))}()")
end

function Base.show(io::IO, ::MIME"text/plain", ic::InitialCondition)
@nospecialize ic # reduce precompilation time

if get(io, :compact, false)
show(io, system)
else
summary_header(io, "InitialCondition{$(eltype(ic))}")
summary_line(io, "#dimensions", "$(ndims(ic))")
summary_line(io, "#particles", "$(nparticles(ic))")
summary_line(io, "particle spacing", "$(ic.particle_spacing)")
summary_footer(io)
end
end

function wrap_function(function_::Function, ::Val)
# Already a function
return function_
Expand Down
18 changes: 18 additions & 0 deletions src/preprocessing/geometries/polygon.jl
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,24 @@ struct Polygon{NDIMS, ELTYPE}
end
end

function Base.show(io::IO, geometry::Polygon)
@nospecialize geometry # reduce precompilation time

print(io, "Polygon{$(ndims(geometry)), $(eltype(geometry))}()")
end

function Base.show(io::IO, ::MIME"text/plain", geometry::Polygon)
@nospecialize geometry # reduce precompilation time

if get(io, :compact, false)
show(io, system)
else
summary_header(io, "Polygon{$(ndims(geometry)), $(eltype(geometry))}")
summary_line(io, "#edges", "$(nfaces(geometry))")
summary_footer(io)
end
end

@inline Base.ndims(::Polygon{NDIMS}) where {NDIMS} = NDIMS

@inline Base.eltype(::Polygon{NDIMS, ELTYPE}) where {NDIMS, ELTYPE} = ELTYPE
Expand Down
19 changes: 19 additions & 0 deletions src/preprocessing/geometries/triangle_mesh.jl
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,25 @@ struct TriangleMesh{NDIMS, ELTYPE}
end
end

function Base.show(io::IO, geometry::TriangleMesh)
@nospecialize geometry # reduce precompilation time

print(io, "TriangleMesh{$(ndims(geometry)), $(eltype(geometry))}()")
end

function Base.show(io::IO, ::MIME"text/plain", geometry::TriangleMesh)
@nospecialize geometry # reduce precompilation time

if get(io, :compact, false)
show(io, system)
else
summary_header(io, "TriangleMesh{$(ndims(geometry)), $(eltype(geometry))}")
summary_line(io, "#faces", "$(nfaces(geometry))")
summary_line(io, "#vertices", "$(length(geometry.vertices))")
summary_footer(io)
end
end

@inline Base.ndims(::TriangleMesh{NDIMS}) where {NDIMS} = NDIMS

@inline Base.eltype(::TriangleMesh{NDIMS, ELTYPE}) where {NDIMS, ELTYPE} = ELTYPE
Expand Down
20 changes: 20 additions & 0 deletions src/preprocessing/point_in_poly/winding_number_jacobson.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,26 @@ struct WindingNumberJacobson{ELTYPE, W}
end
end

function Base.show(io::IO, winding::WindingNumberJacobson)
@nospecialize winding # reduce precompilation time

print(io, "WindingNumberJacobson{$(type2string(winding.winding))}()")
end

function Base.show(io::IO, ::MIME"text/plain", winding::WindingNumberJacobson)
@nospecialize winding # reduce precompilation time

if get(io, :compact, false)
show(io, system)
else
summary_header(io, "WindingNumberJacobson")
summary_line(io, "winding number factor",
"$(round(winding.winding_number_factor; digits=3))")
summary_line(io, "winding", "$(type2string(winding.winding))")
summary_footer(io)
end
end

function (point_in_poly::WindingNumberJacobson)(geometry, points;
store_winding_number=false)
(; winding_number_factor, winding) = point_in_poly
Expand Down
10 changes: 8 additions & 2 deletions src/schemes/solid/total_lagrangian_sph/system.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,17 @@ See [Total Lagrangian SPH](@ref tlsph) for more details on the method.
!!! note
The fixed particles must be the **last** particles in the `InitialCondition`.
To do so, e.g. use the `union` function:
```jldoctest; output = false, filter = r"InitialCondition{Float64}.*", setup = :(fixed_particles = RectangularShape(0.1, (1, 4), (0.0, 0.0), density=1.0); beam = RectangularShape(0.1, (3, 4), (0.1, 0.0), density=1.0))
```jldoctest; output = false, setup = :(fixed_particles = RectangularShape(0.1, (1, 4), (0.0, 0.0), density=1.0); beam = RectangularShape(0.1, (3, 4), (0.1, 0.0), density=1.0))
solid = union(beam, fixed_particles)
# output
InitialCondition{Float64}(...) *the rest of this line is ignored by filter*
┌──────────────────────────────────────────────────────────────────────────────────────────────────┐
│ InitialCondition{Float64} │
│ ═════════════════════════ │
│ #dimensions: ……………………………………………… 2 │
│ #particles: ………………………………………………… 16 │
│ particle spacing: ………………………………… 0.1 │
└──────────────────────────────────────────────────────────────────────────────────────────────────┘
```
where `beam` and `fixed_particles` are of type `InitialCondition`.
"""
Expand Down
8 changes: 7 additions & 1 deletion src/setups/extrude_geometry.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,13 @@ shape = extrude_geometry(shape; direction, particle_spacing=0.1, n_extrude=4, de
└ New particle spacing is set to 0.09387239731236392.
┌ Info: The desired size is not a multiple of the particle spacing 0.1.
└ New particle spacing is set to 0.09198039027185569.
InitialCondition{Float64}(0.1, [0.44999999999999996 0.43096988312782164 … -0.23871756048182058 -0.24999999999999994; 0.4 0.4956708580912724 … 0.5001344202803415 0.4000000000000001; 0.05 0.05 … 0.35000000000000003 0.35000000000000003], [0.0 0.0 … 0.0 0.0; 0.0 0.0 … 0.0 0.0; 0.0 0.0 … 0.0 0.0], [1.0000000000000002, 1.0000000000000002, 1.0000000000000002, 1.0000000000000002, 1.0000000000000002, 1.0000000000000002, 1.0000000000000002, 1.0000000000000002, 1.0000000000000002, 1.0000000000000002 … 1.0000000000000002, 1.0000000000000002, 1.0000000000000002, 1.0000000000000002, 1.0000000000000002, 1.0000000000000002, 1.0000000000000002, 1.0000000000000002, 1.0000000000000002, 1.0000000000000002], [1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0 … 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 … 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0])
┌──────────────────────────────────────────────────────────────────────────────────────────────────┐
│ InitialCondition{Float64} │
│ ═════════════════════════ │
│ #dimensions: ……………………………………………… 3 │
│ #particles: ………………………………………………… 144 │
│ particle spacing: ………………………………… 0.1 │
└──────────────────────────────────────────────────────────────────────────────────────────────────┘
```
!!! warning "Experimental Implementation"
Expand Down
8 changes: 7 additions & 1 deletion src/setups/rectangular_shape.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,13 @@ rectangular = RectangularShape(particle_spacing, (5, 4), (1.0, 2.0),
rectangular = RectangularShape(particle_spacing, (5, 4, 7), (1.0, 2.0, 3.0), density=1000.0)
# output
InitialCondition{Float64}(0.1, [1.05 1.15 … 1.35 1.45; 2.05 2.05 … 2.35 2.35; 3.05 3.05 … 3.65 3.65], [0.0 0.0 … 0.0 0.0; 0.0 0.0 … 0.0 0.0; 0.0 0.0 … 0.0 0.0], [1.0000000000000002, 1.0000000000000002, 1.0000000000000002, 1.0000000000000002, 1.0000000000000002, 1.0000000000000002, 1.0000000000000002, 1.0000000000000002, 1.0000000000000002, 1.0000000000000002 … 1.0000000000000002, 1.0000000000000002, 1.0000000000000002, 1.0000000000000002, 1.0000000000000002, 1.0000000000000002, 1.0000000000000002, 1.0000000000000002, 1.0000000000000002, 1.0000000000000002], [1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0 … 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 … 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0])
┌──────────────────────────────────────────────────────────────────────────────────────────────────┐
│ InitialCondition{Float64} │
│ ═════════════════════════ │
│ #dimensions: ……………………………………………… 3 │
│ #particles: ………………………………………………… 140 │
│ particle spacing: ………………………………… 0.1 │
└──────────────────────────────────────────────────────────────────────────────────────────────────┘
```
"""
function RectangularShape(particle_spacing, n_particles_per_dimension, min_coordinates;
Expand Down
8 changes: 7 additions & 1 deletion src/setups/sphere_shape.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,13 @@ SphereShape(0.1, 0.5, (0.2, 0.4, 0.3), 1000.0)
SphereShape(0.1, 0.5, (0.2, 0.4, 0.3), 1000.0, sphere_type=RoundSphere())
# output
InitialCondition{Float64}(0.1, [0.25 0.17500000000000002 … 0.269548322038589 0.2; 0.4 0.44330127018922194 … 0.3127891626126164 0.4; 0.3 0.3 … -0.13595561786013038 -0.15000000000000002], [0.0 0.0 … 0.0 0.0; 0.0 0.0 … 0.0 0.0; 0.0 0.0 … 0.0 0.0], [1.0000000000000002, 1.0000000000000002, 1.0000000000000002, 1.0000000000000002, 1.0000000000000002, 1.0000000000000002, 1.0000000000000002, 1.0000000000000002, 1.0000000000000002, 1.0000000000000002 … 1.0000000000000002, 1.0000000000000002, 1.0000000000000002, 1.0000000000000002, 1.0000000000000002, 1.0000000000000002, 1.0000000000000002, 1.0000000000000002, 1.0000000000000002, 1.0000000000000002], [1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0 … 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 … 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0])
┌──────────────────────────────────────────────────────────────────────────────────────────────────┐
│ InitialCondition{Float64} │
│ ═════════════════════════ │
│ #dimensions: ……………………………………………… 3 │
│ #particles: ………………………………………………… 518 │
│ particle spacing: ………………………………… 0.1 │
└──────────────────────────────────────────────────────────────────────────────────────────────────┘
```
"""
function SphereShape(particle_spacing, radius, center_position, density;
Expand Down
17 changes: 17 additions & 0 deletions test/general/initial_condition.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,23 @@
SphereShape(0.1, 0.5, (1.0, 0.5), 1000.0,
sphere_type=RoundSphere())))

@testset verbose=true "Show" begin
shape = RectangularShape(0.05, (20, 20), (0.0, 0.0), density=1.0)

show_compact = "InitialCondition{Float64}()"
@test repr(shape) == show_compact

show_box = """
┌──────────────────────────────────────────────────────────────────────────────────────────────────┐
│ InitialCondition{Float64} │
│ ═════════════════════════ │
│ #dimensions: ……………………………………………… 2 │
│ #particles: ………………………………………………… 400 │
│ particle spacing: ………………………………… 0.05 │
└──────────────────────────────────────────────────────────────────────────────────────────────────┘"""
@test repr("text/plain", shape) == show_box
end

@testset verbose=true "Constructors" begin
@testset "Illegal Inputs" begin
error_str = "`coordinates` and `velocities` must be of the same size"
Expand Down
30 changes: 30 additions & 0 deletions test/preprocessing/geometries/geometries.jl
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,36 @@
end
end

@testset verbose=true "Show" begin
data_dir = pkgdir(TrixiParticles, "examples", "preprocessing", "data")
geometry = load_geometry(joinpath(data_dir, "circle.asc"))

show_compact = "Polygon{2, Float64}()"
@test repr(geometry) == show_compact

show_box = """
┌──────────────────────────────────────────────────────────────────────────────────────────────────┐
│ Polygon{2, Float64} │
│ ═══════════════════ │
│ #edges: …………………………………………………………… 63 │
└──────────────────────────────────────────────────────────────────────────────────────────────────┘"""
@test repr("text/plain", geometry) == show_box

geometry = load_geometry(joinpath(data_dir, "sphere.stl"))

show_compact = "TriangleMesh{3, Float64}()"
@test repr(geometry) == show_compact

show_box = """
┌──────────────────────────────────────────────────────────────────────────────────────────────────┐
│ TriangleMesh{3, Float64} │
│ ════════════════════════ │
│ #faces: …………………………………………………………… 3072 │
│ #vertices: …………………………………………………… 1538 │
└──────────────────────────────────────────────────────────────────────────────────────────────────┘"""
@test repr("text/plain", geometry) == show_box
end

@testset verbose=true "Unique Sort" begin
# Fixed seed to ensure reproducibility
Random.seed!(1)
Expand Down
Loading

0 comments on commit 7352afb

Please sign in to comment.