File: vector.cpp

package info (click to toggle)
fenics-dolfinx 1%3A0.10.0.post4-1exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 6,028 kB
  • sloc: cpp: 36,535; python: 25,391; makefile: 226; sh: 171; xml: 55
file content (105 lines) | stat: -rw-r--r-- 3,157 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright (C) 2021 Chris Richardson
//
// This file is part of DOLFINx (https://www.fenicsproject.org)
//
// SPDX-License-Identifier:    LGPL-3.0-or-later
//
// Unit tests for Distributed la::Vector

#include <algorithm>
#include <catch2/catch_template_test_macros.hpp>
#include <catch2/catch_test_macros.hpp>
#include <complex>
#include <dolfinx/common/IndexMap.h>
#include <dolfinx/common/MPI.h>
#include <dolfinx/la/Vector.h>

using namespace dolfinx;

namespace
{
template <typename T>
void test_vector()
{
  const int mpi_size = dolfinx::MPI::size(MPI_COMM_WORLD);
  const int mpi_rank = dolfinx::MPI::rank(MPI_COMM_WORLD);
  constexpr int size_local = 100;

  // Create some ghost entries on next process
  int num_ghosts = (mpi_size - 1) * 3;
  std::vector<std::int64_t> ghosts(num_ghosts);
  for (int i = 0; i < num_ghosts; ++i)
    ghosts[i] = (mpi_rank + 1) % mpi_size * size_local + i;

  const std::vector<int> global_ghost_owner(ghosts.size(),
                                            (mpi_rank + 1) % mpi_size);

  // Create an IndexMap
  auto index_map = std::make_shared<common::IndexMap>(
      MPI_COMM_WORLD, size_local, ghosts, global_ghost_owner);

  la::Vector<T> v(index_map, 1);
  std::ranges::fill(v.array(), 1.0);

  double norm2 = la::squared_norm(v);
  CHECK(norm2 == mpi_size * size_local);

  std::ranges::fill(v.array(), mpi_rank);

  double sumn2
      = size_local * (mpi_size - 1) * mpi_size * (2 * mpi_size - 1) / 6;
  CHECK(la::squared_norm(v) == sumn2);
  CHECK(la::norm(v, la::Norm::l2) == std::sqrt(sumn2));
  CHECK(la::inner_product(v, v) == sumn2);
  CHECK(la::norm(v, la::Norm::linf) == static_cast<T>(mpi_size - 1));
}

void test_vector_cast()
{
  using T = double;
  using U = float;

  const int mpi_size = dolfinx::MPI::size(MPI_COMM_WORLD);
  const int mpi_rank = dolfinx::MPI::rank(MPI_COMM_WORLD);
  constexpr int size_local = 100;

  // Create some ghost entries on next process
  int num_ghosts = (mpi_size - 1) * 3;
  std::vector<std::int64_t> ghosts(num_ghosts);
  for (int i = 0; i < num_ghosts; ++i)
    ghosts[i] = (mpi_rank + 1) % mpi_size * size_local + i;

  std::vector<int> global_ghost_owner(ghosts.size(), (mpi_rank + 1) % mpi_size);

  // Create an IndexMap
  auto index_map = std::make_shared<common::IndexMap>(
      MPI_COMM_WORLD, size_local, ghosts, global_ghost_owner);

  la::Vector<T> v(index_map, 1);
  std::ranges::fill(v.array(), 1);

  la::Vector<U, std::vector<U>, std::vector<std::int64_t>> v1(v);

  U norm2 = la::squared_norm(v1);
  CHECK(norm2 == mpi_size * size_local);

  std::ranges::fill(v1.array(), mpi_rank);

  U sumn2 = size_local * (mpi_size - 1) * mpi_size * (2 * mpi_size - 1) / 6;
  CHECK(la::squared_norm(v1) == sumn2);
  CHECK(la::norm(v1, la::Norm::l2) == std::sqrt(sumn2));
  CHECK(la::inner_product(v1, v1) == sumn2);
  CHECK(la::norm(v1, la::Norm::linf) == static_cast<U>(mpi_size - 1));
}
} // namespace

TEMPLATE_TEST_CASE("Linear Algebra Vector", "[la_vector]", double,
                   std::complex<double>)
{
  CHECK_NOTHROW(test_vector<TestType>());
}

TEST_CASE("Linear Algebra Vector", "[la_vector]")
{
  CHECK_NOTHROW(test_vector_cast());
}