Debian Codegraph @

python/demo/undocumented/functional/demo_functional.py

raw
1 """This demo program computes the value of the functional
2
3 M(v) = int v^2 + (grad v)^2 dx
4
5 on the unit square for v = sin(x) + cos(y). The exact
6 value of the functional is M(v) = 2 + 2*sin(1)*(1 - cos(1))
7
8 The functional M corresponds to the energy norm for a
9 simple reaction-diffusion equation."""
10
11 # Copyright (C) 2007 Kristian B. Oelgaard
12 #
13 # This file is part of DOLFIN.
14 #
15 # DOLFIN is free software: you can redistribute it and/or modify
16 # it under the terms of the GNU Lesser General Public License as published by
17 # the Free Software Foundation, either version 3 of the License, or
18 # (at your option) any later version.
19 #
20 # DOLFIN is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 # GNU Lesser General Public License for more details.
24 #
25 # You should have received a copy of the GNU Lesser General Public License
26 # along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
27 #
28 # Modified by Anders Logg, 2008.
29 #
30 # First added: 2007-11-14
31 # Last changed: 2012-11-12
32
33
34 from dolfin import *
35
36 # Create mesh and define function space
37 mesh = UnitSquareMesh(16, 16)
38 V = FunctionSpace(mesh, "CG", 2)
39
40 # Define the function v
41 v = Expression("sin(x[0]) + cos(x[1])",
42 element=V.ufl_element(),
43 domain=mesh)
44
45 # Define functional
46 M = (v*v + dot(grad(v), grad(v)))*dx(mesh)
47
48 # Evaluate functional
49 value = assemble(M)
50
51 exact_value = 2.0 + 2.0*sin(1.0)*(1.0 - cos(1.0))
52 print("The energy norm of v is: %.15g" % value)
53 print("It should be: %.15g" % exact_value)