KratosMultiphysics
KRATOS Multiphysics (Kratos) is a framework for building parallel, multi-disciplinary simulation software, aiming at modularity, extensibility, and high performance. Kratos is written in C++, and counts with an extensive Python interface.
global_petrov_galerkin_rom_builder_and_solver.h
Go to the documentation of this file.
1 // | / |
2 // ' / __| _` | __| _ \ __|
3 // . \ | ( | | ( |\__ `
4 // _|\_\_| \__,_|\__|\___/ ____/
5 // Multi-Physics
6 //
7 // License: BSD License
8 // Kratos default license: kratos/license.txt
9 //
10 // Main authors: Sebastian Ares de Parga Regalado
11 //
12 
13 #pragma once
14 
15 /* System includes */
16 
17 /* External includes */
18 #include <Eigen/Core>
19 #include <Eigen/Dense>
20 
21 /* Project includes */
22 #include "includes/define.h"
23 #include "includes/model_part.h"
28 
29 /* Application includes */
33 
34 namespace Kratos
35 {
36 
39 
40 
44 
45 
49 
50 
54 
55 
59 
79 template <class TSparseSpace, class TDenseSpace, class TLinearSolver>
80 class GlobalPetrovGalerkinROMBuilderAndSolver : public GlobalROMBuilderAndSolver<TSparseSpace, TDenseSpace, TLinearSolver>
81 {
82 public:
83 
86 
87  // Class pointer definition
89 
90  // The size_t types
91  using SizeType = std::size_t;
92  using IndexType = std::size_t;
93 
96 
107 
108  // Eigen definitions
109  using EigenDynamicMatrix = Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>;
110  using EigenDynamicVector = Eigen::Matrix<double, Eigen::Dynamic, 1>;
111  using EigenSparseMatrix = Eigen::SparseMatrix<double, Eigen::RowMajor, int>;
112 
114  using DofType = typename Node::DofType;
116  using DofQueue = moodycamel::ConcurrentQueue<DofType::Pointer>;
117 
118  // Bring the base class function into scope
119  using BaseType::ProjectROM;
120 
124 
126  typename TLinearSolver::Pointer pNewLinearSystemSolver,
127  Parameters ThisParameters) : BaseType(pNewLinearSystemSolver)
128  {
129  // Validate and assign defaults
130  Parameters this_parameters_copy = ThisParameters.Clone();
131  this_parameters_copy = BaseType::ValidateAndAssignParameters(this_parameters_copy, GetDefaultParameters());
132  AssignSettings(this_parameters_copy);
133  }
134 
136 
140 
141 
145 
149  virtual void BuildAndProjectROM(
150  typename TSchemeType::Pointer pScheme,
151  ModelPart &rModelPart,
152  TSystemMatrixType &rA,
153  TSystemVectorType &rb,
154  TSystemVectorType &rDx) override
155  {
156  KRATOS_TRY
157 
158  KRATOS_ERROR_IF(!pScheme) << "No scheme provided!" << std::endl;
159 
160  const auto assembling_timer = BuiltinTimer();
161 
164  BaseType::ConstructMatrixStructure(pScheme, rA, rModelPart);
165  }
166 
167  BaseType::Build(pScheme, rModelPart, rA, rb);
168 
171  }
172 
173  ProjectROM(rModelPart, rA, rb);
174 
175  double time = assembling_timer.ElapsedSeconds();
176  KRATOS_INFO_IF("GlobalPetrovGalerkinROMBuilderAndSolver", (BaseType::GetEchoLevel() > 0)) << "Build and project time: " << time << std::endl;
177 
178  KRATOS_CATCH("")
179  }
180 
182  const ModelPart& rModelPart,
183  Matrix& rPhiGlobal
184  )
185  {
186  BaseType::BuildRightROMBasis(rModelPart, rPhiGlobal);
187  }
188 
192  virtual void ProjectROM(
193  ModelPart &rModelPart,
194  TSystemMatrixType &rA,
196  ) override
197  {
198  KRATOS_TRY
199 
200  if (mRightRomBasisInitialized==false){
202  mRightRomBasisInitialized = true;
203  }
204 
205  if (mLeftRomBasisInitialized==false){
206  mPsiGlobal = ZeroMatrix(BaseBuilderAndSolverType::mEquationSystemSize, mNumberOfPetrovGalerkinRomModes);
207  mLeftRomBasisInitialized = true;
208  }
209 
210  BaseType::BuildRightROMBasis(rModelPart, mPhiGlobal);
211  BuildLeftROMBasis(rModelPart, mPsiGlobal);
212  auto a_wrapper = UblasWrapper<double>(rA);
213  const auto& eigen_rA = a_wrapper.matrix();
214  Eigen::Map<EigenDynamicVector> eigen_rb(rb.data().begin(), rb.size());
215  Eigen::Map<EigenDynamicMatrix> eigen_mPhiGlobal(mPhiGlobal.data().begin(), mPhiGlobal.size1(), mPhiGlobal.size2());
216  Eigen::Map<EigenDynamicMatrix> eigen_mPsiGlobal(mPsiGlobal.data().begin(), mPsiGlobal.size1(), mPsiGlobal.size2());
217 
218  EigenDynamicMatrix eigen_rA_times_mPhiGlobal = eigen_rA * eigen_mPhiGlobal; //TODO: Make it in parallel.
219 
220  // Compute the matrix multiplication
221  mEigenRomA = eigen_mPsiGlobal.transpose() * eigen_rA_times_mPhiGlobal; //TODO: Make it in parallel.
222  mEigenRomB = eigen_mPsiGlobal.transpose() * eigen_rb; //TODO: Make it in parallel.
223 
224  KRATOS_CATCH("")
225  }
226 
228  const ModelPart& rModelPart,
229  Matrix& rPsiGlobal)
230  {
231  const auto& r_dof_set = BaseBuilderAndSolverType::GetDofSet();
232  block_for_each(r_dof_set, [&](const DofType& r_dof)
233  {
234  const auto& r_node = rModelPart.GetNode(r_dof.Id());
235  const Matrix& r_rom_nodal_basis = r_node.GetValue(ROM_LEFT_BASIS);
236  const Matrix::size_type row_id = BaseType::mMapPhi.at(r_dof.GetVariable().Key());
237  if (r_dof.IsFixed())
238  {
239  noalias(row(rPsiGlobal, r_dof.EquationId())) = ZeroVector(r_rom_nodal_basis.size2());
240  }
241  else{
242  noalias(row(rPsiGlobal, r_dof.EquationId())) = row(r_rom_nodal_basis, row_id);
243  }
244  });
245  }
246 
248  typename TSchemeType::Pointer pScheme,
249  ModelPart &rModelPart,
251  TSystemVectorType &Dx,
252  TSystemVectorType &b) override
253  {
254  KRATOS_TRY
255 
256  BuildAndProjectROM(pScheme, rModelPart, A, b, Dx);
257 
258  BaseType::SolveROM(rModelPart, mEigenRomA, mEigenRomB, Dx);
259 
260  KRATOS_CATCH("")
261  }
262 
264  {
265  Parameters default_parameters = Parameters(R"(
266  {
267  "name" : "global_petrov_galerkin_rom_builder_and_solver",
268  "nodal_unknowns" : [],
269  "number_of_rom_dofs" : 10,
270  "petrov_galerkin_number_of_rom_dofs" : 10,
271  "rom_bns_settings": {
272  "monotonicity_preserving" : false
273  }
274  })");
276 
277  return default_parameters;
278  }
279 
280  static std::string Name()
281  {
282  return "global_petrov_galerkin_rom_builder_and_solver";
283  }
284 
288 
289 
293 
294 
298 
300  virtual std::string Info() const override
301  {
302  return "GlobalPetrovGalerkinROMBuilderAndSolver";
303  }
304 
306  virtual void PrintInfo(std::ostream &rOStream) const override
307  {
308  rOStream << Info();
309  }
310 
312  virtual void PrintData(std::ostream &rOStream) const override
313  {
314  rOStream << Info();
315  }
316 
320 
322 protected:
323 
327 
328 
332 
336 
337 
341 
342  void AssignSettings(const Parameters ThisParameters) override
343  {
344  BaseType::AssignSettings(ThisParameters);
345 
346  // // Set member variables
347  mNumberOfPetrovGalerkinRomModes = ThisParameters["petrov_galerkin_number_of_rom_dofs"].GetInt();
348  }
349 
353 
354 
358 
359 
363 
364 private:
365  SizeType mNumberOfPetrovGalerkinRomModes;
366  EigenDynamicMatrix mEigenRomA;
367  EigenDynamicVector mEigenRomB;
368  Matrix mPhiGlobal;
369  Matrix mPsiGlobal;
370  bool mRightRomBasisInitialized = false;
371  bool mLeftRomBasisInitialized = false;
372  std::unordered_set<std::size_t> mSelectedDofs;
373  bool mIsSelectedDofsInitialized = false;
374 
378 
380 }; /* Class GlobalPetrovGalerkinROMBuilderAndSolver */
381 
385 
386 
388 
389 } /* namespace Kratos.*/
Current class provides an implementation for the base builder and solving operations.
Definition: builder_and_solver.h:64
std::size_t IndexType
Definition of the index type.
Definition: builder_and_solver.h:76
TSparseSpace::VectorType TSystemVectorType
Definition of the vector size.
Definition: builder_and_solver.h:85
TSparseSpace::MatrixType TSystemMatrixType
Definition of the sparse matrix.
Definition: builder_and_solver.h:82
ModelPart::ConditionsContainerType ConditionsArrayType
Definition: builder_and_solver.h:111
virtual Parameters ValidateAndAssignParameters(Parameters ThisParameters, const Parameters DefaultParameters) const
This method validate and assign default parameters.
Definition: builder_and_solver.h:767
TDenseSpace::MatrixType LocalSystemMatrixType
The local matrix definition.
Definition: builder_and_solver.h:94
TDenseSpace::VectorType LocalSystemVectorType
The local vector definition.
Definition: builder_and_solver.h:97
std::size_t SizeType
Definition of the size type.
Definition: builder_and_solver.h:73
int GetEchoLevel() const
It returns the echo level.
Definition: builder_and_solver.h:674
virtual DofsArrayType & GetDofSet()
It allows to get the list of Dofs from the element.
Definition: builder_and_solver.h:507
unsigned int mEquationSystemSize
Flag taking in account if it is needed or not to calculate the reactions.
Definition: builder_and_solver.h:747
ModelPart::ElementsContainerType ElementsArrayType
Definition: builder_and_solver.h:110
Definition: builtin_timer.h:26
Dof represents a degree of freedom (DoF).
Definition: dof.h:86
const VariableData & GetVariable() const
Definition: dof.h:303
IndexType Id() const
Definition: dof.h:292
bool IsFixed() const
Definition: dof.h:376
This class provides an implementation for the GlobalPetrovGalerkinROM builder and solver operations....
Definition: global_petrov_galerkin_rom_builder_and_solver.h:81
GlobalPetrovGalerkinROMBuilderAndSolver(typename TLinearSolver::Pointer pNewLinearSystemSolver, Parameters ThisParameters)
Definition: global_petrov_galerkin_rom_builder_and_solver.h:125
void BuildLeftROMBasis(const ModelPart &rModelPart, Matrix &rPsiGlobal)
Definition: global_petrov_galerkin_rom_builder_and_solver.h:227
virtual void PrintData(std::ostream &rOStream) const override
Print object's.
Definition: global_petrov_galerkin_rom_builder_and_solver.h:312
virtual void BuildAndProjectROM(typename TSchemeType::Pointer pScheme, ModelPart &rModelPart, TSystemMatrixType &rA, TSystemVectorType &rb, TSystemVectorType &rDx) override
Definition: global_petrov_galerkin_rom_builder_and_solver.h:149
Parameters GetDefaultParameters() const override
This method provides the defaults parameters to avoid conflicts between the different constructors.
Definition: global_petrov_galerkin_rom_builder_and_solver.h:263
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > EigenDynamicMatrix
Definition: global_petrov_galerkin_rom_builder_and_solver.h:109
Eigen::Matrix< double, Eigen::Dynamic, 1 > EigenDynamicVector
Definition: global_petrov_galerkin_rom_builder_and_solver.h:110
static std::string Name()
Definition: global_petrov_galerkin_rom_builder_and_solver.h:280
Eigen::SparseMatrix< double, Eigen::RowMajor, int > EigenSparseMatrix
Definition: global_petrov_galerkin_rom_builder_and_solver.h:111
virtual void PrintInfo(std::ostream &rOStream) const override
Print information about this object.
Definition: global_petrov_galerkin_rom_builder_and_solver.h:306
void BuildAndSolve(typename TSchemeType::Pointer pScheme, ModelPart &rModelPart, TSystemMatrixType &A, TSystemVectorType &Dx, TSystemVectorType &b) override
Function to perform the building and solving phase at the same time.
Definition: global_petrov_galerkin_rom_builder_and_solver.h:247
void GetRightROMBasis(const ModelPart &rModelPart, Matrix &rPhiGlobal)
Definition: global_petrov_galerkin_rom_builder_and_solver.h:181
KRATOS_CLASS_POINTER_DEFINITION(GlobalPetrovGalerkinROMBuilderAndSolver)
moodycamel::ConcurrentQueue< DofType::Pointer > DofQueue
Definition: global_petrov_galerkin_rom_builder_and_solver.h:116
void AssignSettings(const Parameters ThisParameters) override
This method assigns settings to member variables.
Definition: global_petrov_galerkin_rom_builder_and_solver.h:342
virtual void ProjectROM(ModelPart &rModelPart, TSystemMatrixType &rA, TSystemVectorType &rb) override
Definition: global_petrov_galerkin_rom_builder_and_solver.h:192
virtual std::string Info() const override
Turn back information as a string.
Definition: global_petrov_galerkin_rom_builder_and_solver.h:300
Definition: global_rom_builder_and_solver.h:64
bool GetMonotonicityPreservingFlag() const noexcept
Definition: global_rom_builder_and_solver.h:238
virtual void ProjectROM(ModelPart &rModelPart, TSystemMatrixType &rA, TSystemVectorType &rb)
Definition: global_rom_builder_and_solver.h:749
typename BaseBuilderAndSolverType::TSystemVectorType TSystemVectorType
Definition: global_rom_builder_and_solver.h:95
std::unordered_map< Kratos::VariableData::KeyType, Matrix::size_type > mMapPhi
Definition: global_rom_builder_and_solver.h:456
void BuildRightROMBasis(const ModelPart &rModelPart, Matrix &rPhiGlobal)
Definition: global_rom_builder_and_solver.h:258
virtual void SolveROM(ModelPart &rModelPart, EigenDynamicMatrix &rEigenRomA, EigenDynamicVector &rEigenRomB, TSystemVectorType &rDx)
Definition: global_rom_builder_and_solver.h:780
void Build(typename TSchemeType::Pointer pScheme, ModelPart &rModelPart, TSystemMatrixType &A, TSystemVectorType &b) override
Function to perform the build of the LHS and RHS multiplied by its corresponding hrom weight.
Definition: global_rom_builder_and_solver.h:668
SizeType GetNumberOfROMModes() const noexcept
Definition: global_rom_builder_and_solver.h:233
Parameters GetDefaultParameters() const override
This method provides the defaults parameters to avoid conflicts between the different constructors.
Definition: global_rom_builder_and_solver.h:390
void AssignSettings(const Parameters ThisParameters) override
This method assigns settings to member variables.
Definition: global_rom_builder_and_solver.h:471
void MonotonicityPreserving(TSystemMatrixType &rA, TSystemVectorType &rB)
Definition: global_rom_builder_and_solver.h:278
typename BaseBuilderAndSolverType::TSystemMatrixType TSystemMatrixType
Definition: global_rom_builder_and_solver.h:94
typename BaseBuilderAndSolverType::ConditionsArrayType ConditionsArrayType
Definition: global_rom_builder_and_solver.h:101
typename BaseBuilderAndSolverType::TSchemeType TSchemeType
Definition: global_rom_builder_and_solver.h:92
typename BaseBuilderAndSolverType::ElementsArrayType ElementsArrayType
Definition: global_rom_builder_and_solver.h:100
iterator begin()
Definition: amatrix_interface.h:241
std::size_t size_type
Definition: amatrix_interface.h:57
This class aims to manage meshes for multi-physics simulations.
Definition: model_part.h:77
NodeType & GetNode(IndexType NodeId, IndexType ThisIndex=0)
Definition: model_part.h:433
Dof< double > DofType
Dof type.
Definition: node.h:83
This class provides to Kratos a data structure for I/O based on the standard of JSON.
Definition: kratos_parameters.h:59
int GetInt() const
This method returns the integer contained in the current Parameter.
Definition: kratos_parameters.cpp:666
Parameters Clone() const
Generates a clone of the current document.
Definition: kratos_parameters.cpp:397
void AddMissingParameters(const Parameters &rDefaultParameters)
This function is designed to verify that the parameters under testing contain at least all parameters...
Definition: kratos_parameters.cpp:1369
virtual void ConstructMatrixStructure(typename TSchemeType::Pointer pScheme, TSystemMatrixType &A, ModelPart &rModelPart)
Definition: residualbased_block_builder_and_solver.h:1457
This class provides the implementation of the basic tasks that are needed by the solution strategy.
Definition: scheme.h:56
Definition: ublas_wrapper.h:32
KeyType Key() const
Definition: variable_data.h:187
#define KRATOS_CATCH(MoreInfo)
Definition: define.h:110
#define KRATOS_TRY
Definition: define.h:109
#define KRATOS_ERROR_IF(conditional)
Definition: exception.h:162
#define KRATOS_INFO_IF(label, conditional)
Definition: logger.h:251
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
KratosZeroMatrix< double > ZeroMatrix
Definition: amatrix_interface.h:559
Eigen::Matrix< _Scalar, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > EigenDynamicMatrix
Definition: linear_solvers_define.h:32
void block_for_each(TIterator itBegin, TIterator itEnd, TFunction &&rFunction)
Execute a functor on all items of a range in parallel.
Definition: parallel_utilities.h:299
std::size_t SizeType
The definition of the size type.
Definition: mortar_classes.h:43
Eigen::Matrix< _Scalar, Eigen::Dynamic, 1 > EigenDynamicVector
Definition: linear_solvers_define.h:33
time
Definition: face_heat.py:85
b
Definition: generate_total_lagrangian_mixed_volumetric_strain_element.py:31
A
Definition: sensitivityMatrix.py:70