Simpatico  v1.10
PairSelector.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 "PairSelector.h"
9 #include <mcMd/chemistry/Atom.h>
10 #include <util/param/Parameter.h>
11 
12 namespace McMd
13 {
14 
15  using namespace Util;
16 
17  /*
18  * Constructor
19  */
21  : pairType_(ALL),
22  atom1TypeId_(-1),
23  atom2TypeId_(-1),
24  avoidDoubleCounting_(false)
25  {}
26 
27  /*
28  * Set policy to avoid double counting (true) or not (true).
29  */
30  void PairSelector::setAvoidDoubleCounting(bool avoidDoubleCounting)
31  { avoidDoubleCounting_ = avoidDoubleCounting; }
32 
36  bool PairSelector::match(const Atom& atom1, const Atom& atom2) const
37  {
38 
39  // Check molecular identities
40  if (pairType_ == INTRA && &atom1.molecule() != &atom2.molecule() ) {
41  return false;
42  } else
43  if (pairType_ == INTER && &atom1.molecule() == &atom2.molecule() ) {
44  return false;
45  }
46 
47  // Exclude case where atom1 and atom2 are identical
48  if (&atom1 == &atom2) {
49  return false;
50  }
51 
52  // Check atom types
53  if (atom1TypeId_ >= 0 && atom1.typeId() != atom1TypeId_) {
54  return false;
55  }
56  if (atom2TypeId_ >= 0 && atom2.typeId() != atom2TypeId_) {
57  return false;
58  }
59 
60  // Check for double counting
61  if (avoidDoubleCounting_) {
62  if (atom1TypeId_ < 0 && atom2TypeId_ < 0) {
63  if (atom1.id() > atom2.id()) {
64  return false;
65  }
66  } else
67  if (atom1.typeId() == atom2.typeId()) {
68  if (atom1.id() > atom2.id()) {
69  return false;
70  }
71  }
72  }
73 
74  // If the pair has passed all previous tests, accept it.
75  return true;
76 
77  }
78 
79  /*
80  * Input a PairSelector from an istream, without line breaks.
81  */
82  std::istream& operator>>(std::istream& in, PairSelector &selector)
83  {
84  in >> selector.pairType_;
85  in >> selector.atom1TypeId_;
86  in >> selector.atom2TypeId_;
87  return in;
88  }
89 
90  /*
91  * Output a PairSelector to an ostream, without line breaks.
92  */
93  std::ostream& operator<<(std::ostream& out, const PairSelector &selector)
94  {
95  out.width(Parameter::Width);
96  out << selector.pairType_;
97  out.width(10);
98  out << selector.atom1TypeId_;
99  out.width(10);
100  out << selector.atom2TypeId_;
101  return out;
102  }
103 
104  /*
105  * Input a PairSelector::PairType from an istream.
106  */
107  std::istream& operator>>(std::istream& in, PairSelector::PairType &type)
108  {
109  std::string buffer;
110  in >> buffer;
111  if (buffer == "intra" || buffer == "Intra" || buffer == "INTRA") {
112  type = PairSelector::INTRA;
113  } else
114  if (buffer == "inter" || buffer == "Inter" || buffer == "INTER") {
115  type = PairSelector::INTER;
116  } else
117  if (buffer == "all" || buffer == "All" || buffer == "ALL") {
118  type = PairSelector::ALL;
119  } else {
120  UTIL_THROW("Unknown PairSelector::Type");
121  }
122  return in;
123  }
124 
125  /*
126  * Output a PairSelector to an ostream, without line breaks.
127  */
128  std::ostream& operator<<(std::ostream& out, const PairSelector::PairType &type)
129  {
130  if (type == PairSelector::INTRA) {
131  out << "Intra";
132  } else
133  if (type == PairSelector::INTER) {
134  out << "Inter";
135  } else
136  if (type == PairSelector::ALL) {
137  out << "All";
138  } else {
139  UTIL_THROW("Unknown PairSelector::PairType");
140  }
141  return out;
142  }
143 
144 }
145 
146 #ifdef UTIL_MPI
147 namespace Util
148 {
149 
150  // Initialize MpiTraits<McMd::PairSelector>
151  MPI::Datatype MpiTraits< McMd::PairSelector>::type = MPI::BYTE;
153 
154  // Initialize MpiTraits<McMd::PairSelector::PairType>
155  MPI::Datatype MpiTraits< McMd::PairSelector::PairType>::type = MPI::INT;
157 
158 }
159 
160 #include <util/mpi/MpiStructBuilder.h>
161 
162 namespace McMd
163 {
164 
165  /*
166  * Commit MPI Datatype.
167  */
169  {
171  MpiStructBuilder builder;
172  PairSelector object;
173  builder.setBase(&object);
174  builder.addMember(&object.pairType_, MpiTraits<PairSelector::PairType>::type);
175  builder.addMember(&object.atom1TypeId_, MPI::INT);
176  builder.addMember(&object.atom2TypeId_, MPI::INT);
179  }
180  }
181 
182 }
183 #endif // ifdef UTIL_MPI
184 
void addMember(void *memberAddress, MPI::Datatype type, int count=1)
Add a new member variable to the type map.
static void commitMpiType()
Commit associated MPI DataType.
Molecule & molecule() const
Get the parent Molecule by reference.
friend std::ostream & operator<<(std::ostream &out, const PairSelector &selector)
ostream inserter (<<) for a PairSelector object.
void setBase(void *objectAddress)
Set address of an class instance.
void commit(MPI::Datatype &newType)
Build and commit a user-defined MPI Struct datatype.
bool match(const Atom &atom1, const Atom &atom2) const
Return true if pair of atoms matches the selector policy.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
int typeId() const
Get type index for this Atom.
A point particle within a Molecule.
Utility classes for scientific computation.
Definition: accumulators.mod:1
PairSelector()
Constructor.
Default MpiTraits class.
Definition: MpiTraits.h:39
friend std::istream & operator>>(std::istream &in, PairSelector &selector)
istream extractor (>>) for a PairSelector object.
int id() const
Get global index for this Atom within the Simulation.
Selection rule for pairs of Atoms.
Definition: PairSelector.h:32
void setAvoidDoubleCounting(bool avoidDoubleCounting)
Set policy to avoid double counting (true) or to not avoid (false).
A MpiStructBuilder objects is used to create an MPI Struct datatype.
PairType
Type of atom pair, based on identity of parent molecules.
Definition: PairSelector.h:45
static const int Width
Width of output field for a scalar variable.
Definition: Parameter.h:53
Single-processor Monte Carlo (MC) and molecular dynamics (MD).