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");
88 for (
unsigned int i = 1; i <
_vars.size(); ++i)
90 mooseError(
"Coupled polycrystal variables differ in periodicity");
115 TIME_SECTION(
"execute", 2,
"Computing Polycrystal Initial Condition");
143 auto n_nodes = current_elem->n_vertices();
146 const Node * current_node = current_elem->node_ptr(i);
159 "prepareDataForTransfer() hasn't been called yet");
203 mooseAssert(largest_id !=
invalid_size_t,
"Largest ID should not be invalid");
212 auto total_items = largest_id + 1;
247 TIME_SECTION(
"finalize", 2,
"Finalizing Polycrystal Initial Condition");
250 constexpr
unsigned int halo_thickness = 2;
298 while (it1 != it_end)
306 it1->merge(std::move(*it2));
319 auto & master_list = orig[0];
320 for (MooseIndex(
_maps_size) map_num = 1; map_num < orig.size(); ++map_num)
322 master_list.splice(master_list.end(), orig[map_num]);
323 orig[map_num].clear();
331 std::size_t & current_index,
334 unsigned int & new_id)
336 mooseAssert(
_t_step == 0,
"PolyIC only works if we begin in the initial condition");
339 auto entity_id = dof_object->id();
344 std::vector<unsigned int> grain_ids;
366 for (
auto grain_id : grains_it->second)
374 saved_grain_id = grain_id;
375 current_index = map_num;
389 new_id = saved_grain_id;
396 const auto & grain_ids = grains_it->second;
397 if (std::find(grain_ids.begin(), grain_ids.end(), feature->
_id) != grain_ids.end())
408 mooseAssert(elem,
"Element is nullptr");
410 std::vector<const Elem *> all_active_neighbors;
413 for (
auto i = decltype(elem->n_neighbors())(0); i < elem->n_neighbors(); ++i)
415 const Elem * neighbor_ancestor =
nullptr;
421 neighbor_ancestor = elem->neighbor_ptr(i);
423 if (neighbor_ancestor)
431 if (neighbor_ancestor->is_remote())
434 neighbor_ancestor->active_family_tree_by_neighbor(all_active_neighbors, elem,
false);
447 if (neighbor_ancestor)
455 if (neighbor_ancestor->is_remote())
458 neighbor_ancestor->active_family_tree_by_topological_neighbor(
464 for (
const auto neighbor : all_active_neighbors)
467 auto neighbor_id = neighbor->id();
472 std::vector<unsigned int> more_grain_ids;
477 neighbor_it, neighbor_id, std::move(more_grain_ids));
480 const auto & more_grain_ids = neighbor_it->second;
481 if (std::find(more_grain_ids.begin(), more_grain_ids.end(), feature->
_id) !=
482 more_grain_ids.end())
505 mooseAssert(
_is_primary,
"This routine should only be called on the primary rank");
516 (*_adjacency_matrix)(i,
j) = 1;
517 (*_adjacency_matrix)(
j, i) = 1;
526 mooseAssert(
_is_primary,
"This routine should only be called on the primary rank");
528 TIME_SECTION(
"assignOpsToGrains", 2,
"Assigning OPs to grains");
534 "The backtracking algorithm has exponential complexity. If you are using very few " 535 "order parameters,\nor you have several hundred grains or more, you should use one " 536 "of the PETSc coloring algorithms such as \"jp\".");
540 "Unable to find a valid grain to op coloring, Make sure you have created enough " 541 "variables to hold a\nvalid polycrystal initial condition (no grains represented " 542 "by the same variable should be allowed to\ntouch, ~8 for 2D, ~25 for 3D)?");
554 catch (std::runtime_error & e)
557 "Unable to find a valid grain to op coloring, Make sure you have created enough " 558 "variables to hold a\nvalid polycrystal initial condition (no grains represented " 559 "by the same variable should be allowed to\ntouch, ~8 for 2D, ~25 for 3D)?");
567 mooseAssert(
_grain_to_op.empty(),
"grain_to_op data structure should be empty here");
580 for (
unsigned int color_idx = 0; color_idx <
_op_num; ++color_idx)
584 unsigned int color = (vertex + color_idx) %
_op_num;
605 for (
unsigned int neighbor = 0; neighbor <
_feature_count; ++neighbor)
614 _console <<
"Grain Adjacency Matrix:\n";
622 _console <<
"Grain to OP assignments:\n";
631 return MooseEnum(
"jp power greedy bt",
"jp");
637 return "The grain neighbor graph coloring algorithm to use: \"jp\" (DEFAULT) Jones and " 638 "Plassmann, an efficient coloring algorithm, \"power\" an alternative stochastic " 639 "algorithm, \"greedy\", a greedy assignment algorithm with stochastic updates to " 640 "guarantee a valid coloring, \"bt\", a back tracking algorithm that produces good " 641 "distributions but may experience exponential run time in the worst case scenario " 642 "(works well on medium to large 2D problems)";
646 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 ...
KOKKOS_INLINE_FUNCTION const T * find(const T &target, const T *const begin, const T *const end)
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()
void paramError(const std::string ¶m, Args... args) const
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.
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...
FEProblemBase & _fe_problem
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.
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...
const std::array< bool, 3 > & queryPeriodicDimensions(const unsigned int sys_num, const unsigned int var_num) const
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
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
static MooseEnum coloringAlgorithms()
void colorAdjacencyMatrix(PetscScalar *adjacency_matrix, unsigned int size, unsigned int colors, std::vector< unsigned int > &vertex_colors, const char *coloring_algorithm)
void mooseError(Args &&... args) const
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...
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...
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()
void paramInfo(const std::string ¶m, Args... args) const
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