-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathPoissonDriver.jl
123 lines (110 loc) · 3.11 KB
/
PoissonDriver.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
module PoissonDriver
using Test
using Gridap
using Gridap.FESpaces
using GridapPETSc
using GridapPETSc: PetscScalar, PetscInt
using SparseArrays
using SparseMatricesCSR
tol = 1e-10
maxits = 1000
options = [
"-ksp_type", "cg",
"-ksp_monitor",
"-ksp_rtol", "$tol",
"-ksp_converged_reason",
"-ksp_max_it", "$maxits",
"-ksp_norm_type", "unpreconditioned",
"-ksp_view",
"-pc_type","gamg",
"-pc_gamg_type","agg",
"-mg_levels_esteig_ksp_type","cg",
"-mg_coarse_sub_pc_type","cholesky",
"-mg_coarse_sub_pc_factor_mat_ordering_type","nd",
"-pc_gamg_process_eq_limit","50",
"-pc_gamg_square_graph","0",
"-pc_gamg_agg_nsmooths","1"]
# Safely open and close PETSc
GridapPETSc.with(args=options) do
domain = (0,1,0,1,0,1)
cells = (10,10,10)
model = CartesianDiscreteModel(domain,cells)
order = 1
reffe = ReferenceFE(lagrangian,Float64,order)
V = TestFESpace(model,reffe,dirichlet_tags="boundary",vector_type=Vector{PetscScalar})
U = TrialFESpace(V)
Ω = Triangulation(model)
dΩ = Measure(Ω,2*order)
f(x) = x[1]*x[2]
a(u,v) = ∫( ∇(v)⋅∇(u) )*dΩ
l(v) = ∫( v*f )*dΩ
solver = LinearFESolver(PETScLinearSolver())
# Assembling on a Julia matrix
# with the same data layout as petsc
# (efficient use of PETScLinearSolver)
Tm = SparseMatrixCSR{0,PetscScalar,PetscInt}
Tv = Vector{PetscScalar}
Tx = get_vector_type(U)
assem = SparseMatrixAssembler(Tm,Tv,U,V)
op = AffineFEOperator(a,l,U,V,assem)
uh = solve(solver,op)
x = get_free_dof_values(uh)
A = get_matrix(op)
b = get_vector(op)
@test typeof(A) == Tm
@test typeof(b) == Tv
@test typeof(x) == Tx
r = A*x - b
@test maximum(abs.(r)) < tol
# Assembling on a Julia matrix
# with different data layout than petsc
# (inefficient use of PETScLinearSolver)
Tm = SparseMatrixCSC{Float64,Int}
Tv = Vector{Float64}
Tx = get_vector_type(U)
assem = SparseMatrixAssembler(Tm,Tv,U,V)
op = AffineFEOperator(a,l,U,V,assem)
uh = solve(solver,op)
x = get_free_dof_values(uh)
A = get_matrix(op)
b = get_vector(op)
@test typeof(A) == Tm
@test typeof(b) == Tv
@test typeof(x) == Tx
r = A*x - b
@test maximum(abs.(r)) < tol
# Now assemble on a native PETScMarix but on a Julia Vector
# with same memory layout as PETScVector
# (efficient use of PETScLinearSolver)
Tm = PETScMatrix
Tv = Vector{PetscScalar}
Tx = get_vector_type(U)
assem = SparseMatrixAssembler(Tm,Tv,U,V)
op = AffineFEOperator(a,l,U,V,assem)
uh = solve(solver,op)
x = get_free_dof_values(uh)
A = get_matrix(op)
b = get_vector(op)
@test typeof(A) == Tm
@test typeof(b) == Tv
@test typeof(x) == Tx
r = A*x - b
@test maximum(abs.(r)) < tol
# Now assemble on a native PETScMarix and on a native PETScVector
# (efficient use of PETScLinearSolver)
Tm = PETScMatrix
Tv = PETScVector
Tx = get_vector_type(U)
assem = SparseMatrixAssembler(Tm,Tv,U,V)
op = AffineFEOperator(a,l,U,V,assem)
uh = solve(solver,op)
x = get_free_dof_values(uh)
A = get_matrix(op)
b = get_vector(op)
@test typeof(A) == Tm
@test typeof(b) == Tv
@test typeof(x) == Tx
r = A*x - b
@test maximum(abs.(r)) < tol
end
end # module