xc
nDarray.h
1 // -*-c++-*-
2 //----------------------------------------------------------------------------
3 // XC program; finite element analysis code
4 // for structural analysis and design.
5 //
6 // Copyright (C) Luis C. Pérez Tato
7 //
8 // This program derives from OpenSees <http://opensees.berkeley.edu>
9 // developed by the «Pacific earthquake engineering research center».
10 //
11 // Except for the restrictions that may arise from the copyright
12 // of the original program (see copyright_opensees.txt)
13 // XC is free software: you can redistribute it and/or modify
14 // it under the terms of the GNU General Public License as published by
15 // the Free Software Foundation, either version 3 of the License, or
16 // (at your option) any later version.
17 //
18 // This software is distributed in the hope that it will be useful, but
19 // WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU General Public License for more details.
22 //
23 //
24 // You should have received a copy of the GNU General Public License
25 // along with this program.
26 // If not, see <http://www.gnu.org/licenses/>.
27 //----------------------------------------------------------------------------
28 //#############################################################################
29 // #
30 // #
31 // /~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~/~~\ #
32 // | |____| #
33 // | | #
34 // | | #
35 // | | #
36 // | | #
37 // | B A S E C L A S S E S | #
38 // | | #
39 // | | #
40 // | | #
41 // | | #
42 // | C + + H E A D E R | #
43 // | | #
44 // | | #
45 // | | #
46 // | | #
47 // /~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~/ | #
48 // \_________________________________________\__/ #
49 // #
50 // #
51 //#############################################################################
52 //#############################################################################
54 //################################################################################
55 //# COPYRIGHT (C): :-)) #
56 //# PROJECT: Object Oriented Finite Element Program #
57 //# PURPOSE: #
58 //# CLASS: nDarray #
59 //# #
60 //# VERSION: #
61 //# LANGUAGE: C++.ver >= 2.0 ( Borland C++ ver=3.10, SUN C++ ver=2.1 ) #
62 //# TARGET OS: DOS || UNIX || . . . #
63 //# DESIGNER(S): Boris Jeremic #
64 //# PROGRAMMER(S): Boris Jeremic #
65 //# #
66 //# #
67 //# DATE: May 28. - July 20 '93 #
68 //# UPDATE HISTORY: july 8. '93. BJtensor02 - BJtensor multiplication #
69 //# inner and outer products #
70 //# December 23 1993 print from the base class, operator==,#
71 //# macheps . . . #
72 //# August 22-29 '94 choped to separate files and worked on#
73 //# const and & issues #
74 //# August 30-31 '94 added use_def_dim to full the CC #
75 //# resolved problem with temoraries for #
76 //# operators + and - ( +=, -= ) #
77 //# January 16 '95 fixed the memory leakage introduced #
78 //# by previous work on +=, -+. I was #
79 //# by mistake decreasing #
80 //# this->pc_nDarray_rep->total_numb--; #
81 //# inststead of #
82 //# this->pc_nDarray_rep->n--; #
83 //# 28June2004 added val4 for efficiency still #
84 //# to be worked on #
85 //# #
86 //# #
87 //# #
88 //# #
89 //#############################################################################
90 //*/
91 
92 #ifndef NDARRAY_HH
93 #define NDARRAY_HH
94 
95 #include "utility/matrix/nDarray/basics.h"
96 #include <string>
97 #include <iostream>
98 #include <boost/python.hpp>
99 #include "tmpl_operators.h"
100 
101 // forward reference
102 namespace XC {
103 class BJtensor;
104 class BJmatrix;
105 class BJvector;
106 
107 
108 class stresstensor;
109 class straintensor;
110 
111 
115  {
116  public:
117  friend class nDarray;
118  friend class BJtensor;
119  friend class BJmatrix;
120 // friend class skyBJmatrix;
121  friend class stiffness_matrix;
122  friend class BJvector;
123  friend class stressstraintensor;
124  friend class stresstensor;
125  friend class straintensor;
126 
127  friend class Cosseratstresstensor;
128  friend class Cosseratstraintensor;
129 
130  private:
131  std::vector<double> pd_nDdata; // nD array as 1D array
132  int nDarray_rank;
133  // 0 -> scalar
134  // 1 -> BJvector
135  // 2 -> BJmatrix
136  // * -> ******** */
137  size_t total_numb; // total number of elements in nDarray
138  std::vector<int> dim; // array of dimensions in each rank direction
139  // for example, if nDarray_rank = 3 :
140  // dim[0] = dimension in direction 1
141  // dim[1] = dimension in direction 2
142  // dim[2] = dimension in direction 3 */
143 
144  inline size_t get_index(int first, int second) const
145  {
146  //assert(nDarray_rank==3);
147  return (first - 1)*dim[1]+second - 1;
148  }
149  inline size_t get_index(int first, int second, int third) const
150  {
151  //assert(nDarray_rank==3);
152  return ((first - 1)*dim[1]+second - 1)*dim[2]+third - 1;
153  }
154  inline size_t get_index(int first, int second, int third, int fourth) const
155  {
156  //assert(nDarray_rank==4);
157  return (((first - 1)*dim[1]+second - 1)*dim[2]+third - 1)*dim[3]+fourth - 1;
158  }
159  public:
160  void init_dim(const size_t &, const int &default_dim= 1);
161  void init_dim(const std::vector<int> &pdim);
162  inline void clear_dim(void)
163  { dim.clear(); }
164  inline bool equal_dim(const std::vector<int> &rval) const
165  { return (dim==rval); }
166  void init_data(void);
167  void init_data(const double &);
168  void init_data(const double *);
169  void init_data(const std::vector<double> &);
170  void init_data(const boost::python::list &);
171  void reset_data_to(const double &);
172  inline double *get_data_ptr(void)
173  { return pd_nDdata.data(); }
174  inline const double *get_data_ptr(void) const
175  { return pd_nDdata.data(); }
176  bool equal_data(const std::vector<double> &other_data) const;
177  inline const double &val(const size_t &where) const
178  { return pd_nDdata[where]; }
179  inline double &val(const size_t &where)
180  { return pd_nDdata[where]; }
181  inline void clear_data(void)
182  { pd_nDdata.clear(); }
183 
184  inline void clear(void)
185  {
186  clear_data();
187  clear_dim();
188  }
189  void sum_data(const std::vector<double> &);
190  void substract_data(const std::vector<double> &);
191  void neg(void);
192  double sum(void) const;
193  bool operator==(const nDarray_rep &rval) const;
194  inline const double &operator()(int first) const
195  {
196  if(nDarray_rank==0)
197  return (pd_nDdata[0]);
198  return val(static_cast<size_t>(first - 1));
199  }
200  inline double &operator()(int first)
201  {
202  if(nDarray_rank==0)
203  return (pd_nDdata[0]);
204  return val(static_cast<size_t>(first - 1));
205  }
206  inline const double &operator()(int first, int second) const
207  {
208  const size_t i= get_index(first, second);
209  return val(i);
210  }
211  inline double &operator()(int first, int second)
212  {
213  const size_t i= get_index(first, second);
214  return val(i);
215  }
216  inline const double &operator()(int first, int second, int third) const
217  {
218  //assert(nDarray_rank==3);
219  const size_t i= get_index(first, second, third);
220  return val(i);
221  }
222  inline double &operator()(int first, int second, int third)
223  {
224  const size_t i= get_index(first, second, third);
225  return val(i);
226  }
227  inline const double &operator()(int first, int second, int third, int fourth) const
228  {
229  //assert(nDarray_rank==4);
230  const size_t i= get_index(first, second, third, fourth);
231  return val(i);
232  }
233  inline double &operator()(int first, int second, int third, int fourth)
234  {
235  const size_t i= get_index(first, second, third, fourth);
236  return val(i);
237  }
238  };
239 
242 class nDarray
243  {
244  private:
245 // int rank(void) const;
246  size_t total_number(void) const;
247  void total_number(size_t );
248  void clear_dim(void);
249  void clear_data(void);
250  void clear_dim_data(void);
251  const int &get_dim_pointer(void) const;
252  // int dim(int which) const;
253 
254  protected:
255  nDarray_rep pc_nDarray_rep;
256 
257  const double *data(void) const;
258  void set_dim(const std::vector<int> &);
259  const std::vector<int> &dim(void) const;
260  void rank(int);
261  public:
262  nDarray(int rank_of_nDarray=1, const double &initval=0.0);// default constructor
263  nDarray(const std::vector<int> &pdim, const double *values);
264  nDarray(const std::vector<int> &pdim, const std::vector<double> &);
265  nDarray(const std::vector<int> &pdim, const boost::python::list &);
266  nDarray(const boost::python::list &, const boost::python::list &);
267  nDarray(const std::vector<int> &pdim, double initvalue);
268 
269  // special case for BJmatrix and BJvector . . .
270  nDarray(int rows, int cols, double *values);
271  nDarray(int rows, int cols, const std::vector<double> &values);
272  nDarray(int rows, int cols, const boost::python::list &);
273  nDarray(int rows, int cols, double initvalue);
274 
275 // special case when I don't want any initialization at all##
276  explicit nDarray(const std::string &){};
277 
278  nDarray(const std::string &flag, const std::vector<int> &pdim); // create a unit nDarray
279  inline virtual ~nDarray(void){};
280 
281 //##############################################################################
282 // copy only data because everything else has already been defined
283 // WATCH OUT IT HAS TO BE DEFINED BEFORE THIS FUNCTIONS IS CALLED
284 // use "from" and initialize already allocated nDarray from "from" values
285  void Initialize(const nDarray &from ); // initialize data only
286  void Initialize_all(const nDarray &from);// initialize and allocate all
287  // ( dimensions, rank and data )
288  // for BJtensor
289 
290  void Reset_to(const double &value); // reset data to "value"
291 
292  inline const double &operator()(int first) const
293  { return pc_nDarray_rep(first); }
294 
295  inline double &operator()(int first)
296  {
297  nDarray *this_no_const= const_cast<nDarray *>(this);
298  return this_no_const->operator()(first);
299  }
300 
301  inline const double &operator()(int first, int second) const
302  { return pc_nDarray_rep(first, second); }
303 
304  inline double &operator()(int first, int second)
305  {
306  nDarray *this_no_const= const_cast<nDarray *>(this);
307  return this_no_const->operator()(first, second);
308  }
309 
310  inline const double &operator()(int first, int second, int third) const
311  { return pc_nDarray_rep(first, second, third); }
312 
313  inline double &operator()(int first, int second, int third)
314  {
315  nDarray *this_no_const= const_cast<nDarray *>(this);
316  return this_no_const->operator()(first, second, third);
317  }
318  inline const double &operator()(int first, int second, int third, int fourth) const
319  { return pc_nDarray_rep(first, second, third, fourth); }
320 
321  inline double &operator()(int first, int second, int third, int fourth)
322  {
323  return pc_nDarray_rep(first, second, third, fourth);
324  }
325 
326  const double &val(int subscript, ...) const;
327  double &val(int subscript, ...);
328  const double &val4(int first, int second, int third, int fourth) const; // overloaded for FOUR arguments for operator * for two tensors
329  double &val4(int first, int second, int third, int fourth); // overloaded for FOUR arguments for operator * for two tensors
330 
331  const double &cval(int subscript, ...) const; // const
332 
333 //..
334 
335 
336 
337 //++ nDarray operator+( nDarray & rval); // nDarray addition
338 //....// This is from JOOP May/June 1990 after ARKoenig
339  nDarray& operator+=(const nDarray &); // nDarray addition
340 
341 
342 //++ nDarray operator-( nDarray & rval); // nDarray subtraction
343 //....// This is from JOOP May/June 1990 after ARKoenig
344  nDarray &operator-=(const nDarray & ); // nDarray subtraction
345 
346  nDarray operator+(const double &rval); // scalar addition
347  nDarray operator-(const double &rval); // scalar subtraction
348  nDarray &operator*=(const double &rval); // scalar multiplication
349  nDarray operator*(const double &rval) const; // scalar multiplication
350 
351  nDarray operator-(); // Unary minus
352 
353  double sum(void) const; // summ of all the elements
354  double trace(void) const; // trace of a 2-nd BJtensor, BJmatrix
355 
356  bool operator==(const nDarray &rval) const;
357 
358 // prebacen u nDarray 14 oktobra 1996
359  public:
360  nDarray eigenvalues(void);
361  nDarray eigenvectors(void);
362 
363 
364  double Frobenius_norm( void ); // return the Frobenius norm of
365  // BJmatrix, BJtensor, BJvector
366  double General_norm( double p ); // return the General p-th norm of
367  // BJmatrix, BJtensor, BJvector
368  public:
369  int rank(void) const;
370  int dim(int which) const;
371 
372  void output(std::ostream &os) const;
373  void outputshort(std::ostream &os) const;
374  void print(const std::string &name = "t",const std::string &msg = "Hi there#", std::ostream &os= std::cout) const;
375  void printshort(std::ostream &os, const std::string &msg = "Hi there#") const;
376  void mathprint(std::ostream &os) const;
377  friend std::string to_string(const nDarray &);
378  inline std::string toString(void) const
379  { return to_string(*this); }
380 
381 // from Numerical recipes in C
382  private:
383  static void tqli(std::vector<double> &d, std::vector<double> &, int n, std::vector<std::vector<double> > &z);
384  static void tred2(std::vector<std::vector<double> > &a, int n, std::vector<double> &d, std::vector<double> &e);
385  static void eigsrt(std::vector<double> &d, std::vector<std::vector<double> > &v, int n);
386 
387  };
388 
389 template nDarray operator*(const double & , const nDarray & );
390 template nDarray operator+(const nDarray & , const nDarray & );
391 template nDarray operator-(const nDarray & , const nDarray & );
392 std::ostream& operator<<(std::ostream &, const nDarray &);
393 std::string to_string(const nDarray &);
394 
395 } // end of XC namespace
396 
397 
398 #endif
399 
void init_data(void)
Initialize data vector.
Definition: nDarray.cpp:139
Base class for strain and stress tensors.
Definition: stress_strain_tensor.h:42
Stress tensor.
Definition: stresst.h:70
Boris Jeremic tensor class.
Definition: BJtensor.h:112
Definition: bimap.h:33
FiberSet operator+(const FiberSet &, const FiberSet &)
Return the union of both containers.
Definition: FiberSet.cc:65
Boris Jeremic vector class.
Definition: BJvector.h:102
n-dimensional array.
Definition: nDarray.h:242
Stress tensor of a Cosserat material.
Definition: Cosseratstresst.h:66
Storage of n-dimensional array data.
Definition: nDarray.h:114
Open source finite element program for structural analysis.
Definition: ContinuaReprComponent.h:35
FiberSet operator-(const FiberSet &, const FiberSet &)
Return the fibers in a that are not in b.
Definition: FiberSet.cc:73
Boris Jeremic matrix class.
Definition: BJmatrix.h:104
std::string to_string(const BJmatrix &)
Returns a string that represents the matrix.
Definition: BJmatrix.cpp:1003
Strain tensor of a Cosserat material.
Definition: Cosseratstraint.h:70
Strain tensor.
Definition: straint.h:68
void init_dim(const size_t &, const int &default_dim=1)
Initialize dimensions vector.
Definition: nDarray.cpp:118