ellipsoid.py 5.27 KB
 Praetorius, Simon committed Apr 15, 2020 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 ``````from sympy import * import numpy as np import collections x, y, z = symbols("x y z") a, b, c = symbols("a b c") # simplify entries of an array def simplify_all(A): if isinstance(A, collections.Iterable): return list(map(lambda a: simplify_all(a), A)) else: return simplify(A) def add(A,B): if isinstance(A, collections.Iterable): return list(map(lambda a,b: add(a,b), A,B)) else: return A + B def negate(A): if isinstance(A, collections.Iterable): return list(map(lambda a: negate(a), A)) else: return -A X = [x,y,z] # normal vector div = sqrt(b**4*c**4*x**2 + a**4*c**4*y**2 + a**4*b**4**z**2) N = [b**2*c**2*x/div, a**2*c**2*y/div, a**2*b**2*z/div] print("N = ") print("return {") for i in range(3): print(" ",ccode(N[i]),",") print("};") print("") # projection P = [[ (1 if i==j else 0) - N[i]*N[j] for j in range(3)] for i in range(3)] # parametrization nrm_X = sqrt(x**2 + y**2 + z**2) X0 = [x/nrm_X, y/nrm_X, z/nrm_X] u = atan(X0[1]/X0[0]) v = acos(X0[2]) X1 = [a*cos(u)*sin(v), b*sin(u)*sin(v), c*cos(v)] # jacobian of parametrization `````` Praetorius, Simon committed Apr 15, 2020 52 ``````J = [[diff(X1[i],X[j]) for i in range(3)] for j in range(3)] `````` Praetorius, Simon committed Apr 15, 2020 53 54 55 56 57 ``````print("J = ") print("return {") for i in range(3): print(" {") for j in range(3): `````` Praetorius, Simon committed Apr 15, 2020 58 `````` print(" ",ccode(simplify(J[i][j])),",") `````` Praetorius, Simon committed Apr 15, 2020 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 `````` print(" },") print("};") print("") # P(F)_j def project1(F): return simplify_all([ np.sum([ P[i][j] * F[i] for i in range(3)]) for j in range(3)]) # P(F)_kl def project2(F): return simplify_all([[ np.sum([np.sum([ P[i][k] * P[j][l] * F[i][j] for j in range(3)]) for i in range(3)]) for l in range(3)] for k in range(3)]) # P(F)_lmn def project3(F): return simplify_all([[[ np.sum([np.sum([np.sum([ P[i][l] * P[j][m] * P[k][n] * F[i][j][k] for k in range(3)]) for j in range(3)]) for i in range(3)]) for n in range(3)] for m in range(3)] for l in range(3)]) # P(F)_mnop def project4(F): return simplify_all([[[[ np.sum([np.sum([np.sum([np.sum([ P[i][m] * P[j][n] * P[k][o] * P[l][p] * F[i][j][k][l] for l in range(3)]) for k in range(3)]) for j in range(3)]) for i in range(3)]) for p in range(3)] for o in range(3)] for n in range(3)] for m in range(3)]) # Euclidean gradient def Grad0(f): return simplify_all([ diff(f, X[i]) for i in range(3)]) # surface gradient (covariant derivative of scalars) def grad0(f): return project1(Grad0(f)) def Grad1(T): return simplify_all([[ diff(T[i], X[j]) for j in range(3)] for i in range(3)]) # surface shape operator #B = negate(project2(Grad1(N))) # covariant derivative of vector field def grad1(T): return simplify_all(add(project2(Grad1(T)), [[ np.sum([ B[i][j] * T[k] * N[k] for k in range(3)]) for j in range(3)] for i in range(3)])) def Grad2(T): return simplify_all([[[ diff(T[i][j], X[k]) for k in range(3)] for j in range(3)] for i in range(3)]) def grad2(T): return simplify_all([[[ np.sum([np.sum([np.sum([ P[L1][I1] * P[L2][I2] * P[L3][K] * diff(T[L1][L2], X[L3]) for L3 in range(3)]) for L2 in range(3)]) for L1 in range(3)]) + np.sum([np.sum([ B[K][I1] * P[J2][I2] * T[L][J2] * N[L] for J2 in range(3)]) for L in range(3)]) + np.sum([np.sum([ B[K][I2] * P[J1][I1] * T[J1][L] * N[L] for J1 in range(3)]) for L in range(3)]) for K in range(3)] for I2 in range(3)] for I1 in range(3)]) def Grad3(T): return simplify_all([[[[ diff(T[i][j][k], X[l]) for l in range(3)] for k in range(3)] for j in range(3)] for i in range(3)]) def grad3(T): return simplify_all(add(project4(Grad3(T)), [[[[ np.sum([np.sum([np.sum([ B[K][I1] * P[J2][I2] * P[J3][I3] * T[L][J2][J3] * N[L] for J2 in range(3)]) for J3 in range(3)]) for L in range(3)]) + np.sum([np.sum([np.sum([ B[K][I2] * P[J1][I1] * P[J3][I3] * T[J1][L][J3] * N[L] for J1 in range(3)]) for J3 in range(3)]) for L in range(3)]) + np.sum([np.sum([np.sum([ B[K][I3] * P[J1][I1] * P[J2][I2] * T[J1][J2][L] * N[L] for J1 in range(3)]) for J2 in range(3)]) for L in range(3)]) for K in range(3)] for I3 in range(3)] for I2 in range(3)] for I1 in range(3)])) # normal-rotation of scalar field def rot0(f): return [diff(f*N[2],y) - diff(f*N[1],z), diff(f*N[0],z) - diff(f*N[2],x), diff(f*N[1],x) - diff(f*N[0],y)] def Div1(F): return diff(F[0],x) + diff(F[1],y) + diff(F[2],z) def div1(F): return Div1(project1(F)) # def div2(t): # F = Matrix([div1(t.row(0).T), div1(t.row(1).T), div1(t.row(2).T)]) # return P*F # div(T)_I1,I2 def div3(T): return simplify_all([[ np.sum([np.sum([np.sum([np.sum([np.sum([ P[L1][I1] * P[L2][I2] * P[L3][K] * P[L4][K] * diff(T[L1][L2][L3],X[L4]) for K in range(3)]) for L4 in range(3)]) for L3 in range(3)]) for L2 in range(3)]) for L1 in range(3)]) + np.sum([np.sum([ B[I3][I1] * T[L][I2][I3] * N[L] + B[I3][I2] * T[I1][L][I3] * N[L] + B[I3][I3] * T[I1][I2][L] * N[L] for I3 in range(3)]) for L in range(3)]) for I2 in range(3)] for I1 in range(3)]) #p0 = simplify_all( rot0(x*y*z) ) # => vector #print("p0 = ", p0) # p1 = simplify_all( grad1(p0) ) # => 2-tensor # print("p1 = ", p1) # f = simplify_all( add(negate(div3(grad2(p1))), p1) ) # print("f = ", f) #F = simplify( -div2(grad1(p0)) + p0 ) #print("F = ", F) #print("F*N = ", simplify(F.dot(N)))``````