Skip to content

Commit

Permalink
Merge pull request #127 from j-fu/state-example
Browse files Browse the repository at this point in the history
Example for using multiple states of a system in parallel
  • Loading branch information
j-fu authored Sep 17, 2024
2 parents a062a02 + b8487f0 commit dae2c6b
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 11 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ jobs:
version:
# - '1.9' # Replace this with the minimum Julia version that your package supports. E.g. if your package requires Julia 1.5 or higher, change this to '1.5'.
- '1' # Leave this line unchanged. '1' will automatically expand to the latest stable 1.x release of Julia.
- 'nightly'
# - 'nightly'
- 'pre'
os:
- ubuntu-latest
- windows-latest
Expand All @@ -37,7 +38,7 @@ jobs:
arch: x64
steps:
- uses: actions/checkout@v4
- uses: julia-actions/setup-julia@v1
- uses: julia-actions/setup-julia@v2
with:
version: ${{ matrix.version }}
arch: ${{ matrix.arch }}
Expand All @@ -53,6 +54,8 @@ jobs:
${{ runner.os }}-
- uses: julia-actions/julia-buildpkg@v1
- uses: julia-actions/julia-runtest@v1
# env:
# JULIA_NUM_THREADS: 4
- uses: julia-actions/julia-processcoverage@v1
- uses: codecov/codecov-action@v4
docs:
Expand Down
1 change: 1 addition & 0 deletions docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ AMGCLWrap = "4f76b812-4ba5-496d-b042-d70715554288"
AlgebraicMultigrid = "2169fc97-5a83-5252-b627-83903c6c433c"
CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0"
Catalyst = "479239e8-5488-4da2-87a7-35f2df7eef83"
ChunkSplitters = "ae650224-84b6-46f8-82ea-d812ca08434e"
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
DiffResults = "163ba53b-c6d8-5494-b064-1a9d43ac40c5"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
Expand Down
3 changes: 2 additions & 1 deletion docs/src/solver.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ fixed_timesteps!
VoronoiFVM.SystemState
VoronoiFVM.SystemState(::Type, system::VoronoiFVM.AbstractSystem; data)
VoronoiFVM.SystemState(system::VoronoiFVM.AbstractSystem; data)
VoronoiFVM.solve!(system::VoronoiFVM.SystemState; kwargs...)
VoronoiFVM.solve!(state::VoronoiFVM.SystemState; kwargs...)
Base.similar(state::VoronoiFVM.SystemState; kwargs...)
```

### Linear solver strategies
Expand Down
58 changes: 58 additions & 0 deletions examples/Example440_ParallelState.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#=
# 440: Parallel solves
([source code](@__SOURCE_URL__))
Demonstrate how to solve one system with different data in parallel using the SystemState (new in v2.0).
=#

module Example440_ParallelState


using VoronoiFVM, ExtendableGrids
using GridVisualize
using ChunkSplitters

function flux(y,u,node,data)
y[1]=u[1,1]^2-u[1,2]^2
end

function bcondition(y,u,node,data)
boundary_dirichlet!(y,u,node, species=1, region=1, value=0.1)
boundary_neumann!(y,u,node,species=1,region=2,value=data.influx)
end

function main(;nref=5, Plotter=nothing)
grid=simplexgrid(range(0,1,length=10*2^nref+1))
sys=VoronoiFVM.System(grid;flux,bcondition, species=[1], data=(influx=0.0,))

## Initial state. First solution creates the matrix
state0=VoronoiFVM.SystemState(sys)
sol=solve!(state0;inival=0.1)

## Prepare parameter and result data
influxes=range(0.0,10.0, length=100)
masses=similar(influxes)

## Split the index range in as many chunks as threads
Threads.@threads for indexes in chunks(influxes;n=Threads.nthreads())
## Create a new state sharing the system - one for each chunk
state=similar(state0)
## Solve for all data values in chunk
for iflux in indexes
data=(influx=influxes[iflux],)
sol=solve!(state;data, inival=0.1, verbose="")
masses[iflux]=integrate(sys,sol)[1,1]
end
end
scalarplot(influxes, masses;Plotter, xlabel="influx", ylabel="mass")
sum(masses)
end

using Test
function runtests()
testval=140.79872772042577
@test main() testval
end

end
2 changes: 1 addition & 1 deletion src/VoronoiFVM.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ using RecursiveArrayTools: RecursiveArrayTools, AbstractDiffEqArray
import RecursiveFactorization
using SciMLBase: SciMLBase
using SparseArrays: SparseArrays, SparseMatrixCSC, dropzeros!, nonzeros,
nzrange, spzeros
nzrange, spzeros, issparse
using SparseDiffTools: SparseDiffTools, forwarddiff_color_jacobian!,
matrix_colors
using StaticArrays: StaticArrays, @MVector, @SArray, @SMatrix
Expand Down
5 changes: 1 addition & 4 deletions src/vfvm_solver.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,7 @@ function solve_step!(state,
values(residual))

values(solution) .-= damp * values(update)

# "incremental collection may only sweep so-called young objects"
GC.gc(false)


if state.system.is_linear
converged = true
break
Expand Down
23 changes: 23 additions & 0 deletions src/vfvm_state.jl
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,27 @@ Shortcut for creating state with value type defined by `Tv` type parameter of sy
"""
SystemState(system::AbstractSystem{Tv,Tc,Ti,Tm}; kwargs...) where {Tv,Tc,Ti,Tm} =SystemState(Tv, system; kwargs...)

"""
similar(state; data=state.data)
Create a new state of with the same system, different work arrays, and possibly different data.
The matrix of the new state initially shares the sparsity structure with `state`.
"""
function Base.similar(state::SystemState; data=state.data)
system=state.system
solution=similar(state.solution)
if issparse(state.matrix)
csc=SparseMatrixCSC(state.matrix)
cscnew=SparseMatrixCSC(csc.m, csc.n, csc.colptr, csc.rowval, similar(csc.nzval))
matrix=ExtendableSparseMatrix(cscnew)
else
matrix=similar(state.matrix)
end
dudp=similar(state.dudp)
residual=similar(state.residual)
update=similar(state.update)
linear_cache=nothing
uhash=zero(UInt64)
history=nothing
SystemState(system, data, solution, matrix, dudp, residual, update, linear_cache, uhash, history)
end
1 change: 1 addition & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ AlgebraicMultigrid = "2169fc97-5a83-5252-b627-83903c6c433c"
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0"
Catalyst = "479239e8-5488-4da2-87a7-35f2df7eef83"
ChunkSplitters = "ae650224-84b6-46f8-82ea-d812ca08434e"
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
DiffResults = "163ba53b-c6d8-5494-b064-1a9d43ac40c5"
ExampleJuggler = "3bbe58f8-ed81-4c4e-a134-03e85fcf4a1a"
Expand Down
10 changes: 7 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,13 @@ function run_all_tests(; run_notebooks = false, notebooksonly = false)
end

# Don't run notebooks on 1.12: https://github.com/fonsp/Pluto.jl/issues/2939
run_all_tests(; run_notebooks = VERSION < v"1.12.0-DEV.0" , notebooksonly = false)
#run_all_tests(; run_notebooks=true, notebooksonly = true)

if haskey(ENV,"EXAMPLES_ONLY")
run_all_tests(; run_notebooks = false, notebooksonly = false)
elseif haskey(ENV,"NOTEBOOKS_ONLY")
run_all_tests(; run_notebooks=true, notebooksonly = true)
else
run_all_tests(; run_notebooks = VERSION < v"1.12.0-DEV.0" , notebooksonly = false)
end



Expand Down

2 comments on commit dae2c6b

@j-fu
Copy link
Member Author

@j-fu j-fu commented on dae2c6b Sep 18, 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/115396

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.0 -m "<description of version>" dae2c6b4438191305c56a7157a8f31752f2c9ebd
git push origin v2.0.0

Please sign in to comment.