0

I'm currently working on a computational physics code in Fortran. In summary, the code performs the following operations:

  • Initialize
  • Loop until done
    • Advance solution over time
    • Possibly write output

We want to support different physics modules, which solve different equations. I am thinking about the following design, which heavily relies on function pointers:

  • Generate a template for a physics module, which contains definitions for the procedures that could be required, and stores pointers to these procedures. For example, there would be routines for initialization, time stepping and producing special output.
  • Each physics module then defines its own procedures (and some data, which can conveniently be stored in a Fortran module)
  • At the start of the code, one module is activated, which sets the template's pointers to the module's procedures

The motivation behind the above approach is that changing modules only requires a single change in the code (activate a different one), and that we keep the code relatively simple (no complex abstractions, inheritance and other object-oriented features).

Would this be considered a good design for a non object-oriented code?

2
  • A suggestion: Before you go too far down this road, it might be worthwhile to consider carefully the decision to write this in Fortran in 2016. In the late 1980s, one of my previous employers did a study, writing a 6DOF simulation in PASCAL rather than FORTRAN, to see what the costs and benefits might be. They discovered that the improvements in productivity, readability, and maintainability VASTLY outweighed the few percent loss is raw speed. Last I heard, they'd sworn an oath never to write another simulation in FORTRAN. PASCAL was That Much Better. There are other options. Commented Oct 20, 2016 at 17:56
  • 1
    A study from the late 1980s has little to do with modern Fortran, which is a great language for scientific computing! Commented Oct 20, 2016 at 18:29

1 Answer 1

1

What you are describing falls under the general heading of Dependency Injection, otherwise known as Inversion of Control. This is an exceptionally powerful technique widely used in the C-derived languages but unfortunately less so in Fortran.

The general idea is to write modules so that they share common interfaces and are otherwise ignorant of each other, and then to assemble a running program by gluing together a chosen set of such modules. A common framework for handling configuration parameters and/or a configuration language (such as XML) may also be used. Sometimes the modules are built as plug-ins or dynamic libraries.

Your plan seems to put too much of the decision making down at module level, where it is better at the higher level. The specific usage of templates and pointers might be just one approach, but would rather depend on the dialect chosen. I would recommend some reading about IOC before going too much further down that track.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.