www.mooseframework.org
RandomPartitioner.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 "RandomPartitioner.h"
11 
12 #include "MooseApp.h"
13 #include "MooseMesh.h"
14 #include "MooseRandom.h"
15 
16 #include "libmesh/elem.h"
17 
19 
21 
24 {
26 
27  params.addParam<unsigned int>("seed", 0, "Seed for the random generator");
28 
29  params.addClassDescription("Assigns element processor ids randomly with a given seed.");
30 
31  return params;
32 }
33 
35  : MoosePartitioner(params), _num_procs(_app.getCommunicator()->size())
36 {
37  MooseRandom::seed(getParam<unsigned int>("seed"));
38 }
39 
41 
42 std::unique_ptr<Partitioner>
44 {
45  return libmesh_make_unique<RandomPartitioner>(_pars);
46 }
47 
48 void
49 RandomPartitioner::_do_partition(MeshBase & mesh, const unsigned int /*n*/)
50 {
51  // Random number is on [0, 1]: scale to number of procs and round down
52  for (auto & elem_ptr : mesh.active_element_ptr_range())
53  elem_ptr->processor_id() = std::floor(MooseRandom::rand() * _num_procs);
54 }
MooseMesh.h
RandomPartitioner::~RandomPartitioner
virtual ~RandomPartitioner()
Definition: RandomPartitioner.C:40
RandomPartitioner
Partitions a mesh randomly using element ids as the seed for the generator.
Definition: RandomPartitioner.h:23
InputParameters::addParam
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an option parameter and a documentation string to the InputParameters object.
Definition: InputParameters.h:1198
RandomPartitioner::RandomPartitioner
RandomPartitioner(const InputParameters &params)
Definition: RandomPartitioner.C:34
MooseRandom::rand
static double rand()
This method returns the next random number (double format) from the generator.
Definition: MooseRandom.h:49
RandomPartitioner.h
InputParameters
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
Definition: InputParameters.h:53
MooseRandom.h
MooseRandom::seed
static void seed(unsigned int seed)
The method seeds the random number generator.
Definition: MooseRandom.h:43
RandomPartitioner::validParams
static InputParameters validParams()
Definition: RandomPartitioner.C:23
MooseApp.h
RandomPartitioner::clone
virtual std::unique_ptr< Partitioner > clone() const override
Definition: RandomPartitioner.C:43
InputParameters::addClassDescription
void addClassDescription(const std::string &doc_string)
This method adds a description of the class that will be displayed in the input file syntax dump.
Definition: InputParameters.C:70
RandomPartitioner::_num_procs
const unsigned int _num_procs
Total number of processors.
Definition: RandomPartitioner.h:37
RandomPartitioner::_do_partition
virtual void _do_partition(MeshBase &mesh, const unsigned int n) override
Definition: RandomPartitioner.C:49
MoosePartitioner::validParams
static InputParameters validParams()
Definition: MoosePartitioner.C:16
MoosePartitioner
Base class for MOOSE partitioner.
Definition: MoosePartitioner.h:27
MooseObject::_pars
const InputParameters & _pars
Parameters of this object, references the InputParameters stored in the InputParametersWarehouse.
Definition: MooseObject.h:169
defineLegacyParams
defineLegacyParams(RandomPartitioner)
registerMooseObject
registerMooseObject("MooseApp", RandomPartitioner)