24 params.
addClassDescription(
"This object provides the base capability for creating proper reduced " 25 "order parameter polycrystal initial conditions.");
27 "variable",
"var_name_base",
"op_num",
"Array of coupled variables");
28 params.
addParam<
bool>(
"output_adjacency_matrix",
30 "Output the Grain Adjacency Matrix used in the coloring algorithms. " 31 "Additionally, the grain to OP assignments will be printed");
40 "ElementSideNeighborLayers",
44 { rm_params.
set<
unsigned short>(
"layers") = 2; }
52 params.
set<std::vector<OutputName>>(
"outputs") = {
"none"};
55 params.
set<
bool>(
"allow_duplicate_execution_on_initial") =
true;
67 _dim(_mesh.dimension()),
68 _op_num(_vars.size()),
69 _coloring_algorithm(getParam<
MooseEnum>(
"coloring_algorithm")),
70 _colors_assigned(false),
71 _output_adjacency_matrix(getParam<bool>(
"output_adjacency_matrix")),
74 mooseAssert(
_single_map_mode,
"Do not turn off single_map_mode with this class");
91 for (
unsigned int i = 1; i <
_vars.size(); ++i)
93 mooseError(
"Coupled polycrystal variables differ in periodicity");
119 TIME_SECTION(
"execute", 2,
"Computing Polycrystal Initial Condition");
147 auto n_nodes = current_elem->n_vertices();
150 const Node * current_node = current_elem->node_ptr(i);
163 "prepareDataForTransfer() hasn't been called yet");
207 mooseAssert(largest_id !=
invalid_size_t,
"Largest ID should not be invalid");
216 auto total_items = largest_id + 1;
251 TIME_SECTION(
"finalize", 2,
"Finalizing Polycrystal Initial Condition");
254 constexpr
unsigned int halo_thickness = 2;
302 while (it1 != it_end)
310 it1->merge(std::move(*it2));
323 auto & master_list = orig[0];
324 for (MooseIndex(
_maps_size) map_num = 1; map_num < orig.size(); ++map_num)
326 master_list.splice(master_list.end(), orig[map_num]);
327 orig[map_num].clear();
335 std::size_t & current_index,
338 unsigned int & new_id)
340 mooseAssert(
_t_step == 0,
"PolyIC only works if we begin in the initial condition");
343 auto entity_id = dof_object->id();
348 std::vector<unsigned int> grain_ids;
370 for (
auto grain_id : grains_it->second)
378 saved_grain_id = grain_id;
379 current_index = map_num;
393 new_id = saved_grain_id;
400 const auto & grain_ids = grains_it->second;
401 if (std::find(grain_ids.begin(), grain_ids.end(), feature->
_id) != grain_ids.end())
412 mooseAssert(elem,
"Element is nullptr");
414 std::vector<const Elem *> all_active_neighbors;
417 for (
auto i = decltype(elem->n_neighbors())(0); i < elem->n_neighbors(); ++i)
419 const Elem * neighbor_ancestor =
nullptr;
425 neighbor_ancestor = elem->neighbor_ptr(i);
427 if (neighbor_ancestor)
435 if (neighbor_ancestor->is_remote())
438 neighbor_ancestor->active_family_tree_by_neighbor(all_active_neighbors, elem,
false);
451 if (neighbor_ancestor)
459 if (neighbor_ancestor->is_remote())
462 neighbor_ancestor->active_family_tree_by_topological_neighbor(
468 for (
const auto neighbor : all_active_neighbors)
471 auto neighbor_id = neighbor->id();
476 std::vector<unsigned int> more_grain_ids;
481 neighbor_it, neighbor_id, std::move(more_grain_ids));
484 const auto & more_grain_ids = neighbor_it->second;
485 if (std::find(more_grain_ids.begin(), more_grain_ids.end(), feature->
_id) !=
486 more_grain_ids.end())
509 mooseAssert(
_is_primary,
"This routine should only be called on the primary rank");
520 (*_adjacency_matrix)(i,
j) = 1;
521 (*_adjacency_matrix)(
j, i) = 1;
530 mooseAssert(
_is_primary,
"This routine should only be called on the primary rank");
532 TIME_SECTION(
"assignOpsToGrains", 2,
"Assigning OPs to grains");
538 "The backtracking algorithm has exponential complexity. If you are using very few " 539 "order parameters,\nor you have several hundred grains or more, you should use one " 540 "of the PETSc coloring algorithms such as \"jp\".");
544 "Unable to find a valid grain to op coloring, Make sure you have created enough " 545 "variables to hold a\nvalid polycrystal initial condition (no grains represented " 546 "by the same variable should be allowed to\ntouch, ~8 for 2D, ~25 for 3D)?");
558 catch (std::runtime_error & e)
561 "Unable to find a valid grain to op coloring, Make sure you have created enough " 562 "variables to hold a\nvalid polycrystal initial condition (no grains represented " 563 "by the same variable should be allowed to\ntouch, ~8 for 2D, ~25 for 3D)?");
571 mooseAssert(
_grain_to_op.empty(),
"grain_to_op data structure should be empty here");
584 for (
unsigned int color_idx = 0; color_idx <
_op_num; ++color_idx)
588 unsigned int color = (vertex + color_idx) %
_op_num;
609 for (
unsigned int neighbor = 0; neighbor <
_feature_count; ++neighbor)
618 _console <<
"Grain Adjacency Matrix:\n";
626 _console <<
"Grain to OP assignments:\n";
635 return MooseEnum(
"jp power greedy bt",
"jp");
641 return "The grain neighbor graph coloring algorithm to use: \"jp\" (DEFAULT) Jones and " 642 "Plassmann, an efficient coloring algorithm, \"power\" an alternative stochastic " 643 "algorithm, \"greedy\", a greedy assignment algorithm with stochastic updates to " 644 "guarantee a valid coloring, \"bt\", a back tracking algorithm that produces good " 645 "distributions but may experience exponential run time in the worst case scenario " 646 "(works well on medium to large 2D problems)";
650 std::numeric_limits<unsigned int>::max();
virtual void finalize() override
std::unique_ptr< DenseMatrix< Real > > _adjacency_matrix
The dense adjacency matrix.
void buildGrainAdjacencyMatrix()
Builds a dense adjacency matrix based on the discovery of grain neighbors and halos surrounding each ...
void expandEdgeHalos(unsigned int num_layers_to_expand)
This method expands the existing halo set by some width determined by the passed in value...
static std::string coloringAlgorithmDescriptions()
Returns corresponding descriptions of available coloring algorithms.
std::map< unsigned int, unsigned int > _grain_to_op
A map of the grain_id to op.
static const std::size_t invalid_size_t
static InputParameters validParams()
PolycrystalUserObjectBase(const InputParameters ¶meters)
Status
This enumeration is used to indicate status of the grains in the _unique_grains data structure...
std::vector< std::set< dof_id_type > > _entities_visited
This variable keeps track of which nodes have been visited during execution.
virtual bool areFeaturesMergeable(const FeatureData &f1, const FeatureData &f2) const override
Method for determining whether two features are mergeable.
bool isTranslatedPeriodic(unsigned int nonlinear_var_num, unsigned int component) const
libMesh::PeriodicBoundaries * _pbs
A pointer to the periodic boundary constraints object.
virtual void finalize() override
std::vector< FeatureData > & _feature_sets
The data structure used to hold the globally unique features.
virtual void getGrainsBasedOnPoint(const Point &point, std::vector< unsigned int > &grains) const =0
Method for retrieving active grain IDs based on some point in the mesh.
std::map< dof_id_type, std::vector< unsigned int > > _entity_to_grain_cache
void assignOpsToGrains()
Method that runs a coloring algorithm to assign OPs to grains.
const bool _is_primary
Convenience variable for testing primary rank.
virtual void initialize() override
const Parallel::Communicator & _communicator
void printGrainAdjacencyMatrix() const
Prints out the adjacency matrix in a nicely spaced integer format.
virtual Elem * queryElemPtr(const dof_id_type i)
ExecFlagEnum getDefaultExecFlagEnum()
virtual void getGrainsBasedOnElem(const Elem &elem, std::vector< unsigned int > &grains) const
This method may be defined in addition to the point based initialization to speed up lookups...
uint8_t processor_id_type
processor_id_type n_processors() const
const dof_id_type n_nodes
const unsigned int _op_num
The maximum number of order parameters (colors) available to assign to the grain structure.
const unsigned int _dim
mesh dimension
processor_id_type _num_chunks
The number of chunks (for merging the features together)
std::vector< MooseVariable * > _vars
The vector of coupled in variables cast to MooseVariable.
static const unsigned int HALO_THICKNESS
Used to hold the thickness of the halo that should be constructed for detecting adjacency.
bool isGraphValid(unsigned int vertex, unsigned int color)
Helper method for the back-tracking graph coloring algorithm.
std::size_t _var_index
The Moose variable where this feature was found (often the "order parameter")
virtual void precomputeGrainStructure()
This callback is triggered after the object is initialized and may be optionally overridden to do pre...
bool _colors_assigned
A Boolean indicating whether the object has assigned colors to grains (internal use) ...
This object will mark nodes or elements of continuous regions all with a unique number for the purpos...
bool hasInitialAdaptivity() const
const std::size_t _maps_size
Convenience variable holding the size of all the datastructures size by the number of maps...
static const unsigned int invalid_id
void paramError(const std::string ¶m, Args... args) const
const bool _single_map_mode
This variable is used to indicate whether or not multiple maps are used during flooding.
unsigned int _feature_count
The number of features seen by this object (same as summing _feature_counts_per_map) ...
void broadcast(T &data, const unsigned int root_id=0, const bool identical_sizes=false) const
std::vector< unsigned int > _grain_idx_to_op
A vector indicating which op is assigned to each grain (by index of the grain)
bool colorGraph(unsigned int vertex)
Built-in simple "back-tracking" algorithm to assign colors to a graph.
processor_id_type linearPartitionChunk(dof_id_type num_items, dof_id_type num_chunks, dof_id_type item_id)
std::vector< std::list< FeatureData > > _partial_feature_sets
The data structure used to hold partial and communicated feature data, during the discovery and mergi...
virtual bool isNewFeatureOrConnectedRegion(const DofObject *dof_object, std::size_t ¤t_index, FeatureData *&feature, Status &status, unsigned int &new_id) override
Method called during the recursive flood routine that should return whether or not the current entity...
virtual void prepareDataForTransfer() override
This routine uses the local flooded data to build up the local feature data structures (_partial feat...
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
const bool _is_elemental
Determines if the flood counter is elements or not (nodes)
bool flood(const DofObject *dof_object, std::size_t current_index)
This method will check if the current entity is above the supplied threshold and "mark" it...
void max(const T &r, T &o, Request &req) const
FEProblemBase & _fe_problem
static MooseEnum coloringAlgorithms()
void colorAdjacencyMatrix(PetscScalar *adjacency_matrix, unsigned int size, unsigned int colors, std::vector< unsigned int > &vertex_colors, const char *coloring_algorithm)
std::vector< unsigned int > _feature_counts_per_map
The number of features seen by this object per map.
virtual void mergeSets() override
This routine is called on the primary rank only and stitches together the partial feature pieces seen...
void mooseError(Args &&... args) const
static const processor_id_type invalid_proc_id
virtual void execute() override
static const std::complex< double > j(0, 1)
Complex number "j" (also known as "i")
virtual void initialSetup() override
virtual unsigned int getNumGrains() const =0
Must be overridden by the deriving class to provide the number of grains in the polycrystal structure...
virtual void initialize() override
const bool _output_adjacency_matrix
A user controllable Boolean which can be used to print the adjacency matrix to the console...
const ConsoleStream _console
static const unsigned int INVALID_COLOR
Used to indicate an invalid coloring for the built-in back-tracking algorithm.
MooseMesh & _mesh
A reference to the mesh.
const libMesh::ConstElemRange & getNonlinearEvaluableElementRange()
virtual processor_id_type numberOfDistributedMergeHelpers() const override
Returns a number indicating the number of merge helpers when running in parallel based on certain imp...
void paramInfo(const std::string ¶m, Args... args) const
auto index_range(const T &sizable)
std::unique_ptr< libMesh::PointLocatorBase > _point_locator
virtual void restoreOriginalDataStructures(std::vector< std::list< FeatureData >> &orig) override
unsigned int _id
An ID for this feature.
const MooseEnum _coloring_algorithm
The selected graph coloring algorithm used by this object.
static InputParameters validParams()
virtual void initialSetup() override
UserObject interface overrides.
virtual void prepareDataForTransfer()
This routine uses the local flooded data to build up the local feature data structures (_partial feat...
const ExecFlagType EXEC_INITIAL