-
Notifications
You must be signed in to change notification settings - Fork 0
/
fpcf.m
272 lines (232 loc) · 8.93 KB
/
fpcf.m
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
classdef fpcf < handle
%FPCF Four Point Correlation Function
% read 4pcf
% usage:
% a = fpcf(test_dir);
% a.parse_log(log_fn);
% a.read_chi4;
% a.read_g4ol;
% a.read_s4;
% a.read_q;
properties
output_dir = [];
log_file = 'log_test.out';
s4_fn = {};
g4ol_fn = {};
output_prefix = '';
output_deltat = [];
output_len = [];
rho = [];
boxsize = [];
temperature = [];
g4ol = []; % (r, t)
g4ol_t = [];
g4ol_r = [];
s4 = [];
s4_q = [];
s4_t = [];
q1 = [];
q2 = [];
chi4 = [];
end
methods
% output_dir is a char or saved struct
function obj = fpcf(output_dir, log_fn)
%FPCF Construct an instance of this class
% Detailed explanation goes here
% default constructor
if nargin == 0
return
end
if ischar(output_dir) || isstring(output_dir)
obj.output_dir = char(output_dir);
else
error('fpcf: constructor: unknown arg')
end
if exist('log_fn', 'var') && ~isempty(log_fn)
obj.log_file = char(log_fn);
end
if nargin == 2
obj.read_all;
end
end
function read_all(obj)
obj.parse_log;
obj.read_chi4;
obj.read_g4ol;
obj.read_s4;
obj.read_q;
end
function parse_log(obj, log_fn)
%parse_log read var from log
if exist('log_fn', 'var') && ~isempty(log_fn)
obj.log_file = char(log_fn);
end
fn = [obj.output_dir,filesep,obj.log_file];
fid = fopen(fn);
if fid < 0
error(['Failed to open log file: ', fn])
end
sampling_per_point = fpcf.log_read_var(fid, 'est sampling/point');
if sampling_per_point < 2000
disp(['WARNING: sampling_per_point is too few, ', ...
'sampling_per_point=', num2str(sampling_per_point)])
end
obj.output_deltat = fpcf.log_read_var(fid, 'output_deltat');
obj.output_len = fpcf.log_read_var(fid, 'output_len');
obj.boxsize = fpcf.log_read_var(fid, 'box');
obj.rho = fpcf.log_read_var(fid, 'Density rho of group2');
obj.temperature = fpcf.log_read_var(fid, 'temperature');
if obj.temperature <= 0
obj.temperature = [];
end
obj.output_prefix = fpcf.log_read_var(fid, 'output_prefix', 'string');
obj.s4_fn = obj.log_read_filelist(fid, ...
' S4_DIRECT filenames:', ...
' S4_DIRECT END filenames');
% obj.s4_fn = obj.log_read_filelist(fid, ...
% ' S4 filenames:', ...
% ' S4 END filenames');
obj.g4ol_fn = obj.log_read_filelist(fid, ...
' g4ol_filename:', ...
' END g4ol_filename');
% generate g4ol filename for old program
if isempty(obj.g4ol_fn)
% trim(adjustl(output_prefix))//'_g4ol' '_iframe=', trim(adjustl(iframe_str)), '.out'
% C0R-C0R_g4ol_iframe=1.out
g4ol_fn_prefix = [obj.output_prefix, '_g4ol', '_iframe='];
g4ol_fn_suffix = '.out';
obj.g4ol_t = [];
obj.g4ol_fn = {};
for i = 1:max(1, floor(obj.output_len/100)):obj.output_len
obj.g4ol_t(end+1) = (i-1)*obj.output_deltat;
obj.g4ol_fn{end+1} = [g4ol_fn_prefix, num2str(i), g4ol_fn_suffix];
end
else
obj.g4ol_t = cellfun(@(x)(sscanf(x(strfind(x,'_iframe='):end),'_iframe=%d.out')), obj.g4ol_fn);
obj.g4ol_t = obj.output_deltat*(obj.g4ol_t-1);
end
fclose(fid);
end
function read_g4ol(obj)
[obj.g4ol, obj.g4ol_r] = obj.read_files(obj.output_dir, obj.g4ol_fn, '%20.10f%20.10f');
end
function read_s4(obj)
% generate s4_t
% C0R-C0R_s4_direct_fine_t=1176.0000.out
obj.s4_t = cellfun(@(x)(sscanf(x(strfind(x,'_t='):end),'_t=%f.out')), obj.s4_fn);
[obj.s4, obj.s4_q] = obj.read_files(obj.output_dir, obj.s4_fn, '%f %f %f');
end
function read_chi4(obj)
chi4_fn = [obj.output_dir, filesep, obj.output_prefix, '_chi4.out'];
obj.chi4 = obj.read_data_column(chi4_fn);
end
function read_q(obj)
% C0R-C0RQ1.out
q1_fn = [obj.output_dir, filesep, obj.output_prefix, 'Q1.out'];
q2_fn = [obj.output_dir, filesep, obj.output_prefix, 'Q2.out'];
obj.q1 = obj.read_data_column(q1_fn);
obj.q2 = obj.read_data_column(q2_fn);
end
function g4ol2csv(obj, fn)
validateattributes(fn, {'char', 'string'}, {'nonempty'}, 'fpcf.g4ol2csv', 'fn');
if isempty(obj.g4ol)
error('obj.g4ol is empty')
end
g4 = zeros(size(obj.g4ol) + 1);
g4(2:end, 2:end) = obj.g4ol; % (r, t)
g4(2:end, 1) = obj.g4ol_r;
g4(1, 2:end) = obj.g4ol_t;
g4(1, 1) = NaN;
dlmwrite(fn, g4, 'delimiter', ',', 'precision','%20.10f','newline','pc');
end
end % methods
methods(Static)
% return val (double): [prefix] 'var_name' = val [suffix]
% only first occurrence
function val = log_read_var(fid, var_name, var_type)
frewind(fid);
while ~feof(fid)
line = fgetl(fid);
pos = strfind(line, var_name);
if ~isempty(pos)
if length(pos) ~= 1
disp(var_name)
disp(line)
error('wrong log format')
end
if exist('var_type', 'var') && ~isempty(var_type)
if strcmpi(var_type, 'string')
val = sscanf(line(pos:end), [var_name, ' %*[=:] %s']);
else
error('unknown var_type')
end
else
val = sscanf(line(pos:end), [var_name, ' %*[=:] %f']);
end
return
end
end
% not found
val = [];
return
end
% read file list in format:
% startline
% n filename{n}
% endline
function fnlist = log_read_filelist(fid, startline, endline)
fnlist = {};
foundregion = false;
frewind(fid);
while ~feof(fid)
line = fgetl(fid);
if foundregion
if strcmpi(line, endline)
return
else
tmp = textscan(line, '%d %s');
fnlist{end+1} = tmp{2}{1};
end
else
if strcmpi(line, startline)
foundregion = true;
end
end
end
end
function [res, comment] = read_data_column(filename, format)
fid = fopen(filename);
if fid < 0
error(['Failed to open data file: ', filename])
end
if ~exist('format', 'var') || isempty(format)
format = '%f %f';
end
comment = textscan(fid, '# %s');
frewind(fid);
res_cell = textscan(fid, format, 'CommentStyle', '#');
res = cell2mat(res_cell);
fclose(fid);
end
function [data, data_x] = read_files(filedir, filelist, format)
if ~exist('format', 'var') || isempty(format)
format = '%f %f';
end
data = [];
data_x = [];
for i = 1:length(filelist)
tmp = fpcf.read_data_column( ...
[filedir, filesep, filelist{i}], ...
format);
if isempty(data)
data = zeros(size(tmp, 1), length(filelist));
end
data(:, i) = tmp(:, 2);
if isempty(data_x)
data_x = tmp(:, 1);
end
end
end
end % methods(static)
end