-
Notifications
You must be signed in to change notification settings - Fork 13
/
compactify_vels.cpp
155 lines (122 loc) · 3.28 KB
/
compactify_vels.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
// -*- coding: utf-8 -*-
// Kimmo Sääskilahti, 2015
// Usage: compactify_vels lammps_dump_file [output_file]
/*
Program writes the velocity data file produced by LAMMPS
in a more compact, ravelled form without the additional data
appearing for each time step in the dump file. The assumed commands used for dumping the velocities from LAMMPS is as follows:
dump vels interface custom dt_dump filename.vels.dat id type vx vy vz
dump_modify vels format "%d %d %.8g %.8g %.8g"
dump_modify vels sort id
"interface" is the group of atoms at the interface where SHC is calculated
"dt_dump" is the sampling time step (integer) specifying the maximum frequency
*/
#include <stdio.h>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void skip_lines(istream&,size_t);
int main(int argc,char** argv){
if(argc==1){
cout<<"Usage: Give the filenames on the command line."<<endl;
return(0);
}
string filename(argv[1]);
string filename2;
if(argc==2){
filename2=filename+".compact";
}
else if(argc==3)
filename2.assign(argv[2]);
else{
cout<<"Usage: Give only the filenames on the command line."<<endl;
return(0);
}
//cin>>filename;
//cout<<"Reading from file "<<filename<<"."<<endl;
//cout<<"Give the filename where to write:"<<endl;
//cin>>filename2;
cout<<"Writing to file "<<filename2<<"."<<endl;
ifstream file1;
double a,b;
//file1.open(filename.c_str());
file1.open(argv[1]);
string str;
int t0,t1;
int N;
int *ids;
if(file1.is_open()){ // Initializations, set number of particles etc.
getline(file1,str);
//cout<<"Read: "<<str<<endl;
file1>>t0;
cout<<"The initial time step is "<<t0<<"."<<endl;
getline(file1,str); // The line break
getline(file1,str);
//cout<<"Read: "<<str<<endl;
file1 >> N;
cout<<"The number of atoms is "<<N<<"."<<endl;
ids=new int[N];
getline(file1,str); // Line break
skip_lines(file1,5);
for (int i=0;i<N;i++){
file1>>ids[i];
getline(file1,str); // Skip the rest of the line
cout<<ids[i]<<endl;
}
skip_lines(file1,1);
file1>>t1;
cout<<"The difference of time steps is "<<t1-t0<<"."<<endl;
}
else{
cout<<"Could not open file "<<filename<<", exiting."<<endl;
return 0;
}
// Rewind back to the beginning
file1.seekg(0,ios::beg);
int id,type;
double vx,vy,vz;
size_t iter=0;
ofstream file2;
file2.open(filename2.c_str());
file2<<"Atoms "<<N<<endl;
file2<<"d_timestep "<<t1-t0<<endl;
file2<<"Atom ids:"<<endl;
for(int i=0;i<N;i++){
file2<<ids[i]<<endl;
}
file2<<"----------"<<endl;
//while(!file1.eof()){
while(file1.peek() != char_traits<wchar_t>::eof()){ // The next character is not EOF
if ((iter)%100000==0)
cout<<"iter="<<iter<<endl;
iter++;
//getline(file1,str);
//cout<<"Read: "<<str<<endl;
skip_lines(file1,9);
//getline(file1,str);
//cout<<"Read: "<<str<<endl;
// Start reading the velocities
int i;
for(i=0;i<N;i++){
file1>>id>>type>>vx>>vy>>vz;
//file2<<id<<'\n';
file2<<vx<<'\n';
file2<<vy<<'\n';
file2<<vz<<'\n';
//cout<<id<<vx<<vy<<vz<<endl;
}
//cout<<id<<endl;
getline(file1,str); // The line break
//return 0;
}
file1.close();
file2.close();
return 0;
}
void skip_lines(istream& pStream, size_t pLines)
{
string s;
for (; pLines; --pLines)
getline(pStream, s);
}