Eötvös Quantum Utilities  v4.8.141
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 = 4.8;
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 
154 
155 %% eig
156 %> @brief Calculates the generalized eigenvalue problem based on the zggev and dggev LAPACK functions.
157 %> @param A A square matrix on the left side.
158 %> @param B A square matrix on the right side.
159 %> @return Returns the calculated generalized eigenvalues and the right and left sided eigenvectors.
160 function [eigenvecs_right, eigenvecs_left, eigvals] = eig( obj, A, B )
161 %TODO check whether LAPACK is deployed
162  % if both the matrices A and B are reals
163  if isreal(A) && isreal(B)
164  % check the presence of the compiled MEX file
165  fncname = 'dggev';
166  [directory, name, ext] = fileparts(which(fncname));
167  if (~strcmpi(name, fncname) || ~strcmpi(ext, ['.' mexext]))
168  warning('CommonFunction:eig', ...
169  'Function dggev is not compiled yet. Please compile first with the MKL component');
170  [eigenvecs_right, eigenvecs_left, eigvals] = eig_MATLAB();
171  return
172  end
173 
174  [alphavarR, alphavarI, betavar, eigenvecs_left, eigenvecs_right] = dggev(A,B);
175  eigenvecs_left = eigenvecs_left';
176  eigvals = (alphavarR + 1i*alphavarI)./betavar;
177  return
178  end
179 
180  if isreal(A)
181  A = complex(A);
182  end
183 
184  if isreal(B)
185  B = complex(B);
186  end
187 
188  % check the presence of the compiled MEX file
189  fncname = 'zggev';
190  [directory, name, ext] = fileparts(which(fncname));
191  if (~strcmpi(name, fncname) || ~strcmpi(ext, ['.' mexext]))
192  warning('CommonFunction:eig', ...
193  'Function zggev is not compiled yet. Please compile first with the MKL component');
194  [eigenvecs_right, eigenvecs_left, eigvals] = eig_MATLAB();
195  return
196  end
197 
198  [alphavar, betavar, eigenvecs_left, eigenvecs_right] = zggev(A,B);
199  eigenvecs_left = eigenvecs_left';
200  eigvals = alphavar./betavar;
201 
202  % nested functions
203 
204  %--------------------------------
205  % Calculates the right and left eigevectors with MATLAB
206  function [eigenvecs_right, eigenvecs_left, eigvals] = eig_MATLAB()
207 
208  [modusmtx_right,k] = eig(A,B);
209  [modusmtx_left,k_left] = eig(transpose(A),transpose(B));
210  modusmtx_left = transpose(modusmtx_left);
211  modusmtx_left_tmp = zeros(size(modusmtx_left));
212 
213  k = diag(k);
214  k_left = diag(k_left);
215 
216  for idx = 1:length(k)
217  [~,jdx] = min( abs(k_left -k(idx)));
218  modusmtx_left_tmp(idx,:) = modusmtx_left(jdx,:);
219  end
220 
221  eigenvecs_right = modusmtx_right;
222  eigenvecs_left = modusmtx_left_tmp;
223  eigvals = k;
224 
225  end
226 
227  % end nested functions
228 
229 end
230 
231 
232 %% getVersion
233 %> @brief Gets the current version of the package.
234 %> @return Returns the current version of the package.
235 function ret = getVersion( obj )
236  ret = obj.version;
237 end
238 
239 %% getProgramName
240 %> @brief Gets the name of the package.
241 %> @return Returns the name of the package.
242 function ret = getProgramName( obj )
243  ret = obj.ProgramName;
244 end
245 
246 %% getProgramShortName
247 %> @brief Gets the short name of the package.
248 %> @return Returns the short name of the package.
249 function ret = getProgramShortName( obj )
250  ret = obj.ProgramShortName;
251 end
252 
253 
254 
255 end
256 
257 
258 methods (Access = public, Static = true )
259 
260 
261 %> @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
262 %> @param y Function values to be integrated
263 %> @param h Increment between the x_i points.
264  function ret = SimphsonInt(y,h)
265  if ~exist( 'h', 'var' )
266  h = 1;
267  end
268 
269  if mod(length(y),2) == 0
270  warning('length of the vector to integral has to be (2n+1)')
271  y(end) = [];
272  end
273 
274  ya = y(1);
275  yb = y(end);
276  y(1) = [];
277 
278  ret = h/3*( ya + yb ) + 2*h/3*sum(y(2:2:end-2)) + 4*h/3*sum(y(1:2:end-1));
279 
280 
281  end
282 
283 
284 %% inv_SVD
285 %> @brief Inverts badly conditioned matrix A with SVD regularization
286 %> @param A A square matrix to be inverted.
287 %> @return Returns the calculated inverse.
288 function invA = inv_SVD( A )
289  SVDtolerance = 1e-15;
290 
291  if issparse(A)
292  [U,S,V] = svd(full(A));
293  else
294  [U,S,V] = svd(A);
295  end
296 
297  S = diag(S);
298  smax = max(abs(S));
299  indexes = logical( abs(S) < smax*SVDtolerance );
300  S(indexes) = smax*SVDtolerance;
301 
302  S = 1./S;
303 
304  invA = V*diag(S)*U';
305 
306 end
307 
308 %% checkMEXfile
309 %> @brief Checks the presence of a given MEX file.
310 %> @param fncname The name of the function.
311 %> @return Returns true if the MEX file is present, false otherwise.
312 function ret = checkMEXfile( fncname )
313  [directory, name, ext] = fileparts(which(fncname));
314  if (~strcmpi(name, fncname) || ~strcmpi(ext, ['.' mexext]))
315  ret = false;
316  else
317  ret = true;
318  end
319 end
320 
321 
322 %% isOctave
323 %> @brief Checks whether the running environment is matlab or octave.
324 %> @return Returns true if running environment is octave, false otherwise.
325 function retval = isOctave()
326  persistent cacheval; % speeds up repeated calls
327 
328  if isempty (cacheval)
329  cacheval = (exist ('OCTAVE_VERSION', 'builtin') > 0);
330  end
331 
332  retval = cacheval;
333 end
334 
335 
336 
337 
338 end %methods
339 
340 
341 end % Global end
function Transport(Energy, B)
creating the Ribbon class representing the twoterminal setup
A class containing common basic functionalities.
Structure param contains data structures describing the physical parameters of the scattering center ...
Definition: structures.m:45