OpenMPCD
NormalModeAutocorrelation.cpp
2 
8 
9 #include <fstream>
10 
11 static const char* const configGroupName =
12  "instrumentation.normalModeAutocorrelation";
13 
14 namespace OpenMPCD
15 {
16 namespace CUDA
17 {
18 namespace MPCFluid
19 {
20 namespace Instrumentation
21 {
22 
24  const Configuration& configuration,
25  const CUDA::MPCFluid::Base* const mpcFluid_)
26  : mpcFluid(mpcFluid_),
27  sweepsSinceLastMeasurement(0),
28  shift(0)
29 {
31 
32  const Configuration::Setting configGroup =
33  configuration.getSetting("instrumentation.normalModeAutocorrelation");
34  if(!isValidConfiguration(configGroup))
36 
40 
41 
42  configGroup.read("measureEveryNthSweep", &measureEveryNthSweep);
43  configGroup.read(
44  "autocorrelationArgumentCount", &autocorrelationArgumentCount);
45 
46  if(configGroup.has("shift"))
47  configGroup.read("shift", &shift);
48 
49  OPENMPCD_DEBUG_ASSERT(measureEveryNthSweep > 0);
50  OPENMPCD_DEBUG_ASSERT(autocorrelationArgumentCount > 0);
51 
52  const std::size_t bufferElementCount =
53  mpcFluid->getNumberOfLogicalEntities() *
54  (mpcFluid->getNumberOfParticlesPerLogicalEntity() + 1);
55  for(std::size_t i = 0; i < autocorrelationArgumentCount; ++i)
56  {
57  snapshots.push_back(
58  new DeviceBuffer<MPCParticlePositionType>(bufferElementCount));
59  }
60 }
61 
63 {
64  for(std::size_t i = 0; i < snapshots.size(); ++i)
65  delete snapshots[i];
66 }
67 
69 {
70  return config.has(configGroupName);
71 }
72 
74  const Configuration::Setting& group)
75 {
76  if(!group.has("measureEveryNthSweep"))
77  return false;
78 
79  if(!group.has("autocorrelationArgumentCount"))
80  return false;
81 
82  try
83  {
84  unsigned int measureEveryNthSweep;
85  group.read("measureEveryNthSweep", &measureEveryNthSweep);
86 
87  if(measureEveryNthSweep == 0)
88  return false;
89 
90 
91  unsigned int autocorrelationArgumentCount;
92  group.read(
93  "autocorrelationArgumentCount", &autocorrelationArgumentCount);
94 
95  if(autocorrelationArgumentCount == 0)
96  return false;
97  }
98  catch(const InvalidConfigurationException&)
99  {
100  return false;
101  }
102 
103  return true;
104 }
105 
107 {
108  ++sweepsSinceLastMeasurement;
109  if(sweepsSinceLastMeasurement != measureEveryNthSweep)
110  {
111  if(autocorrelations.size() != 0)
112  return;
113  }
114 
115  sweepsSinceLastMeasurement = 0;
116 
117  std::vector<DeviceBuffer<MPCParticlePositionType>* > buffers;
118  //buffers[0] holds the normal coordinate snapshot for the current
119  //measurement time, buffers[1] holds the snapshot for the previous one, etc.
120  buffers.reserve(autocorrelationArgumentCount);
121  for(unsigned int i = 0; i < autocorrelationArgumentCount; ++i)
122  buffers.push_back(NULL);
123 
124  const unsigned int currentMeasurementTime = autocorrelations.size();
125 
126  OPENMPCD_DEBUG_ASSERT(autocorrelationArgumentCount > 0);
127  for(unsigned int i = 0; i < autocorrelationArgumentCount; ++i)
128  {
129  if(i > currentMeasurementTime)
130  continue;
131 
132  buffers[i] =
133  snapshots[
134  (currentMeasurementTime - i) % autocorrelationArgumentCount];
135  }
136 
137 
140  mpcFluid->getNumberOfLogicalEntities(),
141  mpcFluid->getDevicePositions(),
142  buffers[0]->getPointer(),
143  shift);
144 
145 
146  autocorrelations.push_back(
147  std::vector<std::vector<MPCParticlePositionType> >());
148 
149 
150  for(unsigned int i = 0; i < buffers.size(); ++i)
151  {
152  if(buffers[i] == NULL)
153  break;
154 
155  const std::vector<MPCParticlePositionType> result =
158  mpcFluid->getNumberOfLogicalEntities(),
159  buffers[i]->getPointer(),
160  buffers[0]->getPointer());
161 
162  autocorrelations[currentMeasurementTime].push_back(result);
163  }
164 }
165 
167 {
168  return autocorrelations.size();
169 }
170 
172 {
173  OPENMPCD_DEBUG_ASSERT(autocorrelationArgumentCount > 0);
174 
175  return autocorrelationArgumentCount - 1;
176 }
177 
179  const unsigned int t,
180  const unsigned int T,
181  const unsigned int normalMode) const
182 {
188  t <= T, InvalidArgumentException);
192  normalMode <= mpcFluid->getNumberOfParticlesPerLogicalEntity(),
194 
195  OPENMPCD_DEBUG_ASSERT(T < autocorrelations.size());
196  OPENMPCD_DEBUG_ASSERT(T - t < autocorrelations[T].size());
197  OPENMPCD_DEBUG_ASSERT(normalMode < autocorrelations[T][T - t].size());
198  return autocorrelations[T][T - t][normalMode];
199 }
200 
201 void NormalModeAutocorrelation::save(std::ostream& stream)
202 {
203  const std::streamsize oldPrecision =
204  stream.precision(std::numeric_limits<FP>::digits10 + 2);
205 
206  const unsigned int normalModeCount =
207  mpcFluid->getNumberOfParticlesPerLogicalEntity() + 1;
208  for(unsigned int t = 0; t < getMeasurementCount(); ++t)
209  {
210  for(unsigned int dT = 0; dT < autocorrelationArgumentCount; ++dT)
211  {
212  const unsigned int T = t + dT;
213  if(T >= getMeasurementCount())
214  break;
215 
216  stream << t << "\t" << dT;
217  for(unsigned int n = 0; n < normalModeCount; ++n)
218  stream << "\t" << getAutocorrelation(t, T, n);
219  stream << "\n";
220  }
221  }
222 
223  stream.precision(oldPrecision);
224 }
225 
226 void NormalModeAutocorrelation::save(const std::string& rundir)
227 {
228  const std::string path = rundir + "/normalModeAutocorrelations.data";
229 
231 
232  std::ofstream file(path.c_str(), std::ios::trunc);
233 
234  save(file);
235 }
236 
237 } //namespace Instrumentation
238 } //namespace MPCFluid
239 } //namespace CUDA
240 } //namespace OpenMPCD
OpenMPCD::CUDA::MPCFluid::Base
Base class for MPC fluids.
Definition: CUDA/MPCFluid/Base.hpp:40
OpenMPCD::Configuration::getSetting
const Setting getSetting(const std::string &name) const
Returns the setting object with the given name.
Definition: Configuration.hpp:579
OpenMPCD::Configuration::has
bool has(const std::string &setting) const
Returns whether a setting with the given name exists.
Definition: Configuration.hpp:509
OpenMPCD::CUDA::MPCFluid::Instrumentation::NormalModeAutocorrelation::getAutocorrelation
MPCParticlePositionType getAutocorrelation(const unsigned int t, const unsigned int T, const unsigned int normalMode) const
Returns the measured value of the autocorrelation at measurement times and , and for normal mode ind...
Definition: NormalModeAutocorrelation.cpp:178
OpenMPCD::Configuration
Represents the configuration of the simulation.
Definition: Configuration.hpp:28
Exceptions.hpp
OPENMPCD_THROW
#define OPENMPCD_THROW(ExceptionType, message)
Throws the given ExceptionType, passing the given message along with file and line number information...
Definition: Exceptions.hpp:22
OpenMPCD::CUDA::MPCFluid::Instrumentation::NormalModeAutocorrelation::~NormalModeAutocorrelation
~NormalModeAutocorrelation()
The destructor.
Definition: NormalModeAutocorrelation.cpp:62
NormalMode.hpp
OpenMPCD::CUDA::MPCFluid::Instrumentation::NormalModeAutocorrelation::NormalModeAutocorrelation
NormalModeAutocorrelation(const OpenMPCD::Configuration &configuration, const OpenMPCD::CUDA::MPCFluid::Base *const mpcFluid_)
The constructor.
Definition: NormalModeAutocorrelation.cpp:23
Base.hpp
OPENMPCD_DEBUG_ASSERT
#define OPENMPCD_DEBUG_ASSERT(assertion)
Asserts that the given expression evaluates to true, but only if OPENMPCD_DEBUG is defined.
Definition: OPENMPCD_DEBUG_ASSERT.hpp:88
OpenMPCD::CUDA::MPCFluid::Instrumentation::NormalModeAutocorrelation::measure
void measure()
Takes measurement data.
Definition: NormalModeAutocorrelation.cpp:106
FilesystemUtilities.hpp
OpenMPCD::CUDA::NormalMode::getAverageNormalCoordinateAutocorrelation
const std::vector< MPCParticlePositionType > getAverageNormalCoordinateAutocorrelation(const unsigned int chainLength, const unsigned int chainCount, const MPCParticlePositionType *const normalModes0, const MPCParticlePositionType *const normalModesT)
Computes the average normal mode coordinate autocorrelation functions.
OpenMPCD::CUDA::NormalMode::computeNormalCoordinates
void computeNormalCoordinates(const unsigned int chainLength, const unsigned int chainCount, const MPCParticlePositionType *const positions, MPCParticlePositionType *const normalModeCoordinates, const MPCParticlePositionType shift=0)
Calculates all normal coordinates for a number of polymer chains stored contiguously in Device memory...
OpenMPCD::CUDA::MPCFluid::Base::getDevicePositions
const MPCParticlePositionType * getDevicePositions() const
Returns a const pointer to the MPC fluid positions on the Device.
Definition: CUDA/MPCFluid/Base.hpp:161
OpenMPCD::MPCParticlePositionType
FP MPCParticlePositionType
The data type for the positions of MPC particles.
Definition: Types.hpp:15
OpenMPCD::CUDA::MPCFluid::Instrumentation::NormalModeAutocorrelation::getMaximumCorrelationTime
unsigned int getMaximumCorrelationTime() const
Returns, in units of measurement time, the maximum correlation time that is configured to be measured...
Definition: NormalModeAutocorrelation.cpp:171
NormalModeAutocorrelation.hpp
OpenMPCD::Configuration::Setting::has
bool has(const std::string &settingName) const
Returns whether a setting with the given name exists.
Definition: Configuration.hpp:82
OpenMPCD::CUDA::MPCFluid::Instrumentation::NormalModeAutocorrelation::getMeasurementCount
unsigned int getMeasurementCount() const
Returns 1 plus the maximum number t may take in getAutocorrelation.
Definition: NormalModeAutocorrelation.cpp:166
OpenMPCD::FilesystemUtilities::ensureParentDirectory
static void ensureParentDirectory(const std::string &path)
Ensures that the parent directory of the path given exists.
Definition: FilesystemUtilities.cpp:18
OpenMPCD::CUDA::MPCFluid::Instrumentation::NormalModeAutocorrelation::isConfigured
static bool isConfigured(const Configuration &config)
Returns whether the an attempt has been made to configure this class, i.e.
Definition: NormalModeAutocorrelation.cpp:68
OPENMPCD_DEBUG_ASSERT.hpp
OPENMPCD_DEBUG_ASSERT_EXCEPTIONTYPE
#define OPENMPCD_DEBUG_ASSERT_EXCEPTIONTYPE(assertion, ExceptionType)
Definition: OPENMPCD_DEBUG_ASSERT.hpp:76
OpenMPCD::CUDA::DeviceBuffer< MPCParticlePositionType >
OpenMPCD::CUDA::MPCFluid::Instrumentation::NormalModeAutocorrelation::save
void save(std::ostream &stream)
Saves the result to the given stream.
Definition: NormalModeAutocorrelation.cpp:201
OpenMPCD::Configuration::Setting
Represents a setting in the configuration.
Definition: Configuration.hpp:36
OpenMPCD::CUDA::MPCFluid::Base::getNumberOfParticlesPerLogicalEntity
virtual unsigned int getNumberOfParticlesPerLogicalEntity() const
Returns the number of MPC particles per logical entity.
OpenMPCD::InvalidConfigurationException
Represents an invalid configuration.
Definition: Exceptions.hpp:80
OpenMPCD::CUDA::MPCFluid::Instrumentation::NormalModeAutocorrelation::isValidConfiguration
static bool isValidConfiguration(const Configuration::Setting &group)
Returns whether the given configuration group is a valid configuration.
Definition: NormalModeAutocorrelation.cpp:73
OpenMPCD::CUDA::MPCFluid::Base::getNumberOfLogicalEntities
virtual unsigned int getNumberOfLogicalEntities() const =0
Returns the number of logical entities in the fluid.
OpenMPCD::NULLPointerException
NULL-pointer exception.
Definition: Exceptions.hpp:96
OpenMPCD::InvalidArgumentException
Invalid argument exception.
Definition: Exceptions.hpp:128
OpenMPCD::Configuration::Setting::read
void read(const std::string &name, ValueType *const value) const
Reads the specified setting and stores it at the given location.
Definition: Configuration.hpp:131
OpenMPCD::CUDA::MPCFluid::Base::numberOfParticlesPerLogicalEntityIsConstant
virtual bool numberOfParticlesPerLogicalEntityIsConstant() const =0
Returns whether all logical entities consist of the same number of MPC particles.