# Detecting afterdepolarisations - C++ version

I've spent the last few weeks (amongst other things) implementing my afterdepolarisations script in C++ within the Chaste framework. Here's some of my code broken down into sections and explained.

```#include "DetectAfterDepolarisations.hpp"
#include &lt;fstream&gt;

double end_time, double stim_start, double stim_period, double lowlimit, double highlimit)
{
```
This function takes as inputs: a vector containing the voltage trace for every time point, a vector of all the times, the end time of the simulation, the time at which the stimulus starts, the period of the stimulus, and the two boundary values for detection. It will output either "true", if there is an afterdepolarisation, or "false", if not.
```    /* detect depolarisations*/
std::vector&lt;double&gt; differences;
for (unsigned int a=0; a &lt; voltage.size()-1; a++)
{
/* list of voltage change at each time point*/
differences.push_back(voltage[a+1]-voltage[a]);
/* if the voltage is going up... */
if (differences[a] &gt; 0.1)
/* store the time */
}
```

The way this works isn't desperately clever: it takes each point on the graph and subtracts it from the one after it, giving the gradient between each point. For all the positive changes in voltage (arbitrarily those over 0.1 mV), it keeps a note of the time.

```    /* erase depolarisations provoked by stimulus */
for (double e = stim_start; e &lt; end_time; e=e+stim_period)
{

for (unsigned int f=0; f&lt;adtimes.size(); f++)
{

{

f--;
}
}

}
```

These awkwardly nested loops check all of the depolarisation times and remove any that coincide with the stimulus. This means that the program finds only afterdepolarisations and not the ones we'd expect to be there. Lowlimit and highlimit can be changed for models with different lengths of action potential.

```
/* find times when the AD starts */

/* iterate through list of voltage upswing times*/

{
for (unsigned int b=0; b &lt; adtimes.size()-2; b++)
{
/* make a list of the differences */
}

/* find minimum time difference */
double minsteps = *step;

for (unsigned int d=0; d&lt; diffadtimes.size(); d++)
{

/* find all points in diff(adtimes) that change more than the minimum*/
if (diffadtimes[d] &gt; minsteps + 0.0001 )
{
/* store times */
/*                std::cout &lt;&lt; adtimes[d+2] &lt;&lt; "\n";*/
}
}
```

This block of code is concerned with finding the start of each afterdepolarisation. Simply put, it looks for discontinuities in the list of times, and stores them.

```
std::ofstream outputfile;
for (unsigned int d=0; d &lt; voltage.size(); d++)
{
outputfile &lt;&lt; simtimes[d] &lt;&lt; " " &lt;&lt; voltage[d] &lt;&lt; " ";

/*output 1 if AD, 0 if not */
bool found = 0;
for (unsigned int e=0; e &lt; adtimes.size(); e++)
{
{
found = 1;
break;
}
}
outputfile &lt;&lt; found;

outputfile &lt;&lt; "\n";
}
```

The output file contains columns of the time, the voltage, and whether or not an afterdepolarisation has been detected at that point.

``` 