libROM  v1.0
Data-driven physical simulation library
Vector.h
1 
11 // Description: A simple, parallel Vector class with the utility needed to
12 // support the basis generation methods of this library. A
13 // distributed Vector has its rows distributed across processors.
14 
15 #ifndef included_Vector_h
16 #define included_Vector_h
17 
18 #include "utils/Utilities.h"
19 #include <memory>
20 #include <vector>
21 #include <functional>
22 
23 namespace CAROM {
24 
30 class Vector
31 {
32 public:
33  Vector();
34 
46  Vector(
47  int dim,
48  bool distributed);
49 
66  Vector(
67  double* vec,
68  int dim,
69  bool distributed,
70  bool copy_data = true);
71 
77  Vector(
78  const Vector& other);
79 
83  ~Vector();
84 
92  Vector&
94  const Vector& rhs);
95 
103  Vector&
104  operator += (
105  const Vector& rhs);
106 
114  Vector&
115  operator -= (
116  const Vector& rhs);
117 
126  Vector&
127  operator = (
128  const double& a);
129 
138  Vector&
139  operator *= (
140  const double& a);
141 
150  Vector&
151  transform(std::function<void(const int size, double* vector)> transformer);
152 
162  void
163  transform(Vector& result,
164  std::function<void(const int size, double* vector)> transformer) const;
165 
175  void
176  transform(Vector*& result,
177  std::function<void(const int size, double* vector)> transformer) const;
178 
188  Vector&
189  transform(
190  std::function<void(const int size, double* origVector, double* resultVector)>
191  transformer);
192 
203  void
204  transform(Vector& result,
205  std::function<void(const int size, double* origVector, double* resultVector)>
206  transformer) const;
207 
216  void
218  {
219  if (dim > d_alloc_size) {
220  if (!d_owns_data) {
221  CAROM_ERROR("Can not reallocate externally owned storage.");
222  }
223  if (d_vec) {
224  delete [] d_vec;
225  }
226 
227  // Allocate new array and initialize all values to zero.
228  d_vec = new double [dim] {0.0};
229  d_alloc_size = dim;
230  }
231  d_dim = dim;
232  }
233 
239  bool
240  distributed() const
241  {
242  return d_distributed;
243  }
244 
250  int
251  dim() const
252  {
253  return d_dim;
254  }
255 
268  double
270  const Vector& other) const;
271 
279  double
280  norm() const;
281 
289  double
290  norm2() const;
291 
299  double
300  normalize();
301 
312  std::unique_ptr<Vector>
313  plus(const Vector& other) const
314  {
315  Vector *result = new Vector(d_dim, d_distributed);
316  plus(other, *result);
317  return std::unique_ptr<Vector>(result);
318  }
319 
332  void
333  plus(
334  const Vector& other,
335  Vector& result) const;
336 
348  std::unique_ptr<Vector>
350  double factor,
351  const Vector& other) const
352  {
353  Vector *result = new Vector(d_dim, d_distributed);
354  plusAx(factor, other, *result);
355  return std::unique_ptr<Vector>(result);
356  }
357 
371  void
372  plusAx(
373  double factor,
374  const Vector& other,
375  Vector& result) const;
376 
386  void
387  plusEqAx(
388  double factor,
389  const Vector& other);
390 
401  std::unique_ptr<Vector>
403  const Vector& other) const
404  {
405  Vector *result = new Vector(d_dim, d_distributed);
406  minus(other, *result);
407  return std::unique_ptr<Vector>(result);
408  }
409 
422  void
423  minus(
424  const Vector& other,
425  Vector& result) const;
426 
435  std::unique_ptr<Vector>
437  double factor) const
438  {
439  Vector *result = new Vector(d_dim, d_distributed);
440  mult(factor, *result);
441  return std::unique_ptr<Vector>(result);
442  }
443 
453  void
454  mult(
455  double factor,
456  Vector& result) const;
457 
467  const double&
468  item(int i) const
469  {
470  CAROM_ASSERT((0 <= i) && (i < dim()));
471  return d_vec[i];
472  }
473 
485  double&
486  item(int i)
487  {
488  CAROM_ASSERT((0 <= i) && (i < dim()));
489  return d_vec[i];
490  }
491 
501  const double& operator() (int i) const
502  {
503  return item(i);
504  }
505 
517  double& operator() (int i)
518  {
519  return item(i);
520  }
521 
528  void print(const char * prefix) const;
529 
536  void write(const std::string& base_file_name);
537 
544  void read(const std::string& base_file_name);
545 
553  void local_read(const std::string& base_file_name, int rank);
554 
558  double *getData() const
559  {
560  return d_vec;
561  }
562 
570  double localMin(int nmax = 0);
571 
583  void distribute(const int local_dim);
584 
593  void gather();
594 
595 private:
599  double* d_vec;
600 
604  int d_dim;
605 
611  int d_alloc_size;
612 
619  bool d_distributed;
620 
624  int d_num_procs;
625 
629  int d_rank;
630 
637  bool d_owns_data;
638 };
639 
644 int getCenterPoint(const std::vector<const Vector*>& points,
645  bool use_centroid);
646 
651 int getCenterPoint(const std::vector<Vector>& points,
652  bool use_centroid);
653 
658 int getClosestPoint(const std::vector<const Vector*>& points,
659  const Vector & test_point);
660 
665 int getClosestPoint(const std::vector<Vector>& points,
666  const Vector & test_point);
667 }
668 
669 #endif
double * getData() const
Get the vector data as a pointer.
Definition: Vector.h:558
void setSize(int dim)
Sets the length of the vector and reallocates storage if needed. All values are initialized to zero.
Definition: Vector.h:217
double & item(int i)
Non-const Vector member access.
Definition: Vector.h:486
void read(const std::string &base_file_name)
read Vector from (a) HDF file(s).
Definition: Vector.cpp:371
const double & operator()(int i) const
Const Vector member access.
Definition: Vector.h:501
Vector & operator-=(const Vector &rhs)
Subtraction operator.
Definition: Vector.cpp:137
bool distributed() const
Returns true if the Vector is distributed.
Definition: Vector.h:240
void print(const char *prefix) const
print Vector into (a) ascii file(s).
Definition: Vector.cpp:355
void distribute(const int local_dim)
Distribute this vector among MPI processes, based on the specified local dimension....
Definition: Vector.cpp:460
double norm() const
Form the norm of this.
Definition: Vector.cpp:216
double normalize()
Normalizes the Vector and returns its norm.
Definition: Vector.cpp:230
std::unique_ptr< Vector > minus(const Vector &other) const
Subtracts other and this and returns the result.
Definition: Vector.h:402
void write(const std::string &base_file_name)
write Vector into (a) HDF file(s).
Definition: Vector.cpp:325
const double & item(int i) const
Const Vector member access.
Definition: Vector.h:468
std::unique_ptr< Vector > plus(const Vector &other) const
Adds other and this and returns the result.
Definition: Vector.h:313
std::unique_ptr< Vector > plusAx(double factor, const Vector &other) const
Adds factor*other and this and returns the result.
Definition: Vector.h:349
Vector & operator=(const Vector &rhs)
Assignment operator.
Definition: Vector.cpp:117
double inner_product(const Vector &other) const
Inner product, reference form.
Definition: Vector.cpp:196
~Vector()
Destructor.
Definition: Vector.cpp:109
void plusEqAx(double factor, const Vector &other)
Adds factor*other to this.
Definition: Vector.cpp:277
void transform(Vector *&result, std::function< void(const int size, double *vector)> transformer) const
Transform a vector using a supplied function and store the results in another vector.
std::unique_ptr< Vector > mult(double factor) const
Multiplies this by the supplied constant and returns the result.
Definition: Vector.h:436
double norm2() const
Form the squared norm of this.
Definition: Vector.cpp:223
Vector & transform(std::function< void(const int size, double *vector)> transformer)
Transform the vector using a supplied function.
Definition: Vector.cpp:160
Vector & operator+=(const Vector &rhs)
Addition operator.
Definition: Vector.cpp:128
double localMin(int nmax=0)
Compute the local minimum of this.
Definition: Vector.cpp:445
void local_read(const std::string &base_file_name, int rank)
read read a single rank of a distributed Vector from (a) HDF file(s).
Definition: Vector.cpp:412
int dim() const
Returns the dimension of the Vector on this processor.
Definition: Vector.h:251
Vector & operator*=(const double &a)
Scaling operator.
Definition: Vector.cpp:153
void gather()
Gather all the distributed elements among MPI processes. This becomes not distributed after this func...
Definition: Vector.cpp:486
int getCenterPoint(const std::vector< const Vector * > &points, bool use_centroid)
Get center point of a group of points.
Definition: Vector.cpp:518
int getClosestPoint(const std::vector< const Vector * > &points, const Vector &test_point)
Get closest point to a test point among a group of points.
Definition: Vector.cpp:585