I've written a program that I've been using to calculate the distance between atoms when given the coordinates of two atoms. It finds atom pairs that have a minimum separation between each other, but I've recently modified it to display all distances for all the pairs. However, for particularly long files (with many atoms to calculate), the program slows progress to a halt. For smaller files it will start off quickly, slow down in the middle, and then speed up very rapidly in approximately the last 5% of the calculations. I've posted the "body" of the code (the first portion has been ommitted, and only one file in the project has been posted). The bolded portion is where I believe the error is occuring.
I've been puzzling over this slowdown for days and I can't figure anything out. Any is greatly appreciated.
Code:
UpdateData(TRUE);
m_sOutput="";
UpdateData(FALSE);
UpdateData(TRUE);
char tempChar[80] = "TEMP!";
char a[80];
bool correct=false;
int lines=0;
double dis;
CString buffer;
int index=0;
ifstream tempFile(m_sFileName, ios::nocreate);
if(tempFile.fail()) {
MessageBox("Sorry, there was an error opening the file.","The Atomic Analyzer",MB_OK);
return;
}
while(tempFile.eof()!=1) {
if(tempChar[0]=='R'&&tempChar[1]=='E'&&tempChar[2]=='M') {
tempFile.getline(a,999);
}
else {
lines++;
tempFile.getline(a,999);
}
}
tempFile.close();
m_cTheAtoms=new CAtom[lines];
ifstream file(m_sFileName, ios::nocreate);
if (file.fail()) {
MessageBox("Sorry, there was an error opening the file.","The Atomic Analyzer",MB_OK);
return;
}
MessageBox("This might take a while.","The Atomic Analyzer",MB_OK);
while(file.eof()!=1) {
file>>tempChar;
if(tempChar[0]=='R'&&tempChar[1]=='E'&&tempChar[2]=='M') {
file.getline(a,999);
}
else if(tempChar[0]=='E'&&tempChar[1]=='N'&&tempChar[2]=='D') {
file.getline(a,999);
}
else if(tempChar[0]=='A'&&tempChar[1]=='T'&&tempChar[2]=='O') {
file>>m_cTheAtoms[index].m_iAtomNum;
file>>m_cTheAtoms[index].m_sAtomName;
file>>m_cTheAtoms[index].m_sResName;
file>>m_cTheAtoms[index].m_iResNum;
file>>m_cTheAtoms[index].m_fX;
file>>m_cTheAtoms[index].m_fY;
file>>m_cTheAtoms[index].m_fZ;
file>>tempChar;
file>>tempChar;
index++;
}
else {
MessageBox("Corrupt Data Detected. Please check input file.","The Atomic Analyzer",MB_OK);
return;
}
Code:
int atomsFound=0;
m_cProgress.SetRange(0,index);
for(int i=0; i<index; i++) {
m_cProgress.SetPos(i);
for(int j=(i+1); j<index; j++) {
dis=sqrt(pow(m_cTheAtoms[j].m_fX-m_cTheAtoms[i].m_fX,2)+pow(m_cTheAtoms[j].m_fY-m_cTheAtoms[i].m_fY,2)+pow(m_cTheAtoms[j].m_fZ-m_cTheAtoms[i].m_fZ,2));
buffer.Format("Atom %d (res#%d) to Atom %d (res#%d), Distance: %f\r\n",m_cTheAtoms[i].m_iAtomNum,m_cTheAtoms[i].m_iResNum,m_cTheAtoms[j].m_iAtomNum,m_cTheAtoms[j].m_iResNum,dis);
m_sOutput=m_sOutput+buffer;
atomsFound++;
UpdateData(FALSE);
}
}
Code:
file.close();
if(m_bYesOut==TRUE) {
time_t currentTime;
time(¤tTime);
char tempDate[80];
struct tm * ptm = localtime(¤tTime);
sprintf(tempDate,"results__%d-%d__%d-%d-%d.txt",ptm->tm_mon+1,ptm->tm_mday,ptm->tm_hour,ptm->tm_min+1,ptm->tm_sec+1);
fstream outFile(tempDate,ios::out);
outFile << m_sOutput << "\n" << atomsFound << " atom pairs were found to meet the max distance ("<<m_iDistance<<") requirement.";
outFile.close();
ptm = NULL;
delete ptm;
}
m_cTheAtoms=NULL;
delete m_cTheAtoms;
UpdateData(FALSE);
buffer.Format("%d atom pairs were found to meet the max distance (%f) requirement.",atomsFound,m_iDistance);
MessageBox(buffer,"The Atomic Analyzer",MB_OK);
I've been puzzling over this slowdown for days and I can't figure anything out. Any is greatly appreciated.