SyFi
0.3
|
00001 #!/usr/bin/env python 00002 00003 __authors__ = "Martin Sandve Alnes" 00004 __date__ = "2012-05-22 -- 2012-05-22" 00005 00006 import unittest 00007 import os, sys, glob, shutil, commands 00008 00009 import ufl 00010 from ufl import * 00011 00012 import SyFi 00013 import sfc as sfc 00014 00015 from test_tempdir_base import TempDirTestBase 00016 from cell_assembly import assemble_on_cell, num_integrals, cell2volume 00017 00018 all_cells = (interval, triangle, tetrahedron, quadrilateral, hexahedron) 00019 all_simplices = (interval, triangle, tetrahedron) 00020 00021 class TestJitFunctionals(TempDirTestBase): 00022 def __init__(self, *args, **kwargs): 00023 TempDirTestBase.__init__(self, *args, **kwargs) 00024 00025 def setUp(self): 00026 TempDirTestBase.setUp(self) 00027 # TODO: Perhaps subclass JitTest and use different setup routines to test options? 00028 self.options.code.integral.integration_method = "quadrature" 00029 00030 def tearDown(self): 00031 TempDirTestBase.tearDown(self) 00032 00033 def xtest_setup(self): 00034 "Just see that setUp and tearDown works." 00035 self.assertTrue("Did not crash in setUp.") 00036 00037 def assertAlmostEqualRelative(self, val1, val2, eps): 00038 reldiff = ((val1 - val2)**2 / max(val1**2, val2**2))**0.5 00039 if not reldiff < eps: 00040 print "Values are not almost equal:" 00041 print "val1:", val1 00042 print "val2:", val2 00043 self.assertLess(reldiff, eps) 00044 00045 def _test_piecewise_constant_assembly_on_cell_single(self, function, cell, family, degree, vector): 00046 eps = 1e-7 00047 detG = cell2volume[cell.domain()] 00048 00049 if vector: 00050 element = VectorElement(family, cell, degree) 00051 else: 00052 element = FiniteElement(family, cell, degree) 00053 00054 u = Coefficient(element) 00055 M = function(u)*dx 00056 00057 if vector: 00058 u = (3.14, 4.56, 7.89)[:element.value_shape()[0]] 00059 coeffs = [u] 00060 expected = float(function(as_vector(u)))*detG 00061 else: 00062 u = 3.14 00063 coeffs = [u] 00064 expected = float(function(u))*detG 00065 00066 form, module, formdata, prefix = sfc.jit(M, parameters=self.options) 00067 00068 if cell in all_simplices: # Quads not supported by dolfin assembler 00069 assembled = assemble_on_cell(form, cell.domain(), coeffs=coeffs, elements=[]) 00070 self.assertAlmostEqualRelative(assembled, expected, eps) 00071 00072 def _test_piecewise_constant_assembly_on_cell_sweep(self, function, vector): 00073 families = ("CG", "DG", "R") 00074 degrees = { "CG": (1,2), 00075 "DG": (0,3), 00076 "R": (None,), 00077 } 00078 for cell in all_cells: 00079 for family in families: 00080 for degree in degrees[family]: 00081 self._test_piecewise_constant_assembly_on_cell_single(function, cell, 00082 family, degree, vector) 00083 00084 def _test_piecewise_constant_assembly_on_cell(self, function, vector=False): 00085 self._test_piecewise_constant_assembly_on_cell_single(function, tetrahedron, "CG", 1, vector) 00086 00087 def xtest_coeff_with_element_sweep(self): # This is too slow, too many jits 00088 self._test_piecewise_constant_assembly_on_cell_sweep(lambda u: u, False) 00089 self._test_piecewise_constant_assembly_on_cell_sweep(lambda u: u[0], True) 00090 00091 def xtest_algebra(self): # FIXME: Failing! 00092 self._test_piecewise_constant_assembly_on_cell(lambda u: (u+2)**2*(u-1.1)**1.5/7.2) 00093 00094 def xtest_trig(self): # FIXME: Failing! 00095 self._test_piecewise_constant_assembly_on_cell(lambda u: (exp(3*u)*ln(u) + 00096 acos(sin(2*u)) + 00097 asin(cos(2*u)) + 00098 atan(tan(2*u)))) 00099 00100 def xtest_tensor_algebra(self): # FIXME: Failing! 00101 def v(u): 00102 return as_vector((u, 2*u)) 00103 def w(u): 00104 return as_matrix(((u, 2*u, u+3, u**2), 00105 (7, ln(u), u/5., exp(u)),)) 00106 00107 def f(u): 00108 return dot(v(u),v(u)) 00109 self._test_piecewise_constant_assembly_on_cell(f) 00110 def f(u): 00111 return inner(w(u),w(u)) 00112 self._test_piecewise_constant_assembly_on_cell(f) 00113 def f(u): 00114 z = dot(w(u).T, v(u)) 00115 return dot(z,z) 00116 self._test_piecewise_constant_assembly_on_cell(f) 00117 00118 def xtest_index_notation(self): # FIXME: Failing! 00119 i,j = indices(2) 00120 00121 def f(v): 00122 return v[i]*v[i] 00123 self._test_piecewise_constant_assembly_on_cell(f, True) 00124 00125 def f(v): 00126 return outer(v,v)[i,j]*(2*v)[i]*(v/4.5)[j] 00127 self._test_piecewise_constant_assembly_on_cell(f, True) 00128