Simpatico  v1.10
LinkMSD.cpp
1 #ifndef LINK_MSD_CPP
2 #define LINK_MSD_CPP
3 
4 /*
5 * Simpatico - Simulation Package for Polymeric and 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 "LinkMSD.h"
12 #include <mcMd/simulation/Simulation.h>
13 #include <mcMd/chemistry/Molecule.h>
14 #include <mcMd/chemistry/Atom.h>
15 #include <simp/species/Species.h>
16 #include <simp/boundary/Boundary.h>
17 #include <util/misc/FileMaster.h>
18 
19 
20 namespace McMd
21 {
22 
23  using namespace Util;
24  using namespace Simp;
25 
28  : SystemAnalyzer<System>(system)
29  { setClassName("LinkMSD"); }
30 
32  void LinkMSD::readParameters(std::istream& in)
33  {
34 
35  readInterval(in);
37  read<int>(in,"capacity", capacity_);
38  read<int>(in,"linkCapacity", linkCapacity_);
39 
40  //Allocate acumulator_ array.
41  accumulator_.allocate(capacity_);
42 
43  // Allocate arrays
44  int nle = 2*linkCapacity_;
45  i0_.allocate(nle);
46  t0_.allocate(nle);
47 
48  nSamplePerBlock_=0;
49 
50  int i;
51  for (i=0; i < capacity_; ++i){
52  accumulator_[i].setNSamplePerBlock(nSamplePerBlock_);
53  }
54  // If nSamplePerBlock != 0, open an output file for block averages.
55  if (nSamplePerBlock_ != 0) {
56  fileMaster().openOutputFile(outputFileName(".dat"), outputFile_);
57  }
58 
59  }
60 
61 
64  {
65  system().linkMaster().Notifier<LinkAddEvent>::registerObserver(*this);
66  system().linkMaster().Notifier<LinkRemoveEvent>::registerObserver(*this);
67  int nle = 2*linkCapacity_;
68  int i;
69 
70  for (i=0; i < capacity_; ++i){
71  accumulator_[i].clear();
72  }
73  for (i=0; i < nle; ++i){
74  i0_[i]=-1;
75  t0_[i]=-1;
76  }
77  }
78 
80  void LinkMSD::sample(long iStep)
81  {
82  if (isAtInterval(iStep)) {
83 
84  int nLink = system().linkMaster().nLink();
85  int idLink, dt, dr, dr2, le, le1, i, t;
86  Link* linkPtr;
87 
88  for (idLink=0; idLink < nLink; ++idLink){
89  linkPtr = &(system().linkMaster().link(idLink));
90  le = (linkPtr->tag())*2;
91  le1 = le+1;
92  if (t0_[le] != -1) {
93  t = iStep;
94  i = linkPtr->atom0().indexInMolecule();
95  dt = (t - t0_[le])/interval();
96  dr = i - i0_[le];
97  dr2 = dr*dr;
98  //std::cout << "Tag: " << linkPtr->tag() << " end: " << le << " dt: " << dt << " dr :" << dr << std::endl;
99  //std::cout << "i0: " << i0_[le] << " i: " << i << " t0: " << t0_[le] << " t :" << t << std::endl;
100  if (dt < capacity_) {
101  accumulator_[dt].sample(double(dr2), outputFile_);
102  }
103  /*else {
104  UTIL_THROW("link MSD accumulator capacity exceded");
105  }*/
106  }
107  else {
108  t0_[le] = iStep;
109  i0_[le] = linkPtr->atom0().indexInMolecule();
110  }
111  if (t0_[le1] != -1) {
112  t = iStep;
113  i = linkPtr->atom1().indexInMolecule();
114  dt = (t - t0_[le1])/interval();
115  dr = i - i0_[le1];
116  dr2 = dr*dr;
117  //std::cout << "Tag: " << linkPtr->tag() << " end: " << le1 << " dt: " << dt << " dr :" << dr << std::endl;
118  //std::cout << "i0: " << i0_[le1] << " i: " << i << " t0: " << t0_[le1] << " t :" << t << std::endl;
119  if (dt < capacity_) {
120  accumulator_[dt].sample(double(dr2), outputFile_);
121  }
122  /*else {
123  UTIL_THROW("link MSD accumulator capacity exceded");
124  }*/
125  }
126  else {
127  t0_[le1] = iStep;
128  i0_[le1] = linkPtr->atom1().indexInMolecule();
129  }
130  }
131 
132  } // if isAtInterval
133  }
134 
135  void LinkMSD::update(const LinkAddEvent& event)
136  {
137  int le = (event.get()->tag())*2;
138  int le1 = le+1;
139  //std::cout << "AddEvent Tag: " << event.get()->tag() << std::endl;
140  t0_[le] = -1;
141  i0_[le] = -1;
142  t0_[le1] = -1;
143  i0_[le1] = -1;
144  }
145 
146  void LinkMSD::update(const LinkRemoveEvent& event)
147  {
148  int le = (event.get()->tag())*2;
149  int le1 = le+1;
150  //std::cout << "RemoveEvent Tag: " << event.get()->tag() << std::endl;
151  t0_[le] = -1;
152  i0_[le] = -1;
153  t0_[le1] = -1;
154  i0_[le1] = -1;
155  }
156 
157  /*
158  * Output param file after simulation is completed.
159  */
161  {
162  // If outputFile_ was used to write block averages, close it.
163  if (nSamplePerBlock_ != 0) {
164  outputFile_.close();
165  }
166 
167  // Write parameters to file
168  fileMaster().openOutputFile(outputFileName(".prm"), outputFile_);
169  ParamComposite::writeParam(outputFile_);
170  outputFile_.close();
171 
172  // Write average to file
173  fileMaster().openOutputFile(outputFileName(".ave"), outputFile_);
174  int i;
175  accumulator_[0].sample(0, outputFile_);;
176  accumulator_[0].output(outputFile_);
177  for(i=1; i<capacity_; ++i){
178  accumulator_[i].output(outputFile_);
179  }
180  outputFile_.close();
181  }
182 
183 }
184 #endif
int nLink() const
Get the total number of active Links.
Definition: LinkMaster.h:261
LinkMSD(System &system)
Constructor.
Definition: LinkMSD.cpp:27
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
virtual void setup()
Empty.
Definition: LinkMSD.cpp:63
A set of interacting Molecules enclosed by a Boundary.
Definition: System.h:115
System & system()
Return reference to parent system.
Event signalling removal of a Link from the LinkMaster.
Definition: LinkEvents.h:78
Classes used by all simpatico molecular simulations.
void readOutputFileName(std::istream &in)
Read outputFileName from file.
virtual void writeParam(std::ostream &out)
Write all parameters to an output stream.
void readInterval(std::istream &in)
Read interval from file, with error checking.
Utility classes for scientific computation.
Definition: accumulators.mod:1
virtual void readParameters(std::istream &in)
Read parameters from file, and allocate data arrays.
Definition: LinkMSD.cpp:32
Event signalling addition of Link to the LinkMaster.
Definition: LinkEvents.h:20
Template for Analyzer associated with one System.
virtual void sample(long iStep)
Evaluate.
Definition: LinkMSD.cpp:80
Single-processor Monte Carlo (MC) and molecular dynamics (MD).
virtual void output()
Output param file at end of simulation.
Definition: LinkMSD.cpp:160
void setClassName(const char *className)
Set class name string.
int indexInMolecule() const
Get local index for this Atom within the parent molecule;.
Link & link(int id) const
Return an active link by an internal set index.
Definition: LinkMaster.h:255
FileMaster & fileMaster()
Get the FileMaster by reference.
bool isAtInterval(long counter) const
Return true iff counter is a multiple of the interval.
const std::string & outputFileName() const
Return outputFileName string.
void allocate(int capacity)
Allocate the underlying C array.
Definition: DArray.h:191
int interval() const
Get interval value.
LinkMaster & linkMaster() const
Get the LinkMaster by reference.
Definition: System.h:1074