def test_doit(self): v1, v2, zero, one, nabla, C, vn1, vn2, x, y, z = self._get_vars() assert isinstance(Advection(v1, v2).doit(), Advection) # identity test a = C.x * C.i + C.y * C.j + C.z * C.k b = C.y * C.z * C.i + C.x * C.z * C.j + C.x * C.y * C.k assert self._check_args((nabla ^ (a ^ b)).doit(), (divergence(b) * a) + Advection(b, a).doit() - (divergence(a) * b) - Advection(a, b).doit()) assert isinstance( Advection(VecAdd(a, b), C.x).doit(deep=False), Advection) assert isinstance( Advection(VecAdd(a, b), VecAdd(a, b)).doit(deep=False), Advection)
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() assert VecDot(vn1, vn2).doit() - vn1.dot(vn2) == 0 assert VecDot(nabla, vn2).doit() - divergence(vn2) == 0 assert isinstance(VecDot(v1, v1).doit(), VecPow) assert isinstance(VecDot(vn1, vn1).doit(), Integer) expr = VecDot(v1, v2.norm).doit() assert tuple(type(a) for a in expr.args) in [(VectorSymbol, VecMul), (VecMul, VectorSymbol)] expr = VecDot(v1, v2.norm).doit(deep=False) assert tuple(type(a) for a in expr.args) in [(VectorSymbol, Normalize), (Normalize, VectorSymbol)] expr = VecDot(v1, VecAdd(vn2, vn1)).doit(deep=False) assert tuple(type(a) for a in expr.args) in [(VectorSymbol, VecAdd), (VecAdd, VectorSymbol)] expr = VecDot(v1, VecAdd(vn2, vn1)).doit() assert tuple(type(a) for a in expr.args) in [(VectorSymbol, VectorAdd), (VectorAdd, VectorSymbol)] expr = VecDot(vn1, vn2 + vn1).doit() assert expr == (x + 2 * y + 3 * z + 14)
def doit(self, **kwargs): deep = kwargs.get('deep', True) args = self.args if deep: args = [arg.doit(**kwargs) for arg in args] if isinstance(args[0], Vector) and \ isinstance(args[1], Vector): return args[0].dot(args[1]) if isinstance(args[0], Vector) and \ isinstance(args[1], Nabla): return divergence(args[0]) if isinstance(args[1], Vector) and \ isinstance(args[0], Nabla): return divergence(args[1]) if args[0] == args[1]: return VecPow(args[0].mag, 2) return self.func(*args)
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)
from pymoab import core, rng, types from mesh_generator import MeshGenerator from sympy import sin, pi, lambdify, Array from sympy.vector import CoordSys3D, Del, divergence from math import atan import numpy as np from numpy.linalg import inv C = CoordSys3D('C') nabla = Del() u_field = -C.x - 0.2 * C.y gradient_u = nabla(u_field) laplacian_u = divergence(gradient_u) q_func = lambdify([C.x, C.y, C.z], laplacian_u) u_func = lambdify([C.x, C.y, C.z], u_field) gradient_u_func = lambda x, y, z: 0.0 M1 = np.array([100.0, 0.0, 0.0, \ 0.0, 10.0, 0.0, \ 0.0, 0.0, 1.0]).reshape(3, 3) M2 = np.array([1.0, 0.0, 0.0, \ 0.0, 0.1, 0.0, \ 0.0, 0.0, 1.0]).reshape(3, 3) theta = atan(0.2) rot_z = np.array([np.cos(theta), np.sin(theta), 0.0, \ -np.sin(theta), np.cos(theta), 0.0, \ 0.0, 0.0, 1.0]).reshape(3, 3) rot_z_inv = inv(rot_z) K1 = ((rot_z * M1) * rot_z_inv).reshape(9) K2 = ((rot_z * M2) * rot_z_inv).reshape(9)
k = 0.09 return w * (-2 * (ca + cb) * c ** 3 - 2 * ca * cb * (ca + cb) * c) - 0.5 * k * c x = np.linspace(-0.1, 1.1, 201) ycon = Fcon(x) yexp = Fexp(x) plt.figure() plt.plot(x, ycon, label="con") plt.plot(x, yexp, label="exp") plt.plot(x, ycon + yexp, label="tot") plt.xlim([-0.1, 1.1]) plt.ylim([-0.5, 0.5]) plt.xlabel(r"$c$") plt.ylabel(r"$f_{\mathrm{chem}}$") plt.legend(loc="best") plt.savefig("fchem.png", bbox_inches="tight", dpi=400) print("Figures generated. Doing math...") from sympy.abc import A, B, C from sympy.vector import CoordSys3D, Del, divergence R = CoordSys3D('R') delop = Del() Phi = A*R.x*R.y + B*R.x + C*R.y print("Phi = ", Phi) print("gradPhi = ", delop(Phi).doit()) print("lapPhi = ", divergence(delop(Phi).doit()))
nabla.cross(c.x * c.y * c.z * c.i).doit() (nabla ^ c.x * c.y * c.z * c.i).doit() #2eme methode curl(C.x * C.y * C.z * C.i) #C.x*C.y*C.j + (-C.x*C.z)*C.k #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.
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)