Skip to content

Instantly share code, notes, and snippets.

@ddemidov
Last active February 2, 2022 18:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ddemidov/79939b5db87957cdffb63f1deb0c75ee to your computer and use it in GitHub Desktop.
Save ddemidov/79939b5db87957cdffb63f1deb0c75ee to your computer and use it in GitHub Desktop.
#include <vector>
#include <iostream>
#include <amgcl/backend/builtin.hpp>
#include <amgcl/value_type/static_matrix.hpp>
#include <amgcl/adapter/crs_tuple.hpp>
#include <amgcl/adapter/block_matrix.hpp>
#include <amgcl/make_solver.hpp>
#include <amgcl/amg.hpp>
#include <amgcl/coarsening/smoothed_aggregation.hpp>
#include <amgcl/coarsening/as_scalar.hpp>
#include <amgcl/coarsening/rigid_body_modes.hpp>
#include <amgcl/relaxation/ilu0.hpp>
#include <amgcl/solver/cg.hpp>
#include <amgcl/io/mm.hpp>
#include <amgcl/profiler.hpp>
int main(int argc, char *argv[]) {
// The profiler:
amgcl::profiler<> prof("Nullspace");
// Read the system matrix, the RHS, and the coordinates:
ptrdiff_t rows, cols, ndim, ncoo;
std::vector<ptrdiff_t> ptr, col;
std::vector<double> val, rhs, coo;
prof.tic("read");
std::tie(rows, rows) = amgcl::io::mm_reader("A.mtx")(ptr, col, val);
std::tie(rows, cols) = amgcl::io::mm_reader("b.mtx")(rhs);
std::tie(ncoo, ndim) = amgcl::io::mm_reader("C.mtx")(coo);
prof.toc("read");
amgcl::precondition(ncoo * ndim == rows && (ndim == 2 || ndim == 3),
"The coordinate file has wrong dimensions");
std::cout << "Matrix: " << rows << "x" << rows << std::endl;
std::cout << "RHS: " << rows << "x" << cols << std::endl;
std::cout << "Coords: " << ncoo << "x" << ndim << std::endl;
// Declare the solver type
typedef amgcl::static_matrix<double, 3, 3> Block;
typedef amgcl::backend::builtin<Block> Backend; // the solver backend
typedef amgcl::make_solver<
amgcl::amg<
Backend,
amgcl::coarsening::as_scalar<
amgcl::coarsening::smoothed_aggregation
>::type,
amgcl::relaxation::ilu0
>,
amgcl::solver::cg<Backend>
> Solver;
// Solver parameters:
Solver::params prm;
prm.solver.maxiter = 1000;
prm.precond.coarsening.aggr.eps_strong = 0;
// Convert the coordinates to the rigid body modes.
// The function returns the number of near null-space vectors
// (3 in 2D case, 6 in 3D case) and writes the vectors to the
// std::vector<double> specified as the last argument:
prm.precond.coarsening.nullspace.cols = amgcl::coarsening::rigid_body_modes(
ndim, coo, prm.precond.coarsening.nullspace.B);
// We use the tuple of CRS arrays to represent the system matrix.
auto A = std::tie(rows, ptr, col, val);
auto Ab = amgcl::adapter::block_matrix<Block>(A);
// Initialize the solver with the system matrix.
prof.tic("setup");
Solver solve(Ab, prm);
prof.toc("setup");
// Show the mini-report on the constructed solver:
std::cout << solve << std::endl;
// Solve the system with the zero initial approximation:
int iters;
double error;
std::vector<double> x(rows, 0.0);
auto F = amgcl::backend::reinterpret_as_rhs<Block>(rhs);
auto X = amgcl::backend::reinterpret_as_rhs<Block>(x);
prof.tic("solve");
std::tie(iters, error) = solve(F, X);
prof.toc("solve");
// Output the number of iterations, the relative error,
// and the profiling data:
std::cout << "Iters: " << iters << std::endl
<< "Error: " << error << std::endl
<< prof << std::endl;
}
all: scalar block
%: %.cpp
g++ -O3 -DNDEBUG -fopenmp -I${AMGCL_ROOT} $^ -o $@
clean:
rm -vf scalar block
#include <vector>
#include <iostream>
#include <amgcl/backend/builtin.hpp>
#include <amgcl/adapter/crs_tuple.hpp>
#include <amgcl/adapter/block_matrix.hpp>
#include <amgcl/make_solver.hpp>
#include <amgcl/amg.hpp>
#include <amgcl/coarsening/smoothed_aggregation.hpp>
#include <amgcl/coarsening/as_scalar.hpp>
#include <amgcl/coarsening/rigid_body_modes.hpp>
#include <amgcl/relaxation/ilu0.hpp>
#include <amgcl/solver/cg.hpp>
#include <amgcl/io/mm.hpp>
#include <amgcl/profiler.hpp>
int main(int argc, char *argv[]) {
// The profiler:
amgcl::profiler<> prof("Nullspace");
// Read the system matrix, the RHS, and the coordinates:
ptrdiff_t rows, cols, ndim, ncoo;
std::vector<ptrdiff_t> ptr, col;
std::vector<double> val, rhs, coo;
prof.tic("read");
std::tie(rows, rows) = amgcl::io::mm_reader("A.mtx")(ptr, col, val);
std::tie(rows, cols) = amgcl::io::mm_reader("b.mtx")(rhs);
std::tie(ncoo, ndim) = amgcl::io::mm_reader("C.mtx")(coo);
prof.toc("read");
amgcl::precondition(ncoo * ndim == rows && (ndim == 2 || ndim == 3),
"The coordinate file has wrong dimensions");
std::cout << "Matrix: " << rows << "x" << rows << std::endl;
std::cout << "RHS: " << rows << "x" << cols << std::endl;
std::cout << "Coords: " << ncoo << "x" << ndim << std::endl;
// Declare the solver type
typedef amgcl::backend::builtin<double> Backend; // the solver backend
typedef amgcl::make_solver<
amgcl::amg<
Backend,
amgcl::coarsening::smoothed_aggregation,
amgcl::relaxation::ilu0
>,
amgcl::solver::cg<Backend>
> Solver;
// Solver parameters:
Solver::params prm;
prm.solver.maxiter = 1000;
prm.precond.coarsening.aggr.eps_strong = 0;
// Convert the coordinates to the rigid body modes.
// The function returns the number of near null-space vectors
// (3 in 2D case, 6 in 3D case) and writes the vectors to the
// std::vector<double> specified as the last argument:
prm.precond.coarsening.nullspace.cols = amgcl::coarsening::rigid_body_modes(
ndim, coo, prm.precond.coarsening.nullspace.B);
// We use the tuple of CRS arrays to represent the system matrix.
auto A = std::tie(rows, ptr, col, val);
// Initialize the solver with the system matrix.
prof.tic("setup");
Solver solve(A, prm);
prof.toc("setup");
// Show the mini-report on the constructed solver:
std::cout << solve << std::endl;
// Solve the system with the zero initial approximation:
int iters;
double error;
std::vector<double> x(rows, 0.0);
prof.tic("solve");
std::tie(iters, error) = solve(rhs, x);
prof.toc("solve");
// Output the number of iterations, the relative error,
// and the profiling data:
std::cout << "Iters: " << iters << std::endl
<< "Error: " << error << std::endl
<< prof << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment