Eötvös Quantum Utilities  v4.9.146
Providing the Horsepowers in the Quantum Realm
CommonFunctions.m
Go to the documentation of this file.
1 %% Eotvos Quantum Transport Utilities - CommonFunctions
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 CommonFunctions.m
20 %> @brief A class containing common basic functionalities.
21 %> @}
22 %> @brief A class containing common basic functionalities.
23 %%
25 
26 
27 properties (Access=protected)
28 %> True if MKL component is built, false otherwise.
29  DeployedMKL
30 %> The current version of the package.
31  version = 'v4.9.146';
32 %> Name of the package.
33  ProgramName = 'Eotvos Quantum Transport Utilities';
34 %> Short name of the package.
35  ProgramShortName = 'EQuUs';
36 %> Maximal size of full matrixes to be handled
37  MaxSize = 1e4;
38 end
39 
40 
41 methods
42 
43 
44 %% xmlread
45 %> @brief Function to load XML files (based on xerces libraries), providing octave compatibility.
46 %> @param filename Absolute path to the file to be opened. (In matlab relative path is sufficient)
47 %> @return The loaded document object model (see http://www.mathworks.com/help/matlab/ref/xmlread.html#outputarg_DOMnode for details.)
48 function tree = xmlread( obj, filename )
49 
50  if obj.isOctave()
51  try
52  parser = javaObject('org.apache.xerces.parsers.DOMParser');
53  catch err
54  error ('xmlread: could not load Xerces parser object, you may have to add required .jar files to your javapath. See the documentation for further details')
55  end
56 
57  parser.parse(filename);
58  tree = parser.getDocument;
59  else
60  tree = xmlread(filename);
61  end
62 
63 end
64 
65 
66 %% xmlwrite
67 %> @brief Function to export XML files (based on xerces libraries), providing octave compatibility.
68 %> @param filename Absolute path to the file to be opened. (In matlab relative path is sufficient)
69 %> @param DOM The loaded document object model (see http://www.mathworks.com/help/matlab/ref/xmlread.html#outputarg_DOMnode for details.)
70 function xmlwrite( obj, filename, DOM )
71 
72  if obj.isOctave()
73  try
74  jfile = javaObject ('java.io.File', filename);
75  jos = javaObject ('java.io.FileOutputStream', jfile);
76  jfmt = javaObject ('org.apache.xml.serialize.OutputFormat', DOM);
77  catch err
78  error ('xmlwrite: could not load Xerces serializer object, you may have to add required .jar files to your javapath. See the documentation for further details')
79  end
80 
81  jfmt.setIndenting (1);
82  serializer = javaObject ('org.apache.xml.serialize.XMLSerializer', ...
83  jos, jfmt);
84  serializer.serialize (DOM);
85 
86  if (nargout > 0 && strcmp (class (jos), 'java.io.StringWriter'))
87  str = char (jos.toString ());
88  end
89 
90  else
91  xmlwrite(filename, DOM);
92  end
93 
94 end
95 
96 
97 
98 
99 %% IsDeployedMKL
100 %> @brief Checks whether the MKL component is deployed
101 %> @return Returns with true if MKL component is deployed, false otherwise.
102 function ret = IsDeployedMKL( obj )
103 
104  if isempty(obj.DeployedMKL)
105  obj.DeployedMKL = obj.checkMEXfile( 'dgetPartialInv' ) & ...
106  obj.checkMEXfile( 'zgetPartialInv' );
107  end
108 
109  ret = obj.DeployedMKL;
110 
111 end
112 
113 
114 
115 %% partialInv
116 %> @brief Calculates a partial inverse of a sparse matrix. If MKL component is not developed, the backslash operator is used insted.
117 %> @param A A sparse matrix to be inverted
118 %> @param sizeInv The size of partial inverse to be calculated.
119 %> @return Returns the calculated partial inverse
120 function invA = partialInv( obj, A, sizeInv )
121 
122  if ~issparse(A)
123  if ( size(A,1) > obj.MaxSize )
124  error('EQuUs:CommonFunctions:partialInv', ['Matrix A has to be sparse or a full matrix smaller than ', num2str(obj.MaxSize), 'x', num2str(obj.MaxSize)]);
125  end
126  end
127 
128  if (~obj.IsDeployedMKL()) && issparse(A)
129  evec = sparse( size(A,1)-sizeInv+1:size(A,1), 1:sizeInv, 1, size(A,1), sizeInv);
130  invA = full(A \ evec);
131  invA = invA( size(A,1)-sizeInv+1:end, :);
132  elseif ~issparse(A)
133  evec = zeros( size(A,1), sizeInv);
134  evec( end-sizeInv+1:end, : ) = eye(sizeInv);
135  invA = full(A \ evec);
136  invA = invA( size(A,1)-sizeInv+1:end, :);
137  else
138  try
139  if isreal(A)
140  invA = dgetPartialInv(A, sizeInv);
141  else
142  invA = zgetPartialInv(A, sizeInv);
143  end
144  catch errCause
145  save('matrix.mat', 'A');
146  error(errCause)
147  end
148  end
149 
150 end
151 
152 
153 %% diagInv
154 %> @brief Calculates the diagonal part of the inverse of a sparse matrix. If MKL component is advised to build.
155 %> @param A A sparse matrix to be inverted
156 %> @return Returns the diagonal elements of the inverse
157 function invA = diagInv( obj, A )
158 
159  if ~issparse(A) && ( size(A,1) > obj.MaxSize )
160  error('EQuUs:CommonFunctions:diagInv', ['Matrix A has to be sparse or smaller full matrix than ', num2str(obj.MaxSize), 'x', num2str(obj.MaxSize)]);
161  end
162 
163  if ~obj.IsDeployedMKL() && (~issparse(A))
164  invA = diag(inv(A));
165  else
166 
167  try
168  if isreal(A)
169  invA = dgetDiagInv(A);
170  else
171  invA = zgetDiagInv(A);
172  end
173  catch errCause
174  save('matrix.mat', 'A');
175  throw(errCause)
176  end
177  end
178 
179 end
180 
181 
182 
183 %% eig
184 %> @brief Calculates the generalized eigenvalue problem based on the zggev and dggev LAPACK functions.
185 %> @param A A square matrix on the left side.
186 %> @param B A square matrix on the right side.
187 %> @return Returns the calculated generalized eigenvalues and the right and left sided eigenvectors.
188 function [eigenvecs_right, eigenvecs_left, eigvals] = eig( obj, A, B )
189 %TODO check whether LAPACK is deployed
190  % if both the matrices A and B are reals
191  if isreal(A) && isreal(B)
192  % check the presence of the compiled MEX file
193  fncname = 'dggev';
194  [directory, name, ext] = fileparts(which(fncname));
195  if (~strcmpi(name, fncname) || ~strcmpi(ext, ['.' mexext]))
196  warning('CommonFunction:eig', ...
197  'Function dggev is not compiled yet. Please compile first with the MKL component');
198  [eigenvecs_right, eigenvecs_left, eigvals] = eig_MATLAB();
199  return
200  end
201 
202  [alphavarR, alphavarI, betavar, eigenvecs_left, eigenvecs_right] = dggev(A,B);
203  eigenvecs_left = eigenvecs_left';
204  eigvals = (alphavarR + 1i*alphavarI)./betavar;
205  return
206  end
207 
208  if isreal(A)
209  A = complex(A);
210  end
211 
212  if isreal(B)
213  B = complex(B);
214  end
215 
216  % check the presence of the compiled MEX file
217  fncname = 'zggev';
218  [directory, name, ext] = fileparts(which(fncname));
219  if (~strcmpi(name, fncname) || ~strcmpi(ext, ['.' mexext]))
220  warning('CommonFunction:eig', ...
221  'Function zggev is not compiled yet. Please compile first with the MKL component');
222  [eigenvecs_right, eigenvecs_left, eigvals] = eig_MATLAB();
223  return
224  end
225 
226  [alphavar, betavar, eigenvecs_left, eigenvecs_right] = zggev(A,B);
227  eigenvecs_left = eigenvecs_left';
228  eigvals = alphavar./betavar;
229 
230  % nested functions
231 
232  %--------------------------------
233  % Calculates the right and left eigevectors with MATLAB
234  function [eigenvecs_right, eigenvecs_left, eigvals] = eig_MATLAB()
235 
236  [modusmtx_right,k] = eig(A,B);
237  [modusmtx_left,k_left] = eig(transpose(A),transpose(B));
238  modusmtx_left = transpose(modusmtx_left);
239  modusmtx_left_tmp = zeros(size(modusmtx_left));
240 
241  k = diag(k);
242  k_left = diag(k_left);
243 
244  for idx = 1:length(k)
245  [~,jdx] = min( abs(k_left -k(idx)));
246  modusmtx_left_tmp(idx,:) = modusmtx_left(jdx,:);
247  end
248 
249  eigenvecs_right = modusmtx_right;
250  eigenvecs_left = modusmtx_left_tmp;
251  eigvals = k;
252 
253  end
254 
255  % end nested functions
256 
257 end
258 
259 
260 %% getVersion
261 %> @brief Gets the current version of the package.
262 %> @return Returns the current version of the package.
263 function ret = getVersion( obj )
264  ret = obj.version;
265 end
266 
267 %% getProgramName
268 %> @brief Gets the name of the package.
269 %> @return Returns the name of the package.
270 function ret = getProgramName( obj )
271  ret = obj.ProgramName;
272 end
273 
274 %% getProgramShortName
275 %> @brief Gets the short name of the package.
276 %> @return Returns the short name of the package.
277 function ret = getProgramShortName( obj )
278  ret = obj.ProgramShortName;
279 end
280 
281 
282 
283 end
284 
285 
286 methods (Access = public, Static = true )
287 
288 
289 %> @brief Simphson integral: y is a vector of function values at equal distant points: y_i = f(x_i), x_i-x_{i-1}=h
290 %> @param y Function values to be integrated
291 %> @param h Increment between the x_i points.
292  function ret = SimphsonInt(y,h)
293  if ~exist( 'h', 'var' )
294  h = 1;
295  end
296 
297  if mod(length(y),2) == 0
298  warning('length of the vector to integral has to be (2n+1)')
299  y(end) = [];
300  end
301 
302  ya = y(1);
303  yb = y(end);
304  y(1) = [];
305 
306  ret = h/3*( ya + yb ) + 2*h/3*sum(y(2:2:end-2)) + 4*h/3*sum(y(1:2:end-1));
307 
308 
309  end
310 
311 
312 %% inv_SVD
313 %> @brief Inverts badly conditioned matrix A with SVD regularization
314 %> @param A A square matrix to be inverted.
315 %> @return Returns the calculated inverse.
316 function invA = inv_SVD( A )
317  SVDtolerance = 1e-15;
318 
319  if issparse(A)
320  [U,S,V] = svd(full(A));
321  else
322  [U,S,V] = svd(A);
323  end
324 
325  S = diag(S);
326  smax = max(abs(S));
327  indexes = logical( abs(S) < smax*SVDtolerance );
328  S(indexes) = smax*SVDtolerance;
329 
330  S = 1./S;
331 
332  invA = V*diag(S)*U';
333 
334 end
335 
336 %% checkMEXfile
337 %> @brief Checks the presence of a given MEX file.
338 %> @param fncname The name of the function.
339 %> @return Returns true if the MEX file is present, false otherwise.
340 function ret = checkMEXfile( fncname )
341  [directory, name, ext] = fileparts(which(fncname));
342  if (~strcmpi(name, fncname) || ~strcmpi(ext, ['.' mexext]))
343  ret = false;
344  else
345  ret = true;
346  end
347 end
348 
349 
350 %% isOctave
351 %> @brief Checks whether the running environment is matlab or octave.
352 %> @return Returns true if running environment is octave, false otherwise.
353 function retval = isOctave()
354  persistent cacheval; % speeds up repeated calls
355 
356  if isempty (cacheval)
357  cacheval = (exist ('OCTAVE_VERSION', 'builtin') > 0);
358  end
359 
360  retval = cacheval;
361 end
362 
363 
364 
365 
366 end %methods
367 
368 
369 end % Global end
function Transport(Energy, B)
Calculates the conductance at a given energy value.
A class containing common basic functionalities.
function()
Structure param contains data structures describing the physical parameters of the scattering center ...
Definition: structures.m:45