Debian Codegraph @

test/unit/cpp/la/Vector.cpp

raw
1 // Copyright (C) 2007 Johan Hake
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 Selected methods for GenericVector
19
20 #include <dolfin.h>
21 #include <catch.hpp>
22
23 using namespace dolfin;
24
25 namespace
26 {
27 void _test_operators(MPI_Comm comm)
28 {
29 Vector v(comm, 10), u(comm, 10);
30 v = 0.0;
31 u = 0.0;
32 CHECK(v.sum() == 0.0);
33
34 // operator=(double a)
35 v = 1.0;
36 CHECK(v.sum() == v.size());
37
38 // operator=(const GenericVector& x)
39 u = v;
40 CHECK(u.sum() == u.size());
41
42 // operator+=(const GenericVector& x)
43 u += v;
44 CHECK(u.sum() == 2*u.size());
45
46 // operator-=(const GenericVector& x)
47 u -= v;
48 u -= v;
49 CHECK(u.sum() == 0.0);
50
51 // operator*=(double a)
52 v *= 5.0;
53 CHECK(v.sum() == v.size()*5.0);
54
55 // operator/=(double a)
56 v /= 2.0;
57 CHECK(v.sum() == 2.5*v.size());
58
59 // operator*=(const GenericVector& x)
60 u = 2.0;
61 v*=u;
62 CHECK(v.sum() == v.size()*5.0);
63 }
64 }
65
66
67 TEST_CASE("Test Vector", "[test_vector]")
68 {
69 SECTION("backends")
70 {
71 // Eigen
72 parameters["linear_algebra_backend"] = "Eigen";
73 _test_operators(MPI_COMM_SELF);
74
75 // PETSc
76 #ifdef HAS_PETSC
77 parameters["linear_algebra_backend"] = "PETSc";
78 _test_operators(MPI_COMM_WORLD);
79 #endif
80 }
81
82 SECTION("init")
83 {
84 // Create local and distributed vector layouts
85
86 // Create local vector layout
87 TensorLayout layout_local(MPI_COMM_SELF, 0, TensorLayout::Sparsity::DENSE);
88 std::vector<std::shared_ptr<const IndexMap>> index_maps(1);
89 index_maps[0].reset(new IndexMap(MPI_COMM_SELF, 203, 1));
90 layout_local.init(index_maps, TensorLayout::Ghosts::UNGHOSTED);
91
92 // Create distributed vector layout
93 TensorLayout layout_distributed(MPI_COMM_WORLD, 0, TensorLayout::Sparsity::DENSE);
94 auto lrange = dolfin::MPI::local_range(MPI_COMM_WORLD, 203);
95 std::size_t nlocal = lrange.second - lrange.first;
96 index_maps[0].reset(new IndexMap(MPI_COMM_SELF, nlocal, 1));
97 layout_distributed.init(index_maps, TensorLayout::Ghosts::UNGHOSTED);
98
99 // Vector
100 #ifdef HAS_PETSC
101 parameters["linear_algebra_backend"] = "PETSc";
102 {
103 Vector x;
104 x.init(layout_local);
105 CHECK(x.size() == (std::size_t) 203);
106
107 Vector y;
108 y.init(layout_distributed);
109 CHECK(x.size() == (std::size_t) 203);
110 }
111 #endif
112
113 // Eigen
114 {
115 EigenVector x;
116 x.init(layout_local);
117 CHECK(x.size() == (std::size_t) 203);
118 }
119
120 // PETSc
121 #ifdef HAS_PETSC
122 {
123 PETScVector x;
124 x.init(layout_local);
125 CHECK(x.size() == (std::size_t) 203);
126
127 PETScVector y;
128 y.init(layout_distributed);
129 CHECK(y.size() == (std::size_t) 203);
130 }
131 #endif
132 }
133
134 SECTION("test_get_local_empty")
135 {
136 // Create local and distributed vector layouts
137 const std::vector<std::size_t> dims(1, 203);
138
139 // Create local vector layout
140 TensorLayout layout_local(MPI_COMM_SELF, 0, TensorLayout::Sparsity::DENSE);
141 std::vector<std::shared_ptr<const IndexMap>> index_maps(1);
142 index_maps[0].reset(new IndexMap(MPI_COMM_SELF, 203, 1));
143 layout_local.init(index_maps, TensorLayout::Ghosts::UNGHOSTED);
144
145 // Create distributed vector layout
146 TensorLayout layout_distributed(MPI_COMM_WORLD, 0, TensorLayout::Sparsity::DENSE);
147 auto lrange = dolfin::MPI::local_range(MPI_COMM_WORLD, 203);
148 std::size_t nlocal = lrange.second - lrange.first;
149 index_maps[0].reset(new IndexMap(MPI_COMM_SELF, nlocal, 1));
150 layout_distributed.init(index_maps, TensorLayout::Ghosts::UNGHOSTED);
151
152 // Vector
153 #ifdef HAS_PETSC
154 parameters["linear_algebra_backend"] = "PETSc";
155 {
156 Vector x;
157 x.init(layout_local);
158 CHECK(x.size() == (std::size_t) 203);
159
160 Vector y;
161 y.init(layout_distributed);
162 CHECK(y.size() == (std::size_t) 203);
163
164 //:get_local(double* block, std::size_t m,
165 // const dolfin::la_index* rows) const
166
167 double* block = NULL;
168 dolfin::la_index* rows = NULL;
169 x.get_local(block, 0, rows);
170 y.get_local(block, 0, rows);
171 }
172 #endif
173 }
174
175 }