Simpatico  v1.10
ddMd/chemistry/Atom.cpp
1 /*
2 * Simpatico - Simulation Package for Polymeric and Molecular Liquids
3 *
4 * Copyright 2010 - 2017, The Regents of the University of Minnesota
5 * Distributed under the terms of the GNU General Public License.
6 */
7 
8 #include "Atom.h"
9 #include <ddMd/chemistry/Mask.h>
10 #include <ddMd/communicate/Plan.h>
11 #ifdef UTIL_MPI
12 #include <ddMd/communicate/Buffer.h>
13 #endif
14 
15 namespace DdMd
16 {
17 
18  using namespace Util;
19 
20  /*
21  * Initialize hasAtomContext_ flag to false. This disables storage and
22  * communication of AtomContext information by default.
23  */
24  bool Atom::hasAtomContext_ = false;
25 
26  /*
27  * Enable (true) or disable (false) use of AtomContext data.
28  */
29  void Atom::setHasAtomContext(bool hasAtomContext)
30  { hasAtomContext_ = hasAtomContext; }
31 
32  /*
33  * Constructor (private, used only by AtomArray).
34  */
35  Atom::Atom() :
36  position_(0.0),
37  typeId_(-1),
38  localId_(0),
39  force_(0.0),
40  arrayPtr_(0)
41  #ifdef UTIL_32BIT
42  , pad_(0)
43  #endif
44  {}
45 
46  /*
47  * Assignment (public).
48  */
49  Atom& Atom::operator= (const Atom& other)
50  {
51  position_ = other.position_;
52  typeId_ = other.typeId_;
53  force_ = other.force_;
54  setIsGhost(other.isGhost());
55  velocity() = other.velocity();
56  setId(other.id());
57  plan() = other.plan();
58  groups() = other.groups();
59  if (hasAtomContext_) {
60  context() = other.context();
61  }
62  mask() = other.mask();
63  return *this;
64  }
65 
66  /*
67  * Reset integer members to null values.
68  */
69  void Atom::clear()
70  {
71  typeId_ = -1;
72  setIsGhost(false);
73  setId(-1);
74  plan().clearFlags();
75  groups() = 0;
76  if (hasAtomContext_) {
77  context().clear();
78  }
79  mask().clear();
80  }
81 
82  #ifdef UTIL_MPI
83  // Atom Exchange
84 
85  /*
86  * Pack a local Atom for exchange of ownership.
87  */
88  void Atom::packAtom(Buffer& buffer)
89  {
90  buffer.pack<int>(id());
91  buffer.pack<int>(typeId());
92  buffer.pack<Vector>(position());
93  buffer.pack<Vector>(velocity());
94  buffer.pack<unsigned int>(plan().flags());
95  buffer.pack<unsigned int>(groups());
96  if (hasAtomContext_) {
97  buffer.pack<AtomContext>(context());
98  }
99 
100  // Pack Mask
101  Mask& m = mask();
102  int size = m.size();
103  buffer.pack<int>(size);
104  for (int j = 0; j < size; ++j) {
105  buffer.pack<int>(m[j]);
106  }
107 
108  buffer.incrementSendSize();
109  }
110 
111  /*
112  * Unpack and receive ownership of an Atom.
113  */
114  void Atom::unpackAtom(Buffer& buffer)
115  {
116  int i;
117  buffer.unpack<int>(i);
118  setId(i);
119  buffer.unpack<int>(i);
120  setTypeId(i);
121  buffer.unpack<Vector>(position());
122  buffer.unpack<Vector>(velocity());
123  unsigned int ui;
124  buffer.unpack<unsigned int>(ui);
125  plan().setFlags(ui);
126  buffer.unpack<unsigned int>(ui);
127  groups() = ui;
128  if (hasAtomContext_) {
129  buffer.unpack<AtomContext>(context());
130  }
131 
132  // Unpack Mask
133  Mask& m = mask();
134  m.clear();
135  int size;
136  buffer.unpack<int>(size);
137  for (int j = 0; j < size; ++j) {
138  buffer.unpack<int>(i);
139  m.append(i);
140  }
141  assert(m.size() == size);
142 
143  buffer.decrementRecvSize();
144  }
145 
146  /*
147  * Return maximum size of packed Atom, in bytes.
148  */
150  {
151  int size = 0;
152  size += 2*sizeof(int); // id + typeId
153  size += 2*sizeof(Vector); // position + velocity
154  size += 2*sizeof(unsigned int); // plan + groups
155  if (hasAtomContext_) {
156  size += sizeof(AtomContext); // context
157  }
158  size += sizeof(int); // mask size
159  size += Mask::Capacity*sizeof(int); // mask ids
160  return size;
161  }
162 
163  // Ghost Exchange
164 
165  /*
166  * Pack data required for a ghost Atom for sending.
167  */
168  void Atom::packGhost(Buffer& buffer)
169  {
170  Vector pos;
171  buffer.pack<int>(id());
172  buffer.pack<int>(typeId());
173  buffer.pack<Vector>(position());
174  buffer.pack<unsigned int>(plan().flags());
175  if (hasAtomContext_) {
176  buffer.pack<AtomContext>(context());
177  }
178  buffer.incrementSendSize();
179  }
180 
181  /*
182  * Unpack data required for a ghost Atom.
183  */
184  void Atom::unpackGhost(Buffer& buffer)
185  {
186  int i;
187  buffer.unpack<int>(i);
188  setId(i);
189  buffer.unpack<int>(i);
190  setTypeId(i);
191  buffer.unpack<Vector>(position());
192  unsigned int ui;
193  buffer.unpack<unsigned int>(ui);
194  plan().setFlags(ui);
195  if (hasAtomContext_) {
196  buffer.unpack<AtomContext>(context());
197  }
198  buffer.decrementRecvSize();
199  }
200 
201  /*
202  * Return size of one packed Ghost in bytes (static method).
203  */
205  {
206  int size = 0;
207  size += 2*sizeof(int);
208  size += sizeof(Vector);
209  size += sizeof(unsigned int);
210  if (hasAtomContext_) {
211  size += sizeof(AtomContext);
212  }
213  return size;
214  }
215 
216  // Ghost Position Updates
217 
218  /*
219  * Pack updates ghost position.
220  */
221  void Atom::packUpdate(Buffer& buffer)
222  {
223  buffer.pack<Vector>(position());
224  buffer.incrementSendSize();
225  }
226 
227  /*
228  * Pack updated ghost position.
229  */
231  {
232  buffer.unpack<Vector>(position());
233  buffer.decrementRecvSize();
234  }
235 
236  // Force Updates
237 
238  /*
239  * Pack ghost force.
240  */
241  void Atom::packForce(Buffer& buffer)
242  {
243  buffer.pack<Vector>(force());
244  buffer.incrementSendSize();
245  }
246 
247  /*
248  * Unpack data ghost Atom force, and add to on this processor.
249  */
250  void Atom::unpackForce(Buffer& buffer)
251  {
252  Vector f;
253  buffer.unpack<Vector>(f);
254  force() += f;
255  buffer.decrementRecvSize();
256  }
257  #endif // ifdef UTIL_MPI
258 
259  /*
260  * Copy ghost atom data from sendAtom to this Atom.
261  */
262  void Atom::copyLocalGhost(const Atom& sendAtom)
263  {
264  setId(sendAtom.id());
265  setTypeId(sendAtom.typeId());
266  plan().setFlags(sendAtom.plan().flags());
267  position() = sendAtom.position();
268  if (hasAtomContext_) {
269  context() = sendAtom.context();
270  }
271  }
272 
273  /*
274  * Copy update position of local ghost from sendAtom to this.
275  */
276  void Atom::copyLocalUpdate(const Atom& sendAtom)
277  { position_ = sendAtom.position(); }
278 
279 }
Set of Atoms for which pair interactions with a parent Atom are "masked".
int typeId() const
Get atom type index.
A Vector is a Cartesian vector.
Definition: Vector.h:75
void unpackUpdate(Buffer &buffer)
Unpack updated ghost position from recv buffer.
void clear()
Reset integer members to initial null values.
unsigned int flags() const
Return raw flags unsigned int.
Definition: Plan.h:128
void unpackForce(Buffer &buffer)
Unpack updated position of ghost Atom from recv buffer.
void packAtom(Buffer &buffer)
Pack an Atom into a send buffer, for exchange of ownership.
Vector & position()
Get position Vector by reference.
static int packedAtomSize()
Return max size of an atom packed for exchange, in bytes.
A point particle in an MD simulation.
Parallel domain decomposition (DD) MD simulation.
void unpackAtom(Buffer &buffer)
Unpack an atom from a recv buffer and receive ownership.
int id() const
Get unique global index for this atom.
void copyLocalUpdate(Atom const &sendAtom)
Copies position of local atom to update this ghost atom.
void unpack(T &data)
Function template unpacking one variable from the receive buffer.
Definition: Buffer.h:578
void packForce(Buffer &buffer)
Pack update of ghost Atom force into send buffer.
bool isGhost() const
Is this atom a ghost?
void packGhost(Buffer &buffer)
Pack a ghost Atom into a send buffer.
unsigned int & groups()
Get groups bit field by non-const reference.
Descriptor for context of an Atom within a molecule and species.
Definition: AtomContext.h:21
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
void packUpdate(Buffer &buffer)
Pack updated ghost position into send buffer.
Buffer for interprocessor communication.
Definition: Buffer.h:217
AtomContext & context()
Get the AtomContext struct by non-const reference.
void copyLocalGhost(Atom const &sendAtom)
Copies data from local atom to update this ghost atom.
static void setHasAtomContext(bool hasAtomContext)
Enable (true) or disable (false) use of AtomContext data.
Atom & operator=(Atom const &other)
Assignment.
Vector & velocity()
Get velocity Vector by reference.
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
static int packedGhostSize()
Return size of ghost atom packed for communication, in bytes.
void unpackGhost(Buffer &buffer)
Unpack a ghost Atom from a recv buffer.
Plan & plan()
Get communication plan by reference.
Mask & mask()
Get the associated Mask by reference.