OpenMPCD
Instrumentation.cpp
2 
10 
11 #include <boost/filesystem.hpp>
12 
13 #include <fstream>
14 #include <sstream>
15 
16 using namespace OpenMPCD;
17 
19  const Simulation* const sim, const unsigned int rngSeed_,
20  const std::string& gitRevision_)
21  : simulation(sim), rngSeed(rngSeed_), gitRevision(gitRevision_),
22 
23  constructionTimeUTC(
24  Utility::HostInformation::getCurrentUTCTimeAsString()),
25 
26  autosave(false),
27 
28  measurementCount(0)
29 {
30  const Configuration config = sim->getConfiguration();
31 
32  if(!config.has("instrumentation"))
33  return;
34 
35  const Configuration::Setting setting = config.getSetting("instrumentation");
36 
37  std::set<std::string> knownSettings;
38  knownSettings.insert("velocityMagnitudeHistogram");
39  knownSettings.insert("densityProfile");
40  knownSettings.insert("flowProfile");
41 
42  knownSettings.insert("dumbbellBondLengthHistogram");
43  knownSettings.insert("dumbbellBondLengthSquaredHistogram");
44  knownSettings.insert("dumbbellBondXHistogram");
45  knownSettings.insert("dumbbellBondYHistogram");
46  knownSettings.insert("dumbbellBondZHistogram");
47  knownSettings.insert("dumbbellBondXXHistogram");
48  knownSettings.insert("dumbbellBondYYHistogram");
49  knownSettings.insert("dumbbellBondZZHistogram");
50  knownSettings.insert("dumbbellBondXYHistogram");
51  knownSettings.insert("dumbbellBondAngleWithFlowDirectionHistogram");
52  knownSettings.insert("dumbbellBondXYAngleWithFlowDirectionHistogram");
53  knownSettings.insert("fourierTransformedVelocity");
54  knownSettings.insert("gaussianChains");
55  knownSettings.insert("gaussianRods");
56  knownSettings.insert("harmonicTrimers");
57  knownSettings.insert("logicalEntityMeanSquareDisplacement");
58  knownSettings.insert("normalModeAutocorrelation");
59  knownSettings.insert("selfDiffusionCoefficient");
60  knownSettings.insert("velocityAutocorrelation");
61 
62  {
63  std::string offender;
64  if(!setting.childrenHaveNamesInCollection(knownSettings, &offender))
65  {
68  std::string("Unknown setting in `instrumentation`: ") +
69  offender);
70  }
71  }
72 
73 
74  if(!sim->hasMPCFluid())
75  return;
76 
77  if(setting.has("velocityMagnitudeHistogram"))
78  {
79  velocityMagnitudeHistogram.reset(
80  new Histogram("velocityMagnitudeHistogram", config));
81  }
82 
83  if(setting.has("densityProfile"))
84  {
85  densityProfile.reset(
86  new DensityProfile(
87  simulation->getSimulationBoxSizeX(),
88  simulation->getSimulationBoxSizeY(),
89  simulation->getSimulationBoxSizeZ(),
90  setting.getSetting("densityProfile")));
91  }
92 
93  if(setting.has("flowProfile"))
94  {
95  flowProfile.reset(
97  simulation->getSimulationBoxSizeX(),
98  simulation->getSimulationBoxSizeY(),
99  simulation->getSimulationBoxSizeZ(),
100  setting.getSetting("flowProfile")));
101  }
102 }
103 
105 {
106  if(autosave)
107  save(autosave_rundir);
108 }
109 
111 {
112  const Profiling::CodeRegion codeRegion("measurement");
113 
114  measureMPCFluid();
115  measureMPCSolute();
116 
117  ++measurementCount;
118 }
119 
120 void CUDA::Instrumentation::save(const std::string& rundir) const
121 {
122  if(velocityMagnitudeHistogram)
123  {
124  velocityMagnitudeHistogram->save(
125  rundir + "/velocityMagnitudeHistogram.data");
126  }
127 
128  if(densityProfile)
129  densityProfile->saveToFile(rundir + "/densityProfile.data");
130 
131  if(flowProfile)
132  flowProfile->saveToFile(rundir + "/flowProfile.data");
133 
134  if(totalFluidVelocityMagnitudeVSTime)
135  {
136  totalFluidVelocityMagnitudeVSTime->save(
137  rundir + "/totalFluidVelocityMagnitudeVSTime.data", false);
138  }
139 
140  if(simulation->hasMPCFluid())
141  simulation->getMPCFluid().getInstrumentation().save(rundir);
142 
143  if(simulation->hasSolute())
144  {
145  if(simulation->getMPCSolute().hasInstrumentation())
146  simulation->getMPCSolute().getInstrumentation().save(rundir);
147  }
148 
149 
150  {
151  std::stringstream ss;
152  ss << "numberOfCompletedSweeps: ";
153  ss << simulation->getNumberOfCompletedSweeps() << "\n";
154 
155  ss << "executingHost: \"";
156  ss << Utility::HostInformation::getHostname() << "\"\n";
157 
158  Device device;
159  ss << "DefaultCUDADevicePCIAddress: \"";
160  ss << device.getPCIAddressString() << "\"\n";
161 
162  ss << "startTimeUTC: \"";
163  ss << constructionTimeUTC << "\"\n";
164 
165  ss << "endTimeUTC: \"";
167 
168  const std::string outname=rundir+"/metadata.txt";
169  std::ofstream dst(outname.c_str(), std::ios::trunc);
170  dst << ss.str();
171  }
172 
173 
174  saveStaticData(rundir);
175 }
176 
177 void CUDA::Instrumentation::measureMPCFluid()
178 {
179  if(!simulation->hasMPCFluid())
180  return;
181 
182  const Profiling::CodeRegion codeRegion("fluid measurement");
183 
184 
185  const BoundaryCondition::LeesEdwards* const leesEdwardsBoundaryConditions =
186  dynamic_cast<const BoundaryCondition::LeesEdwards*>(
187  simulation->getBoundaryConditions());
188 
189  if(leesEdwardsBoundaryConditions == NULL)
190  {
193  "Non-Lees-Edwards currently not supported");
194  }
195 
196 
197  if(flowProfile)
198  flowProfile->newSweep();
199 
200 
201  const MPCFluid::Base& mpcFluid = simulation->getMPCFluid();
202 
203  const unsigned int mpcParticleCount = mpcFluid.getParticleCount();
204 
205  mpcFluid.fetchFromDevice();
206 
207  Vector3D<MPCParticleVelocityType> totalFluidVelocity(0, 0, 0);
208 
209  for(unsigned int i=0; i<mpcParticleCount; ++i)
210  {
212  mpcFluid.getPosition(i);
214  mpcFluid.getVelocity(i);
215 
216  if(velocityMagnitudeHistogram)
217  velocityMagnitudeHistogram->fill(velocity.getMagnitude());
218 
219  MPCParticleVelocityType velocityCorrection;
222  position,
223  simulation->getMPCTime(),
224  leesEdwardsBoundaryConditions->getShearRate(),
225  simulation->getSimulationBoxSizeX(),
226  simulation->getSimulationBoxSizeY(),
227  simulation->getSimulationBoxSizeZ(),
228  &velocityCorrection);
229 
230  Vector3D<MPCParticleVelocityType> correctedVelocity = velocity;
231  correctedVelocity.addToX(velocityCorrection);
232 
233  if(densityProfile)
234  {
235  const std::size_t x =
236  static_cast<std::size_t>(
237  image.getX() * densityProfile->getCellSubdivisionsX());
238  const std::size_t y =
239  static_cast<std::size_t>(
240  image.getY() * densityProfile->getCellSubdivisionsY());
241  const std::size_t z =
242  static_cast<std::size_t>(
243  image.getZ() * densityProfile->getCellSubdivisionsZ());
244 
245  densityProfile->add(x, y, z, mpcFluid.getParticleMass());
246  }
247 
248  if(flowProfile)
249  {
250  const std::size_t x =
251  static_cast<std::size_t>(
252  image.getX() * flowProfile->getCellSubdivisionsX());
253  const std::size_t y =
254  static_cast<std::size_t>(
255  image.getY() * flowProfile->getCellSubdivisionsY());
256  const std::size_t z =
257  static_cast<std::size_t>(
258  image.getZ() * flowProfile->getCellSubdivisionsZ());
259 
260  flowProfile->add(x, y, z, correctedVelocity);
261  }
262 
263  totalFluidVelocity += correctedVelocity;
264  }
265 
266  if(densityProfile)
267  densityProfile->incrementFillCount();
268 
269  if(totalFluidVelocityMagnitudeVSTime)
270  {
271  totalFluidVelocityMagnitudeVSTime->addPoint(
272  simulation->getMPCTime(), totalFluidVelocity.getMagnitude());
273  }
274 
275  mpcFluid.getInstrumentation().measure();
276 }
277 
278 void CUDA::Instrumentation::measureMPCSolute()
279 {
280  if(!simulation->hasSolute())
281  return;
282 
283  if(!simulation->getMPCSolute().hasInstrumentation())
284  return;
285 
286  const Profiling::CodeRegion codeRegion("solute measurement");
287 
288  simulation->getMPCSolute().getInstrumentation().measure();
289 }
290 
291 void CUDA::Instrumentation::saveStaticData(const std::string& rundir) const
292 {
293  {
294  std::stringstream ss;
295  ss<<rngSeed;
296 
297  const std::string outname=rundir+"/rngSeed.txt";
298  std::ofstream dst(outname.c_str(), std::ios::trunc);
299  dst<<ss.str();
300  }
301 
302  if(!boost::filesystem::exists(rundir + "/config.txt"))
303  simulation->getConfiguration().writeToFile(rundir + "/config.txt");
304 
305  if(!gitRevision.empty())
306  {
307  const std::string outname=rundir+"/git-revision";
308  std::ofstream dst(outname.c_str(), std::ios::trunc);
309  dst<<gitRevision;
310  }
311 }
HostInformation.hpp
AnalyticQuantities.hpp
OpenMPCD::CUDA::Simulation::getSimulationBoxSizeZ
unsigned int getSimulationBoxSizeZ() const
Returns the size of the primary simulation box along the z direction.
Definition: CUDA/Simulation.hpp:121
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::RemotelyStoredVector
Represents a vector whose data is stored elsewhere.
Definition: RemotelyStoredVector.hpp:26
OpenMPCD::Profiling::CodeRegion
Marks a code region.
Definition: CodeRegion.hpp:25
OpenMPCD::Histogram
Represents a histogram.
Definition: Histogram.hpp:20
OpenMPCD::CUDA::Simulation::getSimulationBoxSizeY
unsigned int getSimulationBoxSizeY() const
Returns the size of the primary simulation box along the y direction.
Definition: CUDA/Simulation.hpp:113
OpenMPCD::Configuration
Represents the configuration of the simulation.
Definition: Configuration.hpp:28
OpenMPCD::Vector3D::getZ
OPENMPCD_CUDA_HOST_AND_DEVICE T getZ() const
Returns the z coordinate.
Definition: Vector3D.hpp:119
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::BoundaryCondition::LeesEdwards::getShearRate
FP getShearRate() const
Returns the Lees-Edwards shear rate .
Definition: LeesEdwards.hpp:72
OpenMPCD::Vector3D
3-dimensional vector.
Definition: Vector3D.hpp:38
OpenMPCD::Utility::HostInformation::getCurrentUTCTimeAsString
const std::string getCurrentUTCTimeAsString()
Returns the current time in UTC as a string, formatted as YYYY-MM-DDTHH:MM:SS.
Definition: HostInformation.cpp:21
OpenMPCD::UnimplementedException
Exception for unimplemented functionality.
Definition: Exceptions.hpp:192
OpenMPCD::CUDA::Instrumentation::measure
void measure()
Performs measurements on MPC fluids and solutes.
Definition: Instrumentation.cpp:110
OpenMPCD::RemotelyStoredVector::getMagnitude
OPENMPCD_CUDA_HOST_AND_DEVICE T getMagnitude() const
Returns the magnitude of this vector.
Definition: RemotelyStoredVector.hpp:191
OpenMPCD::CUDA::BoundaryCondition::LeesEdwards
Lees-Edwards boundary conditions.
Definition: LeesEdwards.hpp:31
OpenMPCD::CUDA::Simulation::hasMPCFluid
bool hasMPCFluid() const
Returns whether an MPC fluid has been configured.
Definition: CUDA/Simulation.hpp:141
OpenMPCD::Vector3D::getX
OPENMPCD_CUDA_HOST_AND_DEVICE T getX() const
Returns the x coordinate.
Definition: Vector3D.hpp:61
OpenMPCD::CUDA::Simulation::getSimulationBoxSizeX
unsigned int getSimulationBoxSizeX() const
Returns the size of the primary simulation box along the x direction.
Definition: CUDA/Simulation.hpp:105
OpenMPCD::CUDA::Device
Class representing a CUDA Device.
Definition: Device.hpp:21
OpenMPCD::CUDA::Instrumentation::~Instrumentation
~Instrumentation()
The destructor.
Definition: Instrumentation.cpp:104
OpenMPCD::CUDA::MPCFluid::DeviceCode::mpcParticleCount
__constant__ unsigned int mpcParticleCount
The number of MPC fluid particles.
Device.hpp
OpenMPCD::Vector3D::getY
OPENMPCD_CUDA_HOST_AND_DEVICE T getY() const
Returns the y coordinate.
Definition: Vector3D.hpp:90
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::Vector3D::addToX
OPENMPCD_CUDA_HOST_AND_DEVICE void addToX(const T val)
Adds the given value to the x coordinate.
Definition: Vector3D.hpp:81
OpenMPCD::CUDA::Simulation::getConfiguration
const Configuration & getConfiguration() const
Returns the configuration.
Definition: CUDA/Simulation.hpp:81
OpenMPCD::Configuration::Setting::getSetting
const Setting getSetting(const std::string &name) const
Returns the setting object with the given name.
Definition: Configuration.hpp:275
OpenMPCD::CUDA::Device::getPCIAddressString
const std::string getPCIAddressString() const
Returns a string representing the PCI address.
Definition: Device.cpp:56
LeesEdwards.hpp
CodeRegion.hpp
OpenMPCD::CUDA::Instrumentation::Instrumentation
Instrumentation(const Simulation *const sim, const unsigned int rngSeed_, const std::string &gitRevision_)
The constructor.
Definition: Instrumentation.cpp:18
OpenMPCD::CUDA::Simulation
MPCD simulation with Molecular Dynamics on CUDA-capable GPUs.
Definition: CUDA/Simulation.hpp:48
OpenMPCD::Utility::HostInformation::getHostname
const std::string getHostname()
Returns the hostname of the machine this program is being executed on.
Definition: HostInformation.cpp:16
OpenMPCD::Configuration::Setting::childrenHaveNamesInCollection
bool childrenHaveNamesInCollection(const std::set< std::string > &names, std::string *const offender=NULL) const
Returns whether all children in this setting have names that are in the given set.
Definition: Configuration.cpp:12
OpenMPCD::MPCParticleVelocityType
FP MPCParticleVelocityType
The data type for the velocities of MPC particles.
Definition: Types.hpp:16
Instrumentation.hpp
AnalyticQuantitiesGaussianDumbbell.hpp
OpenMPCD::FlowProfile
Represents a flow profile.
Definition: FlowProfile.hpp:47
OpenMPCD::Configuration::Setting
Represents a setting in the configuration.
Definition: Configuration.hpp:36
OpenMPCD::InvalidConfigurationException
Represents an invalid configuration.
Definition: Exceptions.hpp:80
OpenMPCD::CUDA::DeviceCode::getImageUnderLeesEdwardsBoundaryConditions
const __device__ Vector3D< MPCParticlePositionType > getImageUnderLeesEdwardsBoundaryConditions(const FP mpcTime, const Vector3D< MPCParticlePositionType > &position, MPCParticleVelocityType &velocityCorrection)
Returns the image of the given particle position under Lees-Edwards boundary conditions.
LeesEdwardsBoundaryConditions.hpp
OpenMPCD::CUDA::Instrumentation::save
void save(const std::string &rundir) const
Saves the data to the given run directory.
Definition: Instrumentation.cpp:120
OpenMPCD::DensityProfile
Represents a density profile.
Definition: DensityProfile.hpp:33