31.12.2013 Views

Numerical Methods in Quantum Mechanics - Dipartimento di Fisica

Numerical Methods in Quantum Mechanics - Dipartimento di Fisica

Numerical Methods in Quantum Mechanics - Dipartimento di Fisica

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

is an important and well-known library for all k<strong>in</strong>ds of l<strong>in</strong>ear algebra operations:<br />

solution of l<strong>in</strong>ear systems, eigenvalue problems, etc. LAPACK calls<br />

BLAS rout<strong>in</strong>es for all CPU-<strong>in</strong>tensive calculations. The latter are available <strong>in</strong><br />

highly optimized form for many <strong>di</strong>fferent architectures.<br />

BLAS and LAPACK rout<strong>in</strong>es are written <strong>in</strong> Fortran 77. BLAS and LA-<br />

PACK are often available <strong>in</strong> many operat<strong>in</strong>g systems and can be l<strong>in</strong>ked <strong>di</strong>rectly<br />

by the compiler by add<strong>in</strong>g -llapack -lblas. In the case of C compiler, it<br />

may be needed to add an underscore ( ) <strong>in</strong> the call<strong>in</strong>g program, as <strong>in</strong>: dsyev ,<br />

dgemm . This is due to <strong>di</strong>fferent C-Fortran conventions for the nam<strong>in</strong>g of “symbols”<br />

(i.e. compiled rout<strong>in</strong>es). Note that the C compiler may also need -lm to<br />

l<strong>in</strong>k general mathematical libraries (i.e. operations like the square root).<br />

0.1.4 Pitfalls <strong>in</strong> C-Fortran <strong>in</strong>terlanguage calls<br />

In ad<strong>di</strong>tion to the above-mentioned potential mismatches between C and Fortran<br />

nam<strong>in</strong>g conventions, there are a few more pitfalls one has to be aware of<br />

when Fortran 77 rout<strong>in</strong>es are called by C (or vice versa).<br />

• Fortran passes po<strong>in</strong>ters to subrout<strong>in</strong>es and functions; C passes values. In<br />

order to call a Fortran rout<strong>in</strong>e from C, all C variables appear<strong>in</strong>g <strong>in</strong> the<br />

call must be either po<strong>in</strong>ters or arrays.<br />

• In<strong>di</strong>ces of vectors and arrays start from 0 <strong>in</strong> C, from 1 <strong>in</strong> Fortran (unless<br />

<strong>di</strong>fferently specified <strong>in</strong> array declaration or allocation).<br />

• Matrices <strong>in</strong> C are stored <strong>in</strong> memory row-wise, that is: a[i][j+1] follows<br />

a[i][j] <strong>in</strong> memory. In Fortran, they are stored column-wise (the other<br />

way round!): a(i+1,j) follows a(i,j) <strong>in</strong> memory.<br />

An ad<strong>di</strong>tional problem is that C does not provide run-time allocatable matrices<br />

like Fortran does, but only fixed-<strong>di</strong>mension matrices and arrays of po<strong>in</strong>ters.<br />

The former are impractical, the latter are not usable as arguments to pass to<br />

Fortran. It would be possible, us<strong>in</strong>g either non-standard C syntax, or us<strong>in</strong>g<br />

C++ and the new command, to def<strong>in</strong>e dynamically allocated matrices similar<br />

to those used <strong>in</strong> Fortran. We have preferred for our simple C codes to “simulate”<br />

Fortran-style matrices (i.e. stored <strong>in</strong> memory column-wise) by mapp<strong>in</strong>g them<br />

onto one-<strong>di</strong>mensional C vectors.<br />

We remark that Fortran 90 has a more advanced way of pass<strong>in</strong>g arrays to<br />

subrout<strong>in</strong>es us<strong>in</strong>g “array descriptors”. The codes used <strong>in</strong> this course however do<br />

not make use of this possibility but use the old-style Fortran 77 way of pass<strong>in</strong>g<br />

arrays via po<strong>in</strong>ters.<br />

3

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!