OpenMPCD
DensityProfile.cpp
2 
5 
6 #include <fstream>
7 #include <limits>
8 
10  const unsigned int mpcBoxSizeX,
11  const unsigned int mpcBoxSizeY,
12  const unsigned int mpcBoxSizeZ,
13  const Configuration::Setting& settings)
14  : fillCount(0),
15  cellSubdivisionsX(1), cellSubdivisionsY(1), cellSubdivisionsZ(1)
16 {
18  mpcBoxSizeX != 0, InvalidArgumentException);
20  mpcBoxSizeY != 0, InvalidArgumentException);
22  mpcBoxSizeZ != 0, InvalidArgumentException);
23 
24  if(settings.has("cellSubdivision"))
25  {
26  settings.read("cellSubdivision.x", &cellSubdivisionsX);
27  settings.read("cellSubdivision.y", &cellSubdivisionsY);
28  settings.read("cellSubdivision.z", &cellSubdivisionsZ);
29  }
30 
31  if(cellSubdivisionsX == 0)
32  OPENMPCD_THROW(InvalidConfigurationException, "`cellSubdivision.x`");
33  if(cellSubdivisionsY == 0)
34  OPENMPCD_THROW(InvalidConfigurationException, "`cellSubdivision.y`");
35  if(cellSubdivisionsZ == 0)
36  OPENMPCD_THROW(InvalidConfigurationException, "`cellSubdivision.z`");
37 
38  const unsigned int sizeX = mpcBoxSizeX * cellSubdivisionsX;
39  const unsigned int sizeY = mpcBoxSizeY * cellSubdivisionsY;
40  const unsigned int sizeZ = mpcBoxSizeZ * cellSubdivisionsZ;
41 
42  const std::vector<FP> zeroes(sizeZ, 0);
43 
44  points.resize(sizeX, std::vector<std::vector<FP> >(sizeY, zeroes));
45 }
46 
47 void OpenMPCD::DensityProfile::saveToFile(const std::string& path) const
48 {
49  std::ofstream file(path.c_str(), std::ios::trunc);
50  file.precision(std::numeric_limits<FP>::digits10 + 2);
51 
52  if(!file.good())
53  OPENMPCD_THROW(IOException, "Failed to write to file.");
54 
55  for(std::vector<std::vector<std::vector<FP> > >::size_type x=0; x<points.size(); ++x)
56  {
57  const std::vector<std::vector<FP> >& tmp1 = points[x];
58  for(std::vector<std::vector<FP> >::size_type y=0; y<tmp1.size(); ++y)
59  {
60  const std::vector<FP>& tmp2 = tmp1[y];
61  for(std::vector<FP>::size_type z=0; z<tmp2.size(); ++z)
62  {
63  const FP totalMass = tmp2[z];
64  file<<FP(x)/cellSubdivisionsX<<"\t"<<FP(y)/cellSubdivisionsY<<"\t"<<FP(z)/cellSubdivisionsZ<<"\t";
65  file<<totalMass / fillCount<<"\n";
66  }
67  }
68  }
69 }
AnalyticQuantities.hpp
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::DensityProfile::saveToFile
void saveToFile(const std::string &path) const
Saves the data into a file with the given path.
Definition: DensityProfile.cpp:47
DensityProfile.hpp
OpenMPCD::IOException
Error on IO.
Definition: Exceptions.hpp:160
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_DEBUG_ASSERT_EXCEPTIONTYPE
#define OPENMPCD_DEBUG_ASSERT_EXCEPTIONTYPE(assertion, ExceptionType)
Definition: OPENMPCD_DEBUG_ASSERT.hpp:76
OpenMPCD::FP
double FP
Default floating point type.
Definition: Types.hpp:13
OpenMPCD::DensityProfile::DensityProfile
DensityProfile(const unsigned int mpcBoxSizeX, const unsigned int mpcBoxSizeY, const unsigned int mpcBoxSizeZ, const Configuration::Setting &settings)
The constructor.
Definition: DensityProfile.cpp:9
OpenMPCD::Configuration::Setting
Represents a setting in the configuration.
Definition: Configuration.hpp:36
OpenMPCD::InvalidConfigurationException
Represents an invalid configuration.
Definition: Exceptions.hpp:80
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