-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtables.cpp
More file actions
316 lines (267 loc) · 9.43 KB
/
Copy pathtables.cpp
File metadata and controls
316 lines (267 loc) · 9.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#ifndef TABLES_CPP_INCLUDED
#define TABLES_CPP_INCLUDED
/*
This is my bilinear class, BUT without any for loop except when you construct them.
It is very important to have elements of x and y sorted and equispaced beforehand.
Otherwise, this class is only useful to collect and print data in tables
*/
using namespace std;
class Table
{
private:
r_number *x; //x points
int Nx; //Number of x points
r_number *y; //y points
int Ny; //Number of y points
r_number **z; //z(x,y)
r_number bilinear_interpolation(r_number X, r_number Y, int i1, int i2, int j1, int j2){
//Create the needed values:
r_number x1 = x[i1];
r_number x2 = x[i2];
r_number y1 = y[j1];
r_number y2 = y[j2];
r_number z11 = z[i1][j1];
r_number z12 = z[i1][j2];
r_number z21 = z[i2][j1];
r_number z22 = z[i2][j2];
//Vile copy of wikipedia!
r_number den = (y2-y1)*(x2-x1);
r_number num = (y2-Y)*(x2-X)*z11;
num += (y2-Y)*(X-x1)*z21;
num += (Y-y1)*(x2-X)*z12;
num += (Y-y1)*(X-x1)*z22;
//cout<<num/den<<endl;
return num/den;
}
r_number bilog_interpolation(r_number X, r_number Y, int i1, int i2, int j1, int j2){
//Create the needed values:
r_number x1 = (x[i1]);
r_number x2 = (x[i2]);
r_number y1 = (y[j1]);
r_number y2 = (y[j2]);
r_number z11 = std::log10(z[i1][j1]);
r_number z12 = std::log10(z[i1][j2]);
r_number z21 = std::log10(z[i2][j1]);
r_number z22 = std::log10(z[i2][j2]);
//Vile copy of wikipedia!
r_number den = (y2-y1)*(x2-x1);
r_number num = (y2-Y)*(x2-X)*z11;
num += (y2-Y)*(X-x1)*z21;
num += (Y-y1)*(x2-X)*z12;
num += (Y-y1)*(X-x1)*z22;
return std::pow(10.,num/den);
}
void find_indexes(float k, int& k1, int& k2, bool& same)
{
k1 = floor(abs(k)); //Absolute value is to count mirror boundary conditions
k2 = ceil(abs(k));
if( k1 == k2 ){same = true;}
}
public:
Table()
{
x = nullptr;
y = nullptr;
z = nullptr;
}
//Legacy constructor
Table(r_number* X, r_number* Y, r_number** Z, int n_x, int n_y) //Remember, sorted arrays!
{
Nx = n_x;
Ny = n_y;
x = new r_number[Nx];
y = new r_number[Ny];
z = new r_number*[Nx];
for(int i=0;i<Nx;++i)
{
x[i] = X[i];
z[i] = new r_number[Ny];
}
for(int i=0;i<Nx;++i)
{
for(int j=0;j<Ny;++j)
{
if(i==0){ y[j] = Y[j]; }
z[i][j] = Z[i][j];
}
}
}
//This constructor, combined with 'fill' methods, avoids you to do two loops (you don't need to have *x,*y and **z first)
Table(int n_x, int n_y)
{
Nx = n_x;
Ny = n_y;
x = new r_number[Nx];
y = new r_number[Ny];
z = new r_number*[Nx];
for(int i=0;i<Nx;++i){ z[i] = new r_number[Ny]; }
}
//Rule of 3
~Table(){
if( x != nullptr){
delete[] x;
x = nullptr;
}
if( y != nullptr){
delete[] y;
y = nullptr;
}
if( z != nullptr){
for(int i=0; i<Nx;i++){
delete[] z[i];
}
delete[] z;
z = nullptr;
}
}
Table(const Table& other){
//C++ rule of 3: If you have pointer members to dynamic memory, you need a destructor, a copy constructor, and assigment operator.
//This is to avoid shallow copy (copy of pointer members points to the same address as the original) and awful deletes
//Copy x
x = new r_number[other.Nx];
Nx = other.Nx;
for(int i=0;i<Nx;i++){
x[i] = other.x[i];
}
//Copy y
y = new r_number[other.Ny];
Ny = other.Ny;
for(int j=0;j<Ny;j++){
y[j] = other.y[j];
}
//Copy z
z = new r_number*[Nx];
for(int i=0;i<Nx;i++){
z[i] = new r_number[Ny];
}
for(int i=0;i<Nx;i++){
for(int j=0;j<Ny;j++){
z[i][j] = other.z[i][j];
}
}
}
Table& operator=(const Table& other){
//This represent the operation *this = other. WE ARE OVERRIDING *this, with potential memory leaks if we are not careful!
//Override *this first (but not assign to null)
if( x != nullptr){delete[] x;}
if( y != nullptr){delete[] y;}
if( z != nullptr){
for(int i=0;i<Nx;i++){ delete[] z[i]; }
delete[] z;
}
Nx = other.Nx;
x = new r_number[Nx];
for(int i=0;i<Nx;i++){
x[i] = other.x[i];
}
Ny = other.Ny;
y = new r_number[Ny];
for(int j=0;j<Ny;j++){
y[j] = other.y[j];
}
z = new r_number*[Nx];
for(int i=0;i<Nx;i++){
z[i] = new r_number[Ny];
}
for(int i=0;i<Nx;i++){
for(int j=0;j<Ny;j++){
z[i][j] = other.z[i][j];
}
}
return *this;
}
//FILL METHODS
void FillX(r_number X, int index)
{
x[index] = X;
}
void FillY(r_number Y, int index)
{
y[index] = Y;
}
void FillZ(r_number Z, int indexX, int indexY)
{
z[indexX][indexY] = Z;
}
void FillAll(r_number X,r_number Y, r_number Z, int indexX, int indexY)
{
FillX(X,indexX);
FillY(Y,indexY);
FillZ(Z,indexX,indexY);
}
void SumZ(r_number Z, int indexX, int indexY)
{
z[indexX][indexY] += Z;
}
//ACCESS METHODS
r_number X(int index){ return x[index]; }
r_number Y(int index){ return y[index]; }
r_number Z(int indexX, int indexY){ return z[indexX][indexY]; }
int Xdim(){return Nx;}
int Ydim(){return Ny;}
//GET VALUES
r_number get_value(r_number X, r_number Y)
{
//Find the indexes with divisions
float i = (Nx-1.0)*(X-x[0])/(x[Nx-1]-x[0]);
float j = (Ny-1.0)*(Y-y[0])/(y[Ny-1]-y[0]);
//cout<<X<<" "<<Y<<endl;
if( abs(i) > Nx-1 || abs(j) > Ny-1 ) //If you are out of bounds (X > x[Nx-1], idem with Y), give 0.0. If you're negative, see find_indexes for the other boundary condition
{
//cout<<"Out of bounds "<<Nx-1<<" "<<Ny-1<<endl;
return 0.0;
}
else
{
bool same = false;
int i1,i2;
find_indexes(i, i1, i2, same);
if(same){ i2 = (i1 == Nx-1 ) ? i2-1 : i2+1; } //In paper, this does not matter. In code, it means segfault
same = false;
int j1,j2;
find_indexes(j,j1,j2,same);
if(same){ j2 = (j1 == Ny-1 ) ? j2-1 : j2+1; }
//cout<<i1<<" "<<i2<<" , "<<j1<<" "<<j2<<endl;
//Get the interpolation
return bilinear_interpolation(X,Y,i1,i2,j1,j2);
}
}
/*r_number get_log(r_number X, r_number Y)
{
//Find the indexes with divisions
float i = Nx*(X-x[0])/(x[Nx-1]-x[0]);
float j = Ny*(Y-y[0])/(y[Ny-1]-y[0]);
if( abs(i) > Nx-1 && abs(j) > Ny-1 ) //If you are out of bounds (X > x[Nx-1], idem with Y), give 0.0. If you're negative, see find_indexes for the other boundary condition
{
return 0.0;
}
else
{
int i1,i2;
find_indexes(i, i1, i2);
int j1,j2;
find_indexes(j,j1,j2);
//Get the interpolation
return bilog_interpolation(X,Y,i1,i2,j1,j2);
}
}*/
//PRINT OUTPUTS
//This function is used for testing purposes
void write_table(string namefile, string firstline = "")
{
cout<<"Created file "<<namefile<<endl;
ofstream file;
file.open(namefile.c_str());
file<<firstline<<endl;
for(int i=0;i<Nx;++i)
{
for(int j=0;j<Ny;++j)
{
file<<x[i]<<" "<<y[j]<<" "<<z[i][j]<<endl;
}
file<<endl; //This is to indicate a change in x. Helps gnuplot.
}
file.close();
}
};
#endif