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