OpenCAEPoro  0.2.0 Sep/22/2022
A simulator for multicomponent porous media flow
LinearSystem.cpp
Go to the documentation of this file.
1 
11 #include "LinearSystem.hpp"
12 
13 void LinearSystem::AllocateRowMem(const OCP_USI& dimMax, const USI& nb)
14 {
15  blockSize = nb * nb;
16  blockDim = nb;
17  maxDim = dimMax;
18  rowCapacity.resize(maxDim);
19  colId.resize(maxDim);
20  val.resize(maxDim);
21  diagPtr.resize(maxDim);
22  diagVal.resize(maxDim * blockSize);
23  b.resize(maxDim * blockDim);
24  u.resize(maxDim * blockDim);
25 }
26 
28 {
29  for (OCP_USI n = 0; n < maxDim; n++) {
30  colId[n].reserve(rowCapacity[n]);
31  val[n].reserve(rowCapacity[n] * blockSize);
32  }
33 }
34 
36 {
37  for (OCP_USI n = 0; n < maxDim; n++) {
38  rowCapacity[n] = colnum;
39  colId[n].reserve(colnum);
40  val[n].reserve(colnum * blockSize);
41  }
42 }
43 
45 {
46  for (OCP_USI i = 0; i < maxDim; i++) {
47  colId[i].clear(); // actually, only parts of bulks needs to be clear
48  val[i].clear();
49  }
50  // diagPtr.assign(maxDim, 0);
51  fill(diagVal.begin(), diagVal.end(), 0.0);
52  fill(b.begin(), b.end(), 0.0);
53  // In fact, for linear system the current solution is a good initial solution for
54  // next step, so u will not be set to zero. u.assign(maxDim, 0);
55 }
56 
57 void LinearSystem::AssembleRhs(const vector<OCP_DBL>& rhs)
58 {
59  OCP_USI nrow = dim * blockDim;
60  for (OCP_USI i = 0; i < nrow; i++) {
61  b[i] += rhs[i];
62  }
63 }
64 
65 void LinearSystem::OutputLinearSystem(const string& fileA, const string& fileb) const
66 {
67  string FileA = solveDir + fileA;
68  string Fileb = solveDir + fileb;
69 
70  // out A
71  // csr or bsr
72  ofstream outA(FileA);
73  if (!outA.is_open()) cout << "Can not open " << FileA << endl;
74  outA << dim << endl;
75  if (blockDim != 1) {
76  outA << blockDim << endl;
77  }
78  // IA
79  OCP_USI rowId = 1;
80  for (OCP_USI i = 0; i < dim; i++) {
81  outA << rowId << endl;
82  rowId += colId[i].size();
83  }
84  outA << rowId << endl;
85  // JA
86  USI rowSize = 0;
87  for (OCP_USI i = 0; i < dim; i++) {
88  rowSize = colId[i].size();
89  for (USI j = 0; j < rowSize; j++) {
90  outA << colId[i][j] + 1 << endl;
91  }
92  }
93  // val
94  for (OCP_USI i = 0; i < dim; i++) {
95  rowSize = val[i].size();
96  for (USI j = 0; j < rowSize; j++) {
97  outA << val[i][j] << endl;
98  }
99  }
100  outA.close();
101 
102  // out b
103  OCP_USI nRow = dim * blockDim;
104  ofstream outb(Fileb);
105  if (!outb.is_open()) cout << "Can not open " << Fileb << endl;
106  outb << dim << endl;
107  for (OCP_USI i = 0; i < nRow; i++) {
108  outb << b[i] << endl;
109  }
110 }
111 
112 void LinearSystem::OutputSolution(const string& fileU) const
113 {
114  string FileU = solveDir + fileU;
115  ofstream outu(FileU);
116  if (!outu.is_open()) cout << "Can not open " << FileU << endl;
117  outu << dim << endl;
118  OCP_USI nrow = dim * blockDim;
119  for (OCP_USI i = 0; i < nrow; i++) outu << u[i] << endl;
120  outu.close();
121 }
122 
124 {
125  // check A
126  for (OCP_USI n = 0; n < dim; n++) {
127  for (auto v : val[n]) {
128  if (!isfinite(v)) {
129  OCP_ABORT("NAN or INF in MAT");
130  }
131  }
132  }
133  // check b
134  OCP_USI len = dim * blockDim;
135  for (OCP_USI n = 0; n < len; n++) {
136  if (!isfinite(b[n])) {
137  OCP_ABORT("NAN or INF in rhs");
138  }
139  }
140 }
141 
143 {
144  OCP_USI len = dim * blockDim;
145  for (OCP_USI n = 0; n < len; n++) {
146  if (!isfinite(u[n])) {
147  OCP_ABORT("NAN or INF in u");
148  }
149  }
150 }
151 
153 void LinearSystem::SetupLinearSolver(const USI& i, const string& dir,
154  const string& file)
155 {
156  solveDir = dir;
157  switch (i) {
158  case SCALARFASP:
159  // Fasp
160  LS = new ScalarFaspSolver;
161  break;
162  case VECTORFASP:
163  // Blcok Fasp
164  LS = new VectorFaspSolver;
165  break;
166  default:
167  OCP_ABORT("Wrong Linear Solver type!");
168  break;
169  }
170  LS->SetupParam(dir, file);
171  LS->Allocate(rowCapacity, maxDim, blockDim);
172 }
173 
174 /*----------------------------------------------------------------------------*/
175 /* Brief Change History of This File */
176 /*----------------------------------------------------------------------------*/
177 /* Author Date Actions */
178 /*----------------------------------------------------------------------------*/
179 /* Shizhe Li Oct/01/2021 Create file */
180 /* Shizhe Li Nov/22/2021 renamed to LinearSystem */
181 /*----------------------------------------------------------------------------*/
Linear solver class declaration.
const USI VECTORFASP
Use vector linear solver in Fasp.
Definition: OCPConst.hpp:79
unsigned int USI
Generic unsigned integer.
Definition: OCPConst.hpp:22
unsigned int OCP_USI
Long unsigned integer.
Definition: OCPConst.hpp:24
const USI SCALARFASP
Use scalar linear solver in Fasp.
Definition: OCPConst.hpp:78
#define OCP_ABORT(msg)
Abort if critical error happens.
Definition: UtilError.hpp:47
virtual void Allocate(const vector< USI > &rowCapacity, const OCP_USI &maxDim, const USI &blockDim)=0
Allocate maximum memory for linear solvers.
virtual void SetupParam(const string &dir, const string &file)=0
Read the params for linear solvers from an input file.
void OutputLinearSystem(const string &fileA, const string &fileb) const
Output the mat and rhs to fileA and fileb. // TODO: output to some obj?
void AllocateColMem()
Allocate memory for each matrix row with max possible number of columns.
void AllocateRowMem(const OCP_USI &dimMax, const USI &nb)
Allocate memory for linear system with max possible number of rows.
void ClearData()
Clear the internal matrix data for scalar-value problems.
void AssembleRhs(const vector< OCP_DBL > &rhs)
Assign Rhs — used for FIM now.
void CheckEquation() const
Check whether NAN or INF occurs in equations, used in debug mode.
void SetupLinearSolver(const USI &i, const string &dir, const string &file)
Setup LinearSolver.
void OutputSolution(const string &filename) const
Output the solution to a disk file name.
void CheckSolution() const
Check whether NAN or INF occurs in solutions, used in debug mode.
Scalar solvers in CSR format from FASP.
Definition: FaspSolver.hpp:85
Vector solvers in BSR format from FASP.
Definition: FaspSolver.hpp:116