libMesh
mapped_subdomain_partitioner_test.C
Go to the documentation of this file.
1 // Ignore unused parameter warnings coming from cppunit headers
2 #include <libmesh/ignore_warnings.h>
3 #include <cppunit/extensions/HelperMacros.h>
4 #include <cppunit/TestCase.h>
5 #include <libmesh/restore_warnings.h>
6 
7 #include <libmesh/libmesh.h>
8 #include <libmesh/replicated_mesh.h>
9 #include <libmesh/elem.h>
10 #include <libmesh/mesh_generation.h>
11 #include <libmesh/mesh_modification.h>
12 #include "libmesh/mapped_subdomain_partitioner.h"
13 
14 #include "test_comm.h"
15 
16 // THE CPPUNIT_TEST_SUITE_END macro expands to code that involves
17 // std::auto_ptr, which in turn produces -Wdeprecated-declarations
18 // warnings. These can be ignored in GCC as long as we wrap the
19 // offending code in appropriate pragmas. We can't get away with a
20 // single ignore_warnings.h inclusion at the beginning of this file,
21 // since the libmesh headers pull in a restore_warnings.h at some
22 // point. We also don't bother restoring warnings at the end of this
23 // file since it's not a header.
24 #include <libmesh/ignore_warnings.h>
25 
26 using namespace libMesh;
27 
28 class MappedSubdomainPartitionerTest : public CppUnit::TestCase
29 {
34 public:
35  CPPUNIT_TEST_SUITE( MappedSubdomainPartitionerTest );
36 
37  CPPUNIT_TEST( testMappedSubdomainPartitioner );
38 
39  CPPUNIT_TEST_SUITE_END();
40 
45 public:
46  void setUp() {}
47 
48  void tearDown() {}
49 
51  {
52  ReplicatedMesh mesh(*TestCommWorld, /*dim=*/2);
53 
54  Real
55  xmin = 0., xmax = 1.,
56  ymin = 0., ymax = 10.;
57 
59  /*nx*/10,
60  /*ny*/100,
61  xmin, xmax,
62  ymin, ymax,
63  QUAD4);
64 
65  // The MappedSubdomainPartitioner partitions based on user-defined
66  // assignment of subdomains to processors.
68 
69  // Get a pointer to the MappedSubdomainPartitioner so we can call its
70  // API specifically.
71  MappedSubdomainPartitioner * subdomain_partitioner =
72  dynamic_cast<MappedSubdomainPartitioner *>(mesh.partitioner().get());
73 
74  // Create 2x as many subdomains as processors, then assign them in
75  // the following way:
76  // subdomains(0,1) -> processor 0
77  // subdomains(2,3) -> processor 1
78  // subdomains(4,5) -> processor 2
79  // ...
80  // subdomains(n,n+1) -> processor n/2
81  subdomain_id_type n_subdomains = 2 * TestCommWorld->size();
82  for (subdomain_id_type sbd_id=0; sbd_id<n_subdomains; sbd_id+=2)
83  {
84  subdomain_partitioner->subdomain_to_proc[sbd_id] = sbd_id/2;
85  subdomain_partitioner->subdomain_to_proc[sbd_id+1] = sbd_id/2;
86  }
87 
88  // Assign subdomain ids to elements sequentially.
89  {
90  subdomain_id_type current_subdomain_id = 0;
91  for (auto & elem : mesh.element_ptr_range())
92  {
93  elem->subdomain_id() = current_subdomain_id++;
94 
95  // Wrap around
96  if (current_subdomain_id == n_subdomains)
97  current_subdomain_id = 0;
98  }
99  }
100 
101  // Partition again, now that we have set up the MappedSubdomainPartitioner.
102  mesh.partition();
103 
104  // Assert that the partitioning worked as expected.
105  for (auto & elem : mesh.element_ptr_range())
106  {
107  // Subdomain id n should map to processor id n/2.
108  CPPUNIT_ASSERT_EQUAL(static_cast<int>(elem->subdomain_id()/2),
109  static_cast<int>(elem->processor_id()));
110  }
111  }
112 };
113 
114 
The ReplicatedMesh class is derived from the MeshBase class, and is used to store identical copies of...
CPPUNIT_TEST_SUITE_REGISTRATION(MappedSubdomainPartitionerTest)
unsigned int size() const
Definition: parallel.h:726
libMesh::Parallel::Communicator * TestCommWorld
Definition: driver.C:28
virtual SimpleRange< element_iterator > element_ptr_range() libmesh_override
The MappedSubdomainPartitioner partitions the elements based on their subdomain ids.
MeshBase & mesh
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...
std::unique_ptr< T > UniquePtr
Definition: auto_ptr.h:46
virtual void partition(const unsigned int n_parts)
Call the default partitioner (currently metis_partition()).
Definition: mesh_base.C:462
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
virtual UniquePtr< Partitioner > & partitioner()
A partitioner to use at each prepare_for_use()
Definition: mesh_base.h:112