LCOV - code coverage report
Current view: top level - include/interfaces - MeshMetaDataInterface.h (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 2bf808 Lines: 22 22 100.0 %
Date: 2025-07-17 01:28:37 Functions: 31 31 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 "MooseError.h"
      13             : #include "RestartableData.h"
      14             : #include "MooseObject.h"
      15             : 
      16             : #include <string>
      17             : 
      18             : class MeshGenerator;
      19             : 
      20             : /**
      21             :  * The Interface used to retrieve mesh meta data (attributes) set by the MeshGenerator system.
      22             :  * MOOSE objects should avoid retrieving and casting MeshGenerator objects since they are not
      23             :  * re-created during a recover operation. This data is read from files early during the simulation
      24             :  * setup an d can be used to make decisions about how to setup the rest of the problem.
      25             :  */
      26             : class MeshMetaDataInterface
      27             : {
      28             : public:
      29             :   /// The system name used when initializing the Restartable interface
      30             :   static constexpr auto SYSTEM = "MeshMetaData";
      31             : 
      32             :   /// The data name used when initializing the Restartable interface for non-MeshGenerator objects.
      33             :   static constexpr auto NAME = "<empty>";
      34             : 
      35             : protected:
      36             :   /**
      37             :    * Interface for all objects reading MeshMetaData. The name of the object gets a bogus prefix,
      38             :    * which is not intended to be used for storing data. Instead this value is overridden during
      39             :    * retrieval.
      40             :    */
      41             :   MeshMetaDataInterface(const MooseObject * moose_object);
      42             : 
      43             :   /**
      44             :    * This class constructor is used for non-Moose-based objects like interfaces. A name for the
      45             :    * storage as well as a system name must be passed in along with the thread ID explicitly.
      46             :    */
      47             :   MeshMetaDataInterface(MooseApp & moose_app);
      48             : 
      49             :   /**
      50             :    * Method for retrieving a property with the given type and name exists in the mesh meta-data
      51             :    * store. This method will throw an error if the property does not exist.
      52             :    */
      53             :   template <typename T>
      54             :   const T & getMeshProperty(const std::string & data_name, const std::string & prefix);
      55             :   template <typename T>
      56             :   const T & getMeshProperty(const std::string & data_name)
      57             :   {
      58             :     return getMeshProperty<T>(data_name, meshPropertyPrefix(data_name));
      59             :   }
      60             : 
      61             :   /**
      62             :    * @returns Whether or not a mesh meta-data exists.
      63             :    */
      64             :   bool hasMeshProperty(const std::string & data_name, const std::string & prefix) const;
      65             :   /**
      66             :    * @returns Whether or not a mesh meta-data exists with the given type.
      67             :    */
      68             :   template <typename T>
      69             :   bool hasMeshProperty(const std::string & data_name, const std::string & prefix) const;
      70             : 
      71             :   /**
      72             :    * @returns Whether or not a mesh meta-data exists with the default prefix.
      73             :    */
      74      179316 :   bool hasMeshProperty(const std::string & data_name) const
      75             :   {
      76      179316 :     return hasMeshProperty(data_name, meshPropertyPrefix(data_name));
      77             :   }
      78             :   /**
      79             :    * @returns Whether or not a mesh meta-data exists with the default prefix and the given type.
      80             :    */
      81             :   template <typename T>
      82             :   bool hasMeshProperty(const std::string & data_name) const
      83             :   {
      84             :     return hasMeshProperty<T>(data_name, meshPropertyPrefix(data_name));
      85             :   }
      86             : 
      87             :   /**
      88             :    * @returns The full name for mesh property data.
      89             :    */
      90             :   static std::string meshPropertyName(const std::string & data_name, const std::string & prefix);
      91             : 
      92             :   /**
      93             :    * @returns The default mesh property name for mesh property data
      94             :    */
      95      179316 :   std::string meshPropertyName(const std::string & data_name) const
      96             :   {
      97      358632 :     return meshPropertyName(data_name, meshPropertyPrefix(data_name));
      98             :   }
      99             : 
     100             : private:
     101             :   /**
     102             :    * The default prefix to use for getting/seeing if mesh properties exist.
     103             :    *
     104             :    * For now, this is not supported except in MeshGenerators. In the future, we will
     105             :    * automate looking for mesh properties.
     106             :    */
     107             :   virtual std::string meshPropertyPrefix(const std::string & data_name) const;
     108             : 
     109             :   /// Helper for getting a mesh property
     110             :   const RestartableDataValue & getMeshPropertyInternal(const std::string & data_name,
     111             :                                                        const std::string & prefix) const;
     112             : 
     113             :   /// Reference to the application
     114             :   MooseApp & _meta_data_app;
     115             : 
     116             :   /// The MooseObject (if any); used for better error handling
     117             :   const MooseObject * const _meta_data_object;
     118             : 
     119             :   /**
     120             :    * Helper for forwarding a mooseError to an object's mooseError if it is available (said error
     121             :    * will provide more context: object name and type)
     122             :    */
     123             :   template <typename... Args>
     124           8 :   [[noreturn]] void mooseErrorInternal(Args &&... args) const
     125             :   {
     126           8 :     if (_meta_data_object)
     127           4 :       _meta_data_object->mooseError(std::forward<Args>(args)...);
     128           4 :     mooseError(std::forward<Args>(args)...);
     129             :   }
     130             : };
     131             : 
     132             : template <typename T>
     133             : const T &
     134         447 : MeshMetaDataInterface::getMeshProperty(const std::string & data_name, const std::string & prefix)
     135             : 
     136             : {
     137         447 :   if (!hasMeshProperty(data_name, prefix))
     138           4 :     mooseErrorInternal("Failed to get mesh property '", prefix, "/", data_name, "'");
     139             : 
     140         443 :   auto value = &getMeshPropertyInternal(data_name, prefix);
     141             :   mooseAssert(value->declared(), "Value has not been declared");
     142         443 :   const RestartableData<T> * T_value = dynamic_cast<const RestartableData<T> *>(value);
     143         443 :   if (!T_value)
     144           4 :     mooseErrorInternal("While retrieving mesh property '",
     145             :                        prefix,
     146             :                        "/",
     147             :                        data_name,
     148             :                        "' with type '",
     149             :                        MooseUtils::prettyCppType<T>(),
     150             :                        "',\nthe property exists with different type '",
     151           4 :                        value->type(),
     152             :                        "'");
     153         439 :   return T_value->get();
     154             : }
     155             : 
     156             : template <typename T>
     157             : bool
     158        2418 : MeshMetaDataInterface::hasMeshProperty(const std::string & data_name,
     159             :                                        const std::string & prefix) const
     160             : {
     161        2418 :   if (!hasMeshProperty(data_name, prefix))
     162        1787 :     return false;
     163         631 :   const auto & value = getMeshPropertyInternal(data_name, prefix);
     164         631 :   return dynamic_cast<const RestartableData<T> *>(&value) != nullptr;
     165             : }

Generated by: LCOV version 1.14