OpenCAEPoro  v0.5.0
A simulator for multicomponent porous media flow
OCPTable.cpp
Go to the documentation of this file.
1 
12 #include "OCPTable.hpp"
13 
14 OCPTable::OCPTable(const USI& row, const USI& col)
15 {
16  nRow = row;
17  nCol = col;
18  bId = 0;
19  data.resize(nCol);
20  for (USI j = 0; j < nCol; j++) {
21  data[j].resize(nRow);
22  }
23 }
24 
25 OCPTable::OCPTable(const vector<vector<OCP_DBL>>& src) { this->Setup(src); }
26 
27 void OCPTable::Setup(const std::vector<std::vector<OCP_DBL>>& src)
28 {
29  data = src;
30  nCol = data.size();
31  nRow = data[0].size();
32  bId = nRow / 2;
33 }
34 
35 OCP_INT OCPTable::GetRowZero(const USI& mycol) const
36 {
37  if (mycol > nCol) OCP_ABORT("wrong specified column!");
38  for (USI i = 0; i < nRow; i++) {
39  if (data[mycol][i] >= TINY) {
40  return i - 1;
41  }
42  }
43  return nRow - 1;
44 }
45 
48  const OCP_DBL& val,
49  vector<OCP_DBL>& outdata,
50  vector<OCP_DBL>& slope)
51 {
52  if (val >= data[j][bId]) {
53  for (USI i = bId + 1; i < nRow; i++) {
54  if (val < data[j][i]) {
55  bId = i - 1;
56  for (USI k = 0; k < nCol; k++) {
57  slope[k] = (data[k][bId + 1] - data[k][bId]) /
58  (data[j][bId + 1] - data[j][bId]);
59  outdata[k] = data[k][bId] + slope[k] * (val - data[j][bId]);
60  }
61  return bId;
62  }
63  }
64  for (USI k = 0; k < nCol; k++) {
65  slope[k] = 0;
66  outdata[k] = data[k].back();
67  }
68  } else {
69  for (OCP_INT i = bId - 1; i >= 0; i--) {
70  if (val >= data[j][i]) {
71  bId = i;
72  for (USI k = 0; k < nCol; k++) {
73  slope[k] = (data[k][bId + 1] - data[k][bId]) /
74  (data[j][bId + 1] - data[j][bId]);
75  outdata[k] = data[k][bId] + slope[k] * (val - data[j][bId]);
76  }
77  return bId;
78  }
79  }
80  for (USI k = 0; k < nCol; k++) {
81  slope[k] = 0;
82  outdata[k] = data[k].front();
83  }
84  }
85  return bId;
86 }
87 
88 USI OCPTable::Eval_All0(const OCP_DBL& val, vector<OCP_DBL>& outdata)
89 {
90  const USI j = 0;
91  OCP_DBL tmpk = 0;
92  if (val >= data[j][bId]) {
93  for (USI i = bId + 1; i < nRow; i++) {
94  if (val < data[j][i]) {
95  bId = i - 1;
96  for (USI k = 1; k < nCol; k++) {
97  tmpk = (data[k][bId + 1] - data[k][bId]) /
98  (data[j][bId + 1] - data[j][bId]);
99  outdata[k - 1] = data[k][bId] + tmpk * (val - data[j][bId]);
100  }
101  return bId;
102  }
103  }
104  for (USI k = 1; k < nCol; k++) {
105  outdata[k - 1] = data[k].back();
106  }
107  } else {
108  for (OCP_INT i = bId - 1; i >= 0; i--) {
109  if (val >= data[j][i]) {
110  bId = i;
111  for (USI k = 1; k < nCol; k++) {
112  tmpk = (data[k][bId + 1] - data[k][bId]) /
113  (data[j][bId + 1] - data[j][bId]);
114  outdata[k - 1] = data[k][bId] + tmpk * (val - data[j][bId]);
115  }
116  return bId;
117  }
118  }
119  for (USI k = 1; k < nCol; k++) {
120  outdata[k - 1] = data[k].front();
121  }
122  }
123  return bId;
124 }
125 
126 OCP_DBL OCPTable::Eval(const USI& j, const OCP_DBL& val, const USI& destj)
127 {
128  if (val >= data[j][bId]) {
129  for (USI i = bId + 1; i < nRow; i++) {
130  if (val < data[j][i]) {
131  bId = i - 1;
132  OCP_DBL k = (data[destj][bId + 1] - data[destj][bId]) /
133  (data[j][bId + 1] - data[j][bId]);
134  return (data[destj][bId] + k * (val - data[j][bId]));
135  }
136  }
137  return data[destj].back();
138  } else {
139  for (OCP_INT i = bId - 1; i >= 0; i--) {
140  if (val >= data[j][i]) {
141  bId = i;
142  OCP_DBL k = (data[destj][bId + 1] - data[destj][bId]) /
143  (data[j][bId + 1] - data[j][bId]);
144  return (data[destj][bId] + k * (val - data[j][bId]));
145  }
146  }
147  return data[destj].front();
148  }
149 }
150 
151 OCP_DBL OCPTable::Eval(const USI& j, const OCP_DBL& val, const USI& destj, OCP_DBL& myK)
152 {
153  // becareful when the memory outdata and slope have not be allocated before
154 
155  if (val >= data[j][bId]) {
156  for (USI i = bId + 1; i < nRow; i++) {
157  if (val < data[j][i]) {
158  bId = i - 1;
159  myK = (data[destj][bId + 1] - data[destj][bId]) /
160  (data[j][bId + 1] - data[j][bId]);
161  return (data[destj][bId] + myK * (val - data[j][bId]));
162  }
163  }
164  return data[destj].back();
165  } else {
166  for (OCP_INT i = bId - 1; i >= 0; i--) {
167  if (val >= data[j][i]) {
168  bId = i;
169  myK = (data[destj][bId + 1] - data[destj][bId]) /
170  (data[j][bId + 1] - data[j][bId]);
171  return (data[destj][bId] + myK * (val - data[j][bId]));
172  }
173  }
174  return data[destj].front();
175  }
176 }
177 
178 OCP_DBL OCPTable::Eval_Inv(const USI& j, const OCP_DBL& val, const USI& destj)
179 {
180  // becareful when the memory outdata and slope have not be allocated before
181 
182  if (val > data[j][bId]) {
183  for (OCP_INT i = bId - 1; i >= 0; i--) {
184  if (val <= data[j][i]) {
185  bId = i;
186  OCP_DBL k = (data[destj][bId + 1] - data[destj][bId]) /
187  (data[j][bId + 1] - data[j][bId]);
188  return (data[destj][bId] + k * (val - data[j][bId]));
189  }
190  }
191  return data[destj].front();
192  } else {
193  for (USI i = bId + 1; i < nRow; i++) {
194  if (val >= data[j][i]) {
195  bId = i;
196  OCP_DBL k = (data[destj][bId] - data[destj][bId - 1]) /
197  (data[j][bId] - data[j][bId - 1]);
198  return (data[destj][bId - 1] + k * (val - data[j][bId - 1]));
199  }
200  }
201  return data[destj].back();
202  }
203 }
204 
205 void OCPTable::Display() const
206 {
207  cout << "\n---------------------" << endl
208  << "Pressure Distribution"
209  << "\n---------------------" << endl;
210  cout << " Depth "
211  << " Poil "
212  << " Pgas "
213  << " Pwat " << endl;
214 
215  cout << fixed << setprecision(3);
216  for (USI i = 0; i < nRow; i++) {
217  for (USI j = 0; j < nCol; j++) {
218  cout << data[j][i] << "\t";
219  }
220  cout << "\n";
221  }
222 }
223 
224 /*----------------------------------------------------------------------------*/
225 /* Brief Change History of This File */
226 /*----------------------------------------------------------------------------*/
227 /* Author Date Actions */
228 /*----------------------------------------------------------------------------*/
229 /* Shizhe Li Oct/01/2021 Create file */
230 /* Chensong Zhang Oct/15/2021 Format file */
231 /*----------------------------------------------------------------------------*/
const OCP_DBL TINY
Small constant.
Definition: OCPConst.hpp:38
unsigned int USI
Generic unsigned integer.
Definition: OCPConst.hpp:23
double OCP_DBL
Double precision.
Definition: OCPConst.hpp:27
int OCP_INT
Long integer.
Definition: OCPConst.hpp:26
OCPTable class declaration.
#define OCP_ABORT(msg)
Abort if critical error happens.
Definition: UtilError.hpp:47
USI Eval_All(const USI &j, const OCP_DBL &val, vector< OCP_DBL > &outdata, vector< OCP_DBL > &slope)
Careful: the memory outdata and slope have not be allocated before.
Definition: OCPTable.cpp:47
OCP_DBL Eval_Inv(const USI &j, const OCP_DBL &val, const USI &destj)
Definition: OCPTable.cpp:178
void Display() const
Display the data of table on screen.
Definition: OCPTable.cpp:205
void Setup(const vector< vector< OCP_DBL >> &src)
Setup tables from existing data of table.
Definition: OCPTable.cpp:27
OCP_INT GetRowZero(const USI &mycol) const
Definition: OCPTable.cpp:35
USI Eval_All0(const OCP_DBL &val, vector< OCP_DBL > &outdata)
Definition: OCPTable.cpp:88
OCPTable()=default
Default constructor.
OCP_DBL Eval(const USI &j, const OCP_DBL &val, const USI &destj)
Definition: OCPTable.cpp:126