xc
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/kernel/CommandEntity.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 CommandEntity, 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::const_iterator const_iterator;
61  typedef typename mat_vector::reference reference;
62  typedef typename mat_vector::const_reference const_reference;
63 
64  MaterialVector(const size_t &nMat,const MAT *matModel= nullptr);
67  ~MaterialVector(void)
68  { clearAll(); }
69 
70  void setMaterial(const MAT *);
71  void setMaterial(size_t i,MAT *);
72  void setMaterial(const MAT *,const std::string &);
73  bool empty(void) const;
74  int commitState(void);
75  int revertToLastCommit(void);
76  int revertToStart(void);
77 
78  void setInitialGeneralizedStrains(const std::vector<Vector> &);
79  void addInitialGeneralizedStrains(const std::vector<Vector> &);
81 
82  size_t getGeneralizedStressSize(void) const;
83  size_t getGeneralizedStrainSize(void) const;
84  m_double getGeneralizedStresses(void) const;
85  m_double getGeneralizedStrains(void) const;
86  const Vector &getMeanGeneralizedStress(void) const;
87  const Vector &getMeanGeneralizedStrain(void) const;
88  double getMeanGeneralizedStrain(const int &defID) const;
89  double getMeanGeneralizedStress(const int &defID) const;
90  double getMeanGeneralizedStrainByName(const std::string &) const;
91  double getMeanGeneralizedStressByName(const std::string &) const;
92  m_double getGeneralizedStrain(const int &defID) const;
93  m_double getGeneralizedStress(const int &defID) const;
94 
95  std::set<std::string> getNames(void) const;
96  boost::python::list getNamesPy(void) const;
97 
98  int sendSelf(CommParameters &);
99  int recvSelf(const CommParameters &);
100  };
101 
103 template <class MAT>
104 MaterialVector<MAT>::MaterialVector(const size_t &nMat,const MAT *matModel)
105  : std::vector<MAT *>(nMat,nullptr), MovableObject(MAT_VECTOR_TAG)
106  {
107  if(matModel)
108  {
109  for(iterator i= mat_vector::begin();i!=mat_vector::end();i++)
110  {
111  (*i)= matModel->getCopy();
112  if(!(*i))
113  std::cerr << getClassName() << "::" << __FUNCTION__
114  << "; failed allocate material model pointer\n";
115  }
116  }
117  }
118 
120 template <class MAT>
121 void MaterialVector<MAT>::alloc(const std::vector<MAT *> &mats)
122  {
123  clearAll();
124  const size_t nMat= mats.size();
125  this->resize(nMat);
126  for(size_t i= 0;i<nMat;i++)
127  {
128  if(mats[i])
129  {
130  (*this)[i]= mats[i]->getCopy();
131  if(!(*this)[i])
132  std::cerr << getClassName() << "::" << __FUNCTION__
133  << "; failed allocate material model pointer\n";
134  }
135  }
136  }
137 
139 template <class MAT>
141  : std::vector<MAT *>(other.size(),nullptr), MovableObject(MAT_VECTOR_TAG)
142  { alloc(other); }
143 
145 template <class MAT>
147  {
148  alloc(other);
149  return *this;
150  }
151 
152 template <class MAT>
153 void MaterialVector<MAT>::setMaterial(const MAT *new_mat)
154  {
155  clear_materials();
156  if(new_mat)
157  {
158  for(iterator i= mat_vector::begin();i!=mat_vector::end();i++)
159  {
160  (*i)= new_mat->getCopy();
161  if(!(*i))
162  std::cerr << getClassName() << "::" << __FUNCTION__
163  << "; failed allocate material model pointer\n";
164  }
165  }
166  }
167 
168 template <class MAT>
169 void MaterialVector<MAT>::setMaterial(const MAT *new_mat, const std::string &type)
170  {
171  clear_materials();
172  if(new_mat)
173  {
174  for(iterator i= mat_vector::begin();i!=mat_vector::end();i++)
175  {
176  (*i)= new_mat->getCopy(type.c_str());
177  if(!(*i))
178  std::cerr << getClassName() << "::" << __FUNCTION__
179  << "; failed allocate material model pointer\n";
180  }
181  }
182  }
183 
184 template <class MAT>
185 void MaterialVector<MAT>::setMaterial(size_t i,MAT *new_mat)
186  {
187  if((*this)[i])
188  delete (*this)[i];
189  (*this)[i]= new_mat;
190  }
191 
192 template <class MAT>
194  {
195  for(iterator i= mat_vector::begin();i!=mat_vector::end();i++)
196  {
197  if(*i) delete (*i);
198  (*i)= nullptr;
199  }
200  }
201 
203 template <class MAT>
205  {
206  if(mat_vector::empty())
207  return true;
208  else
209  return ((*this)[0]==nullptr);
210  }
211 
212 template <class MAT>
214  {
215  clear_materials();
216  std::vector<MAT *>::clear();
217  }
218 
219 
221 template <class MAT>
223  {
224  int retVal= 0;
225 
226  for(iterator i=mat_vector::begin();i!=mat_vector::end();i++)
227  retVal+= (*i)->commitState();
228  return retVal;
229  }
230 
232 template <class MAT>
234  {
235  int retVal= 0;
236 
237  for(iterator i=mat_vector::begin();i!=mat_vector::end();i++)
238  retVal+= (*i)->revertToLastCommit() ;
239  return retVal;
240  }
241 
242 
244 template <class MAT>
246  {
247  int retVal = 0;
248 
249  for(iterator i=mat_vector::begin();i!=mat_vector::end();i++)
250  retVal+= (*i)->revertToStart() ;
251  return retVal;
252  }
253 
255 template <class MAT>
257  { return (*this)[0]->getGeneralizedStress().Size(); }
258 
260 template <class MAT>
262  { return (*this)[0]->getGeneralizedStrain().Size(); }
263 
265 template <class MAT>
267  {
268  const size_t ncol= getGeneralizedStressSize();
269  const size_t nMat= this->size();
270  m_double retval(nMat,ncol,0.0);
271  for(size_t i= 0;i<nMat;i++)
272  {
273  const Vector &s= (*this)[i]->getGeneralizedStress();
274  for(size_t j= 0;j<ncol;j++)
275  retval(i,j)= s(j);
276  }
277  return retval;
278  }
279 
281 template <class MAT>
283  {
284  const size_t ncol= getGeneralizedStrainSize();
285  const size_t nMat= this->size();
286  m_double retval(nMat,ncol,0.0);
287  for(size_t i= 0;i<nMat;i++)
288  {
289  const Vector &s= (*this)[i]->getGeneralizedStrain();
290  for(size_t j= 0;j<ncol;j++)
291  retval(i,j)= s(j);
292  }
293  return retval;
294  }
295 
297 template <class MAT>
299  {
300  static Vector retval;
301  retval= (*this)[0]->getGeneralizedStress();
302  const size_t nMat= this->size();
303  for(size_t i= 1;i<nMat;i++)
304  retval+= (*this)[i]->getGeneralizedStress();
305  retval/=nMat;
306  return retval;
307  }
308 
310 template <class MAT>
312  {
313  static Vector retval;
314  retval= (*this)[0]->getGeneralizedStrain();
315  const size_t nMat= this->size();
316  for(size_t i= 0;i<nMat;i++)
317  retval+= (*this)[i]->getGeneralizedStrain();
318  retval/=nMat;
319  return retval;
320  }
321 
324 template <class MAT>
325 double MaterialVector<MAT>::getMeanGeneralizedStrain(const int &defID) const
326  {
327  double retval= 0.0;
328  const Vector &e= getMeanGeneralizedStrain(); //generalized strains vector.
329  const ResponseId &code= (*this)[0]->getType();
330  const int order= code.Size();
331  for(register int i= 0;i<order;i++)
332  if(code(i) == defID)
333  retval+= e(i);
334  return retval;
335  }
336 
339 template <class MAT>
340 double MaterialVector<MAT>::getMeanGeneralizedStrainByName(const std::string &cod) const
341  {
342  double retval= 0.0;
343  if(cod == "n1")
344  retval= this->getMeanGeneralizedStrain(MEMBRANE_RESPONSE_n1);
345  else if(cod == "n2")
346  retval= this->getMeanGeneralizedStrain(MEMBRANE_RESPONSE_n2);
347  else if(cod == "m1") //Bending around the axis 1.
348  retval= this->getMeanGeneralizedStrain(PLATE_RESPONSE_m1);
349  else if(cod == "m2") //Bending around the axis 2.
350  retval= this->getMeanGeneralizedStrain(PLATE_RESPONSE_m2);
351  else if(cod == "q13")
352  retval= this->getMeanGeneralizedStrain(PLATE_RESPONSE_q13);
353  else if(cod == "q23")
354  retval= this->getMeanGeneralizedStrain(PLATE_RESPONSE_q23);
355  else if(cod == "m12")
356  retval= this->getMeanGeneralizedStrain(PLATE_RESPONSE_m12);
357  else if(cod == "n12")
358  retval= this->getMeanGeneralizedStrain(MEMBRANE_RESPONSE_n12);
359  else
360  std::cerr << getClassName() << "::" << __FUNCTION__
361  << "stress code: '" << cod << " unknown." << std::endl;
362  return retval;
363  }
364 
367 template <class MAT>
368 double MaterialVector<MAT>::getMeanGeneralizedStress(const int &defID) const
369  {
370  double retval= 0.0;
371  const Vector &f= getMeanGeneralizedStress(); //Vector de esfuerzos.
372  const ResponseId &code= (*this)[0]->getType();
373  const int order= code.Size();
374  for(register int i= 0;i<order;i++)
375  if(code(i) == defID)
376  retval+= f(i);
377  return retval;
378  }
379 
382 template <class MAT>
383 double MaterialVector<MAT>::getMeanGeneralizedStressByName(const std::string &cod) const
384  {
385  double retval= 0.0;
386  if(cod == "n1") //Esfuerzo axil medio per unit length, parallel to the axis 1.
387  retval= this->getMeanGeneralizedStress(MEMBRANE_RESPONSE_n1);
388  else if(cod == "n2") //Esfuerzo axil medio per unit length, parallel to the axis 2.
389  retval= this->getMeanGeneralizedStress(MEMBRANE_RESPONSE_n2);
390  else if(cod == "n12")
391  retval= this->getMeanGeneralizedStress(MEMBRANE_RESPONSE_n12);
392  else if(cod == "m1") //Flector medio per unit length, around the axis 1.
393  retval= this->getMeanGeneralizedStress(PLATE_RESPONSE_m1);
394  else if(cod == "m2") //Flector medio per unit length, around the axis 2.
395  retval= this->getMeanGeneralizedStress(PLATE_RESPONSE_m2);
396  else if(cod == "m12")
397  retval= this->getMeanGeneralizedStress(PLATE_RESPONSE_m12);
398  else if(cod == "q13")
399  retval= this->getMeanGeneralizedStress(PLATE_RESPONSE_q13);
400  else if(cod == "q23")
401  retval= this->getMeanGeneralizedStress(PLATE_RESPONSE_q23);
402  else
403  std::cerr << getClassName() << "::" << __FUNCTION__
404  << "stress code: '" << cod << " unknown." << std::endl;
405  return retval;
406  }
407 
410 template <class MAT>
411 m_double MaterialVector<MAT>::getGeneralizedStress(const int &defID) const
412  {
413  const size_t nMat= this->size();
414  m_double retval(nMat,1,0.0);
415  const ResponseId &code= (*this)[0]->getType();
416  const int order= code.Size();
417  for(size_t i= 0;i<nMat;i++)
418  {
419  const Vector &s= (*this)[i]->getGeneralizedStress();
420  for(register int j= 0;j<order;j++)
421  if(code(j) == defID)
422  retval(i,1)+= s(i);
423  }
424  return retval;
425  }
426 
429 template <class MAT>
430 m_double MaterialVector<MAT>::getGeneralizedStrain(const int &defID) const
431  {
432  const size_t nMat= this->size();
433  m_double retval(nMat,1,0.0);
434  const ResponseId &code= (*this)[0]->getType();
435  const int order= code.Size();
436  for(size_t i= 0;i<nMat;i++)
437  {
438  const Vector &s= (*this)[i]->getGeneralizedStrain();
439  for(register int j= 0;j<order;j++)
440  if(code(j) == defID)
441  retval(i,1)+= s(i);
442  }
443  return retval;
444  }
445 
447 template <class MAT>
448 void MaterialVector<MAT>::setInitialGeneralizedStrains(const std::vector<Vector> &iS)
449  {
450  const size_t nMat= this->size();
451  const size_t sz= std::min(nMat,iS.size());
452  if(iS.size()<nMat)
453  std::cerr << getClassName() << "::" << __FUNCTION__
454  << "; received: "
455  << iS.size() << " generalized strain vectors, expected: "
456  << nMat << ".\n";
457  for(size_t i= 0;i<sz;i++)
458  (*this)[i]->setInitialGeneralizedStrain(iS[i]);
459  }
460 
462 template <class MAT>
463 void MaterialVector<MAT>::addInitialGeneralizedStrains(const std::vector<Vector> &iS)
464  {
465  const size_t nMat= this->size();
466  const size_t sz= std::min(nMat,iS.size());
467  if(iS.size()<nMat)
468  std::cerr << getClassName() << "::" << __FUNCTION__
469  << "; received: "
470  << iS.size() << " generalized strain vectors, expected: "
471  << nMat << ".\n";
472  for(size_t i= 0;i<sz;i++)
473  (*this)[i]->addInitialGeneralizedStrain(iS[i]);
474  }
475 
477 template <class MAT>
479  {
480  const size_t nMat= this->size();
481  for(size_t i= 0;i<nMat;i++)
482  (*this)[i]->zeroInitialGeneralizedStrain();
483  }
484 
487 template <class MAT>
489  {
490  static DbTagData retval(2);
491  return retval;
492  }
493 
495 template <class MAT>
497  {
498  int res= 0;
499  if(this->empty())
500  setDbTagDataPos(0,0);
501  else
502  {
503  setDbTagDataPos(0,1);
504  const size_t nMat= this->size();
505  DbTagData cpMat(nMat*3);
506 
507  for(size_t i= 0;i<nMat;i++)
508  res+= cp.sendBrokedPtr((*this)[i],cpMat,BrokedPtrCommMetaData(i,i+nMat,i+2*nMat));
509  res+= cpMat.send(getDbTagData(),cp,CommMetaData(1));
510  }
511  return res;
512  }
513 
515 template <class MAT>
517  {
518  const int flag = getDbTagDataPos(0);
519  int res= 0;
520  if(flag!=0)
521  {
522  const size_t nMat= this->size();
523  DbTagData cpMat(nMat*3);
524  res+= cpMat.receive(getDbTagData(),cp,CommMetaData(1));
525 
526  for(size_t i= 0;i<nMat;i++)
527  {
528  const BrokedPtrCommMetaData meta(i,i+nMat,i+2*nMat);
529  // Receive the material
530  (*this)[i]= cp.getBrokedMaterial((*this)[i],cpMat,meta);
531  }
532  }
533  return res;
534  }
535 
537 template <class MAT>
538 std::set<std::string> MaterialVector<MAT>::getNames(void) const
539  {
540  std::set<std::string> retval;
541  for(const_iterator i= mat_vector::begin();i!=mat_vector::end();i++)
542  retval.insert((*i)->getName());
543  return retval;
544  }
545 
547 template <class MAT>
548 boost::python::list MaterialVector<MAT>::getNamesPy(void) const
549  {
550  boost::python::list retval;
551  std::set<std::string> tmp= getNames();
552  for(std::set<std::string>::const_iterator i= tmp.begin();i!=tmp.end();i++)
553  retval.append(*i);
554  return retval;
555  }
556 
558 template <class MAT>
560  {
561  inicComm(2);
562  int res= sendData(cp);
563  const int dataTag=getDbTag();
564  res+= cp.sendIdData(getDbTagData(),dataTag);
565  if(res < 0)
566  std::cerr << getClassName() << "::" << __FUNCTION__
567  << dataTag << " failed to send ID";
568  return res;
569  }
570 
572 template <class MAT>
574  {
575  const int dataTag= this->getDbTag();
576  inicComm(2);
577  int res= cp.receiveIdData(getDbTagData(),dataTag);
578  if(res<0)
579  std::cerr << getClassName() << "::" << __FUNCTION__
580  << dataTag << " failed to receive ID\n";
581  else
582  res+= recvData(cp);
583  return res;
584  }
585 
586 } // end of XC namespace
587 
588 #endif
Float vector abstraction.
Definition: Vector.h:93
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:340
void addInitialGeneralizedStrains(const std::vector< Vector > &)
Adds to the materials initial strains the values being passed as parameters.
Definition: MaterialVector.h:463
int sendIdData(const DbTagData &, const int &)
Sends miembro data through the channel being passed as parameter.
Definition: CommParameters.cc:392
void alloc(const std::vector< MAT *> &mats)
Copy materials from another vector.
Definition: MaterialVector.h:121
int revertToLastCommit(void)
Returns materials to its last commited state.
Definition: MaterialVector.h:233
std::set< std::string > getNames(void) const
Return the names of the materials.
Definition: MaterialVector.h:538
int sendSelf(CommParameters &)
Sends object through the channel being passed as parameter.
Definition: MaterialVector.h:559
void setInitialGeneralizedStrains(const std::vector< Vector > &)
Assigns initial values to materials initial strains.
Definition: MaterialVector.h:448
Object that can move between processes.
Definition: MovableObject.h:99
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:383
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:298
bool empty(void) const
Returns true ifno se ha asignado material.
Definition: MaterialVector.h:204
int receiveIdData(DbTagData &, const int &) const
Receives el miembro data through the channel being passed as parameter.
Definition: CommParameters.cc:396
Vector that stores the dbTags of the class members.
Definition: DbTagData.h:43
Data about the index, size,,...
Definition: CommMetaData.h:38
int sendData(CommParameters &)
Send object members through the channel being passed as parameter.
Definition: MaterialVector.h:496
boost::python::list getNamesPy(void) const
Return the names of the materials in a python list.
Definition: MaterialVector.h:548
size_t getGeneralizedStrainSize(void) const
Returns the size of generalized strains vector.
Definition: MaterialVector.h:261
int send(DbTagData &, CommParameters &, const CommMetaData &) const
Sends the object.
Definition: DbTagData.cc:102
int recvData(const CommParameters &)
Receives object through the channel being passed as parameter.
Definition: MaterialVector.h:516
MaterialVector< MAT > & operator=(const MaterialVector< MAT > &)
Assignment operator.
Definition: MaterialVector.h:146
Material pointer container.
Definition: MaterialVector.h:46
int receive(DbTagData &, const CommParameters &, const CommMetaData &)
Receive the object.
Definition: DbTagData.cc:106
size_t getGeneralizedStressSize(void) const
Returns the size of stress vector.
Definition: MaterialVector.h:256
m_double getGeneralizedStrain(const int &defID) const
Returns the defID component of generalized strain vector on each integration point.
Definition: MaterialVector.h:430
m_double getGeneralizedStrains(void) const
Returns generalized strain values on each integration point.
Definition: MaterialVector.h:282
Stiffness material contribution response identifiers.
Definition: ResponseId.h:60
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:311
int commitState(void)
Commits materials state (normally after convergence).
Definition: MaterialVector.h:222
m_double getGeneralizedStresses(void) const
Returns generalized stress values on each integration point.
Definition: MaterialVector.h:266
void zeroInitialGeneralizedStrains(void)
Initialize initial strains.
Definition: MaterialVector.h:478
Data to transmit for a pointer «broked».
Definition: BrokedPtrCommMetaData.h:39
int recvSelf(const CommParameters &)
Receives object through the channel being passed as parameter.
Definition: MaterialVector.h:573
Open source finite element program for structural analysis.
Definition: ContinuaReprComponent.h:34
Communication parameters between processes.
Definition: CommParameters.h:65
DbTagData & getDbTagData(void) const
Returns a vector to store the dbTags of the class members.
Definition: MaterialVector.h:488
int revertToStart(void)
Return materials to its initial state.
Definition: MaterialVector.h:245
m_double getGeneralizedStress(const int &defID) const
Returns the defID component of generalized stress vector on each integration point.
Definition: MaterialVector.h:411
MaterialVector(const size_t &nMat, const MAT *matModel=nullptr)
Default constructor.
Definition: MaterialVector.h:104
int sendBrokedPtr(MovableObject *ptr, DbTagData &, const BrokedPtrCommMetaData &)
Sends a pointer to movable object through the channel being passed as parameter.
Definition: CommParameters.cc:1104
int Size(void) const
Returns the vector size.
Definition: ID.h:113