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  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  ReplicatedMesh mesh(*TestCommWorld, /*dim=*/2);
41 
42  Real
43  xmin = 0., xmax = 1.,
44  ymin = 0., ymax = 10.;
45 
47  /*nx*/10,
48  /*ny*/100,
49  xmin, xmax,
50  ymin, ymax,
51  QUAD4);
52 
53  // The MappedSubdomainPartitioner partitions based on user-defined
54  // assignment of subdomains to processors.
55  mesh.partitioner() = libmesh_make_unique<MappedSubdomainPartitioner>();
56 
57  // Get a pointer to the MappedSubdomainPartitioner so we can call its
58  // API specifically.
59  MappedSubdomainPartitioner * subdomain_partitioner =
60  dynamic_cast<MappedSubdomainPartitioner *>(mesh.partitioner().get());
61 
62  // Create 2x as many subdomains as processors, then assign them in
63  // the following way:
64  // subdomains(0,1) -> processor 0
65  // subdomains(2,3) -> processor 1
66  // subdomains(4,5) -> processor 2
67  // ...
68  // subdomains(n,n+1) -> processor n/2
69  subdomain_id_type n_subdomains = 2 * TestCommWorld->size();
70  for (subdomain_id_type sbd_id=0; sbd_id<n_subdomains; sbd_id+=2)
71  {
72  subdomain_partitioner->subdomain_to_proc[sbd_id] = sbd_id/2;
73  subdomain_partitioner->subdomain_to_proc[sbd_id+1] = sbd_id/2;
74  }
75 
76  // Assign subdomain ids to elements sequentially.
77  {
78  subdomain_id_type current_subdomain_id = 0;
79  for (auto & elem : mesh.element_ptr_range())
80  {
81  elem->subdomain_id() = current_subdomain_id++;
82 
83  // Wrap around
84  if (current_subdomain_id == n_subdomains)
85  current_subdomain_id = 0;
86  }
87  }
88 
89  // Partition again, now that we have set up the MappedSubdomainPartitioner.
90  mesh.partition();
91 
92  // Assert that the partitioning worked as expected.
93  for (auto & elem : mesh.element_ptr_range())
94  {
95  // Subdomain id n should map to processor id n/2.
96  CPPUNIT_ASSERT_EQUAL(static_cast<int>(elem->subdomain_id()/2),
97  static_cast<int>(elem->processor_id()));
98  }
99  }
100 };
101 
102 
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
MappedSubdomainPartitionerTest
Definition: mapped_subdomain_partitioner_test.C:14
mesh
MeshBase & mesh
Definition: mesh_communication.C:1257
libMesh::ReplicatedMesh
The ReplicatedMesh class is derived from the MeshBase class, and is used to store identical copies of...
Definition: replicated_mesh.h:47
MappedSubdomainPartitionerTest::setUp
void setUp()
Note: this second public is necessary, something in the macros above leaves us in a private region.
Definition: mapped_subdomain_partitioner_test.C:34
libMesh::MeshBase::element_ptr_range
virtual SimpleRange< element_iterator > element_ptr_range()=0
libMesh::MeshTools::Generation::build_square
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.
Definition: mesh_generation.C:1501
MappedSubdomainPartitionerTest::testMappedSubdomainPartitioner
void testMappedSubdomainPartitioner()
Definition: mapped_subdomain_partitioner_test.C:38
libMesh::QUAD4
Definition: enum_elem_type.h:41
TestCommWorld
libMesh::Parallel::Communicator * TestCommWorld
Definition: driver.C:111
MappedSubdomainPartitionerTest::tearDown
void tearDown()
Definition: mapped_subdomain_partitioner_test.C:36
CPPUNIT_TEST_SUITE_REGISTRATION
CPPUNIT_TEST_SUITE_REGISTRATION(MappedSubdomainPartitionerTest)
libmesh_cppunit.h
libMesh::MappedSubdomainPartitioner::subdomain_to_proc
std::map< subdomain_id_type, processor_id_type > subdomain_to_proc
Before calling partition() or partition_range(), the user must assign all the Mesh subdomains to cert...
Definition: mapped_subdomain_partitioner.h:79
libMesh::TestClass
Definition: id_types.h:33
libMesh::Real
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
Definition: libmesh_common.h:121
test_comm.h
libMesh::MappedSubdomainPartitioner
The MappedSubdomainPartitioner partitions the elements based on their subdomain ids.
Definition: mapped_subdomain_partitioner.h:42
libMesh::MeshBase::partition
virtual void partition(const unsigned int n_parts)
Call the default partitioner (currently metis_partition()).
Definition: mesh_base.C:599
libMesh::MeshBase::partitioner
virtual std::unique_ptr< Partitioner > & partitioner()
A partitioner to use at each prepare_for_use()
Definition: mesh_base.h:127