LCOV - code coverage report
Current view: top level - include/restart - RestartableEquationSystems.h (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 99787a Lines: 4 4 100.0 %
Date: 2025-10-14 20:01:24 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 <set>
      13             : #include <string>
      14             : #include <unordered_map>
      15             : #include <iostream>
      16             : 
      17             : #include "libmesh/enum_order.h"
      18             : #include "libmesh/fe_type.h"
      19             : #include "libmesh/equation_systems.h"
      20             : #include "nlohmann/json.h"
      21             : 
      22             : namespace libMesh
      23             : {
      24             : class DofObject;
      25             : class EquationSystems;
      26             : class MeshBase;
      27             : }
      28             : 
      29             : /**
      30             :  * Wrapper class that owns a libMesh EquationSystem and adds advanced restart capability to it
      31             :  */
      32             : class RestartableEquationSystems
      33             : {
      34             : public:
      35             :   RestartableEquationSystems(libMesh::MeshBase & mesh);
      36             : 
      37             :   /**
      38             :    * Represents a stored variable in restart
      39             :    */
      40             :   struct VectorHeader
      41             :   {
      42             :     bool operator==(const VectorHeader & other) const
      43             :     {
      44             :       return name == other.name && projections == other.projections &&
      45             :              variable_offset == other.variable_offset && vector == other.vector;
      46             :     }
      47             : 
      48             :     /// The name of the stored vector
      49             :     std::string name;
      50             :     /// The type of the stored vector
      51             :     libMesh::ParallelType type;
      52             :     /// The projection flag (whether or not it should be projected or zeroed)
      53             :     bool projections;
      54             :     /// The position of each variable for this vector (relative to the start of the data)
      55             :     std::map<std::string, std::size_t> variable_offset;
      56             :     /// The underlying vector (only valid during store, not used in load)
      57             :     const libMesh::NumericVector<libMesh::Number> * vector = nullptr;
      58             :   };
      59             : 
      60             :   /**
      61             :    * Represents a stored variable in restart
      62             :    */
      63             :   struct VariableHeader
      64             :   {
      65             :     bool operator==(const VariableHeader & other) const
      66             :     {
      67             :       return name == other.name && type == other.type && size == other.size &&
      68             :              variable == other.variable;
      69             :     }
      70             : 
      71             :     /// The name of the stored variable
      72             :     std::string name;
      73             :     /// The type of the stored variable
      74             :     libMesh::FEType type;
      75             :     /// The size of this variable's data
      76             :     std::size_t size = 0;
      77             :     /// The underlying variable (only valid during store, not used in load)
      78             :     const libMesh::Variable * variable = nullptr;
      79             :   };
      80             : 
      81             :   /**
      82             :    * Represents a stored system in restart
      83             :    */
      84             :   struct SystemHeader
      85             :   {
      86             :     bool operator==(const SystemHeader & other) const
      87             :     {
      88             :       return name == other.name && type == other.type && variables == other.variables &&
      89             :              vectors == other.vectors;
      90             :     }
      91             : 
      92             :     /// The name of the stored system
      93             :     std::string name;
      94             :     /// The type of the stored system
      95             :     std::string type;
      96             :     /// The stored variables in the system
      97             :     std::map<std::string, RestartableEquationSystems::VariableHeader> variables;
      98             :     /// The stored vectors in the system
      99             :     std::map<std::string, RestartableEquationSystems::VectorHeader> vectors;
     100             :     /// Special name for a vector that is the system solution vector
     101             :     static const std::string system_solution_name;
     102             :   };
     103             : 
     104             :   /**
     105             :    * Represents a stored EquationSystems in restart
     106             :    */
     107             :   struct EquationSystemsHeader
     108             :   {
     109             :     bool operator==(const EquationSystemsHeader & other) const { return systems == other.systems; }
     110             : 
     111             :     /// The stored systems in the equation systems
     112             :     std::map<std::string, RestartableEquationSystems::SystemHeader> systems;
     113             :     /// The total size of data for this EquationSystems
     114             :     std::size_t data_size = 0;
     115             :   };
     116             : 
     117             :   /**
     118             :    * Stores the EquationSystems to the given stream
     119             :    */
     120             :   void store(std::ostream & stream) const;
     121             :   /**
     122             :    * Loads the EquationSystems from the given stream
     123             :    */
     124             :   void load(std::istream & stream);
     125             : 
     126             :   /**
     127             :    * @returns The underyling EquationSystems
     128             :    */
     129             :   ///@{
     130     1641274 :   libMesh::EquationSystems & es() { return _es; }
     131             :   const libMesh::EquationSystems & es() const { return _es; }
     132             :   ///@}
     133             : 
     134             :   /**
     135             :    * Sets whether or not all vectors are to be loaded.
     136             :    *
     137             :    * By default, this is true. This means that all vectors
     138             :    * that do not currently exist in the system will be added
     139             :    * and loaded.
     140             :    *
     141             :    * Typically, we would want this to be false in the case
     142             :    * of restart.
     143             :    */
     144        4577 :   void setLoadAllVectors(const bool load_all_vectors) { _load_all_vectors = load_all_vectors; }
     145             : 
     146             :   /// Checks whether variable was successfully restored from a restart file
     147             :   bool isVariableRestored(const std::string & system_name,
     148             :                           const std::string & vector_name,
     149             :                           const std::string & variable_name) const;
     150             : 
     151             :   /// Returns the set of variables that were loaded during the `load()` function. Each set of variables contain; [system name, vector name, variable name]
     152          24 :   const std::set<std::tuple<std::string, std::string, std::string>> & getLoadedVariables() const
     153             :   {
     154          24 :     return _loaded_variables;
     155             :   }
     156             : 
     157             : private:
     158             :   /// Internal method for building the header struct
     159             :   EquationSystemsHeader
     160             :   buildHeader(const std::vector<const libMesh::DofObject *> & ordered_objects) const;
     161             : 
     162             :   /// Internal method for ordering the DofObjects by ID (elems and the nodes)
     163             :   std::vector<const libMesh::DofObject *> orderDofObjects() const;
     164             : 
     165             :   void restore(const SystemHeader & from_sys_header,
     166             :                const VectorHeader & from_vec_header,
     167             :                const VariableHeader & from_var_header,
     168             :                const libMesh::System & to_sys,
     169             :                libMesh::NumericVector<libMesh::Number> & to_vec,
     170             :                const libMesh::Variable & to_var,
     171             :                std::istream & stream);
     172             : 
     173             :   /// The underlying EquationSystems
     174             :   libMesh::EquationSystems _es;
     175             : 
     176             :   /// Whether or not to load _all_ of the vectors, including ones that haven't been added yet
     177             :   bool _load_all_vectors;
     178             :   /// The starting position for the vector data in the input stream
     179             :   std::size_t _loaded_stream_data_begin;
     180             :   /// The object ordering for this data
     181             :   std::vector<const libMesh::DofObject *> _loaded_ordered_objects;
     182             :   /// The loaded header
     183             :   EquationSystemsHeader _loaded_header;
     184             : 
     185             :   /// The variables that were loaded in load(); [system name, vector name, variable name]
     186             :   std::set<std::tuple<std::string, std::string, std::string>> _loaded_variables;
     187             : };
     188             : 
     189             : void dataStore(std::ostream & stream, RestartableEquationSystems & res, void *);
     190             : void dataLoad(std::istream & stream, RestartableEquationSystems & res, void *);
     191             : 
     192             : void dataStore(std::ostream & stream,
     193             :                RestartableEquationSystems::EquationSystemsHeader & header,
     194             :                void *);
     195             : void
     196             : dataLoad(std::istream & stream, RestartableEquationSystems::EquationSystemsHeader & header, void *);
     197             : 
     198             : void dataStore(std::ostream & stream, RestartableEquationSystems::SystemHeader & header, void *);
     199             : void dataLoad(std::istream & stream, RestartableEquationSystems::SystemHeader & header, void *);
     200             : 
     201             : void dataStore(std::ostream & stream, RestartableEquationSystems::VariableHeader & header, void *);
     202             : void dataLoad(std::istream & stream, RestartableEquationSystems::VariableHeader & header, void *);
     203             : 
     204             : void dataStore(std::ostream & stream, RestartableEquationSystems::VectorHeader & header, void *);
     205             : void dataLoad(std::istream & stream, RestartableEquationSystems::VectorHeader & header, void *);
     206             : 
     207             : void to_json(nlohmann::json & json, const RestartableEquationSystems & res);

Generated by: LCOV version 1.14