Mesh Oriented datABase  (version 5.6.0)
An array-based unstructured mesh library
ReadPartFile.cpp
Go to the documentation of this file.
1 /** @example ReadPartFile.cpp
2  * This example demonstrates how to read partition files created by Zoltan processes
3  * and apply them to MOAB meshes. It shows how to:
4  * - Load a mesh file and a partition file
5  * - Remove existing partition sets from the mesh
6  * - Create new partition sets based on the partition file
7  * - Assign entities to appropriate partition sets
8  * - Handle parallel partitioning with global IDs
9  * - Write the partitioned mesh to a new file
10  *
11  * The partition file contains entity-to-partition assignments where entities
12  * are identified by their global IDs. This is useful for load balancing
13  * and parallel mesh processing workflows.
14  *
15  * Usage: ./ReadPartFile <input_mesh> <partition_file> <num_parts> <output_file>
16  *
17  * Example:
18  * ./ReadPartFile mesh.h5m partition.txt 4 partitioned_mesh.h5m
19  *
20  * The partition file should contain one integer per entity (in order)
21  * indicating which partition (0 to num_parts-1) each entity belongs to.
22  */
23 
24 #include "moab/Core.hpp"
25 #include <iostream>
26 #include <fstream>
27 #include <memory>
28 
29 using namespace moab;
30 using namespace std;
31 
32 #ifndef MESH_DIR
33 #define MESH_DIR "."
34 #endif
35 
36 // Note: change the file name below to test a trivial "No such file or directory" error
37 string test_file_name = string( MESH_DIR ) + string( "/3k-tri-sphere.vtk" );
39 int nparts;
40 
41 int main( int argc, char** argv )
42 {
43  if( argc < 5 )
44  {
45  std::cerr << "Usage: " << argv[0] << " <input file> <part file> <#parts> <output file>\n";
46  return 1;
47  }
48  std::string mesh_file = argv[1];
49  std::string part_file = argv[2];
50  int nparts = std::stoi( argv[3] );
51  std::string out_file = argv[4];
52 
53  auto mb = std::make_unique< Core >();
54  MB_CHK_SET_ERR( mb ? MB_SUCCESS : MB_FAILURE, "Error: Could not allocate MOAB Core instance." );
55 
56  std::ifstream inFile( part_file );
57  MB_CHK_SET_ERR( inFile.is_open() ? MB_SUCCESS : MB_FAILURE, "Unable to open file " + part_file );
58 
59  MB_CHK_SET_ERR( mb->load_mesh( mesh_file.c_str() ), "Error: Could not load mesh file '" + mesh_file + "'" );
60 
61  Range sets;
62  MB_CHK_SET_ERR( mb->get_entities_by_type( 0, MBENTITYSET, sets ), "Error: Could not get entity sets." );
63  std::cout << "Number of sets is " << sets.size() << std::endl;
64 
65  Tag tag;
66  MB_CHK_SET_ERR( mb->tag_get_handle( "PARALLEL_PARTITION", tag ), "Error: Could not get PARALLEL_PARTITION tag." );
67 
68  int num_deleted_sets = 0;
69  for( auto it = sets.begin(); it != sets.end(); ++it )
70  {
71  EntityHandle eh = *it;
72  int val = -1;
73  MB_CHK_SET_ERR( mb->tag_get_data( tag, &eh, 1, &val ), "Error: Unable to get tag data" );
74  if( val != -1 )
75  {
76  num_deleted_sets++;
77  MB_CHK_SET_ERR( mb->delete_entities( &eh, 1 ), "Error: Unable to delete entities" );
78  }
79  }
80 
81  if( num_deleted_sets )
82  std::cout << "Deleted " << num_deleted_sets << " existing partition sets, and created new ones.\n";
83 
84  Range cells;
85  MB_CHK_SET_ERR( mb->get_entities_by_dimension( 0, 2, cells ), "Error: Could not get dimension-2 entities." );
86  std::vector< EntityHandle > psets( nparts );
87  for( int i = 0; i < nparts; i++ )
88  {
89  MB_CHK_SET_ERR( mb->create_meshset( MESHSET_SET, psets[i] ), "Error: Could not create meshset." );
90  MB_CHK_SET_ERR( mb->tag_set_data( tag, &( psets[i] ), 1, &i ), "Error: Could not set tag data." );
91  }
92 
93  for( auto it = cells.begin(); it != cells.end(); ++it )
94  {
95  int part;
96  EntityHandle eh = *it;
97  inFile >> part;
98  MB_CHK_SET_ERR( mb->add_entities( psets[part], &eh, 1 ), "Error: Could not add entity to partition set." );
99  }
100 
101  MB_CHK_SET_ERR( mb->write_file( out_file.c_str() ), "Error: Could not write output mesh file '" + out_file + "'" );
102  std::cout << "Partitioned mesh written to '" << out_file << "'.\n";
103 
104  return 0;
105 }