Arcane  v4.1.10.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
CustomAdapter.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/*---------------------------------------------------------------------------*/
9/*
10 * This file is based on the work on AMGCL library (version march 2026)
11 * which can be found at https://github.com/ddemidov/amgcl.
12 *
13 * Copyright (c) 2012-2022 Denis Demidov <dennis.demidov@gmail.com>
14 * SPDX-License-Identifier: MIT
15 */
16/*---------------------------------------------------------------------------*/
17/*---------------------------------------------------------------------------*/
18
19#include <iostream>
20#include <vector>
21#include <map>
22
23#include "arccore/alina/BuiltinBackend.h"
24#include "arccore/alina/PreconditionedSolver.h"
25#include "arccore/alina/AMG.h"
26#include "arccore/alina/Coarsening.h"
27#include "arccore/alina/Relaxation.h"
28#include "arccore/alina/ConjugateGradientSolver.h"
29#include "arccore/alina/Profiler.h"
30
31using namespace Arcane;
32
33class sparse_matrix
34{
35 public:
36
37 typedef std::map<int, double> sparse_row;
38
39 sparse_matrix(int n, int m)
40 : _n(n)
41 , _m(m)
42 , _rows(n)
43 {}
44
45 int nrows() const { return _n; }
46 int ncols() const { return _m; }
47
48 // Get a value at row i and column j
49 double operator()(int i, int j) const
50 {
51 sparse_row::const_iterator elem = _rows[i].find(j);
52 return elem == _rows[i].end() ? 0.0 : elem->second;
53 }
54
55 // Get reference to a value at row i and column j
56 double& operator()(int i, int j) { return _rows[i][j]; }
57
58 // Access the whole row
59 const sparse_row& operator[](int i) const { return _rows[i]; }
60
61 private:
62
63 int _n, _m;
64 std::vector<sparse_row> _rows;
65};
66
67namespace Arcane::Alina::backend
68{
69
70// Let AMGCL know the value type of our matrix:
71template <> struct value_type<sparse_matrix>
72{
73 typedef double type;
74};
75
76// Let AMGCL know the size of our matrix:
77template <> struct rows_impl<sparse_matrix>
78{
79 static int get(const sparse_matrix& A) { return A.nrows(); }
80};
81
82template <> struct cols_impl<sparse_matrix>
83{
84 static int get(const sparse_matrix& A) { return A.ncols(); }
85};
86
87template <> struct nonzeros_impl<sparse_matrix>
88{
89 static int get(const sparse_matrix& A)
90 {
91 int n = A.nrows(), nnz = 0;
92 for (int i = 0; i < n; ++i)
93 nnz += A[i].size();
94 return nnz;
95 }
96};
97
98// Allow AMGCL to iterate over the rows of our matrix:
99template <> struct row_iterator<sparse_matrix>
100{
101 struct iterator
102 {
103 sparse_matrix::sparse_row::const_iterator _it, _end;
104
105 iterator(const sparse_matrix& A, int row)
106 : _it(A[row].begin())
107 , _end(A[row].end())
108 {}
109
110 // Check if we are at the end of the row.
111 operator bool() const
112 {
113 return _it != _end;
114 }
115
116 // Advance to the next nonzero element.
117 iterator& operator++()
118 {
119 ++_it;
120 return *this;
121 }
122
123 // Column number of the current nonzero element.
124 int col() const { return _it->first; }
125
126 // Value of the current nonzero element.
127 double value() const { return _it->second; }
128 };
129
130 typedef iterator type;
131};
132
133template <> struct row_begin_impl<sparse_matrix>
134{
135 typedef row_iterator<sparse_matrix>::type iterator;
136 static iterator get(const sparse_matrix& A, int row)
137 {
138 return iterator(A, row);
139 }
140};
141
142} // namespace Arcane::Alina::backend
143
144int main()
145{
146 auto& prof = Alina::Profiler::globalProfiler();
147
148 // Discretize a 1D Poisson problem
149 const int n = 15000;
150
151 auto t_total = prof.scoped_tic("total");
152 sparse_matrix A(n, n);
153 for (int i = 0; i < n; ++i) {
154 if (i == 0 || i == n - 1) {
155 // Dirichlet boundary condition
156 A(i, i) = 1.0;
157 }
158 else {
159 // Internal point.
160 A(i, i - 1) = -1.0;
161 A(i, i) = 2.0;
162 A(i, i + 1) = -1.0;
163 }
164 }
165
166 // Create an AMGCL solver for the problem.
167 typedef Alina::BuiltinBackend<double> Backend;
168
170 Backend,
174 solve(A);
175
176 std::cout << solve.precond() << std::endl;
177
178 auto t_solve = prof.scoped_tic("solve");
179 std::vector<double> f(n, 1.0), x(n, 0.0);
180 solve(f, x);
181
182 std::cout << prof << std::endl;
183}
Algebraic multigrid method.
Definition AMG.h:71
Convenience class that bundles together a preconditioner and an iterative solver.
-*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
Non-smoothed aggregation.
Definition Coarsening.h:639
Sparse approximate interface smoother.
Implementation for function returning the number of columns in a matrix.
Implementation for function returning the number of nonzeros in a matrix.
Implementation for function returning row iterator for a matrix.
Metafunction returning the row iterator type for a matrix type.
Implementation for function returning the number of rows in a matrix.
Metafunction that returns value type of a matrix or a vector type.