Automatic generation of a C header from Fortran code?

October 31st, 2009

Dear Lazyweb,

In my newest project I need to use Fortran routines and variables in C. From the source code point of view this requires only to declare the needed Fortran entities in your C code whilst taking the “right” types of the variables and the name mangling of the Fortran compiler into account.

Here is an example. Consider you have the following Fortran code:

module X
  real, dimension(0:1) :: v
 
  subroutine Foo(A, b, c, &
    & D)
    integer, intent(in) :: a, &
      & B
    real, intent(out) :: C, d
    ! <Foo's body>
  end subroutine
end module X

To use the array v and the subroutine Foo() in C, you need to add (something like) the following declarations to your C source code:

extern float x_mp_v_[2];
 
void x_mp_foo_(int* a, int* b, float* c, float* d);

If only a handful of Fortran routines are needed to be called from C, declaring them by hand may be feasible. This is however error-prone and becomes impracticable for more than a handful of routines. Especially if the routine signatures change frequently.

So what I’d like to have is a tool that generates a C header file containing all declarations of Fortran entities from a given Fortran source file. Or if such tool does not exist, a tool that extracts variable declarations and routine signatures from a Fortran source file into a format that is easier to parse than Fortran itself would also be helpful. Any suggestions?

SLHA shell interface: slha-io.sh

September 15th, 2009

I’m still working with SLHA files and noticed that people are using C/C++, Fortran, or also Mathematica programs for batch manipulation and processing of SLHA files (for example for repeatedly increasing some parameters in a SLHA file, feeding this file to some program and reading its output). For someone experienced with the shell (no, not that shell) this sounds like an easy task doable in a few lines of shell code. There is however no simple way to manipulate SLHA files on the command line.
The script I’m presenting in this post, slha-io.sh, tries to close that gap. Its description is:

This script is a very simple (and stupid) tool to get or set data in SLHA (SUSY Les Houches Accord) files. It can output complete blocks or individual data lines and replace existing data lines with user given input. It is stupid because it does not check after replacing data lines if the resultant file conforms to the SLHA. So care must be taken when using the “set” action.

But as so often, it is best described by examples. The basic things you can do with slha-io.sh are (MSSM.in is the SLHA file in these examples):

$ slha-io.sh MSSM.in get modsel
Block MODSEL            # Select model
 1    0                 # MSSM
 4    1                 # R-parity is violated
Block SMINPUTS          # Standard Model inputs

$ slha-io.sh MSSM.in get sminputs 6
 6   1.729000E+02       # m_top(pole)

$ slha-io.sh MSSM.in get sminputs 6 | awk '{ print $2 }'
1.729000E+02

$ slha-io.sh MSSM.in set sminputs 6 \
  " 6   1.730000E+02       # m_top(pole)"
$ slha-io.sh MSSM.in get sminputs 6
 6   1.730000E+02       # m_top(pole)

Here is another example of using slha-io.sh to create a batch of files where a parameter is constantly increased from 70 to 80 in steps of 0.5:

for NUM in `seq -f "%.8E" 70 0.5 80`; do
    FILE = "m0_$NUM.in"
    cp MSSM.in "$FILE"
    slha-io.sh "$FILE" set minpar 1 " 1  $NUM  # m0"
done

Maybe this script is useful for one or two!

By the way, more sophisticated tools for SLHA I/O are SLHALib and SLHAio.

Vim syntax highlighting for SUSY Les Houches Accord (SLHA)

August 9th, 2009

In my diploma thesis I’m working with supersymmetry spectrum calculation programs (primarily SPheno) which use the SUSY Les Houches Accord (SLHA), defined in arXiv:hep-ph/0311123 and arXiv:0801.0045 [hep-ph], for data input and output. To ease editing input files and reading output files with my favourite text editor Vim I wrote a corresponding syntax file for the Accord. It is really helpful for preventing spelling errors of block names (which are partially cryptic) and for navigating and extracting numerical data.

To enable syntax highlighting in Vim for SLHA download the two files slha.vim and scripts.vim. Copy the slha.vim into ~/.vim/syntax/ and copy scripts.vim into ~/.vim/ or if you have already a scripts.vim file append the content of my file to it. Without further ado Vim should now highlight SLHA files. Here is the compulsory screenshot:

vim_slha

If you have further suggestions or found an error in the scripts, please don’t hesitate to contact me. I’ll try to keep the scripts up to date here.

Update 08.08.2010: The syntax file now includes the block names of the Flavour Les Houches Accord (FLHA, arXiv:1008.0762 [hep-ph]).

Bankenkrise für Endverbraucher

February 15th, 2009

Am Freitagabend hatte ich mein ganz persönliches Erlebnis mit der derzeitigen Bankenkrise:

Geldautomat im abgesicherten Modus starten

Geldautomat im abgesicherten Modus starten


Ganz schön mutig (oder ziemlich dumm?) von mir, dass ich dieser Bank mein Geld anvertraue.

Übrigens: Gratulation an das Debian-Projekt zur Veröffentlichung von Debian 5.0 alias Lenny.

Noch mehr Kettenbrüche

November 23rd, 2008

Zweite Behauptung des Tages: Für jede natürliche Zahl n gilt

\left[n; \overline{1, 2 n} \right] = \sqrt{n(n+2)} \ .

Der Beweis dieser Behauptung ist einfach, wenn man \sqrt{n(n+2)} = n\sqrt{1+2/n} als Kettenbruch entwickelt und dabei benutzt, dass \lfloor \sqrt{1+2/n} \rfloor = 1 und \lfloor n \sqrt{1+2/n} \rfloor = n sind. Die erste Relation gilt offensichtlich, da das Argument der Gaußklammer immer kleiner gleich \sqrt{3} und größer als 1 ist. Für die zweite Relation betrachtet man die Ungleichungen  n\sqrt{1+2/n} \ge n + 1 und n\sqrt{1+2/n} < n und sieht, dass beide zu Widersprüchen führen, die obige Relation also korrekt sein muss.

Kettenbrüche von Quadratwurzeln

November 23rd, 2008

Es ist \sqrt{2} = \left[1; \overline{2} \right] sowie \sqrt{3} = \left[1; \overline{1, 2} \right] (also die Darstellung als Kettenbruch von der Quadratwurzel aus 2 bzw. aus 3).

Behauptung: Für alle natürliche Zahlen n gilt

\left[n; \overline{2 n} \right] = \sqrt{n^2 + 1} und \left[n; \overline{n, 2 n} \right] = \sqrt{n^2 + 2} \ .

Beispiele:
\left[4; \overline{8} \right] = \sqrt{17} = \sqrt{4^2 + 1}
\left[3; \overline{3, 6} \right] = \sqrt{11} = \sqrt{3^2 + 2}

b is a power of 2

November 16th, 2008

One of the most interesting books I’ve read in the past years is without doubt Gödel, Escher, Bach by Douglas Hofstadter. There he introduces TNT and asks the reader to translate the statement “b is a power of 2″ into TNT. My proposed solution, which I wrote down two years ago, is

\sim\exists a: \, \langle \sim\exists c: \, a=(c+c) \, \wedge \, \forall d: \, b=(\mathrm{SS}a \cdot d) \rangle \ .

This is based on the following equation:
\{u \cdot n \,|\, u=2k+1 \, : \, k, n \in \mathbb{N}\} = \mathbb{N} \,\backslash\, \{2^n \,|\, n \in \mathbb{N}\}
Which means that every natural number except powers of 2 can be written as a product of an odd natural number u and a natural number n. This can be easily understood. Just take the prime factors of an arbitrary natural number. If one of these prime factors is odd, the number can be written as a product of this odd prime factor and the product of the remaining prime factors (which can be even or odd). If there is no odd prime factor, all prime factors must be even and since 2 is the only even prime number the original number must be a power of 2.

I’m wondering if my solution is correct and how other people have written this statement in TNT.

Die USA haben einen neuen Präsidenten gewählt

November 7th, 2008

Seit Mittwoch dem 5. November 2008 (UTC) ist Barack Obama nun President-elect der Vereinigten Staaten von Amerika. Als designierten Amtsnachfolger W.s kann man ihn aber noch nicht bezeichnen, da es den Bürgern der U.S. nicht erlaubt ist ihren Präsidenten direkt zu wählen. Von den derzeitigen 538 Wahlmännern hat Obama gute 2/3 “gewonnen” (68% Obama, 30% McCain), doch insgesamt gingen nur etwas mehr als die Hälfte der Stimmen aller wahlberechtigten US-Bürger an Obama (53% Obama, 46% McCain). Ich finde es faszinierend, dass die Amerikaner sich diese Diskrepanz zwischen Wählerwille und Electoral College gefallen lassen…

Insgesamt muss ich aber sagen, dass ich froh bin, dass diese Wahl endlich zu Ende gegangen ist. Diese andauernden Nichtnachrichten über Sarah Palins Kopfsprünge in die großen Teiche voller Fett (die von belustigend bis beängstigend das volle emotionale Spektrum abgedeckt haben), oder die hundertste Umfrage, die Kandidaten X oder Y in Staat A oder B wieder vorne sah, waren zum Ende hin doch dann eher nervend. Trotzdem hat diese Wahl einen fantastischen Einblick in die Mediendemokratie der USA erlaubt. Teilweise sah es so aus, als ob Aussagen und Positionen der Kandidaten weniger wichtig waren, als die Summe der Werbespots mit denen Swing States überflutet wurden. Kann man sich in Amerika mit genügend Geld die Präsidentschaft erkaufen? Dieser Wahlkampf hat auch gezeigt, was ich vor einer Bundestagswahl in Deutschland nicht miterleben möchte. Kein Kanzlerkandidat sollte kurz vor de Wahl bei Schmidt & Pocher vorbeischauen, und es sollte auch egal sein in welchen Outfits unsere Politiker auftreten (ich hoffe ja auch mal, dass sowas nicht von Parteigeldern bezahlt werden darf).

Ich bin aber nicht nur froh darüber, dass diese Wahl nun vorbei ist, sondern auch über deren Ausgang. Zuersteinmal ist es hoch anzuerkennen, dass die USA einen Schwarzen zu ihrem Präsidenten gewählt haben. Zwar ist Obama kein Nachfahre von afrikanischstämmigen Sklaven, trotzdem wäre ein zukünftiger afroamerikanische Präsidentschaftskandidat kein Novum mehr, dass Ängste schüren würde. Ähnliches gilt nach Angela Merkel wahrscheinlich auch für zukünftige Kanzlerkandidatinnen in der Bundesrepublik. Aber wir sind noch meilenweit davon entfernt z.B. einen türkischstämmigen Politiker zum Kanzler zu wählen. Andererseits hat diese Wahl vielen jungen Menschen in Europa, die sich für Politik zu interessieren begannen als George W. zum Präsidenten (wieder-) gewählt wurde, ein Stück Hoffnung in die USA wiedergegeben und ihr Ansehen teilweise restauriert. Man kann sehr gespannt sein auf die nächsten Jahre, allerdings sollte man auch nicht zu viel erwarten. Nach Bush kann es aber nur besser werden, oder?

US Vice Presidential Debate 2008

October 3rd, 2008

Playlist der TV-Debatte der US-Vizepräsidentschaftskandidaten:

Bei Teil 4/11 wird es grausam. Auf die Frage: What is true and what is false about causes of climate change? antwortete Palin: [...] I don’t want to argue about the causes. What I want to argue about is how we are going to get there to positively affect the impacts [of climate change].

Mit so einer Einstellung bekommt man bestimmt auf die Idee die heiße Herdplatte mit Eiswürfeln zu kühlen, damit man sich nicht mehr die Finger verbrennt, anstatt den Herd einfach auszuschalten.

Goddies with bts’ “status” and “select”

September 21st, 2008

Since I was watching Don Armstrong’s talk about Debbugs’ SOAP interface I wanted to use devscripts' bts to get a list of boinc‘s top five bug reporter. Finally, this is possible with devscripts (>= 2.10.36):

bts status file:- `bts select source:boinc archive:both` \
  | egrep "^originator" | cut -f2- | sort | uniq -ci | sort -n \
  | tail -n5

 2 Riccardo Stagni <unriccio@email.it>
 3 Martin Michlmayr <tbm@cyrius.com>
 6 Robert Millan <rmh@aybabtu.com>
 6 Thibaut VARENE <varenet@debian.org>
10 Sandro Tosi <matrixhasu@gmail.com>

Sweet – Thanks Debbugs and Devscripts maintainers!
And congratulations Sandro (or thank you – whatever you find appropriate)!

Update:
Lucas Nussbaum recently demonstrated how the Ultimate Debian Database (UDD) can be used to assemble cool stats about Debian bugs. This brought me to translate the above shell code into an SQL query for UDD. To retrieve boinc‘s top five bug reporter from UDD, one would use a query like this:

udd=> select submitter, count(*) from (select * from bugs
  where source = 'boinc' union select * from archived_bugs
  where source = 'boinc') as all_boinc_bugs group by submitter
  order by count desc limit 5;
              submitter               | count
--------------------------------------+-------
 Sandro Tosi <matrixhasu@gmail.com>   |     8
 Thibaut VARENE <varenet@debian.org>  |     6
 Robert Millan <rmh@aybabtu.com>      |     6
 Martin Michlmayr <tbm@cyrius.com>    |     3
 Michael Ablassmeier <abi@grinser.de> |     2
(5 rows)

The small difference between the two lists is because UDD does not handle bugs that are assigned to multiple packages correctly yet.