www.mooseframework.org
GridPartitioner.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #include "GridPartitioner.h"
11 
12 #include "GeneratedMesh.h"
13 #include "MooseApp.h"
14 
15 #include "libmesh/mesh_tools.h"
16 #include "libmesh/elem.h"
17 
19 
20 #include <memory>
21 
22 template <>
25 {
26  // These two are in this order because they are from different systems
27  // so you have to apply _this_ system's second to override the base
30 
31  // These are suppressed because they're going to get set programmatically
32  params.suppressParameter<MooseEnum>("elem_type");
33  params.suppressParameter<Real>("xmin");
34  params.suppressParameter<Real>("ymin");
35  params.suppressParameter<Real>("zmin");
36  params.suppressParameter<Real>("xmax");
37  params.suppressParameter<Real>("ymax");
38  params.suppressParameter<Real>("zmax");
39  params.suppressParameter<MooseEnum>("dim");
40 
41  params.set<MooseEnum>("parallel_type") = "REPLICATED";
42  params.set<MooseEnum>("partitioner") = "LINEAR";
43 
44  params.addClassDescription("Create a uniform grid that overlays the mesh to be partitioned. "
45  "Assign all elements within each cell of the grid to the same "
46  "processor.");
47 
48  return params;
49 }
50 
52  : MoosePartitioner(params), _mesh(*getCheckedPointerParam<MooseMesh *>("mesh"))
53 {
54 }
55 
57 
58 std::unique_ptr<Partitioner>
60 {
61  return libmesh_make_unique<GridPartitioner>(_pars);
62 }
63 
64 void
65 GridPartitioner::_do_partition(MeshBase & mesh, const unsigned int /*n*/)
66 {
67  // First: build the GeneratedMesh
68 
69  auto & factory = _app.getFactory();
70 
71  // Figure out the physical bounds of the given mesh
72  auto bounding_box = MeshTools::create_bounding_box(mesh);
73  const auto & min = bounding_box.min();
74  const auto & max = bounding_box.max();
75 
76  // Fill up a parameters object from the parameters for this class
77  auto params = factory.getValidParams("GeneratedMesh");
78  params.applyParameters(_pars);
79 
80  auto dim = mesh.mesh_dimension();
81  params.set<MooseEnum>("dim") = dim;
82 
83  // Need to make sure the number of cells in the grid matches the number of procs to partition for
84  auto num_cells = getParam<unsigned int>("nx");
85 
86  if (dim >= 2)
87  num_cells *= getParam<unsigned int>("ny");
88 
89  if (dim == 3)
90  num_cells *= getParam<unsigned int>("nz");
91 
92  if (num_cells != mesh.n_partitions())
93  mooseError("Number of cells in the GridPartitioner must match the number of MPI ranks! ",
94  num_cells,
95  " != ",
96  mesh.n_partitions());
97 
98  params.set<Real>("xmin") = min(0);
99  params.set<Real>("ymin") = min(1);
100  params.set<Real>("zmin") = min(2);
101 
102  params.set<Real>("xmax") = max(0);
103  params.set<Real>("ymax") = max(1);
104  params.set<Real>("zmax") = max(2);
105 
106  auto grid_mesh_ptr = _app.getFactory().create<MooseMesh>("GeneratedMesh", name() + "_gm", params);
107  grid_mesh_ptr->init();
108 
109  auto point_locator_ptr = grid_mesh_ptr->getPointLocator();
110 
111  // Loop over all of the elements in the given mesh
112  for (auto & elem_ptr : mesh.active_element_ptr_range())
113  {
114  // Find the element it lands in in the GeneratedMesh
115  auto grid_elem_ptr = (*point_locator_ptr)(elem_ptr->centroid());
116 
117  // True if we found something
118  if (grid_elem_ptr)
119  // Assign the _id_ of the cell to the processor_id
120  elem_ptr->processor_id() = grid_elem_ptr->id();
121  else // Should never happen (seriously - we create bounding boxes that should disallow this!
122  mooseError("GridPartitioner unable to locate element within the grid!");
123  }
124 }
std::shared_ptr< MooseObject > create(const std::string &obj_name, const std::string &name, InputParameters parameters, THREAD_ID tid=0, bool print_deprecated=true)
Build an object (must be registered) - THIS METHOD IS DEPRECATED (Use create<T>()) ...
Definition: Factory.C:87
InputParameters validParams< GridPartitioner >()
virtual ~GridPartitioner()
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
virtual void init()
Initialize the Mesh object.
Definition: MooseMesh.C:2032
void mooseError(Args &&... args) const
Definition: MooseObject.h:147
Factory & getFactory()
Retrieve the Factory associated with this App.
Definition: MooseApp.h:286
void suppressParameter(const std::string &name)
This method suppresses an inherited parameter so that it isn&#39;t required or valid in the derived class...
InputParameters validParams< GeneratedMesh >()
Definition: GeneratedMesh.C:27
MooseMesh wraps a libMesh::Mesh object and enhances its capabilities by caching additional data and s...
Definition: MooseMesh.h:74
registerMooseObject("MooseApp", GridPartitioner)
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:31
const InputParameters & _pars
Parameters of this object, references the InputParameters stored in the InputParametersWarehouse.
Definition: MooseObject.h:174
Partitions a mesh using a regular grid.
virtual std::unique_ptr< Partitioner > clone() const override
GridPartitioner(const InputParameters &params)
Base class for MOOSE partitioner.
MooseApp & _app
The MooseApp this object is associated with.
Definition: MooseObject.h:177
const std::string & name() const
Get the name of the object.
Definition: MooseObject.h:59
virtual void _do_partition(MeshBase &mesh, const unsigned int n) override
InputParameters validParams< MoosePartitioner >()