Arcane  4.2.1.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
BasicSolver.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 * Ce fichier est basé sur le travail sur la bibliothèque AMGCL (version mars 2026)
11 * qui peut être trouvée à 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 <string>
21
22#include "arccore/base/PlatformUtils.h"
23#include "arccore/base/String.h"
24#include "arccore/base/Convert.h"
25
26#include "arccore/alina/BuiltinBackend.h"
27#include "arccore/alina/StaticMatrix.h"
28#include "arccore/alina/Adapters.h"
29#include "arccore/alina/Relaxation.h"
30#include "arccore/alina/Coarsening.h"
31#include "arccore/alina/BiCGStabSolver.h"
32#include "arccore/alina/PreconditionedSolver.h"
33#include "arccore/alina/AMG.h"
34#include "arccore/alina/Adapters.h"
35#include "arccore/alina/IO.h"
36
37#include "arccore/alina/SolverRuntime.h"
38#include "arccore/alina/PreconditionerRuntime.h"
39
40#include "arccore/alina/Profiler.h"
41
42#include "arccore/common/internal/ProgramOptions.h"
43
44#include "AlinaSamplesCommon.h"
45#include "arccore/trace/ITraceMng.h"
46
47#include "SampleProblemCommon.h"
48
49using namespace Arcane;
50using Alina::precondition;
51
52/*---------------------------------------------------------------------------*/
53/*---------------------------------------------------------------------------*/
54
57
58/*---------------------------------------------------------------------------*/
59/*---------------------------------------------------------------------------*/
60
61extern "C++" void
62_doHypreSolver(int nb_row,
63 std::vector<ptrdiff_t> const& ptr,
64 std::vector<ptrdiff_t> const& col,
65 std::vector<double> const& val,
66 std::vector<double> const& rhs,
67 std::vector<double>& x,
68 int argc, char* argv[]);
69
70/*---------------------------------------------------------------------------*/
71/*---------------------------------------------------------------------------*/
72
74solve(const Alina::PropertyTree& prm,
75 size_t rows,
76 std::vector<ptrdiff_t> const& ptr,
77 std::vector<ptrdiff_t> const& col,
78 std::vector<double> const& val,
79 std::vector<double> const& rhs,
80 std::vector<double>& x)
81{
82 std::cout << "Using scalar solve ptr_size=" << sizeof(ptrdiff_t)
83 << " ptr_type_size=" << sizeof(Backend::ptr_type)
84 << " col_type_size=" << sizeof(Backend::col_type)
85 << " value_type_size=" << sizeof(Backend::value_type)
86 << "\n";
87 auto& prof = Alina::Profiler::globalProfiler();
88 Backend::params bprm;
89
91 //using Solver = Alina::PreconditionedSolver<Precond, Alina::BiCGStabSolver<Backend>>;
93 //using Solver = Alina::PreconditionedSolver<Precond, Alina::SolverRuntime<Backend>>;
94 //using Solver = Alina::PreconditionedSolver<Alina::PreconditionerRuntime<Backend>, Alina::BiCGStabSolver<Backend>>;
95
97
98 {
99 prof.tic("setup");
100 Solver solve(std::tie(rows, ptr, col, val), prm, bprm);
101 prof.toc("setup");
102
103 std::cout << "Printing solver infos\n";
104 std::cout << solve << std::endl;
106 solve.prm.get(ptree);
107 std::cout << "SOLVER PARAMS: " << ptree << "\n";
108
109 auto f_b = Backend::copy_vector(rhs, bprm);
110 auto x_b = Backend::copy_vector(x, bprm);
111
112 prof.tic("solve");
113 info = solve(*f_b, *x_b);
114 prof.toc("solve");
115 }
116
117 return info;
118}
119
120//---------------------------------------------------------------------------
121
122int main2(const Alina::SampleMainContext& ctx, int argc, char* argv[])
123{
124 ITraceMng* tm = ctx.traceMng();
125
126 auto& prof = Alina::Profiler::globalProfiler();
127 namespace po = Arcane::ProgramOptions;
128 namespace io = Alina::IO;
129
130 using std::string;
131 using std::vector;
132
133 po::options_description desc("Options");
134
135 desc.add_options()("help,h", "Affiche cette aide.");
136 desc.add_options()("prm-file,P", po::value<string>(),
137 "Fichier de paramètres au format json. ");
138 desc.add_options()("prm,p", po::value<vector<string>>()->multitoken(),
139 "Paramètres spécifiés sous forme de paires nom=valeur. "
140 "Peut être fourni plusieurs fois. Exemples:\n"
141 " -p solver.tol=1e-3\n"
142 " -p precond.coarse_enough=300");
143
144 desc.add_options()("size,n",
145 po::value<int>()->default_value(32),
146 "La taille du problème de Poisson à résoudre lorsqu'aucune matrice de système n'est donnée. "
147 "Spécifié comme le nombre de nœuds de grille le long de chaque dimension d'un cube unité. "
148 "Le système résultant aura n*n*n inconnues. ");
149
150 desc.add_options()("anisotropy,a",
151 po::value<double>()->default_value(1.0),
152 "La valeur d'anisotropie pour la valeur de Poisson générée. "
153 "Utilisée pour déterminer la mise à l'échelle du problème le long des axes X, Y et Z : "
154 "hy = hx * a, hz = hy * a.");
155
157 p.add("prm", -1);
158
160 po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
161 po::notify(vm);
162
163 if (vm.count("help")) {
164 tm->info() << desc;
165 return 0;
166 }
167
168 for (int i = 0; i < argc; ++i) {
169 if (i)
170 std::cout << " ";
171 std::cout << argv[i];
172 }
173 std::cout << std::endl;
174
176 if (vm.count("prm-file")) {
177 prm.read_json(vm["prm-file"].as<string>());
178 }
179
180 if (vm.count("prm")) {
181 for (const string& v : vm["prm"].as<vector<string>>()) {
182 prm.putKeyValue(v);
183 }
184 }
185
186 size_t rows = vm["size"].as<int>();
187 vector<ptrdiff_t> ptr, col;
188 vector<double> val, rhs, null, x;
189 std::cout << "ROWS=" << rows << "\n";
190 {
191 auto t = prof.scoped_tic("assembling");
192 rows = sample_problem(rows, val, col, ptr, rhs, vm["anisotropy"].as<double>());
193 }
194
195 x.resize(rows, 0.0);
196
197 String do_hypre_str = Platform::getEnvironmentVariable("ALINA_USE_HYPRE");
198 bool do_hypre = false;
199 if (auto v = Convert::Type<Int32>::tryParseFromEnvironment("ALINA_USE_HYPRE", true))
200 do_hypre = v.value();
201
202 Alina::SolverResult solver_result;
203 if (do_hypre) {
204 _doHypreSolver(rows, ptr, col, val, rhs, x, argc, argv);
205 }
206 else {
207
208 solver_result = solve(prm, rows, ptr, col, val, rhs, x);
209
210 if (vm.count("output")) {
211 auto t = prof.scoped_tic("write");
212 Alina::IO::mm_write(vm["output"].as<string>(), &x[0], x.size());
213 }
214 }
215 std::cout << "Iterations: " << solver_result.nbIteration() << std::endl
216 << "Error: " << solver_result.residual() << std::endl
217 << prof << std::endl;
218 return 0;
219}
220
221/*---------------------------------------------------------------------------*/
222/*---------------------------------------------------------------------------*/
223
224int main(int argc, char* argv[])
225{
226 return Arcane::Alina::SampleMainContext::execMain(main2, argc, argv);
227}
228
229/*---------------------------------------------------------------------------*/
230/*---------------------------------------------------------------------------*/
Algebraic multigrid method.
Definition AMG.h:71
Convenience class that bundles together a preconditioner and an iterative solver.
Classe pour stocker les paramètres sous forme d'arbre hiérarchique clé/valeur.
Definition AlinaUtils.h:112
Résultat d'une solution.
Definition AlinaUtils.h:53
Classe template pour convertir un type.
Interface du gestionnaire de traces.
virtual TraceMessage info()=0
Flot pour un message d'information.
Constructeur d'analyseur d'arguments en ligne de commande fluide.
Décrit un ensemble d'options en ligne de commande.
Décrit les arguments positionnels (non-options).
Stocke les valeurs d'options analysées.
Chaîne de caractères unicode.
String getEnvironmentVariable(const String &name)
Variable d'environnement du nom name.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --