xc
MaterialVector.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 // under the terms of the GNU General Public License 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 //MaterialVector.h
29 
30 #ifndef MaterialVector_h
31 #define MaterialVector_h
32 
33 #include <vector>
34 #include "utility/kernel/CommandEntity.h"
35 #include "material/section/ResponseId.h"
36 #include "utility/actor/actor/MovableID.h"
37 #include "utility/matrix/Vector.h"
38 #include "utility/matrix/Matrix.h"
39 
40 
41 namespace XC {
42 
44 //
47 template <class MAT>
48 class MaterialVector: public std::vector<MAT *>, public CommandEntity, public MovableObject
49  {
50  protected:
51  void clearAll(void);
52  void alloc(const std::vector<MAT *> &mats);
53 
54 
55  DbTagData &getDbTagData(void) const;
56  int sendData(Communicator &);
57  int recvData(const Communicator &);
58  public:
59  typedef typename std::vector<MAT *> mat_vector;
60  typedef typename mat_vector::iterator iterator;
61  typedef typename mat_vector::const_iterator const_iterator;
62  typedef typename mat_vector::reference reference;
63  typedef typename mat_vector::const_reference const_reference;
64 
65  MaterialVector(const size_t &nMat,const MAT *matModel= nullptr);
68  ~MaterialVector(void)
69  { clearAll(); }
70 
71  void clearMaterials(void);
72  void setMaterial(const MAT *);
73  void setMaterial(size_t i,MAT *);
74  void setMaterial(size_t i, const MAT &);
75  void setMaterial(const MAT *,const std::string &);
76  void copyPropsFrom(const EntityWithProperties *);
77 
78  bool empty(void) const;
79  int commitState(void);
80  int revertToLastCommit(void);
81  int revertToStart(void);
82 
83  void setInitialGeneralizedStrains(const std::vector<Vector> &);
84  void incrementInitialGeneralizedStrains(const std::vector<Vector> &);
86 
87  size_t getGeneralizedStressSize(void) const;
88  size_t getGeneralizedStrainSize(void) const;
89  Matrix getGeneralizedStresses(void) const;
90  Matrix getGeneralizedStrains(void) const;
91  const Vector &getMeanGeneralizedStress(void) const;
92  const Vector &getMeanGeneralizedStrain(void) const;
93  double getMeanGeneralizedStrain(const int &defID) const;
94  double getMeanGeneralizedStress(const int &defID) const;
95 
96  Vector getGeneralizedStrainAtGaussPoints(const int &) const;
97  Vector getGeneralizedStressAtGaussPoints(const int &) const;
98 
99  Matrix getGeneralizedStrain(const int &defID) const;
100  Matrix getGeneralizedStress(const int &defID) const;
101 
102  Matrix getValues(const std::string &, bool silent= false) const;
103 
104  std::set<std::string> getNames(void) const;
105  boost::python::list getNamesPy(void) const;
106  std::set<int> getTags(void) const;
107  boost::python::list getTagsPy(void) const;
108 
109  int sendSelf(Communicator &);
110  int recvSelf(const Communicator &);
111  };
112 
114 template <class MAT>
115 MaterialVector<MAT>::MaterialVector(const size_t &nMat,const MAT *matModel)
116  : std::vector<MAT *>(nMat,nullptr), MovableObject(MAT_VECTOR_TAG)
117  {
118  if(matModel)
119  {
120  for(iterator i= mat_vector::begin();i!=mat_vector::end();i++)
121  {
122  (*i)= dynamic_cast<MAT *>(matModel->getCopy());
123  if(!(*i))
124  std::cerr << getClassName() << "::" << __FUNCTION__
125  << "; failed allocate material model pointer\n";
126  }
127  }
128  }
129 
131 template <class MAT>
132 void MaterialVector<MAT>::alloc(const std::vector<MAT *> &mats)
133  {
134  clearAll();
135  const size_t nMat= mats.size();
136  this->resize(nMat);
137  for(size_t i= 0;i<nMat;i++)
138  {
139  if(mats[i])
140  {
141  (*this)[i]= dynamic_cast<MAT *>(mats[i]->getCopy());
142  if(!(*this)[i])
143  std::cerr << getClassName() << "::" << __FUNCTION__
144  << "; failed allocate material model pointer\n";
145  }
146  }
147  }
148 
150 template <class MAT>
152  : std::vector<MAT *>(other.size(),nullptr), MovableObject(MAT_VECTOR_TAG)
153  { alloc(other); }
154 
156 template <class MAT>
158  {
159  alloc(other);
160  return *this;
161  }
162 
163 template <class MAT>
164 void MaterialVector<MAT>::setMaterial(const MAT *new_mat)
165  {
166  clearMaterials();
167  if(new_mat)
168  {
169  for(iterator i= mat_vector::begin();i!=mat_vector::end();i++)
170  {
171  (*i)= new_mat->getCopy();
172  if(!(*i))
173  std::cerr << getClassName() << "::" << __FUNCTION__
174  << "; failed allocate material model pointer\n";
175  }
176  }
177  }
178 
179 template <class MAT>
180 void MaterialVector<MAT>::setMaterial(const MAT *new_mat, const std::string &type)
181  {
182  clearMaterials();
183  if(new_mat)
184  {
185  for(iterator i= mat_vector::begin();i!=mat_vector::end();i++)
186  {
187  (*i)= new_mat->getCopy(type.c_str());
188  if(!(*i))
189  std::cerr << getClassName() << "::" << __FUNCTION__
190  << "; failed allocate material model pointer\n";
191  }
192  }
193  }
194 
195 template <class MAT>
196 void MaterialVector<MAT>::setMaterial(size_t i,MAT *new_mat)
197  {
198  if((*this)[i])
199  delete (*this)[i];
200  (*this)[i]= new_mat;
201  }
202 
203 template <class MAT>
204 void MaterialVector<MAT>::setMaterial(size_t i, const MAT &new_mat)
205  { setMaterial(i, new_mat.getCopy()); }
206 
209 template <class MAT>
211  {
212  if(other_mat)
213  {
214  const EntityWithProperties &tmp= *other_mat;
215  for(iterator i= mat_vector::begin();i!=mat_vector::end();i++)
216  {
217  (*i)->copyPropsFrom(tmp);
218  }
219  }
220  }
221 
222 template <class MAT>
224  {
225  for(iterator i= mat_vector::begin();i!=mat_vector::end();i++)
226  {
227  if(*i)
228  {
229  delete (*i);
230  (*i)= nullptr;
231  }
232  }
233  }
234 
236 template <class MAT>
238  {
239  if(mat_vector::empty())
240  return true;
241  else
242  return ((*this)[0]==nullptr);
243  }
244 
245 template <class MAT>
247  {
248  clearMaterials();
249  std::vector<MAT *>::clear();
250  }
251 
252 
254 template <class MAT>
256  {
257  int retVal= 0;
258 
259  for(iterator i=mat_vector::begin();i!=mat_vector::end();i++)
260  retVal+= (*i)->commitState();
261  return retVal;
262  }
263 
265 template <class MAT>
267  {
268  int retVal= 0;
269 
270  for(iterator i=mat_vector::begin();i!=mat_vector::end();i++)
271  retVal+= (*i)->revertToLastCommit() ;
272  return retVal;
273  }
274 
275 
277 template <class MAT>
279  {
280  int retVal = 0;
281 
282  for(iterator i=mat_vector::begin();i!=mat_vector::end();i++)
283  retVal+= (*i)->revertToStart();
284  return retVal;
285  }
286 
291 template <class MAT>
292 XC::Matrix XC::MaterialVector<MAT>::getValues(const std::string &code, bool silent) const
293  {
294  Matrix retval;
295  const int nMat= this->size();
296  std::vector<Matrix> tmp(nMat);
297  int count= 0;
298  int nRows= 0;
299  int nCols= 0;
300  for(const_iterator i=mat_vector::begin();i!=mat_vector::end();i++, count++)
301  {
302  const Matrix v= (*i)->getValues(code, silent);
303  nRows+= v.noRows();
304  nCols= std::max(nCols, v.noCols());
305  tmp[count]= v;
306  }
307  retval.resize(nRows,nCols);
308  int iRow= 0;
309  int iCol= 0;
310  for(int i= 0;i<nMat;i++)
311  {
312  const Matrix v= tmp[i];
313  for(int j= 0;j<v.noRows();j++)
314  {
315  for(int k= 0;k<v.noCols();k++)
316  {
317  iCol= k;
318  retval(iRow,iCol)= v(j,k);
319  }
320  iRow+= 1;
321  }
322  }
323  return retval;
324  }
325 
327 template <class MAT>
329  { return (*this)[0]->getGeneralizedStress().Size(); }
330 
332 template <class MAT>
334  { return (*this)[0]->getGeneralizedStrain().Size(); }
335 
337 template <class MAT>
339  {
340  const size_t ncol= getGeneralizedStressSize();
341  const size_t nMat= this->size();
342  Matrix retval(nMat,ncol);
343  for(size_t i= 0;i<nMat;i++)
344  {
345  const Vector &s= (*this)[i]->getGeneralizedStress();
346  retval.putRow(i,s);
347  }
348  return retval;
349  }
350 
352 template <class MAT>
354  {
355  const size_t ncol= getGeneralizedStrainSize();
356  const size_t nMat= this->size();
357  Matrix retval(nMat,ncol);
358  for(size_t i= 0;i<nMat;i++)
359  {
360  const Vector &s= (*this)[i]->getGeneralizedStrain();
361  for(size_t j= 0;j<ncol;j++)
362  retval(i,j)= s(j);
363  }
364  return retval;
365  }
366 
368 template <class MAT>
370  {
371  static Vector retval;
372  retval= (*this)[0]->getGeneralizedStress();
373  const size_t nMat= this->size();
374  for(size_t i= 1;i<nMat;i++)
375  retval+= (*this)[i]->getGeneralizedStress();
376  retval/=nMat;
377  return retval;
378  }
379 
381 template <class MAT>
383  {
384  static Vector retval;
385  retval= (*this)[0]->getGeneralizedStrain();
386  const size_t nMat= this->size();
387  for(size_t i= 0;i<nMat;i++)
388  retval+= (*this)[i]->getGeneralizedStrain();
389  retval/=nMat;
390  return retval;
391  }
392 
395 template <class MAT>
396 double MaterialVector<MAT>::getMeanGeneralizedStrain(const int &defID) const
397  {
398  double retval= 0.0;
399  const Vector &e= getMeanGeneralizedStrain(); //generalized strains vector.
400  const ResponseId &code= (*this)[0]->getResponseType();
401  const int order= code.Size();
402  for(int i= 0;i<order;i++)
403  if(code(i) == defID)
404  retval+= e(i);
405  return retval;
406  }
407 
410 template <class MAT>
412  {
413  const size_t nMat= this->size();
414  const ResponseId &code= (*this)[0]->getResponseType();
415  const int order= code.Size();
416  Vector retval(nMat);
417  for(size_t i= 0;i<nMat;i++)
418  {
419  const Vector &e= (*this)[i]->getGeneralizedStrain(); //Strain at i-th Gauss point.
420  for(int j= 0;j<order;j++) // Searching for the component.
421  if(code(j) == defID)
422  retval(i)= e(j);
423  }
424  return retval;
425  }
426 
427 
430 template <class MAT>
431 double MaterialVector<MAT>::getMeanGeneralizedStress(const int &defID) const
432  {
433  double retval= 0.0;
434  const Vector &f= getMeanGeneralizedStress(); //Vector de esfuerzos.
435  const ResponseId &code= (*this)[0]->getResponseType();
436  const int order= code.Size();
437  for(int i= 0;i<order;i++)
438  if(code(i) == defID)
439  retval+= f(i);
440  return retval;
441  }
442 
445 template <class MAT>
447  {
448  const size_t nMat= this->size();
449  const ResponseId &code= (*this)[0]->getResponseType();
450  const int order= code.Size();
451  Vector retval(nMat);
452  for(size_t i= 0;i<nMat;i++)
453  {
454  const Vector &e= (*this)[i]->getGeneralizedStress(); //Stress at i-th Gauss point.
455  for(int j= 0;j<order;j++) // Searching for the component.
456  if(code(j) == defID)
457  retval(i)= e(j);
458  }
459  return retval;
460  }
461 
464 template <class MAT>
466  {
467  const size_t nMat= this->size();
468  Matrix retval(nMat,1);
469  const ResponseId &code= (*this)[0]->getResponseType();
470  const int order= code.Size();
471  for(size_t i= 0;i<nMat;i++)
472  {
473  const Vector &s= (*this)[i]->getGeneralizedStress();
474  for(int j= 0;j<order;j++)
475  if(code(j) == defID)
476  retval(i,1)+= s(i);
477  }
478  return retval;
479  }
480 
483 template <class MAT>
485  {
486  const size_t nMat= this->size();
487  Matrix retval(nMat,1);
488  const ResponseId &code= (*this)[0]->getResponseType();
489  const int order= code.Size();
490  for(size_t i= 0;i<nMat;i++)
491  {
492  const Vector &s= (*this)[i]->getGeneralizedStrain();
493  for(int j= 0;j<order;j++)
494  if(code(j) == defID)
495  retval(i,1)+= s(i);
496  }
497  return retval;
498  }
499 
501 template <class MAT>
502 void MaterialVector<MAT>::setInitialGeneralizedStrains(const std::vector<Vector> &iS)
503  {
504  const size_t nMat= this->size();
505  const size_t sz= std::min(nMat,iS.size());
506  if(iS.size()<nMat)
507  std::cerr << getClassName() << "::" << __FUNCTION__
508  << "; received: "
509  << iS.size() << " generalized strain vectors, expected: "
510  << nMat << ".\n";
511  for(size_t i= 0;i<sz;i++)
512  (*this)[i]->setInitialGeneralizedStrain(iS[i]);
513  }
514 
516 template <class MAT>
518  {
519  const size_t nMat= this->size();
520  const size_t sz= std::min(nMat,iS.size());
521  if(iS.size()<nMat)
522  std::cerr << getClassName() << "::" << __FUNCTION__
523  << "; received: "
524  << iS.size() << " generalized strain vectors, expected: "
525  << nMat << ".\n";
526  for(size_t i= 0;i<sz;i++)
527  (*this)[i]->incrementInitialGeneralizedStrain(iS[i]);
528  }
529 
531 template <class MAT>
533  {
534  const size_t nMat= this->size();
535  for(size_t i= 0;i<nMat;i++)
536  (*this)[i]->zeroInitialGeneralizedStrain();
537  }
538 
541 template <class MAT>
543  {
544  static DbTagData retval(2);
545  return retval;
546  }
547 
549 template <class MAT>
551  {
552  int res= 0;
553  if(this->empty())
554  setDbTagDataPos(0,0);
555  else
556  {
557  setDbTagDataPos(0,1);
558  const size_t nMat= this->size();
559  DbTagData cpMat(nMat*3);
560 
561  for(size_t i= 0;i<nMat;i++)
562  res+= comm.sendBrokedPtr((*this)[i],cpMat,BrokedPtrCommMetaData(i,i+nMat,i+2*nMat));
563  res+= cpMat.send(getDbTagData(),comm,CommMetaData(1));
564  }
565  return res;
566  }
567 
569 template <class MAT>
571  {
572  const int flag= getDbTagDataPos(0);
573  int res= 0;
574  if(flag!=0)
575  {
576  const size_t nMat= this->size();
577  DbTagData cpMat(nMat*3);
578  res+= cpMat.receive(getDbTagData(),comm,CommMetaData(1));
579  clearMaterials(); // erase existing materials if any.
580  for(size_t i= 0;i<nMat;i++)
581  {
582  const BrokedPtrCommMetaData meta(i,i+nMat,i+2*nMat);
583  // Receive the material
584  (*this)[i]= comm.getBrokedMaterial((*this)[i],cpMat,meta);
585  }
586  }
587  return res;
588  }
589 
591 template <class MAT>
592 std::set<std::string> MaterialVector<MAT>::getNames(void) const
593  {
594  std::set<std::string> retval;
595  for(const_iterator i= mat_vector::begin();i!=mat_vector::end();i++)
596  retval.insert((*i)->getName());
597  return retval;
598  }
599 
601 template <class MAT>
602 boost::python::list MaterialVector<MAT>::getNamesPy(void) const
603  {
604  boost::python::list retval;
605  std::set<std::string> tmp= getNames();
606  for(std::set<std::string>::const_iterator i= tmp.begin();i!=tmp.end();i++)
607  retval.append(*i);
608  return retval;
609  }
610 
612 template <class MAT>
613 std::set<int> MaterialVector<MAT>::getTags(void) const
614  {
615  std::set<int> retval;
616  for(const_iterator i= mat_vector::begin();i!=mat_vector::end();i++)
617  retval.insert((*i)->getTag());
618  return retval;
619  }
620 
622 template <class MAT>
623 boost::python::list MaterialVector<MAT>::getTagsPy(void) const
624  {
625  boost::python::list retval;
626  std::set<int> tmp= getTags();
627  for(std::set<int>::const_iterator i= tmp.begin();i!=tmp.end();i++)
628  retval.append(*i);
629  return retval;
630  }
631 
633 template <class MAT>
635  {
636  inicComm(2);
637  int res= sendData(comm);
638  const int dataTag=getDbTag();
639  res+= comm.sendIdData(getDbTagData(),dataTag);
640  if(res < 0)
641  std::cerr << getClassName() << "::" << __FUNCTION__
642  << dataTag << " failed to send ID";
643  return res;
644  }
645 
647 template <class MAT>
649  {
650  const int dataTag= this->getDbTag();
651  inicComm(2);
652  int res= comm.receiveIdData(getDbTagData(),dataTag);
653  if(res<0)
654  std::cerr << getClassName() << "::" << __FUNCTION__
655  << dataTag << " failed to receive ID\n";
656  else
657  res+= recvData(comm);
658  return res;
659  }
660 
661 } // end of XC namespace
662 
663 #endif
int sendIdData(const DbTagData &, const int &)
Sends miembro data through the communicator argument.
Definition: Communicator.cc:411
Matrix getGeneralizedStress(const int &defID) const
Returns the defID component of generalized stress vector on each integration point.
Definition: MaterialVector.h:465
int sendBrokedPtr(MovableObject *ptr, DbTagData &, const BrokedPtrCommMetaData &)
Sends a pointer to movable object through the communicator argument.
Definition: Communicator.cc:1204
int sendSelf(Communicator &)
Sends object through the communicator argument.
Definition: MaterialVector.h:634
Float vector abstraction.
Definition: Vector.h:94
Communication parameters between processes.
Definition: Communicator.h:66
void alloc(const std::vector< MAT *> &mats)
Copy materials from another vector.
Definition: MaterialVector.h:132
Object that can return properties as Python objects.
Definition: EntityWithProperties.h:32
int revertToLastCommit(void)
Returns materials to its last committed state.
Definition: MaterialVector.h:266
std::set< std::string > getNames(void) const
Return the names of the materials.
Definition: MaterialVector.h:592
void setInitialGeneralizedStrains(const std::vector< Vector > &)
Assigns initial values to materials initial strains.
Definition: MaterialVector.h:502
Object that can move between processes.
Definition: MovableObject.h:100
const Vector & getMeanGeneralizedStress(void) const
Returns average generalized stress values on element. In a future we can enhance this by providing an...
Definition: MaterialVector.h:369
int send(DbTagData &, Communicator &, const CommMetaData &) const
Sends the object.
Definition: DbTagData.cc:102
bool empty(void) const
Returns true ifno se ha asignado material.
Definition: MaterialVector.h:237
Vector getGeneralizedStrainAtGaussPoints(const int &) const
Returns the defID component of the strain vector at Gauss points.
Definition: MaterialVector.h:411
int noCols() const
Returns the number of columns, numCols, of the Matrix.
Definition: Matrix.h:273
Vector that stores the dbTags of the class members.
Definition: DbTagData.h:44
Matrix getGeneralizedStresses(void) const
Returns generalized stress values on each integration point.
Definition: MaterialVector.h:338
Data about the index, size,,...
Definition: CommMetaData.h:39
boost::python::list getNamesPy(void) const
Return the names of the materials in a python list.
Definition: MaterialVector.h:602
size_t getGeneralizedStrainSize(void) const
Returns the size of generalized strains vector.
Definition: MaterialVector.h:333
void copyPropsFrom(const EntityWithProperties *)
copy the user defined properties of the given object on each of the materials.
Definition: MaterialVector.h:210
int receive(DbTagData &, const Communicator &, const CommMetaData &)
Receive the object.
Definition: DbTagData.cc:106
Vector getGeneralizedStressAtGaussPoints(const int &) const
Returns the defID component of the stress vector at Gauss points.
Definition: MaterialVector.h:446
void incrementInitialGeneralizedStrains(const std::vector< Vector > &)
Adds to the materials initial strains the values being passed as parameters.
Definition: MaterialVector.h:517
boost::python::list getTagsPy(void) const
Return the identifiers of the materials in a python list.
Definition: MaterialVector.h:623
MaterialVector< MAT > & operator=(const MaterialVector< MAT > &)
Assignment operator.
Definition: MaterialVector.h:157
Material pointer container.
Definition: MaterialVector.h:48
Matrix getGeneralizedStrain(const int &defID) const
Returns the defID component of generalized strain vector on each integration point.
Definition: MaterialVector.h:484
size_t getGeneralizedStressSize(void) const
Returns the size of stress vector.
Definition: MaterialVector.h:328
int recvSelf(const Communicator &)
Receives object through the communicator argument.
Definition: MaterialVector.h:648
std::set< int > getTags(void) const
Return the identifiers of the materials.
Definition: MaterialVector.h:613
Objet that can execute python scripts.
Definition: CommandEntity.h:40
Stiffness material contribution response identifiers.
Definition: ResponseId.h:61
void copyPropsFrom(const EntityWithProperties &)
Copy the properties from the argument.
Definition: EntityWithProperties.cc:82
int noRows() const
Returns the number of rows, numRows, of the Matrix.
Definition: Matrix.h:269
const Vector & getMeanGeneralizedStrain(void) const
Returns average generalized strain values on element.In a future we can enhance this by providing an ...
Definition: MaterialVector.h:382
int sendData(Communicator &)
Send object members through the communicator argument.
Definition: MaterialVector.h:550
Matrix getGeneralizedStrains(void) const
Returns generalized strain values on each integration point.
Definition: MaterialVector.h:353
int commitState(void)
Commits materials state (normally after convergence).
Definition: MaterialVector.h:255
void zeroInitialGeneralizedStrains(void)
Initialize initial strains.
Definition: MaterialVector.h:532
Data to transmit for a pointer «broked».
Definition: BrokedPtrCommMetaData.h:40
int receiveIdData(DbTagData &, const int &) const
Receives el miembro data through the communicator argument.
Definition: Communicator.cc:415
int recvData(const Communicator &)
Receives object through the communicator argument.
Definition: MaterialVector.h:570
Open source finite element program for structural analysis.
Definition: ContinuaReprComponent.h:35
Matrix of floats.
Definition: Matrix.h:111
DbTagData & getDbTagData(void) const
Returns a vector to store the dbTags of the class members.
Definition: MaterialVector.h:542
Matrix getValues(const std::string &, bool silent=false) const
Ask the materials about the values that correspond to the code.
Definition: MaterialVector.h:292
int revertToStart(void)
Return materials to its initial state.
Definition: MaterialVector.h:278
void putRow(int, const Vector &)
Put the vector at the i-th row.
Definition: Matrix.cpp:1118
MaterialVector(const size_t &nMat, const MAT *matModel=nullptr)
Default constructor.
Definition: MaterialVector.h:115
int Size(void) const
Returns the vector size.
Definition: ID.h:115