-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntegrators.py
More file actions
252 lines (211 loc) · 9.34 KB
/
Integrators.py
File metadata and controls
252 lines (211 loc) · 9.34 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import mfem.par as mfem
import numpy as np
SQRT2 = np.sqrt(2.0)
# This integrates the K matrix in the documentation.
class ElasticIntegrator(mfem.BilinearFormIntegrator):
def __init__(
self,
elasticity_tensor
):
super().__init__()
self.elasticity_tensor = elasticity_tensor
def AssembleElementMatrix(
self,
el,
Trans,
elmat
):
num_dofs = el.GetDof()
num_dims = el.GetDim()
elmat.SetSize(num_dofs * num_dims)
elmat.Assign(0.0)
dshape = mfem.DenseMatrix(num_dofs, num_dims)
# Need to get the max polynomial order for the quadrature rule.
# This rule assumes that the elasticity tensor is constant over the
# element.
# If so, the rule should be exact; otherwise, it is approximate.
if el.Space() == mfem.FunctionSpace.Pk:
integration_order = 2*el.GetOrder() - 2 + Trans.OrderW()
else:
integration_order = 2*el.GetOrder() + el.GetDim() - 1 + Trans.OrderW()
int_rule = mfem.IntRules.Get(el.GetGeomType(), integration_order)
for ip in range(int_rule.GetNPoints()):
int_point = int_rule.IntPoint(ip)
Trans.SetIntPoint(int_point)
weight = Trans.Weight() * int_point.weight
el.CalcPhysDShape(Trans, dshape)
x = Trans.Transform(int_point)
for idim in range(num_dims):
for jdim in range(num_dims):
for idof in range(num_dofs):
for jdof in range(num_dofs):
# The vector FES simply comprises num_dims copies
# of a scalar FES,
# which has num_dofs DoFs in this cell.
strain = mfem.DenseMatrix(num_dims)
strain.Assign(0.0)
for k in range(num_dims):
strain[jdim, k] += 0.5 * dshape[jdof, k]
strain[k, jdim] += 0.5 * dshape[jdof, k]
stress_tensor = self.elasticity_tensor.calcContraction(x, strain)
ii = idim*num_dofs + idof
jj = jdim*num_dofs + jdof
for k in range(num_dims):
elmat[ii, jj] += weight * stress_tensor[k,idim] * dshape[idof,k]
# This integrates the G matrix in the documentation,
# which is generally not square.
class InelasticIntegrator(mfem.BilinearFormIntegrator):
def __init__(
self,
elasticity_tensor
):
super().__init__()
self.elasticity_tensor = elasticity_tensor
# The trial finite element space comprises symmetric matrices
# whereas the test finite element space comprises vectors.
# The entries of the trial space are stored in a flattened vector.
# In 1D, it's just a scalar.
# In 2D, store as [e_xx, e_yy, sqrt(2)*e_xy].
# In 3D, store as [e_xx, e_yy, e_zz, sqrt(2)*e_xy, sqrt(2)*e_yz, sqrt(2)*e_xz].
def AssembleElementMatrix2(
self,
trial_fe,
test_fe,
Trans,
elmat
):
space_dims = trial_fe.GetDim()
trial_num_dofs = trial_fe.GetDof()
# trial_num_dims = trial_fe.GetVDim()
trial_num_dims = space_dims * (space_dims+1) // 2
test_num_dofs = test_fe.GetDof()
test_num_dims = space_dims
elmat.SetSize(test_num_dofs*test_num_dims, trial_num_dofs*trial_num_dims)
elmat.Assign(0.0)
# Gradients of the trial space functions
trial_dshape = mfem.DenseMatrix(trial_num_dofs, space_dims)
# Test space functions
test_shapef = mfem.Vector(test_num_dofs)
# Need to get the max polynomial order for the quadrature rule.
# This rule assumes that the elasticity tensor is constant over the
# element.
# If so, the rule should be exact; otherwise, it is approximate.
if test_fe.Space() == mfem.FunctionSpace.Pk:
integration_order = test_fe.GetOrder() + trial_fe.GetOrder() - 1 + Trans.OrderW()
else:
integration_order = test_fe.GetOrder() + trial_fe.GetOrder() + space_dims + Trans.OrderW()
int_rule = mfem.IntRules.Get(trial_fe.GetGeomType(), integration_order)
nabla_C_e = mfem.Vector(space_dims)
# Given that we're looking at the i-th component of the trial function,
# (which, again, is a flattened vector containing symmetric matrices),
# nonzero_e_entries[i] contains the two nonzero matrix elements.
# For diagonal elements, repeat the element indices.
if space_dims == 1:
nonzero_e_entries = np.array([
[ [0,0], [0,0] ]
])
elif space_dims == 2:
nonzero_e_entries = np.array([
[ [0,0], [0,0] ],
[ [1,1], [1,1] ],
[ [0,1], [1,0] ]
])
elif space_dims == 3:
nonzero_e_entries = np.array([
[ [0,0], [0,0] ],
[ [1,1], [1,1] ],
[ [2,2], [2,2] ],
[ [0,1], [1,0] ],
[ [1,2], [2,1] ],
[ [0,2], [2,0] ]
])
for ip in range(int_rule.GetNPoints()):
int_point = int_rule.IntPoint(ip)
Trans.SetIntPoint(int_point)
weight = Trans.Weight() * int_point.weight
trial_fe.CalcPhysDShape(Trans, trial_dshape)
test_fe.CalcPhysShape(Trans, test_shapef)
x = Trans.Transform(int_point)
# TODO optimize this.
# TODO allow for non-constant elasticity tensors.
for idim in range(test_num_dims):
for jdim in range(trial_num_dims):
for idof in range(test_num_dofs):
for jdof in range(trial_num_dofs):
ii = idim*test_num_dofs + idof
jj = jdim*trial_num_dofs + jdof
nabla_C_e.Assign(0.0)
for k in range(space_dims):
for l in range(space_dims):
off_diagonal_multiplier = 1.0
if jdim >= space_dims:
off_diagonal_multiplier = SQRT2
nabla_C_e[k] += off_diagonal_multiplier * 0.5 * trial_dshape[jdof, l] * self.elasticity_tensor.evaluate(
x,
k,
l,
nonzero_e_entries[jdim,0,0],
nonzero_e_entries[jdim,0,1]
)
nabla_C_e[k] += off_diagonal_multiplier * 0.5 * trial_dshape[jdof, l] * self.elasticity_tensor.evaluate(
x,
k,
l,
nonzero_e_entries[jdim,1,0],
nonzero_e_entries[jdim,1,1]
)
elmat[ii, jj] += weight * test_shapef[idof] * nabla_C_e[idim]
# Integrates the F operator in the documentation,
# which is nonlinear.
class CreepStrainRateIntegrator(mfem.NonlinearFormIntegrator):
def __init__(
self,
creep_strain_rate
):
super().__init__()
self.creep_strain_rate = creep_strain_rate
def AssembleElementVector(
self,
el,
Tr,
elfun,
elvect
):
# Use "u" for displacement, "e" for creep strain
num_dofs = el.GetDof()
u_num_dims = el.GetDim()
e_num_dims = u_num_dims * (u_num_dims+1) // 2
assert elfun.Size() == num_dofs*(u_num_dims + e_num_dims)
u_elfun = mfem.Vector(elfun, 0, num_dofs*u_num_dims)
e_elfun = mfem.Vector(elfun, num_dofs*u_num_dims, num_dofs*e_num_dims)
u_elfun_mat = mfem.DenseMatrix(u_elfun.GetData(), num_dofs, u_num_dims)
e_elfun_mat = mfem.DenseMatrix(e_elfun.GetData(), num_dofs, e_num_dims)
elvect.SetSize(elfun.Size())
u_elvect = mfem.Vector(elvect, 0, num_dofs*u_num_dims)
u_elvect.Assign(0.0)
e_elvect = mfem.Vector(elvect, num_dofs*u_num_dims, num_dofs*e_num_dims)
e_elvect_mat = mfem.DenseMatrix(
e_elvect.GetData(),
num_dofs,
e_num_dims
)
e_elvect_mat.Assign(0.0)
e_shapef = mfem.Vector(num_dofs)
F_funval = mfem.Vector(e_num_dims)
# TODO reevaluate the necessary quadrature order.
integration_order = 2*el.GetOrder() + Tr.OrderW()
int_rule = mfem.IntRules.Get(el.GetGeomType(), integration_order)
for ip in range(int_rule.GetNPoints()):
int_point = int_rule.IntPoint(ip)
Tr.SetIntPoint(int_point)
weight = Tr.Weight() * int_point.weight
el.CalcPhysShape(Tr, e_shapef)
# Evaluate creep strain rate at integration point.
self.creep_strain_rate.evaluate(
el,
Tr,
u_elfun_mat,
e_elfun_mat,
F_funval
)
mfem.AddMult_a_VWt(weight, e_shapef, F_funval, e_elvect_mat)