<?xml version="1.0" encoding="UTF-8"?>

<rss version="2.0"
     xmlns:content="http://purl.org/rss/1.0/modules/content/"
     xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
     xmlns:atom="http://www.w3.org/2005/Atom"
     xmlns:dc="http://purl.org/dc/elements/1.1/"
     xmlns:wfw="http://wellformedweb.org/CommentAPI/"
     >
  <channel>
    <atom:link href="http://kitchingroup.cheme.cmu.edu/blog/feed/index.xml" rel="self" type="application/rss+xml" />
    <title>The Kitchin Research Group</title>
    <link>https://kitchingroup.cheme.cmu.edu/blog</link>
    <description>Chemical Engineering at Carnegie Mellon University</description>
    <pubDate>Sat, 01 Nov 2025 13:47:46 GMT</pubDate>
    <generator>Blogofile</generator>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    
    <item>
      <title>Literate programming example with Fortran and org-mode</title>
      <link>https://kitchingroup.cheme.cmu.edu/blog/2014/02/04/Literate-programming-example-with-Fortran-and-org-mode</link>
      <pubDate>Tue, 04 Feb 2014 10:22:00 EST</pubDate>
      <category><![CDATA[org-mode]]></category>
      <category><![CDATA[literate-programming]]></category>
      <guid isPermaLink="false">OrewfEby-EkWfWYRcNHp2BNoFwg=</guid>
      <description>Literate programming example with Fortran and org-mode</description>
      <content:encoded><![CDATA[


&lt;div id="table-of-contents"&gt;
&lt;h2&gt;Table of Contents&lt;/h2&gt;
&lt;div id="text-table-of-contents"&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#sec-1"&gt;1. A slightly more complicated example.&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#sec-2"&gt;2. Summary key points&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
Update: see a short video of how this post works here: &lt;a href="/media/2014-02-04-Literate-programming-example-with-Fortran-and-org-mode/literate-programming.swf"&gt;video&lt;/a&gt; 
&lt;/p&gt;

&lt;p&gt;
I want to illustrate the literate programming capabilities of org-mode. One idea in literate programming is to have code in blocks surrounded by explanatory text. There is a process called "tangling", which extracts the code, and possibly compiles and runs it. I have typically used python and emacs-lisp in org-mode, but today we look at using Fortran. 
&lt;/p&gt;

&lt;p&gt;
The first simple example is a hello world fortran program. Typically you create a file containing code like this:
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-fortran"&gt;&lt;span style="color: #8b0000;"&gt;PROGRAM&lt;/span&gt; &lt;span style="color: #8b2323;"&gt;hello&lt;/span&gt;

&lt;span style="color: #8b0000;"&gt;PRINT&lt;/span&gt; *, &lt;span style="color: #228b22;"&gt;"Hello world"&lt;/span&gt;

&lt;span style="color: #8b0000;"&gt;END&lt;/span&gt; &lt;span style="color: #8b0000;"&gt;PROGRAM&lt;/span&gt; &lt;span style="color: #8b2323;"&gt;hello&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
That file can be named something like hello.f90. We specify that in the source block header like this:
&lt;/p&gt;

&lt;pre class="example"&gt;
:tangle hello.f90
&lt;/pre&gt;

&lt;p&gt;
There are a variety of ways to build your program. Let us create a makefile to do it. We will specify that this block is tangled to a Makefile like this:
&lt;/p&gt;
&lt;pre class="example"&gt;
:tangle Makefile
&lt;/pre&gt;

&lt;p&gt;
Our Makefile will have three targets: 
&lt;/p&gt;
&lt;ol class="org-ol"&gt;
&lt;li&gt;hello, which compiles our program to an executable called a.out. 
&lt;/li&gt;
&lt;li&gt;execute, which depends on hello, and runs the executable
&lt;/li&gt;
&lt;li&gt;clean, which deletes a.out if it exists
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-makefile"&gt;&lt;span style="color: #8b2323;"&gt;hello&lt;/span&gt;:  hello.f90
        gfortran hello.f90

&lt;span style="color: #8b2323;"&gt;execute&lt;/span&gt;: hello
        ./a.out

&lt;span style="color: #8b2323;"&gt;clean&lt;/span&gt;:
        rm -f a.out *.o
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
Now, we can run &lt;pre&gt;elisp:(org-babel-tangle)&lt;/pre&gt; , which will extract these files to the current directory. Here is evidence that the files exist.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-sh"&gt;ls
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
hello.f90
literate.org
Makefile
&lt;/pre&gt;

&lt;p&gt;
Let us go a step further, and use the makefile to execute our program. The first time you run this, you will see that the 
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-sh"&gt;make clean execute
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
rm -f a.out *.o
gfortran hello.f90
./a.out
 Hello world
&lt;/pre&gt;

&lt;p&gt;
That works well! The only really inconvenient issue is that if you update the Fortran code above, you have to manually rerun  &lt;pre&gt;elisp:(org-babel-tangle)&lt;/pre&gt; , then run the &lt;code&gt;make execute&lt;/code&gt; command. We can combine that in a single block, where you do both things at once. 
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-emacs-lisp"&gt;(org-babel-tangle)
(shell-command-to-string &lt;span style="color: #228b22;"&gt;"make clean execute"&lt;/span&gt;)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
rm -f a.out *.o
gfortran hello.f90
./a.out
 Hello world
&lt;/pre&gt;

&lt;p&gt;
That is it in a nutshell. We had a source block for a Fortran program, and a source block for the Makefile. After tangling the org-file, we have those files available for us to use. Next, let us consider a little more complicated example.
&lt;/p&gt;

&lt;div id="outline-container-sec-1" class="outline-2"&gt;
&lt;h2 id="sec-1"&gt;&lt;span class="section-number-2"&gt;1&lt;/span&gt; A slightly more complicated example.&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-1"&gt;
&lt;p&gt;
Now, let us consider a Fortran code with two files. We will define a module file, and a program file. The module file contains a function to compute the area of a circle as a function of its radius. Here is our module file, which is tangled to circle.f90.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-fortran"&gt;MODULE Circle
      &lt;span style="color: #4682b4;"&gt;implicit None&lt;/span&gt;
      public :: area
&lt;span style="color: #ff0000; font-weight: bold;"&gt;c&lt;/span&gt;&lt;span style="color: #ff0000; font-weight: bold;"&gt;ontains&lt;/span&gt;
      &lt;span style="color: #8b0000;"&gt;function&lt;/span&gt; &lt;span style="color: #8b2323;"&gt;area&lt;/span&gt;(r) 
      &lt;span style="color: #4682b4;"&gt;implicit none&lt;/span&gt;
      &lt;span style="color: #4682b4;"&gt;real&lt;/span&gt;, intent(in) :: r
      &lt;span style="color: #4682b4;"&gt;real&lt;/span&gt; :: area
      area = 3.14159 * r**2
      &lt;span style="color: #8b0000;"&gt;return&lt;/span&gt;
      &lt;span style="color: #8b0000;"&gt;end&lt;/span&gt; &lt;span style="color: #8b0000;"&gt;function&lt;/span&gt; &lt;span style="color: #8b2323;"&gt;area&lt;/span&gt;
&lt;span style="color: #8b0000;"&gt;END&lt;/span&gt; MODULE Circle
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
Now, we write a program that will print a table of circle areas. Here we hard-code an array of 5 radius values, then loop through the values and get the area of the circle with that radius. We will print some output that generates an org-mode &lt;a href="http://orgmode.org/manual/Tables.htm"&gt;table&lt;/a&gt; . In this program, we &lt;code&gt;use&lt;/code&gt; our module defined above.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-fortran"&gt;&lt;span style="color: #8b0000;"&gt;program&lt;/span&gt; &lt;span style="color: #8b2323;"&gt;main&lt;/span&gt;
      
use circle, only: area

&lt;span style="color: #4682b4;"&gt;implicit none&lt;/span&gt;
&lt;span style="color: #4682b4;"&gt;integer&lt;/span&gt; :: i
&lt;span style="color: #4682b4;"&gt;REAL&lt;/span&gt;, &lt;span style="color: #4682b4;"&gt;DIMENSION&lt;/span&gt;(&lt;span style="color: #8b008b;"&gt;5&lt;/span&gt;) :: R
R = (/1.0, 2.0, 3.0, 4.0, 5.0 /)

&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; *, &lt;span style="color: #228b22;"&gt;"#+tblname: circle-area"&lt;/span&gt;
&lt;span style="color: #ff0000; font-weight: bold;"&gt;d&lt;/span&gt;&lt;span style="color: #ff0000; font-weight: bold;"&gt;o i = 1, 5&lt;/span&gt;
  &lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; *, &lt;span style="color: #228b22;"&gt;"|"&lt;/span&gt;, R(i), &lt;span style="color: #228b22;"&gt;"|"&lt;/span&gt;, area(R(i)), &lt;span style="color: #228b22;"&gt;"|"&lt;/span&gt;
&lt;span style="color: #8b0000;"&gt;end&lt;/span&gt; &lt;span style="color: #8b0000;"&gt;do&lt;/span&gt;

&lt;span style="color: #8b0000;"&gt;end&lt;/span&gt; &lt;span style="color: #8b0000;"&gt;program&lt;/span&gt; &lt;span style="color: #8b2323;"&gt;main&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
Now, we make a makefile that will build this program. I use a different name for the file, since we already have a Makefile in this directory from the last example.  I also put @ at the front of each command in the makefile to suppress it from being echoed when we run it. Later, we will use the makefile to compile the program, and then run it, and we only want the output of the program. 
&lt;/p&gt;

&lt;p&gt;
The compiling instructions are more complex. We have to compile the circle module first, and then the main program. Here is our makefile. 
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-makefile"&gt;&lt;span style="color: #8b2323;"&gt;circle&lt;/span&gt;:
        &lt;span style="color: #4682b4;"&gt;@&lt;/span&gt;gfortran -c circle.f90

&lt;span style="color: #8b2323;"&gt;main&lt;/span&gt;: circle
        &lt;span style="color: #4682b4;"&gt;@&lt;/span&gt;gfortran -c main.f90
        &lt;span style="color: #4682b4;"&gt;@&lt;/span&gt;gfortran circle.o main.o -o main

&lt;span style="color: #8b2323;"&gt;clean&lt;/span&gt;:
        &lt;span style="color: #4682b4;"&gt;@&lt;/span&gt;rm -f *.o main
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
Now, we run this block, which tangles out our new files.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-emacs-lisp"&gt;(org-babel-tangle)
&lt;/pre&gt;
&lt;/div&gt;

&lt;table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides"&gt;


&lt;colgroup&gt;
&lt;col  class="left" /&gt;

&lt;col  class="left" /&gt;

&lt;col  class="left" /&gt;

&lt;col  class="left" /&gt;

&lt;col  class="left" /&gt;
&lt;/colgroup&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td class="left"&gt;main.f90&lt;/td&gt;
&lt;td class="left"&gt;circle.f90&lt;/td&gt;
&lt;td class="left"&gt;hello.f90&lt;/td&gt;
&lt;td class="left"&gt;makefile-main&lt;/td&gt;
&lt;td class="left"&gt;Makefile&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;
Note that results above show we have tangled &lt;i&gt;all&lt;/i&gt; the source blocks in this file. You can limit the scope of tangling, by narrowing to a subtree, but that is beyond our aim for today.
&lt;/p&gt;

&lt;p&gt;
Finally, we are ready to build our program. We specify the new makefile with the &lt;code&gt;-f&lt;/code&gt; option to make. We use the clean target to get rid of old results, and then the main target with builds the program. Since main depends on circle, the circle file is compiled first. 
&lt;/p&gt;

&lt;p&gt;
Note in this block I use this header:
&lt;/p&gt;
&lt;pre class="example"&gt;
#+BEGIN_SRC sh :results raw
&lt;/pre&gt;

&lt;p&gt;
That will tell the block to output the results directly in the buffer. I have the fortran code prename the table, and put | around the entries, so this entry is output directly as an org table. 
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-sh"&gt;make -f makefile-main clean main
./main
&lt;/pre&gt;
&lt;/div&gt;

&lt;table id="circle-area" border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides"&gt;


&lt;colgroup&gt;
&lt;col  class="right" /&gt;

&lt;col  class="right" /&gt;
&lt;/colgroup&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td class="right"&gt;1.000000&lt;/td&gt;
&lt;td class="right"&gt;3.141590&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td class="right"&gt;2.000000&lt;/td&gt;
&lt;td class="right"&gt;12.56636&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td class="right"&gt;3.000000&lt;/td&gt;
&lt;td class="right"&gt;28.27431&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td class="right"&gt;4.000000&lt;/td&gt;
&lt;td class="right"&gt;50.26544&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td class="right"&gt;5.000000&lt;/td&gt;
&lt;td class="right"&gt;78.53975&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;



&lt;p&gt;
It takes some skill getting used to using &lt;code&gt;:results raw&lt;/code&gt;. The results are not replaced if you run the code again. That can be inconvenient if you print a very large table, which you must manually delete. 
&lt;/p&gt;


&lt;p&gt;
Now that we have a named org table, I can use that table as data in other source blocks, e.g. here in python. You define variables in the header name by referring to the tblname like this.
&lt;/p&gt;

&lt;pre class="example"&gt;
#+BEGIN_SRC python :var data=circle-area
&lt;/pre&gt;

&lt;p&gt;
Then, data is available as a variable in your code. Let us try it and plot the area vs. radius here. For more fun, we will make the plot &lt;a href="http://xkcd.com/"&gt;xkcd&lt;/a&gt; , so it looks like I sketched it by hand.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-python"&gt;&lt;span style="color: #8b0000;"&gt;import&lt;/span&gt; numpy &lt;span style="color: #8b0000;"&gt;as&lt;/span&gt; np
&lt;span style="color: #8b0000;"&gt;import&lt;/span&gt; matplotlib.pyplot &lt;span style="color: #8b0000;"&gt;as&lt;/span&gt; plt
plt.xkcd()

&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; data &lt;span style="color: #ff0000; font-weight: bold;"&gt;# &lt;/span&gt;&lt;span style="color: #ff0000; font-weight: bold;"&gt;data is a list &lt;/span&gt;
&lt;span style="color: #8b008b;"&gt;data&lt;/span&gt; = np.array(data)
plt.plot(data[:, 0], data[:, 1])
plt.xlabel(&lt;span style="color: #228b22;"&gt;'radius'&lt;/span&gt;)
plt.ylabel(&lt;span style="color: #228b22;"&gt;'area'&lt;/span&gt;)
plt.savefig(&lt;span style="color: #228b22;"&gt;'circle-area.png'&lt;/span&gt;)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
[[1.0, 3.14159], [2.0, 12.56636], [3.0, 28.27431], [4.0, 50.26544], [5.0, 78.53975]]
&lt;/pre&gt;


&lt;div class="figure"&gt;
&lt;p&gt;&lt;img src="/media/2014-02-04-Literate-programming-example-with-Fortran-and-org-mode/circle-area.png"&gt; 
&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;
It appears the area increases quadratically with radius. No surprise there! For fun, let us show that. If we divide each area by \(r^2\), we should get back &amp;pi;. Let us do this in emacs-lisp, just to illustrate how flexibly we can switch between languages. In lisp, the data structure will be a list of items like ((radius1 area1) (radius2 area2)&amp;#x2026;). So, we just map a function that divides the area (the second element of an entry) by the square of the first element. My lisp-fu is only average, so I use the &lt;code&gt;nth&lt;/code&gt; function to get those elements. We also load the calc library to get the math-pow function.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #8b0000;"&gt;require&lt;/span&gt; '&lt;span style="color: #cd0000;"&gt;calc&lt;/span&gt;)
(mapcar (&lt;span style="color: #8b0000;"&gt;lambda&lt;/span&gt; (x) (/ (nth 1 x) (math-pow (nth 0 x) 2))) data)
&lt;/pre&gt;
&lt;/div&gt;

&lt;table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides"&gt;


&lt;colgroup&gt;
&lt;col  class="right" /&gt;

&lt;col  class="right" /&gt;

&lt;col  class="right" /&gt;

&lt;col  class="right" /&gt;

&lt;col  class="right" /&gt;
&lt;/colgroup&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td class="right"&gt;3.14159&lt;/td&gt;
&lt;td class="right"&gt;3.14159&lt;/td&gt;
&lt;td class="right"&gt;3.14159&lt;/td&gt;
&lt;td class="right"&gt;3.14159&lt;/td&gt;
&lt;td class="right"&gt;3.14159&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;


&lt;p&gt;
Indeed, we get &amp;pi; for each element, which shows in fact that the area does increase quadratically with radius.
&lt;/p&gt;

&lt;p&gt;
You can learn more about tangling source code from org-mode here &lt;a href="http://orgmode.org/manual/Extracting-source-code.html"&gt;http://orgmode.org/manual/Extracting-source-code.html&lt;/a&gt; .
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-sec-2" class="outline-2"&gt;
&lt;h2 id="sec-2"&gt;&lt;span class="section-number-2"&gt;2&lt;/span&gt; Summary key points&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-2"&gt;
&lt;ol class="org-ol"&gt;
&lt;li&gt;You can organize source files in org-mode as source blocks which can be "tangled" to "real" source code.
&lt;/li&gt;
&lt;li&gt;You can build into your org-file(s) even the Makefile, or other building instructions.
&lt;/li&gt;
&lt;li&gt;You can even run the build program, and the resulting programs from org-mode to capture data.
&lt;/li&gt;
&lt;li&gt;Once that data is in org-mode, you can reuse it in other source blocks, including other languages. 
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;
What benefits could there be for this? One is you work in org-mode, which allows you to structure a document in different ways than code does. You can use headings to make the hierarchy you want. You can put links in places that allow you to easily navigate the document. Second, you can build in the whole workflow into your document, from building to execution. Third, you could have a self-contained file that extracts what someone else needs, but which has documentation and explanation built into it, &lt;i&gt;which you wrote as you developed the program&lt;/i&gt;, rather than as an afterthought. You can still edit each block in its native emacs-mode, and have org-mode too. That is something like having cake, &lt;i&gt;and eating it too&lt;/i&gt;!
&lt;/p&gt;

&lt;p&gt;
Downsides? Probably. Most IDE/project type environments are designed for code. These tools offer nice navigation between functions and files. I don't use those tools, but I imagine if you are hooked on them, you might have to learn something new this way.
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Copyright (C) 2014 by John Kitchin. See the &lt;a href="/copying.html"&gt;License&lt;/a&gt; for information about copying.&lt;p&gt;&lt;p&gt;&lt;a href="/org/2014/02/04/Literate-programming-example-with-Fortran-and-org-mode.org"&gt;org-mode source&lt;/a&gt;&lt;p&gt;&lt;p&gt;Org-mode version = 8.2.5h&lt;/p&gt;]]></content:encoded>
    </item>
  </channel>
</rss>
