Debian Codegraph @

ode/INTERNALS

raw
1 1. Language Implementation Details
2
3 The ode input language is implemented with a bison-generated parser and a
4 flex-generated lexical analyzer. The benefits of this approach are great.
5 The original version of the basic recognizer was completed in about two
6 days. And adding comments to the language required that exactly one line
7 of the lexical analyzer source code be changed.
8
9 Differential equations are compiled into linked lists of stack operations,
10 one list per dynamic variable. The function evaluator (eval) traverses one
11 of these lists, applying the indicated operations to a small operand stack.
12 At the completion of the traverse, the new derivative is on top of the
13 stack. The routine pops and returns this result. Another routine (field)
14 is called by the numerical routines whenever a re-evaluation of the
15 gradient is required. It calls eval() for each dynamic variable in turn.
16
17 Changes to the language involve modifications to the bison grammar, the
18 flex rules, and/or the semantics stored with the grammar and rules. The
19 steps required to add a new builtin function, for example, are (aside from
20 writing the function itself) as follows.
21
22 (1) Invent a new terminal symbol name for the reserved word.
23
24 (2) Add the new reserved word to the flex rules.
25
26 (3) Add the new productions to the right-hand side of
27 "expr" in the grammar, together with whatever semantic
28 action is required (using the included macros if possible).
29
30 (4) Define a new value for exper and add the stack code to eval.
31
32 2. Numerical Schemes
33
34 The numerical and language procedures were designed in accordance with the
35 Jesuit maxim. The major elements manipulated by both are the symbol table,
36 and the functions field() and printq(). Addition of a new numerical scheme
37 requires that the numerical routine be written, and perhaps that some
38 fields inside the symbol table entry be expanded or added. None of these
39 changes affects the performance or compromise the integrity of the parser,
40 although they may change the space-cost of a dynamic variable. Similarly,
41 modifications to the lexical analyzer or the grammar only affect the parse
42 tables and related data structures, and have no effect on the numerical
43 routines.
44
45 At each step the numerical scheme fills each symbol table entry with
46 relevant values. Then a call to field() evaluates every differential
47 equation and puts the result in the symbol table element syrime. The
48 evaluator gets current values for the dynamics from the field syalue. Both
49 syalue and syrime are in some sense temporary variables. All correct
50 results eventually find their way into syal[0] and syri[0]; these are never
51 touched by the language processor. Thus the real interface between the
52 language and numerical procedures is merely syalue and syrime. It is
53 important to maintain meaningful values in syalue and syrime, since a
54 floating-point exception may disrupt the control flow at any moment. It is
55 recommended that the pointer fsp be used in traversing the symbol table, so
56 that arithmetic exceptions can be diagnosed as accurately as possible.
57
58 Fourth and fifth order schemes were chosen as the best compromise between
59 speed and accuracy. The adaptive stepsize has proved a boon in both areas.
60
61 3. Details of Error and Fault Recovery
62
63 Command-line errors are diagnosed at initialization time and are generally
64 fatal.
65
66 Errors in the input stream are diagnosed during the lexical scan or at
67 parse time. They elicit an appropriate message. The error recovery method
68 used in the parser is the vanilla Yacc scheme. (see S. Johnson, "Yacc: Yet
69 Another Compiler-Compiler", 1978, pp. 16-18).
70
71 Run-time errors are detected by the function evaluator, the numerical
72 routines, or the floating-point unit hardware. In each case a message is
73 printed describing the error, and ode returns to a known state awaiting
74 input. The current step is abandoned, but all variable values are left
75 reflecting the state of the machine just prior to the fault. All of the
76 numerical schemes scan the symbol table, computing values for each dynamic
77 variable, using a common pointer known to the error recovery routines named
78 fsp. This pointer provides a way of identifying the offending dynamic
79 variable when a trap occurs.
80
81 4. Space Utilization
82
83 The data space used in numerically solving a system of ODEs consists of
84 both fixed and dynamic regions. The fixed space is used efficiently
85 regardless of the complexity of the problem being solved. The dynamic
86 space contains the symbol table, the expression lists, and several other
87 data structures whose sizes vary. The space for these structures comes out
88 of a common pool; they play against each other.
89
90 Every identifier in an ODE description requires 254 bytes; this space can
91 never be reclaimed. Each operator in an equation costs 14 bytes. For the
92 sake of speed, some operators are expanded into a sequence of simpler
93 operations, each of which requires 14 bytes. In particular, some common
94 powers are reduced to sequences of square roots, squares, cubes, and
95 inversions. The extent of these transformations can be seen with the help
96 of the `examine' statement. The operator space for an equation is
97 reclaimed whenever a new equation is defined for that particular dynamic
98 variable. Each lexical token in the most complex statement costs 10 bytes,
99 but this space is reclaimed during the scan. Thus, the most complex
100 statement circumscribes this requirement. Each element in a `print'
101 statement costs 6 bytes. Upon execution of the next `print' statement this
102 space is reclaimed.
103
104 In addition to these absolute space limitations, the maximum depth of the
105 operand stack used for evaluating derivatives is 30 cells. This limit
106 could conceivably be exceeded for a very complex differential equation.
107 The solution is to simplify the equation if possible, and where not, to
108 recompile ode with a larger operand stack.