https://mooseframework.inl.gov
PODReducedBasisTrainer.h
Go to the documentation of this file.
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 
26 
28 {
29 public:
31 
33 
34  virtual void initialSetup() override;
35 
36  virtual void initialize() override{};
37 
38  virtual void execute() override;
39 
40  virtual void finalize() override;
41 
43  void initReducedOperators();
44 
46  void addSnapshot(unsigned int var_i,
47  unsigned int glob_i,
48  const std::shared_ptr<DenseVector<Real>> & snapshot);
49 
51  void addToReducedOperator(unsigned int base_i,
52  unsigned int tag_i,
53  std::vector<DenseVector<Real>> & residual);
54 
55  const std::vector<std::string> & getVarNames() const { return _var_names; }
56 
57  const std::vector<std::string> & getTagNames() const { return _tag_names; }
58 
59  const std::vector<std::string> & getTagTypes() const { return _tag_types; }
60 
63  unsigned int getSnapsSize(unsigned int var_i) const;
64 
66  unsigned int getBaseSize(unsigned int var_i) const { return _base[var_i].size(); }
67 
69  unsigned int getSumBaseSize() const;
70 
72  const DenseVector<Real> & getBasisVector(unsigned int var_i, unsigned int base_i) const;
73 
75  const DenseVector<Real> & getBasisVector(unsigned int glob_i) const;
76 
78  unsigned int getVariableIndex(unsigned int glob_i) const;
79 
80 protected:
83 
86 
89  void computeBasisVectors();
90 
92  void printEigenvalues();
93 
96  std::vector<std::string> & _var_names;
97 
99  std::vector<Real> _error_res;
100 
102  std::vector<std::string> & _tag_names;
103 
105  std::vector<std::string> & _tag_types;
106 
108  std::vector<DistributedSnapshots> _snapshots;
109 
111  std::vector<DenseMatrix<Real>> _corr_mx;
112 
114  std::vector<DenseVector<Real>> _eigenvalues;
115 
117  std::vector<DenseMatrix<Real>> _eigenvectors;
118 
120  std::vector<std::vector<DenseVector<Real>>> & _base;
121 
123  std::vector<DenseMatrix<Real>> & _red_operators;
124 
129 
134 
135 private:
138  unsigned int determineNumberOfModes(Real error, const std::vector<Real> & inp_vec) const;
139 
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 };
virtual void execute() override
void addToReducedOperator(unsigned int base_i, unsigned int tag_i, std::vector< DenseVector< Real >> &residual)
Adding the contribution of a residual to the reduced operators.
std::vector< std::vector< DenseVector< Real > > > & _base
The reduced basis for the variables.
virtual void initialSetup() override
PODReducedBasisTrainer(const InputParameters &parameters)
std::vector< DenseMatrix< Real > > _eigenvectors
The eigenvectors of the correalation matrix for each variable.
void printEigenvalues()
Prints the eigenvalues of the correlation matrix for each variable.
void initReducedOperators()
Initializing the reduced operators.
The following methods are specializations for using the Parallel::packed_range_* routines for a vecto...
void receiveObjects(ReplicatedMesh &mesh, std::unordered_map< unsigned int, std::vector< std::shared_ptr< DenseVector< Real >>>> &received_vectors, std::unordered_map< unsigned int, std::vector< std::shared_ptr< DenseVector< Real >>>> &local_vectors, processor_id_type, const std::vector< std::tuple< unsigned int, unsigned int, std::shared_ptr< DenseVector< Real >>>> &vectors)
Function that manipulates the received objects and computes the correlation matrices on the fly...
static InputParameters validParams()
void addSnapshot(unsigned int var_i, unsigned int glob_i, const std::shared_ptr< DenseVector< Real >> &snapshot)
Adding a snapshot for a variable.
virtual void finalize() override
std::vector< DistributedSnapshots > _snapshots
Distributed container for snapshots per variable.
uint8_t processor_id_type
bool _empty_operators
Flag describing if the reduced operators are empty or not.
virtual void initialize() override
std::vector< std::string > & _tag_types
list of bools describing which tag is indepedent of the solution.
const std::vector< std::string > & getTagNames() const
std::vector< DenseVector< Real > > _eigenvalues
The eigenvalues of the correalation matrix for each variable.
unsigned int getVariableIndex(unsigned int glob_i) const
Getting appropriate variable index for a global base index.
std::vector< DenseMatrix< Real > > _corr_mx
The correlation matrices for the variables.
std::vector< DenseMatrix< Real > > & _red_operators
The reduced operators that should be transferred to the surrogate.
unsigned int getSnapsSize(unsigned int var_i) const
Getting the snapshot size across all of the processors for a given variable with index var_i...
unsigned int determineNumberOfModes(Real error, const std::vector< Real > &inp_vec) const
Computes the number of bases necessary for a given error indicator.
bool _base_completed
Switch that tells if the object has already computed the necessary basis vectors. ...
StochasticTools::DistributedData< std::shared_ptr< DenseVector< Real > > > DistributedSnapshots
const std::vector< std::string > & getTagTypes() const
void computeEigenDecomposition()
Computes the eigen-decomposition of the stored correlation matrices.
const InputParameters & parameters() const
std::vector< std::string > & _tag_names
Names of the tags that should be used to fetch residuals from the MultiApp.
std::vector< std::string > & _var_names
Vector containing the names of the variables we want to use for constructing the surrogates.
unsigned int getBaseSize(unsigned int var_i) const
Getting the base size for variable with index v_ind.
std::vector< Real > _error_res
Energy limits that define how many basis functions will be kept for each variable.
This is the base trainer class whose main functionality is the API for declaring model data...
const std::vector< std::string > & getVarNames() const
Templated class that specifies a distributed storage of a vector of given objects.
unsigned int getSumBaseSize() const
Getting the overall base size, which is the sum of the individual bases.
void computeCorrelationMatrix()
Computes the correlation matrices using the snapshots.
const DenseVector< Real > & getBasisVector(unsigned int var_i, unsigned int base_i) const
Getting a basis vector for a given variable.
void computeBasisVectors()
Generates the basis vectors using the snapshots together with the eigen-decomposition of the correlat...