Manuel Alfonseca, Enrique Alfonseca, Juan de Lara
Acknowledgement: This paper has been sponsored by the Spanish Interdepartmental Commission of Science and Technology (CICYT), project number TIC-96-0723-C02-01.
This paper describes the procedure used to build several compilers, written in APL and APL2, to translate two continuous simulation languages into APL and C++. The advantages and disadvantages of using APL to write a compiler are discussed. A compromise had to be found between performance (the model execution speed) and flexibility (the ease to modify parameters and test "what if" situations). The resulting compiler (an APL2 packaged workspace) has been used successfully to generate educational applications and in medical research.
System simulation [1] is one of the oldest branches of computer science. It was well advanced in the sixties, and came to maturity in the seventies. Its objective is to build a "model" of a real system, i.e. a system that behaves in a similar way as the real system, but is easier to study and experiment with. If the model is a set of mathematical equations, it is called a "mathematical model". If it is a computer program, we have a "computer model". There are three kinds of computer simulation:
Continuous simulation is programmed either in a special purpose language, or in general purpose code. Continuous simulation languages may be of different kinds, depending on their syntax:
In the late nineteen seventies, one of the authors of this paper implemented in APL a compiler for a subset of the CSMP continuous simulation language [6]. This compiler was subsequently announced by IBM as three different products running under APL interpreters in the mainframe computers of the time [7].
The CSMP language has the following features:
The APL CSMP compiler has the following features and advantages:
The main disadvantage of the APL approach is the execution speed of the simulation models. Their translation into APL implies that they must be executed under an interpreter, with the subsequent performance degradation. On the other hand, continuous simulation models are typical examples of programs that contain loops that cannot be eliminated by means of matrix operations: as simulated time goes by, the values of all the variables in the model are updated as a function of the preceding values.
Listing 1 shows a model written in CSMP, that solves the well-known Volterra equations [8] for a two-species system.
TITLE VOLTERRA EQUATIONS DATA M:=5,N:=.3,P:=.5,Q:=.3 DATA X0:=2 Y0:=20 XP:=(N*X*Y)-M*X YP:=(P*Y)-Q*X*Y X:=INTGRL(X0,XP) Y:=INTGRL(Y0,YP) TIMER delta:=0.01,FINTIM:=10,PRdelta:=0.1 PLOT 20 50 X Y TIME METHOD ADAMS Listing 1. A model of an ecological system written in CSMP
Object-oriented programming originated in the sixties in a discrete simulation language, SIMULA67 [9], which incorporated many of the ideas later included by Alan Kay in the first general purpose object-oriented language, Smalltalk [10], and by Bjarne Stroustrup in C++ [11]. Object-oriented continuous simulation languages and tools, however, took longer to arrive.
We have used the object-oriented concepts to design a new simulation language (OOCSMP), a pure extension of CSMP, with some features that help to simplify the models of systems with several equivalent interacting components. The simplification obtained may be very significant. The main object-oriented extensions added to the language are:
Additional extensions allow us to:
Listing 2 shows an example of a model written in OOCSMP, which makes use of its object-oriented extensions to simulate a multi-level multi-species ecological system.
TITLE EXTENDED VOLTERRA EQUATIONS
*************************************
* Definition of the Species class *
*************************************
CLASS Species {
NAME name
DATA M, X0, start
X:=STEP(start)*LIMIT(0,1000,XT)
XT:=INTGRL(X0,XP)
XP:=M*X
PLOT X TIME
}
*************************************
* Definition of the Plant class *
*************************************
CLASS Plant : Species {
DATA AHPl
XP-= AHPl*X*Herbivore.X
}
*************************************
* Definition of the Herbivore class *
*************************************
CLASS Herbivore : Species {
DATA APlH, APrH
XP+= APlH*X*Plant.X
XP-= APrH*X*Predator.X
}
*************************************
* Definition of the Predator class *
*************************************
CLASS Predator : Species {
DATA AHPr
XP+= AHPr*X*Herbivore.X
}
*************************************
* Actual species *
*************************************
* Level 0: plant
Plant PA ("Plant", 0.4, 100, 0, 0.02)
* Level 1: herbivore
Herbivore HA ("Herb1",-1.28, 20, 0, 0.02, 0.36)
Herbivore HB ("Herb2",-1.28, 5, 10, 0.02, 0.12)
* Level 2: predator
Predator PrA("Pred1",-7.2, 2, 0, 0.36)
Predator PrB("Pred2",-4, 2, 10, 0.20)
STEP(PA)
STEP(HA)
STEP(HB)
STEP(PrA)
STEP(PrB)
*************************************
* Timer and show data *
*************************************
TIMER delta:=0.005,FINTIM:=40,PRdelta:=0.1,PLdelta:=0.1
METHOD ADAMS
Listing 2. An extended model of the ecological system written in OOCSMP
A new compiler has been written in APL2 to translate OOCSMP code. To overcome the performance problem, it was decided to generate C++ code instead of APL. This means that something had to be done to maintain at least part of the flexibility of the previous simulation environment.
The OOCSMP compiler reuses a large part of the code of the previous CSMP compiler written in APL, including the following sections:
Of course, since the target language is different, the code generator had to be completely replaced. However, the effort needed to build the new compiler (with a total of about 1200 APL2 lines) was surprisingly small: It took one person about three weeks, in spite of the fact that the old compiler (which is made of about 900 APL lines) had been written seventeen years before (by the same person) and was sparingly commented. We think that using APL and APL2 made this programming performance possible. In a typical systems language, the effort required would have been much larger.
To maintain the interactive simulation environment provided by the previous compiler for the APL translated models, the new compiler provides the option to generate a main program written in C++ that makes it possible to:
The main program provides a graphic interface built by means of calls to
a C++ library designed and written by one of the authors, that implements a
DOS message-based window system with multitask, similar to higher level
operating subsystems. Figure 1 shows the appearance of the graphic interface
during the execution of the ecological system represented by the model in
listing 2.
Figure 1: The graphic interface during the execution of the model of an
ecological system
If this option is not used, The APL2 compiler generates only the simulation routines, which may be invoked from any hand-written main program. In this way, automatic parameter adjustment or boundary problems may be solved, although in a slightly less simple way that that provided by the old APL compiler, as the main program must be written in C++.
The main advantage of the new compiler, compared with the old one, is the execution speed of the simulation models, which is about one order of magnitude faster, more than balancing the slightly lesser flexibility of use.
The APL2 written OOCSMP compiler has been built as a packaged workspace that may be invoked from a DOS session, either natively, under Windows 95 or OS/2. Another compiler for OOCSMP built by our group [12] generates optionally C++ and JAVA code. In the first case, we can use the same graphics library described above to generate a simulation environment for DOS, or the Amulet graphics interface developed by the Carnegie-Mellon University [13] under Windows 95 and UNIX. In the JAVA case, the standard JAVA user interfaces are used. The compiler also generates an html skeleton embedding the JAVA applets.
These compilers have been used to translate models written for educational purposes. Two of these models are available in the Internet at the following addresses:
The APL2 compiler has also been used in postgraduate courses on continuous simulation, and for medical research, to simulate hepatitis B and AIDS epidemics in Spain [14].
The experience of writing in APL a compiler for a special purpose programming language (a continuous simulation language) was useful for the following reasons:
Using this experience, we wrote a second compiler in APL2 with the following results: