/[escript]/branches/more_shared_ptrs_from_1812/finley/src/CPPAdapter/MeshAdapterFactory.cpp
ViewVC logotype

Diff of /branches/more_shared_ptrs_from_1812/finley/src/CPPAdapter/MeshAdapterFactory.cpp

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

revision 1346 by ksteube, Wed Nov 14 22:48:12 2007 UTC revision 1347 by ksteube, Fri Nov 16 05:37:07 2007 UTC
# Line 52  namespace finley { Line 52  namespace finley {
52    AbstractContinuousDomain* loadMesh(const std::string& fileName)    AbstractContinuousDomain* loadMesh(const std::string& fileName)
53    {    {
54  #ifdef USE_NETCDF  #ifdef USE_NETCDF
55      bool optimize=FALSE; // ksteube should this be an argument to LoadMesh?      bool optimize=FALSE; // Don't optimize since this would cause problems with Data().dump()
56      Paso_MPIInfo *mpi_info = Paso_MPIInfo_alloc( MPI_COMM_WORLD );      Paso_MPIInfo *mpi_info = Paso_MPIInfo_alloc( MPI_COMM_WORLD );
57      AbstractContinuousDomain* temp;      AbstractContinuousDomain* temp;
58      Finley_Mesh *mesh_p=NULL;      Finley_Mesh *mesh_p=NULL;
59      char error_msg[LenErrorMsg_MAX];      char error_msg[LenErrorMsg_MAX];
60      // create a copy of the filename to overcome the non-constness of call      char *fName = Paso_MPI_appendRankToFileName(strdup(fileName.c_str()), mpi_info->size, mpi_info->rank);
     // to Finley_Mesh_read  
     // Win32 refactor  
     char *fName = ((fileName.size()+1)>0) ? TMPMEMALLOC((fileName.size()+1),char) : (char*)NULL;  
     strcpy(fName,fileName.c_str());  
   
     printf("ksteube finley::loadMesh %s\n", fName);  
61    
62      double blocktimer_start = blocktimer_time();      double blocktimer_start = blocktimer_time();
63      Finley_resetError();      Finley_resetError();
# Line 79  namespace finley { Line 73  namespace finley {
73        sprintf(error_msg,"loadMesh: Opening file NetCDF %s for reading failed.", fName);        sprintf(error_msg,"loadMesh: Opening file NetCDF %s for reading failed.", fName);
74        Finley_setError(IO_ERROR,error_msg);        Finley_setError(IO_ERROR,error_msg);
75        Paso_MPIInfo_free( mpi_info );        Paso_MPIInfo_free( mpi_info );
76        throw DataException("Error - loadMesh:: Could not read NetCDF file.");        throw DataException(error_msg);
77      }      }
78    
79      // Read NetCDF integer attributes      // Read NetCDF integer attributes
80      int mpi_size        = NetCDF_Get_Int_Attribute(&dataFile, fName, "mpi_size");      int mpi_size            = NetCDF_Get_Int_Attribute(&dataFile, fName, "mpi_size");
81      int mpi_rank        = NetCDF_Get_Int_Attribute(&dataFile, fName, "mpi_rank");      int mpi_rank            = NetCDF_Get_Int_Attribute(&dataFile, fName, "mpi_rank");
82      int numDim          = NetCDF_Get_Int_Attribute(&dataFile, fName, "numDim");      int numDim              = NetCDF_Get_Int_Attribute(&dataFile, fName, "numDim");
83      int order           = NetCDF_Get_Int_Attribute(&dataFile, fName, "order");      int order               = NetCDF_Get_Int_Attribute(&dataFile, fName, "order");
84      int reduced_order       = NetCDF_Get_Int_Attribute(&dataFile, fName, "reduced_order");      int reduced_order           = NetCDF_Get_Int_Attribute(&dataFile, fName, "reduced_order");
85      int numNodes        = NetCDF_Get_Int_Attribute(&dataFile, fName, "numNodes");      int numNodes            = NetCDF_Get_Int_Attribute(&dataFile, fName, "numNodes");
86      int num_Elements        = NetCDF_Get_Int_Attribute(&dataFile, fName, "num_Elements");      int num_Elements            = NetCDF_Get_Int_Attribute(&dataFile, fName, "num_Elements");
87      int num_FaceElements    = NetCDF_Get_Int_Attribute(&dataFile, fName, "num_FaceElements");      int num_FaceElements        = NetCDF_Get_Int_Attribute(&dataFile, fName, "num_FaceElements");
88      int num_ContactElements = NetCDF_Get_Int_Attribute(&dataFile, fName, "num_ContactElements");      int num_ContactElements     = NetCDF_Get_Int_Attribute(&dataFile, fName, "num_ContactElements");
89      int num_Points      = NetCDF_Get_Int_Attribute(&dataFile, fName, "num_Points");      int num_Points          = NetCDF_Get_Int_Attribute(&dataFile, fName, "num_Points");
90        int num_Elements_numNodes       = NetCDF_Get_Int_Attribute(&dataFile, fName, "num_Elements_numNodes");
91        int Elements_TypeId         = NetCDF_Get_Int_Attribute(&dataFile, fName, "Elements_TypeId");
92        int num_FaceElements_numNodes   = NetCDF_Get_Int_Attribute(&dataFile, fName, "num_FaceElements_numNodes");
93        int FaceElements_TypeId     = NetCDF_Get_Int_Attribute(&dataFile, fName, "FaceElements_TypeId");
94        int num_ContactElements_numNodes    = NetCDF_Get_Int_Attribute(&dataFile, fName, "num_ContactElements_numNodes");
95        int ContactElements_TypeId      = NetCDF_Get_Int_Attribute(&dataFile, fName, "ContactElements_TypeId");
96        int Points_TypeId           = NetCDF_Get_Int_Attribute(&dataFile, fName, "Points_TypeId");
97        int num_Tags            = NetCDF_Get_Int_Attribute(&dataFile, fName, "num_Tags");
98    
99        // Verify size and rank
100        if (mpi_info->size != mpi_size) {
101          sprintf(error_msg, "Error loadMesh - The NetCDF file '%s' can only be read on %d CPUs instead of %d", fName, mpi_size, mpi_info->size);
102          throw DataException(error_msg);
103        }
104        if (mpi_info->rank != mpi_rank) {
105          sprintf(error_msg, "Error loadMesh - The NetCDF file '%s' should be read on CPU #%d instead of %d", fName, mpi_rank, mpi_info->rank);
106          throw DataException(error_msg);
107        }
108    
109      // Read mesh name      // Read mesh name
110      if (! (attr=dataFile.get_att("Name")) ) {      if (! (attr=dataFile.get_att("Name")) ) {
# Line 159  namespace finley { Line 171  namespace finley {
171            free(&mesh_p->Nodes->Coordinates);            free(&mesh_p->Nodes->Coordinates);
172            throw DataException("Error - load:: unable to recover Nodes_Coordinates from netCDF file: " + *fName);            throw DataException("Error - load:: unable to recover Nodes_Coordinates from netCDF file: " + *fName);
173          }          }
174        // Nodes_DofDistribution
175        int *first_component = TMPMEMALLOC(mpi_size+1,index_t);
176            if (! ( nc_var_temp = dataFile.get_var("Nodes_DofDistribution")) )
177              throw DataException("Error - loadMesh:: unable to read Nodes_DofDistribution from netCDF file: " + *fName);
178            if (! nc_var_temp->get(&first_component[0], mpi_size+1) ) {
179              free(&first_component);
180              throw DataException("Error - loadMesh:: unable to recover Nodes_DofDistribution from NetCDF file: " + *fName);
181            }
182        mesh_p->Nodes->degreesOfFreedomDistribution=Paso_Distribution_alloc(mesh_p->Nodes->MPIInfo,first_component,1,0);
183        TMPMEMFREE(first_component);
184    
185          /* read elements */          /* read elements */
186          if (Finley_noError()) {          if (Finley_noError()) {
187              mesh_p->Elements=Finley_ElementFile_alloc((ElementTypeId)Elements_TypeId,mesh_p->order, mesh_p->reduced_order, mpi_info);
188              if (Finley_noError()) Finley_ElementFile_allocTable(mesh_p->Elements, num_Elements);
189              mesh_p->Elements->minColor=0;
190              mesh_p->Elements->maxColor=num_Elements-1;
191            if (num_Elements>0) {            if (num_Elements>0) {
          int num_Elements_numNodes = NetCDF_Get_Int_Attribute(&dataFile, fName, "num_Elements_numNodes");  
          int Elements_TypeId = NetCDF_Get_Int_Attribute(&dataFile, fName, "Elements_TypeId");  
              mesh_p->Elements=Finley_ElementFile_alloc((ElementTypeId)Elements_TypeId,mesh_p->order, mesh_p->reduced_order, mpi_info);  
              if (Finley_noError()) {  
                  Finley_ElementFile_allocTable(mesh_p->Elements, num_Elements);  
                  mesh_p->Elements->minColor=0;  
                  mesh_p->Elements->maxColor=num_Elements-1;  
192                   if (Finley_noError()) {                   if (Finley_noError()) {
193                 // Elements_Id                 // Elements_Id
194                     if (! ( nc_var_temp = dataFile.get_var("Elements_Id")) )                     if (! ( nc_var_temp = dataFile.get_var("Elements_Id")) )
# Line 218  namespace finley { Line 237  namespace finley {
237             }             }
238             TMPMEMFREE(Elements_Nodes);             TMPMEMFREE(Elements_Nodes);
239           }           }
          }  
240        } /* num_Elements>0 */        } /* num_Elements>0 */
241      }      }
242    
243          /* get the face elements */          /* get the face elements */
244          if (Finley_noError()) {          if (Finley_noError()) {
245              mesh_p->FaceElements=Finley_ElementFile_alloc((ElementTypeId)FaceElements_TypeId,mesh_p->order, mesh_p->reduced_order, mpi_info);
246              if (Finley_noError()) Finley_ElementFile_allocTable(mesh_p->FaceElements, num_FaceElements);
247              mesh_p->FaceElements->minColor=0;
248              mesh_p->FaceElements->maxColor=num_FaceElements-1;
249            if (num_FaceElements>0) {            if (num_FaceElements>0) {
          int num_FaceElements_numNodes = NetCDF_Get_Int_Attribute(&dataFile, fName, "num_FaceElements_numNodes");  
          int FaceElements_TypeId = NetCDF_Get_Int_Attribute(&dataFile, fName, "FaceElements_TypeId");  
              mesh_p->FaceElements=Finley_ElementFile_alloc((ElementTypeId)FaceElements_TypeId,mesh_p->order, mesh_p->reduced_order, mpi_info);  
              if (Finley_noError()) {  
                  Finley_ElementFile_allocTable(mesh_p->FaceElements, num_FaceElements);  
                  mesh_p->FaceElements->minColor=0;  
                  mesh_p->FaceElements->maxColor=num_FaceElements-1;  
250                   if (Finley_noError()) {                   if (Finley_noError()) {
251                 // FaceElements_Id                 // FaceElements_Id
252                     if (! ( nc_var_temp = dataFile.get_var("FaceElements_Id")) )                     if (! ( nc_var_temp = dataFile.get_var("FaceElements_Id")) )
# Line 280  namespace finley { Line 295  namespace finley {
295             }             }
296             TMPMEMFREE(FaceElements_Nodes);             TMPMEMFREE(FaceElements_Nodes);
297           }           }
          }  
298        } /* num_FaceElements>0 */        } /* num_FaceElements>0 */
299      }      }
300    
301          /* get the Contact elements */          /* get the Contact elements */
302          if (Finley_noError()) {          if (Finley_noError()) {
303              mesh_p->ContactElements=Finley_ElementFile_alloc((ElementTypeId)ContactElements_TypeId,mesh_p->order, mesh_p->reduced_order, mpi_info);
304              if (Finley_noError()) Finley_ElementFile_allocTable(mesh_p->ContactElements, num_ContactElements);
305              mesh_p->ContactElements->minColor=0;
306              mesh_p->ContactElements->maxColor=num_ContactElements-1;
307            if (num_ContactElements>0) {            if (num_ContactElements>0) {
          int num_ContactElements_numNodes = NetCDF_Get_Int_Attribute(&dataFile, fName, "num_ContactElements_numNodes");  
          int ContactElements_TypeId = NetCDF_Get_Int_Attribute(&dataFile, fName, "ContactElements_TypeId");  
              mesh_p->ContactElements=Finley_ElementFile_alloc((ElementTypeId)ContactElements_TypeId,mesh_p->order, mesh_p->reduced_order, mpi_info);  
              if (Finley_noError()) {  
                  Finley_ElementFile_allocTable(mesh_p->ContactElements, num_ContactElements);  
                  mesh_p->ContactElements->minColor=0;  
                  mesh_p->ContactElements->maxColor=num_ContactElements-1;  
308                   if (Finley_noError()) {                   if (Finley_noError()) {
309                 // ContactElements_Id                 // ContactElements_Id
310                     if (! ( nc_var_temp = dataFile.get_var("ContactElements_Id")) )                     if (! ( nc_var_temp = dataFile.get_var("ContactElements_Id")) )
# Line 342  namespace finley { Line 353  namespace finley {
353             }             }
354             TMPMEMFREE(ContactElements_Nodes);             TMPMEMFREE(ContactElements_Nodes);
355           }           }
          }  
356        } /* num_ContactElements>0 */        } /* num_ContactElements>0 */
357      }      }
358    
359          /* get the Points (nodal elements) */          /* get the Points (nodal elements) */
360          if (Finley_noError()) {          if (Finley_noError()) {
361              mesh_p->Points=Finley_ElementFile_alloc((ElementTypeId)Points_TypeId,mesh_p->order, mesh_p->reduced_order, mpi_info);
362              if (Finley_noError()) Finley_ElementFile_allocTable(mesh_p->Points, num_Points);
363              mesh_p->Points->minColor=0;
364              mesh_p->Points->maxColor=num_Points-1;
365            if (num_Points>0) {            if (num_Points>0) {
          int Points_TypeId = NetCDF_Get_Int_Attribute(&dataFile, fName, "Points_TypeId");  
              mesh_p->Points=Finley_ElementFile_alloc((ElementTypeId)Points_TypeId,mesh_p->order, mesh_p->reduced_order, mpi_info);  
366               if (Finley_noError()) {               if (Finley_noError()) {
                  Finley_ElementFile_allocTable(mesh_p->Points, num_Points);  
                  mesh_p->Points->minColor=0;  
                  mesh_p->Points->maxColor=num_Points-1;  
                  if (Finley_noError()) {  
367                 // Points_Id                 // Points_Id
368                     if (! ( nc_var_temp = dataFile.get_var("Points_Id")) )                     if (! ( nc_var_temp = dataFile.get_var("Points_Id")) )
369                       throw DataException("Error - loadMesh:: unable to read Points_Id from netCDF file: " + *fName);                       throw DataException("Error - loadMesh:: unable to read Points_Id from netCDF file: " + *fName);
# Line 399  namespace finley { Line 407  namespace finley {
407               mesh_p->Nodes->Id[mesh_p->Points->Nodes[INDEX2(0,i,1)]] = Points_Nodes[i];               mesh_p->Nodes->Id[mesh_p->Points->Nodes[INDEX2(0,i,1)]] = Points_Nodes[i];
408             }             }
409             TMPMEMFREE(Points_Nodes);             TMPMEMFREE(Points_Nodes);
          }  
410           }           }
411        } /* num_Points>0 */        } /* num_Points>0 */
412      }      }
413    
414          /* get the name tags */          /* get the tags */
415            if (Finley_noError()) {
416              if (num_Tags>0) {
417                // Temp storage to gather node IDs
418                int *Tags_keys = TMPMEMALLOC(num_Tags, int);
419                char name_temp[4096];
420            int i;
421    
422            // Tags_keys
423                if (! ( nc_var_temp = dataFile.get_var("Tags_keys")) )
424                  throw DataException("Error - loadMesh:: unable to read Tags_keys from netCDF file: " + *fName);
425                if (! nc_var_temp->get(&Tags_keys[0], num_Tags) ) {
426                  free(&Tags_keys);
427                  throw DataException("Error - loadMesh:: unable to recover Tags_keys from NetCDF file: " + *fName);
428                }
429            for (i=0; i<num_Tags; i++) {
430                  // Retrieve tag name
431                  sprintf(name_temp, "Tags_name_%d", i);
432                  if (! (attr=dataFile.get_att(name_temp)) ) {
433                    sprintf(error_msg,"Error retrieving tag name from NetCDF file '%s'", fName);
434                    throw DataException(error_msg);
435                  }
436                  char *name = attr->as_string(0);
437                  delete attr;
438                  Finley_Mesh_addTagMap(mesh_p, name, Tags_keys[i]);
439            }
440          }
441        }
442    
443      } /* Finley_noError() after Finley_Mesh_alloc() */      } /* Finley_noError() after Finley_Mesh_alloc() */
444        
445      if (Finley_noError()) Finley_Mesh_resolveNodeIds(mesh_p);      if (Finley_noError()) Finley_Mesh_createMappings(mesh_p, mesh_p->Nodes->degreesOfFreedomDistribution->first_component);
     if (Finley_noError()) Finley_Mesh_prepare(mesh_p, optimize);  
446    
447      checkFinleyError();      checkFinleyError();
448      temp=new MeshAdapter(mesh_p);      temp=new MeshAdapter(mesh_p);

Legend:
Removed from v.1346  
changed lines
  Added in v.1347

  ViewVC Help
Powered by ViewVC 1.1.26