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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197 | # Authors:
# Loic Gouarin <loic.gouarin@cmap.polytechnique.fr>
# Nicole Spillane <nicole.spillane@cmap.polytechnique.fr>
#
# License: BSD 3 clause
from __future__ import print_function, division
import numpy as np
from six.moves import range
from petsc4py import PETSc
from . import matelem_cython
from .matelem import getMatElemMass
def getMatElemElasticity(h, lamb, mu):
"""
return the elementary matrix of the elasticity operator
discretized using Q1 finite element (works in 2d and 3d)
Parameters
==========
h : list
The space step in each direction.
lamb: double or ndarray
Lame constant. If lamb is an array, the coefficients are
the constant values of this lame constant on each cell.
mu: double or array
Lame constant. If mu is an array, the coefficients are
the constant values of this lame constant on each cell.
Returns
=======
out: ndarray
The elementary matrix of the elasticity operator.
"""
dim = len(h)
if dim == 2:
hx, hy = h
if isinstance(lamb, np.ndarray):
return matelem_cython.getMatElemElasticity_2d_nonconst(hx, hy, lamb, mu)
else:
return matelem_cython.getMatElemElasticity_2d_const(hx, hy, lamb, mu)
elif dim == 3:
hx, hy, hz = h
if isinstance(lamb, np.ndarray):
return matelem_cython.getMatElemElasticity_3d_nonconst(hx, hy, hz, lamb, mu)
else:
return matelem_cython.getMatElemElasticity_3d_const(hx, hy, hz, lamb, mu)
def getIndices(elem, dof):
"""
Return matrix indices for each dof for a given element
using the PETSc numbering.
Parameters
==========
elem : ndarray
The list of the mesh elements.
dof : int
Number of dof (2 in 2d and 3 in 3d).
Returns
=======
ind : ndarray
The list of the entries in the matrix.
"""
ind = np.empty(dof*elem.size, dtype=np.int32)
for i in range(dof):
ind[i::dof] = dof*elem + i
return ind
def buildElasticityMatrix(da, h, lamb, mu):
"""
Assemble the matrix of the elasticity operator
using Q1 finite elements.
Parameters
==========
da : petsc.DMDA
The mesh structure.
h : list
The space step in each direction.
lamb: double or ndarray
Lame constant. If lamb is an array, the coefficients are
the constant values of this lame constant on each cell.
mu: double or array
Lame constant. If mu is an array, the coefficients are
the constant values of this lame constant on each cell.
Returns
=======
A: petsc.Mat
The matrix of the elasticity operator.
"""
Melem = getMatElemElasticity(h, lamb, mu)
A = da.createMatrix()
elem = da.getElements()
ie = 0
dof = da.getDof()
for e in elem:
ind = getIndices(e, dof)
if isinstance(lamb, np.ndarray):
Melem_num = Melem[ie]
else:
Melem_num = Melem
A.setValuesLocal(ind, ind, Melem_num, PETSc.InsertMode.ADD_VALUES)
ie += 1
return A
def buildIdentityMatrix(da):
"""
Assemble the matrix of the identity operator
using Q1 finite elements.
Parameters
==========
da : petsc.DMDA
The mesh structure.
Returns
=======
A: petsc.Mat
The identity matrix.
"""
A = da.createMatrix()
elem = da.getElements()
dof = da.getDof()
if da.getDim() == 2:
Melem = np.eye(8)
else:
Melem = np.eye(24)
for e in elem:
ind = getIndices(e, dof)
A.setValuesLocal(ind, ind, Melem, PETSc.InsertMode.INSERT_VALUES)
return A
def buildMassMatrix(da, h):
"""
Assemble the mass matrix using Q1 finite elements.
Parameters
==========
da : petsc.DMDA
The mesh structure.
h : list
The space step in each direction.
Returns
=======
A: petsc.Mat
The mass matrix.
"""
Melem = getMatElemMass(da.getDim())
if da.getDim() == 2:
Melem = Melem(h[0], h[1], 0)
else:
Melem = Melem(h[0], h[1], h[2])
A = da.createMatrix()
elem = da.getElements()
dof = da.getDof()
for e in elem:
ind = dof*e
for i in range(dof):
A.setValuesLocal(ind+i, ind+i, Melem, PETSc.InsertMode.ADD_VALUES)
return A
|