forked from li12242/AdvectionDiffusionSolver_FDM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.c
103 lines (72 loc) · 1.64 KB
/
utils.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
//
// Created by li12242 on 15-9-14.
//
#include <stdlib.h>
#include <stdio.h>
#include "utils.h"
double *BuildVector(int Nrows){
double *A = (double*) calloc(Nrows, sizeof(double));
return A;
}
double *DestroyVector(double *v){
free(v);
return NULL;
}
int *BuildIntVector(int Nrows){
int *A = (int*) calloc(Nrows, sizeof(int));
return A;
}
int *DestroyIntVector(int *v){
free(v);
return NULL;
}
/* row major storage for a 2D matrix array */
int **BuildIntMatrix(int Nrows, int Ncols){
int n;
int **A = (int**) calloc(Nrows, sizeof(int*));
A[0] = (int*) calloc(Nrows*Ncols, sizeof(int));
for(n=1;n<Nrows;++n){
A[n] = A[n-1]+ Ncols;
}
return A;
}
int **DestroyIntMatrix(int **A){
free(A[0]);
free(A);
return NULL;
}
double **BuildMatrix(int Nrows, int Ncols){
int n;
double **A = (double**) calloc(Nrows, sizeof(double*));
A[0] = (double*) calloc(Nrows*Ncols, sizeof(double));
for(n=1;n<Nrows;++n){
A[n] = A[n-1]+ Ncols;
}
return A;
}
double **DestroyMatrix(double **A){
free(A[0]);
free(A);
return NULL;
}
void PrintMatrix(char *message, double **A, int Nrows, int Ncols){
int n,m;
printf("%s\n", message);
for(n=0;n<Nrows;++n){
for(m=0;m<Ncols;++m){
printf(" %g ", A[n][m]);
}
printf(" \n");
}
}
void SaveMatrix(char *filename, double **A, int Nrows, int Ncols){
int n,m;
FILE *fp = fopen(filename, "w");
for(n=0;n<Nrows;++n){
for(m=0;m<Ncols;++m){
fprintf(fp, " %g ", A[n][m]);
}
fprintf(fp, " \n");
}
fclose(fp);
}