Simpatico  v1.10
Generator.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 <util/global.h>
9 #include "Generator.h"
10 #include <mcMd/simulation/Simulation.h>
11 #include <mcMd/simulation/System.h>
12 #include <mcMd/species/SpeciesMutator.h>
13 #include <mcMd/neighbor/CellList.h>
14 #include <simp/species/Species.h>
15 
16 namespace McMd
17 {
18 
19  using namespace Util;
20  using namespace Simp;
21 
22  /*
23  * Constructor.
24  */
25  Generator::Generator(Species& species, System& system)
26  : speciesPtr_(&species),
27  simulationPtr_(&system.simulation()),
28  systemPtr_(&system),
29  boundaryPtr_(&system.boundary())
30  #ifdef SIMP_BOND
31  , bondPotentialPtr_(0)
32  #endif
33  {}
34 
35  /*
36  * Destructor (virtual because it is an abstract base class).
37  */
39  {}
40 
41  #ifdef SIMP_BOND
42  /*
43  * Set bond potential for molecules that have bonds.
44  */
46  { bondPotentialPtr_ = &bondPotential; }
47  #endif
48 
49  /*
50  * Attempt to place an atom.
51  */
53  Array<double> const & diameters,
54  CellList& cellList)
55  {
56  // Shift atom position to primary image within boundary
57  boundary().shift(atom.position());
58  Vector& pos = atom.position();
59 
60  // Loop over neighbors, check distance to each.
61  // Return false immediately if any neighbor is too close.
62  CellList::NeighborArray neighbors;
63  cellList.getNeighbors(pos, neighbors);
64  double di = diameters[atom.typeId()];
65  double rSq, dj, dSq;
66  Atom* neighborPtr;
67  int n = neighbors.size();
68  for (int j = 0; j < n; ++j) {
69  neighborPtr = neighbors[j];
70  rSq = boundary().distanceSq(neighborPtr->position(), pos);
71  dj = diameters[neighborPtr->typeId()];
72  dSq = 0.5*(di + dj);
73  dSq *= dSq;
74  if (rSq < dSq) {
75  return false;
76  }
77  }
78 
79  // If all neighbors are allowed, add atom to cellList
80  cellList.addAtom(atom);
81  return true;
82  }
83 
84  /*
85  * Generate random molecules
86  */
87  bool Generator::generate(int nMolecule,
88  Array<double> const & diameters,
89  CellList& cellList)
90  {
91  // Preconditions
92  UTIL_CHECK(nMolecule <= species().capacity());
94 
95  // Attempt to place all molecules in Species
96  int speciesId = species().id();
97  int maxAttempt = 200;
98  int iAttempt;
99  bool success;
100  bool isMutable;
101  Species* speciesPtr;
102  for (int iMol = 0; iMol < nMolecule; ++iMol) {
103  Molecule &newMolecule= simulation().getMolecule(speciesId);
104  system().addMolecule(newMolecule);
105  speciesPtr = &system().simulation().species(speciesId);
106  isMutable = speciesPtr->isMutable();
107  if (isMutable) {
108  speciesPtr->mutator().setMoleculeState(newMolecule, 0);
109  }
110  success = false;
111  iAttempt = 0;
112  while (!success && iAttempt < maxAttempt) {
113  success = attemptPlaceMolecule(newMolecule,
114  diameters, cellList);
115  ++iAttempt;
116  }
117  if (!success) {
118  system().removeMolecule(newMolecule);
119  Log::file() << "Failed to insert Linear molecule "
120  << iMol << "\n";
121  return false;
122  }
123  }
124  return true;
125  }
126 
127  /*
128  * Setup cell list for use.
129  */
130  void
131  Generator::setupCellList(int atomCapacity,
133  Array<double> const & diameters,
134  CellList& cellList)
135  {
136  // Set maximum atom id = atomCapacity - 1, and allocate
137  // arrays in cellList that are indexed by atom id
138  cellList.setAtomCapacity(atomCapacity);
139 
140  // Compute maximum exclusion diameter
141  double maxDiameter = 0.0;
142  for (int iType = 0; iType < diameters.capacity(); iType++) {
143  if (diameters[iType] > maxDiameter) {
144  maxDiameter = diameters[iType];
145  }
146  }
147 
148  // Setup grid of empty cells
149  cellList.setup(boundary, maxDiameter);
150  }
151 
152 }
static void setupCellList(int atomCapacity, Boundary &boundary, const Array< double > &diameters, CellList &cellList)
Allocate any required memory for the cell list.
Definition: Generator.cpp:131
void setBondPotential(BondPotential &bondPotential)
Create an association with a BondPotential.
Definition: Generator.cpp:45
A Vector is a Cartesian vector.
Definition: Vector.h:75
void getNeighbors(const Vector &pos, NeighborArray &neighbors) const
Fill a NeighborArray with pointers to atoms near a specified position.
System & system()
Get the associated System by reference.
Definition: Generator.h:196
double distanceSq(const Vector &r1, const Vector &r2) const
Return square distance between positions r1 and r2.
const BondPotential & bondPotential()
Get the associated BondPotential by reference.
Definition: Generator.h:209
virtual bool attemptPlaceMolecule(Molecule &molecule, const Array< double > &diameters, CellList &cellList)=0
Attempt to insert an entire molecule (pure virtual).
An orthorhombic periodic unit cell.
int atomCapacity() const
Get the total number of Atoms allocated.
Generator(Species &species, System &system)
Constructor.
Definition: Generator.cpp:25
A set of interacting Molecules enclosed by a Boundary.
Definition: System.h:115
int atomCapacity() const
Get the maximum allowed atom index + 1.
File containing preprocessor macros for error handling.
Classes used by all simpatico molecular simulations.
Molecule & getMolecule(int speciesId)
Get a new molecule from a reservoir of unused Molecule objects.
virtual ~Generator()
Destructor.
Definition: Generator.cpp:38
const Species & species()
Get the associated Species by reference.
Definition: Generator.h:184
Simulation & simulation() const
Get the parent Simulation by reference.
Definition: System.h:1055
void addMolecule(Molecule &molecule)
Add a Molecule to this System.
Definition: System.cpp:1111
int typeId() const
Get type index for this Atom.
A point particle within a Molecule.
Utility classes for scientific computation.
Definition: accumulators.mod:1
const Boundary & boundary() const
Get the associated Boundary by reference.
Definition: Generator.h:202
void setup(const Boundary &boundary, double cutoff)
Setup grid of empty cells.
void shift(Vector &r) const
Shift Cartesian Vector r to its primary image.
virtual void setMoleculeState(Molecule &molecule, int stateId)=0
Change the state of a specific molecule.
Simulation & simulation()
Get the associated Simulation by reference.
Definition: Generator.h:190
A cell list for Atom objects in a periodic system boundary.
static std::ostream & file()
Get log ostream by reference.
Definition: Log.cpp:57
int id() const
Get integer id of this Species.
Single-processor Monte Carlo (MC) and molecular dynamics (MD).
bool isMutable() const
Is this a mutable Species?
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition: global.h:68
Abstract Bond Potential class.
void setAtomCapacity(int atomCapacity)
Set atom capacity.
int capacity() const
Return allocated size.
Definition: Array.h:153
virtual bool generate(int nMolecule, Array< double > const &diameters, CellList &cellList)
Generate nMolecule molecules of the associated Species.
Definition: Generator.cpp:87
A physical molecule (a set of covalently bonded Atoms).
void removeMolecule(Molecule &molecule)
Remove a specific molecule from this System.
Definition: System.cpp:1129
const Vector & position() const
Get the position Vector by const reference.
McMd::SpeciesMutator & mutator()
Return the species mutator object by reference.
A Species represents a set of chemically similar molecules.
bool attemptPlaceAtom(Atom &atom, const Array< double > &diameters, CellList &cellList)
Attempt to place an atom.
Definition: Generator.cpp:52
Species & species(int i)
Get a specific Species by reference.
int size() const
Return logical size of this array (i.e., number of elements).
Definition: FSArray.h:207
void addAtom(Atom &atom)
Add a Atom to the appropriate cell, based on its position.