Skip to content

Commit

Permalink
Merge pull request #6 from MikeHeiber/development
Browse files Browse the repository at this point in the history
Development - Simulation class bugfix update
  • Loading branch information
MikeHeiber authored Aug 2, 2017
2 parents ddeec5b + eb4f101 commit 398997f
Show file tree
Hide file tree
Showing 12 changed files with 328 additions and 9 deletions.
2 changes: 2 additions & 0 deletions Simulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ void Simulation::removeEvent(Event* event_ptr) {
if (*it == event_ptr) {
events.erase(it);
success = true;
break;
}
}
if (!success) {
Expand All @@ -162,6 +163,7 @@ void Simulation::removeObject(Object* object_ptr) {
if (*it == object_ptr) {
objects.erase(it);
success = true;
break;
}
}
// Update counters
Expand Down
87 changes: 87 additions & 0 deletions Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,93 @@

namespace Utils {

vector<pair<double, double>> calculateProbabilityHist(const vector<double>& data, int num_bins) {
// Determine data range
double min_val, max_val;
auto min_it = min_element(data.begin(), data.end());
if (min_it != data.end()) {
min_val = *min_it;
}
else {
cout << "Minimum value not found. Data vector has " << data.size() << " elements." << endl;
}
auto max_it = max_element(data.begin(), data.end());
if (max_it != data.end()) {
max_val = *max_it;
}
else {
cout << "Maximum value not found. Data vector has " << data.size() << " elements." << endl;
}
// Limit the number of bins to the number of data entries
if (num_bins > (int)data.size()) {
num_bins = (int)data.size();
}
// Determine bin size
double bin_size = (max_val - min_val) / (double)num_bins;
return calculateProbabilityHist(data, bin_size, num_bins);
}

vector<pair<double, double>> calculateProbabilityHist(const vector<double>& data, double bin_size) {
// Determine data range
double min_val, max_val;
auto min_it = min_element(data.begin(), data.end());
if (min_it != data.end()) {
min_val = *min_it;
}
else {
cout << "Minimum value not found. Data vector has " << data.size() << " elements." << endl;
}
auto max_it = max_element(data.begin(), data.end());
if (max_it != data.end()) {
max_val = *max_it;
}
else {
cout << "Maximum value not found. Data vector has " << data.size() << " elements." << endl;
}
// Determine number of bins
int num_bins = (int)ceil((max_val - min_val) / bin_size);
// Limit the number of bins to the number of data entries
if (num_bins > (int)data.size()) {
num_bins = (int)data.size();
bin_size = (max_val - min_val) / (double)num_bins;
}
return calculateProbabilityHist(data, bin_size, num_bins);
}

vector<pair<double, double>> calculateProbabilityHist(const vector<double>& data, const double bin_size, const int num_bins) {
// Determine number of bins
double min_val;
auto min_it = min_element(data.begin(), data.end());
if (min_it != data.end()) {
min_val = *min_it;
}
else {
cout << "Minimum value not found. Data vector has " << data.size() << " elements." << endl;
}
// Calculate bin-centered x values
vector<pair<double, double>> hist(num_bins, make_pair(0.0, 0.0));
for (int i = 0; i < num_bins; i++) {
hist[i].first = min_val + 0.5*bin_size + bin_size*(i + 1);
}
// Calculate histogram
vector<int> counts(num_bins, 0);
int index;
for (int i = 0; i < (int)data.size(); i++) {
index = (int)floor((data[i] - min_val) / bin_size);
counts[index]++;
}
// Calculate total area
double area = 0.0;
for (int i = 0; i < num_bins; i++) {
area += (double)counts[i]*bin_size;
}
// Normalized histogram to get probability
for (int i = 0; i < num_bins; i++) {
hist[i].second = (double)counts[i] / area;
}
return hist;
}

void createExponentialDOSVector(vector<double>& data, const double mode, const double urbach_energy, mt19937& gen) {
exponential_distribution<double> dist_exp(1.0 / urbach_energy);
auto rand_exp = bind(dist_exp, ref(gen));
Expand Down
39 changes: 39 additions & 0 deletions Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,31 @@ namespace Utils {
static constexpr double Pi = 3.14159265359;
static constexpr double Coulomb_constant = 8.987551787e9; // N m^2 C^-2

//! \brief Calculates the probability histogram for the input data vector using the input number of bins.
//! \details Linearly spaced bins are automatically created from the minimum value to the maximum value of the data set.
//! The function outputs bin-centered x values and probability y values in a x-y pair vector.
//! \param data is the input data vector.
//! \param num_bins is the desired number of bins.
//! \returns A vector of x-y pairs consisting of bin-centered x values and probability y values.
vector<pair<double,double>> calculateProbabilityHist(const vector<double>& data, int num_bins);

//! \brief Calculates the probability histogram for the input data vector using the input bin size.
//! \details Linearly spaced bins are automatically created from the minimum value to the maximum value of the data set.
//! with the specified bin spacing. The function outputs bin-centered x values and probability y values in a x-y pair vector.
//! \param data is the input data vector.
//! \param bin_size is the desired bin size.
//! \returns A vector of x-y pairs consisting of bin-centered x values and probability y values.
vector<pair<double, double>> calculateProbabilityHist(const vector<double>& data, double bin_size);

//! \brief Calculates the probability histogram for the input data vector using the input bin size and input number of bins.
//! \details Linearly spaced bins are automatically created starting from the minimum value of the data set. The function
//! outputs bin-centered x values and probability y values in a x-y pair vector.
//! \param data is the input data vector.
//! \param num_bins is the number of bins that will be created.
//! \param bin_size is the input bin size.
//! \returns A vector of x-y pairs consisting of bin-centered x values and probability y values.
vector<pair<double, double>> calculateProbabilityHist(const vector<double>& data, const double bin_size, const int num_bins);

//! \brief Creates a vector of doubles that has a custom asymmetric distribution with an exponential tail.
//! \details The created distribution is Gaussian in the positive direction relative to the mode and exponential in
//! the negative direction. On the Gaussian side, the standard deviation is calculated relative to the urbach energy
Expand Down Expand Up @@ -185,6 +210,20 @@ namespace Utils {
return result;
}

//! \brief This template function outputs the input data vector to a file with the specified filename.
//! \warning This function may overwrite existing files if not used carefully.
//! \param vec is the input data vector.
//! \param filename is the input file name.
template<typename T>
void outputVectorToFile(vector<T>& vec, string filename) {
ofstream outfile;
outfile.open(filename);
for (int i = 0; i < (int)vec.size(); i++) {
outfile << vec[i] << "\n";
}
outfile.close();
}

//! \brief This template function efficienctly removes the duplicate entries from an input vector.
//! \details This algorithm allow efficient removal of duplicate vector objects when > or < comparison operators do not exist.
//! \param vec is the input vector to be operated on.
Expand Down
Binary file added docs/Thumbs.db
Binary file not shown.
15 changes: 8 additions & 7 deletions docs/_utils_8h_source.html

Large diffs are not rendered by default.

174 changes: 174 additions & 0 deletions docs/namespace_utils.html
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@
<table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a name="func-members"></a>
Functions</h2></td></tr>
<tr class="memitem:a7ba881f36f5b29cbe039c329a2f5dd5c"><td class="memItemLeft" align="right" valign="top">vector&lt; pair&lt; double, double &gt; &gt;&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="namespace_utils.html#a7ba881f36f5b29cbe039c329a2f5dd5c">calculateProbabilityHist</a> (const vector&lt; double &gt; &amp;data, int num_bins)</td></tr>
<tr class="memdesc:a7ba881f36f5b29cbe039c329a2f5dd5c"><td class="mdescLeft">&#160;</td><td class="mdescRight">Calculates the probability histogram for the input data vector using the input number of bins. <a href="#a7ba881f36f5b29cbe039c329a2f5dd5c">More...</a><br /></td></tr>
<tr class="separator:a7ba881f36f5b29cbe039c329a2f5dd5c"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ae0d4a17325f78a263879cdb9f04190d2"><td class="memItemLeft" align="right" valign="top">vector&lt; pair&lt; double, double &gt; &gt;&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="namespace_utils.html#ae0d4a17325f78a263879cdb9f04190d2">calculateProbabilityHist</a> (const vector&lt; double &gt; &amp;data, double bin_size)</td></tr>
<tr class="memdesc:ae0d4a17325f78a263879cdb9f04190d2"><td class="mdescLeft">&#160;</td><td class="mdescRight">Calculates the probability histogram for the input data vector using the input bin size. <a href="#ae0d4a17325f78a263879cdb9f04190d2">More...</a><br /></td></tr>
<tr class="separator:ae0d4a17325f78a263879cdb9f04190d2"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a8d27dec53fdab98a47fb93852085e9f1"><td class="memItemLeft" align="right" valign="top">vector&lt; pair&lt; double, double &gt; &gt;&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="namespace_utils.html#a8d27dec53fdab98a47fb93852085e9f1">calculateProbabilityHist</a> (const vector&lt; double &gt; &amp;data, const double bin_size, const int num_bins)</td></tr>
<tr class="memdesc:a8d27dec53fdab98a47fb93852085e9f1"><td class="mdescLeft">&#160;</td><td class="mdescRight">Calculates the probability histogram for the input data vector using the input bin size and input number of bins. <a href="#a8d27dec53fdab98a47fb93852085e9f1">More...</a><br /></td></tr>
<tr class="separator:a8d27dec53fdab98a47fb93852085e9f1"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ad895b1edcec8254af3279938d0ed4ac0"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="namespace_utils.html#ad895b1edcec8254af3279938d0ed4ac0">createExponentialDOSVector</a> (vector&lt; double &gt; &amp;data, const double mode, const double urbach_energy, mt19937 &amp;gen)</td></tr>
<tr class="memdesc:ad895b1edcec8254af3279938d0ed4ac0"><td class="mdescLeft">&#160;</td><td class="mdescRight">Creates a vector of doubles that has a custom asymmetric distribution with an exponential tail. <a href="#ad895b1edcec8254af3279938d0ed4ac0">More...</a><br /></td></tr>
<tr class="separator:ad895b1edcec8254af3279938d0ed4ac0"><td class="memSeparator" colspan="2">&#160;</td></tr>
Expand Down Expand Up @@ -128,6 +137,10 @@
<tr class="memitem:affd19edaa58a3f8425e1f7b4c9233f8a"><td class="memTemplItemLeft" align="right" valign="top">base_type&#160;</td><td class="memTemplItemRight" valign="bottom"><a class="el" href="namespace_utils.html#affd19edaa58a3f8425e1f7b4c9233f8a">intpow</a> (const base_type base, const int exponent)</td></tr>
<tr class="memdesc:affd19edaa58a3f8425e1f7b4c9233f8a"><td class="mdescLeft">&#160;</td><td class="mdescRight">This template function calculates and returns the results of an integer power operation on a base numerical datatype. <a href="#affd19edaa58a3f8425e1f7b4c9233f8a">More...</a><br /></td></tr>
<tr class="separator:affd19edaa58a3f8425e1f7b4c9233f8a"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a516ba09eb26dc6f773a26e0aeba2cfbd"><td class="memTemplParams" colspan="2">template&lt;typename T &gt; </td></tr>
<tr class="memitem:a516ba09eb26dc6f773a26e0aeba2cfbd"><td class="memTemplItemLeft" align="right" valign="top">void&#160;</td><td class="memTemplItemRight" valign="bottom"><a class="el" href="namespace_utils.html#a516ba09eb26dc6f773a26e0aeba2cfbd">outputVectorToFile</a> (vector&lt; T &gt; &amp;vec, string filename)</td></tr>
<tr class="memdesc:a516ba09eb26dc6f773a26e0aeba2cfbd"><td class="mdescLeft">&#160;</td><td class="mdescRight">This template function outputs the input data vector to a file with the specified filename. <a href="#a516ba09eb26dc6f773a26e0aeba2cfbd">More...</a><br /></td></tr>
<tr class="separator:a516ba09eb26dc6f773a26e0aeba2cfbd"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ae6b61a9030cf09d8cba2566a42c871df"><td class="memTemplParams" colspan="2">template&lt;typename T &gt; </td></tr>
<tr class="memitem:ae6b61a9030cf09d8cba2566a42c871df"><td class="memTemplItemLeft" align="right" valign="top">void&#160;</td><td class="memTemplItemRight" valign="bottom"><a class="el" href="namespace_utils.html#ae6b61a9030cf09d8cba2566a42c871df">removeDuplicates</a> (vector&lt; T &gt; &amp;vec)</td></tr>
<tr class="memdesc:ae6b61a9030cf09d8cba2566a42c871df"><td class="mdescLeft">&#160;</td><td class="mdescRight">This template function efficienctly removes the duplicate entries from an input vector. <a href="#ae6b61a9030cf09d8cba2566a42c871df">More...</a><br /></td></tr>
Expand Down Expand Up @@ -225,6 +238,127 @@ <h2 class="memtitle"><span class="permalink"><a href="#a25d09c704b5ae03f01cf76b6
</dl>
<dl class="section return"><dt>Returns</dt><dd>The standard deviation of the data set in double format. </dd></dl>

</div>
</div>
<a id="a7ba881f36f5b29cbe039c329a2f5dd5c"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a7ba881f36f5b29cbe039c329a2f5dd5c">&#9670;&nbsp;</a></span>calculateProbabilityHist() <span class="overload">[1/3]</span></h2>

<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">vector&lt; pair&lt; double, double &gt; &gt; Utils::calculateProbabilityHist </td>
<td>(</td>
<td class="paramtype">const vector&lt; double &gt; &amp;&#160;</td>
<td class="paramname"><em>data</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>num_bins</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">

<p>Calculates the probability histogram for the input data vector using the input number of bins. </p>
<p>Linearly spaced bins are automatically created from the minimum value to the maximum value of the data set. The function outputs bin-centered x values and probability y values in a x-y pair vector. </p><dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">data</td><td>is the input data vector. </td></tr>
<tr><td class="paramname">num_bins</td><td>is the desired number of bins. </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>A vector of x-y pairs consisting of bin-centered x values and probability y values. </dd></dl>

</div>
</div>
<a id="ae0d4a17325f78a263879cdb9f04190d2"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ae0d4a17325f78a263879cdb9f04190d2">&#9670;&nbsp;</a></span>calculateProbabilityHist() <span class="overload">[2/3]</span></h2>

<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">vector&lt; pair&lt; double, double &gt; &gt; Utils::calculateProbabilityHist </td>
<td>(</td>
<td class="paramtype">const vector&lt; double &gt; &amp;&#160;</td>
<td class="paramname"><em>data</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">double&#160;</td>
<td class="paramname"><em>bin_size</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">

<p>Calculates the probability histogram for the input data vector using the input bin size. </p>
<p>Linearly spaced bins are automatically created from the minimum value to the maximum value of the data set. with the specified bin spacing. The function outputs bin-centered x values and probability y values in a x-y pair vector. </p><dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">data</td><td>is the input data vector. </td></tr>
<tr><td class="paramname">bin_size</td><td>is the desired bin size. </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>A vector of x-y pairs consisting of bin-centered x values and probability y values. </dd></dl>

</div>
</div>
<a id="a8d27dec53fdab98a47fb93852085e9f1"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a8d27dec53fdab98a47fb93852085e9f1">&#9670;&nbsp;</a></span>calculateProbabilityHist() <span class="overload">[3/3]</span></h2>

<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">vector&lt; pair&lt; double, double &gt; &gt; Utils::calculateProbabilityHist </td>
<td>(</td>
<td class="paramtype">const vector&lt; double &gt; &amp;&#160;</td>
<td class="paramname"><em>data</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">const double&#160;</td>
<td class="paramname"><em>bin_size</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">const int&#160;</td>
<td class="paramname"><em>num_bins</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">

<p>Calculates the probability histogram for the input data vector using the input bin size and input number of bins. </p>
<p>Linearly spaced bins are automatically created starting from the minimum value of the data set. The function outputs bin-centered x values and probability y values in a x-y pair vector. </p><dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">data</td><td>is the input data vector. </td></tr>
<tr><td class="paramname">num_bins</td><td>is the number of bins that will be created. </td></tr>
<tr><td class="paramname">bin_size</td><td>is the input bin size. </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>A vector of x-y pairs consisting of bin-centered x values and probability y values. </dd></dl>

</div>
</div>
<a id="ad895b1edcec8254af3279938d0ed4ac0"></a>
Expand Down Expand Up @@ -523,6 +657,46 @@ <h2 class="memtitle"><span class="permalink"><a href="#af9a7e13279f18cd8d36ca98a
<dd>
An empty vector when called on other processors. </dd></dl>

</div>
</div>
<a id="a516ba09eb26dc6f773a26e0aeba2cfbd"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a516ba09eb26dc6f773a26e0aeba2cfbd">&#9670;&nbsp;</a></span>outputVectorToFile()</h2>

<div class="memitem">
<div class="memproto">
<div class="memtemplate">
template&lt;typename T &gt; </div>
<table class="memname">
<tr>
<td class="memname">void Utils::outputVectorToFile </td>
<td>(</td>
<td class="paramtype">vector&lt; T &gt; &amp;&#160;</td>
<td class="paramname"><em>vec</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">string&#160;</td>
<td class="paramname"><em>filename</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">

<p>This template function outputs the input data vector to a file with the specified filename. </p>
<dl class="section warning"><dt>Warning</dt><dd>This function may overwrite existing files if not used carefully. </dd></dl>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">vec</td><td>is the input data vector. </td></tr>
<tr><td class="paramname">filename</td><td>is the input file name. </td></tr>
</table>
</dd>
</dl>

</div>
</div>
<a id="ae6b61a9030cf09d8cba2566a42c871df"></a>
Expand Down
Loading

0 comments on commit 398997f

Please sign in to comment.