SyFi 0.3
volumes.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 __authors__ = "Martin Sandve Alnes"
00004 __date__ = "2008-09-04 -- 2008-09-04"
00005 
00006 import unittest
00007 import os, sys, glob, shutil, commands
00008 
00009 import ufl
00010 
00011 from ufl import FiniteElement
00012 from ufl import VectorElement
00013 from ufl import TensorElement
00014 from ufl import MixedElement
00015 
00016 from ufl import BasisFunction
00017 from ufl import TestFunction
00018 from ufl import TrialFunction
00019 
00020 from ufl import Function
00021 from ufl import Constant
00022 
00023 from ufl import dx, ds
00024 
00025 import SyFi
00026 import sfc as sfc
00027 
00028 from cell_assembly import assemble_on_cell, cell2volume
00029 
00030 
00031 def num_integrals(form):
00032     return (form.num_cell_integrals(), form.num_exterior_facet_integrals(), form.num_interior_facet_integrals())
00033 
00034 
00035 _test_temp_dir = "volume_temp_dir"
00036 _done_test_temp_dir = "done_volume_temp_dir"
00037 class VolumeTest(unittest.TestCase):
00038     def __init__(self, *args, **kwargs):
00039         unittest.TestCase.__init__(self, *args, **kwargs)
00040         shutil.rmtree(_done_test_temp_dir, ignore_errors=True)
00041         os.mkdir(_done_test_temp_dir)
00042     
00043     def setUp(self):
00044         #print "Running sfc jit test in testdir"
00045         #print "Imported SyFi from location", SyFi.__file__
00046         #print "Imported sfc  from location", sfc.__file__
00047         self.options = sfc.default_options()
00048         self.options.compilation.cache_dir = os.path.abspath("test_cache")
00049         # Generate code in a clean directory:
00050         shutil.rmtree(_test_temp_dir, ignore_errors=True)
00051         os.mkdir(_test_temp_dir)
00052         os.chdir(_test_temp_dir)
00053     
00054     def tearDown(self):
00055         dirs = glob.glob("*")
00056         os.chdir("..")
00057         for d in dirs:
00058             os.rename(os.path.join(_test_temp_dir, d), os.path.join(_done_test_temp_dir, d))
00059     
00060     def testSetup(self):
00061         pass
00062     
00063     def _testJitVolume(self, polygon):
00064         "Test that the integral of 1.0 over a unit cell equals the length/area/volume of the unit cell."
00065         c = Constant(polygon)
00066         a = c*dx
00067         form = sfc.jit(a, options = self.options)
00068         self.assertTrue(form.rank() == 0)
00069         self.assertTrue(form.num_coefficients() == 1)
00070         self.assertTrue(num_integrals(form) == (1,0,0))
00071         A = assemble_on_cell(form, polygon, coeffs=[1.0])
00072         self.assertAlmostEqual(A, cell2volume[polygon])
00073     
00074     def testJitVolumeInterval(self):
00075         self._testJitVolume("interval")
00076 
00077     def testJitVolumeTriangle(self):
00078         self._testJitVolume("triangle")
00079     
00080     def testJitVolumeTetrahedron(self):
00081         self._testJitVolume("tetrahedron")
00082     
00083     def _testJitVolumeQuadrilateral(self): # Not supported by dolfin yet
00084         self._testJitVolume("quadrilateral")
00085     
00086     def _testJitVolumeHexahedron(self): # Not supported by dolfin yet
00087         self._testJitVolume("hexahedron")
00088 
00089     def _testJitConstant(self, polygon, degree):
00090         """Test that the integral of a constant coefficient over a unit 
00091         cell mesh equals the constant times the volume of the unit cell."""
00092         element = FiniteElement("CG", polygon, degree)
00093         f = Function(element)
00094         a = f*dx
00095         form = sfc.jit(a, options = self.options)
00096         self.assertTrue(form.rank() == 0)
00097         self.assertTrue(form.num_coefficients() == 1)
00098         self.assertTrue(num_integrals(form) == (1,0,0))
00099         const = 1.23
00100         A = assemble_on_cell(form, polygon, coeffs=[const])
00101         self.assertAlmostEqual(A, const*cell2volume[polygon])
00102     
00103     def testJitConstantInterval(self):
00104         polygon = "interval"
00105         self._testJitConstant(polygon, 1)
00106         self._testJitConstant(polygon, 2)
00107 
00108     def testJitConstantTriangle(self):
00109         polygon = "triangle"
00110         self._testJitConstant(polygon, 1)
00111         self._testJitConstant(polygon, 2)
00112     
00113     def testJitConstantTetrahedron(self):
00114         polygon = "tetrahedron"
00115         self._testJitConstant(polygon, 1)
00116         self._testJitConstant(polygon, 2)
00117     
00118     def _testJitConstantQuadrilateral(self): # Not supported by dolfin yet
00119         polygon = "quadrilateral"
00120         self._testJitConstant(polygon, 1)
00121         self._testJitConstant(polygon, 2)
00122 
00123     def _testJitConstantHexahedron(self): # Not supported by dolfin yet
00124         polygon = "hexahedron"
00125         self._testJitConstant(polygon, 1)
00126         self._testJitConstant(polygon, 2)
00127 
00128 
00129 tests = [VolumeTest]
00130 
00131 if __name__ == "__main__":
00132     unittest.main()
00133 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines