forked from lingtikong/dump2phonon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver.cpp
173 lines (147 loc) · 4.15 KB
/
driver.cpp
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
#include "atom.h"
#include "driver.h"
#include "fix_phonon.h"
#include <algorithm>
#include "version.h"
#include "timer.h"
/* --------------------------------------------------------------------------- *
* Constructor, main driver
* --------------------------------------------------------------------------- */
Driver::Driver(int narg, char **arg)
{
first = 1;
iframe = 0;
one = ref = NULL;
flist.clear();
fdump = NULL;
char *ctrl; ctrl = NULL;
// analyse command line options
int iarg = 1;
while (narg > iarg){
if (strcmp(arg[iarg],"-h") == 0){ // help
help();
} else { // control file and dump file(s)
if (ctrl == NULL){
ctrl = new char [strlen(arg[iarg])+1];
strcpy(ctrl, arg[iarg]);
} else {
fname.assign(arg[iarg]);
if (find(flist.begin(), flist.end(), fname) == flist.end()) flist.push_back(fname);
}
}
++iarg;
}
// check command line options
if (ctrl == NULL){
printf("\nError: insufficient command line options!\n");
help();
}
int ndump = flist.size();
if (ndump < 1){
printf("\nError: no dump file passed!\n");
help();
}
// initialize fix-phonon
FixPhonon *phonon = new FixPhonon(ctrl);
delete []ctrl;
if (phonon->status > 0) return;
// read first/reference frame from dump file(s)
fname = flist.front();
FILE *fp = fopen(fname.c_str(), "r");
if (fp == NULL){
printf("\nError: file %s not found!\n", fname.c_str());
help();
}
ref = new DumpAtom(fp, fname.c_str());
fclose(fp);
if (ref->initialized == 0){
printf("\nError while reading the first frame from dump file %s!\n", fname.c_str());
help();
}
// initialize phonon
phonon->one = ref;
if (phonon->init()){
printf("\nError while initializing the phonon calculations!\n");
help();
}
phonon->setup();
Timer *timer = new Timer();
// now to proceed the real computation
for (int i = 0; i <= phonon->nskip; ++i) readdump();
while (one->initialized){
one->wrap2ref(ref);
phonon->one = one;
phonon->end_of_step();
delete one; one = NULL;
readdump();
}
// finalize
phonon->post_run();
delete phonon;
timer->stop();
timer->print();
delete timer;
return;
}
/* --------------------------------------------------------------------------- *
* free memories
* --------------------------------------------------------------------------- */
Driver::~Driver()
{
if (ref) delete ref;
if (one) delete one;
if (fdump) fclose(fdump);
fname.clear();
return;
}
/* --------------------------------------------------------------------------- *
* help
* --------------------------------------------------------------------------- */
void Driver::help()
{
printf("\nd2p version 0.%d, compiled on %s %s\n", VERSION, __DATE__, __TIME__);
printf("\nPhonons from MD trajectories.\n");
printf("\nUsage:\n d2p control-file dump-file [dump-file2]\n");
printf("\n");
exit(0);
}
/* --------------------------------------------------------------------------- *
* Read one image and assign to "one"
* --------------------------------------------------------------------------- */
void Driver::readdump()
{
if (first == 1){
while( fdump == NULL && flist.size() > 0){
fname = flist.front(); flist.pop_front();
fdump = fopen(fname.c_str(), "r");
if (fdump == NULL){
printf("\nWarning: faild to open file: %s\n", fname.c_str());
} else {
printf("Now to process file %s, takes time...\n", fname.c_str());
}
}
first = 0;
}
one = new DumpAtom(fdump, fname.c_str());
one->iframe = ++iframe;
if (one->initialized == 0 && flist.size() > 0){
while (flist.size() > 0){
fclose(fdump);
fname = flist.front(); flist.pop_front();
fdump = fopen(fname.c_str(), "r");
if (fdump == NULL){
printf("\nWarning: faild to open file: %s\n", fname.c_str());
} else {
printf("Now to process file %s, takes time...\n", fname.c_str());
iframe = 0;
break;
}
}
if (fdump){
delete one; one = NULL;
readdump();
}
}
return;
}
/* --------------------------------------------------------------------------- */