Skip to content

Commit

Permalink
Merge pull request #130 from j-fu/fix/odesparse
Browse files Browse the repository at this point in the history
Fix/odesparse
  • Loading branch information
j-fu authored Oct 12, 2024
2 parents e08f5ff + d8d04ba commit c83220b
Show file tree
Hide file tree
Showing 16 changed files with 193 additions and 127 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
# Changes

## v3.0 Planned (pending some improvements in LinearSolve)
- use `precs` based linear solver API, see https://github.com/SciML/LinearSolve.jl/pull/514
- stop re-exporting ForwardDiff.value
- try to remove type piracies

## v2.0.2 October 12, 2024
- Bugfixes
- replace internally used `values` and `vec` methods by new `dofs`
- fix #119, update Example115, Example120, test sparse unknown storage & ODE solver
- directly re-export ForwardDiff.value, dont type-pirate a new method

## v2.0.1 October 7, 2024
- Bugfixes
- fix case where the last element of a time solution is used as initial value for a next `solve`

## v2.0.0 September 12, 2024

Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "VoronoiFVM"
uuid = "82b139dc-5afc-11e9-35da-9b9bdfd336f3"
authors = ["Jürgen Fuhrmann <[email protected]>", "Patrick Jaap", "Dilara Abdel", "Jan Weidner", "Alexander Seiler", "Patricio Farrell", "Matthias Liero"]
version = "2.0.1"
version = "2.0.2"

[deps]
BandedMatrices = "aae01518-5342-5314-be14-df237901396f"
Expand Down
1 change: 1 addition & 0 deletions docs/src/internal.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ VoronoiFVM.getspecies
VoronoiFVM.getnodedof
VoronoiFVM.increase_num_species!
VoronoiFVM.addzrows
VoronoiFVM.dofs
```


Expand Down
35 changes: 26 additions & 9 deletions examples/Example115_HeterogeneousCatalysis1D.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,12 @@ using VoronoiFVM
using ExtendableGrids
using GridVisualize
using LinearAlgebra
using OrdinaryDiffEqRosenbrock
using SciMLBase: NoInit

function main(; n = 10, Plotter = nothing, verbose = false, tend = 1,
unknown_storage = :sparse, assembly = :edgewise)
unknown_storage = :sparse, assembly = :edgewise,
diffeq=false)
h = 1.0 / convert(Float64, n)
X = collect(0.0:h:1.0)
N = length(X)
Expand Down Expand Up @@ -154,10 +157,17 @@ function main(; n = 10, Plotter = nothing, verbose = false, tend = 1,
## Data to store surface concentration vs time

p = GridVisualizer(; Plotter = Plotter, layout = (3, 1))

control = fixed_timesteps!(VoronoiFVM.NewtonControl(), tstep)
tsol = solve(sys; inival, times = [0, tend], control, verbose = verbose)

if diffeq
inival=unknowns(sys,inival=0)
problem = ODEProblem(sys,inival,(0,tend))
## use fixed timesteps just for the purpose of CI
odesol = solve(problem,Rosenbrock23(); initializealg=NoInit(), dt=tstep, adaptive=false)
tsol=reshape(odesol,sys)
else
control = fixed_timesteps!(VoronoiFVM.NewtonControl(), tstep)
tsol = solve(sys; inival, times = [0, tend], control, verbose = verbose)
end

p = GridVisualizer(; Plotter = Plotter, layout = (3, 1), fast = true)
for it = 1:length(tsol)
time = tsol.t[it]
Expand All @@ -175,9 +185,16 @@ end
using Test
function runtests()
testval = 0.87544440641274
@test isapprox(main(; unknown_storage = :sparse, assembly = :edgewise), testval; rtol = 1.0e-12) &&
isapprox(main(; unknown_storage = :dense, assembly = :edgewise), testval; rtol = 1.0e-12) &&
isapprox(main(; unknown_storage = :sparse, assembly = :cellwise), testval; rtol = 1.0e-12) &&
isapprox(main(; unknown_storage = :dense, assembly = :cellwise), testval; rtol = 1.0e-12)
testvaldiffeq = 0.8891082547874963
@test isapprox(main(; unknown_storage = :sparse, assembly = :edgewise), testval; rtol = 1.0e-12)
@test isapprox(main(; unknown_storage = :dense, assembly = :edgewise), testval; rtol = 1.0e-12)
@test isapprox(main(; unknown_storage = :sparse, assembly = :cellwise), testval; rtol = 1.0e-12)
@test isapprox(main(; unknown_storage = :dense, assembly = :cellwise), testval; rtol = 1.0e-12)

@test isapprox(main(; diffeq=true, unknown_storage = :sparse, assembly = :edgewise), testvaldiffeq; rtol = 1.0e-12)
@test isapprox(main(; diffeq=true, unknown_storage = :dense, assembly = :edgewise), testvaldiffeq; rtol = 1.0e-12)
@test isapprox(main(; diffeq=true, unknown_storage = :sparse, assembly = :cellwise), testvaldiffeq; rtol = 1.0e-12)
@test isapprox(main(; diffeq=true, unknown_storage = :dense, assembly = :cellwise), testvaldiffeq; rtol = 1.0e-12)

end
end
201 changes: 119 additions & 82 deletions examples/Example120_ThreeRegions1D.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,83 @@ using VoronoiFVM
using ExtendableGrids
using GridVisualize
using LinearSolve
using OrdinaryDiffEqRosenbrock
using SciMLBase: NoInit

function reaction(f, u, node, data)
k=data.k
if node.region == 1
f[1] = k[1] * u[1]
f[2] = -k[1] * u[1]
elseif node.region == 3
f[2] = k[3] * u[2]
f[3] = -k[3] * u[2]
else
f[1] = 0
end
end

function source(f, node, data)
if node.region == 1
f[1] = 1.0e-4 * (3.0 - node[1])
end
end

## Since 0.17.0 one can
## write into the result also where
## the corresponding species has not been enabled
## Species information is used to prevent the assembly.
function correctionflux(f, u, edge, data)
eps=data.eps
for i = 1:3
f[i] = eps[i] * (u[i, 1] - u[i, 2])
end
end

function correctionstorage(f, u, node, data)
f .= u
end

## This is the "old" way:
## Write into result only where
## the corresponding species has been enabled
function pickyflux(f, u, edge, data)
eps=data.eps
if edge.region == 1
f[1] = eps[1] * (u[1, 1] - u[1, 2])
f[2] = eps[2] * (u[2, 1] - u[2, 2])
elseif edge.region == 2
f[2] = eps[2] * (u[2, 1] - u[2, 2])
elseif edge.region == 3
f[2] = eps[2] * (u[2, 1] - u[2, 2])
f[3] = eps[3] * (u[3, 1] - u[3, 2])
end
end

function pickystorage(f, u, node, data)
if node.region == 1
f[1] = u[1]
f[2] = u[2]
elseif node.region == 2
f[2] = u[2]
elseif node.region == 3
f[2] = u[2]
f[3] = u[3]
end
end


function main(; n = 30, Plotter = nothing, plot_grid = false, verbose = false,
unknown_storage = :sparse, tend = 10,
diffeq=false,
rely_on_corrections = false, assembly = :edgewise)
h = 3.0 / (n - 1)
X = collect(0:h:3.0)

X=range(0,3,length=n)
grid = simplexgrid(X)
cellmask!(grid, [0.0], [1.0], 1)
cellmask!(grid, [1.0], [2.1], 2)
cellmask!(grid, [1.9], [3.0], 3)

subgrid1 = subgrid(grid, [1])
subgrid2 = subgrid(grid, [1, 2, 3])
subgrid3 = subgrid(grid, [3])
Expand All @@ -27,73 +93,15 @@ function main(; n = 30, Plotter = nothing, plot_grid = false, verbose = false,
plotgrid(grid; Plotter = Plotter)
return
end

eps = [1, 1, 1]
k = [1, 1, 1]

function reaction(f, u, node, data)
if node.region == 1
f[1] = k[1] * u[1]
f[2] = -k[1] * u[1]
elseif node.region == 3
f[2] = k[3] * u[2]
f[3] = -k[3] * u[2]
else
f[1] = 0
end
end

function source(f, node, data)
if node.region == 1
f[1] = 1.0e-4 * (3.0 - node[1])
end
end

if rely_on_corrections
## Since 0.17.0 one can
## write into the result also where
## the corresponding species has not been enabled
## Species information is used to prevent the assembly.
flux = function (f, u, edge, data)
for i = 1:3
f[i] = eps[i] * (u[i, 1] - u[i, 2])
end
end

storage = function (f, u, node, data)
f .= u
end
else
## This is the "old" way:
## Write into result only where
## the corresponding species has been enabled
flux = function (f, u, edge, data)
if edge.region == 1
f[1] = eps[1] * (u[1, 1] - u[1, 2])
f[2] = eps[2] * (u[2, 1] - u[2, 2])
elseif edge.region == 2
f[2] = eps[2] * (u[2, 1] - u[2, 2])
elseif edge.region == 3
f[2] = eps[2] * (u[2, 1] - u[2, 2])
f[3] = eps[3] * (u[3, 1] - u[3, 2])
end
end

storage = function (f, u, node, data)
if node.region == 1
f[1] = u[1]
f[2] = u[2]
elseif node.region == 2
f[2] = u[2]
elseif node.region == 3
f[2] = u[2]
f[3] = u[3]
end
end
end

sys = VoronoiFVM.System(grid; flux, reaction, storage, source,
unknown_storage = unknown_storage, assembly = assembly)

data=(eps = [1, 1, 1], k = [1, 1, 1])

flux = rely_on_corrections ? correctionflux : pickyflux
storage = rely_on_corrections ? correctionstorage : pickystorage

sys = VoronoiFVM.System(grid; data,
flux, reaction, storage, source,
unknown_storage, assembly)

enable_species!(sys, 1, [1])
enable_species!(sys, 2, [1, 2, 3])
Expand All @@ -104,37 +112,55 @@ function main(; n = 30, Plotter = nothing, plot_grid = false, verbose = false,
testval = 0
p = GridVisualizer(; Plotter = Plotter, layout = (1, 1))

testval = 0.0
function plot_timestep(U, Uold, time, Δt)
function plot_timestep(U,time)
U1 = view(U[1, :], subgrid1)
U2 = view(U[2, :], subgrid2)
U3 = view(U[3, :], subgrid3)

testval += sum(U2)

if Plotter == nothing
return
end

scalarplot!(p[1, 1], subgrid1, U1; label = "spec1", color = (0.5, 0, 0),
xlimits = (0, 3), flimits = (0, 1e-3),
title = @sprintf("three regions t=%.3g", time))
scalarplot!(p[1, 1], subgrid2, U2; label = "spec2", color = (0.0, 0.5, 0),
clear = false)
scalarplot!(p[1, 1], subgrid3, U3; label = "spec3", color = (0.0, 0.0, 0.5),
clear = false, show = true)
if ismakie(Plotter)
sleep(0.02)
end
end

tsol = solve(sys; inival = 0, times = (0, tend), post = plot_timestep, verbose = verbose, Δu_opt = 1.0e-5,
method_linear=KLUFactorization())

if diffeq
inival=unknowns(sys,inival=0)
problem = ODEProblem(sys,inival,(0,tend))
## use fixed timesteps just for the purpose of CI
odesol = solve(problem,Rosenbrock23(), initializealg=NoInit(), dt=1.0e-2, adaptive=false)
tsol=reshape(odesol,sys)
else
tsol = solve(sys; inival = 0, times = (0, tend),
verbose, Δu_opt = 1.0e-5,
method_linear=KLUFactorization())
end

testval = 0.0
for i=2:length(tsol.t)
ui=view(tsol,2,:,i)
Δt=tsol.t[i]-tsol.t[i-1]
testval+=sum(view(ui,subgrid2))*Δt
end

if !isnothing(Plotter)
for i=2:length(tsol.t)
plot_timestep(tsol.u[i],tsol.t[i])
end
end
return testval
end

using Test

function runtests()
testval = 0.359448515181824
testval = 0.06922262169719146
testvaldiffeq = 0.06889809741891571
@test main(; unknown_storage = :sparse, rely_on_corrections = false, assembly = :edgewise) testval
@test main(; unknown_storage = :dense, rely_on_corrections = false, assembly = :edgewise) testval
@test main(; unknown_storage = :sparse, rely_on_corrections = true, assembly = :edgewise) testval
Expand All @@ -143,6 +169,17 @@ function runtests()
@test main(; unknown_storage = :dense, rely_on_corrections = false, assembly = :cellwise) testval
@test main(; unknown_storage = :sparse, rely_on_corrections = true, assembly = :cellwise) testval
@test main(; unknown_storage = :dense, rely_on_corrections = true, assembly = :cellwise) testval


@test main(; diffeq=true, unknown_storage = :sparse, rely_on_corrections = false, assembly = :edgewise) testvaldiffeq
@test main(; diffeq=true, unknown_storage = :dense, rely_on_corrections = false, assembly = :edgewise) testvaldiffeq
@test main(; diffeq=true, unknown_storage = :sparse, rely_on_corrections = true, assembly = :edgewise) testvaldiffeq
@test main(; diffeq=true, unknown_storage = :dense, rely_on_corrections = true, assembly = :edgewise) testvaldiffeq
@test main(; diffeq=true, unknown_storage = :sparse, rely_on_corrections = false, assembly = :cellwise) testvaldiffeq
@test main(; diffeq=true, unknown_storage = :dense, rely_on_corrections = false, assembly = :cellwise) testvaldiffeq
@test main(; diffeq=true, unknown_storage = :sparse, rely_on_corrections = true, assembly = :cellwise) testvaldiffeq
@test main(; diffeq=true, unknown_storage = :dense, rely_on_corrections = true, assembly = :cellwise) testvaldiffeq

end

end
2 changes: 1 addition & 1 deletion src/VoronoiFVM.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ using ExtendableSparse: ExtendableSparse, BlockPreconditioner,
PointBlockILUZeroPreconditioner, factorize!, flush!,
nnz, rawupdateindex!, sparse, updateindex!, nnznew

using ForwardDiff: ForwardDiff
using ForwardDiff: ForwardDiff, value
using GridVisualize: GridVisualize, GridVisualizer
using InteractiveUtils: InteractiveUtils
using JLD2: JLD2, jldopen
Expand Down
14 changes: 4 additions & 10 deletions src/vfvm_assembly.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
##################################################################
"""
$(SIGNATURES)

Extract value from dual number. Use to debug physics callbacks.
Re-exported from ForwardDiff.jl
"""
const value = ForwardDiff.value

"""
$(SIGNATURES)
Expand Down Expand Up @@ -559,8 +553,8 @@ function _eval_and_assemble_generic_operator(system::AbstractSystem, matrix, U,
return
end
generic_operator(f, u) = system.physics.generic_operator(f, u, system)
vecF = values(F)
vecU = values(U)
vecF = dofs(F)
vecU = dofs(U)
y = similar(vecF)
generic_operator(y, vecU)
vecF .+= y
Expand All @@ -583,8 +577,8 @@ function _eval_generic_operator(system::AbstractSystem, U, F)
return
end
generic_operator(f, u) = system.physics.generic_operator(f, u, system)
vecF = values(F)
vecU = values(U)
vecF = dofs(F)
vecU = dofs(U)
y = similar(vecF)
generic_operator(y, vecU)
vecF .+= y
Expand Down
Loading

2 comments on commit c83220b

@j-fu
Copy link
Member Author

@j-fu j-fu commented on c83220b Oct 12, 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/117148

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 v2.0.2 -m "<description of version>" c83220bd39e0aee0015dd439ccb617e3b801bf32
git push origin v2.0.2

Please sign in to comment.