Skip to content

Commit

Permalink
WIP: deprecate VoronoiFVM.Physics callbacks without data argument
Browse files Browse the repository at this point in the history
This patch implements a warning that is triggered if the user constructs
a VoronoiFVM.Physics object with a callback function that uses only three or
two arguments, respectively.

All examples were overhauled to comply with the new interface.
Hereby, the data=nothing default has to be set to allow calls with
and without the data argument. This needs to be done to preserve the
current checks for isdata() and hasdata().
The default can be removed once the deprecated code is removed.
  • Loading branch information
pjaap committed Sep 5, 2024
1 parent 600888b commit 46d076a
Show file tree
Hide file tree
Showing 44 changed files with 196 additions and 181 deletions.
10 changes: 5 additions & 5 deletions examples/Example001_Solvers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,25 @@ function main(; n = 10, Plotter = nothing, assembly = :edgwwise, kwargs...)

eps = 1.0e-2

function reaction(f, u, node)
function reaction(f, u, node, data=nothing)
f[1] = u[1]^2
end

function flux(f, u, edge)
function flux(f, u, edge, data=nothing)
f[1] = eps * (u[1, 1]^2 - u[1, 2]^2)
end

function source(f, node)
function source(f, node, data=nothing)
x1 = node[1] - 0.5
x2 = node[2] - 0.5
f[1] = exp(-20.0 * (x1^2 + x2^2))
end

function storage(f, u, node)
function storage(f, u, node, data=nothing)
f[1] = u[1]
end

function bcondition(f, u, node)
function bcondition(f, u, node, data=nothing)
boundary_dirichlet!(f,
u,
node;
Expand Down
14 changes: 7 additions & 7 deletions examples/Example002_EdgeReaction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,18 @@ function main(; nref = 0, dim = 2, Plotter = nothing, verbose = "and", case = :c
i1 = 6
end

function storage!(y, u, node)
function storage!(y, u, node, data=nothing)
y[1] = u[1]
end

function flux!(y, u, edge)
function flux!(y, u, edge, data=nothing)
y[1] = u[1, 1] - u[1, 2]
end

# Three ways to give a constant reaction term. As a consequence,
# these need to yield the same solution.
# 1: classical node reaction, multiplied by control volume size
function reaction!(y, u, node)
function reaction!(y, u, node, data=nothing)
y[1] = -1
end

Expand All @@ -76,7 +76,7 @@ function main(; nref = 0, dim = 2, Plotter = nothing, verbose = "and", case = :c
#
# τ=1/h v= s*h/2d = σ*h^2/2d
#
function edgereaction!(y, u, edge)
function edgereaction!(y, u, edge, data=nothing)
h = meas(edge)
y[1] = -1 * h^2 / (2 * dim)
end
Expand All @@ -88,14 +88,14 @@ function main(; nref = 0, dim = 2, Plotter = nothing, verbose = "and", case = :c
# we had before
ϕ = grid[Coordinates][1, :]

function edgereaction2!(y, u, edge)
function edgereaction2!(y, u, edge, data=nothing)
ϕK = ϕ[edge.node[1]]
ϕL = ϕ[edge.node[2]]
y[1] = -(ϕK - ϕL) * (ϕK - ϕL) / 2
end

if case == :compare_max
function bcondition!(y, u, node)
function bcondition!(y, u, node, data=nothing)
boundary_dirichlet!(y, u, node; species = 1, region = 1, value = 0)
boundary_dirichlet!(y, u, node; species = 1, region = 2, value = 0)
boundary_dirichlet!(y, u, node; species = 1, region = 3, value = 0)
Expand Down Expand Up @@ -130,7 +130,7 @@ function main(; nref = 0, dim = 2, Plotter = nothing, verbose = "and", case = :c
end

if case == :compare_flux
function bcondition2!(y, u, node)
function bcondition2!(y, u, node, data=nothing)
boundary_dirichlet!(y, u, node; species = 1, region = i1, value = 0)
end

Expand Down
2 changes: 1 addition & 1 deletion examples/Example101_Laplace1D.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ function main()

## Flux function which describes the flux
## between neighboring control volumes
function flux!(f, u, edge)
function flux!(f, u, edge, data=nothing)
f[1] = u[1, 1] - u[1, 2]
end

Expand Down
6 changes: 3 additions & 3 deletions examples/Example105_NonlinearPoisson1D.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ function main(; n = 10, Plotter = nothing, verbose = false, unknown_storage = :s

## Flux function which describes the flux
## between neighboring control volumes
function flux!(f, u, edge)
function flux!(f, u, edge, data=nothing)
f[1] = ϵ * (u[1, 1] - u[1, 2])
end

## Source term
function source!(f, node)
function source!(f, node, data=nothing)
if node[1] <= 0.5
f[1] = 1
else
Expand All @@ -52,7 +52,7 @@ function main(; n = 10, Plotter = nothing, verbose = false, unknown_storage = :s
end

## Reaction term
function reaction!(f, u, node)
function reaction!(f, u, node, data=nothing)
f[1] = exp(u[1]) - exp(-u[1])
end

Expand Down
4 changes: 2 additions & 2 deletions examples/Example106_NonlinearDiffusion1D.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ function main(; n = 20, m = 2, Plotter = nothing, verbose = false,

## Flux function which describes the flux
## between neighboring control volumes
function flux!(f, u, edge)
function flux!(f, u, edge, data=nothing)
f[1] = u[1, 1]^m - u[1, 2]^m
end

## Storage term
function storage!(f, u, node)
function storage!(f, u, node, data=nothing)
f[1] = u[1]
end

Expand Down
4 changes: 2 additions & 2 deletions examples/Example107_NonlinearStorage1D.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function main(; n = 20, m = 2.0, Plotter = nothing, verbose = false,

## Flux function which describes the flux
## between neighboring control volumes
function flux!(f, u, edge)
function flux!(f, u, edge, data=nothing)
f[1] = u[1, 1] - u[1, 2]
end

Expand All @@ -49,7 +49,7 @@ function main(; n = 20, m = 2.0, Plotter = nothing, verbose = false,
## Storage term
## This needs to be regularized as its derivative
## at 0 is infinity
function storage!(f, u, node)
function storage!(f, u, node, data=nothing)
f[1] =+ u[1])^(1.0 / m)
end

Expand Down
4 changes: 2 additions & 2 deletions examples/Example108_OrdinaryDiffEq1D.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ function main(; n = 20, m = 2, Plotter = nothing, verbose = false,

## Flux function which describes the flux
## between neighboring control volumes
function flux!(f, u, edge)
function flux!(f, u, edg, data=nothing)
f[1] = u[1, 1]^m - u[1, 2]^m
end

## Storage term
function storage!(f, u, node)
function storage!(f, u, node, data=nothing)
f[1] = u[1]
end

Expand Down
8 changes: 4 additions & 4 deletions examples/Example110_ReactionDiffusion1D_TwoSpecies.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,20 @@ function main(; n = 100, Plotter = nothing, verbose = false, unknown_storage = :
eps::Vector{Float64} = [1.0, 1.0]

physics = VoronoiFVM.Physics(
; reaction = function (f, u, node)
; reaction = function (f, u, node, data=nothing)
f[1] = u[1] * u[2]
f[2] = -u[1] * u[2]
end,
flux = function (f, u, edge)
flux = function (f, u, edge, data=nothing)
nspecies = 2
f[1] = eps[1] * (u[1, 1] - u[1, 2]) *
(0.01 + u[2, 1] + u[2, 2])
f[2] = eps[2] * (u[2, 1] - u[2, 2]) *
(0.01 + u[1, 1] + u[1, 2])
end, source = function (f, node)
end, source = function (f, node, data=nothing)
f[1] = 1.0e-4 * (0.01 + node[1])
f[2] = 1.0e-4 * (0.01 + 1.0 - node[1])
end, storage = function (f, u, node)
end, storage = function (f, u, node, data=nothing)
f[1] = u[1]
f[2] = u[2]
end)
Expand Down
10 changes: 5 additions & 5 deletions examples/Example115_HeterogeneousCatalysis1D.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,19 @@ function main(; n = 10, Plotter = nothing, verbose = false, tend = 1,
## Diffusion flux for species A and B
D_A = 1.0
D_B = 1.0e-2
function flux!(f, u, edge)
function flux!(f, u, edge, data=nothing)
f[iA] = D_A * (u[iA, 1] - u[iA, 2])
f[iB] = D_B * (u[iB, 1] - u[iB, 2])
end

## Storage term of species A and B
function storage!(f, u, node)
function storage!(f, u, node, data=nothing)
f[iA] = u[iA]
f[iB] = u[iB]
end

## Source term for species a around 0.5
function source!(f, node)
function source!(f, node, data=nothing)
x1 = node[1] - 0.5
f[iA] = exp(-100 * x1^2)
end
Expand All @@ -110,7 +110,7 @@ function main(; n = 10, Plotter = nothing, verbose = false, tend = 1,
R_AC(u_A, u_C) = kp_AC * u_A * (1 - u_C) - km_AC * u_C
R_BC(u_B, u_C) = kp_BC * u_B * (1 - u_C) - km_BC * u_C

function breaction!(f, u, node)
function breaction!(f, u, node, data=nothing)
if node.region == 1
f[iA] = S * R_AC(u[iA], u[iC])
f[iB] = S * R_BC(u[iB], u[iC])
Expand All @@ -119,7 +119,7 @@ function main(; n = 10, Plotter = nothing, verbose = false, tend = 1,
end

## This is for the term \partial_t u_C at the boundary
function bstorage!(f, u, node)
function bstorage!(f, u, node, data=nothing)
if node.region == 1
f[iC] = u[iC]
end
Expand Down
12 changes: 6 additions & 6 deletions examples/Example120_ThreeRegions1D.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function main(; n = 30, Plotter = nothing, plot_grid = false, verbose = false,
eps = [1, 1, 1]
k = [1, 1, 1]

function reaction(f, u, node)
function reaction(f, u, node, data=nothing)
if node.region == 1
f[1] = k[1] * u[1]
f[2] = -k[1] * u[1]
Expand All @@ -43,7 +43,7 @@ function main(; n = 30, Plotter = nothing, plot_grid = false, verbose = false,
end
end

function source(f, node)
function source(f, node, data=nothing)
if node.region == 1
f[1] = 1.0e-4 * (3.0 - node[1])
end
Expand All @@ -54,20 +54,20 @@ function main(; n = 30, Plotter = nothing, plot_grid = false, verbose = false,
## 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)
flux = function (f, u, edge, data=nothing)
for i = 1:3
f[i] = eps[i] * (u[i, 1] - u[i, 2])
end
end

storage = function (f, u, node)
storage = function (f, u, node, data=nothing)
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)
flux = function (f, u, edge, data=nothing)
if edge.region == 1
f[1] = eps[1] * (u[1, 1] - u[1, 2])
f[2] = eps[2] * (u[2, 1] - u[2, 2])
Expand All @@ -79,7 +79,7 @@ function main(; n = 30, Plotter = nothing, plot_grid = false, verbose = false,
end
end

storage = function (f, u, node)
storage = function (f, u, node, data=nothing)
if node.region == 1
f[1] = u[1]
f[2] = u[2]
Expand Down
6 changes: 3 additions & 3 deletions examples/Example121_PoissonPointCharge1D.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,16 @@ function main(; nref = 0, Plotter = nothing, verbose = false, unknown_storage =

Q::Float64 = 0.0

function flux!(f, u, edge)
function flux!(f, u, edge, data=nothing)
f[1] = u[1, 1] - u[1, 2]
end
function storage!(f, u, node)
function storage!(f, u, node, data=nothing)
f[1] = u[1]
end

## Define boundary reaction defining charge
## Note that the term is written on the left hand side, therefore the - sign
function breaction!(f, u, node)
function breaction!(f, u, node, data=nothing)
if node.region == 3
f[1] = -Q
end
Expand Down
6 changes: 3 additions & 3 deletions examples/Example125_TestFunctions1D.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ function main(; n = 100, Plotter = nothing, verbose = false, unknown_storage = :
eps::Vector{Float64} = [1, 1.0e-1]

physics = VoronoiFVM.Physics(
; reaction = function (f, u, node)
; reaction = function (f, u, node, data=nothing)
f[1] = 10 * (u[1] - u[2])
f[2] = 10 * (u[2] - u[1])
end, flux = function (f, u, edge)
end, flux = function (f, u, edge, data=nothing)
f[1] = eps[1] * (u[1, 1] - u[1, 2])
f[2] = eps[2] * (u[2, 1] - u[2, 2])
end, storage = function (f, u, node)
end, storage = function (f, u, node, data=nothing)
f[1] = u[1]
f[2] = u[2]
end)
Expand Down
2 changes: 1 addition & 1 deletion examples/Example201_Laplace2D.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import Metis

## Flux function which describes the flux
## between neighboring control volumes
function g!(f, u, edge)
function g!(f, u, edge, data=nothing)
f[1] = u[1, 1] - u[1, 2]
end

Expand Down
Loading

0 comments on commit 46d076a

Please sign in to comment.