-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassign_tp.cc
More file actions
executable file
·243 lines (233 loc) · 4.88 KB
/
assign_tp.cc
File metadata and controls
executable file
·243 lines (233 loc) · 4.88 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
* pTP_crd.cc
*
* assign points to transition paths
*/
#include <unistd.h>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
using namespace std;
const char * Usage = "\n\n\
Usage:\n\
\n\
assign_tp -h \n\
for help \n\
assign_tp -l lag -a a_max -b b_min -r r.dat -o outp.dat\n\
\n\
Will write a sequence of numbers, 1 per line\n\
0 for state A\n\
1 for TP\n\
2 for state B\n\
where:\n\
-a a_max is the maximum value of the coord defining state A;\n\
-b b_min is the minimum value of the coord defining state B;\n\
-r r.dat is the file containing values of the \n\
reaction coordinate\n\
-l lag is an optional lag (default: 1) to wait before state change\n\
-o outp.dat is the file for writing output\n\n";
double next_float(FILE *f)
{
const int BUF_LEN = 1024;
char *tok, buf[BUF_LEN];
fgets(buf,BUF_LEN,f);
tok = strtok(buf," \t");
return atof(tok);
}
void bin_data(vector<double> &bins, double bin_lo, double delta, double x)
{
int bin, nbin;
nbin = bins.size();
bin = int(floor((x-bin_lo)/delta));
if (bin >= 0 && bin<nbin)
bins[bin]++;
return;
}
void add_data(vector<double> &a, vector<double> &b)
{
/* add a to b */
for (int i=0; i<a.size(); i++) {
b[i] += a[i];
}
return;
}
int main(int argc, char *argv[])
{
int c, tt, lag, rest, ina, inb, ntp;
char state;
const int BUF_LEN = 1024;
char eq_crd[BUF_LEN], outp_name[BUF_LEN];
char tok[BUF_LEN], buf[BUF_LEN];
int ndat, nbin;
vector<double> pxTP, pTPx, peqx, ptmp;
FILE *eq_rxf, *tp_rxf, *outp;
double eqx, tpx, amax, bmin;
// parse cmd line options
amax = 0.5;
bmin = 0.8;
lag = 1;
while (1) {
c=getopt(argc,argv,"ha:b:r:o:l:");
if (c == -1) // no more options
break;
switch (c) {
case 'h':
fprintf(stdout,"%s\n",Usage);
exit(0);
break;
case 'a':
amax = atof(optarg);
break;
case 'b':
bmin = atof(optarg);
break;
case 'l':
lag = atoi(optarg);
break;
case 'r':
strcpy(eq_crd,optarg);
break;
case 'o':
strcpy(outp_name,optarg);
break;
default:
fprintf(stderr,"?? getopt returned character code 0%o ??\n", c);
fprintf(stderr,"%s\n",Usage);
exit(1);
}
}
fprintf(stdout,"state A max = %8.3f\n",amax);
fprintf(stdout,"state B min = %8.3f\n",bmin);
eq_rxf = fopen(eq_crd,"r");
if (eq_rxf == NULL) {
fprintf(stderr,"Could not read rxn coord file: %s\n",eq_crd);
fprintf(stderr,"%s\n",Usage);
exit(1);
}
// count equilibrium data
fgets(buf,BUF_LEN,eq_rxf);
ndat = 0;
while(feof(eq_rxf)==0) {
ndat++;
fgets(buf,BUF_LEN,eq_rxf);
}
fclose(eq_rxf);
eq_rxf = fopen(eq_crd,"r");
state = 'i';
outp = fopen(outp_name,"w");
if (outp == NULL) {
fprintf(stderr,"Could not open file %s for output\n", outp);
exit(1);
}
fprintf(stdout,"ndat = %i\n",ndat);
tt = 0;
ntp = 0;
for (int i=0; i<ndat; i++) {
eqx = next_float(eq_rxf);
//fprintf(outp,"%1i\t",0);
//continue;
if (state == 'i') {
if (eqx < amax) { // in A
state = 'A';
tt++;
for (int j=0; j<tt; j++) {
fprintf(outp,"%1i\n",0); // in A
}
} else if (eqx > bmin) { // in B
state = 'B';
tt++;
for (int j=0; j<tt; j++) {
fprintf(outp,"%1i\n",2); // in B
}
} else { // not in A or B yet
tt++;
continue;
}
} else if (state == 'A' && eqx > amax) { // possible tp
tt = 1;
rest = 0;
inb = 0;
state = 'a';
} else if (state == 'B' && eqx < bmin) {
tt = 1;
rest = 0;
ina = 0;
state = 'b';
} else if (state == 'a') { // possible A->B tp
if (eqx < amax) {
tt+=1+inb;
for (int j=0; j<tt; j++) {
fprintf(outp,"%1i\n",0); // back in A
}
state = 'A';
} else if (eqx > bmin) {
rest++;
inb++;
if (rest>=lag) { // is tp
ntp++;
for (int j=0; j<tt; j++) {
fprintf(outp,"%1i\n",1);
}
for (int j=0; j<inb; j++) {
fprintf(outp,"%1i\n",2); // now in B
}
state = 'B';
}
} else {
if (inb>0) {
inb++;
} else {
tt++;
}
}
} else if (state == 'b') { // possible B->A tp
if (eqx > bmin) {
tt+=1+ina;
for (int j=0; j<tt; j++) {
fprintf(outp,"%1i\n",2); // in B
}
state = 'B';
} else if (eqx < amax) {
rest++;
ina++;
if (rest>=lag) { // is tp
ntp++;
for (int j=0; j<tt; j++) {
fprintf(outp,"%1i\n",1); // is tp
}
for (int j=0; j<ina; j++) {
fprintf(outp,"%1i\n",0); // now in A
}
state = 'A';
}
} else {
if (ina>0) {
ina++;
} else {
tt++;
}
}
} else {
if (state == 'A') {
fprintf(outp,"%1i\n",0);
} else {
fprintf(outp,"%1i\n",2);
}
}
}
if (state == 'a') {
for (int j=0; j<tt; j++) {
fprintf(outp,"%1i\n",0); // in A
}
} else if (state == 'b') {
for (int j=0; j<tt; j++) {
fprintf(outp,"%1i\n",2); // in B
}
}
fprintf(stdout,"Number of transition paths = %i\n", ntp);
fclose(eq_rxf);
fclose(outp);
return 0;
}