Textual expansion of chemical process flowsheets into algebraic equation sets

Textual expansion of chemical process flowsheets into algebraic equation sets

Compufcn and Chemical Engineering Vol. 4, pp. 123-239 PcrgamonPress Ltd.. M30. Printedin Great Britain TEXTUAL EXPANSION OF CHEMICAL PROCESS FLOWSHE...

1MB Sizes 0 Downloads 33 Views

Compufcn and Chemical Engineering Vol. 4, pp. 123-239

PcrgamonPress Ltd.. M30. Printedin Great Britain

TEXTUAL EXPANSION OF CHEMICAL PROCESS FLOWSHEETS INTO ALGEBRAIC EQUATION SETS MICHAELE. HANYAK,JR.t Department of Chemical Engineering, Bucknell University, Lewisburg, PA 17837,U.S.A. (Received 12 May 1980)

Abstract-In preliminary process design, the engineer must deal with hundreds to thousands of nonlinear algebraic equations that model the steady-state simulation of chemical processes. The manual preparation of all these equations as input to a computer-based program can be a laborious and highly error-prone task. This paper presents the design specifications of a textual expansion language for expressing the text of equations. This language provides a modular capability, called “textual macros,” in which to express concisely the large numbers of equations associated with chemical process simulation. With a standard library of textual macros for process units, all the process simulation equations can be expressed just from the labeling information in the process flowsheet. Scope-In process engineering, chemical process simulation by computer has become an accepted tool for finding the steady-state solution of material and energy balances. As presented in the journal reviews of Motard et a/.[11 and Hlavacek[Z], steady-state process simulation is basically the modeling of a chemical process by a set of nonlinear algebraic equations, called a mathematical model, and then the solution of these equations to obtain information about the performance of the chemical process. The mathematical model can be organized either as one large global set of equations or as subsets of equations, called modules, according to the process units that appear in the chemical process. The modular approach has been used predominately to date, but the global approach is relatively new and an active area of research. In the modular approach, each chemical processing unit is represented by a multi-valued function, called a unit module. The functions composing the mathematical mode1 are ordered manually into a solution procedure, and this ordering becomes part of the input (data) to a module-oriented process simulation program. A library of standard unit modules, which are usually preceded FORTRAN subroutines, is an integral part of the process simulator. PACER 245[3] and FLOWTRAN (Seader et al.[4]) are examples of academic and industrial process simulators, respectively, that use the modular approach. In the global approach, the mathematical model is not considered as a collection of multi-valued functions, but as a large system of nonlinear equations representing the entire process. These equations are the input (data) to an equation-manipulation program that partitions and orders them into a solution procedure and automatically generates a specific computer program for that procedure. Mah & Rafal[5] and Solylemez & Seider [6] have developed computer-based manipulators that automatically transform mathematical models for process simulation and design into specific FORTRAN programs., Since the global approach analyzes all the equations representing an entire chemical process, it takes full advantage of the specific features of the equations which in the modular approach are ignored (Motard et al.[l]), Also, it generates a tailored computer program for each new particular problem, while the module-oriented process simulator is used for all problems. Because of these two advantages, the tailored computer program executes faster and uses less storage than moduleoriented process simulators. The major disadvantage of the global approach is the necessity of manually preparing a new input description of all the equations for each new particular problem (Hlavacek[2]). This preparation becomes a tedious and highly error-prone task, because chemical processes may be modeled by nonlinear equations numbering in the hundreds to thousands.

tMichael E. Hanyak, Jr. received his B.S. degree from the Pennsylvania State University in 1%6, his MS. degree from Carnegie-Mellon University in 1%7, and his Ph.D. from the University of Pennsylvania in 1976. From 1%7-70 he was employed by Air Products, Inc. as a senior engineering programmer in their computer application department. Since 1974he has been at Bucknell University where he is Assistant Professor of ChE. His

research areas are software engineering, computer-aided design, and computer-assisted problem solving. He and Dr. Daniel C. Hyde of the Computer Science Department at Bucknell have just completed a 23 year interdisciplinary NSF grant (MCS 77-03856) entitled “Development of a Language Design Methodology to Formulate User Problem-Oriented Language Specifications”. He is a member of AIChE and is an avid jogger and woodworker.

223

M. E. HANYAKJR.

224

In this paper, design specifications are presented for a textual expansion language. Using this language, the nonlinear algebraic equations of chemical processes may be generated automatically from just the flowsheet descriptions. A library of expansion commands is defined for those equations most commonly associated with chemical flow processes such as material and energy balances. Furthermore, a library of expansion modules is given for some of the standard process unit operations such as compression and counter-current heat exchange. The grammar specifications of a textual expansion language for Conclusions and SIcanceexpressing the text of equations are presented concisely using a formal notation from computer science called Backus-Naur Form. The textual expansions of summation, equation repetition, and text sequencing are the primitive operators of this language. Also, user-defined textual expansion procedures called “macros” provide the generalization needed to express concisely large numbers of equations. With this language, all the algebraic equations associated with the steady-state simulation of chemical processes can be expressed just from the stream, unit, and compound labels contained in the process flowsheet. This capability, when merged with any equation manipulation program that automatically transforms equation models into tailored procedural programs, reduces significantly the user’s input preparation of equation sets that model chemical processes. In the case of the Linde liauefaction nrocess. one innut line renresenting a macro call would be expanded by the macro processor into 118 lines of equations. _ EQUATIONMANFULATORS

In the modular approach to steady-state process simulation such as in the FLOWTRAN system (Seader et al.[4]), process units are represented by FORTRAN subprograms preceded by the system programmer for the potential user. If the user wishes to incorporate his own module, he must code it with some understanding of the data structures used within the simulation system. In an alternative method as demonstrated by Solylemez & Seider[6], mathematical models for process units can be transformed automatically into FORTRAN modules which are compatible with the simulation system. This latter concept is depicted as follows: M&hcmati4 Model

)

Equation Manipulation Program

PORTRAN

+ subproprsm

where the mathematical model is a set of equations that models the physical process. The manipulation program scans these equations, checks them for consistency, orders them into a solution procedure, and generates the required FORTRAN subprogram. Also, the design variables for a model are input (data) to the manipulation program. For example, the mathematical model for the adiabatic compression of a multicomponent process stream is given in Fig. 1. This unit operation is assumed to occur at steady state with no chemical reaction, and the kinetic and potential energy changes are negligible. The independent set of equations contains the material, energy, and entropy balances, the thermodynamic functions for enthalpy, entropy, and vapor fraction, the functions for isentropic conditions, and the definition for adiabatic efficiency, To solve these equations, (NC +6) design variables must be specified. The mathematical model supplied to a manipulation program must be prepared according to the syntax specifications of its input language. For this paper, this syntax is defined as follows: A mathematical model is a list of equations separated by semicolons. The equations are FORTRAN-like assignment statements, except their left parts may be arithmetic expressions also. Only FORTRAN-like scalar, vector, and matrix variables may appear in the equations,

and they must represent the stream and unit (equipment) parameters associated with chemical processes. The manipulator input for the adiabatic compression model of Fig. 1 might be coded for a two-component stream as shown in Fig. 2. In the thermodynamic functions such as HVL for the enthalpy of a vapor-liquid mixture, the notation Z(i) implies all component compositions for the ith stream. The coded equations in Fig. 2 would be processed automatically by a manipulation program to produce the required FORTRAN sub program. For the fourteen equations of Fig. 2, at least a 1004ine FORTRAN subprogram would be’ generated, as illustrated by Hanyak [7]. Obviously, the manipulation concept reduces the user’s effort in preparing his own unit module. A natural extension of this application of the equation manipulation concept is to model the steady-state simulation of a chemical process using ah of the equations, instead of FORTRAN modules, for the process units. In this global approach, all equations for a particular simulation would be transformed automatically by a manipulator program into a tailored FORTRAN executive. Tailored executives are usually smaller and execute faster than general-purpose executives such as FLOWTRAN, as demonstrated by Hanyak[7]. However, the input preparation for the equation manipulator in this global approach can become a very tedious and highly error-prone task. For example, onehundred and eighteen equations are required to model the Linde liquefaction process (to be described later) containing five unit operations that process a ten-component fluid mixture. Using the manipulator input language described above, the user would have to code 118 equations. To improve the coding task, this paper addresses the problem of how to express a large system of equations for a chemical process in a compact form suitable for processing by an equation manipulation program. The major factor needed to solve this problem can be found by examining the equations that model various process units. In general, a chemical process has many sets of process units that are modeled by the same equations differing only in the variable names and the number of terms appearing in them. Furthermore, these diierences are controlled by the stream, unit, and compound labels assigned to the process units. For example, two com-

Textualexpansionof chemical

processflowsheets intoalgebraic equationsets

225

Assumptions

Process Diagram

1. 2. 3. 4.

%

steady state no reaction neglect K.E. and P.E. adiabatic process

Simulation Equations Fl

= F2 for j = 1, .... Nc

Zlj = Z2j

T; = TVL(P2, S,, -2 Z 1

F,H, + W, = F2H2

Hi= HVLQ,

F,S, + Sg = F2S2 Hi = HVL(Ti, Pi* 4) Si = SVL(Ti, Pi* fi)

for i = 1, 2

Ri =

for i = 1, 2

Vf(Ti, Pi* fi)

for i

q

Pp.

4,

Hi - H,

1, 2

A'-

Variable Descriptions Nc

is

the number of chemical components.

Ti

is

the temperature in K for the i-th stream.

Pi

is

the pressure in atm for the i-th stream.

Fi

is

the flow rate in gmole/hr for the i-th stream.

Zij is

the composition in mole fraction for the j-th component in the i-th stream (4 is all NC compositions).

Hi

the enthalpy in cal/gmole for the i-th stream.

is

Si

is

the entropy in cal/gmole-K for the I-th stream.

Ri

is

the molar vapor fraction for the i-th stream.

Ti

is

the isentropic temperature in K for the outlet stream.

Hi

is

the isentropic enthalpy in cal/gmole for the outlet stream.

E

is

the adiabatic efficiency in.percent, ratio of (Ws)rev/(Ws)ac..

W,

is

the shaft work in cal/hr.

Sg

is

the entropy generation in cal/hr-K.

Deqrees of Freedom Number of Variables

q

Number of Equations

=

2N, + 18 NC + 12

D.O.F.

=

NC+

6

Fig. 1. Mathematicalmodel for adiabaticcompression.

M. E. HANYAK JR

226

F(1)

= F(2);

Z(l,l) = Z(2,l);

Z(1.2) = Z(2.2);

F(l)*H(l) + WS = F(2)*H(2); F(l)*S(l) + SG = F(2)*S(2); H(1) = HVL(T(l).P(l)sZ(l));

H(2) =

S(1) = SVL(T(l),P(l),Z(l));

S(2) =

R(1) =

R(2) =

TP2

VE(T(l),P(l),Z(l));

= TVL(P(L),S(l).Z(2));

HP2

=

E/100 = (HP2 - H(l))/(H(2) - H(1));

Fig. 2. Manipulatorinput for adiabatic compression. pressors, both of which are operating on a nitrogen (N2) stream, are modeled by the equations in Fig. 1. The first compressor labeled Unit Cl has Stream 5 entering and Stream 6 leaving, while the second labeled Unit C2 has Stream 8 entering and Stream 4 leaving. The manipulator input for just the material and energy balances of Compressor Cl are: Z(S,N2) = Z(6,N2); F(5) = F(6); F(S)*H(S)+ WSCl = F(6)*H(6) and those for Compressor 2 are: F(8) = F(9);

Z(8,N2) = Z(9,N2);

F(8)*H(8) t WSQ = F(9)*H(9). where WSCl and WSC2 are the shaft works for compressors Cl and C2, respectively. If the contents of these two examples are treated as textual information (a long sequence of characters) and not as equations, the following pattern is recognized as common to both: IV4 = IV); Z($,@) = Z(#,@); F($)*H($) + WS% = F(#)*H(#) where $ and # are the labels for the entering and leaving streams, respectively, @ is the compound label, and % is the unit label. The ability to define a textual expansion procedure (one that operates on text and not numbers) and then to call on that procedure would simplify, greatly, writing the large system of equations for a chemical process. The above textual pattern basically would be the body of a procedure definition, and the $, #, @, and % symbols would be the dummy arguments. When calls are made to this user-defined procedure for Units Cl and C2, actual textual arguments such as ‘5’ and ‘N2’ would be substituted or inserted for the dummy ones ($ and @, respectively). Then the generalized textual pattern would be expanded into the appropriate equations given above. This major fact suggests the need for a textual expansion facility that augments the input language of an equation manipulator. The textual expansion facility, proposed herein, works in conjunction with a manipulator. As the manipulation program scans the input for a mathematical model, it will identify equations and textual expansion commands. When a command is recognized, it calls on the textual expansion facility to generate equa-

tions, similar in form to those in Fig. 2, and then to substitute these generated equations in place of that command. Then these generated equations are subsequently processed by the manipulator. TEXFUAL EXPANSIONLANGUAGE The idea of text substitution and expansion is not new

in computer science. Programming language such as PL/I (ANSI[8]) and XPL (McKeeman et a1.R) provide the ability to use macro commands that aid in the coding and the comprehension of computer programs. Macro commands allow a programmer to associate mnemonic names with similar or identical blocks of source code that are to appear in various parts of the program. A combination of macro commands and source statements can be used to code a computer program. When the appropriate compiler scans this program and identifies a macro riame, it activates the macro processor which substitutes the appropriate block of source code in place of the macro name. The source code that is substituted depends on the macro definition which usually is given in an earlier part of the program. The macro concept is discussed more fully in other references (Aho & Ulltian[lO], Grie’s[ll]). In this paper, the textual expansion facility is a macro processor and will be referred to as such. This processor, itself a computer program, transforms commands of the macro language into the text of equations acceptable to an equation manipulator. What are the design specifications for this textual expansion (or macro) language? To begin with, two basic statements or commands are needed. First, a command that allows the definition of macro procedures with dummy arguments. Second, a command to call a pre-defined macro procedure using actual arguments. This procedure capability provides the power to generalize, similar to subprograms in the FORTRAN language. However, note that macro procedttres will process text and not numeric values. How will a macro procedure expand textual information? Statements of any computer language are composed-of basic components. For example, a FORTRAN expression contains variable names (called identifiers) and operators (such as t and *). FORTRAN operators represent arithmetic operations on numeric values. In the macro language, special textual operators are needed to expand text. What are the basic operators of the macro language? The answer is found by examining the algebraic equations often associated with chemical processes as shown

Textual expansion of chemical process flowsheets into algebraic equationsets System Diagram

Incoming Material Streams

Outgoing

b Material Streams

c Total Material Balance NI 1 i=l

NT Fi

=

Fi x i=NI+l

Component Material Balances NI =

F.Z.

for j = 1, 2, .... NC

1 i,j

i=NI+l Composition Constraints

z.

. = 1.0 lsJ

f

j=l

for i = 1, 2, etc.

Energy Balance p

FiHi

+ Q, - w, n ,

=

FiHi

Entropy Balance N t i=l

T FiSi

+

Q,/T, , n + S g,n

=

f i=NI+l

FiSi

Physical Property Functions Hi 'i

=

HVL(Ti, Pi, 'i)

for i = 1, 2. etc.

=

SVL(Ti, Pi, 'i)

for i = 1, 2, etc.

Vapor-Liquid Equilibrium Distribution Z v,j

=

KjZ, j

for j = 1, 2, .... NC

,

Equilibrium Vaporization Ratios Kj

=

for j = 1, 2, .. .. NC

KVL(Ti, Pi, 4)

Iso-Parameters Ti

=

Tf

and

'i

=

Pf

for i = 1, 2, etc.

Total Mole Balance

zFi+cRl[z Yjsl] =f

i=NI+l

Fi

Component Mole Balances

for j = 1. 2, .... NC

221

228

M. E. HANYAK JR. Molar Atom Balances

for k = 1, 2, .... NE

Variable Descriptions

is the number of incoming material streams.

NI

NT NC

is is is is

the the the the

T. P? F? z? "j

is is is is f i

the temperature of the i-th stream. the pressure of the i-th stream. the total flow rate of the i-th stream. the fractional composition of the j-th component in the i-th stream. is all NC compositions in the i-th stream.

H. s? K?

is the enthalpy of the i-th stream. is the entropy of the i-th stream. is the vaporization ratio of the j-th component.

1;

J

total number of material streams. number of chemical components (compounds). number of chemical reactions. number of chemical elements.

is the reaction constant for the l-th chemical reaction. j , is the stoichiometric coefficient of the j-th component in the 1-th VRl ’ chemical reaction. aj k is the number of atoms of the k-th chemical element in the j-th ’ component. is the heat transfer for the n-th process unit. W" is the shaft work for the n-th process unit. T"" is the average surface temperature for the n-th process unit. Si'l is the irreversible entropy generation for the n-th process unit.

Q

,

Fig. 3. Mathematical equations associated with flow processes.

in Fig. 3. The general system diagram of Fig. 3 has NI incoming material streams and (NT-NI) outgoing ones. There are NC chemical compounds (components) in the system, and they contain NE different chemical elements. NR chemical reactions may be occurring in the system; and, heat and shaft work may be transferred to or from the surroundings. The flow process is assumed to operate at steady state with negligible changes in the kinetic and potential energies. The algebraic equations in Fig. 3 are used to write the material, energy, and entropy balances and the thermodynamic relationships that usually appear in the mathematical models of chemical process units. In Fig. 3 the first two equations represent the material balances of a non-reacting system. The total and component mole balances of Fig. 3 are for a reacting system. Two common notations are used to express the general forms of the equations in Fig. 3. First, the summation notation (Z) allows for the representation of many terms in a compact form. Second, the repetition notation (for j = 1, 2, etc.) expresses many equations compactly. These two notations are the basic textual expansion operators in the macro language, and they are represented by the primitive macros SUM and REPEAT. They are called primitive macros because their definition is built into the code of the macro processor and cannot be changed by the user, much in the same way that the FORTRAN operator of addition (+) is built into the FORTRAN compiler. Macro SUM is used to express the summation of elements for a subscripted variable. For example, the mathematical summation

2%

or

X,+x2+...

+x6

may be written in macro form using the following macro call: (SUM X(J) WITH J = 1 TO 6) where the special delimiters ( and ) denote a macro command whose content is to be expanded by the macro processor. When this macro SUM is expanded, it produces the following text: X( 1)+ X(2) t X(3) t X(4) t X(5) t X(6). In the SUM call, the ‘(SUM’ and ‘WITH’ delimit the text of the summation term. Blanks preceding or following the text are ignored in the expansion, but they are not within the text. The ‘WITH’ and ‘), delimit the index and the number of terms to be produced. Macro REPEAT is used to write compactly a series of mathematical equations that differ only in the index values of their subscripted variables. For example, the following set of mathematical equations: Fl.1+ Fs,, = Fz,;

IL + h.2

= F2.2;

FM

+ Fs,

= F2.3;

may be written more compactly as follows: Ft.j + Fs~ = F2.j;

for j = 1,2,3.

The macro equivalent of this compact notation is the following macro call: (REPEAT F(l,J) t F(6,J) = F(2,J); where

the

to-be-repeated

text

WITHJ=lTO3) is given

between

Textual expansion of chemical process flowsheets into algebraic equation sets

229

‘(REPEAT’ and ‘WITH’ (excluding preceding and trailing blanks). The replacements to be made in this text are given between ‘WITH’ and ‘)‘. When this macro is expanded, it produces the following text of equations:

When the macro processor expands the command delimited by ‘(’ and ‘)‘, it produces the following text:

F(l,l)+ F(6,l) = F(2,l);

In the expansion, the text deiimited by ‘(SEQN’ and ‘WITH’ is reproduced according to the replacements specified between ‘WITH’ and ‘),. At the end of the expansion, the last character is deleted from the sequence. Blanks that precede or follow the to-bereproduced text are ignored in the expansion. The expansion operators SUM, REPEAT, and SEQN along with the macro definition and call are the basic components of the textual expansion language.

F(1,2) + F(6,2) = F(2,2); F( 1,3)t F(6,3) = F(2,3); In addition to SUM and REPEAT, a primitive macro named SEQN is needed to express a sequence of text separated by an appropriate character. For example, the outlet pressure of a multiple-inlet stream mixer may be expressed mathematically as

POUT = MIN(P(l),P(2),P(3),P(4)).

MACRO COMMANDS

POUT = MIN(P(l), P(2), . . . ,P(n))

The

where ‘n’ is the number of inlet streams. This text may be written in macro form for four inlet streams as follows: POUT = MIN((SEQN P(I), WITH I = 1 TO 4)).

formal design specifications for the textual expansion language (TEL) are given in Tables 1 and 2. The TEL grammar of Table 1 is described using BackusNaur Form (BNF) (Naur[l2]), a widely-used formal method developed by computer scientists for the precise syntactic description of context-free computer lan-

Table I. Grammar for the textual expansion language

::=



I



.. .. . Macro Definition .....

::= ::=



::=

I

"<"MACRO END">" ; ;

..... Key-Word Arguments .....

::=





::= 1



::=

$ = $ =

1 ,

::=

. . . . . Macro Body ... ..

: := I

::=

I .. .* . : :’ I ::=

: Macro Text .....



I

::=

I

f

. . . . . Macro Call .....

::=



1

230

M. E. HANYAKJR. Table l(contd)

8NF Rule

Syntax ..... User-Defined Macro .....

:: 34

::= ::= 1

"<" ">"

..... Actual Arguments ..... 33: 37 38 43: 41



::=





::=



1 , I



**= "1 ::=

= %DL =

.. ... Primitive Macro .....



.. ..= .. ..= I .. .= .



.. .= . .. .= . =



I .. ..=

51



.. .= .

:: 54



I .. .= . I &

55 56



.. ..=

5: 44 45 46 d: 49 50



"<" ">" SUM REPEAT SEQN WITH

=

. . . . . Index Set ..... EXCEPT



! ..= .

:;

I



.. ..= .. .= .

TO TO BY

Table 2. Semantics for the textual expansion language 8NF Rules 1-2

Semantics A macro command is either a macro definition or a macro call. A macro call activates the expansion of a macro definition. This expansion must eventually produce syntactically-correct equations. However, it may product the null string. Once these commands are processed, macro definitions are replaced by null strings, and calls to macros are replaced by their expanded text.

3-6

The macro definition begins with< MACRO and ends with END>. and it contains a calling format and a body of text. The calling format, which is terminated by a semicolon, defines the macro's assigned name and possibly its list of indicators. These indicators are arguments that may appear in a macro call. The macro name is an alphanumeric string that begins with a letter and is no longer than 256 characters.

231

Textual expansion of chemicalprocess flowsheets into algebraicequationsets Table 2(contd) BNF Rules 7-12

Semantics The indicator list is a list of key-word arguments separated by CORIMS. The indicator contains a key-word identifier preceded by a dollar sign and followed by an equals sign and possibly a default value. The default value is a character string enclosed in apostrophes. An apostrophe appearing within this string must be represented by two consecutive apostrophes. The indicators appear in the macro body and indicate where text, supplied in a macro call, is to be substituted when the macro is expanded. Indicators without default values imply that they must be supplied in a macro call. Those with default values are only supplied to override the default values.

13-20

The macro body contains one or more definition lines which may be labeled with a line number followed by a colon. Line numbers must be integer constants. Definition lines contain local macro definitions and/or text. The contents of a labeled definition line is everything between its line number and the next line number or, in the case of no next line number, the terminator END>. Local macro definitions are available for expansion only within the macro in which they are defined and in all of that macro's subordinate macros. Their definitions temporarily override any previously-defined macros with the same names.

21-29

The macro text contains a sequence of subparts. A subpart is either a dollar sign, an alphanumeric identifier that begins with a letter, a FORTRAN-like integer or real number, a character string enclosed in apostrophes, a one-character non-reserved symbol, an alphanumeric identifier enclosed in quotes, or a macro call. In the macro text, a dollar sign followed by an identifier is recognized to be an indicator when the beginning part (or possibly all) of the identifier is one of the key words in the calling format. An integer number followed by a colon in the text is treated as a definition line number. Quoted identifiers have a special meaning only within the context of primitive macro calls. A non-reserved symbol is any non-alphanumeric character except for one of the following: < ,> , ‘, II, :, and $. These six reserved characters will not appear in the expanded text of the macro; however, if any of them are to appear, they must be enclosed in apostrophes.

30-31

A macro call activates the expansion of either a user-defined macro or a primitive macro. The definitions for user-defined macros are made through the macro definition command; however, those for primitive macros are built into the program that processes them.

32-36

The call for a user-defined macro identifies the macro's name, and it may contain a list of substitution arguments separated by coamias. These arguments will be used in the expansion process. The arguments that may appear in a call are defined in the macro's definition, and they need not be supplied in any specific order. The expansion process proceeds as follows: first, the arguments in a‘macro call are expanded because they may

-

232

hf. E. HANYAK JR. Table 2 (Contd) BNF Rules

Semantics contain macro calls; next, the expanded arguments are substituted for their corresponding indicators in the macro body; finally, the macro,body is expanded to produce the expanded text that replaces the macro call. During the expansion process, the contents of character strings are not expanded; but, their surrounding apostrophes are dropped and two consecutive apostrophes appearing within these strings are replaced by one apostrophe. Also, the definition line numbers and their colons in a macro definition do not appear in the expanded text.

37-41

A substitution argument is either key word and its substitute, or a line numbers (%DL) that are to be expansion (i.e. they do no appear text).

a macro call, a list of definition ignored in the in the expanded

The expansion of those macro calls that appear within a macro call must produce valid substitution lists. When the substitutes of key-word arguments contain blanks, comas, and/or macro calls that are not to be expanded, they must be enclosed in apostrophes. Key words in the macro call that do not appear in the calling format of the macro definition are ignored in the expansion process. 42-50

The primitive macros provide special built-in expansions that reproduce their primitive texts using the replacement information. These expansions are unique to each primitive macro. The SUM macro creates the sumnation of its text. For example, the expansion of produces X(1)+X(2)+X(4). The REPEAT macro duplicates its text by changing a certain identifier, e.g. the expansion of produces 2(3)-O; 2(4)-O; Z(7)=0;. The SEQN macro produces a sequence of its text and deletes the last character, e.g. the expansion of produces P(2).P(4). The expansion of any primitive macro proceeds as follows: first, all the macro calls appearing within the primitive call are expanded; and finally, the primitive text is reproduced with all its identifiers and/or quoted identifiers that match the index indicator being replaced with the indices, taken one at a time, from the index set. However, if the primitive text contains character strings enclosed in apostrophes, the primitive expansion does not process them but just reproduces them with their surrounding apostrophes dropped and with every two consecutive apostrophes appearing within these strings replaced by one apostrophe. When no index set is supplied in a primitive macro call, the SUM call is replaced by the digit zero and the REPEAT and SEQN call are replaced by their primitive text, unchanged.

51-57

An index set may contain a list of integer numbers, index ranges, macro calls, identifiers, and/or quoted identifiers separated by ampersands. Index numbers that are to be excluded from this list are indicated in the exception option.

58-65

An index range implies a series of integer numbers that start with a low value and increase to a high value by the default increment of one or by the supplied increment.

Textual expansion of chemical process flowsheets into algebraic equation sets guages. The BNF grammar is also instrumental in the syntax-directed translation techniques used for implementing processors of languages (Wirth[l3]). The TEL semantics, the meanings associated with the syntax (grammar), are defined in Table 2. In computer science no widely-used formal method for the semantic description exists. For this paper English descriptions are used for the semantics. Backus-Naur Form (BNF) is a meta-language used to describe the grammar of another language (Wiih[l31). The components of this meta-language are phrase names and words, numbers, and punctuation marks used to compose statements in the language being described. The BNF statements, which are syntactic rules, indicate the order of words, numbers, and punctuation marks in statements of the language being described. For example, the following eight BNF rules describe the English sentence “The black dog eats well.” 1 (sentence) :: = (subject) (predicate). : : = (noun phrase) 2 (subject) 3 (noun phrase): : = (noun) ( (adjective) (noun phrase) 4 (predicate) : : = (verb) (adverb) **=dog 5 (noun) 6 (adjective) i I = The ( black : : = eats 7 (verb) : : = well 8 (adverb) Phrase names are delimited by ‘(’ and ‘),. The symbol ‘: := ’ means “is defined as” and separates a phrase name from its definition. The symbol ‘I’, means “or” and separates alternative definitions of a phrase. These two symbols and the phrase name delimiters are not part of the language being described (in the above example, are not part of English). However, if any of these symbols are part of the language, their appearance in BNF rules is indicated by enclosing them within quotation marks (e.g. ,,(,‘). Two important observations are made about BNF grammar rules (Wirth[13]). First, BNF rules can be defined in a recursive manner; that is, the phrase name can appear on both sides of the symbol ‘: :=‘. In the above example, the noun phrase in Rule 3 is either a noun or an adjective followed by a noun phrase. This recursive property allows an inlinite number of statements to be described by a finite number of BNF rules. Second, a BNF description of a grammar is complete when every phrase name has been defined. In Table 1 all phrases are defined except (identifier), (number), (string), (symbol), and (quoted identifier). These undefined phrases are called primitive phrases, because they are usually the lowest-level phrases detined in the grammars of languages. Their definitions are understood and are given by the semantics in Table 2. As indicated in Tables 1 and 2, a macro command is either a macro definition or a macro call (Rules 1-2). The macro itself is a textual function that, when called, is expanded into specific character strings, called text. It is similar to a mathematical function. However, a mathematical function evaluates an expression, while a macro expands pre-defined text. The expanded text replaces the macro call in much the same way that a numeric result replaces a FORTRAN function call. Macro commands are diflerentiated from the text of equations by the special delimiters ( and ) (Rules 3, 32,

233

and 42). However, macro commands do not appear in the final mathematical model that would be processed by an equation manipulator. When a manipulator scans its input, character by character, and recognizes the symbol (, it would call on the macro processor to operate on the subsequent command. A macro definition is retrieved from the input by the macro processor and stored for later use. A macro call is expanded by the macro processor, and the expanded text replaces the macro command in the input. When the macro processor has completed processing a command, it returns control to the manipulator so it can continue scanning for equations. The macro definition allows the programmer to predefine the text to be associated with a specific macro. This definitjon identifies the user-assigned macro name and describes the macro body of text (Rules 3-6). The definition may also identify a list of dummy arguments called indicators (Rules 7-12). These indicators are keyword identifiers prefixed with dollar signs. They are used in much the same way that dummy variables are used in FORTRAN subprograms. That is, specifk strings of text supplied as arguments in a macro call replace the indicators that appear in the body of the macro definition. The macro body is composed of one or more lines of texts and/or local macro definitions (Rules 13-20). A line may be labeled with an integer number. The macro text may contain indicators, identifiers, numbers, character strings, non-reserved symbols, quoted identifiers, and/or macro calls (Rules 21-29). The real power in using macros is that (1) the argument lists allow the bodies of text to be defined in general terms and (2) they may be nested one within the other, i.e. one macro definition may contain other macro definitions and calls. These powerful features permit rather complicated text to be written in compact, modular form. They are illustrated in the application examples, to be given later, for describing the equations associated with chemical processes. The macro call activates the expansion of either a user-defined macro, one described by a macro definition, or a primitive macro, one whose definition is fixed and built into the macro processor (Rules 30-31). The userdefined macro calls may contain calling arguments that supply texts which are to be substituted in the macro definition (Rules 32-38). Each of these arguments contains a key word and its substitution text (Rules 39-41). The key words that may be used are any of those in the indicator list of the macro definition. The expansion of a user-defined macro proceeds as follows: first, if any of its calling arguments contain macro calls, they are expanded, provided the call is not surrounded by apostrophes; next, the substitution texts of arguments are substituted for their key-word indicators in the macro body; and tinally, the macro body is processed to produce the expanded text which replaces the macro call. During this expansion process, any text that is enclosed by apostrophes is not processed but reproduced with its surrounding apostrophes dropped. Also, those key words in the calling arguments that do not appear in the macro definition are not processed. Finally, the special ‘%DL’ argument may be used to identify those lines in the macro definition that are not processed but deleted from the expanded text. The three primitive macros, SUM, REPEAT and SEQN, provide special built-in expansions that automatically produce the expanded forms of three convenient mathematical notations described earlier. A primitive

M. E. HANYAK

234

macro call contains a macro name, primitive text, and replacements (Rules 424). The replacement indicator appearing in the primitive text is replaced with the indikes, taken one at a time, from the index set (Rules 47-50).The index set (Rules 51-63) may contain ranges of integer numbers, macro calls, identifiers, quoted identifiers,and/or exception lists. Extensive examplesof primitive macro calls are given by Hanyak[7). They primarily illustrate the expansions that occur when the index sets contain identifiers and quoted identifiers or when no index sets are supplied. Using the primitive macros in conjunction with userdetined macros, the text for parts of equations,equations themselves, and sets of equations can be expressed through macro commands. Also, macro definitions can be written for common sets of equations. Finally, a


/*

TBAL

$F

standard library of macro definitionscould be developed for the user of an equation manipulator. This macro library can simplify greatly the input preparation of the equationsto be processed by a manipulator. pRocEssEQuATloN MACROS The SUM, REPEAT and SEQN macros are the foundation from which to construct a library of macros that will produce those algebraic equations associated with chemical flow processes.The macro equivalents of the mathematical equations in Fig. 3 are given in Fii. 4. These macro definitionsare coded in free-format form, and the comments that begin with I* and end with *I provide explanatory informationwhich is ignored by the macro processor. The macro definitionsof Fig. 4, when expanded, will produce the text of equations, similarto

Total Material Balance with no reactions /*

/*

WITH

I = $SI>

=

*/

labels for the incoming streams labels for the outgoing streams total flow term */

/*

= )I &=* $F= ‘FiI)‘;
JR.


$F

WITH

I = $SO>

*/ */

;

END>
CBAL

/*

Component Material Balances with no reactions

%.I=,

/* /* /*

;;;: :: fFZ= ‘F[I)*Z(I,J)‘;

labels for the incoming streams labels for the outgoing streams list of component indices */ component flow term */

/*

l/ */ */


$FZ

WITH

I = $SI>

=


$FZ

WITH

I = $SO>

;

J = $CI>

END>
ZSUM

/*

Composition constraints /* /* /*

K=* $z:"Z(I,J)';

$Z

WITH

*/

list of component indices list of stream labels */ composition term */

J = $CI>

=

1.0;

WITH

*/

I = $SI>

END>
HBAL

/*

Energy Balance

@I=,

/* /*

@:Io'H(I)', $FH

wm

labels labels energy energy

/* /*

;
*/

1 = $sI>

$Qw

=


for the incoming streams l/ for the outgoing streams */ flow term l/ transfer and work term */ SFH

WITH

1 = $so>

;

END>
SBAL

/*

Entropy Balance

$SI=, SSO-,

/*

;



$FS

labels for the incoming streams */ labels for the outgoing streams */ entropy flow term */ entropy transfer and generation term

/*

j;;~,:F(I)*S(I)‘. WITH

I =;$SI>

/*

/* $QG

*/

=


$FS

WITH

I = $SO>

;

*/

Textual expansion of chemical process flowsheets into algebraicequationsets


PPFN

/*

$FN= $CV= $FA= ;;;I

'HVL', 'H(I)',

/*

‘T(I):,

/*

:PjI)

General Physical Property Function

/*

/*

9

/* /*

;;;I $iY.

/*

;
*/

function name */ calculated variable */ first argument l/ second argument */ comna after second argument composition argument */ list of stream labels */

$CV = $FN($FA, $SA$SC $CA);

WITH

235

*I

I = $SI>

END>
/*

VLEQ

Vapor-Liquid Equilibrium Distribution /* /* /*

bVI=, :::rs

/*

S;$‘;;I~$;,J)‘,

I* /*

$ZL= 'Z($LI:J)';
$ZV = $K*$ZL;

WITH

*/

label for the vapor stream */ label for the liquid.stream */ list of component indices */ vapor composition term *I K-value tens *I liquid composition term */

J = $CI>

END>
KVAL

/*

Equilibrium Vaporization Ratios /* /*

frI=* :;;i'::I;{;l

/*

/*

$PA= ‘P(I)‘: $ZA= 'Z(1)';

/*

/*

*/

label for the vapor stream */ list of component indices l/ K-value term */ temperature term */ pressure term */ total vapor-liquid composition term

*/

WITH J = $CI> END>
IS0

/*

Iso-parameters /* /*

f;;:s $AI=: $AV= '$PN(I)', $FV= 'SPN($FI)I;

CACE Vol. 4. No. 4-C

$AV = $FV;

/*

/* /*

WITH

*/ parameter name */ label for the fixed stream */ list of labels for all other streams all-other-parameter term */ fixed-parameter term */

I = $AI>

*/

M. E. HANYAK JR.

236
RTBAL

/*

$F= ‘F(I)‘, $SC= ‘SC(J,L fRC= ‘RC(L)’
Total Mole Balance with chemical reactions

I

/* /* /* /* /*

labels for the incoming streams */ labels for the outgoing streams */ list of component indices */ list of chemical reaction indices */ total flow term */ stoichiometric coefficient term */ reaction constant term */

/*

,

l/

/*

$F WITH I = $SI> +

J = $CI>

WITH

)

L = $RI>

=

;

END>
/*

RCBAL

Component Mole Balances with Chemical Reactions /*

$SI=,


labels for the incoming streams */ labels for the outgoing streams *l list of chemical reaction indices */ list of component indices */ component flow term */ stoichfometrfc coefficient term */ reaction constant term l/

/*

fii=s SCI:: $FZ= 'F(I)*Z(I,J)', $SC= 'SC(J,L)', $RC= 'RC(L)'; $FZ

WITH

/* /* /* /* /*

I = $SI>

*/

+


$SC*$RC

WITH

L = $RI>

=

; WITH J = $CI> END>
MABAL

/*

Molar Atom Balances /*

$SI=, $SO=, %I=,

/*

/*

$EI=,

$F= ‘F(I)‘. $ZA= 'Z(I,J)*A(J,K)';


$F*(



/* /*

/*

*/

labels for the incoming streams labels for the outgoing streams list of component indices */ list of element indices */ total flow term */ atom flow term */

$ZA WITH J = $CI> )

*/ */

WITH 1 = @I> = ) WITH I = $SO> ;

K = $EI>

END>


HSVF

/*

Enthalpy, Entropy, and Vapor Fraction Functions

$SI=;

/*

list of stream labels

1:


FN = HVL,

CV = H(I),

SI = $SI>

2:


FN = SVL,

CV = S(I),

SI = $SI>

3:


FN = VF, 'CV = R(I),

*/

SI = $SI>

END>
/*

STREAMS fSI=, XI=;

Process Stream Equations /* /*

list of stream labels */ list of component indices

1:


CI = $CI,

SI = $SI>

2:


FN = HVL,

CV = H(I),

SI = $SI>

3:


FN = SVL,

CV = S(I),

SI = $SI>

4:


FN =

CV = R(I),

SI = $SI>

VF,

l/

END>

Fig. 4. Equationmacros associated withflowprocesses.

*/

l/

Textualexpansionof chemicalprocess llowsheets intoalgebraicequationsets those in Fii. 2, that are acceptable to an equation manipulator. For example, the component mole balances for a mixer unit that is combiningtwo process streams containing nitrogen,oxygen, and argon can be expressed by the followingmacro call:

237

PROCES3UNlTMACIos

equation macros of Fig. 4 can be used to write the mathematicalmodels of process units found in chemical processes. For example, the mathematicalmodel for the adiabaticcompressionof a process stream given in Fig. 1 is expressed as a macro definitionin Fig. 5. The dummy argumentsin this macro are used to supply the specitic (CBAL SI = Sl & S2, SO- S3, CI = 1 TO 3) labels assignedto the process unit and material streams and the specific list of component indices. The local where Sl and S2 are the inlet stream labels, S3 is the macro SIOis a convenient notation used in the macro outlet stream label, and ‘1 TO 3’ are the component calls that follow it. The 6rst two macro calls represent indices for nitrogen, oxygen, and argon, respectively. the total and component materialbalances;the next two The expanded text of this macro call would be represent the energy and entropy balances; and the ftfth one represents the enthalpy, entropy, and vapor fraction F(Sl)*Z(SI, 1)t F(S2)*Z(S2,1)= F(S3)*Z(S3,1); functions of the two material streams. The s&h and seventh macro calls are for the isentropic outlet conF(Sl)*Z(Sl, 2)t F(S2)*Z(S2,2)= F(S3)*Z(S3,2); ditions, and the last one is for the adiabaticefficiency.In F(Sl)*Z(Sl, 3) t F(S2)*Z(S2,3)= F(S3)*Z(S3,3); the fourth macro call, the characters SG are st&xed with thb process unit label in order to make a unique variable Note that the macro processor will generate array vari- name. The followingmacro call, when expanded, ables with indices that are not integer numbers. Of course, this expansion is only valid if the equation (COMPRE SI=l, SO=2, CI=l&2) manipulatorcan handle this type of indices. In Fig. 4, the key-word arguments in the macro would produce the text of the fourteen equations given definitionsare primarilyused to supplythe labels (i.e. the in Fig. 2. assigned numbers and/or names) of incoming and/or The macro mQde1in Fig. 5 is defined in a general and outgoing material streams and the index numbers assig- flexibleform so as to be usable in many applications.In a ned to chemicalcomponents.Those argumentsthat have similarmanner,general macro models can be definedfor default values primarilyexist so that the standard names other types of process unit operations. A library of for process parameters can be changed. The last two generalmathematicalmodelsand macro modulesis given macro definitionsin Fig. 4, HSVF and STREAMS,are by Hanyak[7j for the followingunit operations: stream convenient for writing the enthalpy, entropy, and vapor splitting and mixing, adiabatic flash vaporization, adifraction functions and the compositionconstraints asso- abatic compression, adiabatic expansion, heating or ciated with process material streams. Macro HSVF is cooling, countercurrent heat exchange, and chemical used primarily in the macros for process units, while reaction. macro STREAMS is used to define the mathematical CRRMICALPROCESS MACROS models of chemical processes. Their specific uses and differences are explained in some examples later. General process unit macros stored in the macro Extensive examplemacro calls and their expandedforms library of the macro processor can serve as the building for the macro definitions of Fig. 4 are given by blocks to write the mathematical models of chemical Hanyak[7]. processes. For example, the flowsheet for the Linde


COMPRE

/*

:::= II' y$: -;

/* /* /* /*


The

Adiabatic Compression

*/

label for the process unit l/ label for the inlet stream */ label for the outlet stream */ list of component indices l/ SI = $SI.

SO = $SO

:i


> , FZ = 'Z(I,J)',

3: 4:


. .

5:


SI = $9

& fSO>

6: 7:


FN=TVL. FN=HVL,

CV=TPfSO, CV=HP$SO,

B:

EtUL/lOO.O=

END>

CI = $CI>

QW = t WS$UL QG = t SG$UL

FA=P(I), SA=S($SI), FA=TP$SO,

(HPbSO - H($SI))/(H($SO) - H($SI));

END> L

FI 5. Macromodel for adiabaticcompression.

SI=$SO> SI=$SO>

M.E.

238

HAM NAK

liquefactionprocess is given in Fig. 6. The mathematical model for this process contains (7Nc+48) equations (Hanyak[fl), where variable Nc is the number of chemical components. AUof these equations are represented very compactlyby the macro definitionnamed LINDE in Fig. 6: The iirst macro definition,NC, in the LINDE,macrois a convenient way of representing the list of component indices. The second, HSVF, is a redefinition of the standardHSVF given in Fig. 4. Its purpose is to suppress the expansion of stream equations in process unit macros. These equations are generated, however, by the special macro, STREAMS, in order to eliminate their duplication in the expanded model. In general, macro STREAMSwrites the composition constraints and the enthalpy, entropy, and vapor fraction functions for the mate& streams of a chemical process. In the LINDE macro, these equations,except for the compositionconstraints, are written for all the pro&s streams in the flowsheet.The composition constraintsare not written because a non-independent set of equations would be

generated.

JR.

In Fig. 6, macro calls are made for each process unit in the chemicalprocess. Their callingargumentssupply the specificlabels assignedto the process units and material streams and the specific list of component indices. This information is given in the chemical process flowsheet. In Fig. 6, the macro LINDE is definedfirst and then it is called. This approach allows the Linde model to be stored possibly in the macro library for future reference. Therefore, to subsequently write this model, only the macro call is needed. From this one macro call, one hundred and eighteen equations are produced automatically by the macro processor. An alternate approach (which is not illustrated)does not use an overall macro definitionfollowed by its macro call. Instead, the macro model would contain just the macro definitionsNC and HSVF, the macro call for STREAMS, and the macro calls for each process unit in the Linde process. This approach is used primarilywhen the mathematicalmodel needs only to be written once. As illustrated by the Linde process, the textual expansion language provides a modular capability in which to express the algebraicequationsassociated with

Process Flowsheet

Assumptions 1. 2. 3. 4. 5.

makeup 1

Q

w,

mix'! 2 -u camp

3

)I

6.

7.

t 0

8.

'I

liquefied gas

steady state no reaction neglect K.E. and P.E. adiabatic stream mixing adiabatic, reversible compression constant-pressure aftercoolinq adiabatic, ideal countercurrent heat exchange adiabatic flash vaporization

L

Linde POL Model
/*

LINDE $CI=;

/*


NC;


HSVF;

1:


2: 3: 4: 5: 6:


Linde Liquefaction Process

List of component indices

CI = SC1 "

END>

SI=l &8. SI=2, SI=3, HI=4. HO=5. FI=5, VO=7,

so=2, so-3, so=4, KI=7. LO=L,

%DL = 1 > > > > KO=8. > >

END>
*/

END>

SI = 1 TO 5 & 7 & 8 & L, UL=M, UL=C, UL-A, UL=E. UL=F,

*/

CI = 1 TO lo>

Fii.6. Macro model for the Lindeprocess.

Textual expansion of chemical processflowsheetsinto algebraicequationsets

the steady-state simulation of chemical processes. Furthermore, expansion modules could be developed for process units such as distillation, absorption, and extraction. Also, this language could be used to write modules for the design and costing of process units. With a standard library of textual expansion modules, all equations for the preliminary steady-state analysis of a chemical process can be expressed just from the information (stream, unit and compound labels) in the chemical flowsheet. Acknowledgements-The author is indebted to the Exxon Education Foundation, the Ford Foundation, and the National Science Foundation (Grant MCS77-03856)for their financial support. I would like to thank Drs. R. L. Klaus, W. D. Seider, D. C. Hyde, W. J. Snyder, J. M. Pommersheim and R. E. Slonaker for their comments and counsel. REEERENCES I. R. L. Motard, M. Shacham & E. M. Rosen, Steady-state chemical process simulation. AIChEJ. 21,417-436 (1975). 2. V. Hlavacek, Analysis of a complex plant: Steady-state and transient behavior. C&ChE 1,75-100 (1977). 3. PACEJ? 245 User Manual. Digital Systems Corp., Hanover, N. H. (1971).

239

4. J. D. Seader, W. D. Seider & A. C. Pauls, PLGWTRAN Simulation-An Inhduch. CACHE Committee. Ulrich’s Bookstore, Ann Arbor, Mich. (1974). 5. R S. H. Mah & hf. Rafal, Automatic program generation in chemical engineering computation. Trans. Znstn. Chem. Engrs. 49, 101-108(1971). 6. S. Soylemez & W. D. Seider, A new technique for procedence ordering chemical process equation sets. AJChE 1. 19,934-942 (1973). 7. M. E. Hanyak, The application of problem-oriented languages and computerized translators to the solution of engineering problems. Ph.D. Thesis. University of Pennsylvania, Philadelphia, PA (1976). 8. ANSI. Americ& National Standard ProgrammingL.anguage PLII. (ANS X3.53-1976).American National Standards Institiie,.New York (1976). 9. W. M. McKeeman, J. J. Horning & D. B. Wortman, A Compi/er Generator. Prentice-Hall, Englewood Cl&., N.J. (1970). IO. k. Vi Aho & J. D. Ullmann, Principles of Compiler Design. Addison-Wesley, Reading, Mass. (1971). Il. D. Gries, Compiler Construction for Digital Computers. Wiley, New York (1971). 12. P. Naur, Revised report on the algorithmic language ALGOL 60. C ACM 6, l-17 (1%3). 13. N. Wiih, Algorithmst Dafa Skuctures = Programs. Prentice-Hall, Englewood Cliffs, N.J. (1976).