-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathfromISwaveResultsToTxt.m
237 lines (184 loc) · 8.3 KB
/
fromISwaveResultsToTxt.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
function fromISwaveResultsToTxt(ISwave_results, prefix)
% FROMISWAVERESULTSTOTXT - Exports data of a set of impedance simulations to text files
% save the main data from an ISwave_results struct created by
% ISwave_full_exec* to text files, for easing the import with Origin (from OriginLab).
%
%
% Syntax: fromISwaveResultsToTxt(ISwave_results, prefix)
%
% Inputs:
% ISWAVE_RESULTS - a struct containing the most important results of the ISwave simulation
% PREFIX - char array, prefix to be used for the text files names
%
% Example:
% fromISwaveResultsToTxt(ISwave_oc, 'ISwave_oc')
% save data from a set of simulations to text files
%
% Other m-files required: none
% Subfunctions: none
% MAT-files required: none
%
% See also ISwave_full_exec.
% Author: Ilario Gelmetti, Ph.D. student, perovskite photovoltaics
% Institute of Chemical Research of Catalonia (ICIQ)
% Research Group Prof. Emilio Palomares
% email address: [email protected]
% Supervised by: Dr. Phil Calado, Dr. Piers Barnes, Prof. Jenny Nelson
% Imperial College London
% October 2017; Last revision: January 2018
%------------- BEGIN CODE --------------
%% create header
% check which was the variable being explored
if numel(unique(ISwave_results.Int)) > 1
legend_text = ISwave_results.Int;
legend_append = ' sun';
else
legend_text = ISwave_results.Vdc;
legend_append = ' Vdc';
end
% round to two significant digits
legendImpedance = round(legend_text, 2, 'significant');
% this will start from 1 sun and go to dark
legendImpedance = string(legendImpedance);
% add sun to numbers in legend
legendImpedance = strcat(legendImpedance, legend_append);
% replace zero in legend with dark
legendImpedance(legendImpedance=="0 sun") = "dark";
headerFrequencyIntVdc = ['Frequency', legendImpedance'];
headerReIm = repelem(strcat(legendImpedance, ' real'), 2);
headerReIm(2:2:end) = legendImpedance;
if iscolumn(headerReIm)
headerNyquist = ['Frequency', headerReIm'];
else
headerNyquist = ['Frequency', headerReIm];
end
%% get measure units
unitsCap = ['Hz', repelem("F/cm\+(2)", length(legendImpedance))];
unitsNyquist = ['Hz', repelem("\g(W)cm\+(2)", 2*length(legendImpedance))];
unitsZabs = ['Hz', repelem("\g(W)cm\+(2)", length(legendImpedance))];
unitsPhase = ['Hz', repelem("rad", length(legendImpedance))];
unitsJ = ['Hz', repelem("A/cm\+(2)", length(legendImpedance))];
%% add comments, like VOCs
commentV_DC = ["V_DC", ISwave_results.Vdc'];
%% get data
% all the lines are the same for ISwave
frequencies = ISwave_results.Freq(1, :)';
% capacitance
cap = ISwave_results.cap';
dataCap = [frequencies, cap];
% ionic capacitance
capIonic = ISwave_results.cap_idrift';
dataCapIonic = [frequencies, capIonic];
% recombination capacitance
capRec = ISwave_results.cap_U';
dataCapRec = [frequencies, capRec];
% accumulating current capacitance
capAcc = ISwave_results.cap_dQ';
dataCapAcc = [frequencies, capAcc];
% nyquist
impedance_re = ISwave_results.impedance_re';
impedance_im = ISwave_results.impedance_im';
impedance = impedance_im(:, [1;1]*(1:size(impedance_im, 2)));
impedance(:, 1:2:end) = impedance_re;
dataNyquist = [frequencies, impedance];
% absolute impedance
impedance_abs = ISwave_results.impedance_abs';
dataZabs = [frequencies, impedance_abs];
% phase
Zphase = -ISwave_results.J_phase';
dataZphase = [frequencies, Zphase];
% ionic phase
ZphaseIonic = -ISwave_results.J_i_phase';
dataZphaseIonic = [frequencies, ZphaseIonic];
% absolute ionic current amplitude
JionicAmpAbs = abs(ISwave_results.J_i_amp)';
dataJionicAmpAbs = [frequencies, JionicAmpAbs];
% absolute out of phase current amplitude
JampOutOfPhaseAbs = abs(ISwave_results.J_amp.*sin(ISwave_results.J_phase))';
dataJampOutOfPhaseAbs = [frequencies, JampOutOfPhaseAbs];
% absolute out of phase recombination current amplitude
JrecAmpOutOfPhaseAbs = abs(ISwave_results.J_U_amp.*sin(ISwave_results.J_U_phase))';
dataJrecAmpOutOfPhaseAbs = [frequencies, JrecAmpOutOfPhaseAbs];
%% join fields
toBeSavedCap = [headerFrequencyIntVdc; unitsCap; dataCap];
toBeSavedCapIonic = [headerFrequencyIntVdc; unitsCap; dataCapIonic];
toBeSavedCapRec = [headerFrequencyIntVdc; unitsCap; dataCapRec];
toBeSavedCapAcc = [headerFrequencyIntVdc; unitsCap; dataCapAcc];
toBeSavedNyquist = [headerNyquist; unitsNyquist; dataNyquist];
toBeSavedZabs = [headerFrequencyIntVdc; unitsZabs; dataZabs];
toBeSavedZphase = [headerFrequencyIntVdc; unitsPhase; dataZphase];
toBeSavedZphaseIonic = [headerFrequencyIntVdc; unitsPhase; dataZphaseIonic];
toBeSavedJionicAmpAbs = [headerFrequencyIntVdc; unitsJ; commentV_DC; dataJionicAmpAbs];
toBeSavedJampOutOfPhaseAbs = [headerFrequencyIntVdc; unitsJ; commentV_DC; dataJampOutOfPhaseAbs];
toBeSavedJrecAmpOutOfPhaseAbs = [headerFrequencyIntVdc; unitsJ; commentV_DC; dataJrecAmpOutOfPhaseAbs];
%% set NaNs to NaN
toBeSavedCap = fillmissing(toBeSavedCap, 'constant', "NaN");
toBeSavedCapIonic = fillmissing(toBeSavedCapIonic, 'constant', "NaN");
toBeSavedCapRec = fillmissing(toBeSavedCapRec, 'constant', "NaN");
toBeSavedCapAcc = fillmissing(toBeSavedCapAcc, 'constant', "NaN");
toBeSavedNyquist = fillmissing(toBeSavedNyquist, 'constant', "NaN");
toBeSavedZabs = fillmissing(toBeSavedZabs, 'constant', "NaN");
toBeSavedZphase = fillmissing(toBeSavedZphase, 'constant', "NaN");
toBeSavedZphaseIonic = fillmissing(toBeSavedZphaseIonic, 'constant', "NaN");
toBeSavedJionicAmpAbs = fillmissing(toBeSavedJionicAmpAbs, 'constant', "NaN");
toBeSavedJampOutOfPhaseAbs = fillmissing(toBeSavedJampOutOfPhaseAbs, 'constant', "NaN");
toBeSavedJrecAmpOutOfPhaseAbs = fillmissing(toBeSavedJrecAmpOutOfPhaseAbs, 'constant', "NaN");
%% save csv
fid_cap = fopen([prefix '-cap.txt'], 'wt+');
fid_capIonic = fopen([prefix '-capIonic.txt'], 'wt+');
fid_capRec = fopen([prefix '-capRecombination.txt'], 'wt+');
fid_capAcc = fopen([prefix '-capAccumulating.txt'], 'wt+');
fid_nyquist = fopen([prefix '-nyquist.txt'], 'wt+');
fid_Zabs = fopen([prefix '-Zabs.txt'], 'wt+');
fid_phase = fopen([prefix '-Zphase.txt'], 'wt+');
fid_phaseIonic = fopen([prefix '-ZphaseIonic.txt'], 'wt+');
fid_JionicAmpAbs = fopen([prefix '-JionicAmpAbs.txt'], 'wt+');
fid_JampOutOfPhaseAbs = fopen([prefix '-JampOutOfPhaseAbs.txt'], 'wt+');
fid_JrecAmpOutOfPhaseAbs = fopen([prefix '-JrecAmpOutOfPhaseAbs.txt'], 'wt+');
for i = 1:size(toBeSavedCap, 1)
fprintf(fid_cap, '%s\t', toBeSavedCap(i, 1:end-1));
fprintf(fid_cap, '%s', toBeSavedCap(i, end));
fprintf(fid_cap, '\n');
fprintf(fid_capIonic, '%s\t', toBeSavedCapIonic(i, 1:end-1));
fprintf(fid_capIonic, '%s', toBeSavedCapIonic(i, end));
fprintf(fid_capIonic, '\n');
fprintf(fid_capRec, '%s\t', toBeSavedCapRec(i, 1:end-1));
fprintf(fid_capRec, '%s', toBeSavedCapRec(i, end));
fprintf(fid_capRec, '\n');
fprintf(fid_capAcc, '%s\t', toBeSavedCapAcc(i, 1:end-1));
fprintf(fid_capAcc, '%s', toBeSavedCapAcc(i, end));
fprintf(fid_capAcc, '\n');
fprintf(fid_nyquist, '%s\t', toBeSavedNyquist(i, 1:end-1));
fprintf(fid_nyquist, '%s', toBeSavedNyquist(i, end));
fprintf(fid_nyquist, '\n');
fprintf(fid_Zabs, '%s\t', toBeSavedZabs(i, 1:end-1));
fprintf(fid_Zabs, '%s', toBeSavedZabs(i, end));
fprintf(fid_Zabs, '\n');
fprintf(fid_phase, '%s\t', toBeSavedZphase(i, 1:end-1));
fprintf(fid_phase, '%s', toBeSavedZphase(i, end));
fprintf(fid_phase, '\n');
fprintf(fid_phaseIonic, '%s\t', toBeSavedZphaseIonic(i, 1:end-1));
fprintf(fid_phaseIonic, '%s', toBeSavedZphaseIonic(i, end));
fprintf(fid_phaseIonic, '\n');
fprintf(fid_JionicAmpAbs, '%s\t', toBeSavedJionicAmpAbs(i, 1:end-1));
fprintf(fid_JionicAmpAbs, '%s', toBeSavedJionicAmpAbs(i, end));
fprintf(fid_JionicAmpAbs, '\n');
fprintf(fid_JampOutOfPhaseAbs, '%s\t', toBeSavedJampOutOfPhaseAbs(i, 1:end-1));
fprintf(fid_JampOutOfPhaseAbs, '%s', toBeSavedJampOutOfPhaseAbs(i, end));
fprintf(fid_JampOutOfPhaseAbs, '\n');
fprintf(fid_JrecAmpOutOfPhaseAbs, '%s\t', toBeSavedJrecAmpOutOfPhaseAbs(i, 1:end-1));
fprintf(fid_JrecAmpOutOfPhaseAbs, '%s', toBeSavedJrecAmpOutOfPhaseAbs(i, end));
fprintf(fid_JrecAmpOutOfPhaseAbs, '\n');
end
fclose(fid_cap);
fclose(fid_capIonic);
fclose(fid_capRec);
fclose(fid_capAcc);
fclose(fid_nyquist);
fclose(fid_Zabs);
fclose(fid_phase);
fclose(fid_phaseIonic);
fclose(fid_JionicAmpAbs);
fclose(fid_JampOutOfPhaseAbs);
fclose(fid_JrecAmpOutOfPhaseAbs);
%------------- END OF CODE --------------