XC Open source finite element analysis program
Vector.h
1 //----------------------------------------------------------------------------
2 // XC program; finite element analysis code
3 // for structural analysis and design.
4 //
5 // Copyright (C) Luis Claudio Pérez Tato
6 //
7 // This program derives from OpenSees <http://opensees.berkeley.edu>
8 // developed by the «Pacific earthquake engineering research center».
9 //
10 // Except for the restrictions that may arise from the copyright
11 // of the original program (see copyright_opensees.txt)
12 // XC is free software: you can redistribute it and/or modify
13 // it under the terms of the GNU General Public License as published by
14 // the Free Software Foundation, either version 3 of the License, or
15 // (at your option) any later version.
16 //
17 // This software is distributed in the hope that it will be useful, but
18 // WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 // GNU General Public License for more details.
21 //
22 //
23 // You should have received a copy of the GNU General Public License
24 // along with this program.
25 // If not, see <http://www.gnu.org/licenses/>.
26 //----------------------------------------------------------------------------
27 /* ****************************************************************** **
28 ** OpenSees - Open System for Earthquake Engineering Simulation **
29 ** Pacific Earthquake Engineering Research Center **
30 ** **
31 ** **
32 ** (C) Copyright 1999, The Regents of the University of California **
33 ** All Rights Reserved. **
34 ** **
35 ** Commercial use of this program without express permission of the **
36 ** University of California, Berkeley, is strictly prohibited. See **
37 ** file 'COPYRIGHT' in main directory for information on usage and **
38 ** redistribution, and for a DISCLAIMER OF ALL WARRANTIES. **
39 ** **
40 ** Developed by: **
41 ** Frank McKenna (fmckenna@ce.berkeley.edu) **
42 ** Gregory L. Fenves (fenves@ce.berkeley.edu) **
43 ** Filip C. Filippou (filippou@ce.berkeley.edu) **
44 ** **
45 ** ****************************************************************** */
46 
47 // $Revision: 1.9 $
48 // $Date: 2005/12/14 23:49:48 $
49 // $Source: /usr/local/cvs/OpenSees/SRC/utility/matrix/Vector.h,v $
50 
51 
52 // File: ~/utility/matrix/Vector.h
53 //
54 // Written: fmk
55 // Created: 11/96
56 // Revision: A
57 //
58 // Description: This file contains the class definition for Vector.
59 // Vector is a concrete class implementing the vector abstraction.
60 //
61 // What: "@(#) Vector.h, revA"
62 
63 #ifndef Vector_h
64 #define Vector_h
65 
66 #include "xc_utils/src/nucleo/EntCmd.h"
67 #include "xc_basic/src/matrices/m_double.h"
68 #include <cmath>
69 
70 class Vector2d;
71 class Vector3d;
72 
73 namespace XC {
74 class ID;
75 
76 class Matrix;
77 class Message;
78 class SystemOfEqn;
79 
81 //
82 class Vector: public EntCmd
83  {
84  private:
85  static double VECTOR_NOT_VALID_ENTRY;
86  int sz;
87  double *theData;
88  int fromFree;
89  void libera(void);
90  void alloc(const size_t &sz);
91  public:
92  // constructors and destructor
93  Vector(void);
94  explicit Vector(const int &, const double &valor= 0.0);
95  explicit Vector(const std::vector<double> &v);
96  explicit Vector(const Vector2d &v);
97  explicit Vector(const Vector3d &v);
98  Vector(const double &,const double &,const double &);
99  Vector(const Vector &);
100  Vector(double *data, int size);
101  Vector(const boost::python::list &);
102  virtual ~Vector(void);
103 
104  // utility methods
105  int setData(double *newData, int size);
106  const double *getDataPtr(void) const;
107  double *getDataPtr(void);
108  bool Nulo(void) const;
109  int Assemble(const Vector &V, const ID &l, double fact = 1.0);
110  double Norm2(void) const;
111  double Norm(void) const;
112  double pNorm(int p) const;
113  double NormInf(void) const;
114  int Size(void) const;
115  int getNumBytes(void) const;
116  int resize(int newSize);
117  void Zero(void);
118  bool isnan(void) const;
119  int reset(const int &newSize);
120  int Normalize(void);
121  int NormalizeInf(void);
122  Vector Normalized(void) const;
123  Vector NormalizedInf(void) const;
124 
125  int addVector(double factThis, const Vector &other, double factOther);
126  int addMatrixVector(double factThis, const Matrix &m, const Vector &v, double factOther);
127  int addMatrixTransposeVector(double factThis, const Matrix &m, const Vector &v, double factOther);
128 
129 
131  virtual double &at(const size_t &f);
133  virtual const double &at(const size_t &f) const;
135  virtual bool CheckIndice0(const size_t &i) const;
136 
137  // overloaded operators
138  const double &operator()(int x) const;
139  double &operator()(int x);
140  const double &operator[](int x) const; // these two operator do bounds checks
141  double &operator[](int x);
142  Vector operator()(const ID &rows) const;
143  Vector &operator=(const Vector &V);
144 
145  template <class TNSR>
146  Vector &operator=(const TNSR &T);
147 
148  Vector &operator+=(double fact);
149  Vector &operator-=(double fact);
150  Vector &operator*=(double fact);
151  Vector &operator/=(double fact);
152 
153  Vector operator+(double fact) const;
154  Vector operator-(double fact) const;
155  Vector operator*(double fact) const;
156  Vector operator/(double fact) const;
157 
158  Vector &operator+=(const Vector &V);
159  Vector &operator-=(const Vector &V);
160 
161  Vector operator+(const Vector &V) const;
162  Vector operator-(const Vector &V) const;
163  double operator^(const Vector &V) const;
164  Vector operator/(const Matrix &M) const;
165 
166  double dot(const Vector &) const;
167 
168  // methods added by Remo
169  int Assemble(const Vector &V, int init_row, double fact = 1.0);
170  int Extract(const Vector &V, int init_row, double fact = 1.0);
171 
172  Vector getComponents(const ID &) const;
173  void putComponents(const Vector &,const ID &);
174  void addComponents(const Vector &,const ID &);
175 
176  void write(std::ofstream &);
177  void read(std::ifstream &);
178  friend std::ostream &operator<<(std::ostream &s, const Vector &);
179  friend std::string to_string(const Vector &);
180  inline std::string toString(void) const
181  { return to_string(*this); }
182  // friend istream &operator>>(istream &s, Vector &V);
183  friend Vector operator*(double a, const Vector &V);
184 
185  friend class Message;
186  friend class SystemOfEqn;
187  friend class Matrix;
188  friend class TCP_SocketNoDelay;
189  friend class TCP_Socket;
190  friend class UDP_Socket;
191  friend class MPI_Channel;
192  };
193 
194 std::vector<double> vector_to_std_vector(const Vector &);
195 m_double vector_to_m_double(const Vector &);
196 
197 double dot(const Vector &a,const Vector &b);
198 Matrix prod_tensor(const Vector &,const Vector &);
199 Matrix operator&(const Vector &u,const Vector &v);
200 
201 Vector normalize(const Vector &);
202 Vector normalize_inf(const Vector &);
203 
204 
205 
206 /********* INLINED VECTOR FUNCTIONS ***********/
207 inline int Vector::Size(void) const
208  { return sz; }
209 
210 inline int Vector::getNumBytes(void) const
211  { return Size()*sizeof(double); }
212 
213 inline const double *Vector::getDataPtr(void) const
214  { return theData; }
215 
216 inline double *Vector::getDataPtr(void)
217  { return theData; }
218 
219 inline bool Vector::Nulo(void) const
220  { return (theData== nullptr); }
221 
222 inline void Vector::Zero(void)
223  {
224  for(register int i=0; i<sz; i++)
225  theData[i] = 0.0;
226  }
227 
228 inline bool Vector::isnan(void) const
229  {
230  bool retval= false;
231  for(register int i=0; i<sz; i++)
232  if(std::isnan(theData[i]))
233  {
234  retval= true;
235  break;
236  }
237  return retval;
238  }
239 
240 inline int Vector::reset(const int &newSize)
241  {
242  const int retval= resize(newSize);
243  Zero();
244  return retval;
245  }
246 
247 inline const double &Vector::operator()(int x) const
248  {
249 #ifdef _G3DEBUG
250  // check if it is inside range [0,sz-1]
251  if(x < 0 || x >= sz)
252  {
253  std::cerr << "XC::Vector::(loc) - loc " << x << " outside range [0, " << sz-1 << endln;
254  return VECTOR_NOT_VALID_ENTRY;
255  }
256 #endif
257  return theData[x];
258  }
259 
260 
261 inline double &Vector::operator()(int x)
262  {
263 #ifdef _G3DEBUG
264  // check if it is inside range [0,sz-1]
265  if (x < 0 || x >= sz)
266  {
267  std::cerr << "XC::Vector::(loc) - loc " << x << " outside range [0, " << sz-1 << std::endln;
268  return VECTOR_NOT_VALID_ENTRY;
269  }
270 #endif
271  return theData[x];
272  }
273 
274 template <class TNSR>
275 Vector & Vector::operator=(const TNSR &V)
276  {
277  int rank= V.rank();
278  if(rank != 2)
279  {
280  std::cerr << "XC::Vector::operator=() - BJtensor must be of rank 2\n";
281  return *this;
282  }
283  int dim= V.dim(1);
284  if(dim != V.dim(2))
285  {
286  std::cerr << "XC::Vector::operator=() - BJtensor must have square dimensions\n";
287  return *this;
288  }
289 
290  if(dim != 2 || dim != 3 || dim != 1)
291  {
292  std::cerr << "XC::Vector::operator=() - BJtensor must be of dimension 2 or 3\n";
293  return *this;
294  }
295 
296  if(dim == 1)
297  {
298  if(sz != 1)
299  {
300  std::cerr << "Vector::operator=() - Vector size must be 1\n";
301  return *this;
302  }
303  theData[0]= V.cval(1,1);
304  }
305  else if(dim == 2)
306  {
307  if(sz != 3)
308  {
309  std::cerr << "Vector::operator=() - Vector size must be 3\n";
310  return *this;
311  }
312  theData[0]= V.cval(1,1);
313  theData[1]= V.cval(2,2);
314  theData[2]= V.cval(1,2);
315  }
316  else
317  {
318  if(sz != 6)
319  {
320  std::cerr << "Vector::operator=() - Vector size must be 6\n";
321  return *this;
322  }
323  theData[0]= V.cval(1,1);
324  theData[1]= V.cval(2,2);
325  theData[2]= V.cval(3,3);
326  theData[3]= V.cval(1,2);
327  theData[4]= V.cval(1,3);
328  theData[5]= V.cval(2,3);
329  }
330  return *this;
331  }
332 
333 } // end of XC namespace
334 
335 
336 #endif
337 
Vector & operator+=(double fact)
The += operator adds fact to each element of the vector, data[i]= data[i]+fact.
Definition: Vector.cpp:855
int Normalize(void)
Normalizes the vector with the euclidean norm.
Definition: Vector.cpp:270
Vector normalize_inf(const Vector &)
Returns the normalized vector (infinite norm).
Definition: Vector.cpp:322
virtual ~Vector(void)
destructor, deletes the [] data
Definition: Vector.cpp:192
Vector & operator*=(double fact)
The *= operator multiplies each element by the factor.
Definition: Vector.cpp:878
Vector NormalizedInf(void) const
Returns the normalized vector con la norma_infinito.
Definition: Vector.cpp:310
Vector operator+(double fact) const
The + operator returns a XC::Vector of the same size as current, whose components are return(i)= theD...
Definition: Vector.cpp:912
virtual bool CheckIndice0(const size_t &i) const
Check the index being passed as parameter.
Definition: Vector.cpp:766
virtual double & at(const size_t &f)
Returns the element at the row being passed as parameter.
Definition: Vector.cpp:750
Definition: Vector.h:82
double NormInf(void) const
Returns the máximo de los valores absolutos de las componentes del vector (norma_infinito).
Definition: Vector.cpp:716
Vector getComponents(const ID &) const
Returns a vector with the specified subset of components.
Definition: Vector.cpp:1208
void putComponents(const Vector &, const ID &)
Assigns the specified values to the specified set of vecto&#39;s components.
Definition: Vector.cpp:1218
Vector & operator-=(double fact)
The -= operator subtracts fact from each element of the vector, data[i]= data[i]-fact.
Definition: Vector.cpp:867
Matrix operator&(const Vector &u, const Vector &v)
tensor product.
Definition: Vector.cpp:1140
m_double vector_to_m_double(const Vector &)
Convierte el vector en un m_double.
Definition: Vector.cpp:1245
void write(std::ofstream &)
Escribe el vector en un archivo binario.
Definition: Vector.cpp:1101
Vector operator*(double fact) const
The + operator returns a XC::Vector of the same size as current, whose components are return(i)= theD...
Definition: Vector.cpp:939
MPI_Channel is a sub-class of channel. It is implemented with Berkeley stream sockets using the TCP p...
Definition: MPI_Channel.h:69
DP_Socket is a sub-class of channel. It is implemented with Berkeley datagram sockets using the UDP p...
Definition: UDP_Socket.h:76
TCP_Socket is a sub-class of channel. It is implemented with Berkeley stream sockets using the TCP pr...
Definition: TCP_Socket.h:71
Vector operator-(double fact) const
The + operator returns a XC::Vector of the same size as current, whose components are return(i)= theD...
Definition: Vector.cpp:925
System of equations base class.
Definition: SystemOfEqn.h:84
Definition: ID.h:77
Vector(void)
Standard constructor, sets size= 0;.
Definition: Vector.cpp:110
int Assemble(const Vector &V, const ID &l, double fact=1.0)
Method to assemble into object the XC::Vector V using the XC::ID l. If XC::ID(x) does not exist progr...
Definition: Vector.cpp:245
Vector Normalized(void) const
Returns the normalized vector con la norma euclidea.
Definition: Vector.cpp:302
Vector & operator=(const Vector &V)
the assignment operator, This is assigned to be a copy of V. if sizes are not compatable this...
Definition: Vector.cpp:835
double Norm(void) const
Method to return the norm of vector.
Definition: Vector.cpp:712
double Norm2(void) const
Returns the cuadrado del módulo del vector.
Definition: Vector.cpp:700
void read(std::ifstream &)
Lee el vector de un archivo binario.
Definition: Vector.cpp:1112
Definition: Matrix.h:82
Vector normalize(const Vector &)
Returns the normalized vector (euclidean norm).
Definition: Vector.cpp:318
Vector & operator/=(double fact)
The /= operator divides each element of the vector by fact, theData[i]= theData[i]/fact. Program exits if divide-by-zero would occur with warning message.
Definition: Vector.cpp:891
std::vector< double > vector_to_std_vector(const Vector &)
Convierte el vector en un std::vector<double>.
Definition: Vector.cpp:1235
TCP_SocketNoDelay is a sub-class of channel. It is implemented with Berkeley stream sockets using the...
Definition: TCP_SocketNoDelay.h:72
int resize(int newSize)
Changes vector size.
Definition: Vector.cpp:212
Message between processes.
Definition: Message.h:67
void addComponents(const Vector &, const ID &)
Sums the specified values to the specified set of vecto&#39;s components.
Definition: Vector.cpp:1226
int NormalizeInf(void)
Normaliza el vector con la norma_infinito.
Definition: Vector.cpp:286
================================================================================
Definition: ContinuaReprComponent.h:34
double operator^(const Vector &V) const
Method to perform (Vector)transposed * vector.
Definition: Vector.cpp:1058
Matrix prod_tensor(const Vector &, const Vector &)
Producto tensorial de dos tensores de primer orden.
Definition: Vector.cpp:1128
Vector operator/(double fact) const
The + operator returns a XC::Vector of the same size as current, whose components are return(i)= theD...
Definition: Vector.cpp:952