Simpatico  v1.10
Cluster.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 "Cluster.h"
9 #include <util/global.h>
10 
11 using namespace Util;
12 
13 namespace McMd
14 {
15 
16  Cluster::Cluster() :
17  id_(-1),
18  size_(0),
19  head_(0)
20  {}
21 
23  {}
24 
26  {
27  ClusterLink* next = 0;
28  ClusterLink* ptr = head_;
29  while (ptr) {
30  next = ptr->next();
31  ptr->clear();
32  ptr = next;
33  }
34  id_ = -1;
35  size_ = 0;
36  head_ = 0;
37  }
38 
39  void Cluster::setId(int id)
40  {
41  if (id < 0) {
42  UTIL_THROW("Cluster id cannot be negative");
43  }
44  id_ = id;
45  ClusterLink* ptr = head_;
46  while (ptr) {
47  ptr->clusterId_ = id;
48  ptr = ptr->next();
49  }
50  }
51 
53  {
54  link.clusterId_ = id_;
55  link.next_ = head_;
56  head_ = &link;
57  ++size_;
58  }
59 
60  bool Cluster::isValid() const
61  {
62  int n = 0;
63  ClusterLink* ptr = head_;
64  while (ptr) {
65  ++n;
66  if (ptr->clusterId() != id_) {
67  return false;
68  }
69  ptr = ptr->next();
70  }
71  if (n != size_) {
72  return false;
73  }
74  return true;
75  }
76 
77  Vector Cluster::clusterCOM(int atomTypeInCluster, Boundary const & boundary)
78  {
79  ClusterLink* thisClusterStart;
80  ClusterLink* next;
81  Vector com;
82  Vector dr;
83  com.zero();
84  thisClusterStart = head();
85  Vector r0 = (thisClusterStart->molecule()).atom(0).position();
86  Molecule thisMolecule;
88  int nAtomsInCluster = 0;
89 
90 
91  com.zero();
92  while(thisClusterStart) {
93  next = thisClusterStart->next();
94  thisMolecule = thisClusterStart->molecule();
95  thisMolecule.begin(atomIter);
96  for( ; atomIter.notEnd(); ++atomIter) {
97  if (atomIter->typeId() == atomTypeInCluster) {
98  boundary.distanceSq(atomIter->position(),r0,dr);
99  com += dr;
100  nAtomsInCluster += 1;
101  }
102  }
103  thisClusterStart = next;
104  }
105  com /= nAtomsInCluster;
106  com += r0;
107  boundary.shift(com);
108  return com;
109  }
110 
111  Tensor Cluster::momentTensor(int atomTypeInCluster, Boundary const & boundary)
112  {
113  Vector com = clusterCOM( atomTypeInCluster, boundary);
114  Tensor rgTensor;
115  rgTensor.zero();
116  ClusterLink* thisClusterStart;
117  ClusterLink* next;
118  thisClusterStart = head();
119  Molecule thisMolecule;
121  Vector dr;
122  Tensor rgDyad;
123  int nAtomsInCluster = 0;
124  while(thisClusterStart) {
125  next = thisClusterStart->next();
126  thisMolecule = thisClusterStart->molecule();
127  thisMolecule.begin(atomIter);
128  for( ; atomIter.notEnd(); ++atomIter) {
129  if (atomIter->typeId() == atomTypeInCluster) {
130  nAtomsInCluster += 1;
131  boundary.distanceSq(atomIter->position(), com,dr);
132  rgTensor += rgDyad.dyad(dr,dr);
133  }
134  }
135  thisClusterStart = next;
136  }
137  rgTensor /= nAtomsInCluster;
138  return rgTensor;
139  }
140 
141 }
Vector & zero()
Set all elements of a 3D vector to zero.
Definition: Vector.h:514
A Vector is a Cartesian vector.
Definition: Vector.h:75
double distanceSq(const Vector &r1, const Vector &r2) const
Return square distance between positions r1 and r2.
An orthorhombic periodic unit cell.
Tensor & zero()
Set all elements of this tensor to zero.
Definition: Tensor.h:441
void begin(AtomIterator &iterator)
Set an Molecule::AtomIterator to first Atom in this Molecule.
bool isValid() const
Return true if valid, false otherwise.
Definition: Cluster.cpp:60
void clear()
Set cluster to empty.
Definition: Cluster.cpp:25
File containing preprocessor macros for error handling.
A Tensor represents a Cartesian tensor.
Definition: Tensor.h:32
Forward const iterator for an Array or a C array.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
ClusterLink * head() const
Get a pointer to the first link in the linked list.
Definition: Cluster.h:80
Utility classes for scientific computation.
Definition: accumulators.mod:1
int id() const
Get the cluster id.
Definition: Cluster.h:66
void shift(Vector &r) const
Shift Cartesian Vector r to its primary image.
~Cluster()
Destructor.
Definition: Cluster.cpp:22
Single-processor Monte Carlo (MC) and molecular dynamics (MD).
void setId(int id)
Set cluster identifier.
Definition: Cluster.cpp:39
Vector clusterCOM(int atomTypeInCluster, Boundary const &boundary)
Return the cluster COM.
Definition: Cluster.cpp:77
A physical molecule (a set of covalently bonded Atoms).
bool notEnd() const
Is this not the end of the array?
Tensor momentTensor(int atomTypeInCluster, Boundary const &boundary)
Return the cluster radius of gyration tensor.
Definition: Cluster.cpp:111
Tensor & dyad(const Vector &v1, const Vector &v2)
Create dyad of two vectors.
Definition: Tensor.h:763
void addLink(ClusterLink &link)
Add a link to the list.
Definition: Cluster.cpp:52