~fenics-core/ufl/spatder2grad

43 by Anders Logg
Add script to run all tests
1
#!/usr/bin/env python
2
170.1.3 by Anders Logg
Remove predefined integral objects dx0, dx1 etc. Not needed anymore.
3
__authors__ = "Martin Sandve Alnes"
384 by Martin Sandve Alnæs
Added SpatialCoordinate class with test "a = sin(x[0])*v*dx".
4
__date__ = "2008-03-12 -- 2008-12-02"
170.1.3 by Anders Logg
Remove predefined integral objects dx0, dx1 etc. Not needed anymore.
5
6
# Modified by Anders Logg, 2008
7
978 by Martin Alnæs
Let all test cases inherit testcase base class.
8
from ufltestcase import UflTestCase, main
43 by Anders Logg
Add script to run all tests
9
10
from ufl import *
1425 by Martin Sandve Alnæs
Test and final fix for form multiplication.
11
from ufl.algorithms import *
43 by Anders Logg
Add script to run all tests
12
1133 by Martin Sandve Alnæs
Small test update.
13
class TestMeasure(UflTestCase):
43 by Anders Logg
Add script to run all tests
14
1094 by Martin Alnes
Allow short domain names for measure construction:
15
    def test_manually_constructed_measures(self):
16
        # Since we can't write 'dx = dx[data]' in a non-global scope,
17
        # because of corner cases in the python scope rules,
18
        # it may be convenient to construct measures directly:
19
        domain_data = ('Stokes', 'Darcy')
20
        dx = Measure('dx')[domain_data]
21
        ds = Measure('ds')[domain_data]
22
        dS = Measure('dS')[domain_data]
23
24
        # Possible PyDOLFIN syntax:
25
        #ds = boundaries.dx(3) # return Measure('dx')[self](3)
26
1075 by Martin Sandve Alnæs
Implement support for dX = dx[domaindata]; M=f*dX(1). Should probably add some convenience functionality to preprocess as well.
27
    def test_measures_with_domain_data(self):
1078 by Martin Sandve Alnæs
Rename domaindata -> domain_data, similar to domain_type and domain_id.
28
        # Configure measure with some arbitrary data object as domain_data
29
        domain_data = ('Stokes', 'Darcy')
30
        dX = dx[domain_data]
1075 by Martin Sandve Alnæs
Implement support for dX = dx[domaindata]; M=f*dX(1). Should probably add some convenience functionality to preprocess as well.
31
1078 by Martin Sandve Alnæs
Rename domaindata -> domain_data, similar to domain_type and domain_id.
32
        # Build form with this domain_data
1075 by Martin Sandve Alnæs
Implement support for dX = dx[domaindata]; M=f*dX(1). Should probably add some convenience functionality to preprocess as well.
33
        element = FiniteElement("Lagrange", triangle, 1)
34
        f = Coefficient(element)
35
        a = f*dX(0) + f**2*dX(1)
36
1426 by Martin Sandve Alnæs
Revert some disabled error messages for e.g. f*dx[...] without domain id.
37
        # Check that we get an UFL error when using dX without domain id
38
        self.assertRaises(UFLException, lambda: f*dX)
39
        # Check that we get a Python error when using unsupported type
40
        self.assertRaises(TypeError, lambda: "foo"*dX(1))
1075 by Martin Sandve Alnæs
Implement support for dX = dx[domaindata]; M=f*dX(1). Should probably add some convenience functionality to preprocess as well.
41
1078 by Martin Sandve Alnæs
Rename domaindata -> domain_data, similar to domain_type and domain_id.
42
        # Check that we get the right domain_data from the preprocessed form data
1083 by Martin Sandve Alnæs
Added test for new domain data extraction in preprocess.
43
        fd = a.compute_form_data()
44
        self.assertIs(fd.domain_data['cell'], domain_data)
45
        self.assertIs(fd.cell_domain_data, domain_data)
46
        self.assertIsNone(fd.exterior_facet_domain_data)
47
48
        # Check that integral_data list is consistent as well
1075 by Martin Sandve Alnæs
Implement support for dX = dx[domaindata]; M=f*dX(1). Should probably add some convenience functionality to preprocess as well.
49
        f2 = f.reconstruct(count=0)
50
        for itd in fd.integral_data:
51
            t, i, itg, md = itd
52
            self.assertEqual(t, 'cell')
53
            self.assertEqual(md, {})
54
            self.assertEqual(itg[0].integrand(), f2**(i+1))
1083 by Martin Sandve Alnæs
Added test for new domain data extraction in preprocess.
55
            self.assertIs(itg[0].measure().domain_data(), domain_data)
1075 by Martin Sandve Alnæs
Implement support for dX = dx[domaindata]; M=f*dX(1). Should probably add some convenience functionality to preprocess as well.
56
1133 by Martin Sandve Alnæs
Small test update.
57
58
class TestIntegrals(UflTestCase):
59
135 by Martin Sandve Alnæs
Added automatic matching of integrals over same domain, i.e.:
60
    def test_separated_dx(self):
61
        "Tests automatic summation of integrands over same domain."
384 by Martin Sandve Alnæs
Added SpatialCoordinate class with test "a = sin(x[0])*v*dx".
62
        element = FiniteElement("Lagrange", triangle, 1)
135 by Martin Sandve Alnæs
Added automatic matching of integrals over same domain, i.e.:
63
        v = TestFunction(element)
870 by Anders Logg
Rename BasisFunctions --> Arguments, Functions --> Coefficients
64
        f = Coefficient(element)
1425 by Martin Sandve Alnæs
Test and final fix for form multiplication.
65
        a = f*v*dx(0) + 2*v*ds + 3*v*dx(0) + 7*v*ds + 3*v*dx(2) + 7*v*dx(2)
66
        b = (f*v + 3*v)*dx(0) + (2*v + 7*v)*ds + (3*v + 7*v)*dx(2)
1009 by Martin Alnæs
Regexp editing: using assertEqual(a, b) instead of assertTrue(a == b) everywhere. This can give better error messages when tests fail.
67
        self.assertEqual(repr(a), repr(b))
43 by Anders Logg
Add script to run all tests
68
1440 by Martin Sandve Alnæs
Allow adding 0 to a form, returning self.
69
    def test_adding_zero(self):
70
        element = FiniteElement("Lagrange", triangle, 1)
71
        v = TestFunction(element)
72
        a = v*dx
73
        b = a + 0
74
        self.assertEqual(id(a), id(b))
1441 by Martin Sandve Alnæs
Added radd to Form to allow 0+a.
75
        b = 0 + a
76
        self.assertEqual(id(a), id(b))
77
        b = sum([a, 2*a])
78
        self.assertEqual(b, a+2*a)
1440 by Martin Sandve Alnæs
Allow adding 0 to a form, returning self.
79
1171 by Martin Sandve Alnæs
Allowing multiplication of Form and Integral by an expression that is globally spatially constant (not piecewise), including arbitrary expressions depending on coefficients in a Real space.
80
class TestFormScaling(UflTestCase):
81
82
    def test_scalar_mult_form(self):
83
        R = FiniteElement("Real", triangle, 0)
84
        element = FiniteElement("Lagrange", triangle, 1)
85
        v = TestFunction(element)
86
        f = Coefficient(element)
87
        c = Coefficient(R)
88
        # These should be acceptable:
89
        self.assertEqual(0*(v*dx), (0*v)*dx)
90
        self.assertEqual(3*(v*dx), (3*v)*dx)
91
        self.assertEqual(3.14*(v*dx), (3.14*v)*dx)
92
        self.assertEqual(c*(v*dx), (c*v)*dx)
93
        self.assertEqual((c**c+c/3)*(v*dx), ((c**c+c/3)*v)*dx)
94
        # These should not be acceptable:
1425 by Martin Sandve Alnæs
Test and final fix for form multiplication.
95
        self.assertRaises(TypeError, lambda: f*(v*dx))
96
        self.assertRaises(TypeError, lambda: (f/2)*(v*dx))
97
        self.assertRaises(TypeError, lambda: (c*f)*(v*dx))
98
99
    def test_action_mult_form(self):
100
        V = FiniteElement("CG", triangle, 1)
101
        u = TrialFunction(V)
102
        v = TrialFunction(V)
103
        f = Coefficient(V)
104
        a = u*v*dx
105
        self.assertEqual(a*f, action(a,f))
106
        self.assertRaises(TypeError, lambda: a*"foo")
107
1133 by Martin Sandve Alnæs
Small test update.
108
109
class TestExampleForms(UflTestCase):
110
43 by Anders Logg
Add script to run all tests
111
    def test_source1(self):
384 by Martin Sandve Alnæs
Added SpatialCoordinate class with test "a = sin(x[0])*v*dx".
112
        element = FiniteElement("Lagrange", triangle, 1)
43 by Anders Logg
Add script to run all tests
113
        v = TestFunction(element)
870 by Anders Logg
Rename BasisFunctions --> Arguments, Functions --> Coefficients
114
        f = Coefficient(element)
43 by Anders Logg
Add script to run all tests
115
        a = f*v*dx
1075 by Martin Sandve Alnæs
Implement support for dX = dx[domaindata]; M=f*dX(1). Should probably add some convenience functionality to preprocess as well.
116
43 by Anders Logg
Add script to run all tests
117
    def test_source2(self):
384 by Martin Sandve Alnæs
Added SpatialCoordinate class with test "a = sin(x[0])*v*dx".
118
        element = VectorElement("Lagrange", triangle, 1)
43 by Anders Logg
Add script to run all tests
119
        v = TestFunction(element)
870 by Anders Logg
Rename BasisFunctions --> Arguments, Functions --> Coefficients
120
        f = Coefficient(element)
43 by Anders Logg
Add script to run all tests
121
        a = dot(f,v)*dx
1075 by Martin Sandve Alnæs
Implement support for dX = dx[domaindata]; M=f*dX(1). Should probably add some convenience functionality to preprocess as well.
122
43 by Anders Logg
Add script to run all tests
123
    def test_source3(self):
384 by Martin Sandve Alnæs
Added SpatialCoordinate class with test "a = sin(x[0])*v*dx".
124
        element = TensorElement("Lagrange", triangle, 1)
43 by Anders Logg
Add script to run all tests
125
        v = TestFunction(element)
870 by Anders Logg
Rename BasisFunctions --> Arguments, Functions --> Coefficients
126
        f = Coefficient(element)
43 by Anders Logg
Add script to run all tests
127
        a = inner(f,v)*dx
128
384 by Martin Sandve Alnæs
Added SpatialCoordinate class with test "a = sin(x[0])*v*dx".
129
    def test_source4(self):
130
        element = FiniteElement("Lagrange", triangle, 1)
131
        v = TestFunction(element)
408 by Martin Sandve Alnæs
- Work on changing Variable to a non-terminal.
132
        x = triangle.x
384 by Martin Sandve Alnæs
Added SpatialCoordinate class with test "a = sin(x[0])*v*dx".
133
        f = sin(x[0])
134
        a = f*v*dx
43 by Anders Logg
Add script to run all tests
135
1075 by Martin Sandve Alnæs
Implement support for dX = dx[domaindata]; M=f*dX(1). Should probably add some convenience functionality to preprocess as well.
136
43 by Anders Logg
Add script to run all tests
137
    def test_mass1(self):
384 by Martin Sandve Alnæs
Added SpatialCoordinate class with test "a = sin(x[0])*v*dx".
138
        element = FiniteElement("Lagrange", triangle, 1)
43 by Anders Logg
Add script to run all tests
139
        v = TestFunction(element)
140
        u = TrialFunction(element)
141
        a = u*v*dx
142
143
    def test_mass2(self):
384 by Martin Sandve Alnæs
Added SpatialCoordinate class with test "a = sin(x[0])*v*dx".
144
        element = FiniteElement("Lagrange", triangle, 1)
43 by Anders Logg
Add script to run all tests
145
        v = TestFunction(element)
146
        u = TrialFunction(element)
147
        a = u*v*dx
1075 by Martin Sandve Alnæs
Implement support for dX = dx[domaindata]; M=f*dX(1). Should probably add some convenience functionality to preprocess as well.
148
43 by Anders Logg
Add script to run all tests
149
    def test_mass3(self):
384 by Martin Sandve Alnæs
Added SpatialCoordinate class with test "a = sin(x[0])*v*dx".
150
        element = VectorElement("Lagrange", triangle, 1)
43 by Anders Logg
Add script to run all tests
151
        v = TestFunction(element)
152
        u = TrialFunction(element)
153
        a = dot(u,v)*dx
1075 by Martin Sandve Alnæs
Implement support for dX = dx[domaindata]; M=f*dX(1). Should probably add some convenience functionality to preprocess as well.
154
43 by Anders Logg
Add script to run all tests
155
    def test_mass4(self):
384 by Martin Sandve Alnæs
Added SpatialCoordinate class with test "a = sin(x[0])*v*dx".
156
        element = TensorElement("Lagrange", triangle, 1)
43 by Anders Logg
Add script to run all tests
157
        v = TestFunction(element)
158
        u = TrialFunction(element)
159
        a = inner(u,v)*dx
160
161
162
    def test_stiffness1(self):
384 by Martin Sandve Alnæs
Added SpatialCoordinate class with test "a = sin(x[0])*v*dx".
163
        element = FiniteElement("Lagrange", triangle, 1)
43 by Anders Logg
Add script to run all tests
164
        v = TestFunction(element)
165
        u = TrialFunction(element)
166
        a = dot(grad(u), grad(v)) * dx
167
168
    def test_stiffness2(self):
384 by Martin Sandve Alnæs
Added SpatialCoordinate class with test "a = sin(x[0])*v*dx".
169
        element = FiniteElement("Lagrange", triangle, 1)
43 by Anders Logg
Add script to run all tests
170
        v = TestFunction(element)
171
        u = TrialFunction(element)
172
        a = inner(grad(u), grad(v)) * dx
173
174
    def test_stiffness3(self):
384 by Martin Sandve Alnæs
Added SpatialCoordinate class with test "a = sin(x[0])*v*dx".
175
        element = VectorElement("Lagrange", triangle, 1)
43 by Anders Logg
Add script to run all tests
176
        v = TestFunction(element)
177
        u = TrialFunction(element)
178
        a = inner(grad(u), grad(v)) * dx
179
180
1209 by Martin Sandve Alnæs
Added nabla_grad based forms to test_forms.py, turns out both the navier stokes test and anisotropic poisson test was using grad wrong...
181
    def test_nonnabla_stiffness_with_conductivity(self):
182
        velement = VectorElement("Lagrange", triangle, 1)
183
        telement = TensorElement("Lagrange", triangle, 1)
184
        v = TestFunction(velement)
185
        u = TrialFunction(velement)
186
        M = Coefficient(telement)
187
        a = inner(grad(u)*M.T, grad(v)) * dx
188
189
    def test_nabla_stiffness_with_conductivity(self):
190
        velement = VectorElement("Lagrange", triangle, 1)
191
        telement = TensorElement("Lagrange", triangle, 1)
192
        v = TestFunction(velement)
193
        u = TrialFunction(velement)
194
        M = Coefficient(telement)
195
        a = inner(M*nabla_grad(u), nabla_grad(v)) * dx
196
197
198
    def test_nonnabla_navier_stokes(self):
199
        cell = triangle
200
        velement = VectorElement("Lagrange", cell, 2)
201
        pelement = FiniteElement("Lagrange", cell, 1)
202
        TH = velement * pelement
203
204
        v, q = TestFunctions(TH)
205
        u, p = TrialFunctions(TH)
206
207
        f = Coefficient(velement)
208
        w = Coefficient(velement)
209
        Re = Constant(cell)
210
        dt = Constant(cell)
211
212
        a = (dot(u, v) + dt*dot(grad(u)*w, v)
213
            - dt*Re*inner(grad(u), grad(v))
214
            + dt*dot(grad(p), v))*dx
215
        L = dot(f, v)*dx
216
        b = dot(u, grad(q))*dx
217
218
    def test_nabla_navier_stokes(self):
219
        cell = triangle
220
        velement = VectorElement("Lagrange", cell, 2)
221
        pelement = FiniteElement("Lagrange", cell, 1)
222
        TH = velement * pelement
223
224
        v, q = TestFunctions(TH)
225
        u, p = TrialFunctions(TH)
226
227
        f = Coefficient(velement)
228
        w = Coefficient(velement)
229
        Re = Constant(cell)
230
        dt = Constant(cell)
231
232
        a = (dot(u, v) + dt*dot(dot(w,nabla_grad(u)), v)
233
            - dt*Re*inner(grad(u), grad(v))
234
            + dt*dot(grad(p), v))*dx
235
        L = dot(f, v)*dx
236
        b = dot(u, grad(q))*dx
237
43 by Anders Logg
Add script to run all tests
238
239
if __name__ == "__main__":
978 by Martin Alnæs
Let all test cases inherit testcase base class.
240
    main()