Eötvös Quantum Utilities  v4.9.146
Providing the Horsepowers in the Quantum Realm
adaptiveQ.m
Go to the documentation of this file.
1 %% Eotvos Quantum Transport Utilities - adaptiveQ
2 % Copyright (C) 2009-2015 Peter Rakyta, Ph.D.
3 %
4 % This program is free software: you can redistribute it and/or modify
5 % it under the terms of the GNU General Public License as published by
6 % the Free Software Foundation, either version 3 of the License, or
7 % (at your option) any later version.
8 %
9 % This program is distributed in the hope that it will be useful,
10 % but WITHOUT ANY WARRANTY; without even the implied warranty of
11 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 % GNU General Public License for more details.
13 %
14 % You should have received a copy of the GNU General Public License
15 % along with this program. If not, see http://www.gnu.org/licenses/.
16 %
17 %> @addtogroup basic Basic Functionalities
18 %> @{
19 %> @file adaptiveQ.m
20 %> @brief A class providing adaptive distribution of the transverse momentum points.
21 %> @}
22 %> @brief A class providing adaptive distribution of the transverse momentum points.
23 %> A minimal list of transverse momentums (#qvec) and the corresponding function values (#y) of fhandle are dynamicaly created in order to obtain the values of fhandle over the elements of #interpq via interpolation. (Typically #qvec contains significantly less elements than #interpq.)
24 %> @Available
25 %> EQuUs v4.6 or later
26 %%
28 
29 properties ( Access = protected )
30 %> The number of elements in qvec in the first iteration.
31  start_qnum
32 %> The list of transverse momentums where the function fhandle is desired to be calculated.
33  interpq
34 %> The interpolated values of fhandle over the elements of interpq.
35  interpy
36 %> The dynamically created vector of the transverse momentums.
37  qvec
38 %> The function values of fhandle calculated over the elements of qvec.
39  y
40 %> Maximum of the iterations.
41  maxiter
42 %> Logical value. If true, the iteration cintinues with the loaded data, or false (default) otherwise.
43  resume
44 %> The filename to export the calculated data.
45  outFileName
46 %> Method of the interpolation (see http://www.mathworks.com/help/matlab/ref/interp1.html )
47  interp_method
48 end
49 
50 
51 
52 methods ( Access = public )
53 
54 %% adaptiveQ
55 %> @brief Constructor of the class.
56 %> @param Opt An instance of the structure Opt.
57 %> @param interpq An array of transverse momentum points at which fhandle is calculated via interpolation.
58 %> @param varargin Cell array of optional parameters. For details see @InputParsing.
59 %> @return An instance of the class
60 function obj = adaptiveQ( Opt, interpq, varargin )
61  obj = obj@Messages( Opt );
62  obj.start_qnum = 4;
63  obj.interpq = interpq; % the transverse momentums of our interest (usually equidistant).
64  obj.interpy = []; % the interpolated function values at momentums given in interpq.
65  obj.qvec = []; % the adaptive 1D grid of transverse momentums used to iteroplate over interpq
66  obj.y = []; % the function values calculated on the adaptive grid qvec
67  obj.maxiter = 10e2;
68  obj.resume = [];
69  obj.outFileName = [];
70  obj.interp_method = [];
71 
72  obj.InputParsing( varargin{:} )
73 end
74 
75 %% runAdaptiveIterations
76 %> @brief Runs the adaptive iterations to obtain the elemets of qvec where the function fhandle changes the most.
77 %> @param fhandle The function handle to be calculated over the transverse momentum points: #y = fhandle( #qvec ), where #y is an MxN matrix, and #qvec is a vector of transverse momentums containing of M elements.
78  function runAdaptiveIterations( obj, fhandle )
79 
80  obj.display( '*************************************')
81  obj.display( 'Starting adaptive iterations for transverse momentum computations.', 1 );
82  obj.display( '*************************************')
83 
84  if obj.resume && ~isempty(obj.outFileName)
85 
86  try
87  HandleForLoad = LoadFromFile( obj.outFileName );
88  obj.qvec = HandleForLoad.LoadVariable('qvec', 'NoEmpty', 'On');
89  obj.y = HandleForLoad.LoadVariable('y', 'NoEmpty', 'On');
90  obj.interpq = HandleForLoad.LoadVariable('interpq', 'NoEmpty', 'On');
91  obj.interpy = HandleForLoad.LoadVariable('interpy', 'NoEmpty', 'On');
92  HandleForLoad.ClearLoadedVariables();
93  catch err
94  obj.display( 'Failed to load data. Restarting calculations.')
95  end
96  end
97 
98  if isempty( obj.interpy )
99  obj.initialize( fhandle )
100  end
101 
102  for idx = 1:obj.maxiter
103 
104  % break the iteration if convergence is reched
105  unaccurateq = obj.getUnaccurateq( );
106  if isempty(unaccurateq)
107  obj.display('convergence reached!!!!!!!!!!');
108  break
109  end
110 
111  newq = obj.defNewq( unaccurateq );
112  newy = feval( fhandle, newq );
113  obj.joinq( newq, newy );
114 
115  obj.display( ' ');
116  obj.display( '*************************************', 1)
117  obj.display( ['Iteration ', num2str(idx), ' finished.'], 1 );
118  obj.display( '*************************************', 1)
119  obj.display( ' ', 1);
120 
121  if ~isempty( obj.outFileName )
122  qvec = obj.qvec;
123  y = obj.y;
124  interpy = obj.interpy;
125  interpq = obj.interpq;
126  maxiter = obj.maxiter;
127  resume = obj.resume;
128  start_qnum = obj.start_qnum;
129  save( obj.outFileName, 'qvec', 'y', 'interpy', 'interpq', 'maxiter', 'resume', 'start_qnum');
130  end
131  end
132 
133 
134  end
135 
136 %% initialize
137 %> @brief Initializes #y = fhandle( #qvec ) for the first iteration.
138 %> @param fhandle The function handle to be calculated over the transverse momentum points: #y = fhandle( #qvec ), where #y is an MxN matrix, and #qvec is a vector of transverse momentums containing of M elements.
139  function initialize( obj, fhandle )
140 
141  qmax = max( obj.interpq );
142  qmin = min( obj.interpq );
143  deltaq = (qmax-qmin)/(obj.start_qnum-1);
144  obj.qvec = transpose(qmin:deltaq:qmax);
145 
146 
147  obj.y = feval( fhandle, obj.qvec );
148  obj.interpy = NaN(length( obj.interpq), size(obj.y,2) );
149 
150  % the first and last elements coincide with the iterpolation array
151  obj.interpy(1,:) = obj.y(1,:);
152  obj.interpy(end,:) = obj.y(end,:);
153  end
154 
155 
156 %% Write
157 %> @brief Sets the value of an attribute in the interface.
158 %> @param MemberName The name of the attribute to be set.
159 %> @param input The value to be set.
160  function Write(obj, MemberName, input)
161 
162  try
163  obj.(MemberName) = input;
164  catch
165  error(['EQuUs:', class(obj), ':Read'], ['No property to write with name: ', MemberName]);
166  end
167 
168  end
169 %% Read
170 %> @brief Query for the value of an attribute in the interface.
171 %> @param MemberName The name of the attribute to be set.
172 %> @return Returns with the value of the attribute.
173  function ret = Read(obj, MemberName)
174 
175  try
176  ret = obj.(MemberName);
177  catch
178  error(['EQuUs:', class(obj), ':Read'], ['No property to read with name: ', MemberName]);
179  end
180 
181  end
182 %% Clear
183 %> @brief Clears the value of an attribute in the interface.
184 %> @param MemberName The name of the attribute to be cleared.
185  function Clear(obj, MemberName)
186  try
187  obj.(MemberName) = [];
188  catch
189  error(['EQuUs:', class(obj), ':Clear'], ['No property to clear with name: ', MemberName]);
190  end
191 
192  end
193 
194 
195 
196 
197 end % methods public
198 
199 
200 methods ( Access = protected )
201 
202 %% defNewq
203 %> @brief Defines the new transverse momentums to performs the next iteration.
204 %> @param unaccurateq The list of transverse moments at which the interpolated function fhandle is unaccurate.
205 %> @return Returns with the list of new transverse moments at which the function fhandle should be calculated to improve the accuracy of the interpolation.
206  function newq = defNewq( obj, unaccurateq )
207  % removing qs higher than max(oldq)
208  indexes = logical( unaccurateq > obj.qvec(end) );
209  unaccurateq(indexes) = [];
210 
211  % identifying the intervals in oldq
212  indexes = fix( interp1( obj.qvec, 1:length(obj.qvec), unaccurateq ) );
213  indexes = unique(indexes);
214 
215  % determining the new transverse momentums in the middle of the
216  % previously determined intervals
217  increments = diff( obj.qvec );
218  newq = obj.qvec(indexes) + increments(indexes)/2;
219 
220  end
221 
222 %% getUnaccurateq
223 %> @brief Determine the transverse momentum points at which the interpolated function fhandle is unaccurate.
224 %> @return Returns with the list of unaccurate transverse moments at which the interpolated function fhandle is unaccurate.
225  function unaccurateq = getUnaccurateq( obj )
226  %the raltive tolareance of the accuracy
227  tolerance = 7e-3;
228 
229  unaccurateq = [];
230  maxvalue = max(max(abs(obj.y)));
231 
232  for idx = 1:size(obj.y,2)
233  interpy_new = interp1( obj.qvec, obj.y(:,idx), obj.interpq, obj.interp_method);
234 
235  if isempty( maxvalue )
236  unaccurateq = obj.interpq;
237  return
238  end
239 
240  NaNindexes = isnan( obj.interpy(:,idx) );
241 
242  relative_differences = abs( (interpy_new - obj.interpy(:,idx))/maxvalue );
243  indexes = logical( relative_differences > tolerance );
244  unaccurateq = [unaccurateq;obj.interpq( indexes | NaNindexes )];
245  obj.interpy(:,idx) = interpy_new;
246  end
247 
248  unaccurateq = unique( unaccurateq );
249  obj.display(['Ratio of unaccurate transverse momentums: ', num2str(length(unaccurateq)/length(obj.interpq)*100, '%2.2f' ), '%'], 1);
250  end
251 
252 %% joinq
253 %> @brief Joins the new transverse moment points with the old ones
254 %> @param newq The new transverse momentum points.
255 %> @param newy The values of the function fhandle calculated at points newq.
256  function joinq( obj, newq, newy )
257  obj.qvec = [obj.qvec; newq];
258  obj.y = [obj.y; newy];
259 
260  [obj.qvec,IX] = sort( obj.qvec );
261  obj.y = obj.y(IX,:);
262 
263  end
264 
265 end % methods protected
266 
267 methods ( Access = private )
268 
269 %% InputParsing
270 %> @brief Parses the optional parameters for the class constructor.
271 %> @param varargin Cell array of optional parameters (https://www.mathworks.com/help/matlab/ref/varargin.html):
272 %> @param 'resume' Logical value. If true, the iteration continues with the loaded data, or false (default) otherwise.
273 %> @param 'outFileName' The filename to save the calculated data. If it is empty (default) no data are exported.
274 %> @param 'interp_method' The interpolation method to be used to calculate #interpy. The default method is <a href="http://www.mathworks.com/help/matlab/ref/spline.html">spline</a>.
275  function InputParsing( obj, varargin )
276 
277  p = inputParser;
278  p.addParameter('resume', false);
279  p.addParameter('outFileName', []);
280  p.addParameter('interp_method', 'spline', @ischar);
281 
282  p.parse(varargin{:});
283 
284 
285  obj.resume = p.Results.resume;
286  obj.outFileName = p.Results.outFileName;
287  obj.interp_method = p.Results.interp_method;
288 
289 
290  end
291 
292 
293 
294 end % methods private
295 
296 end
A class containing methodes for displaying several standard messages.
Definition: Messages.m:24
Structure Opt contains the basic computational parameters used in EQuUs.
Definition: structures.m:60
A class providing adaptive distribution of the transverse momentum points.
Definition: adaptiveQ.m:27
function Transport(Energy, B)
Calculates the conductance at a given energy value.
function()
Structure param contains data structures describing the physical parameters of the scattering center ...
Definition: structures.m:45
A class providing interface to load variables from a file.
Definition: LoadFromFile.m:24