-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix.h
239 lines (221 loc) · 5.12 KB
/
Matrix.h
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
#ifndef _MATRIX3D_H
#define _MATRIX3D_H
#include <vector>
#include <iostream>
#include <math.h>
using namespace std;
template<typename T>
class Matrix3d: public vector<T>
{
public:
//constructors
Matrix3d(size_t xsize, size_t ysize, size_t zsize) :
vector<T>(xsize*ysize*zsize),
_xsize(xsize), _ysize(ysize), _zsize(zsize)
{};
Matrix3d():
vector<T>(0),
_xsize(0), _ysize(0), _zsize(0)
{};
//() operator overloads
T operator()(size_t i, size_t j, size_t k) const
{
return vector<T>::operator[](i*_ysize*_zsize + j*_zsize + k);
}
T& operator()(size_t i, size_t j, size_t k)
{
return vector<double>::operator[](i*_ysize*_zsize + j*_zsize + k);
}
void set_gaussian_interior(double mean, double var, double gridsize,
int xstart_ix, int ystart_ix, int zstart_ix)
{
double sigma_x = var;
double sigma_y = var;
double sigma_z = var;
double mu_x = mean;
double mu_y = mean;
double mu_z = mean;
int xmax = _xsize;
int ymax = _ysize;
int zmax = _zsize;
double pi = 3.14159;
for (int i=0; i<xmax; ++i)
{
double _x = (i+xstart_ix)*gridsize;
double _x_term = (_x - mu_x)*(_x - mu_x) * sigma_x;
for(int j=0; j<ymax; ++j)
{
double _y = (j+ystart_ix)*gridsize;
double _y_term = (_y - mu_y)*(_y - mu_y) * sigma_y;
for(int k=0; k<zmax; ++k)
{
double _z = (k+zstart_ix)*gridsize;
double _z_term = (_z - mu_z)*(_z - mu_z) * sigma_z;
(*this)(i,j,k) = ( 1/(pow(2*pi*sigma_x, 1.5))
* exp(-.5*(_x_term+_y_term+_z_term)) );
}
}
}
}
void set_test_interior(double mean, double var, double gridsize,
int xstart_ix, int ystart_ix, int zstart_ix)
{
double sigma_x = var;
double sigma_y = var;
double sigma_z = var;
double mu_x = mean;
double mu_y = mean;
double mu_z = mean;
int xmax = _xsize;
int ymax = _ysize;
int zmax = _zsize;
double pi = 3.14159;
cout<<"xmax: "<<xmax;
for (int i=0; i<xmax; i++)
{
double _x = (i+xstart_ix)*gridsize;
double _x_term = (_x - mu_x)*(_x - mu_x) * sigma_x;
for(int j=0; j<ymax; j++)
{
double _y = (j+ystart_ix)*gridsize;
double _y_term = (_y - mu_y)*(_y - mu_y) * sigma_y;
for(int k=0; k<zmax; k++)
{
double _z = (k+zstart_ix)*gridsize;
double _z_term = (_z - mu_z)*(_z - mu_z) * sigma_z;
(*this)(i,j,k) = (i)*100+(j)*10+(k);
}
}
}
}
void reset_boundaries(double boundary_value=0)
{
int xmax = _xsize;
int ymax = _ysize;
int zmax = _zsize;
//set xup and xdn borders
for(int j=0; j<ymax; ++j)
{
for(int k=0; k<zmax; ++k)
{
(*this)(0,j,k) = boundary_value;
(*this)(xmax-1, j, k) = boundary_value;
}
}
//set yup and ydn borders
for(int i=0; i<xmax; ++i)
{
for(int k=0; k<zmax; ++k)
{
(*this)(i,0,k) = boundary_value;
(*this)(i, ymax-1, k) = boundary_value;
}
}
//set zup and zdn borders
for(int i=0; i<xmax; ++i)
{
for(int j=0; j<ymax; ++j)
{
(*this)(i,j,0) = boundary_value;
(*this)(i,j, zmax-1) = boundary_value;
}
}
}
void init_mean_var()
{
double sum=0; double varsum=0;
double mean, var;
double t;
int n = 0;
for (int x=1; x<_xsize-1; x++)
{
for (int y=1; y<_ysize-1; y++)
{
for (int z=1; z<_zsize-1; z++)
{
sum += (*this)(x,y,z);
n++;
}
}
}
mean = sum / (double) n;
for (int x=1; x<_xsize-1; x++)
{
for (int y=1; y<_ysize-1; y++)
{
for (int z=1; z<_zsize-1; z++)
{
t = (*this)(x,y,z);
varsum += t;
}
}
}
var = varsum / (double) n;
this->_var = var;
this-> _mean = mean;
}
void calc_heat_equation(double dx, double dt, double alpha)
{
double xup, xdn, yup, ydn, zup, zdn, tn0, tn1;
Matrix3d <T> M_new(_xsize, _ysize, _zsize);
double sum=0, varsum=0;
int n=0;
double mean, var;
for (int x=1; x<_xsize-1; x++)
{
for (int y=1; y<_ysize-1; y++)
{
for (int z=1; z<_zsize-1; z++)
{
xup = (*this)(x+1, y, z);
xdn = (*this)(x-1, y, z);
yup = (*this)(x, y+1, z);
ydn = (*this)(x, y-1, z);
zup = (*this)(x, y, z+1);
zdn = (*this)(x, y, z-1);
tn0 = (*this)(x,y,z);
tn1 = tn0+ ((alpha*dt)/(dx*dx))
* (xup+xdn+yup+ydn+zup+zdn-6*tn0);
M_new(x,y,z) =tn1;
n++;
sum+=tn1;
}
}
}
mean = sum / (double) n;
for (int x=1; x<_xsize-1; x++)
{
for (int y=1; y<_ysize-1; y++)
{
for (int z=1; z<_zsize-1; z++)
{
tn1 = M_new(x,y,z);
varsum += (tn1-mean)*(tn1-mean);
(*this)(x,y,z) = M_new(x,y,z);
}
}
}
var = varsum / (double) n;
this->_var = var;
this-> _mean = mean;
}
size_t xsize(){return _xsize;};
size_t ysize(){return _ysize;};
size_t zsize(){return _zsize;};
double _mean, _var;
private:
size_t _xsize, _ysize, _zsize;
};
//prints cross section of matrix to screen, for debugging
void print_xslice(Matrix3d<double> &M, int xcoord)
{
for (int y=0; y<M.ysize(); y++)
{
for (int z=0; z<M.zsize(); z++)
{
cout<<M(xcoord, y, z)<<',';
}
cout<<endl;
}
}
#endif