-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmakeHistogram1.5.cpp
More file actions
194 lines (148 loc) · 4.01 KB
/
makeHistogram1.5.cpp
File metadata and controls
194 lines (148 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include<stdio.h>
#include <stdlib.h>
#include<iostream>
#include<fstream>
#include<cmath>
#include <iomanip>
#include <string>
#include <sstream>
#include <string.h>
using namespace std;
ifstream infile;
//1.5 Bugfix, removed junk code, increased string buffer from 1024 to 4096 ---WARNING if your dihedral line is greater than 4096 bits, you must increase this buffer otherwise floating pt exception will occur or other overflow errors...
//version 1.4 fix bugs in last column numerical value, use stable conversion methods
//version 1.2 set the number of coordinates, replace natoms*ndihedrals with just ncoords
//bugfix: last column histogram
int main(int argc, char* argv[])
{
if(argc!=2)
{
cout<<"No input file. Use: makeHistogram [dihedral coordinate file]"<<endl;
return 0;
}
string input="";
cout<<"Enter the number of coordinates being analyzed"<<endl;
cin>>input;
int ncoords=1,charcount=0,linecount=0,index=0,ind2=0;
ncoords=atoi(input.c_str());
cout<<"You entered "<<ncoords<<" coordinates. Begin histogram"<<endl;
double * max=new double[ncoords];
double * min=new double[ncoords];
double coord=0;
double * coordinates = new double[ncoords]; //dynamic bruh.
for (int i=0; i<(ncoords); i++)
{
coordinates[i]=0;
min[i]=0;
max[i]=0;
}
char c[4096];
char * s_text;
string number="";
infile.open(argv[1], std::ifstream::in);
bool beginAtom=false,record=false;
while(!infile.fail()&&!infile.eof())
{
infile.getline(c,4096);
s_text=strtok(c," ,{}");//delimiters & set the search at beginning of the line
while(s_text!=NULL) //convert the line to numbers
{
coord=atof(s_text);
if(linecount==0)
{
max[index]=coord;
min[index]=coord;
}
else
{
if(max[index]<coord) {max[index]=coord;}
else if(min[index]>coord) {min[index]=coord;}
}
s_text=strtok(NULL," ,{}");
index++;
}
linecount++;
index=0;
}
linecount--;//correction for extra line read on failbit
cout<<"max values for dihedrals"<<endl;
for (int i=0; i<ncoords; i++)
{
cout<<max[i]<<", ";
//cout<<"res "<<i<<" phi min max: "<<min[2*i]<<' '<<max[2*i]<<endl;
//cout<<"res "<<i<<" psi min max: "<<min[2*i+1]<<' '<<max[2*i+1]<<endl;
}
cout<<endl;
cout<<"minvalues"<<endl;
for (int i=0; i<ncoords; i++)
{
cout<<min[i]<<", ";
}
cout<<'\n'<<linecount<<" lines analysed, begin histogram for 100 bars at 3.6 degree increments..."<<endl;
//BEGIN HISTOGRAM********************
int totallines=linecount;
infile.clear();//clear failbits/eof
infile.seekg(0,infile.beg);//rewind
linecount=0;
int ** histogram = new int*[100];//natoms columns and 100 rows
for (int j=0; j<100; j++)
{
histogram[j]=new int[ncoords];
for (int i=0;i<ncoords;i++)
{
histogram[j][i]=0;
}
}//make array 0s
double * hrange=new double[ncoords];
cout<<"histogram differentials: "<<endl;
for(int i=0; i<ncoords; i++)
{
hrange[i]=(max[i]-min[i])/100;//size of the histogram groupings ********NOT USED NOW
//cout<<hrange[i]<<" "; DEBUG
}
cout<<"percent complete: "<<endl;
index=0;
number="";
while(!infile.fail()&&!infile.eof())
{
printf("%.2i\b\b",(100*linecount/totallines));
infile.getline(c,4096);
s_text=strtok(c," ,{}");//delimiters & set the search at beginning of the line
while(s_text!=NULL) //convert the line to numbers
{
coordinates[index]=atof(s_text);
s_text=strtok(NULL," ,{}"); //next number
index++;//next index
}
index=0;
linecount++;
for(int i=0; i<ncoords; i++) //coord ary to histogram
{
for(int j=0; j<100; j++)
{
//ALTERNATIVE if(coordinates[i]>=(hrange[i]*j)+min[i]&&coordinates[i]<hrange[i]*(j+1)+min[i])//go from smallest value to largest value
if(coordinates[i]>=(3.6*j)-180&&coordinates[i]<3.6*(j+1)-180)
{
histogram[j][i]++;
break;//for speed
}
}
}
}
infile.close();//close the stream
ofstream outputf;
outputf.open("histogramOutput.csv",std::ofstream::trunc);//discard file if it already exists
std::ostringstream oss;
for(int j=0; j<100; j++)
{
for(int i=0; i<ncoords; i++)
{
oss<<histogram[j][i];
if(i==ncoords-1){oss << "\n";}
else {oss <<", ";}
}
}
outputf<<oss.str();
cout<<"\nComplete! Exiting normally"<<endl;
return 0;
}