Object-oriented design patterns in Fortran 90/95: mazev1, mazev2 and mazev3

https://doi.org/10.1016/j.cpc.2007.11.013Get rights and content

Abstract

This paper discusses the concept, application, and usefulness of software design patterns for scientific programming in Fortran 90/95. An example from the discipline of object-oriented design patterns, that of a game based on navigation through a maze, is used to describe how some important patterns can be implemented in Fortran 90/95 and how the progressive introduction of design patterns can usefully restructure Fortran software as it evolves. This example is complemented by a discussion of how design patterns have been used in a real-life simulation of Particle-in-Cell plasma physics. The following patterns are mentioned in this paper: Factory, Strategy, Template, Abstract Factory and Facade.

Program summary

Program title: mazev1, mazev2, mazev3

Catalogue identifier: AEAI_v1_0

Program summary URL: http://cpc.cs.qub.ac.uk/summaries/AEAI_v1_0.html

Program obtainable from: CPC Program Library, Queen's University, Belfast, N. Ireland

Licensing provisions: Standard CPC licence, http://cpc.cs.qub.ac.uk/licence/licence.html

No. of lines in distributed program, including test data, etc.: 1958

No. of bytes in distributed program, including test data, etc.: 17 100

Distribution format: tar.gz

Programming language: Fortran 95

Computer: PC/Mac

Operating system: Unix/Linux/Mac (FreeBSD)/Windows (Cygwin)

RAM: These are interactive programs with small (KB) memory requirements

Classification: 6.5, 20

Nature of problem: A sequence of programs which demonstrate the use of object oriented design patterns for the restructuring of Fortran 90/95 software. The programs implement a simple maze game similar to that described in [1].

Solution method: Restructuring uses versions of the Template, Strategy and Factory design patterns.

Running time: Interactive.

References:

  • [1] 

    E. Gamma, R. Helm, R. Johnson, J. Vlissides, Design Patterns: Elements of Reusable Object Oriented Software, Addison-Wesley, 1995, ISBN 0201633612.

Introduction

As computers become more powerful, there is a growing desire to make scientific codes increasingly complex. In the computer science literature, researchers have discovered that similar solutions to programming complex problems have appeared in different contexts, and they have called such solutions design patterns. The seminal work by Gamma, Helm, Johnson, and Vlissides [1], identified 23 recurring design patterns. These patterns are intended to be language independent, and, indeed, a number of texts have appeared discussing their application to C++, Java, and other languages (see, for example, [2], [3]). But the treatment of design patterns in Fortran 90/95 is quite lacking by comparison to these languages.

Fortran 90/95 is a non-object-oriented language but it contains a language construct, the module, which can be used to emulate “classes”. The resulting “object-based” style of programming has been recommended by some authors [4], [5], [6], [7], [8] as a way of enhancing the encapsulation and reuse of Fortran 90/95 code “components”.

In the following two sections we review the basic concepts behind object-based programming in Fortran 90/95 and introduce the running example of the maze game. This example is used to illustrate the implementation of three important design patterns, Template, Strategy and Factory, in Sections 4 The template and strategy patterns:, 5 A factory pattern:. The way in which a design patterns approach has been used to construct a framework for Particle-in-Cell simulation is described in Section 6 and the paper concludes with Section 7.

Section snippets

Classes in Fortran 90/95

In the following sections we will illustrate how some object-oriented (OO) design patterns can be usefully emulated in Fortran 90/95. As a vehicle for this discussion, we will use a running example which has been adapted from [1]. This is a game program which creates a two-dimensional maze through which a player needs to navigate in order to find an exit. The various versions of the software that we discuss will be available through the CPC software archive.

In our discussions, we will refer to

The maze game: mazev1

In this section we present our simple programming example as if it were a larger software development project.

Imagine that you set out to write a Fortran 90/95 program for a maze game. Good software engineering practice would dictate that you would start this exercise by defining the requirements of your system, that you would follow this phase by an analysis of these requirements before moving on to design and, subsequently, implement and test your software. Standard software process

The template and strategy patterns: mazev2

With all of the attention to encapsulation and information hiding described in the previous section, our mazev1 program represents good software engineering practice for the implementation of a single maze game. Design patterns will now be introduced for the first time to enable the program structure to be generalized to support different maze games.

Suppose we wanted to have several options for an algorithm such as the game logic of the maze game. The Template design pattern is used to

A factory pattern: mazev3

We now consider a case where the maze game is further extended to have rooms in the maze with different properties. Specifically, we will introduce a TreasureRoom_class module which will contain a number of treasures which must be collected by entering each room. A treasure room is a room with additional properties and in object-oriented languages this relationship would be modeled by inheritance.

Factory patterns encapsulate the creation of objects within an inheritance hierarchy. In the case

Design patterns in a Particle-in-Cell simulation framework

In this section, we briefly describe how the design patterns introduced above have been applied in a living scientific simulation code. The application is a framework for Particle-in-Cell (PIC) plasma simulation. PIC plasma codes integrate the self-consistent equations of motion of a large number of charged particles in electromagnetic fields. Their basic structure is to calculate the density of charge, and possibly current, on a fixed grid (by calling “deposit” procedures). Maxwell's

Conclusions

In this paper we have discussed how some object-oriented design patterns can usefully be incorporated into software written in Fortran 90/95. The design patterns that we have mentioned, Factory, Strategy, Template, Abstract Factory and Facade, are all concerned with the encapsulation and reuse of subsystems of Fortran 90/95 programs. If there is one message from this paper, it is that Fortran 90/95 programmers can profitably direct their design focus to encapsulation at the subsystem level. As

Acknowledgements

The authors express their appreciation for a reviewer's recommendation that the maze game would make a good study for illustrating design patterns in Fortran 90/95. They also greatly appreciate the work done by Joseph Antony on the maze game simulation software.

Viktor Decyk's work was performed, in part, at the Jet Propulsion Laboratory, California Institute of Technology, under a contract with NASA. Decyk was also partly supported by the US Department of Energy, under the SCIDAC program.

Henry

References (13)

  • V.K. Decyk et al.

    Computer Physics Communications

    (1998)
  • E. Gamma et al.

    Design Patterns: Elements of Reusable Object Oriented Software

    (1995)
  • A. Shalloway et al.

    Design Patterns Explained

    (2002)
  • S.J. Metsker

    Design Patterns Java Workbook

    (2002)
  • V.K. Decyk et al.

    Scientific Programming

    (1997)
  • M.G. Gray et al.

    Computers in Physics

    (1997)
There are more references available in the full text version of this article.

Cited by (12)

  • The vmf90 program for the numerical resolution of the Vlasov equation for mean-field systems

    2014, Computer Physics Communications
    Citation Excerpt :

    Fortran 90 provides, with respect to older revisions of the Fortran programming language, many improvements for the structured coding of scientific programs. The reader is referred to Refs. [19–21] for more information on this topic. In addition to these ideas, vmf90 takes profit of revision control with the git software [22] and extensive in-code documentation.

  • An experimental approach to the performance penalty of the use of classes in Fortran 95

    2011, Advances in Engineering Software
    Citation Excerpt :

    Additional information on the use of Fortran 95 for OOP in general can be found in e.g. [12,1,21,17]. Information on how to use design patterns in Fortran can be found in [9]. In the object oriented paradigm, the problem that is to be solved by a program is represented by “objects”.

View all citing articles on Scopus

This paper and its associated computer program are available via the Computer Physics Communications homepage on ScienceDirect (http://www.sciencedirect.com/science/journal/00104655).

View full text