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