-
Notifications
You must be signed in to change notification settings - Fork 0
/
ElemTools.py
473 lines (414 loc) · 18.3 KB
/
ElemTools.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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
# -*- coding: utf-8 -*-
"""
@author: Hugh Bird
@copyright Copyright 2016, Hugh Bird
@lisence: MIT
@status: alpha
"""
import numpy as np
import scipy.sparse as ssparse
import time as python_time
from multiprocessing import Pool
class j_cache:
""" Cache element jacobians and jacobian determinants.
"""
cj = {}
cdet_j = {}
def __init__(self):
pass
@staticmethod
def det_j(elem, eta):
coord = tuple(a for a in eta)
try:
return j_cache.cdet_j[(elem, coord)]
except KeyError:
j_cache._add_to_cache(elem, eta, coord)
return j_cache.cdet_j[(elem, coord)]
@staticmethod
def j(elem, eta):
coord = tuple(a for a in eta)
try:
return j_cache.cj[(elem, coord)]
except KeyError:
j_cache._add_to_cache(elem, eta, coord)
return j_cache.cj[(elem, coord)]
@staticmethod
def _add_to_cache(elem, eta, coord):
j = elem.jacobian(eta)
# Force positive jacobian....
detj = abs(np.linalg.det(elem.jacobian(eta)))
try:
# You might have noted this isn't going to happen right now...
assert(detj > 0)
except AssertionError:
print("\n\n\n")
print("##########################################################")
print("FATAL ERROR!")
print("Encountered bad jacobian! (Negative)")
print("Element type" + str(elem.__class__))
print("Local coordinates are normally: \n")
print(str(elem.node_locals()))
print("Actual node coordinates:\n")
print(str(elem.node_coords()))
print("Resulted in a jacobian matrix:")
print(str(j))
print("With det(J) = " + str(detj))
print("##########################################################")
raise AssertionError
j_cache.cj[(elem, coord)] = j
j_cache.cdet_j[(elem, coord)] = detj
def uv_mtrx(elem, eta):
""" Compute product the outer product of the sf at x"""
feval_local = elem.funcs(eta)
fp = np.outer(feval_local, feval_local)
return fp
def gu_gv_mtrx(elem, eta):
""" Compute grad of element shape functions.
grad(v) (DOT) grad(u) at x
"""
grad = np.linalg.solve(j_cache.j(elem, eta),
elem.der_funcs(eta).transpose())
gd = np.zeros((elem.ndof(), elem.ndof()))
for i in range(0, elem.nd()):
gd += np.outer(grad[i, :], grad[i, :])
#print("DEBUG/ElemTools/gu_gv_mtrx:\tgd = \n" + str(gd))
return gd
def integrate_elem(elem, funct, gauss_ord=None, gauss_mult=1):
""" Integrates f(x) over an element's area.
int(f(x), dA) =~ sum(w_i * f(p_i))\n
Args
elem: An element\n
funct: a function that takes the element and a point as arguments.
returns f(x), be it a matrix or otherwise.\n
gauss_ord: optional (default=element.def_gauss_ord()). Order of
integration to use over element.\n
gauss_mult: Multiple of default gauss num gauss points to use.\n
Returns
integral: weighted sum of funct at different points.\n
"""
# Gen mtrx indexes for nodes
if gauss_ord is None and gauss_mult == 1:
gauss_ord = elem.def_gauss_order()
elif gauss_ord is None:
def smult(inp):
return gauss_mult * inp
gauss_ord = tuple(map(smult, elem.def_gauss_order()))
points, weights = elem.gen_gp(gauss_ord)
ngp = len(weights)
integral = weights[0] * funct(elem, points[0, :])* \
j_cache.det_j(elem, points[0, :])
for i in range(1, ngp):
integral += weights[i] * funct(elem, points[i, :]) * \
j_cache.det_j(elem, points[i, :])
return integral
def elems_2_csc(mesh, funct, node_mapping, gauss_ord=None):
"""Assemble a global matrix for function "funct"
mesh is mesh object.\n
funct is a function(elem, eta) over which to integrate.\n
node_mapping is NodeMapping object.\n \n
Optional:\n
gauss_ord is dictionary. If node tag is not found in dictionary default
gauss order will be used. If no dictionary the all is defualt gauss order.
"""
ticy = python_time.clock()
print("elems_2_csc:\tIntegrating "+funct.__name__+" mesh.")
# Find the number of entries that we'll get from all our element matrices.
numijk = 0
for elem in mesh.elems.values():
numijk += (elem.dnen() * (1 + len(elem.enrichment))) ** 2
# Allocate arrays to store the ijk data before matrix assembly.
row = np.zeros(numijk, dtype=np.int32)
col = np.zeros(numijk, dtype=np.int32)
data = np.zeros(numijk, dtype=np.float64)
position = 0
for eleid, elem in mesh.elems.items():
# Check guass order.
try:
go = gauss_ord[eleid]
intermed = integrate_elem(elem, funct, gauss_ord=go)
except:
intermed = integrate_elem(elem, funct)
# Add to our ijk data.
idxs = node_mapping.tags_to_idxs(elem.elem_node_tag_gen())
ndof = len(idxs)**2
xv, yv = np.meshgrid(idxs, idxs)
xv = np.reshape(xv, -1)
yv = np.reshape(yv, -1)
intermed = np.reshape(intermed, -1)
col[position: position + ndof] = xv
row[position: position + ndof] = yv
data[position: position + ndof] = intermed
position += ndof
glo_ndof = node_mapping.count
mtrx = ssparse.coo_matrix((data, (row, col)), shape=(glo_ndof, glo_ndof))
tot_time = python_time.clock() - ticy
print("elems_2_csc:\tGenerated " +str(mtrx.shape)+" matrix with nnz " +\
str(mtrx.nnz) + " in " + "{:10.4f}".format(tot_time) + " s.")
return mtrx.tocsc()
def elems_2_array(mesh, funct, node_mapping, gauss_ord=None, gauss_mult=1):
"""Assemble a global array for function "funct" from all elems in mesh
mesh is mesh object.\n
funct is a function(elem, eta) over which to integrate.\n
node_mapping is NodeMapping object.\n \n
Optional:\n
gauss_ord is dictionary. If node tag is not found in dictionary default
gauss order will be used. If no dictionary the all is defualt gauss order.
"""
ticy = python_time.clock()
print("elems_2_array:\tIntegrating "+funct.__name__+" mesh.")
vect = np.zeros(node_mapping.count, dtype=np.float64)
for eleid, elem in mesh.elems.items():
# Check guass order.
try:
go = gauss_ord[eleid]
except:
go = elem.def_gauss_order() * gauss_mult
intermed = integrate_elem(elem, funct, gauss_ord = go)
# Add to our ijk data.
idxs = node_mapping.tags_to_idxs(elem.elem_node_tag_gen())
#print("DEBUG/ElemTools/elems_to_array:\tintermed = " + str(intermed))
vect[idxs] += intermed
tot_time = python_time.clock() - ticy
print("elems_2_array:\tGenerated " +str(vect.shape)+" array in "
+ "{:10.4f}".format(tot_time) + " s.")
return vect
## Parallel version not working yet or benchmarked.
#def elems_2_array_p(mesh, funct, node_mapping, gauss_ord=None):
# """Assemble a global array for function "funct" from all elems in mesh
#
# mesh is mesh object.\n
# funct is a function(elem, eta) over which to integrate.\n
# node_mapping is NodeMapping object.\n \n
#
# Optional:\n
# gauss_ord is dictionary. If node tag is not found in dictionary default
# gauss order will be used. If no dictionary the all is defualt gauss order.
# """
# ticy = python_time.clock()
# print("elems_2_array_p:\tIntegrating "+funct.__name__+" mesh.")
#
# vect = np.zeros(node_mapping.count, dtype=np.float64)
# pool = Pool()
# se_args = []
# idx_mapping = []
# # Generate se_args
# for eleid, elem in mesh.elems.items():
# se_args.append((funct, elem))
# idx_mapping.append(node_mapping.tags_to_idxs(elem.elem_node_tag_gen()))
#
# # sols are small numpy arrays that need to be added to the main one.
# solns = pool.map(single_elem_2_array_p, se_args)
#
# for i in range(len(se_args)):
# vect[idx_mapping[i]] += solns[i]
#
# tot_time = python_time.clock() - ticy
# print("elems_2_array_p:\tGenerated " +str(vect.shape)+" array in "
# + "{:10.4f}".format(tot_time) + " s.")
# return vect
def single_elem_2_array_p(one_arg):
"""A function for a single element integration for multiprocessing
"""
elem, funct = one_arg
intermed = integrate_elem(elem, funct)
return intermed
def integrate_edge(elem, edge_nids, funct, gauss_order=None, gauss_mult=1):
""" Integrate over the edge given by edge nids
CONTAINS POTENTIAL BUG!
Currently, all edge_nids must be on the edge of the element or who knows
what will happen. Integration is currently only between adjacent nodes
on an edge, not along the whole edge. This is an area for improvement.
elem: The element we're interested in.\n
edge_nids: integration edge node node ids/ tags. \n
funct: a function(elem, eta) that we want to integrate over.\n
Optional:\n
gauss_order: number of gauss points to use in integration. Defaults to
elem.def_gauss_order()\n
gauss_mult: Multiple of default gauss num gauss points to use.\n
___\n
BUG:\n
An two consecutive nodes on an element can be on the edges of a domain,
but the edge of the element itself may not be. Eg:(See ascii art
in source code)\n
|\n
2\n
| '\n
| '\n
0---1----\n
Both edges 1 & 2 are on boundary, but edge 1-2 is not.
"""
# Gen mtrx indexes for nodes
if gauss_order is None:
gauss_order = max(elem.def_gauss_order()) * gauss_mult
points, weights = elem.gauss_legendre_1D(gauss_order)
ngp = len(weights)
# We write signitures for each edge we've already done, since we'll
# get A-B and B-A otherwise. If B-A is about to be done we'd check if
# A-B is already in the set.
done = set()
# Convert node tags to element local node indexes.
nids = elem.nodes
loc_ids = []
node_locals = elem.node_locals()
for nid in edge_nids:
loc_ids.append(nids.index(nid))
# Now work through node connectivities integrating:
for loc_id in loc_ids:
con = elem.edge_adjacentcy()[loc_id]
for con_id in con:
if con_id in loc_ids and (con_id, loc_id) not in done:
done.add((loc_id, con_id))
#print("DEBUG/ElemTools/integrate_edge:\tElem:"
# + str(elem))
#print("DEBUG/ElemTools/integrate_edge:\tEdge:"
# + str((loc_id, con_id)))
# And we can FINALLY integrate over something.
# Since we don't know what a whole edge is without adding this
# to the elements, we'll just integrate between adjacent nodes
int_vector = np.array(node_locals[con_id]) - \
np.array(node_locals[loc_id])
vec_len = np.linalg.norm(int_vector)
# Weights add up to 2 in 1D Gauss, so /2.0
int_wghts = weights * vec_len / 2.0
int_pts = np.repeat( np.matrix(node_locals[loc_id]+
int_vector/2.0),
len(weights), axis=0) \
+np.outer(points, 0.5*int_vector)
toarr = lambda x: np.squeeze(np.asarray(x))
#print("EDGE INT ON ELEM")
#print(elem.__class__)
#n = elem.node_coords()
#print(n[loc_id])
#print(n[con_id])
#print(int_vector)
todx = lambda eta: np.linalg.norm(
np.matmul(int_vector/vec_len, j_cache.j(elem, eta)))
try:
for i in range(0, ngp):
p = toarr(int_pts[i, :])
integral += int_wghts[i] * funct(elem, p) * todx(p)
except NameError:
p = toarr(int_pts[0, :])
integral = int_wghts[0] * funct(elem, p) * todx(p)
for i in range(1, ngp):
p = toarr(int_pts[i, :])
#print("To dx: " + str(todx(p)))
integral += int_wghts[i] * funct(elem, p) * todx(p)
# If we shouldn't have integrated over this element, we'll return None.
try:
return integral
except:
return None
def edge_2_csc(mesh, edge_physgrp, funct, node_mapping, gauss_ord=None,
gauss_mult=1):
""" Integrates over an edge defined by a group of nodes.
mesh is ElemMesh.\n
edge_physgrp is the name of the physical group representing the boundary
of that we want to integrate over.\n
funct is a function(elem, eta)=matrix that we wish to integrate over.\n
node_mapping is a NodeMapping object that we're using.\n
Optional: gauss_ord={eleid:uint} Allows the gauss order to be individually
set for any element tag. If not in dictionary, defaults to
elem.def_gauss_order()\n
gauss_mult: Multiple of default gauss num gauss points to use.\n
"""
print("edge_2_csc:\tIntegrating "+funct.__name__+" over physical " + \
"group " + edge_physgrp + ".")
ticy = python_time.clock()
# First estimate the number of degrees of freedom we're dealing with.
# This may be an overestimate since some elements will just have
numijk = 0
grp_num = [key for key, value in mesh.phys_group_names.items() \
if value == edge_physgrp][0]
for eleid in mesh.elems_in_physical_groups[grp_num]:
elem = mesh.elems[eleid]
numijk += (elem.dnen() * (1 + len(elem.enrichment))) ** 2
# Allocate arrays to store the ijk data before matrix assembly.
row = np.zeros(numijk, dtype=np.int32)
col = np.zeros(numijk, dtype=np.int32)
data = np.zeros(numijk, dtype=np.float64)
edge_set = set(mesh.nodes_in_physical_groups[grp_num])
position = 0
for eleid in mesh.elems_in_physical_groups[grp_num]:
elem = mesh.elems[eleid]
# Check guass order.
try:
go = gauss_ord[eleid]
except:
go = elem.def_gauss_order() * gauss_mult
# Get edge node tags for this element.
elem_nid_set = set(elem.nodes)
edge_nids = elem_nid_set.intersection(edge_set)
go = max(go)
intermed = integrate_edge(elem, edge_nids, funct, gauss_order=go)
if intermed is not None:
# Add to our ijk data.
idxs = node_mapping.tags_to_idxs(elem.elem_node_tag_gen())
ndof = len(idxs)**2
xv, yv = np.meshgrid(idxs, idxs)
xv = np.reshape(xv, -1)
yv = np.reshape(yv, -1)
intermed = np.reshape(intermed, -1)
col[position : position + ndof] = xv
row[position : position + ndof] = yv
data[position : position + ndof] = intermed
position += ndof
glo_ndof = node_mapping.count
mtrx = ssparse.coo_matrix((data, (row, col)), shape=(glo_ndof, glo_ndof))
tot_time = python_time.clock() - ticy
print("edge_2_csc:\tGenerated " +str(mtrx.shape)+" matrix with nnz " +\
str(mtrx.nnz) + " in " + "{:10.4f}".format(tot_time) + " s.")
return mtrx.tocsc()
def edge_2_array(mesh, edge_physgrp, funct, node_mapping, gauss_ord=None,
gauss_mult=1):
"""Integrates over an edge defined by a group of nodes.
mesh is ElemMesh.\n
edge_physgrp is the name of the physical group representing the boundary
of that we want to integrate over.\n
funct is a function(elem, eta)=array that we wish to integrate over.\n
node_mapping is a NodeMapping object that we're using.\n
Optional: gauss_ord={eleid:uint} Allows the gauss order to be individually
set for any element tag. If not in dictionary, defaults to
elem.def_gauss_order()\n
gauss_mult: Multiple of default gauss num gauss points to use.\n
"""
print("edge_2_array:\tIntegrating "+funct.__name__+" over physical " + \
"group " + edge_physgrp + ".")
ticy = python_time.clock()
grp_num = [key for key, value in mesh.phys_group_names.items() \
if value == edge_physgrp][0]
edge_set = set(mesh.nodes_in_physical_groups[grp_num])
# First estimate the number of degrees of freedom we're dealing with.
# This may be an overestimate since some elements will just have
vect = np.zeros(node_mapping.count, dtype=np.float64)
for eleid in mesh.elems_in_physical_groups[grp_num]:
elem = mesh.elems[eleid]
# Check guass order.
try:
go = gauss_ord[eleid]
except:
go = elem.def_gauss_order() * gauss_mult
# Get edge node tags for this element.
elem_nid_set = set(elem.nodes)
edge_nids = elem_nid_set.intersection(edge_set)
try:
go = gauss_ord[eleid]
intermed = integrate_edge(elem, edge_nids, funct, gauss_order=go)
except:
intermed = integrate_edge(elem, edge_nids, funct,
gauss_mult=gauss_mult)
#print("DEBUG/ElemTools/edge_to_array:\tintermed = " + str(intermed))
if intermed is not None:
# Add to our ind/data.
idxs = node_mapping.tags_to_idxs(elem.elem_node_tag_gen())
vect[idxs] += intermed
tot_time = python_time.clock() - ticy
print("edge_2_array:\tGenerated " + str(vect.shape) +" array in "
+ "{:10.4f}".format(tot_time) + " s.")
#true_idxs = []
#for nid in mesh.nodes_in_physical_groups[grp_num]:
# true_idxs.append(node_mapping.tag_to_idx((nid, 0)))
#true_array = np.zeros(len(vect))
#true_array[true_idxs] = 1
#print(np.hstack((np.matrix(vect).transpose(), np.matrix(true_array).transpose())))
return vect