Arcane  v4.1.10.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 * 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 <string>
21
22#include <boost/program_options.hpp>
23
24#include "arccore/alina/BuiltinBackend.h"
25#include "arccore/alina/StaticMatrix.h"
26#include "arccore/alina/Adapters.h"
27
28#include "arcane/utils/PlatformUtils.h"
29#include "arcane/utils/String.h"
30#include "arcane/utils/Convert.h"
31
32#include "arccore/alina/Relaxation.h"
33#include "arccore/alina/Coarsening.h"
34#include "arccore/alina/BiCGStabSolver.h"
35#include "arccore/alina/PreconditionedSolver.h"
36#include "arccore/alina/AMG.h"
37#include "arccore/alina/Adapters.h"
38#include "arccore/alina/IO.h"
39
40#include "arccore/alina/SolverRuntime.h"
41#include "arccore/alina/PreconditionerRuntime.h"
42
43#include "arccore/alina/Profiler.h"
44
45#include "AlinaSamplesCommon.h"
46#include "arccore/trace/ITraceMng.h"
47
48#include "SampleProblemCommon.h"
49
50using namespace Arcane;
51using Alina::precondition;
52
53/*---------------------------------------------------------------------------*/
54/*---------------------------------------------------------------------------*/
55
58
59/*---------------------------------------------------------------------------*/
60/*---------------------------------------------------------------------------*/
61
62extern "C++" void
63_doHypreSolver(int nb_row,
64 std::vector<ptrdiff_t> const& ptr,
65 std::vector<ptrdiff_t> const& col,
66 std::vector<double> const& val,
67 std::vector<double> const& rhs,
68 std::vector<double>& x,
69 int argc, char* argv[]);
70
71/*---------------------------------------------------------------------------*/
72/*---------------------------------------------------------------------------*/
73
75solve(const Alina::PropertyTree& prm,
76 size_t rows,
77 std::vector<ptrdiff_t> const& ptr,
78 std::vector<ptrdiff_t> const& col,
79 std::vector<double> const& val,
80 std::vector<double> const& rhs,
81 std::vector<double>& x)
82{
83 std::cout << "Using scalar solve ptr_size=" << sizeof(ptrdiff_t)
84 << " ptr_type_size=" << sizeof(Backend::ptr_type)
85 << " col_type_size=" << sizeof(Backend::col_type)
86 << " value_type_size=" << sizeof(Backend::value_type)
87 << "\n";
88 auto& prof = Alina::Profiler::globalProfiler();
89 Backend::params bprm;
90
92 //using Solver = Alina::PreconditionedSolver<Precond, Alina::BiCGStabSolver<Backend>>;
94 //using Solver = Alina::PreconditionedSolver<Alina::PreconditionerRuntime<Backend>, Alina::SolverRuntime<Backend>>;
95 //using Solver = Alina::PreconditionedSolver<Alina::PreconditionerRuntime<Backend>, Alina::BiCGStabSolver<Backend>>;
96
98
99 {
100 prof.tic("setup");
101 Solver solve(std::tie(rows, ptr, col, val), prm, bprm);
102 prof.toc("setup");
103
104 std::cout << solve << std::endl;
105
106 auto f_b = Backend::copy_vector(rhs, bprm);
107 auto x_b = Backend::copy_vector(x, bprm);
108
109 prof.tic("solve");
110 info = solve(*f_b, *x_b);
111 prof.toc("solve");
112 }
113
114 return info;
115}
116
117//---------------------------------------------------------------------------
118
119int main2(const Alina::SampleMainContext& ctx, int argc, char* argv[])
120{
121 ITraceMng* tm = ctx.traceMng();
122
123 auto& prof = Alina::Profiler::globalProfiler();
124 namespace po = boost::program_options;
125 namespace io = Alina::IO;
126
127 using std::string;
128 using std::vector;
129
130 po::options_description desc("Options");
131
132 desc.add_options()("help,h", "Show this help.");
133 desc.add_options()("prm-file,P", po::value<string>(),
134 "Parameter file in json format. ");
135 desc.add_options()("prm,p", po::value<vector<string>>()->multitoken(),
136 "Parameters specified as name=value pairs. "
137 "May be provided multiple times. Examples:\n"
138 " -p solver.tol=1e-3\n"
139 " -p precond.coarse_enough=300");
140
141 desc.add_options()("size,n",
142 po::value<int>()->default_value(32),
143 "The size of the Poisson problem to solve when no system matrix is given. "
144 "Specified as number of grid nodes along each dimension of a unit cube. "
145 "The resulting system will have n*n*n unknowns. ");
146
147 desc.add_options()("anisotropy,a",
148 po::value<double>()->default_value(1.0),
149 "The anisotropy value for the generated Poisson value. "
150 "Used to determine problem scaling along X, Y, and Z axes: "
151 "hy = hx * a, hz = hy * a.");
152
153 po::positional_options_description p;
154 p.add("prm", -1);
155
156 po::variables_map vm;
157 po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
158 po::notify(vm);
159
160 if (vm.count("help")) {
161 tm->info() << desc;
162 return 0;
163 }
164
165 for (int i = 0; i < argc; ++i) {
166 if (i)
167 std::cout << " ";
168 std::cout << argv[i];
169 }
170 std::cout << std::endl;
171
173 if (vm.count("prm-file")) {
174 prm.read_json(vm["prm-file"].as<string>());
175 }
176
177 if (vm.count("prm")) {
178 for (const string& v : vm["prm"].as<vector<string>>()) {
179 prm.putKeyValue(v);
180 }
181 }
182
183 size_t rows = vm["size"].as<int>();
184 vector<ptrdiff_t> ptr, col;
185 vector<double> val, rhs, null, x;
186 std::cout << "ROWS=" << rows << "\n";
187 {
188 auto t = prof.scoped_tic("assembling");
189 rows = sample_problem(rows, val, col, ptr, rhs, vm["anisotropy"].as<double>());
190 }
191
192 x.resize(rows, 0.0);
193
194 String do_hypre_str = Platform::getEnvironmentVariable("ALINA_USE_HYPRE");
195 bool do_hypre = false;
196 if (auto v = Convert::Type<Int32>::tryParseFromEnvironment("ALINA_USE_HYPRE", true))
197 do_hypre = v.value();
198
199 Alina::SolverResult solver_result;
200 if (do_hypre) {
201 _doHypreSolver(rows, ptr, col, val, rhs, x, argc, argv);
202 }
203 else {
204 solver_result = solve(prm, rows, ptr, col, val, rhs, x);
205
206 if (vm.count("output")) {
207 auto t = prof.scoped_tic("write");
208 Alina::IO::mm_write(vm["output"].as<string>(), &x[0], x.size());
209 }
210 }
211 std::cout << "Iterations: " << solver_result.nbIteration() << std::endl
212 << "Error: " << solver_result.residual() << std::endl
213 << prof << std::endl;
214 return 0;
215}
216
217/*---------------------------------------------------------------------------*/
218/*---------------------------------------------------------------------------*/
219
220int main(int argc, char* argv[])
221{
222 return Arcane::Alina::SampleMainContext::execMain(main2, argc, argv);
223}
224
225/*---------------------------------------------------------------------------*/
226/*---------------------------------------------------------------------------*/
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
Classe template pour convertir un type.
Interface du gestionnaire de traces.
virtual TraceMessage info()=0
Flot pour un message d'information.
Chaîne de caractères unicode.
ARCCORE_BASE_EXPORT String getEnvironmentVariable(const String &name)
Variable d'environnement du nom name.
-*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
Alina::detail::empty_params params