-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestIO.m
148 lines (122 loc) · 3.35 KB
/
testIO.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
%% Pass data from Matlab to Fortran and viceversa
%% Method 1: Text files
% Write data from Fortran to txt file
% Read txt file in Matlab using loadArray based on dlmread
% Timing:
% Fortran 0.67, Matlab 0.108 seconds
%% Method 2: Binary files
% Write data from Fortran to .bin file
% Read .bin file in Matlab using loadBinary based on fread
% Note: in both methods, always vectorize multidimensional arrays,
% recalling that both Matlab and Fortran store array in column-major order
% Timing:
% Fortran 0.56, Matlab 0.012 seconds
clear;clc;close all
addpath(fullfile('..'));
% Set some parameters in Matlab
par.na = 1000;
par.nz = 7;
par.howread = 2; % 1=txt format, 2=binary format
par.alpha = 0.36;
par.a_grid = linspace(0.01,10,par.na)';
par.z_grid = linspace(1,5,par.nz)';
% Export integer parameters to Fortran
int_params_names = {'na','nz','howread'};
num_par = numel(int_params_names);
int_params = ones(num_par,1);
for ii = 1:num_par
int_params(ii) = par.(int_params_names{ii});
end
% Export real parameters to Fortran
real_params_names = {'alpha'};
num_par = numel(real_params_names);
real_params = ones(num_par,1);
for ii = 1:num_par
real_params(ii) = par.(real_params_names{ii});
end
disp('Writing files..')
% Dimensions
dimensions = [length(real_params);length(int_params)]; %the dimensions
dlmwrite('dimensions.txt', dimensions, 'delimiter', '\t', 'precision', 17);
dlmwrite('int_params.txt', int_params, 'delimiter', '\t', 'precision', 17);
dlmwrite('real_params.txt', real_params, 'delimiter', '\t', 'precision', 17);
% Write Arrays to files
dlmwrite('a_grid.txt', par.a_grid, 'delimiter', '\t', 'precision', 17);
dlmwrite('z_grid.txt', par.z_grid, 'delimiter', '\t', 'precision', 17);
%% Call the Fortran executable
% The file 'run.exe' is generated by Fortran
disp('Calling the executable now:')
[status, ~] = system('run.exe','-echo');
if status ~= 0
error('FORTRAN procedure was not executed properly.')
end
%% Read files generated by Fortran
disp('Reading files generated by fortran.')
disp('Please wait...')
siz = [par.na,par.nz];
if par.howread ==1
% Method 1: txt format
tic
for i=1:20
output = loadArray('output.txt',siz);
end
toc
elseif par.howread == 2
% Method 2: binary format
tic
for i=1:20
output = loadBinary('output.bin','double',siz);
end
toc
end
disp('Files read!')
%% Plot Fortran results
plot(par.a_grid,output)
% A = randi(10,[100000,1]); % random integer b/w 1 and 10
%
% % Write to a text file
% dlmwrite('A.txt', A, 'delimiter', '\t', 'precision', 17);
%
% % Write to a binary file
% fid = fopen('A.bin','wb');
% fwrite(fid,A,'double');
% fclose(fid);
%
% % %% txt, load
% % tic
% % data1 = load('A.txt');
% % toc
% %
% % %% txt, dlmread
% % tic
% % data2 = dlmread('A.txt','\t');
% % toc
% %
% % %% txt, fscanf
% % tic
% % FID = fopen('A.txt','rt');
% % data3 = fscanf(FID,'%f');
% % fclose(FID);
% % toc
% %
% % %% binary, fread
% % tic
% % FID = fopen('A.bin','rb');
% % data4 = fread(FID,inf,'double');
% % fclose(FID);
% % toc
% %
% % isequal(A,data4)
%
% %% Now test my functions
%
% % Read text data
%
% siz = size(A);
% tic
% x1 = loadArray('A.txt',siz);
% toc
%
% tic
% x2 = loadBinary('A.bin', 'double', siz);
% toc