libROM  v1.0
Data-driven physical simulation library
Utilities.cpp
1 
11 #include "Utilities.hpp"
12 
13 namespace CAROM {
14 
15 void ComputeCtAB(const HypreParMatrix& A,
16  const CAROM::Matrix& B, // Distributed matrix
17  const CAROM::Matrix& C, // Distributed matrix
18  CAROM::Matrix& CtAB) // Non-distributed (local) matrix
19 {
20  MFEM_VERIFY(B.distributed() && C.distributed() && !CtAB.distributed(),
21  "In ComputeCtAB, B and C must be distributed, but not CtAB.");
22 
23  const int num_rows = B.numRows();
24  const int num_cols = B.numColumns();
25  const int num_rows_A = A.NumRows();
26 
27  MFEM_VERIFY(C.numRows() == num_rows_A, "");
28 
29  mfem::Vector Bvec(num_rows);
30  mfem::Vector ABvec(num_rows_A);
31 
32  CAROM::Matrix AB(num_rows_A, num_cols, true);
33 
34  for (int i = 0; i < num_cols; ++i) {
35  for (int j = 0; j < num_rows; ++j) {
36  Bvec[j] = B(j, i);
37  }
38  A.Mult(Bvec, ABvec);
39  for (int j = 0; j < num_rows_A; ++j) {
40  AB(j, i) = ABvec[j];
41  }
42  }
43 
44  C.transposeMult(AB, CtAB);
45 }
46 
47 void ComputeCtAB_vec(const HypreParMatrix& A,
48  const HypreParVector& B, // Distributed vector
49  const CAROM::Matrix& C, // Distributed matrix
50  CAROM::Vector& CtAB_vec) // Non-distributed (local) vector
51 {
52  MFEM_VERIFY(C.distributed() && !CtAB_vec.distributed(),
53  "In ComputeCtAB_vec, C must be distributed, but not CtAB_vec");
54 
55  MFEM_VERIFY(C.numRows() == A.NumRows(), "");
56  MFEM_VERIFY(B.GlobalSize() == A.GetGlobalNumRows(), "");
57 
58  HypreParVector* AB = new HypreParVector(B);
59  A.Mult(B, *AB);
60 
61  CAROM::Vector AB_carom(AB->GetData(), AB->Size(), true);
62  C.transposeMult(AB_carom, CtAB_vec);
63 }
64 
65 void verify_within_portion(const mfem::Vector &bb_min,
66  const mfem::Vector &bb_max,
67  const mfem::Vector &t, const double limit)
68 {
69  // helper function to check if t is within limit percentage relative
70  // to the center of the mesh
71  CAROM_VERIFY(t.Size() == bb_min.Size() && bb_min.Size() == bb_max.Size());
72  CAROM_VERIFY(limit >= 0.0 && limit <= 1.0);
73  for (int i = 0; i < t.Size(); i++)
74  {
75  double domain_limit = limit * (bb_max[i] - bb_min[i]);
76  double mesh_center = 0.5 * (bb_max[i] + bb_min[i]);
77 
78  // check that t is within the limit relative to the center of the mesh
79  if (std::abs((t(i) - mesh_center)) - (0.5 * domain_limit) > 1.0e-14)
80  {
81  std::cerr << "Error: value of t exceeds domain limit: t = " << t(
82  i) << ", limit = " << 0.5 * domain_limit << "\n";
83  exit(-1);
84  }
85  }
86 }
87 
88 double map_to_ref_mesh(const double &bb_min, const double &bb_max,
89  const double &fraction)
90 {
91  // helper function to map a fractional value from [-1, 1] to [bb_min, bb_max]
92  CAROM_VERIFY(fraction <= 1.0 && fraction >= -1.0);
93  return bb_min + (fraction + 1.0) * ((bb_max - bb_min) * 0.5);
94 }
95 
96 double map_from_ref_mesh(const double &bb_min, const double &bb_max,
97  const double &value)
98 {
99  // helper function to map a value from the mesh range [bb_min, bb_max] to [-1, 1]
100  CAROM_VERIFY(value <= bb_max && value >= bb_min);
101  return -1.0 + (value - bb_min) * ((2.0) / (bb_max - bb_min));
102 }
103 
104 } // end namespace CAROM
bool distributed() const
Returns true if the Matrix is distributed.
Definition: Matrix.h:177
int numColumns() const
Returns the number of columns in the Matrix. This method will return the same value from each process...
Definition: Matrix.h:220
Matrix * transposeMult(const Matrix &other) const
Multiplies the transpose of this Matrix with other and returns the product, reference version.
Definition: Matrix.h:642
int numRows() const
Returns the number of rows of the Matrix on this processor.
Definition: Matrix.h:194
bool distributed() const
Returns true if the Vector is distributed.
Definition: Vector.h:255
double map_to_ref_mesh(const double &bb_min, const double &bb_max, const double &fraction)
Maps a value from [-1, 1] to the corresponding mesh domain [bb_min, bb_max].
Definition: Utilities.cpp:88
void ComputeCtAB(const HypreParMatrix &A, const CAROM::Matrix &B, const CAROM::Matrix &C, CAROM::Matrix &CtAB)
This function computes a reduced, non-distributed matrix C^t AB stored identically (redundantly) on e...
Definition: Utilities.cpp:15
void ComputeCtAB_vec(const HypreParMatrix &A, const HypreParVector &B, const CAROM::Matrix &C, CAROM::Vector &CtAB_vec)
This function computes a reduced, non-distributed vector C^t AB stored identically (redundantly) on e...
Definition: Utilities.cpp:47
double map_from_ref_mesh(const double &bb_min, const double &bb_max, const double &value)
Maps a value within mesh domain [bb_min, bb_max] to the corresponding value between [-1,...
Definition: Utilities.cpp:96
void verify_within_portion(const mfem::Vector &bb_min, const mfem::Vector &bb_max, const mfem::Vector &t, const double limit)
Helper function to ensure that t is within a given percentage of the domain relative to the center of...
Definition: Utilities.cpp:65