libMesh
mapped_subdomain_partitioner_test.C
Go to the documentation of this file.
1 #include <libmesh/libmesh.h>
2 #include <libmesh/replicated_mesh.h>
3 #include <libmesh/elem.h>
4 #include <libmesh/mesh_generation.h>
5 #include <libmesh/mesh_modification.h>
6 #include "libmesh/mapped_subdomain_partitioner.h"
7 
8 #include "test_comm.h"
9 #include "libmesh_cppunit.h"
10 
11 
12 using namespace libMesh;
13 
14 class MappedSubdomainPartitionerTest : public CppUnit::TestCase
15 {
20 public:
21  LIBMESH_CPPUNIT_TEST_SUITE( MappedSubdomainPartitionerTest );
22 
23 #if LIBMESH_DIM > 1
24  CPPUNIT_TEST( testMappedSubdomainPartitioner );
25 #endif
26 
27  CPPUNIT_TEST_SUITE_END();
28 
33 public:
34  void setUp() {}
35 
36  void tearDown() {}
37 
39  {
40  LOG_UNIT_TEST;
41 
42  ReplicatedMesh mesh(*TestCommWorld, /*dim=*/2);
43 
44  Real
45  xmin = 0., xmax = 1.,
46  ymin = 0., ymax = 10.;
47 
49  /*nx*/10,
50  /*ny*/100,
51  xmin, xmax,
52  ymin, ymax,
53  QUAD4);
54 
55  // The MappedSubdomainPartitioner partitions based on user-defined
56  // assignment of subdomains to processors.
57  mesh.partitioner() = std::make_unique<MappedSubdomainPartitioner>();
58 
59  // Get a pointer to the MappedSubdomainPartitioner so we can call its
60  // API specifically.
61  MappedSubdomainPartitioner * subdomain_partitioner =
62  dynamic_cast<MappedSubdomainPartitioner *>(mesh.partitioner().get());
63 
64  // Create 2x as many subdomains as processors, then assign them in
65  // the following way:
66  // subdomains(0,1) -> processor 0
67  // subdomains(2,3) -> processor 1
68  // subdomains(4,5) -> processor 2
69  // ...
70  // subdomains(n,n+1) -> processor n/2
71  subdomain_id_type n_subdomains = 2 * TestCommWorld->size();
72  for (subdomain_id_type sbd_id=0; sbd_id<n_subdomains; sbd_id+=2)
73  {
74  subdomain_partitioner->subdomain_to_proc[sbd_id] = sbd_id/2;
75  subdomain_partitioner->subdomain_to_proc[sbd_id+1] = sbd_id/2;
76  }
77 
78  // Assign subdomain ids to elements sequentially.
79  {
80  subdomain_id_type current_subdomain_id = 0;
81  for (auto & elem : mesh.element_ptr_range())
82  {
83  elem->subdomain_id() = current_subdomain_id++;
84 
85  // Wrap around
86  if (current_subdomain_id == n_subdomains)
87  current_subdomain_id = 0;
88  }
89  }
90 
91  // Partition again, now that we have set up the MappedSubdomainPartitioner.
92  mesh.partition();
93 
94  // Assert that the partitioning worked as expected.
95  for (auto & elem : mesh.element_ptr_range())
96  {
97  // Subdomain id n should map to processor id n/2.
98  CPPUNIT_ASSERT_EQUAL(static_cast<int>(elem->subdomain_id()/2),
99  static_cast<int>(elem->processor_id()));
100  }
101  }
102 };
103 
104 
The ReplicatedMesh class is derived from the MeshBase class, and is used to store identical copies of...
CPPUNIT_TEST_SUITE_REGISTRATION(MappedSubdomainPartitionerTest)
libMesh::Parallel::Communicator * TestCommWorld
Definition: driver.C:171
The MappedSubdomainPartitioner partitions the elements based on their subdomain ids.
MeshBase & mesh
virtual std::unique_ptr< Partitioner > & partitioner()
A partitioner to use at each prepare_for_use()
Definition: mesh_base.h:160
void build_square(UnstructuredMesh &mesh, const unsigned int nx, const unsigned int ny, const Real xmin=0., const Real xmax=1., const Real ymin=0., const Real ymax=1., const ElemType type=INVALID_ELEM, const bool gauss_lobatto_grid=false)
A specialized build_cube() for 2D meshes.
The libMesh namespace provides an interface to certain functionality in the library.
void setUp()
Note: this second public is necessary, something in the macros above leaves us in a private region...
virtual void partition(const unsigned int n_parts)
Call the default partitioner (currently metis_partition()).
Definition: mesh_base.C:1576
processor_id_type size() const
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real