arrays.c (5505B)
1 #include <stdlib.h> 2 #include <stdio.h> 3 #include <math.h> 4 5 #define DEG2RAD(x) (x*M_PI/180.0) 6 7 void 8 check_magnitude(const char *func_name, int limit, int value) 9 { 10 if (value < limit) { 11 fprintf(stderr, "error: %s: input size %d is less than %d\n", 12 func_name, value, limit); 13 exit(1); 14 } 15 } 16 17 /* Translate a i,j,k index in grid with dimensions nx, ny, nz into a 18 * linear index */ 19 unsigned int 20 idx3(const unsigned int i, 21 const unsigned int j, 22 const unsigned int k, 23 const unsigned int nx, 24 const unsigned int ny) 25 { 26 return i + nx * j + nx * ny * k; 27 } 28 29 /* Translate a i,j,k index in grid with dimensions nx, ny, nz and a 30 * padding of single ghost nodes into a linear index */ 31 unsigned int 32 idx3g(const unsigned int i, 33 const unsigned int j, 34 const unsigned int k, 35 const unsigned int nx, 36 const unsigned int ny) 37 { 38 return i + 1 + (nx + 2) * (j + 1) + (nx + 2) * (ny + 2) * (k + 1); 39 } 40 41 /* Translate a i,j index in grid with dimensions nx, ny into a linear 42 * index */ 43 unsigned int 44 idx2(const unsigned int i, const unsigned int j, const unsigned int nx) 45 { 46 return i + nx * j; 47 } 48 49 /* Translate a i,j index in grid with dimensions nx, ny and a padding of 50 * single ghost nodes into a linear index */ 51 unsigned int 52 idx2g(const unsigned int i, const unsigned int j, const unsigned int nx) 53 { 54 return i + 1 + (nx + 2) * (j + 1); 55 } 56 57 /* Translate a i index in grid with a padding of single into a linear 58 * index */ 59 unsigned int 60 idx1g(const unsigned int i) 61 { 62 return i + 1; 63 } 64 65 /* Return an array of `n` linearly spaced values in the range [lower; 66 * upper] */ 67 double * 68 linspace(const double lower, const double upper, const int n) 69 { 70 int i; 71 double *x; 72 double dx; 73 74 check_magnitude(__func__, 1, n); 75 x = malloc(n * sizeof(double)); 76 dx = (upper - lower) / (double) (n - 1); 77 for (i = 0; i < n; ++i) 78 x[i] = lower + dx * i; 79 80 return x; 81 } 82 83 /* Return an array of `n-1` values with the intervals between `x` values */ 84 double * 85 spacing(const double *x, const int n) 86 { 87 int i; 88 double *dx; 89 90 check_magnitude(__func__, 2, n); 91 dx = malloc((n - 1) * sizeof(double)); 92 for (i = 0; i < n - 1; ++i) 93 dx[i] = x[i + 1] - x[i]; 94 95 return dx; 96 } 97 98 /* Return an array of `n` values with the value 0.0 */ 99 double * 100 zeros(const int n) 101 { 102 int i; 103 double *x; 104 105 check_magnitude(__func__, 1, n); 106 x = malloc(n * sizeof(double)); 107 for (i = 0; i < n; ++i) 108 x[i] = 0.0; 109 110 return x; 111 } 112 113 /* Return an array of `n` values with the value 1.0 */ 114 double * 115 ones(const int n) 116 { 117 int i; 118 double *x; 119 120 check_magnitude(__func__, 1, n); 121 x = malloc(n * sizeof(double)); 122 for (i = 0; i < n; ++i) 123 x[i] = 1.0; 124 125 return x; 126 } 127 128 /* Return an array of `n` values with a specified value */ 129 double * 130 initval(const double value, const int n) 131 { 132 int i; 133 double *x; 134 135 check_magnitude(__func__, 1, n); 136 x = malloc(n * sizeof(double)); 137 for (i = 0; i < n; ++i) 138 x[i] = value; 139 140 return x; 141 } 142 143 /* Return an array of `n` uninitialized values */ 144 double * 145 empty(const int n) 146 { 147 check_magnitude(__func__, 1, n); 148 return malloc(n * sizeof(double)); 149 } 150 151 /* Return largest value in array `a` with size `n` */ 152 double 153 max(const double *a, const int n) 154 { 155 int i; 156 double maxval; 157 158 check_magnitude(__func__, 1, n); 159 maxval = -INFINITY; 160 for (i = 0; i < n; ++i) 161 if (a[i] > maxval) 162 maxval = a[i]; 163 164 return maxval; 165 } 166 167 /* Return smallest value in array `a` with size `n` */ 168 double 169 min(const double *a, const int n) 170 { 171 int i; 172 double minval; 173 174 check_magnitude(__func__, 1, n); 175 minval = +INFINITY; 176 for (i = 0; i < n; ++i) 177 if (a[i] < minval) 178 minval = a[i]; 179 180 return minval; 181 } 182 183 void 184 print_array(const double *a, const int n) 185 { 186 int i; 187 188 check_magnitude(__func__, 1, n); 189 for (i = 0; i < n; ++i) 190 printf("%.17g\n", a[i]); 191 } 192 193 void 194 print_arrays(const double *a, const double *b, const int n) 195 { 196 int i; 197 198 check_magnitude(__func__, 1, n); 199 for (i = 0; i < n; ++i) 200 printf("%.17g\t%.17g\n", a[i], b[i]); 201 } 202 203 void 204 print_arrays_2nd_normalized(const double *a, const double *b, const int n) 205 { 206 int i; 207 double max_b; 208 209 check_magnitude(__func__, 1, n); 210 max_b = max(b, n); 211 for (i = 0; i < n; ++i) 212 printf("%.17g\t%.17g\n", a[i], b[i] / max_b); 213 } 214 215 void 216 print_three_arrays(const double *a, 217 const double *b, 218 const double *c, 219 const int n) 220 { 221 int i; 222 223 check_magnitude(__func__, 1, n); 224 for (i = 0; i < n; ++i) 225 printf("%.17g\t%.17g\t%.17g\n", a[i], b[i], c[i]); 226 } 227 228 void 229 fprint_arrays(FILE *fp, const double *a, const double *b, const int n) 230 { 231 int i; 232 233 check_magnitude(__func__, 1, n); 234 for (i = 0; i < n; ++i) 235 fprintf(fp, "%.17g\t%.17g\n", a[i], b[i]); 236 } 237 238 void 239 fprint_three_arrays(FILE *fp, 240 const double *a, 241 const double *b, 242 const double *c, 243 const int n) 244 { 245 int i; 246 247 check_magnitude(__func__, 1, n); 248 for (i = 0; i < n; ++i) 249 fprintf(fp, "%.17g\t%.17g\t%.17g\n", a[i], b[i], c[i]); 250 } 251 252 void 253 copy_values(const double *in, double *out, const int n) 254 { 255 int i; 256 257 check_magnitude(__func__, 1, n); 258 for (i = 0; i < n; ++i) 259 out[i] = in[i]; 260 } 261 262 double * 263 copy(const double *in, const int n) 264 { 265 double *out; 266 267 check_magnitude(__func__, 1, n); 268 out = empty(n); 269 copy_values(in, out, n); 270 return out; 271 } 272 273 double * 274 normalize(const double *in, const int n) 275 { 276 int i; 277 double max_val; 278 double *out; 279 280 check_magnitude(__func__, 1, n); 281 out = malloc(n * sizeof(double)); 282 copy_values(in, out, n); 283 max_val = max(out, n); 284 285 if (max_val == 0.0) 286 max_val = 1.0; 287 288 for (i = 0; i < n; ++i) 289 out[i] /= max_val; 290 291 return out; 292 }