From ae2205ffa3327ca526dbe532a76b1b7d42eb4871 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olav=20M=C3=B8yner?= Date: Thu, 4 Jan 2024 13:55:25 +0100 Subject: [PATCH 1/2] Fix typo --- src/simulator/print.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/simulator/print.jl b/src/simulator/print.jl index 86fc5a30..83c32251 100644 --- a/src/simulator/print.jl +++ b/src/simulator/print.jl @@ -102,7 +102,7 @@ function final_simulation_message(simulator, p, rec, t_elapsed, reports, timeste if verbose && length(reports) > 0 if aborted start_str = "Simulation aborted" - endstr = "$(stats.steps-1) of $(length(timesteps))" + endstr = "$(stats.steps) of $(length(timesteps))" str_c = :red else start_str = "Simulation complete" From 3a6133b21ab000fc49d0131df0b309b1ee0c6f24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olav=20M=C3=B8yner?= Date: Fri, 5 Jan 2024 11:32:01 +0100 Subject: [PATCH 2/2] Updates to sparsity wrapper --- Project.toml | 2 +- src/ad/sparsity.jl | 34 +++++++++++++++++++-- src/meshes/unstructured/types.jl | 4 +-- src/partitioning.jl | 2 +- test/runtests.jl | 1 + test/sparsity.jl | 51 ++++++++++++++++++++++++++++++++ 6 files changed, 87 insertions(+), 7 deletions(-) create mode 100644 test/sparsity.jl diff --git a/Project.toml b/Project.toml index 20134322..9df5f9e5 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "Jutul" uuid = "2b460a1a-8a2b-45b2-b125-b5c536396eb9" authors = ["Olav Møyner "] -version = "0.2.18" +version = "0.2.19" [deps] AlgebraicMultigrid = "2169fc97-5a83-5252-b627-83903c6c433c" diff --git a/src/ad/sparsity.jl b/src/ad/sparsity.jl index 59e8135e..8ff32ff4 100644 --- a/src/ad/sparsity.jl +++ b/src/ad/sparsity.jl @@ -35,11 +35,39 @@ Base.parent(A::SparsityTracingWrapper) = A.data Base.size(A::SparsityTracingWrapper) = size(A.data) Base.axes(A::SparsityTracingWrapper) = axes(A.data) -function Base.getindex(A::SparsityTracingWrapper, i, j) +function Base.getindex(A::SparsityTracingWrapper, i) + if i isa Colon + return A + else + return map(i -> A[i], i) + end +end + +function Base.getindex(A::SparsityTracingWrapper{T, D, <:Any}, I, J) where {T, D} + @assert D > 1 + if I isa Colon + I = axes(A, 1) + end + if J isa Colon + J = axes(A, 2) + end + Ts = Jutul.ST.ADval{T} + n = length(I) + m = length(J) + out = Matrix{Ts}(undef, n, m) + for (i, ix) in enumerate(I) + for (j, jx) in enumerate(J) + out[i, j] = A[ix, jx] + end + end + return out +end + +function Base.getindex(A::SparsityTracingWrapper, i::Int, j::Int) return as_tracer(A.data[i, j], j) end -function Base.getindex(A::SparsityTracingWrapper{T, 2, D}, ix) where {T, D} +function Base.getindex(A::SparsityTracingWrapper{T, 2, D}, ix::Int) where {T, D} n, m = size(A) zero_ix = ix - 1 i = (zero_ix ÷ m) + 1 @@ -47,7 +75,7 @@ function Base.getindex(A::SparsityTracingWrapper{T, 2, D}, ix) where {T, D} return as_tracer(A.data[i, j], j) end -function Base.getindex(A::SparsityTracingWrapper{T, 1, D}, i) where {T, D} +function Base.getindex(A::SparsityTracingWrapper{T, 1, D}, i::Int) where {T, D} return as_tracer(A.data[i], i) end diff --git a/src/meshes/unstructured/types.jl b/src/meshes/unstructured/types.jl index 1ee05c21..75545582 100644 --- a/src/meshes/unstructured/types.jl +++ b/src/meshes/unstructured/types.jl @@ -226,8 +226,8 @@ function UnstructuredMesh( int_neighbors = convert_neighborship(int_neighbors) @assert length(bnd_cells) == nb bnd_cells::AbstractVector - @assert maximum(bnd_cells) <= nc - @assert minimum(bnd_cells) > 0 + @assert maximum(bnd_cells, init = 1) <= nc + @assert minimum(bnd_cells, init = nc) > 0 @assert maximum(faces_to_nodes.vals, init = 0) <= nn "Too few nodes provided" return UnstructuredMesh(cells_to_faces, cells_to_bnd, faces_to_nodes, bnd_to_nodes, node_points, int_neighbors, bnd_cells; kwarg...) diff --git a/src/partitioning.jl b/src/partitioning.jl index 4a06d3a8..58fe1c7e 100644 --- a/src/partitioning.jl +++ b/src/partitioning.jl @@ -294,7 +294,7 @@ end """ partition_hypergraph(g, n::Int, partitioner = MetisPartitioner(); expand = true) -Partition a hypergraph from [setup_partitioner_hypergraph](@ref) using a given +Partition a hypergraph from [`setup_partitioner_hypergraph`](@ref) using a given partitioner. If the optional `expand` parameter is set to true the result will be expanded to the full graph (i.e. where groups are not condensed). """ diff --git a/test/runtests.jl b/test/runtests.jl index 2aba4d58..b2fa3dac 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -6,3 +6,4 @@ include("adjoints/basic_adjoint.jl") include("utils.jl") include("partitioning.jl") include("units.jl") +include("sparsity.jl") diff --git a/test/sparsity.jl b/test/sparsity.jl new file mode 100644 index 00000000..a029be72 --- /dev/null +++ b/test/sparsity.jl @@ -0,0 +1,51 @@ +using Jutul, Test + +@testset "SparsityTracingWrapper" begin + n = 10 + m = 3 + test_mat = zeros(m, n) + test_vec = zeros(n) + + for i in 1:n + test_vec[i] = i + for j in 1:m + test_mat[j, i] = i + (j-1)*n + end + end + + vec_st = Jutul.SparsityTracingWrapper(test_vec) + for i in 1:n + v = vec_st[i] + @test v isa Jutul.ST.ADval + @test v.derivnode.index == i + @test v.val == test_vec[i] + end + + mat_st = Jutul.SparsityTracingWrapper(test_mat) + for i in 1:n + for j in 1:m + v = mat_st[j, i] + @test v isa Jutul.ST.ADval + @test v.derivnode.index == i + @test v.val == test_mat[j, i] + end + end + + # Test ranges for vector + rng = 2:7 + tmp = vec_st[rng] + @test size(tmp) == size(test_vec[rng]) + + for (i, ix) in enumerate(rng) + @test tmp[i] == vec_st[ix] + end + + # Test subranges + tmp2 = mat_st[:, rng] + @test size(tmp2) == size(test_mat[:, rng]) + for (i, ix) in enumerate(rng) + for j in 1:m + @test tmp2[j, i] == mat_st[j, ix] + end + end +end