From 456387681131b93aeb3d63b866d4c1c2073e2758 Mon Sep 17 00:00:00 2001 From: JordiManyer Date: Thu, 12 Dec 2024 08:54:53 +1100 Subject: [PATCH] Activated tests --- src/BlockSolvers/StaggeredFEOperators.jl | 3 +- .../BlockSolvers/StaggeredFEOperatorsTests.jl | 136 ++++++++++-------- .../mpi/StaggeredFEOperatorsTests.jl | 9 ++ .../seq/StaggeredFEOperatorsTests.jl | 7 + test/BlockSolvers/seq/runtests.jl | 1 + 5 files changed, 91 insertions(+), 65 deletions(-) create mode 100644 test/BlockSolvers/mpi/StaggeredFEOperatorsTests.jl create mode 100644 test/BlockSolvers/seq/StaggeredFEOperatorsTests.jl diff --git a/src/BlockSolvers/StaggeredFEOperators.jl b/src/BlockSolvers/StaggeredFEOperators.jl index 46765e4b..0ba7676e 100644 --- a/src/BlockSolvers/StaggeredFEOperators.jl +++ b/src/BlockSolvers/StaggeredFEOperators.jl @@ -210,8 +210,7 @@ function get_operator!(op_k::AffineFEOperator, op::StaggeredAffineFEOperator{NB} A, b = get_matrix(op_k), get_vector(op_k) a(uk,vk) = op.biforms[k](xhs,uk,vk) l(vk) = op.liforms[k](xhs,vk) - uhd = zero(op.trials[k]) - assemble_matrix_and_vector!(a,l,A,b,op.assems[k],op.trials[k],op.tests[k],uhd) + assemble_matrix_and_vector!(a,l,A,b,op.assems[k],op.trials[k],op.tests[k],zero(op.trials[k])) return op_k end diff --git a/test/BlockSolvers/StaggeredFEOperatorsTests.jl b/test/BlockSolvers/StaggeredFEOperatorsTests.jl index c4a8597d..01771498 100644 --- a/test/BlockSolvers/StaggeredFEOperatorsTests.jl +++ b/test/BlockSolvers/StaggeredFEOperatorsTests.jl @@ -7,13 +7,13 @@ using BlockArrays using GridapSolvers using GridapSolvers.BlockSolvers, GridapSolvers.LinearSolvers, GridapSolvers.NonlinearSolvers -function test_solution(xh,sol,X,dΩ) +function test_solution(xh,sol,X,dΩ,verbose) N = length(sol) xh_exact = interpolate(sol,X) for k in 1:N eh_k = xh[k] - xh_exact[k] e_k = sqrt(sum(∫(eh_k * eh_k)dΩ)) - println("Error in field $k: $e_k") + verbose && println("Error in field $k: $e_k") @test e_k < 1e-10 end end @@ -43,7 +43,7 @@ function driver_affine(model,verbose) l3((u1,(u2,u3)),v4) = ∫(sol[4] * (u1 + u2) * v4)dΩ # Define solver: Each block will be solved with a CG solver - lsolver = CGSolver(JacobiLinearSolver();rtol=1.e-10,verbose=verbose) + lsolver = CGSolver(JacobiLinearSolver();rtol=1.e-12,verbose=verbose) solver = StaggeredFESolver(fill(lsolver,3)) # Create operator from full spaces @@ -52,7 +52,7 @@ function driver_affine(model,verbose) Y = MultiFieldFESpace([V,V,V,V];style=mfs) op = StaggeredAffineFEOperator([a1,a2,a3],[l1,l2,l3],X,Y) xh = solve(solver,op) - test_solution(xh,sol,X,dΩ) + test_solution(xh,sol,X,dΩ,verbose) # Create operator from components UB1, VB1 = U1, V @@ -64,72 +64,82 @@ function driver_affine(model,verbose) xh = zero(X) xh, cache = solve!(xh,solver,op); xh, cache = solve!(xh,solver,op,cache); - test_solution(xh,sol,X,dΩ) + test_solution(xh,sol,X,dΩ,verbose) return true end +function test_nonlinear(model,verbose) + order = 1 + reffe = ReferenceFE(lagrangian,Float64,order) + V = FESpace(model,reffe;dirichlet_tags="boundary") + + sol = [x -> x[1], x -> x[2], x -> x[1] + x[2], x -> 2.0*x[1]] + U1 = TrialFESpace(V,sol[1]) + U2 = TrialFESpace(V,sol[2]) + U3 = TrialFESpace(V,sol[3]) + U4 = TrialFESpace(V,sol[4]) + + # Define weakforms + Ω = Triangulation(model) + dΩ = Measure(Ω,4*order) + + F(u::Function) = x -> (u(x) + 1) * u(x) + F(u) = (u + 1) * u + dF(u,du) = 2.0 * u * du + du + + j1((),u1,du1,dv1) = ∫(dF(u1,du1) * dv1)dΩ + r1((),u1,v1) = ∫((F(u1) - F(sol[1])) * v1)dΩ + + j2((u1,),(u2,u3),(du2,du3),(dv2,dv3)) = ∫(u1 * dF(u2,du2) * dv2)dΩ + ∫(dF(u3,du3) * dv3)dΩ + r2((u1,),(u2,u3),(v2,v3)) = ∫(u1 * (F(u2) - F(sol[2])) * v2)dΩ + ∫((F(u3) - F(sol[3])) * v3)dΩ + + j3((u1,(u2,u3)),u4,du4,dv4) = ∫(u3 * dF(u4,du4) * dv4)dΩ + r3((u1,(u2,u3)),u4,v4) = ∫(u3 * (F(u4) - F(sol[4])) * v4)dΩ + + # Define solver: Each block will be solved with a LU solver + lsolver = LUSolver() + nlsolver = NewtonSolver(lsolver;rtol=1.e-10,verbose=verbose) + solver = StaggeredFESolver(fill(nlsolver,3)) + + # Create operator from full spaces + mfs = BlockMultiFieldStyle(3,(1,2,1)) + X = MultiFieldFESpace([U1,U2,U3,U4];style=mfs) + Y = MultiFieldFESpace([V,V,V,V];style=mfs) + op = StaggeredNonlinearFEOperator([r1,r2,r3],[j1,j2,j3],X,Y) + xh = zero(X) + xh, cache = solve!(xh,solver,op); + test_solution(xh,sol,X,dΩ,verbose) + + # Create operator from components + UB1, VB1 = U1, V + UB2, VB2 = MultiFieldFESpace([U2,U3]), MultiFieldFESpace([V,V]) + UB3, VB3 = U4, V + op = StaggeredNonlinearFEOperator([r1,r2,r3],[j1,j2,j3],[UB1,UB2,UB3],[VB1,VB2,VB3]) + + xh = zero(X) + xh, cache = solve!(xh,solver,op,cache); + test_solution(xh,sol,X,dΩ,verbose) + return true +end -############################################################################################ - -np = (2,2) -ranks = DebugArray(LinearIndices((prod(np),))) -verbose = i_am_main(ranks) -model = CartesianDiscreteModel(ranks,np,(0,1,0,1),(4,4)) - -@testset "StaggeredAffineFEOperators" driver_affine(model,verbose) - -model = CartesianDiscreteModel((0,1,0,1),(4,4)) - -order = 1 -reffe = ReferenceFE(lagrangian,Float64,order) -V = FESpace(model,reffe;dirichlet_tags="boundary") - -sol = [x -> x[1], x -> x[2], x -> x[1] + x[2], x -> x[1] - x[2]] -U1 = TrialFESpace(V,sol[1]) -U2 = TrialFESpace(V,sol[2]) -U3 = TrialFESpace(V,sol[3]) -U4 = TrialFESpace(V,sol[4]) - -# Define weakforms -Ω = Triangulation(model) -dΩ = Measure(Ω,4*order) - -F(u::Function) = x -> (u(x) + 1) * u(x) -F(u) = (u + 1) * u -dF(u,du) = 2.0 * u * du + du - -j1((),u1,du1,dv1) = ∫(dF(u1,du1) * dv1)dΩ -r1((),u1,v1) = ∫((F(u1) - F(sol[1])) * v1)dΩ - -j2((u1,),(u2,u3),(du2,du3),(dv2,dv3)) = ∫(u1 * dF(u2,du2) * dv2)dΩ + ∫(dF(u3,du3) * dv3)dΩ -r2((u1,),(u2,u3),(v2,v3)) = ∫(u1 * (F(u2) - F(sol[2])) * v2)dΩ + ∫((F(u3) - F(sol[3])) * v3)dΩ - -j3((u1,(u2,u3)),u4,du4,dv4) = ∫(u3 * dF(u4,du4) * dv4)dΩ -r3((u1,(u2,u3)),u4,v4) = ∫(u3 * (F(u4) - F(sol[4])) * v4)dΩ - -# Define solver: Each block will be solved with a LU solver -lsolver = LUSolver() -nlsolver = NewtonSolver(lsolver;rtol=1.e-10,verbose=verbose) -solver = StaggeredFESolver(fill(nlsolver,3)) - -# Create operator from full spaces -mfs = BlockMultiFieldStyle(3,(1,2,1)) -X = MultiFieldFESpace([U1,U2,U3,U4];style=mfs) -Y = MultiFieldFESpace([V,V,V,V];style=mfs) -op = StaggeredNonlinearFEOperator([r1,r2,r3],[j1,j2,j3],X,Y) -xh = solve(solver,op) -test_solution(xh,sol,X,dΩ) +function driver(model,verbose) + @testset "StaggeredAffineFEOperators" driver_affine(model,verbose) + @testset "StaggeredNonlinearFEOperators" driver_affine(model,verbose) +end -# Create operator from components -UB1, VB1 = U1, V -UB2, VB2 = MultiFieldFESpace([U2,U3]), MultiFieldFESpace([V,V]) -UB3, VB3 = U4, V -op = StaggeredNonlinearFEOperator([r1,r2,r3],[j1,j2,j3],[UB1,UB2,UB3],[VB1,VB2,VB3]) +# Distributed +function main(distribute,parts) + ranks = distribute(LinearIndices((prod(parts),))) + model = CartesianDiscreteModel(ranks,parts,(0,1,0,1),(8,8)) + driver(model,false) +end -xh = zero(X) -xh, cache = solve!(xh,solver,op); -test_solution(xh,sol,X,dΩ) +# Serial +function main() + model = CartesianDiscreteModel((0,1,0,1),(8,8)) + driver(model,false) +end end # module \ No newline at end of file diff --git a/test/BlockSolvers/mpi/StaggeredFEOperatorsTests.jl b/test/BlockSolvers/mpi/StaggeredFEOperatorsTests.jl new file mode 100644 index 00000000..97ac0bce --- /dev/null +++ b/test/BlockSolvers/mpi/StaggeredFEOperatorsTests.jl @@ -0,0 +1,9 @@ +module StaggeredFEOperatorsMPITests +using MPI, PartitionedArrays +include("../StaggeredFEOperatorsTests.jl") + +with_mpi() do distribute + StaggeredFEOperatorsTests.main(distribute,(2,2)) +end + +end \ No newline at end of file diff --git a/test/BlockSolvers/seq/StaggeredFEOperatorsTests.jl b/test/BlockSolvers/seq/StaggeredFEOperatorsTests.jl new file mode 100644 index 00000000..df49be90 --- /dev/null +++ b/test/BlockSolvers/seq/StaggeredFEOperatorsTests.jl @@ -0,0 +1,7 @@ +module StaggeredFEOperatorsSequentialTests +using PartitionedArrays +include("../StaggeredFEOperatorsTests.jl") + +StaggeredFEOperatorsTests.main() + +end \ No newline at end of file diff --git a/test/BlockSolvers/seq/runtests.jl b/test/BlockSolvers/seq/runtests.jl index 593d9768..30bb9c78 100644 --- a/test/BlockSolvers/seq/runtests.jl +++ b/test/BlockSolvers/seq/runtests.jl @@ -2,3 +2,4 @@ using Test @testset "BlockDiagonalSolvers" begin include("BlockDiagonalSolversTests.jl") end @testset "BlockTriangularSolvers" begin include("BlockTriangularSolversTests.jl") end +@testset "StaggeredFEOperators" begin include("StaggeredFEOperatorsTests.jl") end