| 1 | /************************************************************************* |
| 2 | * * |
| 3 | * Vega FEM Simulation Library Version 3.1 * |
| 4 | * * |
| 5 | * "matrix" library , Copyright (C) 2007 CMU, 2009 MIT * |
| 6 | * All rights reserved. * |
| 7 | * * |
| 8 | * Code author: Jernej Barbic * |
| 9 | * http://www.jernejbarbic.com/code * |
| 10 | * Research: Jernej Barbic, Doug L. James, Jovan Popovic * |
| 11 | * Funding: NSF, Link Foundation, Singapore-MIT GAMBIT Game Lab * |
| 12 | * * |
| 13 | * This library is free software; you can redistribute it and/or * |
| 14 | * modify it under the terms of the BSD-style license that is * |
| 15 | * included with this library in the file LICENSE.txt * |
| 16 | * * |
| 17 | * This library is distributed in the hope that it will be useful, * |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the file * |
| 20 | * LICENSE.TXT for more details. * |
| 21 | * * |
| 22 | *************************************************************************/ |
| 23 | |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include <ctype.h> |
| 28 | #include "matrixMacros.h" |
| 29 | #include "matrixIO.h" |
| 30 | |
| 31 | // writes out the m x n matrix onto the stream, in binary format |
| 32 | // LAPACK column-major order |
| 33 | template <class real> |
| 34 | int WriteMatrixToStream(FILE * file, int m, int n, const real * matrix) |
| 35 | { |
| 36 | if ((int)(fwrite(matrix,sizeof(real),m*n,file)) < m*n) |
| 37 | return 1; |
| 38 | return 0; |
| 39 | } |
| 40 | |
| 41 | // writes out the m x n matrix header onto the stream |
| 42 | int WriteMatrixHeaderToStream(FILE * file, int m, int n) |
| 43 | { |
| 44 | if (fwrite(&m,sizeof(int),1,file) < 1) |
| 45 | return 1; |
| 46 | |
| 47 | if (fwrite(&n,sizeof(int),1,file) < 1) |
| 48 | return 1; |
| 49 | |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | |
| 54 | template <class real> |
| 55 | int WriteMatrixToDisk(const char * filename, int m, int n, const real * matrix) |
| 56 | { |
| 57 | FILE * file; |
| 58 | file = fopen(filename, "wb"); |
| 59 | if (!file) |
| 60 | { |
| 61 | printf ("Can't open output file: %s.\n", filename); |
| 62 | return 1; |
| 63 | } |
| 64 | |
| 65 | if (WriteMatrixHeaderToStream(file,m,n) != 0) |
| 66 | { |
| 67 | printf ("Error writing the matrix header to disk file: %s.\n", filename); |
| 68 | return 1; |
| 69 | } |
| 70 | |
| 71 | if (WriteMatrixToStream(file,m,n,matrix) != 0) |
| 72 | { |
| 73 | printf ("Error writing the matrix to disk file: %s.\n", filename); |
| 74 | return 1; |
| 75 | } |
| 76 | |
| 77 | fclose(file); |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | template <class real> |
| 83 | int AppendMatrixToDisk(const char * filename, int mAppendix, int nAppendix, const real * matrixAppendix) |
| 84 | { |
| 85 | int m, n; |
| 86 | real * matrix; |
| 87 | int code = ReadMatrixFromDisk(filename, &m, &n, &matrix); |
| 88 | if (code != 0) |
| 89 | { |
| 90 | printf ("Can't open output file: %s.\n", filename); |
| 91 | return 1; |
| 92 | } |
| 93 | |
| 94 | if (mAppendix != m) |
| 95 | { |
| 96 | printf ("The existing matrix %s and appendix matrix do not have the same number of rows.\n", filename); |
| 97 | return 1; |
| 98 | } |
| 99 | |
| 100 | matrix = (real*) realloc (matrix, sizeof(real) * mAppendix * (n + nAppendix)); |
| 101 | memcpy(&matrix[mAppendix * n], matrixAppendix, sizeof(real) * mAppendix * nAppendix); |
| 102 | |
| 103 | code = WriteMatrixToDisk(filename, mAppendix, n + nAppendix, matrix); |
| 104 | if (code != 0) |
| 105 | { |
| 106 | printf ("Error writing the matrix to disk file: %s.\n", filename); |
| 107 | return 1; |
| 108 | } |
| 109 | |
| 110 | free(matrix); |
| 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | // read the m x n matrix from the stream, in binary format |
| 116 | template <class real> |
| 117 | int ReadMatrixFromStream(FILE * file, int M, int N, real * matrix) |
| 118 | { |
| 119 | unsigned int readBytes; |
| 120 | if ((readBytes = fread(matrix,sizeof(real),M*N,file)) < (unsigned int)M*N) |
| 121 | { |
| 122 | printf("Error: I have only read %u bytes. sizeof(real)=%lu\n", readBytes, sizeof(real)); |
| 123 | return 1; |
| 124 | } |
| 125 | |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | template <class real> |
| 130 | int ReadMatrixFromDisk(const char * filename, int * m, int * n, real ** matrix) |
| 131 | { |
| 132 | FILE * file; |
| 133 | file = fopen(filename ,"rb"); |
| 134 | if (!file) |
| 135 | { |
| 136 | printf ("Can't open input matrix file: %s.\n", filename); |
| 137 | return 1; |
| 138 | } |
| 139 | |
| 140 | if (ReadMatrixSizeFromStream(file,m,n) != 0) |
| 141 | { |
| 142 | printf ("Error reading matrix header from disk file: %s.\n", filename); |
| 143 | return 1; |
| 144 | } |
| 145 | |
| 146 | //int size = (*m) * (*n) * sizeof(real) + 2 * sizeof(int); |
| 147 | |
| 148 | *matrix = (real *) malloc (sizeof(real)*(*m)*(*n)); |
| 149 | |
| 150 | if (ReadMatrixFromStream(file,*m,*n,*matrix) != 0) |
| 151 | { |
| 152 | printf ("Error reading matrix data from disk file: %s.\n", filename); |
| 153 | return 1; |
| 154 | } |
| 155 | |
| 156 | fclose(file); |
| 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | template <class real> |
| 162 | int ReadMatrixFromDiskListOfFiles(const char * fileList, int * m, int * n, real ** matrix) |
| 163 | { |
| 164 | FILE * jobFileList = fopen(fileList,"r"); |
| 165 | |
| 166 | if (!jobFileList) |
| 167 | { |
| 168 | printf("Couldn't open matrix list file %s.\n", fileList); |
| 169 | return 1; |
| 170 | } |
| 171 | |
| 172 | *n = 0; |
| 173 | |
| 174 | // pass 1: determine the total number of columns |
| 175 | char matrixFilename[4096]; |
| 176 | int totalFiles = 0; |
| 177 | while (fgets(matrixFilename,4096,jobFileList) != NULL) |
| 178 | { |
| 179 | // remove trailing newline character from matrixFilename |
| 180 | int k=0; |
| 181 | while (matrixFilename[k] != '\0') |
| 182 | k++; |
| 183 | |
| 184 | if ((k > 0) && (matrixFilename[k-1] == '\n')) |
| 185 | matrixFilename[k-1] = '\0'; |
| 186 | |
| 187 | if (k <= 1) // blank line, ignore it |
| 188 | continue; |
| 189 | |
| 190 | totalFiles++; |
| 191 | if (totalFiles % 100 == 1) |
| 192 | printf ("\nPass 1: Processing input file: %s.\n",matrixFilename); |
| 193 | |
| 194 | // determine the number of degrees of freedom |
| 195 | int M,N; |
| 196 | if (ReadMatrixSizeFromDisk(matrixFilename,&M,&N) != 0) |
| 197 | { |
| 198 | printf("Error: couldn't load mode information from file %s.\n",matrixFilename); |
| 199 | return 1; |
| 200 | } |
| 201 | |
| 202 | printf("%d ",N); fflush(NULL); |
| 203 | |
| 204 | if (*n == 0) |
| 205 | { |
| 206 | // register the number of degrees of freedom |
| 207 | *m = M; |
| 208 | } |
| 209 | else // compare the current length to the length of the first vector |
| 210 | { |
| 211 | if (M != *m) |
| 212 | { |
| 213 | printf("Error: length of vector (%d x %d) from file %s is not the same as for the other vectors (%d x 1).\n",M,N,matrixFilename,*m); |
| 214 | return 1; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | *n += N; // this file contained N columns |
| 219 | } |
| 220 | |
| 221 | printf("\n"); |
| 222 | |
| 223 | // now, *m contains the number of rows |
| 224 | // *n equals the total number of columns |
| 225 | |
| 226 | printf("Creating %d x %d matrix... (%G Mb)\n", *m, *n, 1.0 * sizeof(double) * (*m) * (*n) / 1024 / 1024); |
| 227 | *matrix = (real*) malloc (sizeof(real) * (*m) * (*n)); |
| 228 | |
| 229 | rewind(jobFileList); |
| 230 | |
| 231 | // pass 2: assemble the files together |
| 232 | totalFiles = 0; |
| 233 | real * currentMatrix; |
| 234 | int currentColumn = 0; |
| 235 | while (fgets(matrixFilename,4096,jobFileList) != NULL) |
| 236 | { |
| 237 | // remove trailing newline character from matrixFilename |
| 238 | int k=0; |
| 239 | while (matrixFilename[k] != '\0') |
| 240 | { |
| 241 | k++; |
| 242 | } |
| 243 | if ((k > 0) && (matrixFilename[k-1] == '\n')) |
| 244 | matrixFilename[k-1] = '\0'; |
| 245 | |
| 246 | if (k <= 1) // blank line, ignore it |
| 247 | continue; |
| 248 | |
| 249 | totalFiles++; |
| 250 | |
| 251 | if (totalFiles % 100 == 1) |
| 252 | printf ("Pass 2: Processing input file: %s.\n",matrixFilename); |
| 253 | |
| 254 | // read the deformations from file 'matrixFilename' |
| 255 | int M,N; |
| 256 | if (ReadMatrixFromDisk(matrixFilename, &M, &N, ¤tMatrix) != 0) |
| 257 | { |
| 258 | printf("Couldn't load matrix from file %s.\n",matrixFilename); |
| 259 | return 1; |
| 260 | } |
| 261 | |
| 262 | memcpy(&(*matrix)[ELT(*m,0,currentColumn)], currentMatrix, sizeof(real) * M * N); |
| 263 | |
| 264 | printf("%d ",N); fflush(NULL); |
| 265 | |
| 266 | free(currentMatrix); |
| 267 | |
| 268 | currentColumn += N; |
| 269 | } |
| 270 | |
| 271 | fclose(jobFileList); |
| 272 | |
| 273 | printf("Matrix has been loaded (from %d files).\n", totalFiles); |
| 274 | |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | template <class real> |
| 279 | int WriteMatrixToDisk_(const char * filename, int m, int n, const real * matrix) |
| 280 | { |
| 281 | if (WriteMatrixToDisk(filename, m, n, matrix) != 0) |
| 282 | exit(1); |
| 283 | |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | template <class real> |
| 288 | int AppendMatrixToDisk_(const char * filename, int mAppendix, int nAppendix, const real * matrixAppendix) |
| 289 | { |
| 290 | if (AppendMatrixToDisk(filename, mAppendix, nAppendix, matrixAppendix) != 0) |
| 291 | exit(1); |
| 292 | |
| 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | template <class real> |
| 297 | int ReadMatrixFromDisk_(const char * filename, int * m, int * n, real ** matrix) |
| 298 | { |
| 299 | if (ReadMatrixFromDisk(filename, m, n, matrix) != 0) |
| 300 | exit(1); |
| 301 | |
| 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | int ReadMatrixSizeFromDisk(const char * filename, int * m, int * n) |
| 306 | { |
| 307 | FILE * file; |
| 308 | file = fopen(filename, "rb"); |
| 309 | if (!file) |
| 310 | { |
| 311 | printf ("Can't open input matrix file: %s.\n", filename); |
| 312 | return 1; |
| 313 | } |
| 314 | |
| 315 | if (fread(m,sizeof(int),1,file) < 1) |
| 316 | return 1; |
| 317 | |
| 318 | if (fread(n,sizeof(int),1,file) < 1) |
| 319 | return 1; |
| 320 | |
| 321 | fclose(file); |
| 322 | |
| 323 | return 0; |
| 324 | } |
| 325 | |
| 326 | void ReadMatrixSizeFromDisk_(const char * filename, int * m, int * n) |
| 327 | { |
| 328 | if (ReadMatrixSizeFromDisk(filename, m, n) != 0) |
| 329 | exit(1); |
| 330 | } |
| 331 | |
| 332 | int ReadMatrixSizeFromStream(FILE * file, int * m, int * n) |
| 333 | { |
| 334 | if (fread(m,sizeof(int),1,file) < 1) |
| 335 | return 1; |
| 336 | |
| 337 | if (fread(n,sizeof(int),1,file) < 1) |
| 338 | return 1; |
| 339 | |
| 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | int OpenFile_(const char * filename, FILE ** fin, const char * mode) |
| 344 | { |
| 345 | *fin = fopen(filename, mode); |
| 346 | if (!(*fin)) |
| 347 | { |
| 348 | printf("Error: could not open file %s.\n", filename); |
| 349 | exit(1); |
| 350 | } |
| 351 | |
| 352 | return 0; |
| 353 | } |
| 354 | |
| 355 | void Abort_(const char * reason) |
| 356 | { |
| 357 | Abort_(reason,1); |
| 358 | } |
| 359 | |
| 360 | void Abort_(const char * reason, int exitCode) |
| 361 | { |
| 362 | printf("%s\n",reason); |
| 363 | exit(exitCode); |
| 364 | } |
| 365 | |
| 366 | int Assert_(int a, int b, int positionIdentifier) |
| 367 | { |
| 368 | if (a != b) |
| 369 | { |
| 370 | printf("Error: assertion %d == %d failed (%d).\n",a,b,positionIdentifier); |
| 371 | exit(1); |
| 372 | } |
| 373 | return 0; |
| 374 | } |
| 375 | |
| 376 | int Assert_(bool cond, int positionIdentifier) |
| 377 | { |
| 378 | if (!cond) |
| 379 | { |
| 380 | printf("Error: assertion failed (%d).\n",positionIdentifier); |
| 381 | exit(1); |
| 382 | } |
| 383 | return 0; |
| 384 | } |
| 385 | |
| 386 | // prints the matrix out to stdout in Mathematica format |
| 387 | template <class real> |
| 388 | void PrintMatrixInMathematicaFormat(int n, int r, real * U) |
| 389 | { |
| 390 | printf("{\n"); |
| 391 | |
| 392 | for(int i=0; i<n; i++) |
| 393 | { |
| 394 | printf("{"); |
| 395 | for(int j=0; j<r; j++) |
| 396 | { |
| 397 | printf("%.15f",U[ELT(n,i,j)]); |
| 398 | if (j != r-1) |
| 399 | printf(", "); |
| 400 | } |
| 401 | printf("}"); |
| 402 | |
| 403 | if (i != n-1) |
| 404 | printf(",\n"); |
| 405 | } |
| 406 | |
| 407 | printf("\n}\n"); |
| 408 | } |
| 409 | |
| 410 | void ChangeMatrixHeader(const char * filename, int numRows, int numColumns) |
| 411 | { |
| 412 | FILE * fin = fopen(filename, "r+"); |
| 413 | fwrite(&numRows,sizeof(int),1,fin); |
| 414 | fwrite(&numColumns,sizeof(int),1,fin); |
| 415 | fclose(fin); |
| 416 | } |
| 417 | |
| 418 | template <class T> |
| 419 | int ReadBinary(FILE * fin, T * data) |
| 420 | { |
| 421 | if (fread(data,sizeof(T),1,fin) < 1) |
| 422 | return 1; |
| 423 | |
| 424 | return 0; |
| 425 | } |
| 426 | |
| 427 | |
| 428 | template <class T> |
| 429 | void ReadBinary_(FILE * fin, T * data) |
| 430 | { |
| 431 | if (ReadBinary<T>(fin,data) != 0) |
| 432 | { |
| 433 | printf("Read binary: Error reading data.\n"); |
| 434 | exit(1); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | template <class T> |
| 439 | int ReadBinaryBuffer(FILE * fin, int size, T * data) |
| 440 | { |
| 441 | if (fread(data,sizeof(T),size,fin) < (unsigned int)size) |
| 442 | return 1; |
| 443 | |
| 444 | return 0; |
| 445 | } |
| 446 | |
| 447 | template <class T> |
| 448 | void ReadBinaryBuffer_(FILE * fin, int size, T * data) |
| 449 | { |
| 450 | if (ReadBinaryBuffer<T>(fin,size,data) != 0) |
| 451 | { |
| 452 | printf("Read binary buffer: Error reading data.\n"); |
| 453 | exit(1); |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | template <class real> |
| 458 | int ReadMatrixFromDiskTextFile(const char * filename, int * m, int * n, real ** matrix) |
| 459 | { |
| 460 | // open input |
| 461 | FILE * file; |
| 462 | file = fopen(filename, "r"); |
| 463 | if (!file) |
| 464 | { |
| 465 | printf("Error: couldn't open input file %s.\n", filename); |
| 466 | return 1; |
| 467 | } |
| 468 | |
| 469 | // parse dimensions |
| 470 | if (fscanf(file,"%d\n",m) < 1) |
| 471 | { |
| 472 | printf ("ReadMatrixFromDiskTextFile: failed to read the number of rows.\n"); |
| 473 | return 1; |
| 474 | } |
| 475 | |
| 476 | if (fscanf(file,"%d\n",n) < 1) |
| 477 | { |
| 478 | printf ("ReadMatrixFromDiskTextFile: failed to read the number of columns.\n"); |
| 479 | return 1; |
| 480 | } |
| 481 | |
| 482 | if (*m <= 0) |
| 483 | { |
| 484 | printf("Error: bad parameter for the number of rows: %d.\n", *m); |
| 485 | return 1; |
| 486 | } |
| 487 | |
| 488 | if (*n <= 0) |
| 489 | { |
| 490 | printf("Error: bad parameter for the number of columns: %d.\n", *n); |
| 491 | return 0; |
| 492 | } |
| 493 | |
| 494 | // allocate space |
| 495 | *matrix = (real *)malloc(sizeof(real)* *m* *n); |
| 496 | real data; |
| 497 | |
| 498 | int i=0; |
| 499 | |
| 500 | int bufSize = *n * 100; |
| 501 | char * buf = (char*) malloc (sizeof(char) * bufSize); |
| 502 | |
| 503 | char sscanfFormat[4] = "%lf"; |
| 504 | if (sizeof(real) == sizeof(float)) |
| 505 | strcpy(sscanfFormat,"%f"); |
| 506 | |
| 507 | while(fgets(buf,bufSize,file) != NULL) |
| 508 | { |
| 509 | /* |
| 510 | if (i % 100 == 1) |
| 511 | { |
| 512 | printf("%d ",i); |
| 513 | fflush(NULL); |
| 514 | } |
| 515 | */ |
| 516 | |
| 517 | if (i == *m) |
| 518 | { |
| 519 | printf("Error: the number of data rows in file %s is greater than the declared dimension %d x %d .\n", filename, *m, *n); |
| 520 | free(buf); |
| 521 | free(*matrix); |
| 522 | return 1; |
| 523 | } |
| 524 | |
| 525 | // remove multiple white space characters from line |
| 526 | char * w = buf; |
| 527 | while (*w != '\0') |
| 528 | { |
| 529 | while ((*w == ' ') && (*(w+1) == ' ')) // erase second blank |
| 530 | { |
| 531 | char * u = w+1; |
| 532 | while (*u != '\0') // shift everything left one char |
| 533 | { |
| 534 | *u = *(u+1); |
| 535 | u++; |
| 536 | } |
| 537 | } |
| 538 | w++; |
| 539 | } |
| 540 | |
| 541 | // remove whitespace at beginning of line |
| 542 | if (*buf == ' ') |
| 543 | { |
| 544 | char * u = buf; |
| 545 | while (*u != '\0') // shift everything left one char |
| 546 | { |
| 547 | *u = *(u+1); |
| 548 | u++; |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | //printf("%s\n",buf); |
| 553 | |
| 554 | char * token = strtok(buf," "); |
| 555 | |
| 556 | for(int j=0; j<*n; j++) |
| 557 | { |
| 558 | if (token == NULL) |
| 559 | { |
| 560 | printf("Token is NULL. Error parsing line %d.\n",i+1); |
| 561 | return 1 ; |
| 562 | } |
| 563 | |
| 564 | |
| 565 | |
| 566 | if (sscanf(token,sscanfFormat,&data) < 1) |
| 567 | { |
| 568 | printf("Cannot parse data from current token. Error parsing line %d.\n",i); |
| 569 | return 1 ; |
| 570 | } |
| 571 | |
| 572 | //printf("%G\n",data); |
| 573 | |
| 574 | (*matrix)[ELT(*m,i,j)] = data; |
| 575 | |
| 576 | token = strtok(NULL," "); |
| 577 | |
| 578 | } |
| 579 | i++; |
| 580 | } |
| 581 | |
| 582 | fclose(file); |
| 583 | |
| 584 | free(buf); |
| 585 | |
| 586 | if (i < *m) // not enough data has been read |
| 587 | { |
| 588 | printf("Error: the number of data rows in file %s is smaller than the declared dimension %d x %d .\n", filename, *m, *n); |
| 589 | free(*matrix); |
| 590 | return 1; |
| 591 | } |
| 592 | |
| 593 | return 0; |
| 594 | } |
| 595 | |
| 596 | template <class real> |
| 597 | int WriteMatrixToDiskTextFile(const char * filename, int m, int n, const real * matrix) |
| 598 | { |
| 599 | // open file |
| 600 | FILE * fout; |
| 601 | fout = fopen(filename, "w"); |
| 602 | |
| 603 | if( !fout ) |
| 604 | { |
| 605 | printf("Couldn't open output file %s.\n", filename); |
| 606 | return 1; |
| 607 | } |
| 608 | |
| 609 | fprintf(fout, "%d\n%d\n", m, n); |
| 610 | |
| 611 | // matrix is m x n |
| 612 | int i,j; |
| 613 | for (i=0; i<m; i++) |
| 614 | { |
| 615 | for (j=0; j<n; j++) |
| 616 | fprintf(fout,"%.15G ",matrix[ELT(m,i,j)]); |
| 617 | fprintf(fout,"\n"); |
| 618 | } |
| 619 | |
| 620 | fclose(fout); |
| 621 | |
| 622 | return 0; |
| 623 | } |
| 624 | |
| 625 | // n will contain number of nodes |
| 626 | // r will contain number of frequencies |
| 627 | // modes will contain the mode matrix |
| 628 | template <class real> |
| 629 | int ReadModesFromDisk(const char * filename, int * N, int * R, real ** frequencies, real ** modes) |
| 630 | { |
| 631 | FILE * file; |
| 632 | file = fopen(filename, "r"); |
| 633 | |
| 634 | if (!file) |
| 635 | { |
| 636 | printf ("ReadModesFromDisk: failed to open input file %s.\n", filename); |
| 637 | return 1; |
| 638 | } |
| 639 | |
| 640 | if (fscanf(file,"%d\n",N) < 1) |
| 641 | { |
| 642 | printf ("ReadModesFromDisk: failed to read the number of nodes.\n"); |
| 643 | return 1; |
| 644 | } |
| 645 | |
| 646 | if (fscanf(file,"%d\n",R) < 1) |
| 647 | { |
| 648 | printf ("ReadModesFromDisk: failed to read the number of modes.\n"); |
| 649 | return 1; |
| 650 | } |
| 651 | |
| 652 | int n = *N; |
| 653 | int r = *R; |
| 654 | |
| 655 | *modes = (real *) malloc (sizeof(real)*3*n*r); |
| 656 | *frequencies = (real *) malloc (sizeof(real)*r); |
| 657 | |
| 658 | // read in frequencies |
| 659 | int i,j,k; |
| 660 | char s[1024]; |
| 661 | |
| 662 | for (i=0; i<r; i++) |
| 663 | { |
| 664 | char formatString[6]; |
| 665 | if (sizeof(real) == sizeof(float)) |
| 666 | strcpy(formatString,"%f\n"); |
| 667 | else |
| 668 | strcpy(formatString,"%lf\n"); |
| 669 | |
| 670 | if (fscanf(file,formatString,&((*frequencies)[i])) < 1) |
| 671 | { |
| 672 | printf ("ReadModesFromDisk: failed to read frequency %d.\n",i+1); |
| 673 | return 1; |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | char formatStringLong[20]; |
| 678 | if (sizeof(real) == sizeof(float)) |
| 679 | strcpy(formatStringLong,"%d %f %f %f\n"); |
| 680 | else |
| 681 | strcpy(formatStringLong,"%d %lf %lf %lf\n"); |
| 682 | |
| 683 | // read in modes |
| 684 | // each mode is one column |
| 685 | // matrix is 3n x r |
| 686 | for (i=0; i<r; i++) // over all modes |
| 687 | { |
| 688 | if (!fgets(s,1024,file)) // node header |
| 689 | { |
| 690 | printf ("ReadModesFromDisk: failed to read mode header for mode %d.\n",i+1); |
| 691 | return 1; |
| 692 | } |
| 693 | |
| 694 | for (j=0; j<n; j++) // over all nodes |
| 695 | { |
| 696 | if (fscanf(file, formatStringLong, &k, |
| 697 | &((*modes)[ELT(3*n,3*j,i)]), |
| 698 | &((*modes)[ELT(3*n,3*j+1,i)]), |
| 699 | &((*modes)[ELT(3*n,3*j+2,i)])) < 4) |
| 700 | { |
| 701 | printf ("ReadModesFromDisk: failed to read mode %d data at node %d.\n",i+1,j+1); |
| 702 | return 1; |
| 703 | } |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | fclose(file); |
| 708 | |
| 709 | return 0; |
| 710 | } |
| 711 | |
| 712 | int ReadModeInfoFromDisk(const char * filename, int * N, int * R) |
| 713 | { |
| 714 | FILE * file; |
| 715 | file = fopen(filename, "r"); |
| 716 | |
| 717 | if (!file) |
| 718 | { |
| 719 | printf ("ReadModeInfoFromDisk: failed to open input file %s.\n", filename); |
| 720 | return 1; |
| 721 | } |
| 722 | |
| 723 | if (fscanf(file,"%d\n",N) < 1) |
| 724 | { |
| 725 | printf ("ReadModeInfoFromDisk: failed to read the number of nodes.\n"); |
| 726 | return 1; |
| 727 | } |
| 728 | |
| 729 | if (fscanf(file,"%d\n",R) < 1) |
| 730 | { |
| 731 | printf ("ReadModeInfoFromDisk: failed to read the number of modes.\n"); |
| 732 | return 1; |
| 733 | } |
| 734 | |
| 735 | fclose(file); |
| 736 | return 0; |
| 737 | } |
| 738 | |
| 739 | template <class real> |
| 740 | int WriteModesToDisk(const char * filename, int n, int r, real * frequencies, real * modes) |
| 741 | { |
| 742 | FILE * file; |
| 743 | file = fopen(filename, "w"); |
| 744 | |
| 745 | if (!file) |
| 746 | return 1; |
| 747 | |
| 748 | if (fprintf(file,"%d\n", n) < 1) |
| 749 | return 1; |
| 750 | |
| 751 | if (fprintf(file,"%d\n", r) < 1) |
| 752 | return 1; |
| 753 | |
| 754 | // write frequencies |
| 755 | for (int i=0; i<r; i++) // over all modes |
| 756 | { |
| 757 | if (fprintf(file,"%.15f\n",frequencies[i]) < 1) |
| 758 | return 1; |
| 759 | } |
| 760 | |
| 761 | // write modes |
| 762 | for (int i=0; i<r; i++) // over all modes |
| 763 | { |
| 764 | fprintf(file,"------ Mode %d : ----\n",i+1); |
| 765 | |
| 766 | for (int j=0; j<n; j++) // over all nodes |
| 767 | { |
| 768 | if (fprintf(file,"%d %.15f %.15f %.15f\n", j+1, |
| 769 | modes[ELT(3*n,3*j,i)], |
| 770 | modes[ELT(3*n,3*j+1,i)], |
| 771 | modes[ELT(3*n,3*j+2,i)]) < 4) |
| 772 | return 1; |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | fclose(file); |
| 777 | |
| 778 | return 0; |
| 779 | } |
| 780 | |
| 781 | template void PrintMatrixInMathematicaFormat<double>(int n, int r, double * U); |
| 782 | template void PrintMatrixInMathematicaFormat<float>(int n, int r, float * U); |
| 783 | |
| 784 | template int ReadModesFromDisk<double>(const char * filename, int * n, int * r, double ** frequencies, double ** modes); |
| 785 | template int ReadModesFromDisk<float>(const char * filename, int * n, int * r, float ** frequencies, float ** modes); |
| 786 | |
| 787 | template int WriteModesToDisk<double>(const char * filename, int n, int r, double * frequencies, double * modes); |
| 788 | template int WriteModesToDisk<float>(const char * filename, int n, int r, float * frequencies, float * modes); |
| 789 | |
| 790 | template int WriteMatrixToStream<double>(FILE * file, int m, int n, const double * matrix); |
| 791 | template int WriteMatrixToStream<float>(FILE * file, int m, int n, const float * matrix); |
| 792 | |
| 793 | template int WriteMatrixToDisk<double>(const char * filename, int m, int n, const double * matrix); |
| 794 | template int WriteMatrixToDisk<float>(const char * filename, int m, int n, const float * matrix); |
| 795 | |
| 796 | template int WriteMatrixToDisk_<double>(const char * filename, int m, int n, const double * matrix); |
| 797 | template int WriteMatrixToDisk_<float>(const char * filename, int m, int n, const float * matrix); |
| 798 | |
| 799 | template int AppendMatrixToDisk_<double>(const char * filename, int m, int n, const double * matrix); |
| 800 | template int AppendMatrixToDisk_<float>(const char * filename, int m, int n, const float * matrix); |
| 801 | |
| 802 | template int ReadMatrixFromDiskListOfFiles<double>(const char * fileList, int * m, int * n, double ** matrix); |
| 803 | template int ReadMatrixFromDiskListOfFiles<float>(const char * fileList, int * m, int * n, float ** matrix); |
| 804 | |
| 805 | template int ReadMatrixFromStream<double>(FILE * file, int m, int n, double * matrix); |
| 806 | template int ReadMatrixFromStream<float>(FILE * file, int m, int n, float * matrix); |
| 807 | |
| 808 | template int ReadMatrixFromDisk<double>(const char * filename, int * m, int * n, double ** matrix); |
| 809 | template int ReadMatrixFromDisk<float>(const char * filename, int * m, int * n, float ** matrix); |
| 810 | |
| 811 | template int ReadMatrixFromDisk_<double>(const char * filename, int * m, int * n, double ** matrix); |
| 812 | template int ReadMatrixFromDisk_<float>(const char * filename, int * m, int * n, float ** matrix); |
| 813 | |
| 814 | template int ReadMatrixFromDiskTextFile<double>(const char * filename, int * m, int * n, double ** matrix); |
| 815 | template int ReadMatrixFromDiskTextFile<float>(const char * filename, int * m, int * n, float ** matrix); |
| 816 | |
| 817 | template int WriteMatrixToDiskTextFile<double>(const char * filename, int m, int n, const double * matrix); |
| 818 | template int WriteMatrixToDiskTextFile<float>(const char * filename, int m, int n, const float * matrix); |
| 819 | |
| 820 | template int ReadBinary(FILE * fin, bool * data); |
| 821 | template int ReadBinary(FILE * fin, int * data); |
| 822 | template int ReadBinary(FILE * fin, float * data); |
| 823 | template int ReadBinary(FILE * fin, double * data); |
| 824 | |
| 825 | template void ReadBinary_(FILE * fin, bool * data); |
| 826 | template void ReadBinary_(FILE * fin, int * data); |
| 827 | template void ReadBinary_(FILE * fin, float * data); |
| 828 | template void ReadBinary_(FILE * fin, double * data); |
| 829 | |
| 830 | template int ReadBinaryBuffer(FILE * fin, int size, bool * data); |
| 831 | template int ReadBinaryBuffer(FILE * fin, int size, int * data); |
| 832 | template int ReadBinaryBuffer(FILE * fin, int size, float * data); |
| 833 | template int ReadBinaryBuffer(FILE * fin, int size, double * data); |
| 834 | |
| 835 | template void ReadBinaryBuffer_(FILE * fin, int size, bool * data); |
| 836 | template void ReadBinaryBuffer_(FILE * fin, int size, int * data); |
| 837 | template void ReadBinaryBuffer_(FILE * fin, int size, float * data); |
| 838 | template void ReadBinaryBuffer_(FILE * fin, int size, double * data); |
| 839 |