Skip to content

Commit

Permalink
Added a block weighted partitioner
Browse files Browse the repository at this point in the history
  • Loading branch information
fdkong committed Jul 15, 2019
1 parent 745e114 commit 8a32c03
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 0 deletions.
47 changes: 47 additions & 0 deletions framework/include/partitioner/BlockWeightedPartitioner.h
@@ -0,0 +1,47 @@
//* This file is part of the MOOSE framework
//* https://www.mooseframework.org
//*
//* All rights reserved, see COPYRIGHT for full restrictions
//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
//*
//* Licensed under LGPL 2.1, please see LICENSE for details
//* https://www.gnu.org/licenses/lgpl-2.1.html

#pragma once

// MOOSE includes
#include "MooseEnum.h"
#include "PetscExternalPartitioner.h"

class BlockWeightedPartitioner;
class MooseMesh;

template <>
InputParameters validParams<BlockWeightedPartitioner>();

/**
* Partition a mesh by weighting blocks. The motivation is that differenct
* blocks may have different physics. The work load per element is different
* block-by-block. This partitioner allows users to assign high weights for
* heavy blocks and low weights for other light blocks.
*/
class BlockWeightedPartitioner : public PetscExternalPartitioner
{
public:
BlockWeightedPartitioner(const InputParameters & params);

virtual std::unique_ptr<Partitioner> clone() const override;

virtual dof_id_type computeElementWeight(Elem & elm) override;

private:

/// Vector the block names supplied by the user via the input file
std::vector<SubdomainName> _blocks;
/// Block weights
std::vector<dof_id_type> _weights;
/// A map from subdomain to weight
std::unordered_map<SubdomainID, dof_id_type> _blocks_to_weights;
/// Moose mesh
MooseMesh & _mesh;
};
75 changes: 75 additions & 0 deletions framework/src/partitioner/BlockWeightedPartitioner.C
@@ -0,0 +1,75 @@
//* This file is part of the MOOSE framework
//* https://www.mooseframework.org
//*
//* All rights reserved, see COPYRIGHT for full restrictions
//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
//*
//* Licensed under LGPL 2.1, please see LICENSE for details
//* https://www.gnu.org/licenses/lgpl-2.1.html

#include "BlockWeightedPartitioner.h"
#include "MooseMesh.h"

#include "libmesh/elem.h"

registerMooseObject("MooseApp", BlockWeightedPartitioner);

template <>
InputParameters
validParams<BlockWeightedPartitioner>()
{
InputParameters params = validParams<PetscExternalPartitioner>();

params.addRequiredParam<std::vector<SubdomainName>>(
"block", "The list of block ids (SubdomainID) that this object will be applied");

params.addRequiredParam<std::vector<dof_id_type>>(
"weight", "The list of weights (integer) that specify how heavy each block is");

params.set<bool>("apply_element_weight") = true;

params.addClassDescription(
"Partition mesh by weighting blocks");

return params;
}

BlockWeightedPartitioner::BlockWeightedPartitioner(const InputParameters & params)
: PetscExternalPartitioner(params),
_blocks(getParam<std::vector<SubdomainName>>("block")),
_weights(getParam<std::vector<dof_id_type>>("weight")),
_mesh(*getParam<MooseMesh *>("mesh"))
{
if (_blocks.size() != _weights.size())
paramError("block", "Number of weights ",_weights.size(), " does not match with the number of blocks ", _blocks.size());

// Get the IDs from the supplied names
auto block_ids = _mesh.getSubdomainIDs(_blocks);

if (block_ids.size() != _blocks.size())
mooseError("Number of block ids ", block_ids.size(), " does not match with the number of blocks ", _blocks.size());

_blocks_to_weights.reserve(_weights.size());

for (MooseIndex(block_ids.size()) i = 0; i<block_ids.size(); i++)
{
_blocks_to_weights[block_ids[i]] = _weights[i];
}
}

std::unique_ptr<Partitioner>
BlockWeightedPartitioner::clone() const
{
return libmesh_make_unique<BlockWeightedPartitioner>(_pars);
}

dof_id_type
BlockWeightedPartitioner::computeElementWeight(Elem & elem)
{
auto blockid_to_weight = _blocks_to_weights.find(elem.subdomain_id());

if (blockid_to_weight == _blocks_to_weights.end())
mooseError("Can not find a weight for block id ",elem.subdomain_id());

return blockid_to_weight->second;
}

0 comments on commit 8a32c03

Please sign in to comment.