XC Open source finite element analysis program
MaterialVector.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 // under the terms of the GNU General Public License 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 //MaterialVector.h
28 
29 #ifndef MaterialVector_h
30 #define MaterialVector_h
31 
32 #include <vector>
33 #include "xc_utils/src/nucleo/EntCmd.h"
34 #include "material/section/ResponseId.h"
35 #include "utility/actor/actor/MovableID.h"
36 #include "utility/matrix/Vector.h"
37 
38 
39 namespace XC {
40 
42 //
45 template <class MAT>
46 class MaterialVector: public std::vector<MAT *>, public EntCmd, public MovableObject
47  {
48  protected:
49  void clear_materials(void);
50  void clearAll(void);
51  void alloc(const std::vector<MAT *> &mats);
52 
53 
54  DbTagData &getDbTagData(void) const;
55  int sendData(CommParameters &);
56  int recvData(const CommParameters &);
57  public:
58  typedef typename std::vector<MAT *> mat_vector;
59  typedef typename mat_vector::iterator iterator;
60  typedef typename mat_vector::reference reference;
61  typedef typename mat_vector::const_reference const_reference;
62 
63  MaterialVector(const size_t &nMat,const MAT *matModel= nullptr);
66  ~MaterialVector(void)
67  { clearAll(); }
68 
69  void setMaterial(const MAT *);
70  void setMaterial(size_t i,MAT *);
71  void setMaterial(const MAT *,const std::string &tipo);
72  bool empty(void) const;
73  int commitState(void);
74  int revertToLastCommit(void);
75  int revertToStart(void);
76 
77  void setInitialGeneralizedStrains(const std::vector<Vector> &);
78  void addInitialGeneralizedStrains(const std::vector<Vector> &);
80 
81  size_t getGeneralizedStressSize(void) const;
82  size_t getGeneralizedStrainSize(void) const;
83  m_double getGeneralizedStresses(void) const;
84  m_double getGeneralizedStrains(void) const;
85  const Vector &getMeanGeneralizedStress(void) const;
86  const Vector &getMeanGeneralizedStrain(void) const;
87  double getMeanGeneralizedStrain(const int &defID) const;
88  double getMeanGeneralizedStress(const int &defID) const;
89  double getMeanGeneralizedStrainByName(const std::string &) const;
90  double getMeanGeneralizedStressByName(const std::string &) const;
91  m_double getGeneralizedStrain(const int &defID) const;
92  m_double getGeneralizedStress(const int &defID) const;
93 
94  int sendSelf(CommParameters &);
95  int recvSelf(const CommParameters &);
96  };
97 
99 template <class MAT>
100 MaterialVector<MAT>::MaterialVector(const size_t &nMat,const MAT *matModel)
101  : std::vector<MAT *>(nMat,nullptr), MovableObject(MAT_VECTOR_TAG)
102  {
103  if(matModel)
104  {
105  for(iterator i= mat_vector::begin();i!=mat_vector::end();i++)
106  {
107  (*i)= matModel->getCopy();
108  if(!(*i))
109  std::cerr << nombre_clase() << "::" << __FUNCTION__
110  << "; failed allocate material model pointer\n";
111  }
112  }
113  }
114 
116 template <class MAT>
117 void MaterialVector<MAT>::alloc(const std::vector<MAT *> &mats)
118  {
119  clearAll();
120  const size_t nMat= mats.size();
121  this->resize(nMat);
122  for(size_t i= 0;i<nMat;i++)
123  {
124  if(mats[i])
125  {
126  (*this)[i]= mats[i]->getCopy();
127  if(!(*this)[i])
128  std::cerr << nombre_clase() << "::" << __FUNCTION__
129  << "; failed allocate material model pointer\n";
130  }
131  }
132  }
133 
135 template <class MAT>
137  : std::vector<MAT *>(otro.size(),nullptr), MovableObject(MAT_VECTOR_TAG)
138  { alloc(otro); }
139 
141 template <class MAT>
143  {
144  alloc(otro);
145  return *this;
146  }
147 
148 template <class MAT>
149 void MaterialVector<MAT>::setMaterial(const MAT *new_mat)
150  {
151  clear_materials();
152  if(new_mat)
153  {
154  for(iterator i= mat_vector::begin();i!=mat_vector::end();i++)
155  {
156  (*i)= new_mat->getCopy();
157  if(!(*i))
158  std::cerr << nombre_clase() << "::" << __FUNCTION__
159  << "; failed allocate material model pointer\n";
160  }
161  }
162  }
163 
164 template <class MAT>
165 void MaterialVector<MAT>::setMaterial(const MAT *new_mat, const std::string &tipo)
166  {
167  clear_materials();
168  if(new_mat)
169  {
170  for(iterator i= mat_vector::begin();i!=mat_vector::end();i++)
171  {
172  (*i)= new_mat->getCopy(tipo.c_str());
173  if(!(*i))
174  std::cerr << nombre_clase() << "::" << __FUNCTION__
175  << "; failed allocate material model pointer\n";
176  }
177  }
178  }
179 
180 template <class MAT>
181 void MaterialVector<MAT>::setMaterial(size_t i,MAT *new_mat)
182  {
183  if((*this)[i])
184  delete (*this)[i];
185  (*this)[i]= new_mat;
186  }
187 
188 template <class MAT>
190  {
191  for(iterator i= mat_vector::begin();i!=mat_vector::end();i++)
192  {
193  if(*i) delete (*i);
194  (*i)= nullptr;
195  }
196  }
197 
199 template <class MAT>
201  {
202  if(mat_vector::empty())
203  return true;
204  else
205  return ((*this)[0]==nullptr);
206  }
207 
208 template <class MAT>
210  {
211  clear_materials();
212  std::vector<MAT *>::clear();
213  }
214 
215 
217 template <class MAT>
219  {
220  int retVal= 0;
221 
222  for(iterator i=mat_vector::begin();i!=mat_vector::end();i++)
223  retVal+= (*i)->commitState();
224  return retVal;
225  }
226 
228 template <class MAT>
230  {
231  int retVal= 0;
232 
233  for(iterator i=mat_vector::begin();i!=mat_vector::end();i++)
234  retVal+= (*i)->revertToLastCommit() ;
235  return retVal;
236  }
237 
238 
240 template <class MAT>
242  {
243  int retVal = 0;
244 
245  for(iterator i=mat_vector::begin();i!=mat_vector::end();i++)
246  retVal+= (*i)->revertToStart() ;
247  return retVal;
248  }
249 
251 template <class MAT>
253  { return (*this)[0]->getGeneralizedStress().Size(); }
254 
256 template <class MAT>
258  { return (*this)[0]->getGeneralizedStrain().Size(); }
259 
261 template <class MAT>
263  {
264  const size_t ncol= getGeneralizedStressSize();
265  const size_t nMat= this->size();
266  m_double retval(nMat,ncol,0.0);
267  for(size_t i= 0;i<nMat;i++)
268  {
269  const Vector &s= (*this)[i]->getGeneralizedStress();
270  for(size_t j= 0;j<ncol;j++)
271  retval(i,j)= s(j);
272  }
273  return retval;
274  }
275 
277 template <class MAT>
279  {
280  const size_t ncol= getGeneralizedStrainSize();
281  const size_t nMat= this->size();
282  m_double retval(nMat,ncol,0.0);
283  for(size_t i= 0;i<nMat;i++)
284  {
285  const Vector &s= (*this)[i]->getGeneralizedStrain();
286  for(size_t j= 0;j<ncol;j++)
287  retval(i,j)= s(j);
288  }
289  return retval;
290  }
291 
293 template <class MAT>
295  {
296  static Vector retval;
297  retval= (*this)[0]->getGeneralizedStress();
298  const size_t nMat= this->size();
299  for(size_t i= 1;i<nMat;i++)
300  retval+= (*this)[i]->getGeneralizedStress();
301  retval/=nMat;
302  return retval;
303  }
304 
306 template <class MAT>
308  {
309  static Vector retval;
310  retval= (*this)[0]->getGeneralizedStrain();
311  const size_t nMat= this->size();
312  for(size_t i= 0;i<nMat;i++)
313  retval+= (*this)[i]->getGeneralizedStrain();
314  retval/=nMat;
315  return retval;
316  }
317 
320 template <class MAT>
321 double MaterialVector<MAT>::getMeanGeneralizedStrain(const int &defID) const
322  {
323  double retval= 0.0;
324  const Vector &e= getMeanGeneralizedStrain(); //generalized strains vector.
325  const ResponseId &code= (*this)[0]->getType();
326  const int order= code.Size();
327  for(register int i= 0;i<order;i++)
328  if(code(i) == defID)
329  retval+= e(i);
330  return retval;
331  }
332 
335 template <class MAT>
336 double MaterialVector<MAT>::getMeanGeneralizedStrainByName(const std::string &cod) const
337  {
338  double retval= 0.0;
339  if(cod == "n1")
340  retval= this->getMeanGeneralizedStrain(MEMBRANE_RESPONSE_n1);
341  else if(cod == "n2")
342  retval= this->getMeanGeneralizedStrain(MEMBRANE_RESPONSE_n2);
343  else if(cod == "m1") //Flector en torno al eje 1.
344  retval= this->getMeanGeneralizedStrain(PLATE_RESPONSE_m1);
345  else if(cod == "m2") //Flector en torno al eje 2.
346  retval= this->getMeanGeneralizedStrain(PLATE_RESPONSE_m2);
347  else if(cod == "q13")
348  retval= this->getMeanGeneralizedStrain(PLATE_RESPONSE_q13);
349  else if(cod == "q23")
350  retval= this->getMeanGeneralizedStrain(PLATE_RESPONSE_q23);
351  else if(cod == "m12")
352  retval= this->getMeanGeneralizedStrain(PLATE_RESPONSE_m12);
353  else if(cod == "n12")
354  retval= this->getMeanGeneralizedStrain(MEMBRANE_RESPONSE_n12);
355  else
356  std::cerr << nombre_clase() << "::" << __FUNCTION__
357  << "stress code: '" << cod << " unknown." << std::endl;
358  return retval;
359  }
360 
363 template <class MAT>
364 double MaterialVector<MAT>::getMeanGeneralizedStress(const int &defID) const
365  {
366  double retval= 0.0;
367  const Vector &f= getMeanGeneralizedStress(); //Vector de esfuerzos.
368  const ResponseId &code= (*this)[0]->getType();
369  const int order= code.Size();
370  for(register int i= 0;i<order;i++)
371  if(code(i) == defID)
372  retval+= f(i);
373  return retval;
374  }
375 
378 template <class MAT>
379 double MaterialVector<MAT>::getMeanGeneralizedStressByName(const std::string &cod) const
380  {
381  double retval= 0.0;
382  if(cod == "n1") //Esfuerzo axil medio por unidad de longitud, paralelo al eje 1.
383  retval= this->getMeanGeneralizedStress(MEMBRANE_RESPONSE_n1);
384  else if(cod == "n2") //Esfuerzo axil medio por unidad de longitud, paralelo al eje 2.
385  retval= this->getMeanGeneralizedStress(MEMBRANE_RESPONSE_n2);
386  else if(cod == "n12")
387  retval= this->getMeanGeneralizedStress(MEMBRANE_RESPONSE_n12);
388  else if(cod == "m1") //Flector medio por unidad de longitud, en torno al eje 1.
389  retval= this->getMeanGeneralizedStress(PLATE_RESPONSE_m1);
390  else if(cod == "m2") //Flector medio por unidad de longitud, en torno al eje 2.
391  retval= this->getMeanGeneralizedStress(PLATE_RESPONSE_m2);
392  else if(cod == "m12")
393  retval= this->getMeanGeneralizedStress(PLATE_RESPONSE_m12);
394  else if(cod == "q13")
395  retval= this->getMeanGeneralizedStress(PLATE_RESPONSE_q13);
396  else if(cod == "q23")
397  retval= this->getMeanGeneralizedStress(PLATE_RESPONSE_q23);
398  else
399  std::cerr << nombre_clase() << "::" << __FUNCTION__
400  << "stress code: '" << cod << " unknown." << std::endl;
401  return retval;
402  }
403 
406 template <class MAT>
407 m_double MaterialVector<MAT>::getGeneralizedStress(const int &defID) const
408  {
409  const size_t nMat= this->size();
410  m_double retval(nMat,1,0.0);
411  const ResponseId &code= (*this)[0]->getType();
412  const int order= code.Size();
413  for(size_t i= 0;i<nMat;i++)
414  {
415  const Vector &s= (*this)[i]->getGeneralizedStress();
416  for(register int j= 0;j<order;j++)
417  if(code(j) == defID)
418  retval(i,1)+= s(i);
419  }
420  return retval;
421  }
422 
425 template <class MAT>
426 m_double MaterialVector<MAT>::getGeneralizedStrain(const int &defID) const
427  {
428  const size_t nMat= this->size();
429  m_double retval(nMat,1,0.0);
430  const ResponseId &code= (*this)[0]->getType();
431  const int order= code.Size();
432  for(size_t i= 0;i<nMat;i++)
433  {
434  const Vector &s= (*this)[i]->getGeneralizedStrain();
435  for(register int j= 0;j<order;j++)
436  if(code(j) == defID)
437  retval(i,1)+= s(i);
438  }
439  return retval;
440  }
441 
443 template <class MAT>
444 void MaterialVector<MAT>::setInitialGeneralizedStrains(const std::vector<Vector> &iS)
445  {
446  const size_t nMat= this->size();
447  const size_t sz= std::min(nMat,iS.size());
448  if(iS.size()<nMat)
449  std::cerr << nombre_clase() << "::" << __FUNCTION__
450  << "; received: "
451  << iS.size() << " generalized strain vectors, expected: "
452  << nMat << ".\n";
453  for(size_t i= 0;i<sz;i++)
454  (*this)[i]->setInitialGeneralizedStrain(iS[i]);
455  }
456 
458 template <class MAT>
459 void MaterialVector<MAT>::addInitialGeneralizedStrains(const std::vector<Vector> &iS)
460  {
461  const size_t nMat= this->size();
462  const size_t sz= std::min(nMat,iS.size());
463  if(iS.size()<nMat)
464  std::cerr << nombre_clase() << "::" << __FUNCTION__
465  << "; received: "
466  << iS.size() << " generalized strain vectors, expected: "
467  << nMat << ".\n";
468  for(size_t i= 0;i<sz;i++)
469  (*this)[i]->addInitialGeneralizedStrain(iS[i]);
470  }
471 
473 template <class MAT>
475  {
476  const size_t nMat= this->size();
477  for(size_t i= 0;i<nMat;i++)
478  (*this)[i]->zeroInitialGeneralizedStrain();
479  }
480 
483 template <class MAT>
485  {
486  static DbTagData retval(2);
487  return retval;
488  }
489 
491 template <class MAT>
493  {
494  int res= 0;
495  if(this->empty())
496  setDbTagDataPos(0,0);
497  else
498  {
499  setDbTagDataPos(0,1);
500  const size_t nMat= this->size();
501  DbTagData cpMat(nMat*3);
502 
503  for(size_t i= 0;i<nMat;i++)
504  res+= cp.sendBrokedPtr((*this)[i],cpMat,BrokedPtrCommMetaData(i,i+nMat,i+2*nMat));
505  res+= cpMat.send(getDbTagData(),cp,CommMetaData(1));
506  }
507  return res;
508  }
509 
511 template <class MAT>
513  {
514  const int flag = getDbTagDataPos(0);
515  int res= 0;
516  if(flag!=0)
517  {
518  const size_t nMat= this->size();
519  DbTagData cpMat(nMat*3);
520  res+= cpMat.receive(getDbTagData(),cp,CommMetaData(1));
521 
522  for(size_t i= 0;i<nMat;i++)
523  {
524  const BrokedPtrCommMetaData meta(i,i+nMat,i+2*nMat);
525  // Receive the material
526  (*this)[i]= cp.getBrokedMaterial((*this)[i],cpMat,meta);
527  }
528  }
529  return res;
530  }
531 
533 template <class MAT>
535  {
536  inicComm(2);
537  int res= sendData(cp);
538  const int dataTag=getDbTag();
539  res+= cp.sendIdData(getDbTagData(),dataTag);
540  if(res < 0)
541  std::cerr << nombre_clase() << "::" << __FUNCTION__
542  << dataTag << " failed to send ID";
543  return res;
544  }
545 
547 template <class MAT>
549  {
550  const int dataTag= this->getDbTag();
551  inicComm(2);
552  int res= cp.receiveIdData(getDbTagData(),dataTag);
553  if(res<0)
554  std::cerr << nombre_clase() << "::" << __FUNCTION__
555  << dataTag << " failed to receive ID\n";
556  else
557  res+= recvData(cp);
558  return res;
559  }
560 
561 } // end of XC namespace
562 
563 #endif
double getMeanGeneralizedStrainByName(const std::string &) const
Returns the component of the average strain vector which has the code being passed as parameter...
Definition: MaterialVector.h:336
void addInitialGeneralizedStrains(const std::vector< Vector > &)
Adds to the materials initial strains the values being passed as parameters.
Definition: MaterialVector.h:459
int send(DbTagData &, CommParameters &, const CommMetaData &) const
Sends the object.
Definition: DbTagData.cc:102
MaterialVector< MAT > & operator=(const MaterialVector< MAT > &)
Assignment operator.
Definition: MaterialVector.h:142
m_double getGeneralizedStrains(void) const
Returns generalized strain values on each integration point.
Definition: MaterialVector.h:278
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:307
int getDbTag(void) const
Returns the tag para la database.
Definition: MovableObject.cpp:92
Definition: Vector.h:82
Vector que almacena los dbTags de los miembros de la clase.
Definition: DbTagData.h:43
int receiveIdData(DbTagData &, const int &) const
Recibe el miembro data through the channel being passed as parameter.
Definition: CommParameters.cc:396
int sendIdData(const DbTagData &, const int &)
Sends miembro data through the channel being passed as parameter.
Definition: CommParameters.cc:392
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:294
Stiffness material contribution response identifiers.
Definition: ResponseId.h:60
Data to transmit for a pointer «broked».
Definition: BrokedPtrCommMetaData.h:39
DbTagData & getDbTagData(void) const
Returns a vector para almacenar los dbTags de los miembros de la clase.
Definition: MaterialVector.h:484
Object that can move between processes.
Definition: MovableObject.h:91
bool empty(void) const
Returns true ifno se ha asignado material.
Definition: MaterialVector.h:200
m_double getGeneralizedStresses(void) const
Returns generalized stress values on each integration point.
Definition: MaterialVector.h:262
size_t getGeneralizedStressSize(void) const
Returns the size of stress vector.
Definition: MaterialVector.h:252
int recvData(const CommParameters &)
Receives object through the channel being passed as parameter.
Definition: MaterialVector.h:512
m_double getGeneralizedStrain(const int &defID) const
Returns the defID component of generalized strain vector on each integration point.
Definition: MaterialVector.h:426
int commitState(void)
Commits materials state (normally after convergence).
Definition: MaterialVector.h:218
MaterialVector(const size_t &nMat, const MAT *matModel=nullptr)
Default constructor.
Definition: MaterialVector.h:100
Material pointer container. It&#39;s used by elements to store materials for each integration point...
Definition: MaterialVector.h:46
size_t getGeneralizedStrainSize(void) const
Returns the size of generalized strains vector.
Definition: MaterialVector.h:257
void setInitialGeneralizedStrains(const std::vector< Vector > &)
Assigns initial values to materials initial strains.
Definition: MaterialVector.h:444
int receive(DbTagData &, const CommParameters &, const CommMetaData &)
Recibe el objeto.
Definition: DbTagData.cc:106
int recvSelf(const CommParameters &)
Receives object through the channel being passed as parameter.
Definition: MaterialVector.h:548
void inicComm(const int &dataSize) const
Initializes communication.
Definition: DistributedBase.cc:57
int revertToStart(void)
Return materials to its initial state.
Definition: MaterialVector.h:241
double getMeanGeneralizedStressByName(const std::string &) const
Returns the component of the average generalized stress vector which corresponds to the code being pa...
Definition: MaterialVector.h:379
void alloc(const std::vector< MAT * > &mats)
Copy materials from another vector.
Definition: MaterialVector.h:117
Communication parameters between processes.
Definition: CommParameters.h:65
int sendData(CommParameters &)
Send object members through the channel being passed as parameter.
Definition: MaterialVector.h:492
================================================================================
Definition: ContinuaReprComponent.h:34
Data about the index, size,,... of the object to transmit.
Definition: CommMetaData.h:38
const int & getDbTagDataPos(const int &i) const
Returns the data at the i-th position.
Definition: DistributedBase.cc:49
int sendBrokedPtr(MovableObject *ptr, DbTagData &, const BrokedPtrCommMetaData &)
Envía a pointer to movable object through the channel being passed as parameter.
Definition: CommParameters.cc:996
void setDbTagDataPos(const int &i, const int &v)
Sets the data at the i-th position.
Definition: DistributedBase.cc:53
m_double getGeneralizedStress(const int &defID) const
Returns the defID component of generalized stress vector on each integration point.
Definition: MaterialVector.h:407
int sendSelf(CommParameters &)
Sends object through the channel being passed as parameter.
Definition: MaterialVector.h:534
int revertToLastCommit(void)
Returns materials to its last commited state.
Definition: MaterialVector.h:229
void zeroInitialGeneralizedStrains(void)
Initialize initial strains.
Definition: MaterialVector.h:474