libMesh
Loading...
Searching...
No Matches
matrixsolve.C
Go to the documentation of this file.
1// The libMesh Finite Element Library.
2// Copyright (C) 2002-2026 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3
4// This library is free software; you can redistribute it and/or
5// modify it under the terms of the GNU Lesser General Public
6// License as published by the Free Software Foundation; either
7// version 2.1 of the License, or (at your option) any later version.
8
9// This library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12// Lesser General Public License for more details.
13
14// You should have received a copy of the GNU Lesser General Public
15// License along with this library; if not, write to the Free Software
16// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18// libmesh includes
19#include "libmesh/getpot.h"
20#include "libmesh/libmesh.h"
21#include "libmesh/libmesh_logging.h"
22#include "libmesh/linear_solver.h"
23#include "libmesh/numeric_vector.h"
24#include "libmesh/sparse_matrix.h"
25
26using namespace libMesh;
27
28void usage(const std::string & prog_name);
29
30// Open the matrix and the right-hand-side vector named in command
31// line arguments, solve them with a linear solver, and optionally
32// write the solution to the given output filename.
33int main(int argc, char ** argv)
34{
35 LibMeshInit init(argc, argv);
36
37 if (argc < 3)
38 usage(argv[0]);
39
40 // Create a GetPot object to parse the command line
41 GetPot command_line (argc, argv);
42
43 std::string matfile, rhsfile;
44 if (command_line.search(1, "-m"))
45 matfile = command_line.next(matfile);
46 else
47 usage(argv[0]);
48
49 if (command_line.search(1, "-r"))
50 rhsfile = command_line.next(rhsfile);
51 else
52 usage(argv[0]);
53
54 auto mat = SparseMatrix<Number>::build(init.comm());
55
56 LOG_CALL("mat.read()", "main", mat->read(matfile));
57
58 libMesh::out << "Loaded " << mat->m() << " by " << mat->n() <<
59 " matrix " << matfile << std::endl;
60
61 auto rhs = NumericVector<Number>::build(init.comm()),
62 solution = NumericVector<Number>::build(init.comm());
63
64 LOG_CALL("rhs.read()", "main", rhs->read_matlab(rhsfile));
65
66 libMesh::out << "Loaded length-" << rhs->size() <<
67 " vector " << rhsfile << std::endl;
68
69 solution->init(rhs->size(), rhs->local_size());
70
71 auto solver = LinearSolver<Number>::build(init.comm());
72
73 Real tol = 1e-9;
74 if (command_line.search(1, "-t"))
75 tol = command_line.next(tol);
76
77 unsigned int n_iters = 1e3;
78 if (command_line.search(1, "-n"))
79 n_iters = command_line.next(n_iters);
80
81 auto [iters, res] = solver->solve(*mat, *solution, *rhs, tol, n_iters);
82
83 libMesh::out << "Finished in " << iters << " iterations with " << res
84 << " residual " << std::endl;
85
86 return 0;
87}
88
89
90void usage(const std::string & prog_name)
91{
92 std::ostringstream helpList;
93 helpList <<
94 "Usage: " << prog_name <<
95 " [options]\n"
96 "\n"
97 "options:\n"
98 " -m <string> Matrix filename (required)\n"
99 " -r <string> RHS Vector filename (required)\n"
100 " -t <tol> Linear Solver tolerance (default 1e-9)\n"
101 " -n <n_iter> Linear Solver max iterations (default 1e3)\n";
102
103 libMesh::out << helpList.str();
104 exit(0);
105}
The LibMeshInit class, when constructed, initializes the dependent libraries (e.g.
Definition libmesh.h:92
static std::unique_ptr< LinearSolver< T > > build(const libMesh::Parallel::Communicator &comm_in, const SolverPackage solver_package=libMesh::default_solver_package())
Builds a LinearSolver using the linear solver package specified by solver_package.
static std::unique_ptr< NumericVector< T > > build(const Parallel::Communicator &comm, SolverPackage solver_package=libMesh::default_solver_package(), ParallelType parallel_type=AUTOMATIC)
Builds a NumericVector on the processors in communicator comm using the linear solver package specifi...
static std::unique_ptr< SparseMatrix< T > > build(const Parallel::Communicator &comm, const SolverPackage solver_package=libMesh::default_solver_package(), const MatrixBuildType matrix_build_type=MatrixBuildType::AUTOMATIC)
Builds a SparseMatrix<T> using the linear solver package specified by solver_package.
void usage(const std::string &prog_name)
Definition matrixsolve.C:90
The libMesh namespace provides an interface to certain functionality in the library.
OStreamProxy out
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
int main()