def vector_gradient(self, expr): grad = [] for i in range(len(expr)): print(self.ijk_to_list(spv.gradient(eval(expr[i])))) grad.append(self.ijk_to_list(spv.gradient(eval(expr[i])))) return grad
def makeMatrix(U,B,p,order,dic,vort =True): # if order !=0: buoy = Ri*(rho*g) # else: # r1 = rho0 # buoy = Ri*r1*g Cor=((2+chi*y)*qRo*C.k).cross(U) BgradB = (AgradB.xreplace({Ax:B&C.i,Ay:B&C.j,Az:B&C.k,Bx:B&C.i,By:B&C.j,Bz:B&C.k})).doit() UgradU = (AgradB.xreplace({Ax:U&C.i,Ay:U&C.j,Az:U&C.k,Bx:U&C.i,By:U&C.j,Bz:U&C.k})).doit() Eq_NS = diff(U,t)+Cor+UgradU-(-gradient(p)+qRe*laplacian(U)+buoy+BgradB) Eq_vort=diff((Eq_NS&C.j),x)-diff((Eq_NS&C.i),y) Eq_m=divergence(U) Eq_b=diff(B,t)- (qRm*laplacian(B) + curl(U.cross(B))) if vort == True: eq = zeros(7,1) for i,j in enumerate([Eq_NS&C.i,Eq_vort,Eq_NS&C.k,Eq_b&C.i,Eq_b&C.j,Eq_b&C.k,Eq_m]): eq[i] = taylor(j,order,dic) var = [Symbol('u'+str(order)+'x'),Symbol('u'+str(order)+'y'),Symbol('u'+str(order)+'z'),Symbol('p'+str(order)),Symbol('b'+str(order)+'x'),Symbol('b'+str(order)+'y'),Symbol('b'+str(order)+'z')] M, rme = linear_eq_to_matrix(eq, var) M = simplify((M/ansatz)).xreplace(dic) print("Matrix OK") return(M,rme,r1)
def test_doit(self): v1, v2, zero, one, nabla, C, vn1, vn2, x, y, z = self._get_vars() v = C.x * C.y * C.z assert gradient(v) == Grad(v).doit() expr = v * (VecDot(vn1, vn2)) assert isinstance(Grad(expr).doit(deep=False), Grad) assert isinstance(Grad(expr).doit(), Vector)
def doit(self, **kwargs): deep = kwargs.get('deep', True) args = self.args if deep: args = [arg.doit(**kwargs) for arg in args] # TODO: is the following condition ok? if not isinstance(args[1], VectorExpr): return gradient(args[1]) return self.func(*args)
def compute_true_res_formula(): """Tool to compute true res from sympy""" import sympy as sp from sympy.vector import CoordSysCartesian, gradient R = CoordSysCartesian('R') u = (sp.sin(2*sp.pi*R.x)*sp.cos(2*sp.pi*R.y)*sp.cos(2*sp.pi*R.z)) * R.i + \ (-sp.cos(2*sp.pi*R.x)*sp.sin(2*sp.pi*R.y)*sp.cos(2*sp.pi*R.z)) * R.j n = sp.symbols('n') res = lambda r, c: sp.simplify(u.dot(gradient(u.components[r], R)) - n * sp.diff(u.components[r], c, c)) for r, c in zip((R.i, R.j), (R.x, R.y)): print str(res(r, c)).replace('R.', '')
def laplacian(funct): """ Parameters ---------- funct : CoordSys3D type The function that we are finding the laplacian of Returns ------- CoordSys3D type This returns the laplacian of the function, which is the divergence of the gradient of the function. """ return divergence(gradient(funct))
def laplacian(self, expr): out = [] for i in range(len(expr)): out.append(spv.divergence(spv.gradient(eval(expr[i])))) return self.post_process(out)
#divergence #1ere methode nabla.dot(c.x * c.y * c.z * (c.i + c.j + c.k)).doit() #C.x*C.y + C.x*C.z + C.y*C.z (nabla & c.x * c.y * c.z * (c.i + c.j + c.k)).doit() #C.x*C.y + C.x*C.z + C.y*C.z #2eme methode: divergence(c.x * c.y * c.z * (c.i + c.j + c.k)) #c.x*C.y + C.x*C.z + C.y*C.z #Gradient #1ere methode """"Consider a scalar field f(x,y,z) in 3D space. The gradient of this field is defined as the vector of the 3 partial derivatives of f with respect to x, y and z in the X, Y and Z axes respectively. In the 3D Cartesian system, the divergence of a scalar field f, denoted by ∇f is given by - ∇f=∂f∂xi^+∂f∂yj^+∂f∂zk^ Computing the divergence of a vector field in sympy.vector can be accomplished in two ways. One, by using the Del() class""" gradient(c.x * c.y * c.z) #c.y*c.z*c.i+c.x*c.z*c.j+c.x*x.y*c.k. #or nabla.gradient(c.x * c.y * c.z).doit() #C.y*C.z*C.i + C.x*C.z*C.j + C.x*C.y*C.k nabla(c.x * c.y * c.z).doit() #C.y*C.z*C.i + C.x*C.z*C.j + C.x*C.y*C.k
init_printing() global R global transformation u1, u2, u3 = symbols('u1:4', type='Function') f1 = symbols('f_0', type='Function') omega1 = symbols(r'\Omega', type='Function') nabla = Del() R.i = getattr(R, vec_names[0]) R.j = getattr(R, vec_names[1]) R.k = getattr(R, vec_names[2]) R.x = getattr(R, var_names[0]) R.y = getattr(R, var_names[1]) R.z = getattr(R, var_names[2]) u = u1(R.x, R.y, R.z) * R.i + u2(R.x, R.y, R.z) * R.j + u3(R.x, R.y, R.z) * R.k e = eta(R.x, R.y, R.z) if transformation == 'spherical': u_h = u2(R.x, R.y, R.z) * R.j + u3(R.x, R.y, R.z) * R.k stream = psi(R.y, R.z) pot = chi(R.y, R.z) u_r = -curl(R.i * stream).doit() elif transformation == 'cartesian': u_h = u1(R.x, R.y, R.z) * R.i + u2(R.x, R.y, R.z) * R.j stream = psi(R.x, R.y) pot = chi(R.x, R.y) u_r = -curl(R.k * stream).doit() u_d = gradient(pot).doit() u_helm = u_r + u_d
def Bound(U,B,sol,eig,dic,order,condB = "harm pot",condU = 'Inviscid'): lso = len(sol) for s in range(len(sol)): globals() ['C'+str(s)] = Symbol('C'+str(s)) ################################# ### Inviscid solution : ### ### lso=3 --> 1 boundary ### ### lso=6 --> 2 boundaries ### ################################# nn = n.xreplace(dic).doit() U = (U.xreplace(makedic(veigen(eig,sol),order))).xreplace({U0:1}) B = (B.xreplace(makedic(veigen(eig,sol),order))) if (condB == "harm pot"): bchx,bchy,bchz = symbols("bchx,bchy,bchz") bcc =surfcond((bchx*C.i +bchy*C.j + bchz*C.k)*ansatz - gradient(psi),dic).doit() sob = list(linsolve([bcc&C.i,bcc&C.j,bcc&C.k],(bchx,bchy,bchz)))[0] bbc = sob[0]*C.i + sob[1]*C.j + sob[2]*C.k bb = B.xreplace(makedic(veigen(eig,sol),order)) - (bbc*ansatz) Eq_b= surfcond(bb,dic) Eq_bx = Eq_b&C.i; Eq_by = Eq_b&C.j; Eq_bz = Eq_b&C.k if params.Bound_nb ==2: bchx2,bchy2,bchz2 = symbols("bchx2,bchy2,bchz2") bcc2 =surfcond_2((bchx2*C.i +bchy2*C.j +bchz2*C.k)*ansatz - gradient(psi_2b),dic) sob2 = list(linsolve([bcc2&C.i,bcc2&C.j,bcc2&C.k],(bchx2,bchy2,bchz2)))[0] bbc2 = sob2[0]*C.i + sob2[1]*C.j + sob2[2]*C.k bb2 = B.xreplace(makedic(veigen(eig,sol),order)) - (bbc2*ansatz) Eq_b2= surfcond_2(bb2,dic) Eq_b2x = Eq_b2&C.i; Eq_b2y = Eq_b2&C.j; Eq_b2z = Eq_b2&C.k if (condB == "thick"): bchx,bchy,bchz,eta = symbols("bchx,bchy,bchz,eta") kz_t = -sqrt(-kxl**2-kyl**2-I*omega/qRmm) # kz_t = I*1e12 B_mant = (bchx*C.i +bchy*C.j + bchz*C.k)*ansatz.xreplace({kz:kz_t}) eq_ind = (surfcond((diff(B_mant,t)-qRmm*laplacian(B_mant)-diff(B,t) +qRm*laplacian(B) - curl(U.cross(B))).xreplace({kz:kz_t}),dic).xreplace(dic)) eq_E = (qRm*curl(B)-U.cross(B)-qRmm*curl(B_mant)) eq_Et = surfcond(((nn).cross(eq_E)).xreplace({kz:kz_t}),dic) eq_B = surfcond(((B_mant.dot(nn)-B.dot(nn))).xreplace({kz:kz_t}),dic) un = (U.dot(nn)) Eq_n1= surfcond((un).xreplace({kz:kz_t}),dic).xreplace(dic) TEq = [(taylor(eq,order,dic)).xreplace({x:0,y:0,t:0}) for eq in [Eq_n1,eq_ind&C.i,eq_ind&C.j,eq_ind&C.k,eq_Et.dot(tx),eq_Et.dot(ty)]] Mat, res = linear_eq_to_matrix(TEq,(C0,C1,C2,bchx,bchy,bchz)) U = (U.xreplace(makedic(veigen(eig,sol),order))).xreplace({U0:1}) un = U.dot(nn) Eq_n1= surfcond(un,dic).xreplace(dic) if condU == "Inviscid": if params.Bound_nb ==2: nn2 = n2.xreplace(dic).doit() un2 = U.dot(nn2) Eq_n2= surfcond_2(un2,dic) TEq = [(taylor(eq,order,dic)).xreplace({x:0,y:0,t:0}) for eq in [Eq_n1,Eq_n2,Eq_bx,Eq_by,Eq_bz,Eq_b2x,Eq_b2y,Eq_b2z]] Mat, res = linear_eq_to_matrix(TEq,(C0,C1,C2,C3,C4,C5,Symbol("psi"+str(order)),Symbol("psi"+str(order)+"_2b"))) elif params.Bound_nb ==1: TEq = [(taylor(eq,order,dic)).xreplace({x:0,y:0,t:0}) for eq in [Eq_n1,Eq_bx,Eq_by,Eq_bz]] Mat, res = linear_eq_to_matrix(TEq,(C0,C1,C2,Symbol("psi"+str(order)))) elif condU == 'noslip': if params.Bound_nb ==1: U = (U.xreplace(makedic(veigen(eig,sol),order))) ut1 = U.dot(tx) ut2 = U.dot(ty) Eq_BU1= surfcond(ut1,dic) Eq_BU2 = surfcond(ut2,dic) TEq = [(taylor(eq,order,dic)).xreplace({x:0,y:0,t:0}) for eq in [Eq_n1,Eq_BU1,Eq_BU2,Eq_bx,Eq_by,Eq_bz]] Mat, res = linear_eq_to_matrix(TEq,(C0,C1,C2,C3,C4,Symbol("psi"+str(order)))) elif params.Bound_nb ==2: un1 = U.dot(tx2) un2 = U.dot(ty2) Eq2_BU1= surfcond_2(un1,dic) Eq2_BU2 = surfcond_2(un2,dic) TEq = [(taylor(eq,order,dic)).xreplace({x:0,y:0,t:0}) for eq in [Eq_n1,Eq_n2,Eq_BU1,Eq_BU2,Eq2_BU1,Eq2_BU2,Eq_bx,Eq_by,Eq_bz,Eq_b2x,Eq_b2y,Eq_b2z]] Mat, res = linear_eq_to_matrix(TEq,(C0,C1,C2,C3,C4,C5,C6,C7,C8,C9,Symbol("psi"+str(order)),Symbol("psi"+str(order)+"_2b"))) elif condU == 'stressfree': if params.Bound_nb ==1: eu = strain(U)*nn eu1 = eu*tx eu2 = eu*ty Eq_BU1 = surfcond(eu1,dic,realtopo =False) Eq_BU2 = surfcond(eu2,dic,realtopo =False) TEq = [(taylor(eq,order,dic)).xreplace({x:0,y:0,t:0}) for eq in [Eq_n1,Eq_BU1,Eq_BU2,Eq_bx,Eq_by,Eq_bz]] Mat, res = linear_eq_to_matrix(TEq,(C0,C1,C2,C3,C4,Symbol("psi"+str(order)))) elif params.Bound_nb ==2: eu = strain(U)*nn2 eu1 = eu*tx2 eu2 = eu*ty2 Eq2_BU1 = surfcond2(eu1,dic,realtopo =False) Eq2_BU2 = surfcond2(eu2,dic,realtopo =False) TEq = [(taylor(eq,order,dic)).xreplace({x:0,y:0,t:0}) for eq in [Eq_n1,Eq_n2,Eq_BU1,Eq_BU2,Eq2_BU1,Eq2_BU2,Eq_bx,Eq_by,Eq_bz,Eq_b2x,Eq_b2y,Eq_b2z]] Mat, res = linear_eq_to_matrix(TEq,(C0,C1,C2,C3,C4,C5,C6,C7,C8,C9,Symbol("psi"+str(order)),Symbol("psi"+str(order)+"_2b"))) Mat = Mat.evalf(mp.mp.dps) res = res.evalf(mp.mp.dps) Mat = mpmathM(Mat) res = mpmathM(res) try: abc = mp.qr_solve(Mat,res)[0] except: abc = mp.lu_solve(Mat,res) mantle =0 #In progress ... solans = zeros(7,1) for l in range(lso): solans = solans + abc[l]*Matrix(eig[l])*(ansatz).xreplace({kz:sol[l]}) solans = solans.xreplace(dic) return(abc,solans,mantle)
# Calculus for each order i for i in range(1,order+1): print("ORDER",i) if Bound_nb ==2: U,B,p,psi,psi_2b = makeVar() if Bound_nb ==1: U,B,p,psi = makeVar() # Solve the mass conservation equation if i ==1: rho= rho0.xreplace({**dico0,**dico1}) rho = rho + e**i*Symbol('rho'+str(i))*ansatz0.xreplace({kxl:i*kxl,kyl:i*kyl,omega:i*omega}) print(rho) Eq_rho = diff(rho,t)+ U.dot(gradient(rho)) print(Eq_rho) Eq_rho1 = taylor(Eq_rho,i,{**dico0,**dico1}) print(Eq_rho1) r1 = list(solveset(Eq_rho1,Symbol('rho'+str(i))))[0] rho = rho.xreplace({Symbol('rho'+str(i)):r1}) ansatz = (exp(I*(i*omega*t+i*kxl*x+i*kyl*y+kz*z))).xreplace({**dico0,**dico1}) ### TEST OF ORDER0 ### # print("we test the order 0...") # M0,rme0,r10 = makeMatrix(U0 *u0,qAl*b0,p0,0,{**dico0,**dico1},vort=True) # print(rme0.xreplace({**dico0,**dico1})) M,rme,r1 = makeMatrix(U,B,p,i,{**dico0,**dico1},vort=True)
def q(u): """Return nonlinear coefficient""" return 1 + u * u R = CoordSys3D("R") def apply(f, coords): x, y = symbols("x y") return lambdify((x, y), f.subs({R.x: x, R.y: y}))(*coords) u_exact = 1 + R.x + 2 * R.y # exact solution f = -divergence(q(u_exact) * gradient(u_exact)) # manufactured RHS mesh = MeshTri() mesh.refine(3) V = InteriorBasis(mesh, ElementTriP1()) boundary = V.get_dofs().all() interior = V.complement_dofs(boundary) @LinearForm def load(v, w): return v * apply(f, w.x)