Mesh Oriented datABase  (version 5.6.0)
An array-based unstructured mesh library
ReadPartFile.cpp

This example demonstrates how to read partition files created by Zoltan processes and apply them to MOAB meshes. It shows how to:

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.

/** @example ReadPartFile.cpp
* 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 "moab/Core.hpp"
#include <iostream>
#include <fstream>
#include <memory>
using namespace moab;
using namespace std;
#ifndef MESH_DIR
#define MESH_DIR "."
#endif
// Note: change the file name below to test a trivial "No such file or directory" error
string test_file_name = string( MESH_DIR ) + string( "/3k-tri-sphere.vtk" );
int nparts;
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] );
std::string out_file = argv[4];
auto mb = std::make_unique< Core >();
MB_CHK_SET_ERR( mb ? MB_SUCCESS : MB_FAILURE, "Error: Could not allocate MOAB Core instance." );
std::ifstream inFile( part_file );
MB_CHK_SET_ERR( inFile.is_open() ? MB_SUCCESS : MB_FAILURE, "Unable to open file " + part_file );
MB_CHK_SET_ERR( mb->load_mesh( mesh_file.c_str() ), "Error: Could not load mesh file '" + mesh_file + "'" );
Range sets;
MB_CHK_SET_ERR( mb->get_entities_by_type( 0, MBENTITYSET, sets ), "Error: Could not get entity sets." );
std::cout << "Number of sets is " << sets.size() << std::endl;
Tag tag;
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 )
{
EntityHandle eh = *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->create_meshset( MESHSET_SET, psets[i] ), "Error: Could not create meshset." );
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;
EntityHandle eh = *it;
inFile >> part;
MB_CHK_SET_ERR( mb->add_entities( psets[part], &eh, 1 ), "Error: Could not add entity to partition set." );
}
MB_CHK_SET_ERR( mb->write_file( out_file.c_str() ), "Error: Could not write output mesh file '" + out_file + "'" );
std::cout << "Partitioned mesh written to '" << out_file << "'.\n";
return 0;
}