xc
ModelComponentContainer.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 //ModelComponentContainer.h
29 
30 #ifndef ModelComponentContainer_H
31 #define ModelComponentContainer_H
32 
33 #include "preprocessor/multi_block_topology/ModelComponentContainerBase.h"
34 #include <map>
35 #include "boost/python/list.hpp"
36 
37 namespace XC {
38 
40 //
42 template <class T>
43 class ModelComponentContainer: public ModelComponentContainerBase, public std::map<ModelComponentContainerBase::Indice,T *>
44  {
45  private:
46  int sendObjects(Communicator &);
47  int receiveObjects(const size_t &sz,const Communicator &);
48  protected:
49  virtual int sendData(Communicator &);
50  virtual int recvData(const Communicator &);
51  public:
52  typedef typename std::map<Indice,T *> map_base;
53  typedef typename std::pair<Indice,T *> pair;
54  typedef typename map_base::iterator iterator;
55  typedef typename map_base::const_iterator const_iterator;
56 
58  virtual ~ModelComponentContainer(void);
59 
60  T * busca(const Indice &);
61  const T * busca(const Indice &) const;
62  bool exists(const Indice &) const;
63  T * get(const Indice &);
64  boost::python::list getKeys(void) const;
65 
66  void clearAll(void);
67 
68  };
69 
71 template <class T>
74 
77 template <class T>
78 T * ModelComponentContainer<T>::busca(const Indice &id)
79  {
80  T * retval= nullptr;
81  iterator i= this->find(id);
82  if(i!= this->end()) //Entity exists.
83  retval= (*i).second;
84  return retval;
85  }
86 
88 template <class T>
89 const T * ModelComponentContainer<T>::busca(const Indice &id) const
90  {
91  const T * retval= nullptr;
92  const_iterator i= this->find(id);
93  if(i!= this->end()) //Entity exists.
94  retval= (*i).second;
95  return retval;
96  }
97 
99 template <class T>
100 bool ModelComponentContainer<T>::exists(const Indice &id) const
101  { return (busca(id)!=nullptr); }
102 
103 
105 template <class T>
106 T * ModelComponentContainer<T>::get(const size_t &iEnt)
107  {
108  T *retval= busca(iEnt);
109  if(!retval)
110  std::cerr << getClassName() << "::" << __FUNCTION__
111  << "; entity: '"
112  << iEnt << "' not found.\n";
113  return retval;
114  }
115 
117 template <class T>
119  {
120  for(iterator i=this->begin();i!=this->end();i++)
121  {
122  T * tmp= (*i).second;
123  if(tmp)
124  delete tmp;
125  tmp= nullptr;
126  }
127  this->clear();
128  }
129 
131 template <class T>
132 boost::python::list ModelComponentContainer<T>::getKeys(void) const
133  {
134  boost::python::list retval;
135  for(const_iterator i=this->begin();i!=this->end();i++)
136  retval.append((*i).first);
137  return retval;
138  }
139 
141 template <class T>
143  { clearAll(); }
144 
146 template <class T>
148  {
149  const int size= this->size();
150  int res= 0;
151  if(size>0)
152  {
153  for(const_iterator i=this->begin();i!=this->end();i++)
154  {
155  T *ptr= (*i).second;
156  MovableObject *tmp= dynamic_cast<MovableObject *>(ptr);
157  if(tmp)
158  {
159  tmp->setDbTag(comm);
160  res= tmp->sendSelf(comm);
161  if(res<0)
162  {
163  std::cerr << getClassName() << "::" << __FUNCTION__
164  << "; object failed in sendSelf.\n";
165  break;
166  }
167  }
168  else
169  std::cerr << getClassName() << "::" << __FUNCTION__
170  << "; the object is not movable." << std::endl;
171  }
172  }
173  return res;
174  }
175 
178 template <class T>
179 int XC::ModelComponentContainer<T>::receiveObjects(const size_t &sz, const Communicator &comm)
180  {
181  int res= 0;
182 
183  // We need to "broke" the object here just as they do
184  // with TaggedObjectStorage.
185  // static T t_object= T();
186  // for(size_t i= 0;i<sz;i++)
187  // {
188  // res+= t_object.recvSelf(comm);
189  // T *ptr= dynamic_cast<T *>(t_object.getCopy());
190  // (*this)[t_object.getTag()]= ptr;
191  // }
192  return res;
193  }
194 
196 template <class T>
198  {
199  int res= ModelComponentContainerBase::sendData(comm);
200  const size_t sz= this->size();
201  res+= comm.sendSzt(sz,getDbTagData(),CommMetaData(1));
202  if(sz>0)
203  { res+= sendObjects(comm); }
204  return res;
205  }
206 
208 template <class T>
210  {
211  int res= ModelComponentContainerBase::recvData(comm);
212  size_t sz= 0;
213  res+= comm.receiveSzt(sz,getDbTagData(),CommMetaData(1));
214  if(sz>0)
215  { res+= receiveObjects(sz, comm); }
216  return res;
217  }
218 
219 } //end of XC namespace
220 #endif
virtual ~ModelComponentContainer(void)
Destructor.
Definition: ModelComponentContainer.h:142
Communication parameters between processes.
Definition: Communicator.h:66
void setDbTag(int dbTag)
Sets the object identifier in the database.
Definition: MovableObject.cpp:111
Object that can move between processes.
Definition: MovableObject.h:100
Model geometry manager.
Definition: MultiBlockTopology.h:68
size_t sendSzt(const size_t &, DbTagData &, const CommMetaData &)
Sends entero through the communicator argument.
Definition: Communicator.cc:1014
Data about the index, size,,...
Definition: CommMetaData.h:39
virtual int sendSelf(Communicator &comm)=0
Send the object.
boost::python::list getKeys(void) const
Return the container&#39;s keys.
Definition: ModelComponentContainer.h:132
void clearAll(void)
Erase all the entities.
Definition: ModelComponentContainer.h:118
Base class for model component containers.
Definition: ModelComponentContainerBase.h:44
bool exists(const Indice &) const
Return true if the entity already exists.
Definition: ModelComponentContainer.h:100
size_t receiveSzt(size_t &, DbTagData &, const CommMetaData &) const
Receives the integers through the communicator argument.
Definition: Communicator.cc:1070
ModelComponentContainer(MultiBlockTopology *mbt=nullptr)
Constructor.
Definition: ModelComponentContainer.h:72
Open source finite element program for structural analysis.
Definition: ContinuaReprComponent.h:35
virtual int recvData(const Communicator &)
Receive data through the communicator argument.
Definition: ModelComponentContainer.h:209
Container for model entities.
Definition: ModelComponentContainer.h:43
virtual int sendData(Communicator &)
Send members through the communicator argument.
Definition: ModelComponentContainer.h:197
T * busca(const Indice &)
Return a pointer to the geometry entity whose identifier is passed as parameter.
Definition: ModelComponentContainer.h:78