-
Notifications
You must be signed in to change notification settings - Fork 0
/
data3D.cpp
230 lines (198 loc) · 3.36 KB
/
data3D.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
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
#include "input.h"
#include "data3D.h"
void Data3D::Routine()
{
if(INPUT.format3D==1)
{
Format_clear();
}
else if(INPUT.format3D==2)
{
Format_PROFESS();
}
return;
}
void Data3D::Format_PROFESS()
{
cout << " Read in the 3D data format : PROFESS " << endl;
ifstream ifs(INPUT.data_in.c_str());
if(!ifs)
{
cout << " Can't find the file: " << INPUT.data_in << endl;
exit(0);
}
string tmp;
ifs >> tmp >> this->nx >> tmp >> this->ny >> tmp >> this->nz;
int nspin;
ifs >> tmp >> tmp >> tmp >> nspin;
cout << " dimension of 3D data is " << nx << " " << ny << " " << nz << endl;
cout << " spin is " << nspin << endl;
assert(nx>0);
assert(ny>0);
assert(nz>0);
assert(nspin>0);
// read in the 3D data.
double*** data;
data = new double**[nx];
for(int i=0; i<nx; ++i)
{
data[i] = new double*[ny];
for(int j=0; j<ny; ++j)
{
data[i][j] = new double[nz];
}
}
for(int k=0; k<nz; ++k)
{
for(int j=0; j<ny; ++j)
{
for(int i=0; i<nx; ++i)
{
ifs >> data[i][j][k];
}
}
}
// output the profile data.
this->Profile(data);
// delete the 3D data
for(int i=0; i<nx; ++i)
{
for(int j=0; j<ny; ++j)
{
delete[] data[i][j];
}
delete[] data[i];
}
delete[] data;
ifs.close();
return;
}
void Data3D::Format_clear()
{
// read in the dimension of data.
ifstream ifs(INPUT.data_in.c_str());
if(!ifs)
{
cout << " Can't find the file:" << INPUT.data_in << endl;
exit(0);
}
string line1, line2, line3;
getline(ifs, line1);
getline(ifs, line2);
string tmp;
ifs >> tmp >> tmp;
ifs >> nx >> tmp >> ny >> tmp >> nz;
getline(ifs, line3);
cout << " dimension is " << nx << " " << ny << " " << nz << endl;
assert(nx>0);
assert(ny>0);
assert(nz>0);
// read in the 3D data.
double*** data;
data = new double**[nx];
for(int i=0; i<nx; ++i)
{
data[i] = new double*[ny];
for(int j=0; j<ny; ++j)
{
data[i][j] = new double[nz];
}
}
for(int k=0; k<nz; ++k)
{
for(int j=0; j<ny; ++j)
{
for(int i=0; i<nx; ++i)
{
int tmpx, tmpy, tmpz;
ifs >> tmpx >> tmpy >> tmpz >> data[i][j][k];
}
}
}
// output the profile data.
this->Profile(data);
// delete the 3D data
for(int i=0; i<nx; ++i)
{
for(int j=0; j<ny; ++j)
{
delete[] data[i][j];
}
delete[] data[i];
}
delete[] data;
return;
}
void Data3D::Profile(double*** data)
{
double* pdata;
if(INPUT.direction==1)
{
pdata = new double[nx];
}
else if(INPUT.direction==2)
{
pdata = new double[ny];
}
else if(INPUT.direction==3)
{
pdata = new double[nz];
}
ofstream ofs(INPUT.data_out.c_str());
if(!ofs)
{
cout << " Can't open the file: " << INPUT.data_out << endl;
}
if(INPUT.direction==3)
{
for(int k=0; k<nz; ++k)
{
double v = 0.0;
for(int i=0; i<nx; ++i)
{
for(int j=0; j<ny; ++j)
{
v += data[i][j][k];
}
}
ofs << k+1 << " " << v << endl;
}
}
else if(INPUT.direction==2)
{
for(int j=0; j<ny; ++j)
{
double v = 0.0;
for(int i=0; i<nx; ++i)
{
for(int k=0; k<nz; ++k)
{
v += data[i][j][k];
}
}
ofs << j+1 << " " << v << endl;
}
}
else if(INPUT.direction==1)
{
for(int i=0; i<nx; ++i)
{
double v = 0.0;
for(int j=0; j<ny; ++j)
{
for(int k=0; k<nz; ++k)
{
v += data[i][j][k];
}
}
ofs << i+1 << " " << v << endl;
}
}
else
{
cout << "Something wrong." << endl;
exit(0);
}
ofs.close();
return;
}