-
Notifications
You must be signed in to change notification settings - Fork 0
/
ftcs.c
850 lines (749 loc) · 21.2 KB
/
ftcs.c
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<time.h>
#include<assert.h>
#define SWAP(a,b) {double temp=(a);(a)=(b);(b)=temp;}
#define DEBUG 0
void forwardTime(double ***matrix, double ***matrix_np1, int nx, int ny, int nz, double dx, double dy, double dz, double C, double q, int border, int NT);
void crank(double ***A, double ***B, int nx, int ny, int nz, double alpha, double dx, double C, int border, int NT);
void adi(double ***matrix, double ***matrix_np1, int nx, int ny, int nz, double alpha, double dx, double C, int border, int NT);
double ***initGaussians(int nx, int ny, int nz, double dx, double dy, double dz, double noise);
void borders(double ***matrix, double ***matrix_np1, double *zcoords, double *zcoords_np1, int nx, int ny, int nz, double dx, double dy, double dz, double noise, char *border);
void freeMats( double ***matrix, double ***matrix_np1, int nx, int ny);
void print3DMatrix(double ***matrix, int nx, int ny, int nz, double dx, double dy, double dz, int when);
void rref(double **matrix, int n, int m);
void rowSwap(double **A, int swap, int with, int n, int m);
void setMatrix(double **A, int n, int m);
void onesPivot(double **A, int row, int col, int m);
void multAdd(double **A, int row, int row2, int col, int m);
double distanceFormula(double *mat_old, double *mat_new, int length);
void welcome(int *nx, int *ny, int *nz, double *alpha, double *q, double *dt, double *dx, int *algo, int *border );
void rref2(double **A, int m, int n);
double *dvector(long nl, long nh);
int main(int argc, char *argv[]){
int i, j, l, m, n;
/* Matrix Dimensions*/
int nx=4, ny=4, nz=4;
/*parameters to be changed by user*/
int NT; /* Max Number of timesteps*/
double alpha = .01;
double dt = .001;
double q = .005;
double dx = .01;
double dz = .01;
double dy = .01;
double C = alpha*dt/pow(dx,2);
int algo = 0;
int border = 0;
srand(time(NULL));/*make some noise*/
double noise = .09 * (double)(rand()/(double)RAND_MAX);
welcome(&nx, &ny, &nz, &alpha, &q, &dt, &dx, &algo, &border );
/* if(argc >1){
FILE *fp;
fp = fopen(argv[1], "r");
fscanf(fp, "%d %d %d %d %lf %lf %lf %d %d", &nx, &ny, &nz, &NT, &dt, &alpha, &q, &border, &algo);
fclose(fp);
}
*/
assert(alpha * dt <= (0.5 * dx * dx));
/*2 3D matrices init under gaussian conditions + boundaries*/
double ***matrix = initGaussians(nx, ny, nz, dx, dy, dz, noise);
double ***matrix_np1 = initGaussians(nx, ny, nz, dx, dy, dz, noise);
int when = 0;
print3DMatrix(matrix, nx, ny, nz, dx, dx, dx, when);
double t_start, t_end;
if( algo ==0 ){
NT = 10000;
t_start = clock();
forwardTime(matrix, matrix_np1, nx, ny, nz, dx, dy, dz, C, q,border, NT);
t_end = clock();
}
else if( algo == 1 ){
NT = 5;
t_start = clock();
crank(matrix, matrix_np1, nx, ny, nz, alpha, dx, C, border, NT);
t_end = clock();
}
else if( algo == 2){
NT = 5;
t_start = clock();
adi(matrix, matrix_np1, nx, ny, nz, alpha, dx, C, border, NT);
t_end = clock();
}
else{
printf("error in parameters");
return 0;
}
freeMats(matrix, matrix_np1, nx, ny);
printf("Total Time Elapsed.....\n");
printf("%f\n",(t_end - t_start)/CLOCKS_PER_SEC);
return 0;
}
void crank(double ***matrix, double ***matrix_np1, int nx, int ny, int nz, double alpha, double dx, double C, int border, int NT){
int i, j, k, l, f, g, h;
double d;
int b0;
int bn;
int when = 1;
double ***temp;
double *tempb;
double *mat_b = (double*) malloc(sizeof(double)* nx*ny*nz);
double *mat_newb = (double*) malloc(sizeof(double)* nx*ny*nz);
double **mat_A = (double**) malloc(sizeof(double*) *nx*ny*nz);
double **augmented = (double**) malloc(sizeof(double*) *nx*ny*nz);
double *data = (double*) malloc(sizeof(double) * nx*nx*ny*ny*nz*nz);
double *augdata = (double*) malloc(sizeof(double) * nx*nx*ny*ny*nz*nz+nx*ny*nz);/*care for last column - b*/
/*now need to make augmented matrix to pass into rref*/
for(h=0;h<nx*ny*nz;h++){
*(augmented+h) = augdata + h*nx*ny*nz+1;/*care for augmented with b*/
}
switch(border){
case 0:
b0 = 0;
bn = 0;
break;
case 1:
b0 = 3;
bn = 3;
break;
case 2:
b0 = nx;
bn = 0;
break;
default :
b0 = 0;
bn = 0;
}
print3DMatrix(matrix, nx, ny, nz, dx, dx, dx, when);
when++;
/*malloc space for A*/
for(i=0;i<nx*ny*nz;i++){
*(mat_A+i) = data + i*nx*ny*nz;
}
/*make matrix b which is 1-D from init 3-D matrix --flattened*/
for(i = 0 ; i< nx ; i++){
for(j = 0 ; j < ny ; j++){
for(k = 0 ; k < nz ; k++){
*(mat_b + i*ny*nz + j*nz + k) = *(*(*(matrix + i) + j) +k);
}
}
}
for(k=0;k<NT;k++){
for(i = 0; i< nx*ny*nz ; i++){
for(j = 0;j< nx*ny*nz ; j++){
if( i == j){
*(*(mat_A+i)+j) = /*mat_b[i]**/1+6.0*C;
}
else if (i == j-1 ){
if( i != 0%nz){
*(*(mat_A+i)+j) = /**(mat_b + i-1)**/(-1.0*C);
}
else if(i ==0%nz && border== 2){
*(*(mat_A+i)+j) = /**(mat_b + (i-1)%nz)**/(-1.0*C);
}
}
else if (i == j+1){
if( i != (nz-1)%(nz-1)){
*(*(mat_A+i)+j) =/* *(mat_b + i+1)**/(-1.0*C);
}
else if(i == (nz-1)%(nz-1) && border == 2){
*(*(mat_A+i)+j) =/* *(mat_b + (i+1)%nz)**/(-1.0*C);
}
}
else if(i == j-ny*nz){
if( i > ny*nz){
*(*(mat_A+i)+j) =/* *(mat_b + i-ny*nz)**/(-1.0*C);
}
else if(i <= ny*nz && border == 2){
*(*(mat_A+i)+j) =/* *(mat_b + (i-ny*nz)%(nx*ny*nz))**/(-1.0*C);
}
}
else if(i == j+ny*nz){
if( i < ny*nz){
*(*(mat_A+i)+j) = /**(mat_b + i+ny*nz)**/(-1.0*C);
}
else if(i >= ny*nz && border == 2){
*(*(mat_A+i)+j) = /**(mat_b + (i+ny*nz)%(nx*ny*nz))**/(-1.0*C);
}
}
else if(i == j+ny){
if( i != (ny-1)%(ny-1)){
*(*(mat_A+i)+j) =/* *(mat_b + i+ny)**/(-1.0*C);
}
else if(i ==(ny-1)%(ny-1) && border == 2){
*(*(mat_A+i)+j) =/* *(mat_b + (i+ny)%ny)**/(-1.0*C);
}
}
else if(i == j-ny){
if( i != 0%ny){
*(*(mat_A+i)+j) =/* *(mat_b + i-ny)**/(-1.0*C);
}
else if(i == 0%ny && border == 2){
*(*(mat_A+i)+j) =/* *(mat_b + (i-ny)%ny)**/(-1.0*C);
}
}
else{
*(*(mat_A+i)+j) = 0.0;
}
}
/*at this point A is created*/
for(g = 0; g< nx*ny*nz ; g++){
for(h = 0;h< nx*ny*nz ; h++){
if(h == nx*ny*nz-1){
*(*(augmented+g)+h) = *(mat_b+g);
}
else{
*(*(augmented+g)+h) = *(*(mat_A+g)+h);
}
}
}
/*at this point i have Ax=b in augmented matrix form to use rref on {augmented | b} */
rref(augmented, nx*ny*nz, nx*ny*nz+1);
/*now i need to extract the nx*ny*nz+1 column to cpy back to my old mat_b, mat_A*/
for(g = 0; g< nx*ny*nz ; g++){
for(h = 0;h< nx*ny*nz ; h++){
if(h == nx*ny*nz-1){
*(mat_newb+g) = *(*(augmented+g)+h);
}
/* else{
*(*(mat_A+g)+h) = *(*(augmented+g)+h);
}*/
}
}
d = distanceFormula(mat_newb, mat_b, nx*ny*nz);
//printf("%f \n",d);
tempb = mat_b;
mat_b = mat_newb;
mat_newb = tempb;
for(f = 0 ; f< nx ; f++){
for(g = 0 ; g < ny ; g++){
for( h = 0 ; h < nz ; h++){
*(*(*(matrix + f) + g) +h) = *(mat_b + f*ny*nz + g*nz + h);
}
}
}
}
if( k==0){
print3DMatrix(matrix, nx, ny, nz, dx, dx, dx, when);
when++;
}
}
}
void adi(double ***matrix, double ***matrix_np1, int nx, int ny, int nz, double alpha, double dx, double C, int border, int NT){
int i, j, k, l, f, g, h;
int dir;
int when =1;
double d;
double ***temp;
double *tempb;
double *mat_b = (double*) malloc(sizeof(double)* nx*ny*nz);
double *mat_newb = (double*) malloc(sizeof(double)* nx*ny*nz);
double **mat_A = (double**) malloc(sizeof(double*) *nx*ny*nz);
double **augmented = (double**) malloc(sizeof(double*) *nx*ny*nz);
double *data = (double*) malloc(sizeof(double) * nx*nx*ny*ny*nz*nz);
double *augdata = (double*) malloc(sizeof(double) * nx*nx*ny*ny*nz*nz+nx*ny*nz);/*care for last column - b*/
print3DMatrix(matrix, nx, ny, nz, dx, dx, dx, when);
when++;
/*now need to make augmented matrix to pass into rref*/
for(h=0;h<nx*ny*nz;h++){
*(augmented+h) = augdata + h*nx*ny*nz+1;/*care for augmented with b*/
}
/*malloc space for A*/
for(i=0;i<nx*ny*nz;i++){
*(mat_A+i) = data + i*nx*ny*nz;
}
/*make matrix b which is 1-D from init 3-D matrix --flattened*/
for(i = 0 ; i< nx ; i++){
for(j = 0 ; j < ny ; j++){
for(k = 0 ; k < nz ; k++){
*(mat_b + i*ny*nz + j*nz + k) = *(*(*(matrix + i) + j) +k);
}
}
}
/*run in each direction, without X, then without Y, then without Z, so A is made up of 3 different 2 D variations*/
for(k=0;k<NT;k++){
for(dir=0;dir<3;dir++){
switch(dir){
case 0:
for(i = 0; i< nx*ny*nz ; i++){
for(j = 0;j< nx*ny*nz ; j++){
if( i == j){
*(*(mat_A+i)+j) = /*mat_b[i]**/1+4.0*C;
}
else if (i == j-1){
if( i != 0%nz){
*(*(mat_A+i)+j) = /**(mat_b + i-1)**/(-1.0*C);
}
else if(i ==0%nz && border== 2){
*(*(mat_A+i)+j) = /**(mat_b + (i-1)%nz)**/(-1.0*C);
}
}
else if (i == j+1){
if( i != (nz-1)%(nz-1)){
*(*(mat_A+i)+j) = /**(mat_b + i+1)**/(-1.0*C);
}
else if(i == (nz-1)%(nz-1) && border == 2){
*(*(mat_A+i)+j) =/* *(mat_b + (i+1)%nz)**/(-1.0*C);
}
}
else if(i == j+ny){
if( i != (ny-1)%(ny-1)){
*(*(mat_A+i)+j) = /**(mat_b + i+ny)**/(-1.0*C);
}
else if(i ==(ny-1)%(ny-1) && border == 2){
*(*(mat_A+i)+j) =/* *(mat_b + (i+ny)%ny)**/(-1.0*C);
}
}
else if(i == j-ny){
if( i != 0%ny){
*(*(mat_A+i)+j) = /**(mat_b + i-ny)**/(-1.0*C);
}
else if(i == 0%ny && border == 2){
*(*(mat_A+i)+j) = /**(mat_b + (i-ny)%ny)**/(-1.0*C);
}
}
else{
*(*(mat_A+i)+j) = 0.0;
}
}
}
break;
case 1:
for(i = 0; i< nx*ny*nz ; i++){
for(j = 0;j< nx*ny*nz ; j++){
if( i == j){
*(*(mat_A+i)+j) =/* mat_b[i]**/1+4.0*C;
}
else if (i == j-1){
if( i != 0%nz){
*(*(mat_A+i)+j) = /**(mat_b + i-1)**/(-1.0*C);
}
else if(i ==0%nz && border== 2){
*(*(mat_A+i)+j) = /**(mat_b + (i-1)%nz)**/(-1.0*C);
}
}
else if (i == j+1){
if( i != (nz-1)%(nz-1)){
*(*(mat_A+i)+j) = /**(mat_b + i+1)**/(-1.0*C);
}
else if(i == (nz-1)%(nz-1) && border == 2){
*(*(mat_A+i)+j) = /**(mat_b + (i+1)%nz)**/(-1.0*C);
}
}
else if(i == j-ny*nz){
if( i > ny*nz){
*(*(mat_A+i)+j) = /**(mat_b + i-ny*nz)**/(-1.0*C);
}
else if(i <= ny*nz && border == 2){
*(*(mat_A+i)+j) =/* *(mat_b + (i-ny*nz)%(nx*ny*nz))**/(-1.0*C);
}
}
else if(i == j+ny*nz){
if( i < ny*nz){
*(*(mat_A+i)+j) = /**(mat_b + i+ny*nz)**/(-1.0*C);
}
else if(i >= ny*nz && border == 2){
*(*(mat_A+i)+j) = /**(mat_b + (i+ny*nz)%(nx*ny*nz))**/(-1.0*C);
}
}
else{
*(*(mat_A+i)+j) = 0.0;
}
}
}
break;
case 2:
for(i = 0; i< nx*ny*nz ; i++){
for(j = 0;j< nx*ny*nz ; j++){
if( i == j){
*(*(mat_A+i)+j) = /*mat_b[i]**/1+4.0*C;
}
else if(i == j-ny*nz){
if( i > ny*nz){
*(*(mat_A+i)+j) = /**(mat_b + i-ny*nz)**/(-1.0*C);
}
else if(i <= ny*nz && border == 2){
*(*(mat_A+i)+j) = /**(mat_b + (i-ny*nz)%(nx*ny*nz))**/(-1.0*C);
}
}
else if(i == j+ny*nz){
if( i < ny*nz){
*(*(mat_A+i)+j) = /**(mat_b + i+ny*nz)**/(-1.0*C);
}
else if(i >= ny*nz && border == 2){
*(*(mat_A+i)+j) = /**(mat_b + (i+ny*nz)%(nx*ny*nz))**/(-1.0*C);
}
}
else if(i == j+ny){
if( i != (ny-1)%(ny-1)){
*(*(mat_A+i)+j) = /**(mat_b + i+ny)**/(-1.0*C);
}
else if(i ==(ny-1)%(ny-1) && border == 2){
*(*(mat_A+i)+j) = /**(mat_b + (i+ny)%ny)**/(-1.0*C);
}
}
else if(i == j-ny){
if( i != 0%ny){
*(*(mat_A+i)+j) = /**(mat_b + i-ny)**/(-1.0*C);
}
else if(i == 0%ny && border == 2){
*(*(mat_A+i)+j) = /**(mat_b + (i-ny)%ny)**/(-1.0*C);
}
}
else{
*(*(mat_A+i)+j) = 0.0;
}
}
}
break;
}
/*at this point A is created*/
for(g = 0; g< nx*ny*nz ; g++){
for(h = 0;h< nx*ny*nz ; h++){
if(h == nx*ny*nz-1){
*(*(augmented+g)+h) = *(mat_b+g);
}
else{
*(*(augmented+g)+h) = *(*(mat_A+g)+h);
}
}
}
/*at this point i have Ax=b in augmented matrix form to use rref on {augmented | b} */
rref(augmented, nx*ny*nz, nx*ny*nz+1);
/*now i need to extract the nx*ny*nz+1 column to cpy back to my old mat_b, mat_A*/
for(g = 0; g< nx*ny*nz ; g++){
for(h = 0;h< nx*ny*nz ; h++){
if(h == nx*ny*nz-1){
*(mat_newb+g) = *(*(augmented+g)+h);
}
/* else{
*(*(mat_A+g)+h) = *(*(augmented+g)+h);
}*/
}
}
tempb = mat_b;
mat_b = mat_newb;
mat_newb = tempb;
}
for(f = 0 ; f< nx ; f++){
for(g = 0 ; g < ny ; g++){
for( h = 0 ; h < nz ; h++){
*(*(*(matrix + f) + g) +h) = *(mat_b + f*ny*nz + g*nz + h);
}
}
}
if(k == 1){
print3DMatrix(matrix, nx, ny, nz, dx, dx, dx, when);
when++;
}
}
}
void forwardTime(double ***matrix, double ***matrix_np1, int nx, int ny, int nz, double dx, double dy, double dz, double C, double q, int border, int NT){
int i=0, j=0, k=0, l=0;
double ***temp;
double b0 = 0;
double bn = 0;
int when = 1;
switch(border){
case 0:
b0 = 0;
bn = 0;
break;
case 1:
b0 = 3;
bn = 3;
break;
case 2:
b0 = nx;
bn = 0;
break;
default :
b0 = 0;
bn = 0;
}
double up = 0.0, down = 0.0, left = 0.0, right = 0.0, front = 0.0, back = 0.0;
for(l=0;l<NT;l++){
temp = matrix;
matrix = matrix_np1;
matrix_np1 = temp;
for(i = 0 ; i< nx ; i++){
for(j = 0 ; j < ny ; j++){
for(k = 0 ; k < nz ; k++){
if(i==0){
up = b0;
}
else{
up = matrix[i-1][j][k];
}
if(i==nx-1){
down = bn;
}
else{
down = matrix[i+1][j][k];
}
if(j==0){
front = b0;
}
else{
front = matrix[i][j-1][k];
}
if(j == ny-1){
back = bn;
}
else{
back = matrix[i][j+1][k];
}
if(k==0){
left = b0;
}
else{
left = matrix[i][j][k-1];
}
if(k==nz-1){
right = bn;
}
else{
right = matrix[i][j][k+1];
}
matrix_np1[i][j][k] = matrix[i][j][k] + C * left + C*back +C* down -(C*6.0 * matrix[i][j][k]) + C*right + C*front + C*up + q;
if(l == 13 ||l==107 || l==777){
print3DMatrix(matrix_np1, nx, ny, nz, dx, dy, dz, when);
when++;
}
}
}
}
}
}
double ***initGaussians(int nx, int ny, int nz, double dx, double dy, double dz, double noise){
int i, j, l, m, n;
double ***matrix = (double ***) malloc(sizeof(double**) * nx);
double *zcoords = (double*) malloc(sizeof(double) *nx * ny * nz);
double cx2, cy2, cz2;
double midx = nx*dx/2.0;/*phnyical size divided by 2*/
double midy = ny*dy/2.0;
double midz = nz*dz/2.0;
double std2 = 0.005;/*std dev times 2*/
double e = 0.0;
/*malloc the space for the 3D array*/
/* contiguous memory bitches */
for(i=0;i<nx;i++){
*(matrix+i) = malloc(ny*sizeof(double*));
for(j=0;j<ny;j++){
*(*(matrix+i) + j) =/* (double*)malloc(nz*sizeof(double));*/zcoords+(i*nz*ny)+(j*nz);
}
}
for(l = 0;l<nx;l++){
cx2 = (l*dx-midx)*(l*dx-midx);
for(m=0;m<ny;m++){
cy2 = (m*dy-midy)*(m*dy-midy);
for(n=0;n<nz;n++){
cz2 = (n*dz-midz)*(n*dz-midz);
*(*(*(matrix+l)+m)+n) = exp(-cx2/std2-cy2/std2-cz2/std2);/*gaussian*/
}
}
}
return matrix;
}
void freeMats( double ***matrix, double ***matrix_np1, int nx, int ny){
int i;
/*free my mallocs forever*/
for(i = 0; i < nx; i++)
{
free(matrix[i]);
free(matrix_np1[i]);
}
free (matrix);
free (matrix_np1);
}
double distanceFormula(double *mat_old, double *mat_new, int length){
int i;
double sum = 0.0;
for(i=0;i<length;i++){
sum += (*(mat_old+i) - *(mat_new+i)) * (*(mat_old+i) - *(mat_new+i));
}
sum = sqrt(sum);
return sum;
}
void rref(double **matrix, int n, int m){
int i, j;
/*larger values of potential pivot on top*/
setMatrix(matrix, n, m);
int col = 0;
int row = 0;
for(row=0;row<n;row++){
while(*(*(matrix+row)+col) == 0 && col<m){
col++;
}
onesPivot(matrix, row, col, m);
for(j = row+1;j<n;j++){
if(*(*(matrix + j)+col) !=0){
multAdd(matrix, row, j, col, m);
}
}
if(row != 0){
for(j = row;j>0;j--){
multAdd(matrix, row, j-1, col, m);
}
}
col++;
}
}
void setMatrix(double **A, int n, int m){
int i, j;
for(i = 0;i<n-1;i++){
for(j=i+1;j<n;j++){
if(*(*(A+i)) < *(*(A+j))){
rowSwap(A, i, j, n, m);
}
}
}
}
void onesPivot(double **A, int row, int col, int m){
int i, j;
double pivot = *(*(A+row)+col);
for(i=col;i<m;i++){
*(*(A+row)+i) = (double)(*(*(A+row)+i) / pivot);/*this will divide all by the pivot*/
}
}
void multAdd(double **A, int row, int row2, int col, int m){
int i;
double eliminate = *(*(A+row2)+col);
for(i=col;i<m;i++){
*(*(A+row2)+i) = (*(*(A+row)+i)*eliminate*-1) + *(*(A+row2)+i);
}
}
void rowSwap(double **A, int swap, int with, int n, int m){
int i, j;
double *temp = (double *) malloc (sizeof(double)*m);
temp = *(A+swap);
*(A+swap) = *(A+with);
*(A+with) = temp;
}
void print3DMatrix(double ***matrix, int nx, int ny, int nz, double dx, double dy, double dz, int when){
int l, m, n;
FILE * init;
FILE * init1;
FILE * init2;
FILE * init3;
for(l = 0;l<nx;l++){
for(m=0;m<ny;m++){
for(n=0;n<nz;n++){
if(n == nz/2){/*slice at arbitrary (middle) z coordinate*/
switch(when){
case 0:
init = fopen ("ftcs0.txt", "a");
fprintf(init, "%lf %lf %lf %c",l*dx, m*dy, *(*(*(matrix+l)+m)+n),'\n' );
fclose(init);
break;
case 1:
init1 = fopen ("ftcs1.txt", "a");
fprintf(init1, "%lf %lf %lf %c",l*dx, m*dy, *(*(*(matrix+l)+m)+n),'\n' );
fclose(init1);
break;
case 2:
init2 = fopen ("ftcs2.txt", "a");
fprintf(init2, "%lf %lf %lf %c",l*dx, m*dy, *(*(*(matrix+l)+m)+n),'\n' );
fclose(init2);
break;
case 3:
init3 = fopen ("ftcs3.txt", "a");
fprintf(init3, "%lf %lf %lf %c",l*dx, m*dy, *(*(*(matrix+l)+m)+n),'\n' );
fclose(init3);
break;
}
// printf("%f %f %f \n", l*dx, m*dy,*(*(*(matrix+l)+m)+n));
}
}
}
}
}
void welcome(int *nx, int *ny, int *nz, double *alpha, double *q, double *dt, double *dx, int *algo, int *border ){
printf("\n\n\n\n\n********************************************\n");
printf("* FTCS - Crank-Nicholson - ADI Heat Solver *\n");
printf("********************************************\n");
printf("* The Initial Conditions Have Been Set To: *\n");
printf("nx: %d\n",*nx);
printf("ny: %d\n",*ny);
printf("nz: %d\n",*nz);
printf("alpha: %f\n", *alpha);
printf("q: %f\n",*q);
printf("dt: %f\n",*dt);
printf("dx=dy=dx: %f\n\n",*dx);
int *answer = malloc(sizeof(int));;
printf("To change these values by passing in a file, type 1 (return), otherwise type 0 (return): \n");
scanf("%d", answer);
if(*answer ==1){
printf("USAGE: int nx int ny int nz double dt double alpha double q int method int boundary\n");
printf("Please enter the complete file name> \n");
/*create file pointer, bring in file with inits via argv
* * */
char *name = malloc(sizeof(char)*255);
scanf("%s",name);
printf("%s",name);
FILE *fp;
fp = fopen(name, "w+");
fscanf(fp, "%d %d %d %lf %lf %lf %d %d", nx, ny, nz, dt, alpha,q, border, algo);
fclose(fp);
}
printf("Select Method\n0 FTCS\n1 Crank Nicholson\n2 ADI\n> ");
scanf("%d", algo);
printf("Select Boundary Conditions\n0 Zeroes\n1 Constant (3)\n2 Periodic\n > ");
scanf("%d", border);
}
void rref2(double **A, int m, int n){
double* tmp=dvector(1,n);
double tol=0.000000000000001;
double p;
long i,j,k,l;
i=1;
j=1;
while ((i <= m) && (j<= n)){
/* Initialize pivot */
p=0.; k=i;
/* Extract best pivot, and it's row index
* (even non-zero entries. Called partial pivoting -- improves numerical stability)
* */
for(l=i;l<=m;l++){
if ( p < fabs( A[l][j] ) ){
p=A[l][j];
k=l;
}
}
if (fabs(p)<=tol){
for(l=i;l<=m;l++){
A[l][j]=0.;
}
j++;
continue;
}
if(k!=i) /* Swap ith and kth rows if needed */
for(l=j;l<=n;l++) SWAP( A[i][l] , A[k][l] );
/* Divide pivot row by pivot element */
p=A[i][j];
for (l=j;l<=n;l++) A[i][l] = A[i][l]/p;
/* Subtract multiples of the pivot row from all
* other rows */
for (k=1;k<=m;k++){
if (k!=i){
for (l=j;l<=n;l++)
tmp[l]=A[k][j]*A[i][l];
for (l=j;l<=n;l++)
A[k][l]=A[k][l]-tmp[l];
}
}
if (DEBUG)
i++;
j++;
}
}
double *dvector(long nl, long nh)
/* allocate a double vector with subscript range v[nl..nh] */
{
double *v;
v=(double *)malloc((size_t) ((nh-nl+1+1)*sizeof(double)));
if (!v) printf("allocation failure in dvector()");
return v-nl+1;
}