-
Notifications
You must be signed in to change notification settings - Fork 16
/
gallery.jl
556 lines (538 loc) · 21.7 KB
/
gallery.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
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
"""
laplacian_fdm(
nodes_per_dir,
parts_per_dir,
parts;
index_type = Int64,
value_type = Float64)
Document me!
"""
function laplacian_fdm(
nodes_per_dir,
parts_per_dir,
parts;
index_type::Type{Ti} = Int64,
value_type::Type{Tv} = Float64,
) where {Ti,Tv}
function neig_node(cartesian_node_i,d,i,cartesian_node_to_node)
function is_boundary_node(node_1d,nodes_1d)
!(node_1d in 1:nodes_1d)
end
D = length(nodes_per_dir)
inc = ntuple(k->( k==d ? i : zero(i)),Val{D}())
cartesian_node_j = CartesianIndex(Tuple(cartesian_node_i) .+ inc)
boundary = any(map(is_boundary_node,Tuple(cartesian_node_j),nodes_per_dir))
T = eltype(cartesian_node_to_node)
if boundary
return zero(T)
end
node_j = cartesian_node_to_node[cartesian_node_j]
node_j
end
function setup(nodes,::Type{index_type},::Type{value_type}) where {index_type,value_type}
D = length(nodes_per_dir)
α = value_type(prod(i->(i+1),nodes_per_dir))
node_to_cartesian_node = CartesianIndices(nodes_per_dir)
cartesian_node_to_node = LinearIndices(nodes_per_dir)
first_cartesian_node = node_to_cartesian_node[first(nodes)]
last_cartesian_node = node_to_cartesian_node[last(nodes)]
ranges = map(:,Tuple(first_cartesian_node),Tuple(last_cartesian_node))
cartesian_nodes = CartesianIndices(ranges)
nnz = 0
for cartesian_node_i in cartesian_nodes
nnz+=1
for d in 1:D
for i in (-1,1)
node_j = neig_node(cartesian_node_i,d,i,cartesian_node_to_node)
if node_j == 0
continue
end
nnz+=1
end
end
end
myI = zeros(index_type,nnz)
myJ = zeros(index_type,nnz)
myV = zeros(value_type,nnz)
t = 0
for cartesian_node_i in cartesian_nodes
t += 1
node_i = cartesian_node_to_node[cartesian_node_i]
myI[t] = node_i
myJ[t] = node_i
myV[t] = α*2*D
for d in 1:D
for i in (-1,1)
node_j = neig_node(cartesian_node_i,d,i,cartesian_node_to_node)
if node_j == 0
continue
end
t += 1
myI[t] = node_i
myJ[t] = node_j
myV[t] = -α
end
end
end
myI,myJ,myV
end
node_partition = uniform_partition(parts,parts_per_dir,nodes_per_dir)
I,J,V = map(node_partition) do nodes
setup(nodes,Ti,Tv)
end |> tuple_of_arrays
I,J,V,node_partition,node_partition
end
"""
laplacian_fem(
nodes_per_dir,
parts_per_dir,
parts;
index_type = Int64,
value_type = Float64)
Document me!
"""
function laplacian_fem(
nodes_per_dir, # free (== interior) nodes
parts_per_dir,
parts;
index_type::Type{Ti} = Int64,
value_type::Type{Tv} = Float64,
) where {Ti,Tv}
cells_per_dir = nodes_per_dir .+ 1
function is_boundary_node(node_1d,nodes_1d)
!(node_1d in 1:nodes_1d)
end
function ref_matrix(cartesian_local_nodes,h_per_dir,::Type{value_type}) where value_type
D = ndims(cartesian_local_nodes)
gp_1d = value_type[-sqrt(3)/3,sqrt(3)/3]
sf_1d = zeros(value_type,length(gp_1d),2)
sf_1d[:,1] = 0.5 .* (1 .- gp_1d)
sf_1d[:,2] = 0.5 .* (gp_1d .+ 1)
sg_1d = zeros(value_type,length(gp_1d),2)
sg_1d[:,1] .= - 0.5
sg_1d[:,2] .= 0.5
cartesian_points = CartesianIndices(ntuple(d->length(gp_1d),Val{D}()))
cartesian_local_node_to_local_node = LinearIndices(cartesian_local_nodes)
cartesian_point_to_point = LinearIndices(cartesian_points)
n = 2^D
sg = Matrix{NTuple{D,value_type}}(undef,n,length(gp_1d)^D)
for cartesian_local_node in cartesian_local_nodes
local_node = cartesian_local_node_to_local_node[cartesian_local_node]
local_node_tuple = Tuple(cartesian_local_node)
for cartesian_point in cartesian_points
point = cartesian_point_to_point[cartesian_point]
point_tuple = Tuple(cartesian_point)
v = ntuple(Val{D}()) do d
prod(1:D) do i
if i == d
(2/h_per_dir[d])*sg_1d[local_node_tuple[d],point_tuple[d]]
else
sf_1d[local_node_tuple[i],point_tuple[i]]
end
end
end
sg[local_node,point] = v
end
end
Aref = zeros(value_type,n,n)
dV = prod(h_per_dir)/(2^D)
for i in 1:n
for j in 1:n
for k in 1:n
Aref[i,j] += dV*dot(sg[k,i],sg[k,j])
end
end
end
Aref
end
function setup(cells,::Type{index_type},::Type{value_type}) where {index_type,value_type}
D = length(nodes_per_dir)
h_per_dir = map(i->1/(i+1),nodes_per_dir)
ttt = ntuple(d->2,Val{D}())
cartesian_local_nodes = CartesianIndices(ttt)
Aref = ref_matrix(cartesian_local_nodes,h_per_dir,value_type)#ones(value_type,2^D,2^D)
cell_to_cartesian_cell = CartesianIndices(cells_per_dir)
first_cartesian_cell = cell_to_cartesian_cell[first(cells)]
last_cartesian_cell = cell_to_cartesian_cell[last(cells)]
ranges = map(:,Tuple(first_cartesian_cell),Tuple(last_cartesian_cell))
cartesian_cells = CartesianIndices(ranges)
offset = CartesianIndex(ttt)
cartesian_local_node_to_local_node = LinearIndices(cartesian_local_nodes)
cartesian_node_to_node = LinearIndices(nodes_per_dir)
nnz = 0
for cartesian_cell in cartesian_cells
for cartesian_local_node_i in cartesian_local_nodes
local_node_i = cartesian_local_node_to_local_node[cartesian_local_node_i]
# This is ugly to support Julia 1.6 (idem below)
cartesian_node_i = CartesianIndex(Tuple(cartesian_cell) .+ Tuple(cartesian_local_node_i) .- Tuple(offset))
boundary = any(map(is_boundary_node,Tuple(cartesian_node_i),nodes_per_dir))
if boundary
continue
end
node_i = cartesian_node_to_node[cartesian_node_i]
for cartesian_local_node_j in cartesian_local_nodes
local_node_j = cartesian_local_node_to_local_node[cartesian_local_node_j]
cartesian_node_j = CartesianIndex(Tuple(cartesian_cell) .+ Tuple(cartesian_local_node_j) .- Tuple(offset))
boundary = any(map(is_boundary_node,Tuple(cartesian_node_j),nodes_per_dir))
if boundary
continue
end
node_j = cartesian_node_to_node[cartesian_node_j]
nnz += 1
end
end
end
myI = zeros(index_type,nnz)
myJ = zeros(index_type,nnz)
myV = zeros(value_type,nnz)
k = 0
for cartesian_cell in cartesian_cells
for cartesian_local_node_i in cartesian_local_nodes
local_node_i = cartesian_local_node_to_local_node[cartesian_local_node_i]
cartesian_node_i = CartesianIndex(Tuple(cartesian_cell) .+ Tuple(cartesian_local_node_i) .- Tuple(offset))
boundary = any(map(is_boundary_node,Tuple(cartesian_node_i),nodes_per_dir))
if boundary
continue
end
node_i = cartesian_node_to_node[cartesian_node_i]
for cartesian_local_node_j in cartesian_local_nodes
local_node_j = cartesian_local_node_to_local_node[cartesian_local_node_j]
cartesian_node_j = CartesianIndex(Tuple(cartesian_cell) .+ Tuple(cartesian_local_node_j) .- Tuple(offset))
boundary = any(map(is_boundary_node,Tuple(cartesian_node_j),nodes_per_dir))
if boundary
continue
end
node_j = cartesian_node_to_node[cartesian_node_j]
k += 1
myI[k] = node_i
myJ[k] = node_j
myV[k] = Aref[local_node_i,local_node_j]
end
end
end
myI,myJ,myV
end
node_partition = uniform_partition(parts,parts_per_dir,nodes_per_dir)
cell_partition = uniform_partition(parts,parts_per_dir,cells_per_dir)
I,J,V = map(cell_partition) do cells
setup(cells,Ti,Tv)
end |> tuple_of_arrays
I,J,V,node_partition,node_partition
end
function linear_elasticity_fem(
nodes_per_dir, # free (== interior) nodes
parts_per_dir,
parts,
;
E = 1,
ν = 0.25,
index_type::Type{Ti} = Int64,
value_type::Type{Tv} = Float64,
) where {Ti,Tv}
cells_per_dir = nodes_per_dir .+ 1
function is_boundary_node(node_1d,nodes_1d)
!(node_1d in 1:nodes_1d)
end
function ref_matrix(cartesian_local_nodes,h_per_dir,::Type{value_type}) where value_type
D = ndims(cartesian_local_nodes)
gp_1d = value_type[-sqrt(3)/3,sqrt(3)/3]
sf_1d = zeros(value_type,length(gp_1d),2)
sf_1d[:,1] = 0.5 .* (1 .- gp_1d)
sf_1d[:,2] = 0.5 .* (gp_1d .+ 1)
sg_1d = zeros(value_type,length(gp_1d),2)
sg_1d[:,1] .= - 0.5
sg_1d[:,2] .= 0.5
cartesian_points = CartesianIndices(ntuple(d->length(gp_1d),Val{D}()))
cartesian_local_node_to_local_node = LinearIndices(cartesian_local_nodes)
cartesian_point_to_point = LinearIndices(cartesian_points)
n = 2^D
sg = Matrix{NTuple{D,value_type}}(undef,n,length(gp_1d)^D)
for cartesian_local_node in cartesian_local_nodes
local_node = cartesian_local_node_to_local_node[cartesian_local_node]
local_node_tuple = Tuple(cartesian_local_node)
for cartesian_point in cartesian_points
point = cartesian_point_to_point[cartesian_point]
point_tuple = Tuple(cartesian_point)
v = ntuple(Val{D}()) do d
prod(1:D) do i
if i == d
(2/h_per_dir[d])*sg_1d[local_node_tuple[d],point_tuple[d]]
else
sf_1d[local_node_tuple[i],point_tuple[i]]
end
end
end
sg[local_node,point] = v
end
end
Aref = zeros(value_type,n*D,n*D)
dV = prod(h_per_dir)/(2^D)
ε_i = zeros(value_type,D,D)
ε_j = zeros(value_type,D,D)
λ = (E*ν)/((1+ν)*(1-2*ν))
μ = E/(2*(1+ν))
for i in 1:n
for j in 1:n
for ci in 1:D
for cj in 1:D
idof = (i-1)*D+ci
jdof = (j-1)*D+cj
ε_i .= 0
ε_j .= 0
for k in 1:n
ε_i[ci,:] = collect(sg[k,i])
ε_j[cj,:] = collect(sg[k,j])
ε_i .= 0.5 .* ( ε_i .+ transpose(ε_i))
ε_j .= 0.5 .* ( ε_j .+ transpose(ε_j))
σ_j = λ*tr(ε_j)*one(ε_j) + 2*μ*ε_j
Aref[idof,jdof] += tr(ε_i*σ_j)
end
end
end
end
end
Aref
end
function setup(cells,::Type{index_type},::Type{value_type}) where {index_type,value_type}
D = length(nodes_per_dir)
h_per_dir = map(i->1/(i+1),nodes_per_dir)
ttt = ntuple(d->2,Val{D}())
cartesian_local_nodes = CartesianIndices(ttt)
Aref = ref_matrix(cartesian_local_nodes,h_per_dir,value_type)#ones(value_type,2^D,2^D)
cell_to_cartesian_cell = CartesianIndices(cells_per_dir)
first_cartesian_cell = cell_to_cartesian_cell[first(cells)]
last_cartesian_cell = cell_to_cartesian_cell[last(cells)]
ranges = map(:,Tuple(first_cartesian_cell),Tuple(last_cartesian_cell))
cartesian_cells = CartesianIndices(ranges)
offset = CartesianIndex(ttt)
cartesian_local_node_to_local_node = LinearIndices(cartesian_local_nodes)
cartesian_node_to_node = LinearIndices(nodes_per_dir)
nnz = 0
for cartesian_cell in cartesian_cells
for cartesian_local_node_i in cartesian_local_nodes
local_node_i = cartesian_local_node_to_local_node[cartesian_local_node_i]
# This is ugly to support Julia 1.6 (idem below)
cartesian_node_i = CartesianIndex(Tuple(cartesian_cell) .+ Tuple(cartesian_local_node_i) .- Tuple(offset))
boundary = any(map(is_boundary_node,Tuple(cartesian_node_i),nodes_per_dir))
if boundary
continue
end
node_i = cartesian_node_to_node[cartesian_node_i]
for cartesian_local_node_j in cartesian_local_nodes
local_node_j = cartesian_local_node_to_local_node[cartesian_local_node_j]
cartesian_node_j = CartesianIndex(Tuple(cartesian_cell) .+ Tuple(cartesian_local_node_j) .- Tuple(offset))
boundary = any(map(is_boundary_node,Tuple(cartesian_node_j),nodes_per_dir))
if boundary
continue
end
node_j = cartesian_node_to_node[cartesian_node_j]
nnz += D*D
end
end
end
myI = zeros(index_type,nnz)
myJ = zeros(index_type,nnz)
myV = zeros(value_type,nnz)
k = 0
for cartesian_cell in cartesian_cells
for cartesian_local_node_i in cartesian_local_nodes
local_node_i = cartesian_local_node_to_local_node[cartesian_local_node_i]
cartesian_node_i = CartesianIndex(Tuple(cartesian_cell) .+ Tuple(cartesian_local_node_i) .- Tuple(offset))
boundary = any(map(is_boundary_node,Tuple(cartesian_node_i),nodes_per_dir))
if boundary
continue
end
node_i = cartesian_node_to_node[cartesian_node_i]
for cartesian_local_node_j in cartesian_local_nodes
local_node_j = cartesian_local_node_to_local_node[cartesian_local_node_j]
cartesian_node_j = CartesianIndex(Tuple(cartesian_cell) .+ Tuple(cartesian_local_node_j) .- Tuple(offset))
boundary = any(map(is_boundary_node,Tuple(cartesian_node_j),nodes_per_dir))
if boundary
continue
end
node_j = cartesian_node_to_node[cartesian_node_j]
for ci in 1:D
for cj in 1:D
dof_i = (node_i-1)*D + ci
dof_j = (node_j-1)*D + cj
local_dof_i = (local_node_i-1)*D + ci
local_dof_j = (local_node_j-1)*D + cj
k += 1
myI[k] = dof_i
myJ[k] = dof_j
myV[k] = Aref[local_dof_i,local_dof_j]
end
end
end
end
end
myI,myJ,myV
end
node_partition = uniform_partition(parts,parts_per_dir,nodes_per_dir)
dof_partition = node_to_dof_partition(node_partition,length(nodes_per_dir))
cell_partition = uniform_partition(parts,parts_per_dir,cells_per_dir)
I,J,V = map(cell_partition) do cells
setup(cells,Ti,Tv)
end |> tuple_of_arrays
I,J,V,dof_partition,dof_partition
end
function node_to_dof_partition(node_partition,D)
global_node_to_owner = global_to_owner(node_partition)
dof_partition = map(node_partition) do mynodes
@assert ghost_length(mynodes) == 0
own_to_global_node = own_to_global(mynodes)
n_own_nodes = length(own_to_global_node)
own_to_global_dof = zeros(Int,D*n_own_nodes)
for own_node in 1:n_own_nodes
for ci in 1:D
own_dof = (own_node-1)*D+ci
global_node = own_to_global_node[own_node]
global_dof = (global_node-1)*D+ci
own_to_global_dof[own_dof] = global_dof
end
end
n_global_dofs = global_length(mynodes)*D
owner = part_id(mynodes)
own_dofs = OwnIndices(n_global_dofs,owner,own_to_global_dof)
ghost_dofs = GhostIndices(n_global_dofs,Int[],Int32[])
global_dof_to_owner = global_dof -> begin
global_node = div(global_dof-1,D)+1
global_node_to_owner[global_node]
end
mydofs = OwnAndGhostIndices(own_dofs,ghost_dofs,global_dof_to_owner)
mydofs
end
dof_partition
end
function node_coordinates_unit_cube(
nodes_per_dir, # free (== interior) nodes
parts_per_dir,
parts,
;
split_format = Val(false),
value_type::Type{Tv} = Float64,) where Tv
function setup!(own_x,mynodes)
D = length(nodes_per_dir)
h_per_dir = map(i->1/(i+1),nodes_per_dir)
global_node_to_cartesian_global_node = CartesianIndices(nodes_per_dir)
n_own_nodes = own_length(mynodes)
own_to_global_node = own_to_global(mynodes)
for own_node in 1:n_own_nodes
global_node = own_to_global_node[own_node]
cartesian_global_node = global_node_to_cartesian_global_node[global_node]
xi = Tuple(cartesian_global_node)
own_x[own_node] = h_per_dir .* xi
end
end
node_partition = uniform_partition(parts,parts_per_dir,nodes_per_dir)
T = SVector{length(nodes_per_dir),Tv}
x = pzeros(T,node_partition;split_format)
foreach(setup!,own_values(x),node_partition)
x
end
function near_nullspace_linear_elasticity(a...;b...)
@warn "near_nullspace_linear_elasticity is deprecated, use nullspace_linear_elasticity instead"
nullspace_linear_elasticity(a...;b...)
end
function nullspace_linear_elasticity(x,
row_partition = node_to_dof_partition(partition(axes(x,1)),length(eltype(x)))
)
T = eltype(x)
D = length(T)
Tv = eltype(T)
if D == 1
nb = 1
elseif D==2
nb=3
elseif D == 3
nb = 6
else
error("case not implemented")
end
dof_partition = row_partition
split_format = Val(eltype(partition(x)) <: SplitVector)
B = [ pzeros(Tv,dof_partition;split_format) for _ in 1:nb ]
nullspace_linear_elasticity!(B,x)
end
function nullspace_linear_elasticity!(B,x)
D = length(eltype(x))
if D == 1
foreach(own_values(B[1])) do own_b
fill!(own_b,1)
end
elseif D==2
foreach(own_values(B[1]),own_values(B[2]),own_values(x)) do own_b1,own_b2,own_x
T = eltype(own_b1)
n_own_nodes = length(own_x)
for own_node in 1:n_own_nodes
dof_x1 = (own_node-1)*2 + 1
dof_x2 = (own_node-1)*2 + 2
#
own_b1[dof_x1] = one(T)
own_b1[dof_x2] = zero(T)
#
own_b2[dof_x1] = zero(T)
own_b2[dof_x2] = one(T)
end
end
foreach(own_values(B[3]),own_values(x)) do own_b3,own_x
T = eltype(own_b3)
n_own_nodes = length(own_x)
for own_node in 1:n_own_nodes
x1,x2 = own_x[own_node]
dof_x1 = (own_node-1)*2 + 1
dof_x2 = (own_node-1)*2 + 2
#
own_b3[dof_x1] = -x2
own_b3[dof_x2] = x1
end
end
elseif D == 3
foreach(own_values(B[1]),own_values(B[2]),own_values(B[3]),own_values(x)) do own_b1,own_b2,own_b3,own_x
T = eltype(own_b1)
n_own_nodes = length(own_x)
for own_node in 1:n_own_nodes
dof_x1 = (own_node-1)*3 + 1
dof_x2 = (own_node-1)*3 + 2
dof_x3 = (own_node-1)*3 + 3
#
own_b1[dof_x1] = one(T)
own_b1[dof_x2] = zero(T)
own_b1[dof_x3] = zero(T)
#
own_b2[dof_x1] = zero(T)
own_b2[dof_x2] = one(T)
own_b2[dof_x3] = zero(T)
#
own_b3[dof_x1] = zero(T)
own_b3[dof_x2] = zero(T)
own_b3[dof_x3] = one(T)
end
end
foreach(own_values(B[4]),own_values(B[5]),own_values(B[6]),own_values(x)) do own_b4,own_b5,own_b6,own_x
T = eltype(own_b4)
n_own_nodes = length(own_x)
for own_node in 1:n_own_nodes
x1,x2,x3 = own_x[own_node]
dof_x1 = (own_node-1)*3 + 1
dof_x2 = (own_node-1)*3 + 2
dof_x3 = (own_node-1)*3 + 3
#
own_b4[dof_x1] = -x2
own_b4[dof_x2] = x1
own_b4[dof_x3] = zero(T)
#
own_b5[dof_x1] = zero(T)
own_b5[dof_x2] = -x3
own_b5[dof_x3] = x2
#
own_b6[dof_x1] = x3
own_b6[dof_x2] = zero(T)
own_b6[dof_x3] = -x1
end
end
else
error("case not implemented")
end
B
end