Debian Codegraph @

demo/undocumented/functional/cpp/main.cpp

raw
1 // Copyright (C) 2006-2008 Anders Logg
2 //
3 // This file is part of DOLFIN.
4 //
5 // DOLFIN is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // DOLFIN is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // First added: 2006-09-19
19 // Last changed: 2012-11-12
20 //
21 // This demo program computes the value of the functional
22 //
23 // M(v) = int v^2 + (grad v)^2 dx
24 //
25 // on the unit square for v = sin(x) + cos(y). The exact
26 // value of the functional is M(v) = 2 + 2*sin(1)*(1-cos(1))
27 //
28 // The functional M corresponds to the energy norm for a
29 // simple reaction-diffusion equation.
30
31 #include <dolfin.h>
32 #include "EnergyNorm.h"
33
34 using namespace dolfin;
35
36 int main()
37 {
38 // The function v
39 class MyFunction : public Expression
40 {
41 public:
42
43 void eval(Array<double>& values, const Array<double>& x) const
44 { values[0] = sin(x[0]) + cos(x[1]); }
45
46 };
47
48 // Define functional
49 auto mesh = std::make_shared<UnitSquareMesh>(16, 16);
50 auto v = std::make_shared<MyFunction>();
51 EnergyNorm::Functional M(mesh, v);
52
53 // Evaluate functional
54 double approximate_value = assemble(M);
55
56 // Compute exact value
57 double exact_value = 2.0 + 2.0*sin(1.0)*(1.0 - cos(1.0));
58
59 info("The energy norm of v is: %.15g", approximate_value);
60 info("It should be: %.15g", exact_value);
61
62 return 0;
63 }