Simpatico  v1.10
ddMd/chemistry/Group.h
1 #ifndef DDMD_GROUP_H
2 #define DDMD_GROUP_H
3 
4 /*
5 * Simpatico - Simulation Package for Polymeric and Molecular Liquids
6 *
7 * Copyright 2010 - 2017, The Regents of the University of Minnesota
8 * Distributed under the terms of the GNU General Public License.
9 */
10 
11 #include <ddMd/communicate/Plan.h> // member
12 #include <ddMd/communicate/Buffer.h> // method implementation
13 
14 
15 namespace DdMd
16 {
17 
18  class Atom;
19 
20  // Forward declarations required for friend declarations
21 
22  template <int N> class Group;
23 
24  template <int N>
25  std::istream& operator >> (std::istream& in, Group<N> &group);
26 
27  template <int N>
28  std::ostream& operator << (std::ostream& out, const Group<N> &group);
29 
30  template <class Archive, int N>
31  void serialize(Archive& ar, Group<N>& group, const unsigned int version);
32 
33  using namespace Util;
34 
49  template <int N>
50  class Group
51  {
52  public:
53 
57  static int packedSize();
58 
62  Group();
63 
67  void clear();
68 
74  void setId(int id);
75 
81  void setTypeId(int typeId);
82 
89  void setAtomId(int i, int atomId);
90 
97  void setAtomPtr(int i, Atom* atomPtr);
98 
104  void clearAtomPtr(int i);
105 
109  Plan& plan();
110 
111  // Accessors
112 
116  int id() const;
117 
121  int typeId() const;
122 
128  int atomId(int i) const;
129 
135  Atom* atomPtr(int i) const;
136 
140  int nPtr() const;
141 
145  const Plan& plan() const;
146 
152  void pack(Buffer& buffer);
153 
165  void unpack(Buffer& buffer);
166 
167  private:
168 
170  Atom* atomPtrs_[N];
171 
173  int atomIds_[N];
174 
176  int typeId_;
177 
179  int id_;
180 
182  int nPtr_;
183 
184  // Communication Plan.
185  Plan plan_;
186 
187  //friends:
188 
189  friend
190  std::istream& operator >> <> (std::istream& in, Group<N> &group);
191 
192  friend
193  std::ostream& operator << <> (std::ostream& out, const Group<N> &group);
194 
195  template <class Archive>
196  friend
197  void serialize(Archive& ar, Group<N>& group, const unsigned int version);
198 
199  };
200 
201  // Friend function declarations
202 
210  template <int N>
211  std::istream& operator>>(std::istream& in, Group<N> &group);
212 
222  template <int N>
223  std::ostream& operator<<(std::ostream& out, const Group<N> &group);
224 
235  template <class Archive, int N>
236  void serialize(Archive& ar, Group<N>& group,
237  const unsigned int version);
238 
239  // Member function definitions
240 
241  /*
242  * Constructor
243  */
244  template <int N>
246  : typeId_(-1),
247  id_(-1),
248  nPtr_(0)
249  {
250  for (int i=0; i < N; ++i) {
251  atomIds_[i] = -1;
252  atomPtrs_[i] = 0;
253  }
254  plan_.clearFlags();
255  }
256 
257  /*
258  * Set group to empty initial state.
259  */
260  template <int N>
262  {
263  typeId_ = -1;
264  id_ = -1;
265  nPtr_ = 0;
266  for (int i=0; i < N; ++i) {
267  atomIds_[i] = -1;
268  atomPtrs_[i] = 0;
269  }
270  plan_.clearFlags();
271  }
272 
273  /*
274  * Set the global id for this group.
275  */
276  template <int N>
277  inline void Group<N>::setId(int id)
278  { id_ = id; }
279 
280  /*
281  * Set the group type id for this group.
282  */
283  template <int N>
284  inline void Group<N>::setTypeId(int typeId)
285  { typeId_ = typeId; }
286 
287  /*
288  * Set the id for a specific atom.
289  */
290  template <int N>
291  inline void Group<N>::setAtomId(int i, int atomId)
292  { atomIds_[i] = atomId; }
293 
294  /*
295  * Set the pointer to a specific atom.
296  */
297  template <int N>
299  {
300  if (atomPtr == 0) {
301  UTIL_THROW("Attempt to set null pointer");
302  }
303  if (atomPtrs_[i] == 0) {
304  ++nPtr_;
305  }
306  atomPtrs_[i] = atomPtr;
307  }
308 
309  /*
310  * Clear the pointer to a specific atom.
311  */
312  template <int N>
313  inline void Group<N>::clearAtomPtr(int i)
314  {
315  if (atomPtrs_[i] != 0) {
316  --nPtr_;
317  atomPtrs_[i] = 0;
318  }
319  }
320 
321  /*
322  * Get communication plan by reference.
323  */
324  template <int N>
326  { return plan_; }
327 
328  // Accessors
329 
330  /*
331  * Get the global id for this group.
332  */
333  template <int N>
334  inline int Group<N>::id() const
335  { return id_; }
336 
337  /*
338  * Get the typeId for this group.
339  */
340  template <int N>
341  inline int Group<N>::typeId() const
342  { return typeId_; }
343 
344  /*
345  * Get the id for a specific atom in the Group.
346  */
347  template <int N>
348  inline int Group<N>::atomId(int i) const
349  { return atomIds_[i]; }
350 
351  /*
352  * Get a pointer to a specific Atom.
353  */
354  template <int N>
355  inline Atom* Group<N>::atomPtr(int i) const
356  { return atomPtrs_[i]; }
357 
358  /*
359  * Return the number of non-null atom pointers in this group.
360  */
361  template <int N>
362  inline int Group<N>::nPtr() const
363  { return nPtr_; }
364 
365  /*
366  * Get communication plan (const reference).
367  */
368  template <int N>
369  inline const Plan& Group<N>::plan() const
370  { return plan_; }
371 
372  // Friend function definitions.
373 
374  /*
375  * Input a Group<N> from an istream, without line breaks.
376  */
377  template <int N>
378  std::istream& operator >> (std::istream& in, Group<N> &group)
379  {
380  in >> group.id_;
381  in >> group.typeId_;
382  for (int i=0; i < N; ++i) {
383  in >> group.atomIds_[i];
384  }
385  return in;
386  }
387 
388  /*
389  * Output a Group to an ostream, without line breaks.
390  */
391  template <int N>
392  std::ostream& operator << (std::ostream& out, const Group<N> &group)
393  {
394  out.width(10);
395  out << group.id_;
396  out.width(10);
397  out << group.typeId_;
398  for (int i = 0; i < N; ++i) {
399  out.width(10);
400  out << group.atomIds_[i];
401  }
402  return out;
403  }
404 
405  // Partial specializations of serialize template
406 
407  template <class Archive>
408  void serialize(Archive& ar, Group<2>& group, const unsigned int version)
409  {
410  ar & group.id_;
411  ar & group.typeId_;
412  for (int i = 0; i < 2; ++i) {
413  ar & group.atomIds_[i];
414  }
415  }
416 
417  template <class Archive>
418  void serialize(Archive& ar, Group<3>& group, const unsigned int version)
419  {
420  ar & group.id_;
421  ar & group.typeId_;
422  for (int i = 0; i < 3; ++i) {
423  ar & group.atomIds_[i];
424  }
425  }
426 
427  template <class Archive>
428  void serialize(Archive& ar, Group<4>& group, const unsigned int version)
429  {
430  ar & group.id_;
431  ar & group.typeId_;
432  for (int i = 0; i < 4; ++i) {
433  ar & group.atomIds_[i];
434  }
435  }
436 
437  /*
438  * Packed size of one Group<N> object.
439  */
440  template <int N>
442  { return (2 + N)*sizeof(int) + sizeof(unsigned int); }
443 
444  /*
445  * Pack a Group at end of a send buffer.
446  */
447  template <int N>
448  void Group<N>::pack(Buffer& buffer)
449  {
450  buffer.pack<int>(id());
451  buffer.pack<int>(typeId());
452  for (int j = 0; j < N; ++j) {
453  buffer.pack<int>(atomId(j));
454  }
455  buffer.pack<unsigned int>(plan().flags());
456  buffer.incrementSendSize();
457  }
458 
459  /*
460  * Unpack a Group from a receive buffer.
461  */
462  template <int N>
463  void Group<N>::unpack(Buffer& buffer)
464  {
465  int i;
466  buffer.unpack<int>(i);
467  setId(i);
468  buffer.unpack<int>(i);
469  setTypeId(i);
470  for (int j = 0; j < N; ++j) {
471  buffer.unpack<int>(i);
472  setAtomId(j, i);
473  clearAtomPtr(j);
474  }
475  unsigned int ui;
476  buffer.unpack<unsigned int>(ui);
477  plan().setFlags(ui);
478  buffer.decrementRecvSize();
479  }
480 
481 }
482 #endif
void serialize(Archive &ar, AtomType &atomType, const unsigned int version)
Serialize an AtomType.
void pack(Buffer &buffer)
Pack a Group into a send buffer.
int typeId() const
Get the typeId for this group.
std::istream & operator>>(std::istream &in, AtomType &atomType)
istream extractor (>>) for an AtomType.
unsigned int flags() const
Return raw flags unsigned int.
Definition: Plan.h:128
void unpack(Buffer &buffer)
Unpack a Group from the recv buffer.
void setAtomPtr(int i, Atom *atomPtr)
Set the pointer to a specific atom.
void setFlags(unsigned int flags)
Set all flags (contains all bits).
Definition: Plan.h:98
void clear()
Set group to empty initial state.
friend void serialize(Archive &ar, Group< N > &group, const unsigned int version)
Serialize one Group<N>.
void clearFlags()
Clear all flags (set all to false, set flags_ = 0).
Definition: Plan.h:104
Atom * atomPtr(int i) const
Get a pointer to a specific Atom.
Group()
Constructor.
A point particle in an MD simulation.
Parallel domain decomposition (DD) MD simulation.
void unpack(T &data)
Function template unpacking one variable from the receive buffer.
Definition: Buffer.h:578
friend std::istream & operator>>(std::istream &in, Group< N > &group)
istream extractor (>>) for a Group.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
void setTypeId(int typeId)
Set the group type id for this group.
void incrementSendSize()
Increment sendSize counter after packing an item (an Atom or Group).
Definition: Buffer.h:604
Utility classes for scientific computation.
Definition: accumulators.mod:1
Buffer for interprocessor communication.
Definition: Buffer.h:217
void decrementRecvSize()
Decrement recvSize counter after completely unpacking an item.
Definition: Buffer.h:610
void pack(const T &data)
Function template for packing one variable into the send buffer.
Definition: Buffer.h:563
Communication plan.
Definition: Plan.h:45
A group of covalently interacting atoms.
int nPtr() const
Return the number of non-null atom pointers in this group.
static int packedSize()
Return packed size of a Group<N> object in a send buffer, in bytes.
void setAtomId(int i, int atomId)
Set the id for a specific atom.
void clearAtomPtr(int i)
Clear the pointer to a specific atom.
int id() const
Get the global id for this group.
int atomId(int i) const
Get the id for a specific atom in the Group.
Plan & plan()
Get communication plan by reference.
void setId(int id)
Set the global id for this group.