Arcane  v4.1.10.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
Poisson2D.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 "arccore/alina/AMG.h"
20#include "arccore/alina/Adapters.h"
21#include "arccore/alina/Coarsening.h"
22#include "arccore/alina/PreconditionedSolver.h"
23#include "arccore/alina/Relaxation.h"
24#include "arccore/alina/BiCGStabSolver.h"
25
26#include <iostream>
27#include <vector>
28#include <tuple>
29
30using namespace Arcane;
31
32struct poisson_2d
33{
34 typedef double val_type;
35 typedef long col_type;
36
37 size_t n;
38 double h2i;
39
40 poisson_2d(size_t n)
41 : n(n)
42 , h2i((n - 1) * (n - 1))
43 {}
44
45 size_t rows() const { return n * n; }
46 size_t nonzeros() const { return 5 * rows(); }
47
48 void operator()(size_t row,
49 std::vector<col_type>& col,
50 std::vector<val_type>& val) const
51 {
52 size_t i = row % n;
53 size_t j = row / n;
54
55 if (j > 0) {
56 col.push_back(row - n);
57 val.push_back(-h2i);
58 }
59
60 if (i > 0) {
61 col.push_back(row - 1);
62 val.push_back(-h2i);
63 }
64
65 col.push_back(row);
66 val.push_back(4 * h2i);
67
68 if (i + 1 < n) {
69 col.push_back(row + 1);
70 val.push_back(-h2i);
71 }
72
73 if (j + 1 < n) {
74 col.push_back(row + n);
75 val.push_back(-h2i);
76 }
77 }
78};
79
80int main(int argc, char* argv[])
81{
82 auto& prof = Alina::Profiler::globalProfiler();
83
84 int m = argc > 1 ? atoi(argv[1]) : 1024;
85 int n = m * m;
86
87 using Backend = Alina::BuiltinBackend<double>;
88 std::vector<double> f(n);
89 std::vector<double> x(n);
90
91 prof.tic("build");
92
95
96 Solver solve(Alina::adapter::make_matrix(poisson_2d(m)));
97 prof.toc("build");
98
99 //std::cout << solve.amg() << std::endl;
100
101 std::fill_n(f.data(), n, 1.0);
102 std::fill_n(x.data(), n, 0.0);
103
104 prof.tic("solve");
105 Alina::SolverResult r = solve(f, x);
106 prof.toc("solve");
107
108 std::cout << "Iterations: " << r.nbIteration() << std::endl
109 << "Error: " << r.residual() << std::endl
110 << std::endl;
111
112 std::cout << prof << std::endl;
113}
Algebraic multigrid method.
Definition AMG.h:71
Convenience class that bundles together a preconditioner and an iterative solver.
Result of a solving.
Definition AlinaUtils.h:52
-*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-