MoDeNa  1.0
Software framework facilitating sequential multi-scale modelling
allocation.cc
Go to the documentation of this file.
1 
7 #include <stdlib.h>
9 double ***alloc_3Ddmatrix (\
10  int nx , \
11  int ny , \
12  int nz )
13 {
14  int i, j;
15  double ***amat = NULL;
16 
17  amat = (double ***)calloc((size_t)nx, sizeof(double **));
18  if (amat == NULL)
19  return NULL;
20 
21  *amat = (double **)calloc((size_t)(nx*ny), sizeof(double *));
22  if (*amat == NULL)
23  return NULL;
24  for (i = 1; i < nx; i++)
25  amat[i] = &amat[0][i*ny];
26 
27  **amat = (double *)calloc((size_t)(nx*ny*nz), sizeof(double));
28  if (**amat == NULL)
29  return NULL;
30  for (i = 0; i < nx; i++)
31  for (j = 0; j < ny; j++)
32  amat[i][j] = &amat[0][0][i*ny*nz+j*nz];
33 
34  return amat;
35 }
37 double ***free_3Ddmatrix (double ***amat )
38 {
39  free ((void *)**amat);
40  free ((void *) *amat);
41  free ((void *) amat);
42  amat = NULL;
43  return amat;
44 }
46 float ***alloc_3Dfmatrix (\
47  int nx , \
48  int ny , \
49  int nz )
50 {
51  int i, j;
52  float ***amat = NULL;
53 
54  amat = (float ***)calloc((size_t)nx, sizeof(float **));
55  if (amat == NULL)
56  return NULL;
57 
58  *amat = (float **)calloc((size_t)(nx*ny), sizeof(float *));
59  if (*amat == NULL)
60  return NULL;
61  for (i = 1; i < nx; i++)
62  amat[i] = &amat[0][i*ny];
63 
64  **amat = (float *)calloc((size_t)(nx*ny*nz), sizeof(float));
65  if (**amat == NULL)
66  return NULL;
67  for (i = 0; i < nx; i++)
68  for (j = 0; j < ny; j++)
69  amat[i][j] = &amat[0][0][i*ny*nz+j*nz];
70 
71  return amat;
72 }
74 float ***free_3Dfmatrix (float ***amat )
75 {
76  free ((void *)**amat);
77  free ((void *) *amat);
78  free ((void *) amat);
79  amat = NULL;
80  return amat;
81 }
83 int ***alloc_3Dmatrix (\
84  int nx , \
85  int ny , \
86  int nz )
87 {
88  int i, j;
89  int ***amat = NULL;
90 
91  amat = (int ***)calloc((size_t)nx, sizeof(int **));
92  if (amat == NULL)
93  return NULL;
94 
95  *amat = (int **)calloc((size_t)(nx*ny), sizeof(int *));
96  if (*amat == NULL)
97  return NULL;
98  for (i = 1; i < nx; i++)
99  amat[i] = &amat[0][i*ny];
100 
101  **amat = (int *)calloc((size_t)(nx*ny*nz), sizeof(int));
102  if (**amat == NULL)
103  return NULL;
104  for (i = 0; i < nx; i++)
105  for (j = 0; j < ny; j++)
106  amat[i][j] = &amat[0][0][i*ny*nz+j*nz];
107 
108  return amat;
109 }
111 int ***free_3Dmatrix (int ***amat )
112 {
113  free ((void *)**amat);
114  free ((void *) *amat);
115  free ((void *) amat);
116  amat = NULL;
117  return amat;
118 }
120 double **alloc_dmatrix (\
121  int nx , \
122  int ny )
123 {
124  int i;
125  double **amat = NULL;
126 
127  /*
128  * Allocate the matrix.
129  */
130  amat = (double **)calloc((size_t)nx, sizeof(double *));
131  if (amat == NULL)
132  return NULL;
133 
134  *amat = (double *)calloc((size_t)(nx*ny), sizeof(double));
135  if (*amat == NULL)
136  return NULL;
137 
138  for (i = 1; i < nx; i++)
139  amat[i] = &amat[0][i*ny];
140 
141  return amat;
142 }
144 double **free_dmatrix (double **amat )
145 {
146  free ((void *)*amat);
147  free ((void *) amat);
148  amat = NULL;
149  return amat;
150 }
152 float **alloc_fmatrix (\
153  int nx , \
154  int ny )
155 {
156  int i;
157  float **amat = NULL;
158 
159  /*
160  * Allocate the matrix.
161  */
162  amat = (float **)calloc((size_t)nx, sizeof(float *));
163  if (amat == NULL)
164  return NULL;
165 
166  *amat = (float *)calloc((size_t)(nx*ny), sizeof(float));
167  if (*amat == NULL)
168  return NULL;
169 
170  for (i = 1; i < nx; i++)
171  amat[i] = &amat[0][i*ny];
172 
173  return amat;
174 }
176 float **free_fmatrix (float **amat )
177 {
178  free ((void *)*amat);
179  free ((void *) amat);
180  amat = NULL;
181  return amat;
182 }
184 int **alloc_matrix (\
185  int nx , \
186  int ny )
187 {
188  int i;
189  int **amat = NULL;
190 
191  /*
192  * Allocate the matrix.
193  */
194  amat = (int **)calloc((size_t)nx, sizeof(int *));
195  if (amat == NULL)
196  return NULL;
197 
198  *amat = (int *)calloc((size_t)(nx*ny), sizeof(int));
199  if (*amat == NULL)
200  return NULL;
201 
202  for (i = 1; i < nx; i++)
203  amat[i] = &amat[0][i*ny];
204 
205  return amat;
206 }
208 int **free_matrix (int **amat )
209 {
210  free ((void *)*amat);
211  free ((void *) amat);
212  amat = NULL;
213  return amat;
214 }
float ** free_fmatrix(float **amat)
free 2D float matrix
Definition: allocation.cc:176
double *** alloc_3Ddmatrix(int nx, int ny, int nz)
allocate 3D double matrix
Definition: allocation.cc:9
float *** free_3Dfmatrix(float ***amat)
free 3D float matrix
Definition: allocation.cc:74
float ** alloc_fmatrix(int nx, int ny)
allocate 2D float matrix
Definition: allocation.cc:152
int *** free_3Dmatrix(int ***amat)
free 3D integer matrix
Definition: allocation.cc:111
double ** alloc_dmatrix(int nx, int ny)
allocate 2D double matrix
Definition: allocation.cc:120
int ** alloc_matrix(int nx, int ny)
allocate 2D integer matrix
Definition: allocation.cc:184
float *** alloc_3Dfmatrix(int nx, int ny, int nz)
allocate 3D float matrix
Definition: allocation.cc:46
int ** free_matrix(int **amat)
free 2D integer matrix
Definition: allocation.cc:208
int nx
domain size in X
Definition: globals.cc:19
double *** free_3Ddmatrix(double ***amat)
free 3D double matrix
Definition: allocation.cc:37
double ** free_dmatrix(double **amat)
free 2D double matrix
Definition: allocation.cc:144
int *** alloc_3Dmatrix(int nx, int ny, int nz)
allocate 3D integer matrix
Definition: allocation.cc:83
int ny
domain size in Y
Definition: globals.cc:20