Privacy and Security Notice
stable_current.cpp
# include <cstdlib>
# include <cmath>
# include <string>
# include <iostream>
# include <fstream>
# include <iomanip>
using namespace std;
double bcmcur(string k, double u, double d, double t);
const double curerr = 5.0;
const double edgetime = 20.0;
const double goodcur = 5.0;
const double goodtime = 60;
const double uconst[2] = {4092.4, 4101.6};
const double dconst[2] = {4188.5, 4165.65};
const double uoff[2] = {163.8, 163.8};
const double doff[2] = {110.0, 110.0};
const int nscal = 13;
main(int argc, char* argv[])
{
double ref[nscal];
double tmp[nscal];
double low[nscal];
double pre[nscal];
double high[nscal];
double refcur;
double tmpcur;
double lowcur;
double ctime;
double cbcmu3;
double cbcmd3;
double avecur;
double avechar;
double ct1;
double ct3;
double ct5;
double ce1;
double ce3;
double ce5;
int n = 0;
int s = 0;
bool lowset = false;
bool highset = false;
string dummy;
char* kin = new char [20]; char* infile = new char [20]; char* outfile = new char [20];
fstream fin; fstream fout;
cout << '\n';
cout << " -----------------------\n";
cout << "| Stable Current Finder |\n";
cout << " -----------------------\n";
cout << '\n';
if (argc == 4)
{
kin = argv[1];
infile = argv[2];
outfile = argv[3];
}
else
{
cout << "Kinematics (q1,q2,q3) : "; cin >> kin;
cout << "Input Filename : "; cin >> infile;
cout << "Output Filename : "; cin >> outfile;
cout << '\n';
}
fin.open(infile, ios_base::in);
if ( !fin )
{
cerr << ">>> Error opening input file!\n\n";
exit(1);
}
getline(fin, dummy);
for (int i=0; i<nscal; i++) fin >> ref[i];
refcur = bcmcur(kin, ref[4], ref[6], ref[2]);
for (int i=0; i<nscal; i++) fin >> tmp[i];
fout.open(outfile, ios_base::out);
fout.precision(10);
fout.setf(ios_base::left);
fout << setw(24) << "From (Event) To" << setw(24) << "From (Time/s) To" << setw(12) << "Q (uC)" << setw(12) << "I (uA)" << setw(12) << "T1" << setw(12) << "T3" << setw(12) << "T5" << setw(12) << "E1" << setw(12) << "E3" << setw(12) << "E5" << '\n';
while (!fin.eof())
{
n++;
tmpcur = bcmcur(kin, tmp[4], tmp[6], tmp[2]);
if (!lowset)
{
if (tmpcur > goodcur)
{
if (abs(refcur - tmpcur) < curerr)
{
if ((tmp[1] - ref[1]) / 1024 > edgetime)
{
for (int i=0; i<nscal; i++) low[i] = tmp[i];
for (int i=0; i<nscal; i++) pre[i] = tmp[i];
lowcur = tmpcur;
lowset = true;
}
}
else
{
for (int i=0; i<nscal; i++) ref[i] = tmp[i];
refcur = bcmcur(kin, ref[4], ref[6], ref[2]);
}
}
}
else
{
if (abs(lowcur - tmpcur) > curerr)
{
for (int i=0; i<nscal; i++) high[i] = pre[i];
highset = true;
}
else
{
for (int i=0; i<nscal; i++) pre[i] = tmp[i];
}
}
for (int i=0; i<nscal; i++) fin >> tmp[i];
if (lowset && (highset || fin.eof()))
{
if (fin.eof())
{
for (int i=0; i<nscal; i++) high[i] = pre[i];
}
ctime = high[1] - low[1];
if (ctime / 1024 > goodtime)
{
s++;
cbcmu3 = high[3] - low[3];
cbcmd3 = high[5] - low[5];
avecur = bcmcur(kin, cbcmu3, cbcmd3, ctime);
avechar = avecur * ctime / 1024;
ct1 = high[7] - low[7];
ct3 = high[8] - low[8];
ct5 = high[9] - low[9];
ce1 = high[10] - low[10];
ce3 = high[11] - low[11];
ce5 = high[12] - low[12];
fout << setw(12) << low[0] << setw(12) << high[0] << setw(12) << low[1] / 1024 << setw(12) << high[1] / 1024 << setw(12) << avechar << setw(12) << avecur << setw(12) << ct1 << setw(12) << ct3 << setw(12) << ct5 << setw(12) << ce1 << setw(12) << ce3 << setw(12) << ce5 << '\n';
}
lowset = false;
highset = false;
}
}
fin.close(); fout.close();
cout << "The input file " << infile << " includes " << n << " lines.\n";
cout << "Number of stable current periods is " << s << ".\n";
cout << "The output file " << outfile << " was saved.\n\n";
}
double bcmcur(string k, double u, double d, double t)
{
double c;
double uc;
double dc;
int m;
m = (k == "q3") ? 1 : 0;
uc = (u / t * 1024 - uoff[m]) / uconst[m];
dc = (d / t * 1024 - doff[m]) / dconst[m];
c = ( uc + dc ) / 2.0;
return c;
}