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.
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: Riccardo Rossi
11 // Raul Bravo
12 //
13 
14 #if !defined(KRATOS_ROM_BUILDER_AND_SOLVER)
15 #define KRATOS_ROM_BUILDER_AND_SOLVER
16 
17 /* System includes */
18 
19 /* External includes */
20 #include "concurrentqueue/concurrentqueue.h"
21 
22 /* Project includes */
23 #include "includes/define.h"
24 #include "includes/model_part.h"
29 
30 /* Application includes */
33 
34 namespace Kratos
35 {
36 
39 
40 
44 
45 
49 
50 
54 
55 
59 
60 template <class TSparseSpace, class TDenseSpace, class TLinearSolver>
61 class ROMBuilderAndSolver : public BuilderAndSolver<TSparseSpace, TDenseSpace, TLinearSolver>
62 {
63 public:
64 
65  //TODO: UPDATE THIS
74 
77  // Class pointer definition
79 
80  // The size_t types
81  typedef std::size_t SizeType;
82  typedef std::size_t IndexType;
83 
86 
99 
104  typedef boost::numeric::ublas::compressed_matrix<double> CompressedMatrixType;
107 
109  typedef Node NodeType;
110  typedef typename NodeType::DofType DofType;
112  typedef moodycamel::ConcurrentQueue<DofType::Pointer> DofQueue;
113 
117 
119  typename TLinearSolver::Pointer pNewLinearSystemSolver,
120  Parameters ThisParameters): BaseType(pNewLinearSystemSolver)
121  {
122  // Validate and assign defaults
123  Parameters this_parameters_copy = ThisParameters.Clone();
124  this_parameters_copy = this->ValidateAndAssignParameters(this_parameters_copy, this->GetDefaultParameters());
125  this->AssignSettings(this_parameters_copy);
126  }
127 
129  typename TLinearSolver::Pointer pNewLinearSystemSolver)
130  : BuilderAndSolver<TSparseSpace, TDenseSpace, TLinearSolver>(pNewLinearSystemSolver)
131  {
132  }
133 
134  ~ROMBuilderAndSolver() = default;
135 
139 
140 
144 
145  typename BaseType::Pointer Create(
146  typename TLinearSolver::Pointer pNewLinearSystemSolver,
147  Parameters ThisParameters) const override
148  {
149  return Kratos::make_shared<ClassType>(pNewLinearSystemSolver,ThisParameters);
150  }
151 
153  typename TSchemeType::Pointer pScheme,
154  ModelPart &rModelPart) override
155  {
156  KRATOS_TRY;
157 
158  KRATOS_INFO_IF("ROMBuilderAndSolver", (this->GetEchoLevel() > 1)) << "Setting up the dofs" << std::endl;
159  KRATOS_INFO_IF("ROMBuilderAndSolver", (this->GetEchoLevel() > 2)) << "Number of threads" << ParallelUtilities::GetNumThreads() << "\n" << std::endl;
160  KRATOS_INFO_IF("ROMBuilderAndSolver", (this->GetEchoLevel() > 2)) << "Initializing element loop" << std::endl;
161 
162  // Get model part data
163  if (mHromWeightsInitialized == false) {
164  InitializeHROMWeights(rModelPart);
165  }
166 
167  auto dof_queue = ExtractDofSet(pScheme, rModelPart);
168 
169  // Fill a sorted auxiliary array of with the DOFs set
170  KRATOS_INFO_IF("ROMBuilderAndSolver", (this->GetEchoLevel() > 2)) << "Initializing ordered array filling\n" << std::endl;
171  auto dof_array = SortAndRemoveDuplicateDofs(dof_queue);
172 
173  // Update base builder and solver DOFs array and set corresponding flag
174  BaseType::GetDofSet().swap(dof_array);
176 
177  // Throw an exception if there are no DOFs involved in the analysis
178  KRATOS_ERROR_IF(BaseType::GetDofSet().size() == 0) << "No degrees of freedom!" << std::endl;
179  KRATOS_INFO_IF("ROMBuilderAndSolver", (this->GetEchoLevel() > 2)) << "Number of degrees of freedom:" << BaseType::GetDofSet().size() << std::endl;
180  KRATOS_INFO_IF("ROMBuilderAndSolver", (this->GetEchoLevel() > 2)) << "Finished setting up the dofs" << std::endl;
181 
182 #ifdef KRATOS_DEBUG
183  // If reactions are to be calculated, we check if all the dofs have reactions defined
185  {
186  for (const auto& r_dof: BaseType::GetDofSet())
187  {
188  KRATOS_ERROR_IF_NOT(r_dof.HasReaction())
189  << "Reaction variable not set for the following :\n"
190  << "Node : " << r_dof.Id() << '\n'
191  << "Dof : " << r_dof << '\n'
192  << "Not possible to calculate reactions." << std::endl;
193  }
194  }
195 #endif
196  KRATOS_CATCH("");
197  }
198 
200  typename TSchemeType::Pointer pScheme,
201  ModelPart& rModelPart,
202  TSystemMatrixType& rA,
203  TSystemVectorType& rDx,
204  TSystemVectorType& rb) override
205  {
206  TSparseSpace::SetToZero(rb);
207 
208  //refresh RHS to have the correct reactions (with contributions on Dirichlet BCs)
209  BuildRHSNoDirichlet(rModelPart, rb);
210 
211  //NOTE: dofs are assumed to be numbered consecutively in the BuilderAndSolver
213  const std::size_t i = rDof.EquationId();
214 
215  rDof.GetSolutionStepReactionValue() = -rb[i];
216  });
217  }
218 
219  void SetUpSystem(ModelPart &rModelPart) override
220  {
221  auto& r_dof_set = BaseType::GetDofSet();
222  BaseType::mEquationSystemSize = r_dof_set.size();
223  IndexPartition<IndexType>(r_dof_set.size()).for_each([&](IndexType Index)
224  {
225  auto dof_iterator = r_dof_set.begin() + Index;
226  dof_iterator->SetEquationId(Index);
227  });
228  }
229 
230  // Vector ProjectToReducedBasis(
231  // const TSystemVectorType& rX,
232  // ModelPart::NodesContainerType& rNodes
233  // )
234  // {
235  // Vector rom_unknowns = ZeroVector(GetNumberOfROMModes());
236  // for(const auto& node : rNodes)
237  // {
238  // unsigned int node_aux_id = node.GetValue(AUX_ID);
239  // const auto& nodal_rom_basis = node.GetValue(ROM_BASIS);
240  // for (int i = 0; i < GetNumberOfROMModes(); ++i) {
241  // for (int j = 0; j < mNodalDofs; ++j) {
242  // rom_unknowns[i] += nodal_rom_basis(j, i)*rX(node_aux_id*mNodalDofs + j);
243  // }
244  // }
245  // }
246  // return rom_unknowns;
247  // }
248 
250  {
251  return mNumberOfRomModes;
252  }
253 
255  const TSystemVectorType& rRomUnkowns,
256  const ModelPart& rModelPart,
257  TSystemVectorType& rDx) const
258  {
259  const auto& r_dof_set = BaseType::GetDofSet();
260  block_for_each(r_dof_set, [&](const DofType& r_dof)
261  {
262  const auto& r_node = rModelPart.GetNode(r_dof.Id());
263  const Matrix& r_rom_nodal_basis = r_node.GetValue(ROM_BASIS);
264  const Matrix::size_type row_id = mMapPhi.at(r_dof.GetVariable().Key());
265  rDx[r_dof.EquationId()] = inner_prod(row(r_rom_nodal_basis, row_id), rRomUnkowns);
266  });
267  }
268 
270  ModelPart& rModelPart,
271  TSystemMatrixType& rA,
272  TSystemVectorType& rDx,
273  TSystemVectorType& rb) override
274  {
275  // Call the base B&S InitializeSolutionStep
276  BaseType::InitializeSolutionStep(rModelPart, rA, rDx, rb);
277 
278  // Reset the ROM solution increment in the root modelpart database
279  auto& r_root_mp = rModelPart.GetRootModelPart();
280  r_root_mp.GetValue(ROM_SOLUTION_INCREMENT) = ZeroVector(GetNumberOfROMModes());
281  }
282 
283 
285  typename TSchemeType::Pointer pScheme,
286  ModelPart &rModelPart,
288  TSystemVectorType &Dx,
289  TSystemVectorType &b) override
290  {
291  KRATOS_TRY
292 
295 
296  BuildROM(pScheme, rModelPart, Arom, brom);
297  SolveROM(rModelPart, Arom, brom, Dx);
298 
299  KRATOS_CATCH("")
300  }
301 
303  typename TSchemeType::Pointer pScheme,
307  ModelPart &rModelPart) override
308  {
309  KRATOS_TRY
310 
311  // If not initialized, initalize the system arrays to an empty vector/matrix
312  if (!pA) {
313  TSystemMatrixPointerType p_new_A = Kratos::make_shared<TSystemMatrixType>(0, 0);
314  pA.swap(p_new_A);
315  }
316  if (!pDx) {
317  TSystemVectorPointerType p_new_Dx = Kratos::make_shared<TSystemVectorType>(0);
318  pDx.swap(p_new_Dx);
319  }
320  if (!pb) {
321  TSystemVectorPointerType p_new_b = Kratos::make_shared<TSystemVectorType>(0);
322  pb.swap(p_new_b);
323  }
324 
325  TSystemVectorType& r_Dx = *pDx;
326  if (r_Dx.size() != BaseType::GetEquationSystemSize()) {
327  r_Dx.resize(BaseType::GetEquationSystemSize(), false);
328  }
329 
330  TSystemVectorType& r_b = *pb;
331  if (r_b.size() != BaseType::GetEquationSystemSize()) {
332  r_b.resize(BaseType::GetEquationSystemSize(), false);
333  }
334 
335  KRATOS_CATCH("")
336  }
337 
339  {
340  Parameters default_parameters = Parameters(R"(
341  {
342  "name" : "rom_builder_and_solver",
343  "nodal_unknowns" : [],
344  "number_of_rom_dofs" : 10,
345  "rom_bns_settings" : {}
346  })");
348 
349  return default_parameters;
350  }
351 
352  static std::string Name()
353  {
354  return "rom_builder_and_solver";
355  }
356 
360 
361 
365 
366 
370 
372  virtual std::string Info() const override
373  {
374  return "ROMBuilderAndSolver";
375  }
376 
378  virtual void PrintInfo(std::ostream &rOStream) const override
379  {
380  rOStream << Info();
381  }
382 
384  virtual void PrintData(std::ostream &rOStream) const override
385  {
386  rOStream << Info();
387  }
388 
392 
393 
395 protected:
399 
400 
404 
406  std::unordered_map<Kratos::VariableData::KeyType, Matrix::size_type> mMapPhi;
407 
410 
411  bool mHromSimulation = false;
413 
417 
419  ModelPart& rModelPart,
420  TSystemVectorType& rb)
421  {
422  KRATOS_TRY
423 
424  // Get ProcessInfo from main model part
425  const auto& r_current_process_info = rModelPart.GetProcessInfo();
426 
427  auto& r_elements = mHromSimulation ? mSelectedElements : rModelPart.Elements();
428  if(!r_elements.empty())
429  {
430  block_for_each(r_elements, Kratos::Vector(), [&](Element& r_element, Kratos::Vector& r_rhs_elem)
431  {
433 
434  r_element.CalculateRightHandSide(r_rhs_elem, r_current_process_info);
435  r_element.GetDofList(dofs, r_current_process_info);
436  for (IndexType i = 0; i < dofs.size(); ++i){
437  double& r_bi = rb[dofs[i]->EquationId()];
438  AtomicAdd(r_bi, r_rhs_elem[i]); // Building RHS.
439  }
440  });
441  }
442 
443  auto& r_conditions = mHromSimulation ? mSelectedConditions : rModelPart.Conditions();
444  if(!r_conditions.empty())
445  {
446  block_for_each(r_conditions, Kratos::Vector(), [&](Condition& r_condition, Kratos::Vector& r_rhs_cond)
447  {
448  DofsVectorType dofs = {};
449  r_condition.CalculateRightHandSide(r_rhs_cond, r_current_process_info);
450  r_condition.GetDofList(dofs, r_current_process_info);
451  for (IndexType i = 0; i < dofs.size(); ++i){
452  double& r_bi = rb[dofs[i]->EquationId()];
453  AtomicAdd(r_bi, r_rhs_cond[i]); // Building RHS.
454  }
455  });
456  }
457 
458  KRATOS_CATCH("")
459 
460  }
461 
465 
466  void AssignSettings(const Parameters ThisParameters) override
467  {
468  BaseType::AssignSettings(ThisParameters);
469 
470  // Set member variables
471  mNodalDofs = ThisParameters["nodal_unknowns"].size();
472  mNumberOfRomModes = ThisParameters["number_of_rom_dofs"].GetInt();
473 
474  // Set up a map with key the variable key and value the correct row in ROM basis
475  IndexType k = 0;
476  for (const auto& r_var_name : ThisParameters["nodal_unknowns"].GetStringArray()) {
477  if(KratosComponents<Variable<double>>::Has(r_var_name)) {
478  const auto& var = KratosComponents<Variable<double>>::Get(r_var_name);
479  mMapPhi[var.Key()] = k++;
480  } else {
481  KRATOS_ERROR << "Variable \""<< r_var_name << "\" not valid" << std::endl;
482  }
483  }
484  }
485 
486 
488  {
489  KRATOS_TRY
490 
491  using ElementQueue = moodycamel::ConcurrentQueue<Element::Pointer>;
492  using ConditionQueue = moodycamel::ConcurrentQueue<Condition::Pointer>;
493 
494  // Inspecting elements
495  ElementQueue element_queue;
496  block_for_each(rModelPart.Elements().GetContainer(),
497  [&](Element::Pointer p_element)
498  {
499  if (p_element->Has(HROM_WEIGHT)) {
500  element_queue.enqueue(std::move(p_element));
501  } else {
502  p_element->SetValue(HROM_WEIGHT, 1.0);
503  }
504  });
505 
506  // Inspecting conditions
507  ConditionQueue condition_queue;
508  block_for_each(rModelPart.Conditions().GetContainer(),
509  [&](Condition::Pointer p_condition)
510  {
511  if (p_condition->Has(HROM_WEIGHT)) {
512  condition_queue.enqueue(std::move(p_condition));
513  } else {
514  p_condition->SetValue(HROM_WEIGHT, 1.0);
515  }
516  });
517 
518  // Dequeueing elements
519  std::size_t err_id;
520  mSelectedElements.reserve(element_queue.size_approx());
521  Element::Pointer p_element;
522  while ( (err_id = element_queue.try_dequeue(p_element)) != 0) {
523  mSelectedElements.push_back(std::move(p_element));
524  }
525 
526  // Dequeueing conditions
527  mSelectedConditions.reserve(condition_queue.size_approx());
528  Condition::Pointer p_condition;
529  while ( (err_id = condition_queue.try_dequeue(p_condition)) != 0) {
530  mSelectedConditions.push_back(std::move(p_condition));
531  }
532 
533  // Wrap-up
534  mHromSimulation = !(mSelectedElements.empty() && mSelectedConditions.empty());
535  mHromWeightsInitialized = true;
536 
537  KRATOS_CATCH("")
538  }
539 
541  typename TSchemeType::Pointer pScheme,
542  ModelPart& rModelPart)
543  {
544  KRATOS_TRY
545 
546  DofQueue dof_queue;
547 
548  // Emulates ConcurrentQueue::enqueue_bulk adding move semantics to avoid atomic ops
549  const auto enqueue_bulk_move = [](DofQueue& r_queue, DofsVectorType& r_dof_list) {
550  for(auto& p_dof: r_dof_list) {
551  r_queue.enqueue(std::move(p_dof));
552  }
553  r_dof_list.clear();
554  };
555 
556  // Inspecting elements
557  DofsVectorType tls_dof_list; // Preallocation
558  block_for_each(rModelPart.Elements(), tls_dof_list,
559  [&](const Element& r_element, DofsVectorType& r_dof_list)
560  {
561  pScheme->GetDofList(r_element, r_dof_list, rModelPart.GetProcessInfo());
562  enqueue_bulk_move(dof_queue, r_dof_list);
563  });
564 
565  // Inspecting conditions
566  block_for_each(rModelPart.Conditions(), tls_dof_list,
567  [&](const Condition& r_condition, DofsVectorType& r_dof_list)
568  {
569  pScheme->GetDofList(r_condition, r_dof_list, rModelPart.GetProcessInfo());
570  enqueue_bulk_move(dof_queue, r_dof_list);
571  });
572 
573  // Inspecting master-slave constraints
574  std::pair<DofsVectorType, DofsVectorType> tls_ms_dof_lists; // Preallocation
575  block_for_each(rModelPart.MasterSlaveConstraints(), tls_ms_dof_lists,
576  [&](const MasterSlaveConstraint& r_constraint, std::pair<DofsVectorType, DofsVectorType>& r_dof_lists)
577  {
578  r_constraint.GetDofList(r_dof_lists.first, r_dof_lists.second, rModelPart.GetProcessInfo());
579 
580  enqueue_bulk_move(dof_queue, r_dof_lists.first);
581  enqueue_bulk_move(dof_queue, r_dof_lists.second);
582  });
583 
584  return dof_queue;
585 
586  KRATOS_CATCH("")
587  }
588 
590  {
591  KRATOS_TRY
592 
593  DofsArrayType dof_array;
594  dof_array.reserve(rDofQueue.size_approx());
595  DofType::Pointer p_dof;
596  std::size_t err_id;
597  while ( (err_id = rDofQueue.try_dequeue(p_dof)) != 0) {
598  dof_array.push_back(std::move(p_dof));
599  }
600 
601  dof_array.Unique(); // Sorts internally
602 
603  return dof_array;
604 
605  KRATOS_CATCH("")
606  }
607 
611  struct AssemblyTLS
612  {
614  : romA(ZeroMatrix(NRomModes, NRomModes)),
615  romB(ZeroVector(NRomModes))
616  { }
617  AssemblyTLS() = delete;
618 
619  Matrix phiE = {}; // Elemental Phi
620  LocalSystemMatrixType lhs = {}; // Elemental LHS
621  LocalSystemVectorType rhs = {}; // Elemental RHS
622  EquationIdVectorType eq_id = {}; // Elemental equation ID vector
623  DofsVectorType dofs = {}; // Elemental dof vector
624  RomSystemMatrixType romA; // reduced LHS
625  RomSystemVectorType romB; // reduced RHS
626  RomSystemMatrixType aux = {}; // Auxiliary: romA = phi.t * (LHS * phi) := phi.t * aux
627  };
628 
629 
633  template<typename T>
635  {
636  typedef T value_type;
637  typedef T return_type;
638 
640  bool mInitialized = false;
641 
642  void Init(const value_type& first_value)
643  {
644  mValue = first_value;
645  mInitialized = true;
646  }
647 
650  {
651  return mValue;
652  }
653 
654  void LocalReduce(const value_type& value)
655  {
656  if(!mInitialized) {
657  Init(value);
658  } else {
659  noalias(mValue) += value;
660  }
661  }
662 
664  {
665  if(!rOther.mInitialized) return;
666 
667  const std::lock_guard<LockObject> scope_lock(ParallelUtilities::GetGlobalLock());
668  LocalReduce(rOther.mValue);
669  }
670  };
671 
675  template<typename TMatrix>
676  static void ResizeIfNeeded(TMatrix& mat, const SizeType rows, const SizeType cols)
677  {
678  if(mat.size1() != rows || mat.size2() != cols) {
679  mat.resize(rows, cols, false);
680  }
681  }
682 
686  virtual void BuildROM(
687  typename TSchemeType::Pointer pScheme,
688  ModelPart &rModelPart,
691  {
692  KRATOS_TRY
693 
694  // Define a dense matrix to hold the reduced problem
695  rA = ZeroMatrix(GetNumberOfROMModes(), GetNumberOfROMModes());
696  rb = ZeroVector(GetNumberOfROMModes());
697 
698  // Build the system matrix by looping over elements and conditions and assembling to A
699  KRATOS_ERROR_IF(!pScheme) << "No scheme provided!" << std::endl;
700 
701  // Get ProcessInfo from main model part
702  const auto& r_current_process_info = rModelPart.GetProcessInfo();
703 
704 
705  // Assemble all entities
706  const auto assembling_timer = BuiltinTimer();
707 
709  AssemblyTLS assembly_tls_container(GetNumberOfROMModes());
710 
711  auto& elements = mHromSimulation ? mSelectedElements : rModelPart.Elements();
712  if(!elements.empty())
713  {
714  std::tie(rA, rb) =
715  block_for_each<SystemSumReducer>(elements, assembly_tls_container,
716  [&](Element& r_element, AssemblyTLS& r_thread_prealloc)
717  {
718  return CalculateLocalContribution(r_element, r_thread_prealloc, *pScheme, r_current_process_info);
719  });
720  }
721 
722  auto& conditions = mHromSimulation ? mSelectedConditions : rModelPart.Conditions();
723  if(!conditions.empty())
724  {
725  RomSystemMatrixType Aconditions;
726  RomSystemVectorType bconditions;
727 
728  std::tie(Aconditions, bconditions) =
729  block_for_each<SystemSumReducer>(conditions, assembly_tls_container,
730  [&](Condition& r_condition, AssemblyTLS& r_thread_prealloc)
731  {
732  return CalculateLocalContribution(r_condition, r_thread_prealloc, *pScheme, r_current_process_info);
733  });
734 
735  rA += Aconditions;
736  rb += bconditions;
737  }
738 
739  KRATOS_INFO_IF("ROMBuilderAndSolver", (this->GetEchoLevel() > 0)) << "Build time: " << assembling_timer.ElapsedSeconds() << std::endl;
740  KRATOS_INFO_IF("ROMBuilderAndSolver", (this->GetEchoLevel() > 2)) << "Finished parallel building" << std::endl;
741 
742  KRATOS_CATCH("")
743  }
744 
748  virtual void SolveROM(
749  ModelPart &rModelPart,
752  TSystemVectorType &rDx)
753  {
754  KRATOS_TRY
755 
756  RomSystemVectorType dxrom(GetNumberOfROMModes());
757 
758  const auto solving_timer = BuiltinTimer();
759  MathUtils<double>::Solve(rA, dxrom, rb);
760  // KRATOS_WATCH(dxrom)
761  KRATOS_INFO_IF("ROMBuilderAndSolver", (this->GetEchoLevel() > 0)) << "Solve reduced system time: " << solving_timer.ElapsedSeconds() << std::endl;
762 
763  // Save the ROM solution increment in the root modelpart database
764  auto& r_root_mp = rModelPart.GetRootModelPart();
765  noalias(r_root_mp.GetValue(ROM_SOLUTION_INCREMENT)) += dxrom;
766 
767  // project reduced solution back to full order model
768  const auto backward_projection_timer = BuiltinTimer();
769  ProjectToFineBasis(dxrom, rModelPart, rDx);
770  KRATOS_INFO_IF("ROMBuilderAndSolver", (this->GetEchoLevel() > 0)) << "Project to fine basis time: " << backward_projection_timer.ElapsedSeconds() << std::endl;
771 
772  KRATOS_CATCH("")
773  }
774 
778 
779 
783 
784 
788 
789 private:
790 
791  SizeType mNumberOfRomModes;
792 
796 
800  template<typename TEntity>
801  std::tuple<LocalSystemMatrixType, LocalSystemVectorType> CalculateLocalContribution(
802  TEntity& rEntity,
803  AssemblyTLS& rPreAlloc,
804  TSchemeType& rScheme,
805  const ProcessInfo& rCurrentProcessInfo)
806  {
807  if (rEntity.IsDefined(ACTIVE) && rEntity.IsNot(ACTIVE))
808  {
809  rPreAlloc.romA = ZeroMatrix(GetNumberOfROMModes(), GetNumberOfROMModes());
810  rPreAlloc.romB = ZeroVector(GetNumberOfROMModes());
811  return std::tie(rPreAlloc.romA, rPreAlloc.romB);
812  }
813 
814  // Calculate elemental contribution
815  rScheme.CalculateSystemContributions(rEntity, rPreAlloc.lhs, rPreAlloc.rhs, rPreAlloc.eq_id, rCurrentProcessInfo);
816  rEntity.GetDofList(rPreAlloc.dofs, rCurrentProcessInfo);
817 
818  const SizeType ndofs = rPreAlloc.dofs.size();
819  ResizeIfNeeded(rPreAlloc.phiE, ndofs, GetNumberOfROMModes());
820  ResizeIfNeeded(rPreAlloc.aux, ndofs, GetNumberOfROMModes());
821 
822  const auto &r_geom = rEntity.GetGeometry();
823  RomAuxiliaryUtilities::GetPhiElemental(rPreAlloc.phiE, rPreAlloc.dofs, r_geom, mMapPhi);
824 
825  const double h_rom_weight = mHromSimulation ? rEntity.GetValue(HROM_WEIGHT) : 1.0;
826 
827  noalias(rPreAlloc.aux) = prod(rPreAlloc.lhs, rPreAlloc.phiE);
828  noalias(rPreAlloc.romA) = prod(trans(rPreAlloc.phiE), rPreAlloc.aux) * h_rom_weight;
829  noalias(rPreAlloc.romB) = prod(trans(rPreAlloc.phiE), rPreAlloc.rhs) * h_rom_weight;
830 
831  return std::tie(rPreAlloc.romA, rPreAlloc.romB);
832  }
833 
834 
836 }; /* Class ROMBuilderAndSolver */
837 
841 
842 
844 
845 } /* namespace Kratos.*/
846 
847 #endif /* KRATOS_ROM_BUILDER_AND_SOLVER defined */
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
virtual void InitializeSolutionStep(ModelPart &rModelPart, TSystemMatrixType &rA, TSystemVectorType &rDx, TSystemVectorType &rb)
It applies certain operations at the system of equations at the beginning of the solution step.
Definition: builder_and_solver.h:553
bool GetCalculateReactionsFlag() const
This method returns the flag mCalculateReactionsFlag.
Definition: builder_and_solver.h:184
TSparseSpace::MatrixType TSystemMatrixType
Definition of the sparse matrix.
Definition: builder_and_solver.h:82
ModelPart::ConditionsContainerType ConditionsArrayType
Definition: builder_and_solver.h:111
virtual void AssignSettings(const Parameters ThisParameters)
This method assigns settings to member variables.
Definition: builder_and_solver.h:780
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
TSparseSpace::VectorPointerType TSystemVectorPointerType
Definition of the pointer to the vector.
Definition: builder_and_solver.h:91
void SetDofSetIsInitializedFlag(bool DofSetIsInitialized)
This method sets the flag mDofSetIsInitialized.
Definition: builder_and_solver.h:211
DofsArrayType mDofSet
Pointer to the linear solver.
Definition: builder_and_solver.h:739
TDenseSpace::VectorType LocalSystemVectorType
The local vector definition.
Definition: builder_and_solver.h:97
unsigned int GetEquationSystemSize() const
This method returns the value mEquationSystemSize.
Definition: builder_and_solver.h:238
TSparseSpace::MatrixPointerType TSystemMatrixPointerType
Definition of the pointer to the sparse matrix.
Definition: builder_and_solver.h:88
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
virtual Parameters GetDefaultParameters() const
This method provides the defaults parameters to avoid conflicts between the different constructors.
Definition: builder_and_solver.h:628
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
Base class for all Conditions.
Definition: condition.h:59
virtual void GetDofList(DofsVectorType &rElementalDofList, const ProcessInfo &rCurrentProcessInfo) const
Definition: condition.h:273
virtual void CalculateRightHandSide(VectorType &rRightHandSideVector, const ProcessInfo &rCurrentProcessInfo)
Definition: condition.h:440
TDataType & GetValue(const Variable< TDataType > &rThisVariable)
Gets the value associated with a given variable.
Definition: data_value_container.h:268
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
EquationIdType EquationId() const
Definition: dof.h:324
TDataType & GetSolutionStepReactionValue(IndexType SolutionStepIndex=0)
Definition: dof.h:278
Base class for all Elements.
Definition: element.h:60
virtual void CalculateRightHandSide(VectorType &rRightHandSideVector, const ProcessInfo &rCurrentProcessInfo)
Definition: element.h:437
std::vector< DofType::Pointer > DofsVectorType
Definition: element.h:100
virtual void GetDofList(DofsVectorType &rElementalDofList, const ProcessInfo &rCurrentProcessInfo) const
Definition: element.h:271
std::vector< std::size_t > EquationIdVectorType
Definition: element.h:98
This class is useful for index iteration over containers.
Definition: parallel_utilities.h:451
void for_each(TUnaryFunction &&f)
Definition: parallel_utilities.h:514
std::size_t size_type
Definition: amatrix_interface.h:57
KratosComponents class encapsulates a lookup table for a family of classes in a generic way.
Definition: kratos_components.h:49
Definition: amatrix_interface.h:497
Definition: amatrix_interface.h:530
A class that implements the interface for different master-slave constraints to be applied on a syste...
Definition: master_slave_constraint.h:76
Various mathematical utilitiy functions.
Definition: math_utils.h:62
This class aims to manage meshes for multi-physics simulations.
Definition: model_part.h:77
MasterSlaveConstraintContainerType & MasterSlaveConstraints(IndexType ThisIndex=0)
Definition: model_part.h:654
ConditionsContainerType & Conditions(IndexType ThisIndex=0)
Definition: model_part.h:1381
ProcessInfo & GetProcessInfo()
Definition: model_part.h:1746
ModelPart & GetRootModelPart()
Definition: model_part.cpp:510
ElementsContainerType & Elements(IndexType ThisIndex=0)
Definition: model_part.h:1189
NodeType & GetNode(IndexType NodeId, IndexType ThisIndex=0)
Definition: model_part.h:433
This class defines the node.
Definition: node.h:65
static int GetNumThreads()
Returns the current number of threads.
Definition: parallel_utilities.cpp:34
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
SizeType size() const
This method returns the total size of the current array parameter.
Definition: kratos_parameters.cpp:993
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
A sorted associative container similar to an STL set, but uses a vector to store pointers to its data...
Definition: pointer_vector_set.h:72
size_type size() const
Returns the number of elements in the container.
Definition: pointer_vector_set.h:502
void swap(PointerVectorSet &rOther)
Swaps the contents of this PointerVectorSet with another.
Definition: pointer_vector_set.h:532
TContainerType & GetContainer()
Definition: pointer_vector_set.h:778
void reserve(int reservedsize)
Reserves memory for a specified number of elements.
Definition: pointer_vector_set.h:733
ProcessInfo holds the current value of different solution parameters.
Definition: process_info.h:59
Definition: rom_builder_and_solver.h:62
virtual void PrintInfo(std::ostream &rOStream) const override
Print information about this object.
Definition: rom_builder_and_solver.h:378
static void ResizeIfNeeded(TMatrix &mat, const SizeType rows, const SizeType cols)
Definition: rom_builder_and_solver.h:676
BaseType::TSystemMatrixType TSystemMatrixType
Definition: rom_builder_and_solver.h:91
virtual void SolveROM(ModelPart &rModelPart, RomSystemMatrixType &rA, RomSystemVectorType &rb, TSystemVectorType &rDx)
Definition: rom_builder_and_solver.h:748
DofType::Pointer DofPointerType
Definition: rom_builder_and_solver.h:111
void InitializeHROMWeights(ModelPart &rModelPart)
Definition: rom_builder_and_solver.h:487
void ProjectToFineBasis(const TSystemVectorType &rRomUnkowns, const ModelPart &rModelPart, TSystemVectorType &rDx) const
Definition: rom_builder_and_solver.h:254
ElementsArrayType mSelectedElements
Definition: rom_builder_and_solver.h:408
ModelPart::MasterSlaveConstraintContainerType MasterSlaveConstraintContainerType
Additional definitions.
Definition: rom_builder_and_solver.h:101
BaseType::TSchemeType TSchemeType
Definition: rom_builder_and_solver.h:89
virtual void PrintData(std::ostream &rOStream) const override
Print object's data.
Definition: rom_builder_and_solver.h:384
BaseType::TSystemVectorType TSystemVectorType
Definition: rom_builder_and_solver.h:92
ConditionsArrayType mSelectedConditions
Definition: rom_builder_and_solver.h:409
NodeType::DofType DofType
Definition: rom_builder_and_solver.h:110
static DofsArrayType SortAndRemoveDuplicateDofs(DofQueue &rDofQueue)
Definition: rom_builder_and_solver.h:589
SizeType GetNumberOfROMModes() const noexcept
Definition: rom_builder_and_solver.h:249
Element::DofsVectorType DofsVectorType
Definition: rom_builder_and_solver.h:103
ROMBuilderAndSolver(typename TLinearSolver::Pointer pNewLinearSystemSolver)
Definition: rom_builder_and_solver.h:128
moodycamel::ConcurrentQueue< DofType::Pointer > DofQueue
Definition: rom_builder_and_solver.h:112
void SetUpSystem(ModelPart &rModelPart) override
It organises the dofset in order to speed up the building phase.
Definition: rom_builder_and_solver.h:219
Node NodeType
DoF types definition.
Definition: rom_builder_and_solver.h:109
Parameters GetDefaultParameters() const override
This method provides the defaults parameters to avoid conflicts between the different constructors.
Definition: rom_builder_and_solver.h:338
virtual void InitializeSolutionStep(ModelPart &rModelPart, TSystemMatrixType &rA, TSystemVectorType &rDx, TSystemVectorType &rb) override
It applies certain operations at the system of equations at the beginning of the solution step.
Definition: rom_builder_and_solver.h:269
BaseType::LocalSystemVectorType LocalSystemVectorType
Definition: rom_builder_and_solver.h:93
void AssignSettings(const Parameters ThisParameters) override
This method assigns settings to member variables.
Definition: rom_builder_and_solver.h:466
BaseType::Pointer Create(typename TLinearSolver::Pointer pNewLinearSystemSolver, Parameters ThisParameters) const override
Create method.
Definition: rom_builder_and_solver.h:145
ROMBuilderAndSolver< TSparseSpace, TDenseSpace, TLinearSolver > ClassType
The definition of the current class.
Definition: rom_builder_and_solver.h:85
BuilderAndSolver< TSparseSpace, TDenseSpace, TLinearSolver > BaseType
Definition of the classes from the base class.
Definition: rom_builder_and_solver.h:88
bool mHromSimulation
Definition: rom_builder_and_solver.h:411
LocalSystemMatrixType RomSystemMatrixType
Definition: rom_builder_and_solver.h:105
BaseType::TSystemMatrixPointerType TSystemMatrixPointerType
Definition: rom_builder_and_solver.h:95
BaseType::ConditionsArrayType ConditionsArrayType
Definition: rom_builder_and_solver.h:98
void ResizeAndInitializeVectors(typename TSchemeType::Pointer pScheme, TSystemMatrixPointerType &pA, TSystemVectorPointerType &pDx, TSystemVectorPointerType &pb, ModelPart &rModelPart) override
This method initializes and resizes the system of equations.
Definition: rom_builder_and_solver.h:302
BaseType::TSystemVectorPointerType TSystemVectorPointerType
Definition: rom_builder_and_solver.h:96
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: rom_builder_and_solver.h:284
std::unordered_map< Kratos::VariableData::KeyType, Matrix::size_type > mMapPhi
Definition: rom_builder_and_solver.h:406
std::size_t IndexType
Definition: rom_builder_and_solver.h:82
void BuildRHSNoDirichlet(ModelPart &rModelPart, TSystemVectorType &rb)
Definition: rom_builder_and_solver.h:418
KRATOS_CLASS_POINTER_DEFINITION(ROMBuilderAndSolver)
virtual void BuildROM(typename TSchemeType::Pointer pScheme, ModelPart &rModelPart, RomSystemMatrixType &rA, RomSystemVectorType &rb)
Definition: rom_builder_and_solver.h:686
std::size_t SizeType
Definition: rom_builder_and_solver.h:81
bool mHromWeightsInitialized
Definition: rom_builder_and_solver.h:412
ROMBuilderAndSolver(typename TLinearSolver::Pointer pNewLinearSystemSolver, Parameters ThisParameters)
Definition: rom_builder_and_solver.h:118
void CalculateReactions(typename TSchemeType::Pointer pScheme, ModelPart &rModelPart, TSystemMatrixType &rA, TSystemVectorType &rDx, TSystemVectorType &rb) override
It computes the reactions of the system.
Definition: rom_builder_and_solver.h:199
static std::string Name()
Definition: rom_builder_and_solver.h:352
LocalSystemVectorType RomSystemVectorType
Definition: rom_builder_and_solver.h:106
boost::numeric::ublas::compressed_matrix< double > CompressedMatrixType
Definition: rom_builder_and_solver.h:104
BaseType::ElementsArrayType ElementsArrayType
Definition: rom_builder_and_solver.h:97
virtual std::string Info() const override
Turn back information as a string.
Definition: rom_builder_and_solver.h:372
Element::EquationIdVectorType EquationIdVectorType
Definition: rom_builder_and_solver.h:102
BaseType::LocalSystemMatrixType LocalSystemMatrixType
Definition: rom_builder_and_solver.h:94
void SetUpDofSet(typename TSchemeType::Pointer pScheme, ModelPart &rModelPart) override
Builds the list of the DofSets involved in the problem by "asking" to each element and condition its ...
Definition: rom_builder_and_solver.h:152
SizeType mNodalDofs
Definition: rom_builder_and_solver.h:405
static DofQueue ExtractDofSet(typename TSchemeType::Pointer pScheme, ModelPart &rModelPart)
Definition: rom_builder_and_solver.h:540
BaseType::DofsArrayType DofsArrayType
Definition: rom_builder_and_solver.h:90
This class provides the implementation of the basic tasks that are needed by the solution strategy.
Definition: scheme.h:56
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
Definition: exception.h:161
#define KRATOS_ERROR_IF_NOT(conditional)
Definition: exception.h:163
#define KRATOS_ERROR_IF(conditional)
Definition: exception.h:162
#define KRATOS_INFO_IF(label, conditional)
Definition: logger.h:251
bool Has(const std::string &ModelerName)
Checks if the modeler is registered.
Definition: modeler_factory.cpp:24
std::size_t SizeType
Definition: nurbs_utilities.h:41
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
KratosZeroVector< double > ZeroVector
Definition: amatrix_interface.h:561
void AtomicAdd(TDataType &target, const TDataType &value)
Definition: atomic_utilities.h:55
KratosZeroMatrix< double > ZeroMatrix
Definition: amatrix_interface.h:559
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
AMatrix::MatrixProductExpression< TExpression1Type, TExpression2Type > prod(AMatrix::MatrixExpression< TExpression1Type, TCategory1 > const &First, AMatrix::MatrixExpression< TExpression2Type, TCategory2 > const &Second)
Definition: amatrix_interface.h:568
TExpression1Type::data_type inner_prod(AMatrix::MatrixExpression< TExpression1Type, TCategory1 > const &First, AMatrix::MatrixExpression< TExpression2Type, TCategory2 > const &Second)
Definition: amatrix_interface.h:592
std::size_t SizeType
The definition of the size type.
Definition: mortar_classes.h:43
T & noalias(T &TheMatrix)
Definition: amatrix_interface.h:484
AMatrix::MatrixRow< const TExpressionType > row(AMatrix::MatrixExpression< TExpressionType, TCategory > const &TheExpression, std::size_t RowIndex)
Definition: amatrix_interface.h:649
AMatrix::TransposeMatrix< const T > trans(const T &TheMatrix)
Definition: amatrix_interface.h:486
rhs
Definition: generate_frictional_mortar_condition.py:297
lhs
Definition: generate_frictional_mortar_condition.py:297
dofs
Enforced auxTangentSlipNonObjective = delta_time * gap_time_derivative_non_objective....
Definition: generate_frictional_mortar_condition.py:210
b
Definition: generate_total_lagrangian_mixed_volumetric_strain_element.py:31
def Index()
Definition: hdf5_io_tools.py:38
tuple const
Definition: ode_solve.py:403
int k
Definition: quadrature.py:595
mat
Definition: script_ELASTIC.py:155
A
Definition: sensitivityMatrix.py:70
integer i
Definition: TensorModule.f:17
Definition: reduction_utilities.h:310
Definition: rom_builder_and_solver.h:612
AssemblyTLS(SizeType NRomModes)
Definition: rom_builder_and_solver.h:613
RomSystemMatrixType romA
Definition: rom_builder_and_solver.h:624
RomSystemVectorType romB
Definition: rom_builder_and_solver.h:625
Definition: rom_builder_and_solver.h:635
bool mInitialized
Definition: rom_builder_and_solver.h:640
T value_type
Definition: rom_builder_and_solver.h:636
T mValue
Definition: rom_builder_and_solver.h:639
void Init(const value_type &first_value)
Definition: rom_builder_and_solver.h:642
void LocalReduce(const value_type &value)
Definition: rom_builder_and_solver.h:654
T return_type
Definition: rom_builder_and_solver.h:637
return_type GetValue() const
access to reduced value
Definition: rom_builder_and_solver.h:649
void ThreadSafeReduce(const NonTrivialSumReduction &rOther)
Definition: rom_builder_and_solver.h:663