/[escript]/trunk/finley/src/Mesh_read.c
ViewVC logotype

Diff of /trunk/finley/src/Mesh_read.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1887 by ksteube, Wed Oct 15 03:26:25 2008 UTC revision 2311 by jfenwick, Mon Mar 16 23:00:08 2009 UTC
# Line 21  Line 21 
21  #include <ctype.h>  #include <ctype.h>
22  #include "Mesh.h"  #include "Mesh.h"
23    
24  #define FSCANF_CHECK(scan_ret, reason) { if (scan_ret == EOF) perror(reason); return NULL; }  #define FSCANF_CHECK(scan_ret, reason) { if (scan_ret == EOF) { perror(reason); Finley_setError(IO_ERROR,"scan error while reading finley file"); return NULL;} }
25    
26  /**************************************************************/  /**************************************************************/
27    
# Line 37  Finley_Mesh* Finley_Mesh_read(char* fnam Line 37  Finley_Mesh* Finley_Mesh_read(char* fnam
37    Finley_Mesh *mesh_p=NULL;    Finley_Mesh *mesh_p=NULL;
38    char name[LenString_MAX],element_type[LenString_MAX],frm[20];    char name[LenString_MAX],element_type[LenString_MAX],frm[20];
39    char error_msg[LenErrorMsg_MAX];    char error_msg[LenErrorMsg_MAX];
40      #ifdef Finley_TRACE
41    double time0=Finley_timer();    double time0=Finley_timer();
42      #endif
43    FILE *fileHandle_p = NULL;    FILE *fileHandle_p = NULL;
44    ElementTypeId typeID, faceTypeID, contactTypeID, pointTypeID;    ElementTypeId typeID=NoType, faceTypeID=NoType, contactTypeID=NoType, pointTypeID=NoType;
45    int scan_ret;    int scan_ret;
46        
47    Finley_resetError();    Finley_resetError();
# Line 60  Finley_Mesh* Finley_Mesh_read(char* fnam Line 62  Finley_Mesh* Finley_Mesh_read(char* fnam
62       sprintf(frm,"%%%d[^\n]",LenString_MAX-1);       sprintf(frm,"%%%d[^\n]",LenString_MAX-1);
63       scan_ret = fscanf(fileHandle_p, frm, name);       scan_ret = fscanf(fileHandle_p, frm, name);
64       FSCANF_CHECK(scan_ret, "Finley_Mesh_read")       FSCANF_CHECK(scan_ret, "Finley_Mesh_read")
65    
66        
67       /* get the nodes */       /* get the nodes */
68        
# Line 267  Finley_Mesh* Finley_Mesh_read_MPI(char* Line 270  Finley_Mesh* Finley_Mesh_read_MPI(char*
270    
271  {  {
272    
273    Paso_MPIInfo *mpi_info = Paso_MPIInfo_alloc( MPI_COMM_WORLD );    Paso_MPIInfo *mpi_info = NULL;
274    dim_t numNodes, numDim, numEle, i0, i1;    dim_t numNodes, numDim, numEle, i0, i1;
275    Finley_Mesh *mesh_p=NULL;    Finley_Mesh *mesh_p=NULL;
276    char name[LenString_MAX],element_type[LenString_MAX],frm[20];    char name[LenString_MAX],element_type[LenString_MAX],frm[20];
277    char error_msg[LenErrorMsg_MAX];    char error_msg[LenErrorMsg_MAX];
278      #ifdef Finley_TRACE
279    double time0=Finley_timer();    double time0=Finley_timer();
280      #endif
281    FILE *fileHandle_p = NULL;    FILE *fileHandle_p = NULL;
282    ElementTypeId typeID, faceTypeID, contactTypeID, pointTypeID;    ElementTypeId typeID=NoType;
   Finley_TagMap* tag_map;  
   index_t tag_key;  
283    int scan_ret;    int scan_ret;
284    
285    Finley_resetError();    Finley_resetError();
286      mpi_info = Paso_MPIInfo_alloc( MPI_COMM_WORLD );
287    
288    if (mpi_info->rank == 0) {    if (mpi_info->rank == 0) {
289       /* get file handle */       /* get file handle */
# Line 323  Finley_Mesh* Finley_Mesh_read_MPI(char* Line 327  Finley_Mesh* Finley_Mesh_read_MPI(char*
327    }    }
328  #endif  #endif
329    
330    
331       /* allocate mesh */       /* allocate mesh */
332       mesh_p = Finley_Mesh_alloc(name,numDim,order,reduced_order,mpi_info);       mesh_p = Finley_Mesh_alloc(name,numDim,order,reduced_order,mpi_info);
333       if (Finley_noError()) {       if (Finley_noError()) {
# Line 854  Finley_Mesh* Finley_Mesh_read_MPI(char* Line 859  Finley_Mesh* Finley_Mesh_read_MPI(char*
859    
860        /* get the name tags */        /* get the name tags */
861        if (Finley_noError()) {        if (Finley_noError()) {
862          char *remainder, *ptr;          char *remainder=0, *ptr;
863          int tag_key, error_code;          size_t len=0;
864          size_t len;  #ifdef PASO_MPI
865          long cur_pos, end_pos;          int len_i;
866    #endif
867            int tag_key;
868          if (mpi_info->rank == 0) {  /* Master */          if (mpi_info->rank == 0) {  /* Master */
869        /* Read the word 'Tag' */        /* Read the word 'Tag' */
870        if (! feof(fileHandle_p)) {        if (! feof(fileHandle_p)) {
871          scan_ret = fscanf(fileHandle_p, "%s\n", name);          scan_ret = fscanf(fileHandle_p, "%s\n", name);
872          FSCANF_CHECK(scan_ret, "Finley_Mesh_read")          FSCANF_CHECK(scan_ret, "Finley_Mesh_read")
873        }        }
       /* Read rest of file in one chunk, after using seek to find length */  
874    
875  #if defined(_WIN32)  /* windows ftell lies on unix formatted text files */  #if defined(_WIN32)  /* windows ftell lies on unix formatted text files */
876    
# Line 872  Finley_Mesh* Finley_Mesh_read_MPI(char* Line 878  Finley_Mesh* Finley_Mesh_read_MPI(char*
878            len=0;            len=0;
879        while (1)        while (1)
880            {            {
881               size_t MALLOC_CHUNK = 1024;               size_t malloc_chunk = 1024;
882               size_t buff_size = 0;               size_t buff_size = 0;
883               int ch;               int ch;
884    
# Line 883  Finley_Mesh* Finley_Mesh_read_MPI(char* Line 889  Finley_Mesh* Finley_Mesh_read_MPI(char*
889               }               }
890               if( len+1 > buff_size )               if( len+1 > buff_size )
891               {               {
892                  TMPMEMREALLOC(remainder,remainder,buff_size+MALLOC_CHUNK,char);                  TMPMEMREALLOC(remainder,remainder,buff_size+malloc_chunk,char);
893               }               }
894               if( ch == EOF )               if( ch == EOF )
895               {               {
# Line 895  Finley_Mesh* Finley_Mesh_read_MPI(char* Line 901  Finley_Mesh* Finley_Mesh_read_MPI(char*
901               len++;               len++;
902            }            }
903  #else  #else
904            cur_pos = ftell(fileHandle_p);        /* Read rest of file in one chunk, after using seek to find length */
905            fseek(fileHandle_p, 0L, SEEK_END);            {
906            end_pos = ftell(fileHandle_p);               long cur_pos, end_pos;
907            fseek(fileHandle_p, (long)cur_pos, SEEK_SET);  
908        remainder = TMPMEMALLOC(end_pos-cur_pos+1, char);               cur_pos = ftell(fileHandle_p);
909        if (! feof(fileHandle_p)) {               fseek(fileHandle_p, 0L, SEEK_END);
910          scan_ret = fread(remainder, (size_t) end_pos-cur_pos, sizeof(char), fileHandle_p);               end_pos = ftell(fileHandle_p);
911              FSCANF_CHECK(scan_ret, "Finley_Mesh_read")               fseek(fileHandle_p, (long)cur_pos, SEEK_SET);
912        }               remainder = TMPMEMALLOC(end_pos-cur_pos+1, char);
913        remainder[end_pos-cur_pos] = 0;               if (! feof(fileHandle_p))
914                 {
915                    scan_ret = fread(remainder, (size_t) end_pos-cur_pos,
916                                     sizeof(char), fileHandle_p);
917    
918                    FSCANF_CHECK(scan_ret, "Finley_Mesh_read")
919                    remainder[end_pos-cur_pos] = 0;
920                }
921              }
922  #endif  #endif
923        len = strlen(remainder);        len = strlen(remainder);
924        while ((--len)>0 && isspace(remainder[len])) remainder[len]=0;  /*    while (((--len)>0) && isspace((int)(remainder[len]))) {remainder[len]=0;}  */
925          while ((len>1) && isspace(remainder[--len])) {remainder[len]=0;}
926        len = strlen(remainder);        len = strlen(remainder);
927            TMPMEMREALLOC(remainder,remainder,len+1,char);            TMPMEMREALLOC(remainder,remainder,len+1,char);
928          }          }
929  #ifdef PASO_MPI  #ifdef PASO_MPI
930          error_code = MPI_Bcast (&len, 1, MPI_INT,  0, mpi_info->comm);  
931          if (error_code != MPI_SUCCESS) {          len_i=(int) len;
932            Finley_setError(PASO_MPI_ERROR, "Finley_Mesh_read: broadcast of tag len failed");          if (MPI_Bcast (&len_i, 1, MPI_INT,  0, mpi_info->comm) != MPI_SUCCESS)
933            return NULL;          {
934               Finley_setError(PASO_MPI_ERROR,
935                               "Finley_Mesh_read: broadcast of tag len failed");
936               return NULL;
937          }          }
938            len=(size_t) len_i;
939      if (mpi_info->rank != 0) {      if (mpi_info->rank != 0) {
940        remainder = TMPMEMALLOC(len+1, char);        remainder = TMPMEMALLOC(len+1, char);
941        remainder[0] = 0;        remainder[0] = 0;
942      }      }
943          error_code = MPI_Bcast (remainder, len+1, MPI_CHAR,  0, mpi_info->comm);          if (MPI_Bcast (remainder, len+1, MPI_CHAR,  0, mpi_info->comm) !=
944          if (error_code != MPI_SUCCESS) {              MPI_SUCCESS)
945            Finley_setError(PASO_MPI_ERROR, "Finley_Mesh_read: broadcast of tags failed");          {
946            return NULL;             Finley_setError(PASO_MPI_ERROR,
947                               "Finley_Mesh_read: broadcast of tags failed");
948               return NULL;
949          }          }
950  #endif  #endif
951    
952      if (remainder[0]) {      if (remainder[0]) {
953            ptr = remainder;            ptr = remainder;
954            do {            do {

Legend:
Removed from v.1887  
changed lines
  Added in v.2311

  ViewVC Help
Powered by ViewVC 1.1.26