LCOV - code coverage report
Current view: top level - include/userobjects - PolycrystalUserObjectBase.h (source / functions) Hit Total Coverage
Test: idaholab/moose phase_field: #31405 (292dce) with base fef103 Lines: 6 6 100.0 %
Date: 2025-09-04 07:55:36 Functions: 3 3 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //* This file is part of the MOOSE framework
       2             : //* https://mooseframework.inl.gov
       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             : #pragma once
      11             : 
      12             : #include "DenseMatrix.h"
      13             : #include "FeatureFloodCount.h"
      14             : 
      15             : // Forward Declarations
      16             : 
      17             : /**
      18             :  * This object provides the base capability for creating proper polycrystal ICs. It is
      19             :  * able to discover the grain structure to provide information about neighboring grains
      20             :  * so that they will not be assigned the same order parameters with a reduced set of variables.
      21             :  */
      22             : class PolycrystalUserObjectBase : public FeatureFloodCount
      23             : {
      24             : public:
      25             :   static InputParameters validParams();
      26             : 
      27             :   PolycrystalUserObjectBase(const InputParameters & parameters);
      28             : 
      29             :   /**
      30             :    * This callback is triggered after the object is initialized and may be optionally
      31             :    * overridden to do precompute the element to grain identifiers ahead of time.
      32             :    */
      33         111 :   virtual void precomputeGrainStructure() {}
      34             : 
      35             :   /**
      36             :    * Method for retrieving active grain IDs based on some point in the mesh. Typically these
      37             :    * are element centroids or nodes depending on the basis functions being initialized. ICs that
      38             :    * have fixed resolution data (i.e. experimental datasets) may choose to implement
      39             :    * the element based method as well for added convenience.
      40             :    */
      41             :   virtual void getGrainsBasedOnPoint(const Point & point,
      42             :                                      std::vector<unsigned int> & grains) const = 0;
      43             : 
      44             :   /**
      45             :    * This method may be defined in addition to the point based initialization to speed up lookups.
      46             :    * It returns grain IDs based on the current element. Note: If your simulation contains adaptivity
      47             :    * the point based method may be used to retrieve grain information as well as this method.
      48             :    */
      49      541499 :   virtual void getGrainsBasedOnElem(const Elem & elem, std::vector<unsigned int> & grains) const
      50             :   {
      51      541499 :     getGrainsBasedOnPoint(elem.vertex_average(), grains);
      52      541499 :   }
      53             : 
      54             :   /**
      55             :    * Must be overridden by the deriving class to provide the number of grains in the polycrystal
      56             :    * structure.
      57             :    */
      58             :   virtual unsigned int getNumGrains() const = 0;
      59             : 
      60             :   /**
      61             :    * Returns the variable value for a given op_index and mesh point. This is the method used by the
      62             :    * initial condition after the Polycrystal grain structure has be setup. Those grains are
      63             :    * then distributed to the typically smaller number of order parameters by this class.
      64             :    * This method is then used to return those values but it may be overridden in a derived class.
      65             :    */
      66             :   virtual Real getVariableValue(unsigned int op_index, const Point & p) const = 0;
      67             : 
      68             :   /**
      69             :    * Similarly to the getVariableValue method, this method also returns values but may be optimized
      70             :    * for returning nodal values.
      71             :    */
      72     7972216 :   virtual Real getNodalVariableValue(unsigned int op_index, const Node & n) const
      73             :   {
      74     7972216 :     return getVariableValue(op_index, static_cast<const Point &>(n));
      75             :   }
      76             : 
      77             :   /* Returns all available coloring algorithms as an enumeration type for input files.
      78             :    */
      79             :   static MooseEnum coloringAlgorithms();
      80             : 
      81             :   /**
      82             :    * Returns corresponding descriptions of available coloring algorithms.
      83             :    */
      84             :   static std::string coloringAlgorithmDescriptions();
      85             : 
      86             :   /**
      87             :    * UserObject interface overrides. Derived classes should _not_ override any of these methods.
      88             :    */
      89             :   virtual void initialSetup() override;
      90             :   virtual void initialize() override;
      91             :   virtual void execute() override;
      92             :   virtual void finalize() override;
      93             : 
      94             : protected:
      95             :   virtual bool areFeaturesMergeable(const FeatureData & f1, const FeatureData & f2) const override;
      96             :   virtual bool isNewFeatureOrConnectedRegion(const DofObject * dof_object,
      97             :                                              std::size_t & current_index,
      98             :                                              FeatureData *& feature,
      99             :                                              Status & status,
     100             :                                              unsigned int & new_id) override;
     101             :   virtual void prepareDataForTransfer() override;
     102             :   virtual void mergeSets() override;
     103             :   virtual processor_id_type numberOfDistributedMergeHelpers() const override;
     104             :   virtual void restoreOriginalDataStructures(std::vector<std::list<FeatureData>> & orig) override;
     105             : 
     106             :   /**
     107             :    * Builds a dense adjacency matrix based on the discovery of grain neighbors and halos
     108             :    * surrounding each grain.
     109             :    */
     110             :   void buildGrainAdjacencyMatrix();
     111             : 
     112             :   /**
     113             :    * Method that runs a coloring algorithm to assign OPs to grains.
     114             :    */
     115             :   void assignOpsToGrains();
     116             : 
     117             :   /**
     118             :    * Built-in simple "back-tracking" algorithm to assign colors to a graph.
     119             :    */
     120             :   bool colorGraph(unsigned int vertex);
     121             : 
     122             :   /**
     123             :    * Helper method for the back-tracking graph coloring algorithm.
     124             :    */
     125             :   bool isGraphValid(unsigned int vertex, unsigned int color);
     126             : 
     127             :   /**
     128             :    * Prints out the adjacency matrix in a nicely spaced integer format.
     129             :    */
     130             :   void printGrainAdjacencyMatrix() const;
     131             : 
     132             :   /*************************************************
     133             :    *************** Data Structures *****************
     134             :    ************************************************/
     135             :   /// The dense adjacency matrix
     136             :   std::unique_ptr<DenseMatrix<Real>> _adjacency_matrix;
     137             : 
     138             :   /// mesh dimension
     139             :   const unsigned int _dim;
     140             : 
     141             :   /// The maximum number of order parameters (colors) available to assign to the grain structure
     142             :   const unsigned int _op_num;
     143             : 
     144             :   /// A map of the grain_id to op
     145             :   std::map<unsigned int, unsigned int> _grain_to_op;
     146             : 
     147             :   /// The selected graph coloring algorithm used by this object
     148             :   const MooseEnum _coloring_algorithm;
     149             : 
     150             :   /// A Boolean indicating whether the object has assigned colors to grains (internal use)
     151             :   bool _colors_assigned;
     152             : 
     153             :   /// A user controllable Boolean which can be used to print the adjacency matrix to the console
     154             :   const bool _output_adjacency_matrix;
     155             : 
     156             :   /// Used to indicate an invalid coloring for the built-in back-tracking algorithm
     157             :   static const unsigned int INVALID_COLOR;
     158             : 
     159             :   /// Used to hold the thickness of the halo that should be constructed for detecting adjacency
     160             :   static const unsigned int HALO_THICKNESS;
     161             : 
     162             : private:
     163             :   /// The number of chunks (for merging the features together)
     164             :   processor_id_type _num_chunks;
     165             : 
     166             :   /// A vector indicating which op is assigned to each grain (by index of the grain)
     167             :   std::vector<unsigned int> _grain_idx_to_op;
     168             : 
     169             :   /// Temporary storage area for current grains at a point to avoid memory churn
     170             :   std::vector<unsigned int> _prealloc_tmp_grains;
     171             : 
     172             :   std::map<dof_id_type, std::vector<unsigned int>> _entity_to_grain_cache;
     173             : };

Generated by: LCOV version 1.14