Evaluating Analytical Functions (Extracted from Can/Am EMTP News Apr/1997) $INCLUDE files should be capable of mathematical processing of the arguments. This was mentioned briefly in the preceding issue. The subject is being explained now in considerably more detail, and in more general terms. Yes, $INCLUDE is a use of immediate interest. But the subject is much broader: how does the user specify mathematical formulas, and how does ATP respond to those demands? The study of TACS (see preceding two issues), and its reliance upon functional evaluation, is what has made the difference for $INCLUDE now as opposed to years past. Before, it was viewed as a special need. Now, it is viewed as just another use of a general capability (the evaluation of functions). Supplemental variables and devices of TACS are just functions, after all, and GNUPLOT allows the user to represent functions analytically (see preceding issue). So, the consideration of dependent variables within $INCLUDE was viewed this same way : as a function. Yes, TACS supplemental variables could be used for this, with the model here being old CSP (concurrent sequential processing) from SPY of some 12 years ago. But this was a little awkward; a case of overkill. All that really was needed was a simple, self-contained, scientific pocket calculator. If GNUPLOT could do such mathematics, maybe we could borrow that code? When asked, BPA*s Walter Powell volunteered his own code, which comes from BPA*s Power Flow Program. As half a dozen *.f files beginning with TEST_PAR, this was first supplied January 8th, and was found to execute correctly using Salford DBOS as disk file WALTER.FTN An analytical electric network source is one use of the new Powell pocket calculator. The thinking is as follows: Why have a different type code for each different function? This implies a fixed function, for which the user then supplies parameters in fixed data fields. But this is a low level of thinking. Why not instead allow the user to specify his own function of time analytically? Automatically, the Powell source would be free-format, just as supplemental variables of TACS are. The user simply supplies his algebra such as 303 * COS ( OMEGA * T - ALPHA) where OMEGA and ALPHA typically would be constants, but more generally, might themselves be functions of time or other variables. Just as with supplemental variables of TACS, one function after another would be evaluated, with the latest value of each variable used at any time. Why use TACS or MODELS for such mathematics? Why not instead supply such functions directly on the electrical side? The new Powell memorial source will do this. A new Type-10 electric-network source first provided the just-described capability on January 29th as reported in E-mail to Mr. Powell. The analytical expression can be keyed anywhere in columns 11-60 with TIMEX (TACS notation, recall) used to indicate any occurrence of simulation time. If more than 50 columns are needed, a following continuation card carrying 30 additional bytes can be requested as illustrated in DCNEW-19. As with Dube*s MODELS, execution is slow for large-scale usage. The first experiment involved 70. * COS ( TIMEX) which required about 2/3rds of a msec per evaluation on your Editor*s 133-MHz Pentium. But this is just the first try, with no memory between uses. Mr. Powell has indicated that he can speed execution significantly for repetitive evaluation of the same structure. Modules SUBR5 and SUBTS3 are the only ones affected. $PARAMETER is a new $-card that precedes an arbitrary number of definitions of symbols that are defined by analytical functions. In fact, this preceded dependent variables of $INCLUDE because the details are much simpler. The same basic concept is involved, however. Why require $INCLUDE for math associated with input data? This is the question your Editor asked himself on January 29th during free time of that week-long wait for permission that never came (see separate story). The idea is as follows: For any data card, any character string can be replaced by the numeric evaluation of an expression. The character string, hereafter referred to as a variable of the usage, is defined using free-format FORTRAN such as : R-DAMP =. 075 * "DELTAT" / CAP444 (inspired by formula 2.39 of BPA*s EMTP Theory Book). Here the variable R-DAMP is 6 columns wide to fit in cols. 27-32 of a series R-L-C branch card, DELTAT is an ATP variable name (the time-step size), and CAP444 might itself be a symbol or a constant. Note that quotation marks are used to enclose program variables such as DELTAT. Walter Powell*s pocket calculator will evaluate this variable R-DAMP once, and then will search for it, and replace it by the associated number, wherever it is found. This is the concept, and it is powerful. Logic is confined to new SUBROUTINE MATDAT. See the new test case DCNEW-19 for an illustration. SPY APPEND has offered low-level mathematics for the past dozen years as illustrated by INCLSPY5.DAT (a parametric study). The new Powell pocket calculator could replace the existing low-level mathematics by high-level analytical expressions. For years, TACS supplemental variables or MODELS were considered as a replacement, but now thinking has changed. Why bother with such complexity when the Powell pocket calculator will suffice? No replacement for TACS arithmetic seems likely, however. In the absence of changes, the Powell pocket calculator is too slow for heavy-duty use within the time-step loop. The separate story about TACS and MODELS reported that TACS was slow by a factor of 33.8 for a simple benchmark. Well, MATHWALT.FTN is the corresponding test of the Powell pocket calculator, and no loop of 100 is required for accurate timing. For a single execution of 10K steps, just over 15 seconds were required. Evaluating Analytical Functions , II (Extracted from Can/Am EMTP News Oct/1998) The pocket calculator from BPA's Walter Powell first was mentioned in the April, 1997, issue. Several uses were considered at the time. First, there was the support for mathematics within $INCLUDE files (see the January, 1997, issue). Second, there was the Type-10 user-defined analytical source. Third, there was $PARAMETER. Illustrations of these three ideas were placed in DC-36 ($INCLUDE) and DCNEW-19 (Type-10 source and $PARAMETER). But execution has been imperfect, so real, general use has been limited. A fourth possible use, to convert FORTRAN of TACS supplemental variables into TACS ASSEMBLY LANGUAGE (TAL), never was seriously considered due to slow execution. Replacement code that almost worked right first was demonstrated for BPA's Dr. Tsu-huei Liu on July 23 rd. This was program COMPILER.FTN after some four or five working days of effort spread over two weeks. Volume is moderate: 475 lines including occasional blank and comment lines. More work is needed, but success now seems guaranteed. Enough complicated expressions already have been handled correctly to convince your Editor that the project can be completed to handle all cases. The plan is to replace code of the Powell pocket calculator by this new one. That will be the first use in ATP. But it is far from the last. The real motivation for all-new coding is TAL, which until now was written by the human mind. Existing TAL is close to the language of the new pocket calculator. After perfecting the new logic as a replacement for the Powell pocket calculator, it is expected that compilation of TACS supplemental variables will be attempted. This is the long-term plan as writing of this story begins on July 23 rd. Lack of modularization is a feature of your Editor's code. Unlike the Powell pocket calculator, all of your Editor's code is contained in one main program. That is, no SUBROUTINE or FUNCTION module is used, and this should make execution fast enough for use within the time-step loop. That was the fundamental problem of the Powell pocket calculator: it was slow --- presumably because it called subroutines with arguments as part of each evaluation. Even if operation could be made perfect, lack of speed would have prevented widespread use within the time-step loop. This should not be a problem with the new TAL-like code (see MATHPLT3 in the April, 1997, issue). That inquiry for help ("What reader knows enough about compilers to offer an efficient solution?"), publicized in the January newsletter, went unanswered. Your Editor began coding during July only because no one else offered any advice or alternative. Necessity was the mother of invention in the case of faster pocket calculators, too. COMPILER.LIS is the output file that documents compilation of COMPILER.DAT, the input file that automatically is connected internally. For each line of input, there will be a corresponding output table. On July 25 th when improvement of the compiler had stalled, there were 37 such examples. Output can be illustrated by showing the 23 rd example, which is relatively simple: Compile next line KNT = 23 =========================== 88TEST2 = - COS ( THETA ) =========================== 1 COS THETA 2 -SAVE TEST2 The input (a TACS supplemental variable definition) is shown first, between separator lines of equal signs. Below this will be found the lines of TAL (in this case, just two) that produce the same effect. Note the minus sign in front of SAVE. This is new: saving the negative of the accumulator. While writing and testing the compiler, this and several other new assembly language instructions were found to be either convenient or more efficient. Details of TAL have changed, and a few lines of MATHPLT4.DAT differ from MATHPLT3.DAT in small ways. The new TAL allows modified SAVE whereas the old TAL used comparable modified library functions (no longer allowed). Removal of reliance upon blank separator characters was the first of two projects for the weekend that began August. As this paragraph is written during the evening of August 2 nd , blanks can be inserted anywhere except inside constants than involve scientific notation. Alternatively, all blanks can be removed. For Dube-format supplemental variables with a type-code in columns 1-2 and an equal sign in column 11, each input line is processed exactly as it is supplied by the user. But standard FORTRAN lines, which begin in column 7, are compressed by the removal of all blanks prior to processing. Perhaps Dube-format lines later will be compressed. But, for now, this is being avoided as a safety measure. If compressed logic ever might fail, one can switch to Dube-format lines with blanks to separate the troubled items. The possible embedded plus or minus sign of constants involving scientific notation posed the greatest problem of recognition. Either letter D or letter E must immediately precede the plus or minus sign, which in turn must immediately precede a decimal digit. Also, the E or D must immediately be preceded by either a decimal point or a decimal digit. If these conditions are not satisfied, the plus or minus is taken as a request for arithmetic (addition or subtraction). Unlimited precision of constants was the second of two projects for the weekend that began August. As originally coded, constants were limited to 8 bytes, and TAL involved fields of width 8 for all input. While program variables remain 6 bytes, with blank fill on the right, constants can be as wide as the user wants within the limit of column 80 (continuation lines are not yet allowed). Syntax of TAL has been converted to free-format, with output begins the first parameter in column 9, and thereafter uses two blanks to separate parameters. The extra blanks are not necessary, but make the lines more readable (a cosmetic consideration). IF-THEN-ENDIF and IF-THEN-ELSE-ENDIF were the first structured IF statements to be allowed. This was August 3 rd . Logic seems to be working correctly, and could be extended to allow an indeterminate number of internal ELSEIF if there is sufficient interest. But your Editor is rapidly losing interest. The single conditional jump handles all cases of interest. Who needs the structure? To your Editor, structure is more of a problem than it is a solution. Long live statement numbers (which remain a cornerstone of TAL in any case). Speed of compilation seems to be a strength of your Editor's new code. The first timing was performed on August 3 rd by looping 10K times over those same 6 famous lines that end with TWOXX (see the April, 1997 issue). After suppressing all printout, the 60K lines were compiled in 23.90 sec on your Editor's 133-MHz Pentium. This implies a compilation rate of 151K lines/minute --- plenty fast for massive usage prior to each simulation. Type-10 sources are to be the first feature supported by the new compiler. The only rule being changed concerns overflow. Previously, such sources were limited in number to 20 by local storage. That storage now is being removed. There will be some limits, but the new ones will be much larger; and they will be associated with TAL creation (by compilation) and execution rather than with the sources themselves. This is as implementation begins on August 6 th . Data of DCNEW-19 provided the first demonstration of correct handling of Type-10 sources. This was on August 8 th . For ease of timing, 100K steps to 2 seconds were taken as explained in the April issue. The same math is being performed, only TACS has been replaced by Type-10 voltage sources which drive one-ohm resistors to ground. Time-step loop output is identical, and execution seems believably fast --- and obviously faster than Dube's logic. We now can add a third row to the two that were published six months ago: 1980 data of MATHTAC1.DAT : 18.077 1975 data of MATHOLD.DAT : 13.077 Type-10 in MATHTY10.DAT : 6.538 About the structure of execution, there is a single call to the new SUBROUTINE POCKET during each time step. All type-10 source functions are evaluated for that particular instant of simulation time. This is the same as TACS: one call per time step. In order to separate the time of function evaluation from the remaining burden, the code to service TAL within POCKET was temporarily bypassed: Type-10 without functions : 4.780 In round numbers, less than 2 seconds are required for 100K evaluations of that set of six functions. Type-10 sources have been boosted to warp speed. $PARAMETER use was activated August 11 th after struggling with a known ver. 2.66 Salford compiler bug (imperfect operation of virtual scratch files). Unlike TACS supplemental variables and Type-10 sources, speed of execution is not an important consideration for the $PARAMETER application. This is because usage is outside the time-step loop (each evaluation is performed only once). Whereas all Type-10 functions are evaluated during a single CALL to the new POCKET, each line of $PARAMETER definition was permitted to involve its own, separate compilation and execution. A fundamental improvement concerns the handling of SPY symbols (e.g., EPSILN for the familiar singularity tolerance). Previously, each such symbol was eliminated by substitution of its numerical value for the A6 name. This caused two problems: 1) full precision of floating-point values required that the user leave artificial, extra space; and 2) negative values sometimes resulted in an error termination by the Powell pocket calculator (e.g., "-TMAX" would become "- -2.5" if TMAX had value -2.5). Both of these complications have been eliminated by the new code. No longer does extra white space affect precision (parameters automatically are handled as an 18-byte encoded string), and negative parameters always should be handled correctly. If not, complain to your Editor. Laurent Dube's logical functions and operators were the preoccupation of the weekend of August 15 th and 16 th. As a first step, in order to gain familiarity, the logical functions and operators were added to MATHPLT4. Next, they were added to COMPILER. Finally, after this simplified and isolated testing, changes of the preceding two programs were transferred to subroutine POCKET of ATP. Recall what is involved. Unlike FORTRAN, Dube'slanguage of supplemental variables allows floating-point variables to be processed by functions. NOT.,. AND.,. OR.,. NAND., and. NOR., and by relational operators. EQ.,. NE.,. GT.,. GE.,. LT., and. LE. Meanings correspond to FORTRAN, but no separate variable type LOGICAL is involved. This is the main reason compiled TACS provided an incomplete solution: Dube established his own rules for logical variables. This difference was a primary motivation for allowing the AA and BB alternatives as explained in the April, 1997, newsletter. Well, a strength of your Editor's compiler is that he, unlike Salford or Watcom or GNU, can follow Dube's non-standard rules for logical variables. Anyway, this has been the hope. This provided the motivation for doing the work. About order of evaluation, all NOT are processed first, then all AND and all NAND, then all OR and all NOR, and finally the six relational operators. Despite the separate name, the relational operators, too, are functions, it is to be noted. The logical function evaluation is not started until all numerical operations have been completed. Also, within any one of the four logical classes, processing is done from left to right. These rules seem plausible to your Editor, but part did not come from any book. Any reader who believes them to be incorrect is encouraged to send E-mail correcting the order of evaluation. Change should not be difficult if it is made soon enough, while details remain familiar. Logical operations have been less efficiently coded than numerical operations, it should be explained. With only one exception, logical operations are lower in level (more RISC-like). Implementation was simplified by assuming that both arguments of all 2-argument functions are stored in memory. That is, neither is allowed to be in the accumulator. As a result, otherwise-extraneous SAVEs of the accumulator will be required in places. The only exception is. NOT., which involves just a single argument. Programming allows this to be either in memory or in the accumulator (i.e., the compiler appropriately avoids the intermediate SAVE if the argument already is in the accumulator). Another extension applied to. NOT. is an optional, automatic SAVE to memory upon completion. As an illustration, consider the FORTRAN line SIGNAL =. NOT. FIRE2 Many comparable statements can be found in Robert Hasibar's DC-1 --- comparable, but different in structure, because his data creation preceded the free-format alternative that Dube introduced during 1980. The stated illustration will be compiled into the single line of assembler :. NOTSV FIRE2 SIGNAL Whether other logical operators might be raised to this higher, CISC-like level will depend on demand. Your Editor is aware of no fundamental theoretical problem (for the Salford compiler, anyway). Evaluating Analytical Functions , III (Extracted from Can/Am EMTP News Jan/1999) Exponentiation is requested by the ** of FORTRAN, and it was supposed to be a supported function, of course. But as Gabor Furst observed in E-mail dated September 30 th , exponent value 2 was handled correctly whereas any other value seemed to result in a temporary error termination. Apparently the general case never had been tested. Once existence of the trouble was known, correction was found to be easy enough (the following morning). Exponentiation A ** B was not properly handled for negative A prior to October 12 th . Working with Gabor Furst who had interest in the subject, Orlando Hevia of Universidad Tecnologica Nacional in Santa Fe, Argentina, wrote that your Editor's assurances were theoretical rather than actual: "Yes, but this is in FORTRAN, not in PCVP. I tried (-1) ** KNT and ATP aborted with some error message like 'real exponent for negative base' from DBOS." So, your Editor modified logic to check for non-negative A. Should A be negative, ATP will continue with special logic provided B is an integer. If B is not an integer, there will be an error message. $PARAMETER definitions were limited to a single line for each variable that was being replaced. This was prior to October 17 th when intermediate variables first were allowed in response to a request from Mr. Furst. Now, an arbitrary number of intermediate variables is allowed, permitting much more complicated formulas. For an illustration involving one intermediate variable, see the 5 th subcase of DCNEW-25. The user is warned, however, that all intermediate variables are erased upon use of the following variable of substitution. So, if one intermediate variable is used by two or more variables of substitution, it must be repeated before each. Later, if this is found to result in substantial repetition, the rule might be modified. But, for now, it is convenient. As usual, the capability evolves (bottom-up design, as with most creative engineering). An induction motor model prompted an increase in the number of intermediate variables from 15 (the original limit) to 25. This was a couple of days later. It did not take long for limits of the preceding paragraph to be recognized as inadequate. Writing to Orlando Hevia and Gabor Furst on October 20 th , your Editor finally could report success handling Mr. Hevia's induction motor data: have been debugging my way through the pocket calculator. … I modified the code to preserve intermediate variables after each substitution variable. … Preceding intermediate variables remain defined … Next, I found lack of protection against overflow of temporary variables. This morning, I allowed only 15 (it seemed like a lot at the time). … clearly, I had not anticipated heavy duty usage such as yours. So, I added protection against overflow at the same time I expanded the storage to 25. Next, I found that xxxxxx was an assembly language verb produced by my compiler. But it was missing in the code to execute. … So, I now add the missing execution code (no problem once the failing is recognized). … Good deal, now execution runs to completion. … Let me know what more might be wrong, if anything, and I will work on it.” One or more blanks separating a parameter name from its following equal sign were not generally permitted prior to October 22 nd . The preceding day, Gabor Furst had noticed that column boundaries for R and L of a series R-L-C card were not being properly observed. This was for his induction motor model within an HFS study. The error was traced to an extra blank left of the equal sign of a substitution variable. But blanks provide useful visual separation, and should be encouraged. So, the logic of S.N. 1094 and below was added to MATDAT in order to ignore any number of blanks preceding an equal sign. The Type-10 electrical source card carries an analytical expression to the left of familiar T-start in columns 61-70. Unfortunately, an easy error for the user to make was a formula that was too long. If the formula spilled over into T-start, the result generally would be unreadable, and execution would die (for Salford EMTP, with DBOS opening a debugger error window). Beginning October 28 th , ATP will check for legal T-start and T-stop, and will end the subcase in civilized fashion (KILL = 236) if an error is found. This is illustrated by a new 2 nd subcase of DCNEW-19 (the former 2 nd has become the 3 rd ). While few users care about Type-10 sources, the improvement is believed to be noteworthy as a test of procedures that might be applied for checking of other, more popular data. A sawtooth waveform on the electrical side (i.e., not in TACS or MODELS) was the contribution of Orlando Hevia. His E-mail dated November 10 th began as follows: "I send a file with a sawtooth source." For your Editor, this was a complete surprise. Mr. Hevia used the pocket calculator to define a Type-10 source, as first mentioned in the October issue. That is, rather than use sawtooth logic that is built into the program, Mr. Hevia supplies his own analytically, in data. To be sure that this creative idea is not forgotten, Mr. Hevia's centered waveform has been added to the 1st subcase of DC-22. Might there be more such contributions? Maybe. Mr. Hevia wrote: "I think that with some work, a rectangular pulse train (as TACS 23 source) could be simulated." The numerical interface of the pocket calculator was changed November 11 th in order to remove a conflict with other sources. This was found to be necessary for the just-mentioned modification of DC-22. Surprisingly, two signals of one other standard test case changed in the process: node voltage COS and associated current (COS, TERRA) of the 1st subcase of DCNEW-19. Clearly, the signals have been improved (previously, they were wrong after the first time step). Continuation lines of FORTRAN are distinguished by blank columns 1 through 5 and nonblank column 6. This capability was added to your Editor's compiler on November 14 th in order to solve a problem posed by Orlando Hevia. His E-mail the preceding day explained: "I tried the pulse source, but I can't reduce the number of lines to two." First, the Type-10 analytical source allowed just columns 11-60. Then, when more space was required, CONT. in columns 61-65 was provided to indicate the desire for a single continuation line. But even this was not enough to satisfy Mr. Hevia's creative mind, it would seem. So, expansion to an indefinite number of lines now is being provided. This is independent of CONT. which will continue to be honored for historical reasons. Yet, it is not required. FORTRAN-like continuation is being allowed, and is illustrated in the 3 rd subcase of DCNEW-19. $$ ( a double dollar sign) was selected to mark an intermediate variable within $PARAMETER data. By definition, an intermediate variable is one that is used to define later data symbols, but is not itself a data symbol. Such a symbol will not be searched for and replaced in data cards that follow the $PARAMETER data structure, then. Illustration is provided in the 5 th subcase of DCNEW-25, although this use is trivial and artificial. The need came from Orlando Hevia during mid-October, as he was constructing a frequency-dependent model of an induction motor for Gabor Furst's HFS use. In a disk file named GABMOT.DAT, this data uses 20 intermediate variables followed by 5 data symbols. Sample lines are scheduled for illustration in the associated Rule Book section. Simulation speed for TACS first was measured on December 4 th . This involved the same data as for the original TACS ASSEMBLY LANGUAGE ( TAL ) as summarized in the January, 1998, issue. Once again, we use the best 5 of 6 consecutive trials of the 60-variable, 100K-step MATHTACS.DAT. But this time Salford DBOS is run under DOS of Win95 rather than within a DOS window of Win95. We now can add a row to the preceding tabulation of time-step loop seconds using your Editor's 133-MHz Pentium: Dube's original TACS, 60 variables : 106.21 TACS assembly language, 60 variables: 18.33 New built-in compiler, 60 variables : 18.41 Original TACS, one dummy variable : 8.46 About time to compile, this additional burden would be part of the initial timing figure, which measures data input. This has not changed significantly. Again taking the best 5 of 6 figures gives 2.08 seconds. No big deal (your Editor's compiler is plenty fast). Evaluating Analytical Functions , IV (Extracted from Can/Am EMTP News Apr/1999) A sawtooth waveform on the electrical side was mentioned in the January issue. Since then, Orlando Hevia has supplied additional creative uses of the pocket calculator to define Type-10 sources. On January 26 th, that 1 st subcase of DC-22 was expanded once again. Yes, the predicted pulse train is there, along with a rectangular waveform. As Mr. Hevia shows, these are easily generated analytically from a reference sine wave. The time has been extended to two cycles, and an added CALCOMP PLOT displays all 4 signals for easy comprehension. Superposition of two or more such Type-10 sources at a single node was not possible prior to the correction of POCKET on January 29 th. Once again, it was Orlando Hevia who accurately reported the trouble following your Editor's assurance that superposition should work. But it did not due to a structural peculiarity of your Editor's pocket calculator. Each Type-10 source involves a name, of course, and two or more at the same node led to confusion about which one was which during evaluation. The correction allows up to 9 sources at the same node, and it should be fool proof as long as the analytical function of each later Type-10 source is independent of the value of each source that participates in the superposition. A negative offset of 1/4 was added to DC-22a to illustrate such use. This was by means of a second Type-10 source at node PULSE. Order does matter, however: sources involving superposition are assumed to be contiguous.