-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathmatrix_coefficient.py
78 lines (60 loc) · 2.01 KB
/
matrix_coefficient.py
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
'''
Matrix coefficient example
How to run:
python <arguments>
Example of arguments:
coefficient_example.py
Description: This example code demonstrates how to use matrix coefficient,
which is defiend in Python.
'''
import mfem.ser as mfem
import numpy as np
class sigma(mfem.MatrixPyCoefficient):
def __init__(self, dim):
mfem.MatrixPyCoefficient.__init__(self, dim)
def EvalValue(self, x):
return np.array([[0.1, 0.], [0., 10.]])
# create sample mesh for square shape
mesh = mfem.Mesh(10, 10, "TRIANGLE")
# create finite element function space
fec = mfem.H1_FECollection(1, mesh.Dimension()) # H1 order=1
fespace = mfem.FiniteElementSpace(mesh, fec)
#
ess_tdof_list = mfem.intArray()
ess_bdr = mfem.intArray([1]*mesh.bdr_attributes.Size())
fespace.GetEssentialTrueDofs(ess_bdr, ess_tdof_list)
# constant coefficient
one = mfem.ConstantCoefficient(1.0)
# define Bilinear and Linear operator
a = mfem.BilinearForm(fespace)
a.AddDomainIntegrator(mfem.DiffusionIntegrator(sigma(2)))
a.Assemble()
b = mfem.LinearForm(fespace)
b.AddDomainIntegrator(mfem.DomainLFIntegrator(one))
b.Assemble()
# create gridfunction, which is where the solution vector is stored
x = mfem.GridFunction(fespace);
x.Assign(0.0)
# form linear equation (AX=B)
A = mfem.OperatorPtr()
B = mfem.Vector()
X = mfem.Vector()
a.FormLinearSystem(ess_tdof_list, x, b, A, X, B);
print("Size of linear system: " + str(A.Height()))
# solve it using PCG solver and store the solution to x
AA = mfem.OperatorHandle2SparseMatrix(A)
M = mfem.GSSmoother(AA)
mfem.PCG(AA, M, B, X, 1, 200, 1e-12, 0.0)
a.RecoverFEMSolution(X, b, x)
# extract vertices and solution as numpy array
verts = mesh.GetVertexArray()
sol = x.GetDataArray()
# plot solution using Matplotlib
import matplotlib.pyplot as plt
import matplotlib.tri as tri
triang = tri.Triangulation(verts[:,0], verts[:,1])
fig1, ax1 = plt.subplots()
ax1.set_aspect('equal')
tpc = ax1.tripcolor(triang, sol, shading='gouraud')
fig1.colorbar(tpc)
plt.show()