This example demonstrates how to read partition files created by Zoltan processes and apply them to MOAB meshes. It shows how to:
- Load a mesh file and a partition file
- Remove existing partition sets from the mesh
- Create new partition sets based on the partition file
- Assign entities to appropriate partition sets
- Handle parallel partitioning with global IDs
- Write the partitioned mesh to a new file
The partition file contains entity-to-partition assignments where entities are identified by their global IDs. This is useful for load balancing and parallel mesh processing workflows.
Usage: ./ReadPartFile <input_mesh> <partition_file> <num_parts> <output_file>
Example: ./ReadPartFile mesh.h5m partition.txt 4 partitioned_mesh.h5m
The partition file should contain one integer per entity (in order) indicating which partition (0 to num_parts-1) each entity belongs to.
#include <iostream>
#include <fstream>
#include <memory>
using namespace std;
#ifndef MESH_DIR
#define MESH_DIR "."
#endif
int main(
int argc,
char** argv )
{
if( argc < 5 )
{
std::cerr << "Usage: " << argv[0] << " <input file> <part file> <#parts> <output file>\n";
return 1;
}
std::string mesh_file = argv[1];
std::string part_file = argv[2];
int nparts = std::stoi( argv[3] );
auto mb = std::make_unique< Core >();
std::ifstream inFile( part_file );
MB_CHK_SET_ERR(
mb->load_mesh( mesh_file.c_str() ),
"Error: Could not load mesh file '" + mesh_file +
"'" );
Range sets;
std::cout << "Number of sets is " << sets.size() << std::endl;
MB_CHK_SET_ERR(
mb->tag_get_handle(
"PARALLEL_PARTITION", tag ),
"Error: Could not get PARALLEL_PARTITION tag." );
int num_deleted_sets = 0;
for( auto it = sets.begin(); it != sets.end(); ++it )
{
int val = -1;
MB_CHK_SET_ERR(
mb->tag_get_data( tag, &eh, 1, &val ),
"Error: Unable to get tag data" );
if( val != -1 )
{
num_deleted_sets++;
MB_CHK_SET_ERR(
mb->delete_entities( &eh, 1 ),
"Error: Unable to delete entities" );
}
}
if( num_deleted_sets )
std::cout << "Deleted " << num_deleted_sets << " existing partition sets, and created new ones.\n";
Range cells;
MB_CHK_SET_ERR(
mb->get_entities_by_dimension( 0, 2, cells ),
"Error: Could not get dimension-2 entities." );
std::vector< EntityHandle > psets(
nparts );
for(
int i = 0; i <
nparts; i++ )
{
MB_CHK_SET_ERR(
mb->tag_set_data( tag, &( psets[i] ), 1, &i ),
"Error: Could not set tag data." );
}
for( auto it = cells.begin(); it != cells.end(); ++it )
{
int part;
inFile >> part;
MB_CHK_SET_ERR(
mb->add_entities( psets[part], &eh, 1 ),
"Error: Could not add entity to partition set." );
}
std::cout <<
"Partitioned mesh written to '" <<
out_file <<
"'.\n";
return 0;
}