Skip to content

Commit

Permalink
Merge pull request #54 from sintefmath/dev
Browse files Browse the repository at this point in the history
Test + fix for sparsity wrapper
  • Loading branch information
moyner authored Jan 5, 2024
2 parents 18e297b + 3a6133b commit c9c1cdd
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Jutul"
uuid = "2b460a1a-8a2b-45b2-b125-b5c536396eb9"
authors = ["Olav Møyner <[email protected]>"]
version = "0.2.18"
version = "0.2.19"

[deps]
AlgebraicMultigrid = "2169fc97-5a83-5252-b627-83903c6c433c"
Expand Down
34 changes: 31 additions & 3 deletions src/ad/sparsity.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,47 @@ 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
j = mod(zero_ix, m) + 1
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

Expand Down
4 changes: 2 additions & 2 deletions src/meshes/unstructured/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down
2 changes: 1 addition & 1 deletion src/partitioning.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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).
"""
Expand Down
2 changes: 1 addition & 1 deletion src/simulator/print.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ include("adjoints/basic_adjoint.jl")
include("utils.jl")
include("partitioning.jl")
include("units.jl")
include("sparsity.jl")
51 changes: 51 additions & 0 deletions test/sparsity.jl
Original file line number Diff line number Diff line change
@@ -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

2 comments on commit c9c1cdd

@moyner
Copy link
Member Author

@moyner moyner commented on c9c1cdd Jan 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/98272

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.2.19 -m "<description of version>" c9c1cdd8dab867562927fc480b80f427d1cb1272
git push origin v0.2.19

Please sign in to comment.