Debian Codegraph @

demo/undocumented/eval/cpp/main.cpp

raw
1 // Copyright (C) 2008-2011 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: 2008-03-11
19 // Last changed: 2014-08-11
20 //
21 // Demonstrating function evaluation at arbitrary points.
22
23 #include <dolfin.h>
24 #include "Projection.h"
25
26 using namespace dolfin;
27
28 class F : public Expression
29 {
30 public:
31
32 F() {}
33
34 void eval(Array<double>& values, const Array<double>& x) const
35 {
36 values[0] = sin(3.0*x[0])*sin(3.0*x[1])*sin(3.0*x[2]);
37 }
38
39 };
40
41 int main()
42 {
43 if (dolfin::MPI::size(MPI_COMM_WORLD) > 1)
44 {
45 std::cout << "This demo does not work in parallel" << std::endl;
46 return 0;
47 }
48
49 // Create mesh and a point in the mesh
50 auto mesh = std::make_shared<UnitCubeMesh>(8, 8, 8);
51 Point x(0.31, 0.32, 0.33);
52
53 // A user-defined function
54 auto f = std::make_shared<F>();
55
56 // Project to a discrete function
57 auto V = std::make_shared<Projection::FunctionSpace>(mesh);
58 Projection::BilinearForm a(V, V);
59 Projection::LinearForm L(V);
60 L.f = f;
61 Function g(V);
62 solve(a == L, g);
63
64 // Evaluate user-defined function f
65 info("f(x) = %g", (*f)(x));
66
67 // Evaluate discrete function g (projection of f)
68 info("g(x) = %g", g(x));
69 }