|
| 1 | +module test_cachesol |
| 2 | +using ExtendableGrids |
| 3 | +using VoronoiFVM |
| 4 | +using LinearSolve |
| 5 | +using ExtendableSparse |
| 6 | +using LinearAlgebra |
| 7 | +using SparseArrays |
| 8 | +using Test |
| 9 | + |
| 10 | +function flux!(f, u, edge, data) |
| 11 | + f[1] = u[1, 1] - u[1, 2] |
| 12 | + return nothing |
| 13 | +end |
| 14 | + |
| 15 | +function bcondition!(f, u, bnode, data) |
| 16 | + boundary_dirichlet!(f, u, bnode; species = 1, region = 1, value = 0.0) |
| 17 | + boundary_dirichlet!(f, u, bnode; species = 1, region = 2, value = 1.0) |
| 18 | + return nothing |
| 19 | +end |
| 20 | + |
| 21 | +function setup_linear_solve(n) |
| 22 | + # define system, initialize system matrix |
| 23 | + X = linspace(0.0, 1.0, n) |
| 24 | + system = VoronoiFVM.System(X; flux = flux!, bcondition = bcondition!, species = [1]) |
| 25 | + F = unknowns(system; inival = 0.0) |
| 26 | + # assemble system matrix A and the residual F of A*U=RHS in U=0, i.e. |
| 27 | + # F = A*0 - RHS = -RHS |
| 28 | + F, A = VoronoiFVM.evaluate_residual_and_jacobian(system, F) |
| 29 | + |
| 30 | + p = LinearProblem(A, -VoronoiFVM.dofs(F)) |
| 31 | + linear_cache = init(p, LinearSolve.UMFPACKFactorization()) |
| 32 | + |
| 33 | + return X, linear_cache |
| 34 | +end |
| 35 | + |
| 36 | +function solve_problem(cache) |
| 37 | + sol = LinearSolve.solve!(cache) |
| 38 | + return sol |
| 39 | +end |
| 40 | + |
| 41 | +function main(; n = 10) |
| 42 | + X, cache = setup_linear_solve(n) |
| 43 | + approx_sol = solve_problem(cache) |
| 44 | + true_sol = map((x) -> (x), X) |
| 45 | + @test norm(approx_sol .- true_sol, Inf) ≤ 3.0e-16 |
| 46 | + return nothing |
| 47 | +end |
| 48 | + |
| 49 | +function runtests() |
| 50 | + main() |
| 51 | + return nothing |
| 52 | +end |
| 53 | +end |
0 commit comments