OpenMPCD
FlowProfile.cpp
2 
5 
6 #include <boost/static_assert.hpp>
7 #include <boost/type_traits/is_integral.hpp>
8 
9 #include <fstream>
10 #include <limits>
11 
12 template<typename T>
14  const unsigned int mpcBoxSizeX,
15  const unsigned int mpcBoxSizeY,
16  const unsigned int mpcBoxSizeZ,
17  const Configuration::Setting& settings)
18  : simulationBoxSizeX(mpcBoxSizeX),
19  simulationBoxSizeY(mpcBoxSizeY),
20  simulationBoxSizeZ(mpcBoxSizeZ),
21  cellSubdivisionsX(1), cellSubdivisionsY(1), cellSubdivisionsZ(1),
22  sweepCountPerOutput(0), currentBlockSweepCount(0)
23 {
25  mpcBoxSizeX != 0, InvalidArgumentException);
27  mpcBoxSizeY != 0, InvalidArgumentException);
29  mpcBoxSizeZ != 0, InvalidArgumentException);
30 
31  if(settings.has("cellSubdivision"))
32  {
33  settings.read("cellSubdivision.x", &cellSubdivisionsX);
34  settings.read("cellSubdivision.y", &cellSubdivisionsY);
35  settings.read("cellSubdivision.z", &cellSubdivisionsZ);
36  }
37 
38  if(settings.has("sweepCountPerOutput"))
39  settings.read("sweepCountPerOutput", &sweepCountPerOutput);
40 
41 
42  if(cellSubdivisionsX == 0)
43  OPENMPCD_THROW(InvalidConfigurationException, "`cellSubdivision.x`");
44  if(cellSubdivisionsY == 0)
45  OPENMPCD_THROW(InvalidConfigurationException, "`cellSubdivision.y`");
46  if(cellSubdivisionsZ == 0)
47  OPENMPCD_THROW(InvalidConfigurationException, "`cellSubdivision.z`");
48 }
49 
50 template<typename T>
52 {
53  if(outputBlocks.empty())
54  {
55  createOutputBlock();
56  return;
57  }
58 
59 
60  ++currentBlockSweepCount;
61 
62  if(sweepCountPerOutput == 0)
63  return;
64 
65  if(currentBlockSweepCount != sweepCountPerOutput)
66  {
67  OPENMPCD_DEBUG_ASSERT(currentBlockSweepCount < sweepCountPerOutput);
68  return;
69  }
70 
71  currentBlockSweepCount = 0;
72  createOutputBlock();
73 }
74 
75 template<typename T>
76 void OpenMPCD::FlowProfile<T>::saveToFile(const std::string& path) const
77 {
78  std::ofstream file(path.c_str(), std::ios::trunc);
79  file.precision(std::numeric_limits<FP>::digits10 + 2);
80 
81  if(!file.good())
82  OPENMPCD_THROW(IOException, "Failed to write to file in FlowProfile::saveToFile");
83 
84  file << "#x\ty\tz\t";
85  file << "meanVelX\tmeanVelY\tmeanVelZ\t";
86  file << "stdDevVelX\tstdDevVelY\tstdDevVelZ\t";
87  file << "sampleSize";
88 
89  for(std::size_t i = 0; i < outputBlocks.size(); ++i)
90  {
91  file << "\n";
92  printOutputBlockToStream(*(outputBlocks[i]), file);
93  }
94 }
95 
96 template<typename T>
98 {
99  typedef boost::multi_array<OnTheFlyStatistics<T>, 4> MA;
100  typedef boost::shared_ptr<MA> SPMA;
101 
102  outputBlocks.push_back(
103  SPMA(
104  new MA(
105  boost::extents
106  [simulationBoxSizeX * cellSubdivisionsX]
107  [simulationBoxSizeY * cellSubdivisionsY]
108  [simulationBoxSizeZ * cellSubdivisionsZ]
109  [3]
110  )
111  )
112  );
113 }
114 
115 template<typename T>
117  const boost::multi_array<OpenMPCD::OnTheFlyStatistics<T>, 4>& outputBlock,
118  std::ostream& stream) const
119 {
120  for(std::size_t x = 0; x < outputBlock.shape()[0]; ++x)
121  {
122  for(std::size_t y = 0; y < outputBlock.shape()[1]; ++y)
123  {
124  for(std::size_t z = 0; z < outputBlock.shape()[2]; ++z)
125  {
126  //If T were an integral type, the division below would not make
127  //much sense. Should the need arise, this can be reworked.
128  BOOST_STATIC_ASSERT(!boost::is_integral<T>::value);
129 
130  stream << x / double(cellSubdivisionsX) << "\t";
131  stream << y / double(cellSubdivisionsY) << "\t";
132  stream << z / double(cellSubdivisionsZ) << "\t";
133 
134  stream <<
135  outputBlock[x][y][z][0].getSampleMean() << "\t";
136  stream <<
137  outputBlock[x][y][z][1].getSampleMean() << "\t";
138  stream <<
139  outputBlock[x][y][z][2].getSampleMean() << "\t";
140 
141  stream <<
142  outputBlock[x][y][z][0].
143  getSampleStandardDeviation() << "\t";
144  stream <<
145  outputBlock[x][y][z][1].
146  getSampleStandardDeviation() << "\t";
147  stream <<
148  outputBlock[x][y][z][2].
149  getSampleStandardDeviation() << "\t";
150 
151  stream << outputBlock[x][y][z][0].getSampleSize() << "\n";
152 
154  outputBlock[x][y][z][0].getSampleSize()
155  ==
156  outputBlock[x][y][z][1].getSampleSize());
158  outputBlock[x][y][z][0].getSampleSize()
159  ==
160  outputBlock[x][y][z][2].getSampleSize());
161  }
162  }
163  }
164 }
165 
166 template class OpenMPCD::FlowProfile<float>;
167 template class OpenMPCD::FlowProfile<double>;
AnalyticQuantities.hpp
OpenMPCD::FlowProfile::newSweep
void newSweep()
Signals that a new sweep has begun.
Definition: FlowProfile.cpp:51
FlowProfile.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::FlowProfile::FlowProfile
FlowProfile(const unsigned int mpcBoxSizeX, const unsigned int mpcBoxSizeY, const unsigned int mpcBoxSizeZ, const Configuration::Setting &settings)
The constructor.
Definition: FlowProfile.cpp:13
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::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::FlowProfile::saveToFile
void saveToFile(const std::string &path) const
Saves the data into a file with the given path.
Definition: FlowProfile.cpp:76
OPENMPCD_DEBUG_ASSERT_EXCEPTIONTYPE
#define OPENMPCD_DEBUG_ASSERT_EXCEPTIONTYPE(assertion, ExceptionType)
Definition: OPENMPCD_DEBUG_ASSERT.hpp:76
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::OnTheFlyStatistics
Computes sample means and variances "on-the-fly" or "online", i.e.
Definition: OnTheFlyStatistics.hpp:34
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