Debian Codegraph @

test/unit/cpp/la/LinearOperator.cpp

raw
1 // Copyright (C) 2012 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 // Unit tests for matrix-free linear solvers (LinearOperator)
19
20 #include <dolfin.h>
21 #include "forms/ReactionDiffusion.h"
22 #include "forms/ReactionDiffusionAction.h"
23 #include <catch.hpp>
24
25 using namespace dolfin;
26
27 //-----------------------------------------------------------------------------
28 TEST_CASE("Testing LinearOperator", "[linear_operator]")
29 {
30 // Backends supporting the LinearOperator interface
31 //std::vector<std::string> backends = {"PETSc", "Eigen"};
32 std::vector<std::string> backends = {"PETSc"};
33
34 // Define linear operator
35 class MyLinearOperator : public LinearOperator
36 {
37 public:
38
39 MyLinearOperator(Form& a_action, Function& u)
40 : LinearOperator(*u.vector(), *u.vector()),
41 a_action(a_action), u(u)
42 {
43 // Do nothing
44 }
45
46 std::size_t size(std::size_t dim) const
47 { return u.function_space()->dim(); }
48
49 void mult(const GenericVector& x, GenericVector& y) const
50 {
51 // Update coefficient vector
52 *u.vector() = x;
53
54 // Assemble action
55 Assembler assembler;
56 assembler.assemble(y, a_action);
57 }
58
59 private:
60
61 Form& a_action;
62 Function& u;
63
64 };
65
66 SECTION("using linear operator")
67 {
68 // Iterate over backends supporting linear operators
69 for (std::size_t i = 0; i < backends.size(); i++)
70 {
71 // Check whether backend is available
72 if (!has_linear_algebra_backend(backends[i]))
73 continue;
74
75 // Skip testing Eigen in parallel
76 if (dolfin::MPI::size(MPI_COMM_WORLD) > 1
77 && backends[i] == "Eigen")
78 {
79 info("Not running Eigen test in parallel");
80 continue;
81 }
82
83 // Set linear algebra backend
84 parameters["linear_algebra_backend"] = backends[i];
85
86 // Compute reference value by solving ordinary linear system
87 auto mesh = std::make_shared<UnitSquareMesh>(8, 8);
88 auto V = std::make_shared<ReactionDiffusion::FunctionSpace>(mesh);
89 ReactionDiffusion::BilinearForm a(V, V);
90 ReactionDiffusion::LinearForm L(V);
91 auto f = std::make_shared<Constant>(1.0);
92 L.f = f;
93 Matrix A;
94 Vector x, b;
95 assemble(A, a);
96 assemble(b, L);
97 solve(A, x, b, "gmres", "none");
98 const double norm_ref = norm(x, "l2");
99
100 //continue;
101
102 // Solve using linear operator defined by form action
103 ReactionDiffusionAction::LinearForm a_action(V);
104 auto u = std::make_shared<Function>(V);
105 a_action.u = u;
106 MyLinearOperator O(a_action, *u);
107 solve(O, x, b, "gmres", "none");
108 const double norm_action = norm(x, "l2");
109
110 // Check results
111 CHECK(norm_ref == Approx(norm_action));
112 }
113 }
114 }
115 //-----------------------------------------------------------------------------