GranOO  3.0
A robust and versatile workbench to build 3D dynamic simulations based on the Discrete Element Method
Pair.hpp
Go to the documentation of this file.
1 // This file is part of GranOO, a workbench for DEM simulation.
2 //
3 // Author(s) : - Damien Andre IRCER/UNILIM, Limoges France
4 // <damien.andre@unilim.fr>
5 // - Jean-luc Charles Arts et Metiers ParisTech, CNRS, I2M, Bordeaux France
6 // <jean-luc.charles@ensam.eu>
7 // - Jeremie Girardot Arts et Metiers ParisTech, CNRS, I2M, Bordeaux France
8 // <jeremie.girardot@ensam.eu>
9 // - Cedric Hubert LAMIH/UPHF, Valenciennes France
10 // <cedric.hubert@uphf.fr>
11 // - Ivan Iordanoff Arts et Metiers ParisTech, CNRS, I2M, Bordeaux France
12 // <ivan.iordanoff@ensam.eu>
13 //
14 // Copyright (C) 2008-2019 D. Andre, JL. Charles, J. Girardot, C. Hubert, I. Iordanoff
15 //
16 // This program is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 // This program is distributed in the hope that it will be useful,
22 // but WITHOUT ANY WARRANTY; without even the implied warranty of
23 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 // GNU General Public License for more details.
25 //
26 // You should have received a copy of the GNU General Public License
27 // along with this program. If not, see <http://www.gnu.org/licenses/>.
28 
29 
30 #ifndef _LibPhysic_Pair_H
31 #define _LibPhysic_Pair_H
32 
33 #include "GranOO3/Core/Base.hpp"
35 
36 namespace GranOO3
37 {
38  namespace Core
39  {
40 
41  // The most simple interaction between two particles.
42  // This class is not designed to be derived.
43  // This class must replace the bond class for generic interaction between particles
44  template<typename type>
45  class Pair : public Base,
46  public PropClass< Pair<type> >,
47  public Register< Pair<type> >
48  {
49 
50  GRANOO_CLASS_T(Core::Pair, type, Base);
51 
52  public:
53  // Connectivity management
54  static void update_connectivity_map();
55  static SetOfBase<Pair<type> >& get_connection_for(const type&);
56  static unsigned int get_number_of_connection_for(const type& item);
57 
58  public:
59  // Constructors and destructor
60  Pair(type& p1, type& p2);
61  ~Pair();
62 
63  type& get_item1();
64  const type& get_item1() const;
65  type& get_item2();
66  const type& get_item2() const;
67 
68  type& item1();
69  type& item2();
70 
71  // IO
72  std::ostream& write_ascii (std::ostream& out) const;
73  std::istream& read_ascii (std::istream& in);
74  std::string info() const;
75  void draw();
76 
77  private:
78  Pair(const Pair&) = delete;
79  Pair & operator=(const Pair &) = delete;
80 
81  //BOOST SERIALIZATION
83  template<class Archive> void serialize(Archive&, const unsigned int);
84 
85  private:
86  static std::map<const type*, SetOfBase<Pair<type> > > _connectivity_map;
87 
88  protected:
89  type& item1_;
90  type& item2_;
91  };
92 
93  template<typename type> std::map<const type*, SetOfBase<Pair<type> > >
94  Pair<type>::_connectivity_map = std::map<const type*, SetOfBase<Pair<type> > >();
95 
96  template<typename type> void
98  _connectivity_map.clear();
99  SetOf<Pair<type> >& set = SetOf<Pair<type> >::get();
100  for (auto& it: set) {
101  _connectivity_map[&it->item1_].add_item(it);
102  _connectivity_map[&it->item2_].add_item(it);
103  }
104  }
105 
106  template<typename type> SetOfBase<Pair<type> >&
107  Pair<type>::get_connection_for(const type& item) {
108  AssertMsg(_connectivity_map.size() > 0, "The connectivity map is null, you must update the connectivity map before");
109  AssertMsg(_connectivity_map.count(&item) == 1, "Can't find the required item in the connectivity map, maybe you need to update it before");
110  return _connectivity_map[&item];
111  }
112 
113  template<typename type> unsigned int
115  AssertMsg(_connectivity_map.size() > 0, "The connectivity map is null, you must update the connectivity map before");
116  if (_connectivity_map.count(&item) == 0)
117  return 0;
118 
119  return _connectivity_map[&item].size();
120  }
121 
122  template<typename type>
123  Pair<type>::Pair(type& p1, type& p2)
124  : item1_(p1), item2_(p2) {
125  }
126 
127  template<typename type>
129  }
130 
131  template<typename type> type&
133  return item1_;
134  }
135 
136  template<typename type> const type&
138  return item1_;
139  }
140 
141  template<typename type> type&
143  return item2_;
144  }
145 
146  template<typename type> const type&
148  return item2_;
149  }
150 
151  template<typename type> type&
153  return item1_;
154  }
155 
156  template<typename type> type&
158  return item2_;
159  }
160 
161  template<typename type> std::string
163  std::ostringstream os;
164  os << "pair between "
165  << item1_.Core::template Register<type>::get_numeric_ID()
166  << " and "
167  << item2_.Core::template Register<type>::get_numeric_ID()
168  << "\n";
169  return os.str();
170  }
171 
172  template<typename type> std::ostream&
173  Pair<type>::write_ascii (std::ostream& out) const {
174  out << item1_.numID() << '\t' << item2_.numID();
175  return out;
176  }
177 
178  template<typename type> std::istream&
179  Pair<type>::read_ascii (std::istream& in) {
180  return in;
181  }
182 
183  template<typename type> void
185 
186  }
187 
188  template<typename type>
189  template<class Archive>
190  inline void
191  Pair<type>::serialize(Archive & ar, const unsigned int ) {
192  ar & boost::serialization::base_object<Base>(*this);
193  }
194 
195  }
196 }
197 
198 
199 
200 // need placement new snippet because no default constructor exist
201 namespace boost
202 {
203  namespace serialization
204  {
205 
206  template<typename type, class Archive>
207  void save_construct_data(Archive & ar,
209  const unsigned int) {
210  const type* item1 = &t->get_item1();
211  const type* item2 = &t->get_item2();
212  ar << item1;
213  ar << item2;
214  }
215 
216  template<typename type, class Archive>
217  void load_construct_data(Archive & ar,
219  const unsigned int) {
220  type* item1 = nullptr;
221  type* item2 = nullptr;
222  ar >> item1;
223  ar >> item2;
224  ::new(t)GranOO3::Core::Pair<type>(*item1,*item2);
225  }
226 
227  }
228 }
229 
230 
231 
232 namespace GranOO3
233 {
234  namespace Core
235  {
236 
237  template<class type>
238  struct Read<Pair<type> >
239  {
240  static Base* new_object(const std::string& classID, std::istream& in) {
241  int num1, num2;
242  in >> num1 >> num2;
243  type& item1 = SetOf<type>::get_item_by_base_class_rank(num1);
244  type& item2 = SetOf<type>::get_item_by_base_class_rank(num2);
245  Pair<type>* pt = new Pair<type>(item1, item2);
246  pt->read_ascii(in);
247  return pt;
248  }
249 
250  };
251 
252  }
253 }
254 
255 
256 
257 
258 #endif
259 
#define AssertMsg(condition, message)
Definition: Macro.hpp:67
Definition: Base.hpp:61
Base & item()
Definition: Base.hpp:237
Definition: Pair.hpp:48
std::istream & read_ascii(std::istream &in)
Definition: Pair.hpp:179
static std::map< const type *, SetOfBase< Pair< type > > > _connectivity_map
Definition: Pair.hpp:86
void serialize(Archive &, const unsigned int)
Definition: Pair.hpp:191
type & get_item1()
Definition: Pair.hpp:132
Pair(const Pair &)=delete
std::string info() const
Definition: Pair.hpp:162
static unsigned int get_number_of_connection_for(const type &item)
Definition: Pair.hpp:114
static SetOfBase< Pair< type > > & get_connection_for(const type &)
Definition: Pair.hpp:107
type & item1_
Definition: Pair.hpp:89
std::ostream & write_ascii(std::ostream &out) const
Definition: Pair.hpp:173
static void update_connectivity_map()
Definition: Pair.hpp:97
type & item1()
Definition: Pair.hpp:152
void draw()
Definition: Pair.hpp:184
type & item2()
Definition: Pair.hpp:157
friend class boost::serialization::access
Definition: Pair.hpp:82
type & get_item2()
Definition: Pair.hpp:142
Pair(type &p1, type &p2)
Definition: Pair.hpp:123
~Pair()
Definition: Pair.hpp:128
type & item2_
Definition: Pair.hpp:90
Pair & operator=(const Pair &)=delete
Definition: PropClass.hpp:47
Definition: SetOf.hpp:346
unsigned long long int get_numeric_ID() const
Definition: SetOf.hpp:153
Definition: SetOf.hpp:236
static type & get_item_by_base_class_rank(size_t i)
Definition: Common.hpp:198
void save_construct_data(Archive &ar, const GranOO3::Core::Pair< type > *t, const unsigned int)
Definition: Pair.hpp:207
void load_construct_data(Archive &ar, GranOO3::Core::Pair< type > *t, const unsigned int)
Definition: Pair.hpp:217
Definition: Pair.hpp:202
x y t t *t x y t t t x y t t t x *y t *t t x *y t *t t x y t t t x y t t t t(t+t)") define_sfop3(16
static Base * new_object(const std::string &classID, std::istream &in)
Definition: Pair.hpp:240
Definition: SetOfManager.hpp:63