forked from smitchaudhary/Heat-Equation-OOP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject_10.cpp
556 lines (472 loc) · 14 KB
/
project_10.cpp
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
#include <cmath>
#include <iostream>
#include <initializer_list>
#include <memory>
#include <map>
#include <stdexcept>
#include <utility>
using namespace std;
template <typename T>
class Vector
{
// Your implementation of the Vector class starts here
public:
int length;
T* data; // dynamical array
Vector()
:length(0), data(nullptr)
{
}
Vector(int n)
:length(n), data(new T[n])
{
}
int len() const
{
return length;
}
Vector(const Vector<T>& other) // Copy constructor
:Vector(other.length)
{
for(int i = 0; i<length; i++)
{
data[i] = other.data[i];
}
}
Vector(Vector<T>&& other) // Move constructor
:length(other.len()), data(other.data)
{
other.length = 0;
other.data = nullptr;
}
Vector(const initializer_list<T>& list) // Initializer list constructor
:Vector((int)list.size())
{
uninitialized_copy(list.begin(), list.end(), data);
}
~Vector() {delete[] data;length=0;data=nullptr;} // Destructor
Vector<T>& operator=(const Vector<T>& other) // Copy operator
{
if (this != &other)
{
length = other.length;
delete[] data;
data = new T[length];
for(int i=0;i<length;i++)
{
data[i] = other.data[i];
}
}
return *this;
}
Vector<T>& operator=(Vector<T>&& other) // Move operator
{
if (this != &other)
{
delete[] data;
data = other.data;
other.data = nullptr;
length = other.length;
other.length = 0;
}
return *this;
}
T& operator[](int i)
{
if(i>=length)
{
cout<<"Index of vector out of bounds. Exiting..."<<endl;
abort();
};
return data[i];
}
const T& operator[](int i) const
{
if(i>=length)
{
cout<<"Index of vector out of bounds. Exiting..."<<endl;
abort();
};
return data[i];
}
template<typename S>
Vector<typename common_type<S,T>::type> operator+(const Vector<S>& other) const
{
if (length != other.len())
{
throw "Vectors of different length";
}
else
{
Vector<typename common_type<S,T>::type> result_vector(length);
for(int i=0;i<length;i++)
{
result_vector.data[i] = data[i] + other.data[i];
}
return result_vector;
}
}
template<typename S>
Vector<typename common_type<S,T>::type> operator-(const Vector<S>& other) const
{
if (length != other.len())
{
throw "Vectors of different length";
}
else
{
Vector<typename common_type<S,T>::type> result_vector(length);
for(int i=0;i<length;i++)
{
result_vector.data[i] = data[i] - other.data[i];
}
return result_vector;
}
}
template<typename S>
Vector<typename common_type<S,T>::type> operator*(const S scalar) const
{
Vector<typename common_type<S,T>::type> result_vector(length);
for(int i=0;i<length;i++)
{
result_vector[i] = scalar*data[i];
}
return result_vector;
}
};
template<typename S, typename T>
Vector<typename common_type<S,T>::type> operator*(const S scalar ,const Vector<T>& other)
{
Vector<typename common_type<S,T>::type> result_vector(other.len());
for(int i=0;i<other.len();i++)
{
result_vector[i] = scalar*other[i];
}
return result_vector;
}
template<typename T, typename U>
typename common_type<T,U>::type
dot(const Vector<T>& lhs, const Vector<U>& rhs)
{
if (rhs.len() != lhs.len())
{
throw "Vectors of different length";
}
else
{
typename common_type<T,U>::type result = 0;
for(int i=0;i<lhs.len();i++)
{
result = result + rhs[i] * lhs[i];
}
return result;
}
}
template <typename T>
class Matrix
{
public:
int rows;
int columns;
map <pair<int, int>, T> matrixMap;
Matrix()
:rows(0), columns(0), matrixMap()
{
}
Matrix(int n, int m)
:rows(n), columns(m)
{
}
~Matrix()
{
rows=0;
columns=0;
matrixMap.clear();
}
T& operator[](const std::pair<int, int>& ij)
{
int i = ij.first;
int j = ij.second;
if (i >= rows || j >= columns)
{
throw "Index out of range";
}
else
{
for(auto it = matrixMap.begin(); it != matrixMap.end(); ++it)
{
if (i == it->first.first && j == it->first.second)
{
return it->second;
}
}
matrixMap.insert( {ij, 0});
return matrixMap.at(ij);
}
}
const T& operator()(const std::pair<int, int>& ij) const
{
int i = ij.first;
int j = ij.second;
if (i >= rows || j >= columns)
{
throw "Index out of range";
}
else
{
for(auto it = matrixMap.begin(); it != matrixMap.end(); ++it)
{
if (i == it->first.first && j == it->first.second)
{
return it->second;
}
}
throw "Value does not exist";
}
}
int col() const
{
return columns;
}
int row() const
{
return rows;
}
map <pair<int, int>, T> Map() const //reference?????
{
return matrixMap;
}
void print_matrix()
{
for(int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
cout << matrixMap[{i,j}]<<" ";
}
cout<<""<<endl;
}
}
};
template<typename T, typename U>
Vector<typename std::common_type<T,U>::type> operator*(const Matrix<T>& lhs, const Vector<U>& rhs)
{
if(lhs.col() != rhs.len())
{
throw "Incompatible sizes";
}
else
{
//map <pair<int, int>, T> matmap = lhs.Map();
Vector<typename std::common_type<T,U>::type> result_vector(lhs.row());
for(int i = 0; i < lhs.row(); i++)
{
result_vector[i] = 0;
}
for (auto const& it : lhs.Map()) // This is a c++14 thing that works
{
int i = it.first.first;
int j = it.first.second;
result_vector[i] += rhs[j]*it.second;
}
/*for(auto it = matmap.begin(); it != matmap.end(); ++it)
{
int i = it->first.first;
int j = it->first.second;
result_vector[i] += rhs[j]*matmap.at({i,j});
}*/
return result_vector;
}
}
template<typename T>
int cg(const Matrix<T>& A, const Vector<T>& b, Vector<T>& x, T tol = (T)1e-8, int maxiter = 100)
{
// CHECK IF SYMMETRIC?????
Vector<T> p = b - A*x;
Vector<T> r = b - A*x;
T alpha, beta, prev_dot;
int k=0;
while (k< maxiter)
{
prev_dot = dot(r,r);
alpha = prev_dot / dot(A*p, p);
x = x + (alpha*p);
r = r - alpha*(A*p);
if (dot(r,r) <= tol*tol)
{
return k;
}
beta = dot(r,r) / prev_dot;
p = r + beta*p;
++k;
}
return -1;
}
template <int n, typename T>
class Heat
{
// Your implementation of the heat class starts here
public:
T alpha;
int m;
T dt;
Matrix<T> M;
Heat(T a, int mu ,T timestep) : alpha(a),m(mu),dt(timestep)
{
int points = (int) pow(mu,n);
Matrix<T> Mt( points, points );
T coeff = alpha*(m+1)*(m+1)*dt;
for(int j=points-1 ; j>=0 ; j--)
{
Mt[{j,j}] = 1 + coeff*(2*n);
for(int k=0 ; k<n ; k++)
{
bool boundary_exception = !(j%(int)pow(m,k+1) < (int)pow(m,k));
if(boundary_exception)
{
for(int i=0 ; i<j ; i++)
{
bool is_neighbour = (j-i)==(int)pow(m,k);
if(is_neighbour)
{
Mt[{i,j}] = -coeff;
Mt[{j,i}] = -coeff;
}
}
}
}
}
M = Mt;
}
void print_heat_matrix()
{
M.print_matrix();
}
Matrix<T> get_matrix() const
{
return M;
}
Vector<T> exact(T t) const
{
int points = (int)pow(m,n);
Vector<T> sol(points);
T dx = (T)1/(m+1); //
for(int i = 0; i < points; i++)
{
sol[i] = exp(-t*n*alpha* pow(M_PI,2) );
for (int k = 0; k < n; k++)
{
int xk_index = i%(int)pow(m,k+1)/((int)pow(m,k));
T xk = xk_index * dx + dx;
sol[i] *= sin( M_PI * xk );
}
}
return sol;
}
Vector<T> solve(T t) const
{
int points = (int)pow(m,n); // Mwl+1=wl
Vector<T> sol(points);
T dx = (T)1/(m+1);
for(int i = 0; i < points; i++)
{
sol[i] = 1;
for (int k = 0; k < n; k++)
{
int xk_index = i%(int)pow(m,k+1)/((int)pow(m,k));
T xk = xk_index * dx + dx;
sol[i] *= sin( M_PI * xk );
}
}
int nnc = 0;
for (T l = 0; l < t ; l+=dt )
{
int iter = cg( M , sol, sol);
if(iter==-1)
{
++nnc;
}
}
cout<<" Non convergences: "<<nnc<<endl;
return sol;
}
};
int main(int argc, char* argv[])
{
// Verification of 1d system matrix
//Heat<1,double> Heat1d(0.3125, 0.1, 3);
//std::cout << Heat1d.getMatrix() << std::endl;
// Verification of 2d system matrix
//Heat<2,double> Heat2d(0.3125, 0.1, 3);
//std::cout << Heat2d.getMatrix() << std::endl;
const Vector<int> a();
Vector<int> b({1,2});
Vector<double> x({1.,1.});
auto f = b + x;
f=f*8;
return 0;
}
/*
int main(int argc, char* argv[])
{
// Verification of 1d system matrix
Heat<1,double> Heat1d(0.3125, 0.1, 3);
std::cout << Heat1d.getMatrix() << std::endl;
// Verification of 2d system matrix
Heat<2,double> Heat2d(0.3125, 0.1, 3);
std::cout << Heat2d.getMatrix() << std::endl;
return 0;
}
*/
// Ina's implementation
/*for (int i = 0; i < points; i++)
{
for (int j = 0; j< points; j++)
{
if(i == j)
{
M[i,j] = 1 + alpha*(dt/(dx*dx))*(2*n);
}
else
{
for (auto k = 0; k<n-1; k++)
{
if (i%pow(m,k) != 0 && (i+1)%pow(m,k) != 0 ) // NOT boundary points
{
M[i,j + pow(m,k)] = 1 + alpha*(dt/(dx*dx));
M[i,j - pow(m,k)] = 1 + alpha*(dt/(dx*dx));
}
else if (i%pow(m,k) = 0) // no left neighbour in dimension k
{
M[i,j + pow(m,k)] = 1 + alpha*(dt/(dx*dx)); // only right neighbour in dim k
}
else if ((i+1)%pow(m,k) = 0) // no right neighbour in dimension k
{
M[i,j - pow(m,k)] = 1 + alpha*(dt/(dx*dx)); // only left neighbour in dim k
}
}
}
}
}
// smito TEST
int points = (int) pow(mu,n);
Matrix<T> M( points, points );
T dx=1/(mu+1);
for (int i = 0; i < points; i++)
{
M[{i,i}] = 1 + alpha*(dt/(dx*dx))*(2*n);
for (int j = i+1; j < points; j++)
{
for(int k = 0; k < n; k++)
{
if((j-i)==pow(m,k) && !(j%pow(m,k+1) < pow(m,k)) )
{
M[{i,j}] = -alpha*(dt/(dx*dx));
M[{j,i}] = -alpha*(dt/(dx*dx));
}
}
}
}
*/
// Loop magic }
// Loop magic