Software diagramming for Fortran code

I’m working with a rather large codebase of Fortran code and I’m looking into diagramming and documenting it.

So far I’ve looked into UML class diagrams, but that doesn’t seem like the best fit for Fortran code because it seems centered around heavily object oriented designs that involve lots of inheritance, for example. The code I’m working with is set up closer to vanilla C, where data types are combined into structs, but the methods are not type-bound procedures and there’s little to no polymorphism.

I’m wondering if anyone has any advice about how to visualize and document these kinds of projects.

1 Like

Do you know FORD?

1 Like

UML also defines object diagrams (i.e. what objects will actually exist at runtime) as opposed to class diagrams (i.e. how are the classes defined).

UML also defines sequence diagrams, i.e. what calls to what procedures occur in what sequence. They’re usually described in terms of objects invoking methods, but they don’t have to be.

Finally, you can always result to good old flow diagrams.

I use doxygen for this sort of thing. Your milage may vary.

I did not. I’ll check it out. Thanks!

1 Like

At one time there was a Java application called ForUML that could read modern Fortran source (i.e., OO F03, F08?) and generate UML diagrams. Last I looked, the official URL for it had gone dead. Someone captured some/all of the software on github, but it hasn’t been maintained so has suffered bit-rot and no longer works (out-of-the-box, at least for me).

Here’s a recent publication from the original author. There he indicates it is available “upon request”. Earlier it had been freely downloadable.

If anyone has more up-to-date info about ForUML please chime in; I found it to be a useful tool.

1 Like

It would also be cool to have a graphical UML editor that generates Fortran code!

In KDE, there is GitHub - KDE/umbrello: GUI for diagramming Unified Modelling Language (UML) which can export to many languages, but not Fortran:
umbrello/umbrello/codegenerators at master · KDE/umbrello · GitHub

You can use Mermaid - a JavaScript based diagramming and charting tool - to produce UML diagrams. Mermaid allows you to define charts and diagrams in a Markdown-like syntax. I’ve also had my eyes on the book Creating Software with Modern Diagramming Techniques: Build Better Software with Mermaid by Ashley Peacock published by The Pragmatic Programmers for a while now.

For instance, the Fortran type

module rockets
implicit none

type :: rocket
  private
  real :: weight
contains
  procedure :: launch
end type

contains

  subroutine launch(this)
    class(rocket), intent(inout) :: this
    write(*,'("Launching rocket weighing ", G0, " tons")') this%weight
  end subroutine

end module

could be represented as follows in Mermaid:

classDiagram

class rocket {
    +launch() void
    -real weight
}

For more examples have a look at the documentation on classDiagram. You can try out Mermaid in the live editor.

It would be nice if someone put together basic Fortran examples demonstrating the eight different kinds of UML relationships (see table below). Alternatively, I’d appreciate anyone who knows of a good resource which explains the different relationships.

Type Description
<|-- Inheritance
*-- Composition
o-- Aggregation
--> Association
-- Link (Solid)
..> Dependency
..|> Realization
.. Link (Dashed)

With sufficient time (and motivation) you could try using the capability of LFortran, to export the abstract semantic representation using lfortran --show-asr --json and then write your own plugin which reads the JSON file and generates the Mermaid markdown.

As a larger example, here is a diagram I created with Mermaid manually (took c. 1 hour) to understand the relation between the different entities in a code I was refactoring:

3 Likes

Nice!
And Mermaid can be used in GitHub wikis.

As @Carltoffel , I would also recommend FORD (with graphviz). It allows for consistent documentation and the diagrams are for free! :slight_smile:
You can find an (unfinished) example here All Modules – pbepack (hugomvale.github.io)

2 Likes

Hello.
I am currently working on a tool to scan software codebases.
Name is “marauder’s map”( Studying the geography of a software · The COOP Blog)

Still WIP but if your project is available on Gitlab or Github, I would be glad to take a look.
Best regards

4 Likes

PlanUML seems to offer functionalities similar to Mermaid.

1 Like

I’ve been using PlantUML occasionally for a while now, and had good experiences. That said, the ability to write it directly in Markdown is quite tempting to switch to Mermaid.

2 Likes

I’m a big fan of FORD as well, but in my experience graphs generated by such tools are not very helpful for understanding how the code works. They tend to quickly become a garbled mess of boxes and lines (c.f. the link posted by @HugoMVale). In my view you need hand-crafted graphs in order to control the amount of information that’s displayed and possibly add supplemental information.

In the past I played around with embedding PlantUML sequence diagrams in FORD pages with links to the generated pages for procedures and modules. However, that did not work out-of-the-box; see my issue on github.

More recently I’ve been looking into the C4 approach with structurizr. I like that because you can make interactive click-and-zoom type of gaphs. However, it’s not really meant for going down to the source code level.