Simpatico  v1.10
AtomArray.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 "AtomArray.h"
9 #include "Atom.h"
10 #include <util/misc/Memory.h>
11 
12 #include <stdlib.h>
13 
14 namespace DdMd
15 {
16 
17  using namespace Util;
18 
19  /*
20  * Constructor.
21  *
22  * The data_ and capacity_ are nullified in Array<Atom> constructor.
23  */
25  : Array<Atom>(),
26  velocities_(0),
27  masks_(0),
28  plans_(0),
29  ids_(0),
30  groups_(0),
31  contexts_(0)
32  {}
33 
34  /*
35  * Destructor.
36  */
38  {
39  if (data_) {
40  Memory::deallocate<Atom>(data_, capacity_);
41  Memory::deallocate<Vector>(velocities_, capacity_);
42  Memory::deallocate<Mask>(masks_, capacity_);
43  Memory::deallocate<Plan>(plans_, capacity_);
44  Memory::deallocate<int>(ids_, capacity_);
45  Memory::deallocate<unsigned int>(groups_, capacity_);
46  if (contexts_) {
47  Memory::deallocate<AtomContext>(contexts_, capacity_);
48  }
49  capacity_ = 0;
50  }
51  }
52 
53  /*
54  * Allocate the underlying C array.
55  */
57  {
58  if (!(data_ == 0)) {
59  UTIL_THROW("Cannot re-allocate an AtomArray");
60  }
61  if (capacity <= 0) {
62  UTIL_THROW("Cannot allocate with capacity <= 0");
63  }
64  if (sizeof(Atom) != 64) {
65  Log::file() << "Warning: sizeof(Atom) != 64" << std::endl;
66  Log::file() << "Size of Atom = " << sizeof(Atom) << std::endl;
67  Log::file() << "Size of Atom* = " << sizeof(Atom*) << std::endl;
68  }
69 
70  // Allocate memory
71  //posix_memalign((void**) &data_, 64, capacity*sizeof(Atom));
72  Memory::allocate<Atom>(data_, capacity);
73  Memory::allocate<Vector>(velocities_, capacity);
74  Memory::allocate<Mask>(masks_, capacity);
75  Memory::allocate<Plan>(plans_, capacity);
76  Memory::allocate<int>(ids_, capacity);
77  Memory::allocate<unsigned int>(groups_, capacity);
78  if (Atom::hasAtomContext()) {
79  Memory::allocate<AtomContext>(contexts_, capacity);
80  }
82 
83  // Initialize values.
84  for (int i = 0; i < capacity_; ++i) {
85  data_[i].localId_ = (i << 1);
86  data_[i].arrayPtr_ = this;
87  masks_[i].clear();
88  plans_[i].clearFlags();
89  ids_[i] = -1;
90  groups_[i] = 0;
91  if (Atom::hasAtomContext()) {
92  contexts_[i].clear();
93  }
94  }
95 
96  }
97 
98  /*
99  * Set all forces to zero.
100  */
102  {
103  for (int i = 0; i < capacity_; ++i) {
104  data_[i].force_[0] = 0.0;
105  data_[i].force_[1] = 0.0;
106  data_[i].force_[2] = 0.0;
107  }
108  }
109 
110  /*
111  * Return true if this is already allocated, false otherwise.
112  */
114  { return (bool)data_; }
115 
116 }
AtomArray()
Constructor.
Definition: AtomArray.cpp:24
void allocate(int capacity)
Allocate memory on the heap.
Definition: AtomArray.cpp:56
Atom * data_
Pointer to an array of Data elements.
Definition: Array.h:103
bool isAllocated() const
Return true if this is already allocated, false otherwise.
Definition: AtomArray.cpp:113
virtual ~AtomArray()
Destructor.
Definition: AtomArray.cpp:37
int capacity_
Allocated size of the data_ array.
Definition: Array.h:106
Array container class template.
Definition: AutoCorrArray.h:28
A point particle in an MD simulation.
Parallel domain decomposition (DD) MD simulation.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
Utility classes for scientific computation.
Definition: accumulators.mod:1
static bool hasAtomContext()
Is AtomContext data enabled?
void clear()
Set all data members to null default values.
Definition: AtomContext.h:70
static std::ostream & file()
Get log ostream by reference.
Definition: Log.cpp:57
int capacity() const
Return allocated size.
void zeroForces()
Set force vector to zero for all atoms in this array.
Definition: AtomArray.cpp:101