MOAB_iMeshP_unit_tests.cpp 102 KB
Newer Older
1
#include "iMeshP.h"
's avatar
committed
2
#include "moab_mpi.h"
3 4 5 6 7 8 9
#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <assert.h>
#include <math.h>
#include <map>
10
#include <string.h>
11
#include <stdio.h> // remove()
12

's avatar
committed
13
#if !defined(_MSC_VER) && !defined(__MINGW32__)
14 15 16
#include <unistd.h>
#endif

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
#define STRINGIFY_(X) #X
#define STRINGIFY(X) STRINGIFY_(X)
const char* const FILENAME = "iMeshP_test_file";


/**************************************************************************
                              Error Checking
 **************************************************************************/

#define CHKERR do { \
  if (ierr) { \
    std::cerr << "Error code  " << ierr << " at " << __FILE__ << ":" << __LINE__ << std::endl;\
    return ierr; \
  } \
} while (false) 

#define PCHECK do { ierr = is_any_proc_error(ierr); CHKERR; } while(false)

35
// Use my_rank instead of rank to avoid shadowed declaration
36 37
#define ASSERT(A) do { \
  if (is_any_proc_error(!(A))) { \
38 39 40
    int my_rank = 0; \
    MPI_Comm_rank( MPI_COMM_WORLD, &my_rank ); \
    if (0 == my_rank) std::cerr << "Failed assertion: " #A << std::endl \
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
                         << "  at " __FILE__ ":" << __LINE__ << std::endl; \
    return 1; \
  } } while (false)
              
// Test if is_my_error is non-zero on any processor in MPI_COMM_WORLD
int is_any_proc_error( int is_my_error )
{
  int result;
  int err = MPI_Allreduce( &is_my_error, &result, 1, MPI_INT, MPI_MAX, MPI_COMM_WORLD );
  return err || result;
}

/**************************************************************************
                           Test  Declarations
 **************************************************************************/

class PartMap;

/**\brief Consistency check for parallel load
 *
 * All other tests depend on this one.
 */
int test_load( iMesh_Instance, iMeshP_PartitionHandle prtn, PartMap& map, int comm_size );


/**\brief Test partition query methods
 *
 * Test:
 * - iMeshP_getPartitionComm
 * - iMeshP_getNumPartitions
 * - iMeshP_getPartitions
 */
int test_get_partitions( iMesh_Instance, iMeshP_PartitionHandle prtn, const PartMap& );

/**\brief Test part quyery methods
 *
 * Test:
 * - iMeshP_getNumGlobalParts
 * - iMeshP_getNumLocalParts
 * - iMeshP_getLocalParts
 */
int test_get_parts( iMesh_Instance, iMeshP_PartitionHandle prtn, const PartMap& );

/**\brief Test query by entity type
 *
 * Test:
 * - iMeshP_getNumOfTypeAll
 * - iMeshP_getNumOfType
 * - iMeshP_getEntities
 * - 
 */
int test_get_by_type( iMesh_Instance, iMeshP_PartitionHandle prtn, const PartMap& );

/**\brief Test query by entity topology
 *
 * Test:
 * - iMeshP_getNumOfTopoAll
 * - iMeshP_getNumOfTopo
 * - iMeshP_getEntities
 * - 
 */
int test_get_by_topo( iMesh_Instance, iMeshP_PartitionHandle prtn, const PartMap& );

/**\brief Test mapping from part id to part handle
 * 
 * Test:
 * - iMeshP_getPartIdFromPartHandle
 * - iMeshP_getPartIdsFromPartHandlesArr
 * - iMeshP_getPartHandleFromPartId
 * - iMeshP_getPartHandlesFromPartsIdsArr
 * - iMeshP_getRankOfPart
 * - iMeshP_getRankOfPartArr
 */
int test_part_id_handle( iMesh_Instance, iMeshP_PartitionHandle prtn, const PartMap& );

/**\brief Test get part rank
 *
 * Tests:
 * - iMeshP_getRankOfPart
 * - iMeshP_getRankOfPartArr
 */
int test_part_rank( iMesh_Instance, iMeshP_PartitionHandle prtn, const PartMap& );

/**\brief Test querying of part neighbors
 *
 * Test:
 * - iMeshP_getNumPartNbors
 * - iMeshP_getNumPartNborsArr
 * - iMeshP_getPartNbors
 * - iMeshP_getPartNborsArr
 */
int test_get_neighbors( iMesh_Instance, iMeshP_PartitionHandle prtn, const PartMap& );

/**\brief Test querying of part boundary entities
 *
 * Test:
 * - iMeshP_getNumPartBdryEnts
 * - iMeshP_getPartBdryEnts
 */
int test_get_part_boundary( iMesh_Instance, iMeshP_PartitionHandle prtn, const PartMap& );

/**\brief Test querying of part boundary entities
 *
 * Test:
 * - iMeshP_initPartBdryEntIter
 * - iMeshP_initPartBdryEntArrIter
 */
int test_part_boundary_iter( iMesh_Instance, iMeshP_PartitionHandle prtn, const PartMap& );

/**\brief Test adjacent entity query
 *
 * Test:
 * - iMeshP_getAdjEntities
 */
int test_get_adjacencies( iMesh_Instance, iMeshP_PartitionHandle prtn, const PartMap& );

/**\brief Test entity iterators
 *
 * Test:
 * - iMeshP_initEntIter
 * - iMeshP_initEntArrIter
 */
int test_entity_iterator( iMesh_Instance, iMeshP_PartitionHandle prtn, const PartMap& );

/**\brief Test entity owner queries
 *
 * Test:
 * - iMeshP_getEntOwnerPart
 * - iMeshP_getEntOwnerPartArr
 * - iMeshP_isEntOwner
 * - iMeshP_isEntOwnerArr
 */
int test_entity_owner( iMesh_Instance, iMeshP_PartitionHandle prtn, const PartMap& );

/**\brief Test entity status
 *
 * Test:
 * - iMeshP_getEntStatus
 * - iMeshP_getEntStatusArr
 */
int test_entity_status( iMesh_Instance, iMeshP_PartitionHandle prtn, const PartMap& );

/**\brief Test information about entity copies for interface entities
 *
 * Test:
 * - iMeshP_getNumCopies
 * - iMeshP_getCopyParts
's avatar
committed
188 189 190 191 192 193
 */
int test_entity_copy_parts( iMesh_Instance, iMeshP_PartitionHandle prtn, const PartMap& );

/**\brief Test information about entity copies for interface entities
 *
 * Test:
194 195 196 197 198 199 200 201 202
 * - iMeshP_getCopies
 * - iMeshP_getCopyOnPart
 * - iMeshP_getOwnerCopy
 */
int test_entity_copies( iMesh_Instance, iMeshP_PartitionHandle prtn, const PartMap& );

/**\brief Test creation of ghost entities
 *
 * Test:
203
 * - iMeshP_createGhostEntsAll
204 205 206 207 208 209 210 211 212
 */
int test_create_ghost_ents( iMesh_Instance, iMeshP_PartitionHandle prtn, const PartMap& );

/**\brief Test commuinication of tag data
 *
 * Test:
 * - iMeshP_pushTags
 * - iMeshP_pushTagsEnt
 */
213 214
int test_push_tag_data_iface( iMesh_Instance, iMeshP_PartitionHandle prtn, const PartMap& );
int test_push_tag_data_ghost( iMesh_Instance, iMeshP_PartitionHandle prtn, const PartMap& );
215

216
int test_exchange_ents( iMesh_Instance imesh, iMeshP_PartitionHandle prtn, const PartMap& map );
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273

/**************************************************************************
                              Helper Funcions
 **************************************************************************/
 
class PartMap
{
public:
  int num_parts() const 
    { return sortedPartList.size(); }

  iMeshP_Part part_id_from_local_id( int local_id ) const
    { return sortedPartList[idx_from_local_id(local_id)]; }
    
  int local_id_from_part_id( iMeshP_Part part ) const
    { return partLocalIds[idx_from_part_id(part)]; }
  
  int rank_from_part_id( iMeshP_Part part ) const
    { return partRanks[idx_from_part_id(part)]; }
  
  int rank_from_local_id( int id ) const
    { return partRanks[idx_from_local_id(id)]; }
  
  int count_from_rank( int rank ) const
    { return std::count( partRanks.begin(), partRanks.end(), rank ); }
    
  void part_id_from_rank( int rank, std::vector<iMeshP_Part>& parts ) const;
  
  void local_id_from_rank( int rank, std::vector<int>& ids ) const;
  
  const std::vector<iMeshP_Part>& get_parts() const 
    { return sortedPartList; }
  
  const std::vector<int>& get_ranks() const 
    { return partRanks; }

  int build_map( iMesh_Instance imesh,
                 iMeshP_PartitionHandle partition,
                 int num_expected_parts );

  static int part_from_coords( iMesh_Instance imesh, 
                               iMeshP_PartHandle part, 
                               int& id_out );

private:
  inline int idx_from_part_id( iMeshP_Part id ) const
    { return std::lower_bound( sortedPartList.begin(), sortedPartList.end(), id ) 
          -  sortedPartList.begin(); }
  inline int idx_from_local_id( int id ) const
    { return localIdReverseMap[id]; }
    
  std::vector<iMeshP_Part> sortedPartList;
  std::vector<int> partRanks;
  std::vector<int> partLocalIds;
  std::vector<int> localIdReverseMap;
};

274

275 276 277
/**\brief Create mesh for use in parallel tests */
int create_mesh( const char* filename, int num_parts );

278 279 280
int create_mesh_in_memory( int rank, int size,  iMesh_Instance imesh,
    iMeshP_PartitionHandle& prtn, PartMap& map);

281 282 283
/**\brief get unique identifier for each vertex */
int vertex_tag( iMesh_Instance imesh, iBase_EntityHandle vertex, int& tag );

284 285 286 287 288 289 290 291 292 293 294
int get_local_parts( iMesh_Instance instance,
                     iMeshP_PartitionHandle prtn,
                     std::vector<iMeshP_PartHandle>& handles,
                     std::vector<iMeshP_Part>* ids = 0 )
{
  iMeshP_PartHandle* arr = 0;
  int ierr, alloc = 0, size;
  iMeshP_getLocalParts( instance, prtn, &arr, &alloc, &size, &ierr );
  CHKERR;
  handles.resize( size );
  std::copy( arr, arr + size, handles.begin() );
295
  free(arr);
296 297 298 299 300 301 302 303 304 305 306 307 308 309
  if (!ids)
    return iBase_SUCCESS;
  
  ids->resize( size );
  alloc = size;
  iMeshP_Part* ptr = &(*ids)[0];
  iMeshP_getPartIdsFromPartHandlesArr( instance, prtn, &handles[0], handles.size(),
                                       &ptr, &alloc, &size, &ierr );
  CHKERR;
  assert( size == (int)ids->size() );
  assert( ptr == &(*ids)[0] );
  return iBase_SUCCESS;
}

's avatar
committed
310 311 312 313 314 315 316

static int get_entities( iMesh_Instance imesh,
                         iBase_EntitySetHandle set,
                         iBase_EntityType type,
                         iMesh_EntityTopology topo,
                         std::vector<iBase_EntityHandle>& entities )
{
317
  iBase_EntityHandle* array = 0;
's avatar
committed
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
  int junk = 0, size = 0, err;
  iMesh_getEntities( imesh, set, type, topo, &array, &junk, &size, &err );
  if (!err) {
    entities.clear();
    entities.resize( size );
    std::copy( array, array + size, entities.begin() );
    free( array );
  }
  return err;
}

static int get_part_quads_and_verts( iMesh_Instance imesh,
                                     iMeshP_PartHandle part,
                                     std::vector<iBase_EntityHandle>& elems,
                                     std::vector<iBase_EntityHandle>& verts )
{
  int ierr = get_entities( imesh, part, iBase_FACE, iMesh_QUADRILATERAL, elems );
  CHKERR;
  
  verts.resize(4*elems.size());
  std::vector<int> junk(elems.size()+1);
  int junk1 = verts.size(), count, junk2 = junk.size(), junk3;
  iBase_EntityHandle* junk4 = &verts[0];
  int* junk5 = &junk[0];
  iMesh_getEntArrAdj( imesh, &elems[0], elems.size(), iBase_VERTEX,
                      &junk4, &junk1, &count,
                      &junk5, &junk2, &junk3, &ierr );
  CHKERR;
  assert( junk1 == (int)verts.size() );
  assert( count == (int)(4*elems.size()) );
  assert( junk2 == (int)junk.size() );
  assert( junk4 == &verts[0] );
  assert( junk5 == &junk[0] );
  std::sort( verts.begin(), verts.end() );
  verts.erase( std::unique( verts.begin(), verts.end() ), verts.end() );
  return iBase_SUCCESS;
}
  
static int get_coords( iMesh_Instance imesh,
                       const iBase_EntityHandle* verts,
                       int num_verts,
                       double* coords )
{
  double* junk1 = coords;
  int junk2 = 3*num_verts;
  int junk3;
  int ierr;
365
  iMesh_getVtxArrCoords( imesh, verts, num_verts, iBase_INTERLEAVED, &junk1, &junk2, &junk3, &ierr );
's avatar
committed
366 367 368 369 370 371 372 373
  if (iBase_SUCCESS != ierr)
    return ierr;
  assert( junk1 == coords );
  assert( junk2 == 3*num_verts );
  assert( junk3 == 3*num_verts );
  return iBase_SUCCESS;
}
  
374 375 376 377
/**************************************************************************
                              Main Method
 **************************************************************************/

378
#define RUN_TEST(A) run_test( &A, #A )
379 380

int run_test( int (*func)(iMesh_Instance, iMeshP_PartitionHandle, const PartMap&), 
381
              const char* func_name )
382
{
383
  int rank, size, ierr;
384
  MPI_Comm_rank( MPI_COMM_WORLD, &rank );
385
  MPI_Comm_size( MPI_COMM_WORLD, &size );
386 387 388 389 390 391 392
  iMesh_Instance imesh;
  iMesh_newMesh( 0, &imesh, &ierr, 0 );
  PCHECK;

  iMeshP_PartitionHandle prtn;
  iMeshP_createPartitionAll( imesh, MPI_COMM_WORLD, &prtn, &ierr );
  PCHECK;
393
  
394 395 396
  PartMap map;

#ifdef MOAB_HAVE_HDF5
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
  if (rank == 0) {
    ierr = create_mesh( FILENAME, size );
  }
  MPI_Bcast( &ierr, 1, MPI_INT, 0, MPI_COMM_WORLD );
  if (ierr) {
    if (rank == 0) {
      std::cerr << "Failed to create input test file on root processor.  Aborting."
                << std::endl;
    }
    abort();
  }
  
  ierr = test_load( imesh, prtn, map, size );
  if (ierr) {
    if (rank == 0) {
      std::cerr << "Failed to load input mesh." << std::endl
                << "Cannot run further tests." << std::endl
                << "ABORTING" << std::endl;
    }
    abort();
  }
418 419 420 421 422 423 424 425 426 427 428 429
#else
  // so we have MPI and no HDF5; in order to run the test we need to create the
  // model in memory, and then call sync to resolve shared ents, as if it was read
  ierr = create_mesh_in_memory( rank, size, imesh, prtn, map);
  MPI_Bcast( &ierr, 1, MPI_INT, 0, MPI_COMM_WORLD );
  if (ierr) {
    if (rank == 0) {
      std::cerr << "Failed to create mesh.  Aborting."
                << std::endl;
    }
    abort();
  }
430

431
#endif
432 433
  int result = (*func)(imesh,prtn,map);
  int is_err = is_any_proc_error( result );
434 435 436 437 438 439 440
  if (rank == 0) {
    if (is_err) 
      std::cout << func_name << " : FAILED!!" << std::endl;
    else
      std::cout << func_name << " : success" << std::endl;
  }
  
441 442
  iMesh_dtor( imesh, &ierr );
  CHKERR;
443 444 445 446 447 448
  return is_err;
}

int main( int argc, char* argv[] )
{
  MPI_Init(&argc, &argv);
449
  int size, rank;
450 451 452
  MPI_Comm_rank( MPI_COMM_WORLD, &rank );
  MPI_Comm_size( MPI_COMM_WORLD, &size );

453
  if (argc > 2 && !strcmp(argv[1], "-p")) {
's avatar
committed
454
#if !defined(_MSC_VER) && !defined(__MINGW32__)
455 456 457 458 459
    std::cout << "Processor " << rank << " of " << size << " with PID " << getpid() << std::endl;
    std::cout.flush();
#endif
      // loop forever on requested processor, giving the user time
      // to attach a debugger.  Once the debugger in attached, user
460
      // can change 'pause'.  E.g. on gdb do "set var pause = 0"
461
    if (atoi(argv[2]) == rank) {
462 463
      volatile int pause = 1;
      while (pause);
464 465 466 467
    }
    MPI_Barrier(MPI_COMM_WORLD);
  }

468
  int num_errors = 0;
469 470 471 472 473 474 475 476 477 478 479 480 481
  num_errors += RUN_TEST( test_get_partitions );
  num_errors += RUN_TEST( test_get_parts );
  num_errors += RUN_TEST( test_get_by_type );
  num_errors += RUN_TEST( test_get_by_topo );
  num_errors += RUN_TEST( test_part_id_handle );
  num_errors += RUN_TEST( test_part_rank );
  num_errors += RUN_TEST( test_get_neighbors );
  num_errors += RUN_TEST( test_get_part_boundary );
  num_errors += RUN_TEST( test_part_boundary_iter );
  num_errors += RUN_TEST( test_get_adjacencies );
  num_errors += RUN_TEST( test_entity_iterator );
  num_errors += RUN_TEST( test_entity_owner );
  num_errors += RUN_TEST( test_entity_status );
's avatar
committed
482
  num_errors += RUN_TEST( test_entity_copy_parts );
483
  num_errors += RUN_TEST( test_entity_copies );
484 485
  num_errors += RUN_TEST( test_push_tag_data_iface );
  num_errors += RUN_TEST( test_push_tag_data_ghost );
486
  num_errors += RUN_TEST( test_create_ghost_ents );
487
  num_errors += RUN_TEST( test_exchange_ents );
488 489 490 491 492
  
    // wait until all procs are done before writing summary data
  std::cout.flush();
  MPI_Barrier( MPI_COMM_WORLD );
  
493
#ifdef MOAB_HAVE_HDF5
's avatar
committed
494 495 496
    // clean up output file
  if (rank == 0)
    remove( FILENAME );
497
#endif
's avatar
committed
498
  
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
  if (rank == 0) {
    if (!num_errors) 
      std::cout << "All tests passed" << std::endl;
    else
      std::cout << num_errors << " TESTS FAILED!" << std::endl;
  }
  
  MPI_Finalize();
  return num_errors;
}

// Create a mesh
//
// 
// Groups of four quads will be arranged into parts as follows:
// +------+------+------+------+------+-----
// |             |             |
// |             |             |
// +    Part 0   +    Part 2   +    Part 4
// |             |             |
// |             |             |
// +------+------+------+------+------+-----
// |             |             |
// |             |             |
// +    Part 1   +    Part 3   +    Part 5
// |             |             |
// |             |             |
// +------+------+------+------+------+-----
//
// Vertices will be enumerated as follows:
// 1------6-----11-----16-----21-----26-----
// |             |             |
// |             |             |
// 2      7     12     17     22     27
// |             |             |
// |             |             |
// 3------8-----13-----18-----23-----28-----
// |             |             |
// |             |             |
// 4      9     14     19     24     29
// |             |             |
// |             |             |
// 5-----10-----15-----20-----25-----30-----
//
// Element IDs will be [4*rank+1,4*rank+5]
template <int size> struct EHARR
{ 
  iBase_EntityHandle h[size];
  iBase_EntityHandle& operator[](int i){ return h[i]; } 
  operator iBase_EntityHandle*() { return h; }
};
int create_mesh( const char* filename, int num_parts )
{
  const char* tagname = "GLOBAL_ID";
  int ierr;
  
  iMesh_Instance imesh;
  iMesh_newMesh( 0, &imesh, &ierr, 0 ); CHKERR;
  
  const int num_full_cols = 2 * (num_parts / 2);
  const int need_half_cols = num_parts % 2;
  const int num_cols = num_full_cols + 2*need_half_cols;
561
  const int num_vtx = 5+5*num_cols - 4*(num_parts%2);
562 563 564 565 566 567 568 569 570 571
  std::vector< EHARR<5> > vertices( num_cols + 1 );
  std::vector< EHARR<4> > elements( num_cols );
  std::vector<int> vertex_ids( num_vtx );
  std::vector<iBase_EntityHandle> vertex_list(num_vtx);
  for (int i = 0; i < num_vtx; ++i)
    vertex_ids[i] = i+1;
  
    // create vertices
  int vl_pos = 0;
  for (int i = 0; i <= num_cols; ++i) {
572 573 574 575 576
    double coords[15] = { static_cast<double>(i), 0, 0,
                          static_cast<double>(i), 1, 0,
                          static_cast<double>(i), 2, 0,
                          static_cast<double>(i), 3, 0,
                          static_cast<double>(i), 4, 0 };
577
    iBase_EntityHandle* ptr = vertices[i];
578
    const int n = (num_full_cols == num_cols || i <= num_full_cols) ? 5 : 3;
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
    int junk1 = n, junk2 = n;
    iMesh_createVtxArr( imesh, n, iBase_INTERLEAVED, coords, 3*n,
                        &ptr, &junk1, &junk2, &ierr ); CHKERR;
    assert( ptr == vertices[i] );
    assert( junk1 == n );
    assert( junk2 == n );
    for (int j = 0; j < n; ++j)
      vertex_list[vl_pos++] = vertices[i][j];
  }
  
    // create elements
  for (int i = 0; i < num_cols; ++i) {
    iBase_EntityHandle conn[16];
    for (int j = 0; j < 4; ++j) {
      conn[4*j  ] = vertices[i  ][j  ];
      conn[4*j+1] = vertices[i  ][j+1];
      conn[4*j+2] = vertices[i+1][j+1];
      conn[4*j+3] = vertices[i+1][j  ];
    }
    iBase_EntityHandle* ptr = elements[i];
    const int n = (i < num_full_cols) ? 4 : 2;
    int junk1 = n, junk2 = n, junk3 = n, junk4 = n;
    int stat[4];
    int* ptr2 = stat;
    iMesh_createEntArr( imesh, 
                        iMesh_QUADRILATERAL, 
                        conn, 4*n,
                        &ptr, &junk1, &junk2,
                        &ptr2, &junk3, &junk4, 
                        &ierr ); CHKERR;
    assert( ptr == elements[i] );
    assert( junk1 == n );
    assert( junk2 == n );
    assert( ptr2 == stat );
    assert( junk3 == n );
    assert( junk4 == n );
  }
  
    // create partition
  iMeshP_PartitionHandle partition;
  iMeshP_createPartitionAll( imesh, MPI_COMM_SELF, &partition, &ierr ); CHKERR;
  for (int i = 0; i < num_parts; ++i) {
    iMeshP_PartHandle part;
    iMeshP_createPart( imesh, partition, &part, &ierr ); CHKERR;
623 624 625 626
    iBase_EntityHandle quads[] = { elements[2*(i/2)  ][2*(i%2)  ],
                                   elements[2*(i/2)+1][2*(i%2)  ],
                                   elements[2*(i/2)  ][2*(i%2)+1],
                                   elements[2*(i/2)+1][2*(i%2)+1] };
627
    iMesh_addEntArrToSet( imesh, quads, 4, part, &ierr ); CHKERR;
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
  }
  
    // assign global ids to vertices
  iBase_TagHandle id_tag = 0;
  iMesh_getTagHandle( imesh, tagname, &id_tag, &ierr, strlen(tagname) );
  if (iBase_SUCCESS == ierr) {
    int tag_size, tag_type;
    iMesh_getTagSizeValues( imesh, id_tag, &tag_size, &ierr );
    CHKERR;
    if (tag_size != 1)
      return iBase_TAG_ALREADY_EXISTS;
    iMesh_getTagType( imesh, id_tag, &tag_type, &ierr );
    CHKERR;
    if (tag_type != iBase_INTEGER)
      return iBase_TAG_ALREADY_EXISTS;
  }
  else {
    iMesh_createTag( imesh, tagname, 1, iBase_INTEGER, &id_tag, &ierr, strlen(tagname) );
    CHKERR;
  }
  iMesh_setIntArrData( imesh, &vertex_list[0], num_vtx, id_tag, &vertex_ids[0], num_vtx, &ierr );
  CHKERR;
  
    // write file
  iBase_EntitySetHandle root_set;
  iMesh_getRootSet( imesh, &root_set, &ierr );
's avatar
committed
654
  iMeshP_saveAll( imesh, partition, root_set, filename, 0, &ierr, strlen(filename), 0 );
655 656 657 658 659 660
  CHKERR;
  
  iMesh_dtor( imesh, &ierr ); CHKERR;
  
  return 0;
}
661 662 663 664 665
int create_mesh_in_memory( int rank, int num_parts,  iMesh_Instance imesh,
    iMeshP_PartitionHandle & partition, PartMap & map)
{
  const char* tagname = "GLOBAL_ID";
  int ierr;
666

667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
  const int num_cols = 2;
  const int num_vtx = 9;
  // we are on the top or botton row
  int bottom=rank%2; // 0  2
                     // 1  3
  std::vector< EHARR<3> > vertices( 3 );
  std::vector< EHARR<2> > elements( 2 ); // 4 elements per process
  std::vector<int> vertex_ids( num_vtx );
  std::vector<iBase_EntityHandle> vertex_list(num_vtx);
  int start = 1+2*bottom+10*(rank/2);

  for (int i = 0; i < 3; ++i)
    for (int j = 0; j < 3; ++j)
    {
      vertex_ids[i+3*j]= start+i+5*j;
    }

    // create vertices
  int vl_pos = 0;
  int startI=2*(rank/2); // so it will be 0, 0, 2, 2, ...)
Iulian Grindeanu's avatar
Iulian Grindeanu committed
687 688 689 690
  for (int i = 0; i <= 2; ++i) {
    double coords[9] = { static_cast<double>(i+startI),   2.*bottom, 0,
                         static_cast<double>(i+startI), 1+2.*bottom, 0,
                         static_cast<double>(i+startI), 2+2.*bottom, 0,
691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
                       };
    iBase_EntityHandle* ptr = vertices[i];
    const int n = 3;
    int junk1 = n, junk2 = n;
    iMesh_createVtxArr( imesh, n, iBase_INTERLEAVED, coords, 3*n,
                        &ptr, &junk1, &junk2, &ierr ); CHKERR;
    assert( ptr == vertices[i] );
    assert( junk1 == n );
    assert( junk2 == n );
    for (int j = 0; j < n; ++j)
      vertex_list[vl_pos++] = vertices[i][j];
  }

    // create elements
  for (int i = 0; i < num_cols; ++i) {
    iBase_EntityHandle conn[8];
    for (int j = 0; j < 2; ++j) {
      conn[4*j  ] = vertices[i  ][j  ];
      conn[4*j+1] = vertices[i  ][j+1];
      conn[4*j+2] = vertices[i+1][j+1];
      conn[4*j+3] = vertices[i+1][j  ];
    }
    iBase_EntityHandle* ptr = elements[i];
    const int n = 2;
    int junk1 = n, junk2 = n, junk3 = n, junk4 = n;
    int stat[4];
    int* ptr2 = stat;
    iMesh_createEntArr( imesh,
                        iMesh_QUADRILATERAL,
                        conn, 4*n,
                        &ptr, &junk1, &junk2,
                        &ptr2, &junk3, &junk4,
                        &ierr ); CHKERR;
    assert( ptr == elements[i] );
    assert( junk1 == n );
    assert( junk2 == n );
    assert( ptr2 == stat );
    assert( junk3 == n );
    assert( junk4 == n );
  }

  // create partition
  iMeshP_createPartitionAll( imesh, MPI_COMM_WORLD, &partition, &ierr ); CHKERR;

  iMeshP_PartHandle part;
  iMeshP_createPart( imesh, partition, &part, &ierr ); CHKERR;
  iBase_EntityHandle quads[] = { elements[0  ][ 0  ],
                                 elements[0  ][ 1  ],
                                 elements[1  ][ 0  ],
                                 elements[1  ][ 1  ] };
  iMesh_addEntArrToSet( imesh, quads, 4, part, &ierr ); CHKERR;


    // assign global ids to vertices
  iBase_TagHandle id_tag = 0;
  iMesh_getTagHandle( imesh, tagname, &id_tag, &ierr, strlen(tagname) );
  if (iBase_SUCCESS == ierr) {
    int tag_size, tag_type;
    iMesh_getTagSizeValues( imesh, id_tag, &tag_size, &ierr );
    CHKERR;
    if (tag_size != 1)
      return iBase_TAG_ALREADY_EXISTS;
    iMesh_getTagType( imesh, id_tag, &tag_type, &ierr );
    CHKERR;
    if (tag_type != iBase_INTEGER)
      return iBase_TAG_ALREADY_EXISTS;
  }
  else {
    iMesh_createTag( imesh, tagname, 1, iBase_INTEGER, &id_tag, &ierr, strlen(tagname) );
    CHKERR;
  }
  iMesh_setIntArrData( imesh, &vertex_list[0], num_vtx, id_tag, &vertex_ids[0], num_vtx, &ierr );
  CHKERR;

  // some mesh sync
  iMeshP_syncPartitionAll(imesh, partition, &ierr);
  CHKERR;
768 769
  iMeshP_syncMeshAll(imesh, partition, &ierr);
  CHKERR;
770 771 772 773 774

  ierr = map.build_map( imesh, partition, num_parts );
  CHKERR;
  return 0;
}
775 776 777 778 779 780 781 782 783
// generate unique for each vertex from coordinates.
// Assume integer coordinate values with x in [0,inf] and y in [0,4]
// as generated by create_mean(..).
int vertex_tag( iMesh_Instance imesh, iBase_EntityHandle vertex, int& tag ) 
{
  int ierr;
  double x, y, z;
  iMesh_getVtxCoord( imesh, vertex, &x, &y, &z, &ierr );
  CHKERR;
784
  
785 786 787 788 789
  int xc = (int)round(x);
  int yc = (int)round(y);
  tag = 5*xc + yc + 1;
  return ierr;
}
790 791 792 793 794 795 796 797 798 799 800

/**************************************************************************
                           Test  Implementations
 **************************************************************************/

int test_load( iMesh_Instance imesh, iMeshP_PartitionHandle prtn, PartMap& map, int proc_size )
{
  int ierr;
  
  iBase_EntitySetHandle root_set;
  iMesh_getRootSet( imesh, &root_set, &ierr );
801
  const char *opt = "moab:PARTITION=PARALLEL_PARTITION";
802
  iMeshP_loadAll( imesh, prtn, root_set, FILENAME, opt, &ierr, strlen(FILENAME), strlen(opt) );
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
  PCHECK;

  
  ierr = map.build_map( imesh, prtn, proc_size );
  CHKERR;
  return iBase_SUCCESS;
}


/**\brief Test partition query methods
 *
 * Test:
 * - iMeshP_getPartitionComm
 * - iMeshP_getNumPartitions
 * - iMeshP_getPartitions
 */
int test_get_partitions( iMesh_Instance imesh, iMeshP_PartitionHandle prtn, const PartMap& )
{
  int ierr;
  
    // test iMeshP_getPartitionCom
  MPI_Comm comm = MPI_COMM_SELF;
  iMeshP_getPartitionComm( imesh, prtn, &comm, &ierr );
  PCHECK;
  ASSERT(comm == MPI_COMM_WORLD);
  
  
    // test iMeshP_getPartitions
  iMeshP_PartitionHandle* array = 0;
  int alloc = 0, size = -1;
  iMeshP_getPartitions( imesh, &array, &alloc, &size, &ierr );
  PCHECK;
  ASSERT(array != 0);
  ASSERT(alloc == size);
  ASSERT(size > 0);
  int idx = std::find(array, array+size, prtn) - array;
  free(array);
  ASSERT(idx < size);
  
    // test iMesP_getNumPartitions
  int size2 = -1;
  iMeshP_getNumPartitions( imesh, &size2, &ierr );
  PCHECK;
  ASSERT(size2 == size);
  return 0;
}

  

/**\brief Test part quyery methods
 *
 * Test:
 * - iMeshP_getNumGlobalParts
 * - iMeshP_getNumLocalParts
 * - iMeshP_getLocalParts
 */
int test_get_parts( iMesh_Instance imesh, iMeshP_PartitionHandle prtn, const PartMap& map )
{
  int size, rank, ierr;
  MPI_Comm_rank( MPI_COMM_WORLD, &rank );
  MPI_Comm_size( MPI_COMM_WORLD, &size );
  
  int num_part_g;
  iMeshP_getNumGlobalParts( imesh, prtn, &num_part_g, &ierr );
  PCHECK;
  ASSERT( num_part_g == map.num_parts() );
  
  int num_part_l;
  iMeshP_getNumLocalParts( imesh, prtn, &num_part_l, &ierr );
  PCHECK;
  ASSERT( num_part_l == map.count_from_rank( rank ) );
  
  std::vector<iMeshP_PartHandle> parts(num_part_l);
  iMeshP_PartHandle* ptr = &parts[0];
  int junk1 = num_part_l, count = -1;
  iMeshP_getLocalParts( imesh, prtn, &ptr, &junk1, &count, &ierr );
  PCHECK;
  assert( ptr == &parts[0] );
  assert( junk1 == num_part_l );
  ASSERT( count == num_part_l );
  
  return iBase_SUCCESS;
}

static int test_get_by_type_topo_all( iMesh_Instance imesh,
                                      iMeshP_PartitionHandle prtn,
                                      bool test_type,
                                      int num_parts )
{
    // calculate number of quads and vertices in entire mesh
    // from number of parts (see create_mesh(..) function.)
  const int expected_global_quad_count = 4 * num_parts;
  const int num_col = 2 * (num_parts / 2 + num_parts % 2);
896 897 898
  const int expected_global_vtx_count = num_parts == 1 ? 9 :
                                        num_parts % 2  ? 1 + 5*num_col :
                                                         5 + 5*num_col;
899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926
  
    // test getNumOf*All for root set
  int ierr, count;
  iBase_EntitySetHandle root;
  iMesh_getRootSet( imesh, &root, &ierr );  
  if (test_type) 
    iMeshP_getNumOfTypeAll( imesh, prtn, root, iBase_VERTEX, &count, &ierr );
  else
    iMeshP_getNumOfTopoAll( imesh, prtn, root, iMesh_POINT, &count, &ierr );
  PCHECK;
  ASSERT( count == expected_global_vtx_count );
  if (test_type) 
    iMeshP_getNumOfTypeAll( imesh, prtn, root, iBase_FACE, &count, &ierr );
  else
    iMeshP_getNumOfTopoAll( imesh, prtn, root, iMesh_QUADRILATERAL, &count, &ierr );
  PCHECK;
  ASSERT( count == expected_global_quad_count );
  
    // create an entity set containing half of the quads
  std::vector<iBase_EntityHandle> all_quads, half_quads;
  ierr = get_entities( imesh, root, iBase_FACE, iMesh_QUADRILATERAL, all_quads );
  assert( 0 == all_quads.size() % 2 );
  half_quads.resize(all_quads.size()/2);
  for (size_t i = 0; i < all_quads.size() / 2; ++i)
    half_quads[i] = all_quads[2*i];
  iBase_EntitySetHandle set;
  iMesh_createEntSet( imesh, 1, &set, &ierr );
  CHKERR;
927
  iMesh_addEntArrToSet( imesh, &half_quads[0], half_quads.size(), set, &ierr );
928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992
  CHKERR;
  
    // test getNumOf*All with defined set
  if (test_type) 
    iMeshP_getNumOfTypeAll( imesh, prtn, set, iBase_VERTEX, &count, &ierr );
  else
    iMeshP_getNumOfTopoAll( imesh, prtn, set, iMesh_POINT, &count, &ierr );
  PCHECK;
  ASSERT( count == 0 );
  if (test_type) 
    iMeshP_getNumOfTypeAll( imesh, prtn, set, iBase_FACE, &count, &ierr );
  else
    iMeshP_getNumOfTopoAll( imesh, prtn, set, iMesh_QUADRILATERAL, &count, &ierr );
  PCHECK;
  ASSERT( count == expected_global_quad_count/2 );
  
  return 0;
}

static int test_get_by_type_topo_local( iMesh_Instance imesh,
                                        iMeshP_PartitionHandle prtn,
                                        bool test_type )
{
  int ierr;
  iBase_EntitySetHandle root;
  iMesh_getRootSet( imesh, &root, &ierr );  
 
    // select a single part
  std::vector<iMeshP_PartHandle> parts;
  ierr = get_local_parts( imesh, prtn, parts );
  CHKERR;
  iMeshP_PartHandle part = parts.front();
  
    // get the entities contained in the part
  std::vector<iBase_EntityHandle> part_quads, part_all;
  ierr = get_entities( imesh, part, iBase_FACE, iMesh_QUADRILATERAL, part_quads ); CHKERR;
  ierr = get_entities( imesh, part, iBase_ALL_TYPES, iMesh_ALL_TOPOLOGIES, part_all ); CHKERR;
  
    // compare local counts (using root set)
    
  int count;
  if (test_type)
    iMeshP_getNumOfType( imesh, prtn, part, root, iBase_FACE, &count, &ierr );
  else
    iMeshP_getNumOfTopo( imesh, prtn, part, root, iMesh_QUADRILATERAL, &count, &ierr );
  CHKERR;
  ASSERT( count == (int)part_quads.size() );

  if (test_type)
    iMeshP_getNumOfType( imesh, prtn, part, root, iBase_ALL_TYPES, &count, &ierr );
  else
    iMeshP_getNumOfTopo( imesh, prtn, part, root, iMesh_ALL_TOPOLOGIES, &count, &ierr );
  CHKERR;
  ASSERT( count == (int)part_all.size() );
  
    // compare local contents (using root set)

  iBase_EntityHandle* ptr = 0;
  int num_ent, junk1 = 0;
  iMeshP_getEntities( imesh, prtn, part, root, test_type ? iBase_FACE : iBase_ALL_TYPES,
                      test_type ? iMesh_ALL_TOPOLOGIES : iMesh_QUADRILATERAL,
                      &ptr, &junk1, &num_ent, &ierr ); CHKERR;
  std::vector<iBase_EntityHandle> act_quads( ptr, ptr+num_ent );
  free(ptr);
  junk1 = num_ent = 0;
993
  ptr = 0;
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
  iMeshP_getEntities( imesh, prtn, part, root, iBase_ALL_TYPES,
                      iMesh_ALL_TOPOLOGIES,
                      &ptr, &junk1, &num_ent, &ierr ); CHKERR;
  std::vector<iBase_EntityHandle> act_all( ptr, ptr+num_ent );
  free(ptr);
  std::sort( part_quads.begin(), part_quads.end() );
  std::sort( part_all.begin(), part_all.end() );
  std::sort( act_quads.begin(), act_quads.end() );
  std::sort( act_all.begin(), act_all.end() );
  ASSERT( part_quads == act_quads );
  ASSERT( part_all   == act_all   );
  
    // create an entity set containing half of the quads from the part
  std::vector<iBase_EntityHandle> half_quads(part_quads.size()/2);
  for (size_t i = 0; i < half_quads.size(); ++i)
    half_quads[i] = part_quads[2*i];
  iBase_EntitySetHandle set;
  iMesh_createEntSet( imesh, 1, &set, &ierr );
  CHKERR;
1013
  iMesh_addEntArrToSet( imesh, &half_quads[0], half_quads.size(), set, &ierr );
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
  CHKERR;
  
    // check if there exists any quads not in the part that we 
    // can add to the set
  std::vector<iBase_EntityHandle> all_quads, other_quads;
  ierr = get_entities( imesh, root, iBase_FACE, iMesh_QUADRILATERAL, all_quads); CHKERR;
  std::sort( all_quads.begin(), all_quads.end() );
  std::sort( part_quads.begin(), part_quads.end() );
  std::set_difference( all_quads.begin(), all_quads.end(),
                       part_quads.begin(), part_quads.end(),
                       std::back_inserter( other_quads ) );
1025
  iMesh_addEntArrToSet( imesh, &other_quads[0], other_quads.size(), set, &ierr );
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
  CHKERR;
  
    // compare local counts (using non-root set)
    
  if (test_type)
    iMeshP_getNumOfType( imesh, prtn, part, set, iBase_FACE, &count, &ierr );
  else
    iMeshP_getNumOfTopo( imesh, prtn, part, set, iMesh_QUADRILATERAL, &count, &ierr );
  CHKERR;
  ASSERT( count == (int)half_quads.size() );

  if (test_type)
    iMeshP_getNumOfType( imesh, prtn, part, set, iBase_VERTEX, &count, &ierr );
  else
    iMeshP_getNumOfTopo( imesh, prtn, part, set, iMesh_POINT, &count, &ierr );
  CHKERR;
  ASSERT( count == 0 );
  
    // compare local contents (using non-root set)

  junk1 = 0; num_ent = 0;
1047
  ptr = 0;
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331
  iMeshP_getEntities( imesh, prtn, part, set, test_type ? iBase_FACE : iBase_ALL_TYPES,
                      test_type ? iMesh_ALL_TOPOLOGIES : iMesh_QUADRILATERAL,
                      &ptr, &junk1, &num_ent, &ierr ); CHKERR;
  act_quads.resize(num_ent);
  std::copy( ptr, ptr + num_ent, act_quads.begin() );
  free(ptr);
  std::sort( half_quads.begin(), half_quads.end() );
  std::sort( act_quads.begin(), act_quads.end() );
  ASSERT( act_quads == half_quads );
  
  return iBase_SUCCESS;
}
    


/**\brief Test query by entity type
 *
 * Test:
 * - iMeshP_getNumOfTypeAll
 * - iMeshP_getNumOfType
 * - iMeshP_getEntities
 * - 
 */
int test_get_by_type( iMesh_Instance imesh, iMeshP_PartitionHandle prtn, const PartMap& map )
{
  int ierr;
  ierr = test_get_by_type_topo_all( imesh, prtn, true, map.num_parts() );
  PCHECK;
  ierr = test_get_by_type_topo_local( imesh, prtn, true );
  PCHECK;
  return 0;
}

/**\brief Test query by entity topology
 *
 * Test:
 * - iMeshP_getNumOfTopoAll
 * - iMeshP_getNumOfTopo
 * - iMeshP_getEntities
 * - 
 */
int test_get_by_topo( iMesh_Instance imesh, iMeshP_PartitionHandle prtn, const PartMap& map )
{
  int ierr;
  ierr = test_get_by_type_topo_all( imesh, prtn, false, map.num_parts() );
  PCHECK;
  ierr = test_get_by_type_topo_local( imesh, prtn, false );
  PCHECK;
  return 0;
}


/**\brief Test mapping from part id to part handle
 * 
 * Test:
 * - iMeshP_getPartIdFromPartHandle
 * - iMeshP_getPartIdsFromPartHandlesArr
 * - iMeshP_getPartHandleFromPartId
 * - iMeshP_getPartHandlesFromPartsIdsArr
 */
int test_part_id_handle( iMesh_Instance imesh, iMeshP_PartitionHandle prtn, const PartMap& map )
{
    // get local part ids
  int rank, ierr;
  MPI_Comm_rank( MPI_COMM_WORLD, &rank );
  std::vector<iMeshP_Part> ids;
  map.part_id_from_rank( rank, ids );
  
    // check single-part functions and build list of part handles
  std::vector<iMeshP_PartHandle> handles( ids.size() );
  size_t i;
  for (i = 0; i < ids.size(); ++i) {
    iMeshP_getPartHandleFromPartId( imesh, prtn, ids[i], &handles[i], &ierr );
    CHKERR;
    iMeshP_Part id;
    iMeshP_getPartIdFromPartHandle( imesh, prtn, handles[i], &id, &ierr );
    CHKERR;
    if (id != ids[i])
      break;
  }
  ASSERT( i == ids.size() );
  
    // test iMeshP_getPartIdsFromPartHandlesArr
  std::vector<iMeshP_Part> ids2( ids.size() );
  int junk1 = ids.size(), junk2 = 0;
  iMeshP_Part* ptr = &ids2[0];
  iMeshP_getPartIdsFromPartHandlesArr( imesh, prtn, &handles[0], handles.size(),
                                       &ptr, &junk1, &junk2, &ierr );
  PCHECK;
  ASSERT( ptr == &ids2[0] );
  ASSERT( junk2 == (int)ids2.size() );
  ASSERT( ids == ids2 );
  
    // test iMeshP_getPartHandlesFromPartsIdsArr
  std::vector<iMeshP_PartHandle> handles2(handles.size());
  junk1 = handles.size();
  junk2 = 0;
  iMeshP_PartHandle* ptr2 = &handles2[0];
  iMeshP_getPartHandlesFromPartsIdsArr( imesh, prtn, &ids[0], ids.size(),
                                        &ptr2, &junk1, &junk2, &ierr );
  PCHECK;
  ASSERT( ptr2 == &handles2[0] );
  ASSERT( junk2 == (int)handles2.size() );
  ASSERT( handles == handles2 );
  
  return 0;
}

/**\brief Test get part rank
 *
 * Tests:
 * - iMeshP_getRankOfPart
 * - iMeshP_getRankOfPartArr
 */
int test_part_rank( iMesh_Instance imesh, iMeshP_PartitionHandle prtn, const PartMap& map )
{
  int ierr = 0, rank;
  std::vector<iMeshP_Part> invalid, failed;
  MPI_Comm_rank( MPI_COMM_WORLD, &rank );
  
    // test iMeshP_getRankOfPart
  for (size_t i = 0; i < map.get_parts().size(); ++i) {
    int pr;
    iMeshP_getRankOfPart( imesh, prtn, map.get_parts()[i], &pr, &ierr );
    if (iBase_SUCCESS != ierr)
      failed.push_back( map.get_parts()[i] );
    else if (pr != map.get_ranks()[i])
      invalid.push_back( map.get_parts()[i] );
  }
  if (!failed.empty()) {
    std::cerr << "Processor " << rank << ": iMeshP_getRankOfPart failed for " << failed.size() << " parts." << std::endl;
    ierr = iBase_FAILURE;
  }
  if (!invalid.empty()) {
    std::cerr << "Processor " << rank << ": iMeshP_getRankOfPart was incorrect for " << invalid.size() << " parts." << std::endl;
    ierr = iBase_FAILURE;
  }
  PCHECK;
   
    // test iMeshP_getRankOfPartArr
  std::vector<int> ranks( map.get_parts().size() );
  int junk1 = ranks.size(), junk2, *ptr = &ranks[0];
  iMeshP_getRankOfPartArr( imesh, prtn, &map.get_parts()[0], map.get_parts().size(),
                           &ptr, &junk1, &junk2, &ierr );
  PCHECK; 
  assert( ptr == &ranks[0] );
  assert( junk1 == (int)ranks.size() );
  ASSERT( junk2 == (int)ranks.size() );
  for (size_t i = 0; i < map.get_parts().size(); ++i) {
    if (ranks[i] != map.get_ranks()[i])
      invalid.push_back( map.get_parts()[i] );
  }
  if (!invalid.empty()) {
    std::cerr << "Processor " << rank << ": iMeshP_getRankOfPartArr was incorrect for " << invalid.size() << " parts." << std::endl;
    ierr = iBase_FAILURE;
  }
  PCHECK;
  
  return 0;
}
   

// see create_mesh(..)
static void get_part_neighbors( int logical_part_id,
                                int num_parts,
                                int neighbors[5],
                                int& num_neighbors )
{
  num_neighbors = 0;
  if (logical_part_id + 1 < num_parts)
    neighbors[num_neighbors++] = logical_part_id + 1;
  if (logical_part_id + 2 < num_parts)
    neighbors[num_neighbors++] = logical_part_id + 2;
  if (logical_part_id % 2) {
    neighbors[num_neighbors++] = logical_part_id - 1;
    if (logical_part_id > 2) {
      neighbors[num_neighbors++] = logical_part_id - 3;
      neighbors[num_neighbors++] = logical_part_id - 2;
    }
  }
  else {
    if (logical_part_id + 3 < num_parts)
      neighbors[num_neighbors++] = logical_part_id + 3;
    if (logical_part_id > 1) {
      neighbors[num_neighbors++] = logical_part_id - 1;
      neighbors[num_neighbors++] = logical_part_id - 2;
    }
  }
}

/**\brief Test querying of part neighbors
 *
 * Test:
 * - iMeshP_getNumPartNbors
 * - iMeshP_getNumPartNborsArr
 * - iMeshP_getPartNbors
 * - iMeshP_getPartNborsArr
 */
int test_get_neighbors( iMesh_Instance imesh, iMeshP_PartitionHandle prtn, const PartMap& map )
{
  int ierr, rank;
  MPI_Comm_rank( MPI_COMM_WORLD, &rank );
  
  std::vector<iMeshP_Part> local_parts;
  map.part_id_from_rank( rank, local_parts );
  
    // get handles for local parts
  std::vector<iMeshP_PartHandle> handles(local_parts.size());
  iMeshP_PartHandle* ptr = &handles[0];
  int junk1 = handles.size(), junk2 = 0;
  iMeshP_getPartHandlesFromPartsIdsArr( imesh, prtn, &local_parts[0], local_parts.size(),
                                        &ptr, &junk1, &junk2, &ierr );
  PCHECK;
  assert( ptr == &handles[0] );
  assert( junk2 == (int)handles.size() );
  
    // get logical ids for local parts
  std::vector<int> logical_ids;
  map.local_id_from_rank( rank, logical_ids );
  
    // get neighbors for each local part
  std::vector< std::vector<iMeshP_Part> > neighbors( logical_ids.size() );
  for (size_t i = 0;i < logical_ids.size(); ++i) {
    int logical_neighbors[5], num_neighbors;
    get_part_neighbors( logical_ids[i], map.num_parts(), logical_neighbors, num_neighbors );
    neighbors[i].resize( num_neighbors );
    for (int j = 0; j < num_neighbors; ++j)
      neighbors[i][j] = map.part_id_from_local_id( logical_neighbors[j] );
    std::sort( neighbors[i].begin(), neighbors[i].end() );
  }
  
    // test iMeshP_getNumPartNbors
  std::vector< iMeshP_Part > invalid, failed;
  for (size_t i = 0; i < local_parts.size(); ++i) {
    int count;
    iMeshP_getNumPartNbors( imesh, prtn, handles[i], iBase_VERTEX, &count, &ierr );
    if (ierr)
      failed.push_back( local_parts[i] );
    else if (count != (int)neighbors[i].size())
      invalid.push_back( local_parts[i] );
  }
  if (!failed.empty()) {
    std::cerr << "Processor " << rank << ": iMeshP_getNumPartNbors failed for " << failed.size() << " parts." << std::endl;
    ierr = iBase_FAILURE; PCHECK;
  }
  if (!invalid.empty()) {
    std::cerr << "Processor " << rank << ": iMeshP_getNumPartNbors was incorrect for " << invalid.size() << " parts." << std::endl;
    ierr = iBase_FAILURE; PCHECK;
  }
  
    // test iMeshP_getPartNbors
  ierr = 0;
  for (size_t i = 0; i < local_parts.size(); ++i) {
    int count, junk = 0, another_count;
    iMeshP_Part* list = 0;
    iMeshP_getPartNbors( imesh, prtn, handles[i], iBase_VERTEX, &another_count, &list, &junk, &count, &ierr );
    assert( count == another_count );
    if (ierr)
      failed.push_back( local_parts[i] );
    else {
      std::sort( list, list+count );
      std::vector<iMeshP_Part> cpy( list, list+count );
      if (cpy != neighbors[i])
        invalid.push_back( local_parts[i] );
      free(list);
    }
  }
  if (!failed.empty()) {
    std::cerr << "Processor " << rank << ": iMeshP_getPartNbors failed for " << failed.size() << " parts." << std::endl;
    ierr = iBase_FAILURE;
  }
  if (!invalid.empty()) {
    std::cerr << "Processor " << rank << ": iMeshP_getPartNbors was incorrect for " << invalid.size() << " parts." << std::endl;
    ierr = iBase_FAILURE;
  }
  PCHECK;
        
    // test iMeshP_getNumPartNborsArr
  std::vector<int> count_vect( handles.size() );
  int* count_arr = &count_vect[0];
  junk1 = handles.size();
  iMeshP_getNumPartNborsArr( imesh, prtn, &handles[0], handles.size(), iBase_VERTEX,
                             &count_arr, &junk1, &junk2, &ierr );
  PCHECK;
1332
  assert( count_arr == &count_vect[0] );
1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345
  assert( junk2 == (int)handles.size() );
  for (size_t i = 0; i < local_parts.size(); ++i) {
    if (count_arr[i] != (int)neighbors[i].size())
      invalid.push_back( local_parts[i] );
  }
  if (!invalid.empty()) {
    std::cerr << "Processor " << rank << ": iMeshP_getNumPartNborsArr was incorrect for " << invalid.size() << " parts." << std::endl;
    ierr = iBase_FAILURE;
  }
  PCHECK;
  
    // test iMeshP_getPartNborsArr
  iMeshP_Part* nbor_arr = 0;
1346
  junk1  = handles.size(), junk2 = 0;
1347 1348 1349 1350 1351 1352
  int junk3 = 0, nbor_size;
  iMeshP_getPartNborsArr( imesh, prtn, &handles[0], handles.size(), iBase_VERTEX,
                          &count_arr, &junk1, &junk2, 
                          &nbor_arr, &junk3, &nbor_size, 
                          &ierr );
  PCHECK;
1353
  assert( count_arr == &count_vect[0] );
1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395
  assert( junk2 == (int)handles.size() );
  std::vector<iMeshP_Part> all_nbors( nbor_arr, nbor_arr + nbor_size );
  free( nbor_arr );
  std::vector<iMeshP_Part>::iterator j = all_nbors.begin();
  bool bad_length = false;
  for (size_t i = 0; i < local_parts.size(); ++i) {
    if (all_nbors.end() - j > count_arr[i]) {
      bad_length = true;
      break;
    }
    if (count_arr[i] != (int)neighbors[i].size()) {
      invalid.push_back( local_parts[i] );
    }
    else {
      std::vector<iMeshP_Part>::iterator e = j + count_arr[i];
      std::sort( j, e );
      if (!std::equal( j, e, neighbors[i].begin() ))
        invalid.push_back( local_parts[i] );
    }
  }
  if (bad_length)  {
    std::cerr << "Processor " << rank << ": iMeshP_getPartNborsArr had inconsistent result array lengths." << std::endl;
    ierr = iBase_FAILURE;
  }
  if (!invalid.empty()) {
    std::cerr << "Processor " << rank << ": iMeshP_getPartNborsArr was incorrect for " << invalid.size() << " parts." << std::endl;
    ierr = iBase_FAILURE;
  }
  PCHECK;
  
  return 0;
}

// Determine the expected vertices on the interface between two parts.
// Returns no vertices for non-adjacient parts and fails if both parts
// are the same.
// See create_mesh(..) for the assumed mesh.
static int interface_verts( iMesh_Instance imesh,
                            iMeshP_PartitionHandle prtn,
                            iMeshP_PartHandle local_part,
                            iMeshP_Part other_part,
                            const PartMap& map,
Jim Porter's avatar
Jim Porter committed
1396
			    std::vector<iBase_EntityHandle> &vtx_handles )
1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424
{
  int ierr, rank;
  MPI_Comm_rank( MPI_COMM_WORLD, &rank );

  iMeshP_Part local_id;
  iMeshP_getPartIdFromPartHandle( imesh, prtn, local_part, &local_id, &ierr );
  CHKERR;
  
  const int local_logical = map.local_id_from_part_id( local_id );
  const int other_logical = map.local_id_from_part_id( other_part );
  
    // get grid of local vertices
  
  iBase_EntityHandle verts[3][3];
  const double xbase = (local_id / 2) * 2;
  const double ybase = (local_id % 2) * 2;
  
    // get quads in partition
  iBase_EntityHandle quads[4], *ptr = quads;
  int junk1 = 4, junk2;
  iMesh_getEntities( imesh, local_part, iBase_FACE, iMesh_QUADRILATERAL, &ptr, &junk1, &junk2, &ierr );
  CHKERR;
  assert( ptr == quads );
  assert( junk1 == 4 );
  assert( junk2 == 4 );
  
    // get vertices in quads
  iBase_EntityHandle conn[16];
1425
  int offsets[5], *off_ptr = offsets, junk3 = 5, junk4;
1426
  ptr = conn;
1427
  junk1 = 16;
1428 1429 1430 1431 1432 1433 1434 1435 1436
  iMesh_getEntArrAdj( imesh, quads, 4, iBase_VERTEX, 
                      &ptr, &junk1, &junk2,
                      &off_ptr, &junk3, &junk4,
                      &ierr );
  CHKERR;
  assert( ptr == conn );
  assert( junk1 == 16 );
  assert( junk2 == 16 );
  assert( off_ptr == offsets );
1437 1438
  assert( junk3 == 5 );
  assert( junk4 == 5 );
1439
  
1440
    // make unique vertex list
1441 1442 1443 1444 1445
  std::sort( conn, conn + 16 );
  const int num_vtx = std::unique( conn, conn+16 ) - conn;
  assert(9 == num_vtx);
  
    // get vertex coords
's avatar
committed
1446 1447
  std::vector<double> coords(27);
  ierr = get_coords( imesh, conn, 9, &coords[0] );
1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468
  CHKERR;
  
    // use vertex coords to determine logical position
  for (int i = 0; i < num_vtx; ++i) {
    int x = (int)round(coords[3*i  ] - xbase);
    int y = (int)round(coords[3*i+1] - ybase);
    if (x < 0 || x > 2 || y < 0 || y > 2) {
      std::cerr << "Processor " << rank << ": Error at " __FILE__ ":" << __LINE__ << std::endl
                << "  Invalid vertex coordinate: (" << coords[3*i] << ", " << coords[3*i+1]
                << ", " << coords[3*i+2] << ")" << std::endl
                << "  For logical partition " << local_id << std::endl;
      return iBase_FAILURE;
    }
    verts[x][y] = conn[i];
  }
  
  if (local_logical % 2) {
    switch (other_logical - local_logical) {
      case 0:
        return iBase_FAILURE;
      case 1: // upper right
Jim Porter's avatar
Jim Porter committed
1469
        vtx_handles.resize(1);
1470 1471 1472
        vtx_handles[0] = verts[2][0];
        break;
      case 2: // right
Jim Porter's avatar
Jim Porter committed
1473 1474
        vtx_handles.resize(3);
        std::copy( verts[2], verts[2]+3, vtx_handles.begin() );
1475 1476
        break;
      case -1: // above
Jim Porter's avatar
Jim Porter committed
1477
        vtx_handles.resize(3);
1478 1479 1480 1481 1482
        vtx_handles[0] = verts[0][0];
        vtx_handles[1] = verts[1][0];
        vtx_handles[2] = verts[2][0];
        break;
      case -2: // left
Jim Porter's avatar
Jim Porter committed
1483 1484
        vtx_handles.resize(3);
        std::copy( verts[0], verts[0]+3, vtx_handles.begin() );
1485 1486
        break;
      case -3: // upper left
Jim Porter's avatar
Jim Porter committed
1487
        vtx_handles.resize(1);
1488 1489 1490
        vtx_handles[0] = verts[0][0];
        break;
      default:
Jim Porter's avatar
Jim Porter committed
1491
        vtx_handles.clear();
1492 1493 1494 1495 1496 1497 1498 1499
        break;
    }
  }
  else {
    switch (other_logical - local_logical) {
      case 0:
        return iBase_FAILURE;
      case 1: // below
Jim Porter's avatar
Jim Porter committed
1500
        vtx_handles.resize(3);
1501 1502 1503 1504 1505
        vtx_handles[0] = verts[0][2];
        vtx_handles[1] = verts[1][2];
        vtx_handles[2] = verts[2][2];
        break;
      case 2: // right
Jim Porter's avatar
Jim Porter committed
1506 1507
        vtx_handles.resize(3);
        std::copy( verts[2], verts[2]+3, vtx_handles.begin() );
1508 1509
        break;
      case 3: // lower right
Jim Porter's avatar
Jim Porter committed
1510
        vtx_handles.resize(1);
1511 1512 1513
        vtx_handles[0] = verts[2][2];
        break;
      case -1: // lower left
Jim Porter's avatar
Jim Porter committed
1514
        vtx_handles.resize(1);
1515 1516 1517
        vtx_handles[0] = verts[0][2];
        break;
      case -2: // left
Jim Porter's avatar
Jim Porter committed
1518 1519
        vtx_handles.resize(3);
        std::copy( verts[0], verts[0]+3, vtx_handles.begin() );