Mechanics of Defect Evolution Library
Loading...
Searching...
No Matches
SpatialODESolver.h
Go to the documentation of this file.
1#ifndef model_SpatialODESolver_H_
2#define model_SpatialODESolver_H_
3
4#include<Eigen/SparseCore>
5#include<Eigen/SparseLU>
6#include<Eigen/IterativeLinearSolvers>
7#include<TrialBase.h>
8
9namespace model
10{
11 // template<typename TrialFunctionType>
13 {
14
15 typedef Eigen::SparseMatrix<double,Eigen::RowMajor> SparseMatrixType;
16
17 SparseMatrixType A;
18 const bool use_directSolver;
19 const double tolerance;
20 const size_t gSize; // Store gSize directly
21
22 double lastError;
23 int lastIterations;
24
25// #ifdef CHOLMOD_H // SuiteSparse Cholmod (LLT) module
26// typedef Eigen::CholmodSimplicialLDLT<SparseMatrixType> DirectSolverType; //If problem with speed try this
27// #else
28// typedef Eigen::SimplicialLLT<SparseMatrixType> DirectSolverType;
29// #endif
30 // typedef Eigen::ConjugateGradient<SparseMatrixType> IterativeSolverType;
31
32// #ifdef UMFPACK_H // SuiteSparse Umfpack (LU) module
33// typedef Eigen::UmfPackLU<SparseMatrixType> DirectSolverType;
34// #else
35// typedef Eigen::SparseLU<SparseMatrixType> DirectSolverType;
36// #endif
37// typedef Eigen::BiCGSTAB<SparseMatrixType> IterativeSolverType;
38
39 typedef Eigen::SimplicialLLT<SparseMatrixType> DirectSolverType;
40 typedef Eigen::ConjugateGradient<SparseMatrixType> IterativeSolverType;
41
42 DirectSolverType directSolver;
43 IterativeSolverType iterativeSolver;
44
45 public:
46
47 template<typename TrialFunctionType, typename IntegrationDomainType>
48 SpatialODESolver(const TrialFunctionType& trial, const IntegrationDomainType& dV, const bool& use_directSolver_in, const double& tol):
49 /* init */use_directSolver(use_directSolver_in)
50 /* init */,tolerance(tol)
51 /* init */,gSize(trial->gSize())
52 /* init */,lastError(0.0)
53 /* init */,lastIterations(0)
54 {
55 // const size_t gSize=trial->gSize();
56 const auto bwfAi((test(trial),trial)*dV);
57 std::vector<Eigen::Triplet<double> > globalTripletsAi(bwfAi->globalTriplets());
58 A.resize(gSize,gSize);
59 A.setFromTriplets(globalTripletsAi.begin(),globalTripletsAi.end());
60
61 if(use_directSolver)
62 {
63 directSolver.compute(A);
64 assert(directSolver.info()==Eigen::Success && "SOLVER FAILED");
65 }
66 else
67 {
68 iterativeSolver.compute(A);
69 assert(iterativeSolver.info()==Eigen::Success && "SOLVER FAILED");
70 }
71 }
72
73 // Eigen::VectorXd solve(const TrialFunctionType& trial, const Eigen::VectorXd& b)
74 Eigen::VectorXd solve(const Eigen::VectorXd& b)
75 {
78
79 if(use_directSolver)
80 {
81 assert(directSolver.info()==Eigen::Success && "SOLVER FAILED");
82 Eigen::VectorXd x = directSolver.solve(b);
83 lastError = 0.0;
84 lastIterations = 1;
85 return x;
86 }
87 else
88 {
89 iterativeSolver.setTolerance(tolerance);
90 // const size_t gSize=trial->gSize();
91 Eigen::VectorXd g(Eigen::VectorXd::Zero(gSize));
92 assert(iterativeSolver.info()==Eigen::Success && "SOLVER FAILED");
93 Eigen::VectorXd x = iterativeSolver.solveWithGuess(b,g);
94 lastError = iterativeSolver.error();
95 lastIterations = iterativeSolver.iterations();
96 return x;
97 }
98 }
99
100 double error() const
101 {
102 return lastError;
103 }
104
105 int iterations() const
106 {
107 return lastIterations;
108 }
109 };
110
111}
112#endif
SpatialODESolver(const TrialFunctionType &trial, const IntegrationDomainType &dV, const bool &use_directSolver_in, const double &tol)
Eigen::VectorXd solve(const Eigen::VectorXd &b)
b
Definition int_R.m:16
x
Definition int_R.m:12