예제 #1
0
    def test_scalar_nabla_cart_vs_spher(self):
        C=Cartesian()
        ux,uy,uz=C.U
        sp=Spherical()
        xu,yu,zu=sp.XofU
        r,phi,theta=sp.U
        
        # define a zero order tensor f in cartesian coordinates
        # and compute the application of nabla to it
        # expressed according to the cartesian base vectors
        fxcomp=ux**2+uy**2
        fX=Tensor.Scalar(C,fxcomp)
        nfX=fX.nabla()
        pp("nfX",locals())
        # for later comparison express the cartesian components 
        # in terms of r phi and theta
        nfX_spher=nfX.subs({ux:xu,uy:yu,uz:zu})
        pp("nfX_spher",locals())

        # now express the same tensor f in terms of r phi and theta 
        # and compute the application of nabla to it
        # expressed according to the spherical base vectors
        fucomp=sympy.simplify(fxcomp.subs({ux:xu,uy:yu,uz:zu}))
        pp("fucomp",locals())
        fU=Tensor.Scalar(sp,fucomp)
        nfU=fU.nabla()
        pp("nfU",locals())
        # now transform back to cartesian base vectors
        nfU_cart=nfU.transform2(["cart"])
        pp("nfU_cart",locals())
        
        #now show that both ways to compute nabla f yield the same result
        self.assertTrue(nfX_spher,nfU_cart)
예제 #2
0
    def test_transpose(self):
        c=Cartesian()
        # note that roof and cellar
        # components are equal in this case
        x,y,z=c.U
        A=Tensor.Tensor(c,["roof","cellar"],{(2,0):1})
        a=Tensor.Tensor(c,["cellar"],{(2,):x})
        b=Tensor.Tensor(c,["roof"],{(2,):x})
        res=a|A|b        
        res_trans=b|A.transpose()|a  #definition of the transposed Tensor       
        self.assertEqual(res,res_trans)
        
        sp=Spherical()
        r,phi,theta=sp.U
        
        A=Tensor.Tensor(sp,["roof","cellar"],{(2,0):1})
        a=Tensor.Tensor(sp,["cellar"],{(0,):r,(1,):phi,(2,):r*phi})
        b=Tensor.Tensor(sp,["roof"],{(2,):phi})
        res=a|A|b        
        res_trans=b|A.transpose()|a  #definition of the transposed Tensor       
        self.assertEqual(res,res_trans)

        A=Tensor.Tensor(sp,["cart","cart"],{(2,0):1})
        a=Tensor.Tensor(sp,["cellar"],{(1,):r})
        b=Tensor.Tensor(sp,["roof"],{(2,):phi})
        res=a|A|b        
        res_trans=b|A.transpose()|a  #definition of the transposed Tensor       
        self.assertEqual(res,res_trans)

        A=Tensor.Tensor(sp,["roof","roof"],{(2,0):1})
        a=Tensor.Tensor(sp,["cellar"],{(0,):r})
        b=Tensor.Tensor(sp,["cellar"],{(2,):phi})
        res=a|A|b        
        res_trans=b|A.transpose()|a  #definition of the transposed Tensor       
        self.assertEqual(res,res_trans)
예제 #3
0
 def test_nabla(self):
     # we start with the following vector valued function f given in 
     # cartesian coordinates
     c=Cartesian()
     x,y,z=c.U
     fX=Tensor.Tensor(c,["roof"],{(2,):x}) #=x*e_z
     pp("fX",locals())
     fXn=fX.nabla()
예제 #4
0
    def test_scalar_nabla_cart(self):
        ##################
        # cartesian part #
     	################## 
        C=Cartesian()
        ux,uy,uz=C.U
	    # an example scalar function
        fX=Tensor.Scalar(C,ux**2+uy**2) 
	    # compute the cellar components of the nabla f and compare them to the expected result
        nfX=fX.nabla()
        self.assertEqual(nfX,Tensor.Tensor(C,["cellar"],{(0,):2*ux,(1,):2*uy,(2,):0}))
예제 #5
0
    def testAdd(self):
        sp=Spherical()
        u=Tensor.Tensor(sp,["roof"],{(1,):1})
        v=Tensor.Tensor(sp,["roof"],{(1,):2})
        # make a copy for later comparisons
        cu=copy.deepcopy(u)
        cv=copy.deepcopy(v)
        ref=Tensor.Tensor(sp,["roof"],{(1,):3})
        res=u+v
        self.assertEqual(res,ref)
        # test that we do not modyfy the ingredients inadvertatly
        # so u=cu and v=cv should still hold
        self.assertEqual(u,cu)
        self.assertEqual(v,cv)
        # now use __radd__ by changing the order of the parts
        res_reverse=v+u
        self.assertEqual(res_reverse,ref)
        # test that we do not modyfy the ingredients inadvertatly
        # so u=cu and v=cv should still hold
        self.assertEqual(u,cu)
        self.assertEqual(v,cv)
        
        u=Tensor.Tensor(Cartesian(),['cellar', 'roof'],{(0, 2): 1})
        v=Tensor.Tensor(Cartesian(),['cellar', 'roof'],{(1, 2): 1})

        cu=copy.deepcopy(u)
        cv=copy.deepcopy(v)
        ref=Tensor.Tensor(Cartesian(),['cellar', 'roof'],{(1, 2): 1, (0, 2): 1})
        res=u+v
        self.assertEqual(res,ref)
        # test that we do not modyfy the ingredients inadvertatly
        # so u=cu and v=cv should still hold
        self.assertEqual(u,cu)
        self.assertEqual(v,cv)
        # now use __radd__ by changing the order of the parts
        res_reverse=v+u
        self.assertEqual(res_reverse,ref)
        # test that we do not modyfy the ingredients inadvertatly
        # so u=cu and v=cv should still hold
        self.assertEqual(u,cu)
        self.assertEqual(v,cv)


        self.assertEqual(res,ref)
        u=Tensor.Tensor(sp,["cart"],{(1,):1})
        v=Tensor.Tensor(sp,["cart"],{(1,):2})
        ref=Tensor.Tensor(sp,["cart"],{(1,):3})
        res=u+v
        self.assertEqual(res,ref)
        res=v+u
        u=Tensor.Tensor(sp,["roof"],{(1,):1})
        v=Tensor.Tensor(sp,["cart"],{(1,):2})
        with self.assertRaises(Exceptions.NotImplementedError):
            u+v
        u=Tensor.Tensor(sp,["roof","roof"],{(1,1):1})
        v=Tensor.Tensor(sp,["cart"],{(1,):2})
        with self.assertRaises(Exceptions.ArgumentSizeError):
           u+v
        
        with self.assertRaises(Exceptions.ArgumentTypeError):
           u+3 
예제 #6
0
    def test_vec_grad_cart(self):  
        # we first verify that our implementation 
        # works for the comparativly simple case
        # of cartisian coordinates. Here the base vectors are constant
        # so the derivative of a vector has only to take into account
        # the components. Also roof and cellar base vectors are 
        # indistinguishable in this case
        
        # Consider a vector valued function F at positon X.
        # If we change X by delta_X also F will change by delta_F
        # The vector of change delta_F according to a change of position
        # delta_X can be expressend by a linear function A acting on 
        # delta_X and a remainder. We call the linear part dF
        #
        # delta_F = A(delta_X) + O(|delta_X|**2) 
        #
        #         = dF         + O(|delta_X|**2) 
        # 
        # we can express the application of A on delta_X by
        # the scalar product. So we get 
        #
        # d_F=A|delta_X
        #
        # where A is the second order tensor to represent the 
        # gradient of v and | is the scalar product 
        # To test our implementation we choose a linear function
        # F which then coincides with its own derivative.
        # we then have 
        # 
        # delta_F=d_F
        #
        c=Cartesian()
        x,y,z=c.U
        # for F we chose
        #                                         z       
        F=Tensor.Tensor(c,["roof"],{(2,):x}) #=x*e   =x*e   (roof and cellar base vectors are equal in this case) 
        #                                  z
        # for X0 we chose the origin
        x0=0;y0=0;z0=0
        # for X1 we chose 
        x1=1;y1=0;z1=0
        # which means that  delta_X =X1-X1
        # which in cartesian coordinates can be expressed very easily
        delta_X=Tensor.Tensor(c,["roof"],{(0,):x1-x0,(1,):y1-y0,(2,):z1-z0})
        #
        # now we first compute F0=F(X0)=F(x0,y0,z0)
        F0=F.subs({x:x0,y:y0,z:z0})
        F1=F.subs({x:x1,y:y1,z:z1})
        delta_F=F1-F0
        pp("delta_F",locals())

        # now we compute the same result using the grad operator 
        A=F.grad()
        d_F=A|delta_X
        
        self.assertEqual(d_F,delta_F)
        
        
        # we now do this for several examles for F and delta_X
        # where the common property is that F is linear
        # for F we chose
        F=Tensor.Tensor(c,["roof"],{(2,):x+y}) 
        # for X0 we chose the origin
        x0=1;y0=0;z0=0
        # for X1 we chose 
        x1=2;y1=2;z1=1
        # which means that  delta_X =X1-X1
        # which in cartesian coordinates can be expressed very easily
        delta_X=Tensor.Tensor(c,["roof"],{(0,):x1-x0,(1,):y1-y0,(2,):z1-z0})
        #
        # now we first compute F0=F(X0)=F(x0,y0,z0)
        F0=F.subs({x:x0,y:y0,z:z0})
        F1=F.subs({x:x1,y:y1,z:z1})
        delta_F=F1-F0

        # now we compute the same result using the grad operator 
        A=F.grad()
        d_F=A|delta_X
        
        self.assertEqual(d_F,delta_F)
예제 #7
0
    def test_scalar_initialization(self):
        C=Cartesian()
        ux,uy,uz=C.U
	    # an example scalar function
        fU=Tensor.Scalar(C,ux**2+uy**2) 
예제 #8
0
    def setUp(self):
        # for all tests we create 2 instances of a general coordinate system
        # a cartesian and a spherical one

        self.C = Cartesian()
        self.S = Spherical()