Peano
Loading...
Searching...
No Matches
StencilToElement.py
Go to the documentation of this file.
1import numpy as np
2
3def StencilToElement(stencil, dimensions):
4 if dimensions == 2:
6 elif dimensions == 3:
8 else:
9 raise NotImplementedError
10
12 """!
13 We return a square matrix
14 with 2**Dimensions rows.
15
16 Much of this code is identical to 2d version. We could
17 even reuse this for the 2d version with clever indexing,
18 but it the code is much more readable this way.
19 """
20 # assert stencil.shape == (3,3,3)
21
22 Dimensions = 3
23 TwoPowerD = 2 ** Dimensions
24 result = np.zeros((TwoPowerD, TwoPowerD))
25
26 AAi = 0
27 BBi = 0
28 AAj = 0
29 BBj = 0
30 CCi = 0
31 CCj = 0
32
33 #replicating for dfor2(i) in 3d
34 for iScalar in range(TwoPowerD):
35 i = [0 for _ in range(Dimensions)]
36 i[0] = AAi
37 i[1] = BBi
38 i[2] = CCi
39 AAi = int(not AAi)
40 BBi = int( not( AAi ^ BBi ) )
41 CCi = int( CCi or ( not(AAi) and not(BBi) and not(CCi) ) )
42
43 for jScalar in range(TwoPowerD):
44 j = [0 for _ in range(Dimensions)]
45 j[0] = AAj
46 j[1] = BBj
47 j[2] = CCj
48 AAj = int(not AAj)
49 BBj = int( not( AAj ^ BBj ) )
50 CCj = int( CCi or ( not(AAj) and not(BBj) and not(CCj) ) )
51
52 stencilEntry = [0 for _ in range(Dimensions)]
53 commonFacesPower2 = 1.0
54
55 for d in range(Dimensions):
56 stencilEntry[d] = i[d] - j[d] + 1
57 if i[d] == j[d]:
58 commonFacesPower2 *= 2.0
59 result[iScalar, jScalar] = stencil.flatten()[ dLinearised(stencilEntry,3, Dimensions) ] / commonFacesPower2
60 return result
61
63 """!
64 This is borrowed from src/toolbox/ElementMatrix.cpp.
65 In particular toolbox::finiteelements::getElementWiseAssemblyMatrix()
66 """
67 #should be an np matrix. check size before proceeding
68 assert stencil.shape == (3,3), "Wrong shape for this dimensions!"
69
70 Dimensions = 2
71 TwoPowerD = 2 ** Dimensions
72 result = np.zeros((TwoPowerD, TwoPowerD))
73
74 AAi = 0
75 BBi = 0
76 AAj = 0
77 BBj = 0
78
79 #replicating dfor2(i)
80 for iScalar in range(TwoPowerD):
81 i = [0 for _ in range(Dimensions)]
82 i[0] = AAi
83 i[1] = BBi
84 AAi = int(not AAi)
85 BBi = int( not( AAi ^ BBi ) )
86
87 #replicating dfor2(j)
88 for jScalar in range(TwoPowerD):
89 j = [0 for _ in range(Dimensions)]
90 j[0] = AAj
91 j[1] = BBj
92 AAj = int(not AAj)
93 BBj = int( not( AAj ^ BBj ) )
94
95 stencilEntry = [0 for _ in range(Dimensions)]
96 commonFacesPower2 = 1.0
97
98 for d in range(Dimensions):
99 stencilEntry[d] = i[d] - j[d] + 1
100 if i[d] == j[d]:
101 commonFacesPower2 *= 2.0
102 result[iScalar, jScalar] = stencil.flatten()[ dLinearised(stencilEntry,3, Dimensions) ] / commonFacesPower2
103 return result
104
105def dLinearised(vector, max, dimensions):
106 result = vector[dimensions - 1]
107 for d in range(dimensions-2, -1, -1): #Dimensions-2 -> 0 inclusive
108 result = result * max + vector[d]
109 return result
convertStencilToElementMatrix3d(stencil)
We return a square matrix with 2**Dimensions rows.
convertStencilToElementMatrix2d(stencil)
This is borrowed from src/toolbox/ElementMatrix.cpp.