SyFi  0.3
test_ufl2swiginac.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 __authors__ = "Martin Sandve Alnes"
00004 __date__ = "2008-15-10 -- 2012-16-06"
00005 
00006 import unittest
00007 
00008 import ufl
00009 from ufl import *
00010 from ufl.algorithms import expand_compounds
00011 
00012 import sfc as sfc
00013 from sfc.representation.swiginac_eval import SwiginacEvaluator
00014 
00015 from sfc.symbolic_utils import symbolic_matrix, symbolic_vector
00016 
00017 class MockCell:
00018     def __init__(self, nsd):
00019         self.nsd = nsd
00020 
00021 class MockFormRep:
00022     def __init__(self, nsd):
00023         self.rank = 0
00024         self.nsd = nsd
00025         self.cell = MockCell(nsd)
00026 
00027 class MockVariables:
00028     def __init__(self, nsd):
00029         self.x = symbolic_vector(nsd, "x")
00030         self.Ginv_sym = symbolic_matrix(nsd, nsd, "Ginv")
00031 
00032 def ufl2swiginac(expression, nsd):
00033     formrep = MockFormRep(nsd)
00034     variables = MockVariables(nsd)
00035     use_symbols = False
00036     s = SwiginacEvaluator(formrep, variables, use_symbols)
00037     return s.visit(expression)
00038 
00039 class Ufl2SwiginacTest(unittest.TestCase):
00040     def __init__(self, *args, **kwargs):
00041         unittest.TestCase.__init__(self, *args, **kwargs)
00042 
00043     def setUp(self):
00044         pass
00045 
00046     def tearDown(self):
00047         pass
00048 
00049     def testSetup(self):
00050         pass
00051 
00052     def testIndexed1(self):
00053         A = as_vector((1,2,3))
00054         a = A[0] + A[1] +A[2]
00055         b = ufl2swiginac(a, 3)
00056         c = 1 + 2 + 3
00057         self.assertEqual(c, b)
00058 
00059     def testIndexed2(self):
00060         A = as_matrix(((1,2),(3,4)))
00061         a = sum(A[i,j] for i in (0,1) for j in (0,1))
00062         b = ufl2swiginac(a, 2)
00063         c = 1 + 2 + 3 + 4
00064         self.assertEqual(c, b)
00065 
00066     def testIndexed3(self):
00067         A = as_tensor(((1,2,3), (4,5,6), (7,8,9)))
00068         a = A[i,i]
00069         b = ufl2swiginac(a, 3)
00070         c = 1+5+9
00071         self.assertEqual(c, b)
00072 
00073     def testProduct1(self):
00074         A = as_vector((1,2,3))
00075         a = A[0]*A[1]*A[2]
00076         b = ufl2swiginac(a, 3)
00077         c = 1 * 2 * 3
00078         self.assertEqual(c, b)
00079 
00080     def testProduct2(self):
00081         u = as_vector((2,3,5))
00082         v = as_vector((7,11,13))
00083         a = u[i]*v[i]
00084         b = ufl2swiginac(a, 3)
00085         c = 2*7 + 3*11 + 5*13
00086         self.assertEqual(c, b)
00087 
00088     def testProduct3(self):
00089         u = as_tensor(((1,2,3), (4,5,6), (7,8,9)))
00090         v = as_tensor(((4,5,6), (1,2,3), (4,5,6)))
00091         a = u[i,j]*v[i,j]
00092         b = ufl2swiginac(a, 3)
00093         c = (4+10+18)*2 + 7*4+8*5+9*6
00094         self.assertEqual(c, b)
00095 
00096     def testProduct4(self):
00097         u = as_tensor(((1,2,3), (4,5,6), (7,8,9)))
00098         v = as_tensor(u[i,j], (j,i))
00099         a = inner(u, v.T)
00100         a = expand_compounds(a, 3)
00101         b = ufl2swiginac(a, 3)
00102         c = sum(ii**2 for ii in range(10))
00103         self.assertEqual(c, b)
00104 
00105     def testSpatialDiff1(self):
00106         "Test a single constant dx"
00107         A = as_vector((2,3))
00108         a = A[0].dx(0)
00109         b = ufl2swiginac(a, 2)
00110         c = 0
00111         self.assertEqual(c, b)
00112 
00113     def testSpatialDiff2(self):
00114         "Test a single div-type df_i/dx_i"
00115         A = as_vector((2,3))
00116         a = A[i].dx(i)
00117         b = ufl2swiginac(a, 2)
00118         c = 0
00119         self.assertEqual(c, b)
00120 
00121     def testSpatialDiff3(self):
00122         "Test a double div-type df_ij/dx_ij"
00123         A = as_vector(((2,3),(4,5)))
00124         a = A[i,j].dx(j,i)
00125         b = ufl2swiginac(a, 2)
00126         c = 0
00127         self.assertEqual(c, b)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines