2 % Copyright (C) 2009-2015 Peter Rakyta, Ph.D.
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.
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.
14 % You should have received a copy of the GNU General Public License
15 % along with
this program. If not, see http:
17 %> @addtogroup basic Basic Functionalities
20 %> @brief A
class providing adaptive distribution of the transverse momentum points.
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.)
25 %> EQuUs v4.6 or later
29 properties ( Access =
protected )
30 %> The number of elements in qvec in the first iteration.
32 %> The list of transverse momentums where the
function fhandle is desired to be calculated.
34 %> The interpolated values of fhandle over the elements of interpq.
36 %> The dynamically created vector of the transverse momentums.
38 %> The
function values of fhandle calculated over the elements of qvec.
40 %> Maximum of the iterations.
42 %> Logical value. If
true, the iteration cintinues with the loaded data, or
false (
default) otherwise.
44 %> The filename to export the calculated data.
46 %> Method of the interpolation (see http:
52 methods ( Access =
public )
55 %> @brief Constructor of the
class.
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 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
70 obj.interp_method = [];
72 obj.InputParsing( varargin{:} )
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 )
80 obj.display(
'*************************************')
81 obj.display(
'Starting adaptive iterations for transverse momentum computations.', 1 );
82 obj.display(
'*************************************')
84 if obj.resume && ~isempty(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();
94 obj.display( 'Failed to load data. Restarting calculations.')
98 if isempty( obj.interpy )
99 obj.initialize( fhandle )
102 for idx = 1:obj.maxiter
104 % break the iteration if convergence is reched
105 unaccurateq = obj.getUnaccurateq( );
106 if isempty(unaccurateq)
107 obj.display('convergence reached!!!!!!!!!!');
111 newq = obj.defNewq( unaccurateq );
112 newy = feval( fhandle, newq );
113 obj.joinq( newq, newy );
116 obj.display( '*************************************', 1)
117 obj.display( ['Iteration ', num2str(idx), ' finished.'], 1 );
118 obj.display( '*************************************', 1)
119 obj.display( ' ', 1);
121 if ~isempty( obj.outFileName )
124 interpy = obj.interpy;
125 interpq = obj.interpq;
126 maxiter = obj.maxiter;
128 start_qnum = obj.start_qnum;
129 save( obj.outFileName, 'qvec', 'y', 'interpy', 'interpq', 'maxiter', 'resume', 'start_qnum');
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 )
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);
147 obj.y = feval( fhandle, obj.qvec );
148 obj.interpy = NaN(length( obj.interpq), size(obj.y,2) );
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,:);
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)
163 obj.(MemberName) = input;
165 error([
'EQuUs:',
class(obj),
':Read'], [
'No property to write with name: ', MemberName]);
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)
176 ret = obj.(MemberName);
178 error([
'EQuUs:',
class(obj),
':Read'], [
'No property to read with name: ', MemberName]);
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)
187 obj.(MemberName) = [];
189 error([
'EQuUs:',
class(obj),
':Clear'], [
'No property to clear with name: ', MemberName]);
200 methods ( Access =
protected )
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) = [];
211 % identifying the intervals in oldq
212 indexes = fix( interp1( obj.qvec, 1:length(obj.qvec), unaccurateq ) );
213 indexes = unique(indexes);
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;
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
230 maxvalue = max(max(abs(obj.y)));
232 for idx = 1:size(obj.y,2)
233 interpy_new = interp1( obj.qvec, obj.y(:,idx), obj.interpq, obj.interp_method);
235 if isempty( maxvalue )
236 unaccurateq = obj.interpq;
240 NaNindexes = isnan( obj.interpy(:,idx) );
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;
248 unaccurateq = unique( unaccurateq );
249 obj.display(['Ratio of unaccurate transverse momentums: ', num2str(length(unaccurateq)/length(obj.interpq)*100, '%2.2f' ), '%'], 1);
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];
260 [obj.qvec,IX] = sort( obj.qvec );
265 end % methods protected
267 methods ( Access = private )
270 %> @brief Parses the optional parameters for the class constructor.
271 %> @
param varargin Cell array of optional parameters (https:
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 )
278 p.addParameter(
'resume',
false);
279 p.addParameter(
'outFileName', []);
280 p.addParameter(
'interp_method',
'spline', @ischar);
282 p.parse(varargin{:});
285 obj.resume = p.Results.resume;
286 obj.outFileName = p.Results.outFileName;
287 obj.interp_method = p.Results.interp_method;
294 end % methods
private A class containing methodes for displaying several standard messages.
Structure Opt contains the basic computational parameters used in EQuUs.
A class providing adaptive distribution of the transverse momentum points.
function Transport(Energy, B)
creating the Ribbon class representing the twoterminal setup
Structure param contains data structures describing the physical parameters of the scattering center ...
A class providing interface to load variables from a file.