Arcane  4.2.3.0
Developer documentation
Loading...
Searching...
No Matches
SolverComplex.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/range/iterator_range.hpp>
23#include <boost/preprocessor/seq/for_each.hpp>
24
25#include "arccore/alina/BuiltinBackend.h"
26#include "arccore/alina/ValueTypeComplex.h"
27#include "arccore/alina/StaticMatrix.h"
28#include "arccore/alina/Adapters.h"
29
30#include "arccore/alina/SolverRuntime.h"
31#include "arccore/alina/CoarseningRuntime.h"
32#include "arccore/alina/RelaxationRuntime.h"
33#include "arccore/alina/PreconditionerRuntime.h"
34#include "arccore/alina/PreconditionedSolver.h"
35#include "arccore/alina/AMG.h"
36#include "arccore/alina/IO.h"
37
38#include "arccore/alina/Profiler.h"
39
40#include "arccore/common/internal/ProgramOptions.h"
41
42#include "SampleProblemCommon.h"
43
44#ifndef ARCCORE_ALINA_BLOCK_SIZES
45#define ARCCORE_ALINA_BLOCK_SIZES (2)(3)(4)
46#endif
47using namespace Arcane;
48using namespace Arcane::Alina;
49
50using Alina::precondition;
51
52//---------------------------------------------------------------------------
53template <class Precond, class Matrix>
55solve(const AlinaContext& context,
56 const Matrix& A,
57 const Alina::PropertyTree& prm,
58 std::vector<std::complex<double>> const& f,
59 std::vector<std::complex<double>>& x)
60{
61 auto& prof = Alina::Profiler::globalProfiler();
62
63 typedef typename Precond::backend_type Backend;
64
65 typedef typename Alina::math::rhs_of<typename Backend::value_type>::type rhs_type;
66 size_t n = Alina::backend::nbRow(A);
67
68 rhs_type const* fptr = reinterpret_cast<rhs_type const*>(&f[0]);
69 rhs_type* xptr = reinterpret_cast<rhs_type*>(&x[0]);
70 SmallSpan<const rhs_type> frng(fptr, n);
71 SmallSpan<rhs_type> xrng(xptr, n);
72
74
75 prof.tic("setup");
76 Solver solve(context, A, prm);
77 prof.toc("setup");
78
79 std::cout << solve << std::endl;
80
81 {
82 auto t = prof.scoped_tic("solve");
83 return solve(frng, xrng);
84 }
85}
86
87//---------------------------------------------------------------------------
88int main(int argc, char* argv[])
89{
90 auto& prof = Alina::Profiler::globalProfiler();
91 namespace po = Arcane::ProgramOptions;
92 namespace io = Alina::IO;
93 Alina::AlinaContext context;
94 using std::string;
95 using std::vector;
96
97 po::options_description desc("Options");
98
99 desc.add_options()("help,h", "Show this help.");
100 desc.add_options()("prm-file,P",
101 po::value<string>(),
102 "Parameter file in json format. ");
103 desc.add_options()("prm,p",
104 po::value<vector<string>>()->multitoken(),
105 "Parameters specified as name=value pairs. "
106 "May be provided multiple times. Examples:\n"
107 " -p solver.tol=1e-3\n"
108 " -p precond.coarse_enough=300");
109 desc.add_options()("matrix,A",
110 po::value<string>(),
111 "System matrix in the MatrixMarket format. "
112 "When not specified, solves a Poisson problem in 3D unit cube. ");
113 desc.add_options()("rhs,f",
114 po::value<string>(),
115 "The RHS vector in the MatrixMarket format. "
116 "When omitted, a vector of ones is used by default. "
117 "Should only be provided together with a system matrix. ");
118 desc.add_options()("null,N",
119 po::value<string>(),
120 "The near null-space vectors in the MatrixMarket format. "
121 "Should be a dense matrix of size N*M, where N is the number of "
122 "unknowns, and M is the number of null-space vectors. "
123 "Should only be provided together with a system matrix. ");
124 desc.add_options()("binary,B",
125 po::bool_switch()->default_value(false),
126 "When specified, treat input files as binary instead of as MatrixMarket. "
127 "It is assumed the files were converted to binary format with mm2bin utility. ");
128 desc.add_options()("block-size,b",
129 po::value<int>()->default_value(1),
130 "The block size of the system matrix. "
131 "When specified, the system matrix is assumed to have block-wise structure. "
132 "This usually is the case for problems in elasticity, structural mechanics, "
133 "for coupled systems of PDE (such as Navier-Stokes equations), etc. ");
134 desc.add_options()("size,n",
135 po::value<int>()->default_value(32),
136 "The size of the Poisson problem to solve when no system matrix is given. "
137 "Specified as number of grid nodes along each dimension of a unit cube. "
138 "The resulting system will have n*n*n unknowns. ");
139 desc.add_options()("single-level,1",
140 po::bool_switch()->default_value(false),
141 "When specified, the AMG hierarchy is not constructed. "
142 "Instead, the problem is solved using a single-level smoother as preconditioner. ");
143 desc.add_options()("initial,x",
144 po::value<double>()->default_value(0),
145 "Value to use as initial approximation. ");
146 desc.add_options()("output,o",
147 po::value<string>(),
148 "Output file. Will be saved in the MatrixMarket format. "
149 "When omitted, the solution is not saved. ");
150
152 po::store(po::parse_command_line(argc, argv, desc), vm);
153 po::notify(vm);
154
155 if (vm.count("help")) {
156 std::cout << desc << std::endl;
157 return 0;
158 }
159
161 if (vm.count("prm-file")) {
162 prm.read_json(vm["prm-file"].as<string>());
163 }
164
165 if (vm.count("prm")) {
166 for (const string& v : vm["prm"].as<vector<string>>()) {
167 prm.putKeyValue(v);
168 }
169 }
170
171 size_t rows;
172 vector<ptrdiff_t> ptr, col;
173 vector<std::complex<double>> val, rhs, null, x;
174
175 if (vm.count("matrix")) {
176 auto t = prof.scoped_tic("reading");
177
178 string Afile = vm["matrix"].as<string>();
179 bool binary = vm["binary"].as<bool>();
180
181 if (binary) {
182 io::read_crs(Afile, rows, ptr, col, val);
183 }
184 else {
185 size_t cols;
186 std::tie(rows, cols) = io::mm_reader(Afile)(ptr, col, val);
187 precondition(rows == cols, "Non-square system matrix");
188 }
189
190 if (vm.count("rhs")) {
191 string bfile = vm["rhs"].as<string>();
192
193 size_t n, m;
194 if (binary) {
195 io::read_dense(bfile, n, m, rhs);
196 }
197 else {
198 std::tie(n, m) = io::mm_reader(bfile)(rhs);
199 }
200
201 precondition(n == rows && m == 1, "The RHS vector has wrong size");
202 }
203 else {
204 rhs.resize(rows, 1.0);
205 }
206
207 if (vm.count("null")) {
208 string nfile = vm["null"].as<string>();
209
210 size_t m, nv;
211
212 if (binary) {
213 io::read_dense(nfile, m, nv, null);
214 }
215 else {
216 std::tie(m, nv) = io::mm_reader(nfile)(null);
217 }
218
219 precondition(m == rows, "Near null-space vectors have wrong size");
220
221 prm.put("precond.coarsening.nullspace.cols", nv);
222 prm.put("precond.coarsening.nullspace.rows", rows);
223 prm.put("precond.coarsening.nullspace.B", &null[0]);
224 }
225 }
226 else {
227 auto t = prof.scoped_tic("assembling");
228 rows = Alina::PoissonProblemGenerator::createSequentialMatrix(vm["size"].as<int>(), val, col, ptr, rhs);
229 }
230
231 x.resize(rows, vm["initial"].as<double>());
232
233 if (vm["single-level"].as<bool>())
234 prm.put("precond.class", "relaxation");
235
236 int block_size = vm["block-size"].as<int>();
238#define CALL_BLOCK_SOLVER(z, data, B) \
239 case B: { \
240 typedef StaticMatrix<std::complex<double>, B, B> value_type; \
241 typedef ::Arcane::Alina::BuiltinBackend<value_type> Backend; \
242 r = solve<::Arcane::Alina::PreconditionerRuntime<Backend>>(context, \
243 ::Arcane::Alina::adapter::block_matrix<value_type>( \
244 std::tie(rows, ptr, col, val)), \
245 prm, rhs, x); \
246 } break;
247
248 switch (block_size) {
249 case 1: {
251 r = solve<PreconditionerRuntime<Backend>>(context, std::tie(rows, ptr, col, val), prm, rhs, x);
252 } break;
253 BOOST_PP_SEQ_FOR_EACH(CALL_BLOCK_SOLVER, ~, ARCCORE_ALINA_BLOCK_SIZES)
254 }
255
256#undef CALL_BLOCK_SOLVER
257
258 if (vm.count("output")) {
259 auto t = prof.scoped_tic("write");
260 Alina::IO::mm_write(vm["output"].as<string>(), &x[0], x.size());
261 }
262
263 std::cout << "Iterations: " << r.nbIteration() << std::endl
264 << "Error: " << r.residual() << std::endl
265 << prof << std::endl;
266}
Convenience class that bundles together a preconditioner and an iterative solver.
Class to store parameters as a hierarchical key/value tree.
Definition AlinaUtils.h:113
Result of a solution.
Definition AlinaUtils.h:56
Matrix class, to be used by user.
Describes a set of command-line options.
Stores parsed option values.
View of an array of elements of type T.
Definition Span.h:803
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --