Simpatico  v1.10
Crosslinker.cpp
1 #ifndef CROSSLINKER_CPP
2 #define CROSSLINKER_CPP
3 
4 /*
5 * MolMcD - Monte Carlo and Molecular Dynamics Simulator for Molecular Liquids
6 *
7 * Copyright 2010 - 2014, The Regents of the University of Minnesota
8 * Distributed under the terms of the GNU General Public License.
9 */
10 
11 #include "Crosslinker.h"
12 #include <util/format/Dbl.h>
13 #include <util/format/Int.h>
14 #include <util/misc/FileMaster.h>
15 #include <mcMd/simulation/Simulation.h>
16 #include <mcMd/links/LinkMaster.h>
17 
18 #include <sstream>
19 
20 namespace McMd
21 {
22 
23  using namespace Util;
24 
25  // Constructor
27  SystemAnalyzer<System>(system),
28  nSample_(0),
29  cutoff_(0),
30  probability_(0)
31  { setClassName("CrossLinker"); }
32 
33  void Crosslinker::readParameters(std::istream& in)
34  {
35  readInterval(in);
37  read<double>(in, "cutoff", cutoff_);
38  read<double>(in, "probability", probability_);
39 
40  }
41 
43  {
44  // Initialize the private CellList
45  cellList_.setAtomCapacity(system().nAtom());
46  cellList_.setup(system().boundary(), cutoff_);
47 
48  }
49 
50  // Create links and print.
51  void Crosslinker::sample(long iStep)
52  {
53  if (isAtInterval(iStep)) {
54 
55  // Clear the cellList
56  cellList_.clear();
57  // Add every atom in this System to the CellList
59  Atom* atomPtr;
60  for (int iSpec=0; iSpec < system().simulation().nSpecies(); ++iSpec) {
61  for (system().begin(iSpec, molIter); molIter.notEnd(); ++molIter) {
62  for (int ia=0; ia < molIter->nAtom(); ++ia) {
63  atomPtr = &molIter->atom(ia);
64  system().boundary().shift(atomPtr->position());
65  cellList_.addAtom(*atomPtr);
66  }
67  }
68  }
69 
70  //Use the cell list to find neighbours and create links
71  CellList::NeighborArray cellNeighbor;
72  Vector iPos, jPos;
73  Atom *iAtomPtr, *jAtomPtr;
74  double dRSq, cutoffSq=cutoff_*cutoff_;
75  int nCellNeighbor, nCellAtom, totCells;
76  int ic, ip, iAtomId, jp, jAtomId;
77  // Loop over cells containing primary atom. ic = cell index
78  totCells = cellList_.totCells();
79  for (ic = 0; ic < totCells; ++ic) {
80  // Get Array cellNeighbor of Ids of neighbor atoms for cell ic.
81  // Elements 0,..., nCellAtom - 1 contain Ids for atoms in cell ic.
82  // Elements nCellAtom,..., nCellNeighbor-1 are from neighboring cells.
83  cellList_.getCellNeighbors(ic, cellNeighbor, nCellAtom);
84  nCellNeighbor = cellNeighbor.size();
85  // Loop over atoms in cell ic
86  for (ip = 0; ip < nCellAtom; ++ip) {
87  iAtomPtr = cellNeighbor[ip];
88  iPos = iAtomPtr->position();
89  iAtomId = iAtomPtr->id();
90  // Loop over atoms in all neighboring cells, including cell ic.
91  for (jp = 0; jp < nCellNeighbor; ++jp) {
92  jAtomPtr = cellNeighbor[jp];
93  jPos = jAtomPtr->position();
94  jAtomId = jAtomPtr->id();
95  // Avoid double counting: only count pairs with jAtomId > iAtomId
96  if ( jAtomId > iAtomId ) {
97  // Exclude bonded pairs
98  if (!iAtomPtr->mask().isMasked(*jAtomPtr)) {
99  // Calculate distance between atoms i and j
100  dRSq = system().boundary().distanceSq(iPos, jPos);
101  if (dRSq < cutoffSq) {
102  if(system().simulation().random().uniform(0.0, 1.0) < probability_){
103  //create a link between i and j atoms
104  system().linkMaster().addLink(*iAtomPtr, *jAtomPtr, 0);
105  }
106  }
107  }
108  } // end if jAtomId > iAtomId
109  } // end for jp (j atom)
110  } // end for ip (i atom)
111  } // end for ic (i cell)
112 
113  // Construct a string representation of integer nSample
114  std::stringstream ss;
115  std::string nSampleString;
116  ss << nSample_;
117  nSampleString = ss.str();
118 
119  // Construct new fileName: outputFileName + char(nSample)
120  std::string filename;
121  filename = outputFileName();
122  filename += nSampleString;
123 
124  // Open output file, write data, and close file
125  fileMaster().openOutputFile(filename, outputFile_);
126  system().writeConfig(outputFile_);
127  outputFile_.close();
128  nSample_++;
129 
130  // Clear the LinkMaster
131  system().linkMaster().clear();
132 
133  } // end isAtInterval
134  }
135 
136 
137 
138 }
139 
140 #endif
141 
A Vector is a Cartesian vector.
Definition: Vector.h:75
void begin(int speciesId, MoleculeIterator &iterator)
Initialize an iterator for molecules of one species in this System.
Definition: System.h:1147
double distanceSq(const Vector &r1, const Vector &r2) const
Return square distance between positions r1 and r2.
void openOutputFile(const std::string &filename, std::ofstream &out, std::ios_base::openmode mode=std::ios_base::out) const
Open an output file.
Definition: FileMaster.cpp:290
Mask & mask()
Get the associated Mask by reference.
A set of interacting Molecules enclosed by a Boundary.
Definition: System.h:115
System & system()
Return reference to parent system.
void setup()
Complete any required initialization.
Definition: Crosslinker.cpp:42
void readOutputFileName(std::istream &in)
Read outputFileName from file.
void clear()
Sets all Cell objects to empty state (no Atoms).
bool isMasked(const Atom &atom) const
True if the atom is in the masked set for the target Atom.
virtual void readParameters(std::istream &in)
Read output file and nStepPerSample.
Definition: Crosslinker.cpp:33
Simulation & simulation() const
Get the parent Simulation by reference.
Definition: System.h:1055
void getCellNeighbors(int ic, NeighborArray &neighbors, int &nInCell) const
Fill an array with pointers to atoms in a cell and neighboring cells.
void readInterval(std::istream &in)
Read interval from file, with error checking.
A point particle within a Molecule.
bool notEnd() const
Is the current pointer not at the end of the PArray?
Utility classes for scientific computation.
Definition: accumulators.mod:1
void clear()
Clear LinkMaster.
Definition: LinkMaster.cpp:324
void setup(const Boundary &boundary, double cutoff)
Setup grid of empty cells.
int id() const
Get global index for this Atom within the Simulation.
void shift(Vector &r) const
Shift Cartesian Vector r to its primary image.
int totCells() const
Get total number of cells in this CellList.
Forward iterator for a PArray.
Definition: ArraySet.h:19
Template for Analyzer associated with one System.
Boundary & boundary() const
Get the Boundary by reference.
Definition: System.h:1064
Single-processor Monte Carlo (MC) and molecular dynamics (MD).
int nSpecies() const
Get the number of Species in this Simulation.
void sample(long iStep)
Create crosslinks and dump configuration to file.
Definition: Crosslinker.cpp:51
Crosslinker(System &system)
Constructor.
Definition: Crosslinker.cpp:26
void setClassName(const char *className)
Set class name string.
FileMaster & fileMaster()
Get the FileMaster by reference.
void setAtomCapacity(int atomCapacity)
Set atom capacity.
bool isAtInterval(long counter) const
Return true iff counter is a multiple of the interval.
const std::string & outputFileName() const
Return outputFileName string.
const Vector & position() const
Get the position Vector by const reference.
void writeConfig(std::ostream &out)
Write system configuration to a specified ostream.
Definition: System.cpp:836
void addLink(Atom &atom0, Atom &atom1, int typeId)
Add a link betwen two specific Atoms.
Definition: LinkMaster.cpp:39
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.
LinkMaster & linkMaster() const
Get the LinkMaster by reference.
Definition: System.h:1074