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 "libmesh/utility.h" 13 : #include "SurrogateTrainer.h" 14 : #include "MultiApp.h" 15 : #include "MooseTypes.h" 16 : #include "libmesh/parallel.h" 17 : #include "DistributedData.h" 18 : 19 : // Forward declarations 20 : namespace libMesh 21 : { 22 : class ReplicatedMesh; 23 : } 24 : 25 : typedef StochasticTools::DistributedData<std::shared_ptr<DenseVector<Real>>> DistributedSnapshots; 26 : 27 : class PODReducedBasisTrainer : public SurrogateTrainerBase 28 : { 29 : public: 30 : static InputParameters validParams(); 31 : 32 : PODReducedBasisTrainer(const InputParameters & parameters); 33 : 34 : virtual void initialSetup() override; 35 : 36 160 : virtual void initialize() override{}; 37 : 38 : virtual void execute() override; 39 : 40 : virtual void finalize() override; 41 : 42 : /// Initializing the reduced operators. 43 : void initReducedOperators(); 44 : 45 : /// Adding a snapshot for a variable. 46 : void addSnapshot(unsigned int var_i, 47 : unsigned int glob_i, 48 : const std::shared_ptr<DenseVector<Real>> & snapshot); 49 : 50 : /// Adding the contribution of a residual to the reduced operators. 51 : void addToReducedOperator(unsigned int base_i, 52 : unsigned int tag_i, 53 : std::vector<DenseVector<Real>> & residual); 54 : 55 420 : const std::vector<std::string> & getVarNames() const { return _var_names; } 56 : 57 240 : const std::vector<std::string> & getTagNames() const { return _tag_names; } 58 : 59 320 : const std::vector<std::string> & getTagTypes() const { return _tag_types; } 60 : 61 : /// Getting the snapshot size across all of the processors for a given variable 62 : /// with index var_i. 63 : unsigned int getSnapsSize(unsigned int var_i) const; 64 : 65 : /// Getting the base size for variable with index v_ind. 66 80 : unsigned int getBaseSize(unsigned int var_i) const { return _base[var_i].size(); } 67 : 68 : /// Getting the overall base size, which is the sum of the individual bases. 69 : unsigned int getSumBaseSize() const; 70 : 71 : /// Getting a basis vector for a given variable. 72 : const DenseVector<Real> & getBasisVector(unsigned int var_i, unsigned int base_i) const; 73 : 74 : /// Getting basis vector based on its global index. 75 : const DenseVector<Real> & getBasisVector(unsigned int glob_i) const; 76 : 77 : /// Getting appropriate variable index for a global base index. 78 : unsigned int getVariableIndex(unsigned int glob_i) const; 79 : 80 : protected: 81 : /// Computes the correlation matrices using the snapshots. 82 : void computeCorrelationMatrix(); 83 : 84 : /// Computes the eigen-decomposition of the stored correlation matrices. 85 : void computeEigenDecomposition(); 86 : 87 : /// Generates the basis vectors using the snapshots together with the 88 : /// eigen-decomposition of the correlation matrices 89 : void computeBasisVectors(); 90 : 91 : /// Prints the eigenvalues of the correlation matrix for each variable. 92 : void printEigenvalues(); 93 : 94 : /// Vector containing the names of the variables we want to use for constructing 95 : /// the surrogates. 96 : std::vector<std::string> & _var_names; 97 : 98 : /// Energy limits that define how many basis functions will be kept for each variable. 99 : std::vector<Real> _error_res; 100 : 101 : /// Names of the tags that should be used to fetch residuals from the MultiApp. 102 : std::vector<std::string> & _tag_names; 103 : 104 : /// list of bools describing which tag is indepedent of the solution. 105 : std::vector<std::string> & _tag_types; 106 : 107 : /// Distributed container for snapshots per variable. 108 : std::vector<DistributedSnapshots> _snapshots; 109 : 110 : /// The correlation matrices for the variables. 111 : std::vector<DenseMatrix<Real>> _corr_mx; 112 : 113 : /// The eigenvalues of the correalation matrix for each variable. 114 : std::vector<DenseVector<Real>> _eigenvalues; 115 : 116 : /// The eigenvectors of the correalation matrix for each variable. 117 : std::vector<DenseMatrix<Real>> _eigenvectors; 118 : 119 : /// The reduced basis for the variables. 120 : std::vector<std::vector<DenseVector<Real>>> & _base; 121 : 122 : /// The reduced operators that should be transferred to the surrogate. 123 : std::vector<DenseMatrix<Real>> & _red_operators; 124 : 125 : /// Switch that tells if the object has already computed the necessary basis 126 : /// vectors. This switch is used in execute() to determine if we want to compute 127 : /// basis vectors or do something else. 128 : bool _base_completed; 129 : 130 : /// Flag describing if the reduced operators are empty or not. This is necessary 131 : /// to be able to do both the base and reduced operator generation in the same 132 : /// object. 133 : bool _empty_operators; 134 : 135 : private: 136 : /// Computes the number of bases necessary for a given error indicator. This 137 : /// needs a sorted vector as input. 138 : unsigned int determineNumberOfModes(Real error, const std::vector<Real> & inp_vec) const; 139 : 140 : /// Function that manipulates the received objects and computes the 141 : /// correlation matrices on the fly. 142 : void receiveObjects( 143 : ReplicatedMesh & mesh, 144 : std::unordered_map<unsigned int, std::vector<std::shared_ptr<DenseVector<Real>>>> & 145 : received_vectors, 146 : std::unordered_map<unsigned int, std::vector<std::shared_ptr<DenseVector<Real>>>> & 147 : local_vectors, 148 : processor_id_type /*pid*/, 149 : const std::vector< 150 : std::tuple<unsigned int, unsigned int, std::shared_ptr<DenseVector<Real>>>> & vectors); 151 : };