Recursive Techniques in Property Information Retrieval and


Recursive Techniques in Property Information Retrieval and...

1 downloads 98 Views 682KB Size

Chapter 6 Recursive Techniques i n Property Information Retrieval a n d Calculation for Computer-Aided Formulation R. Albrecht-Mallinger and E. H. Givens

Downloaded by RUTGERS UNIV on December 28, 2017 | http://pubs.acs.org Publication Date: August 29, 1989 | doi: 10.1021/bk-1989-0404.ch006

Automotive Research Laboratory, The Sherwin-Williams Company, Chicago, IL 60628 Software to predict the properties of formulated products is made more powerful by a recursive procedure which can use formulas stored in files as raw materials. Particular care must be taken with program flow control and data structures for the recursion to be effective. This paper illustrates these issues using an example derived from a working formulation system for coatings development. INTRODUCTION: FORMULATED PRODUCTS USED AS RAW MATERIALS Polymer and coating chemists use computer models to predict the properties of formulated products from the characteristics of the raw materials and processing conditions (I, 2). Usually, the chemist supplies the identification and amounts of the materials. The software retrieves raw material property data needed for the modelling calculations from a raw material database. However, the chemist often works with groups of materials that are used as a unit. For instance, intermediates used in multiple products or premixes are themselves formulated products, not raw materials in the sense of being purchased or basic chemical species. Also, some ingredients are often used in constant ratio. In these cases, experimentation and calculation are simplified if the chemist can refer to these sets of materials as a unit, even though the unit may not be part of the raw material database. A recursive modelling procedure will let the chemist use formulated products as a raw materials. In this paper, we illustrate this use of recursion, and provide a working example that highlights some of the complexities encountered when using this technique. The key is being able to calculate and return the same type of data for a formula as is normally retrieved from a raw material database. A simple example is material cost: knowing the cost and concentration of each raw material in a formula, the material cost of the formula is easily calculated. A slightly more complex example is calculating specific gravity and solids from the raw material properties and processing conditions of a reacted product. frEgVRgiQN Recursion, when used in the context of computer programs, refers to a procedure that calls itself as a subprocedure. The classic examples cited in programming texts (3, 4) are computation of the factorial function and 0097-6156/89/0404-0054$06.00/0 © 1989 American Chemical Society

Provder; Computer Applications in Applied Polymer Science II ACS Symposium Series; American Chemical Society: Washington, DC, 1989.

Downloaded by RUTGERS UNIV on December 28, 2017 | http://pubs.acs.org Publication Date: August 29, 1989 | doi: 10.1021/bk-1989-0404.ch006

6. ALBRECHT-MALLINGER & GIVENS

Information Retrieval and Calculation

Fibonacci numbers, where the number sought is the nth number in a series defined in terms of relationships to the n-1, n-2, etc. members of the series. All recursive procedures must have a terminating condition, so that they do not call themselves endlessly. In our case, we define a formula as consisting of raw materials and/or other formulas. We develop a modelling procedure we call to determine formula characteristics from raw material properties, and give it the recursive property of being able to call itself when it encounters a formula used as a raw material. The procedures terminate when all formulas are resolved into basic raw materials. This terminating condition cannot be met if any formula contains a reference to itself, either directly or indirectly, through another formula. Figure 1 shows a flow chart for part of a recursive modelling procedure, illustrated in this paper, which accepts as input a formula consisting of constituent raw material codes or formula names, and quantities. The procedure retrieves property data for each raw material in order to perform the required calculations. When the procedure encounters a constituent that is a formulated product, it calls itself using that product as input. The output of the procedure consists of the calculated properties of the formula, including those properties of the formula that would be retrieved from data files for non-formulated or purchased raw materials. By returning this latter set of properties, the procedure can treat formulas as raw materials. Recursion is not necessary in a formula modelling system if the only material inputs have the necessary characteristics available in a raw material database. The flow chart in Figure 2 shows how the retrieval process occurs in this case. Compare this with the flow chart in Figure 1. PROGRAMMING ISSUES Recursive procedures demand special attention to flow control and data structures. For instance, the flow control within the procedure must correctly handle an error, say missing file information or inconsistent data, that is discovered several iterations deep. Should the procedure break and return to the previous level only? Should it force return to the level of the original invocation of the procedure? Should it allow an interactive user a choice of supplying missing data, and if so, on any level of iteration? If files are opened within the procedures, should they be closed when a recursive call is needed, or are new channel numbers to be requested, using up system resources? These and other issues about flow control become critical as real-world features to enhance ease of use are added to a system, features we do not illustrate in our example. We will instead focus on complications of the data structure that are evident even in our simple procedure. In a non-recursive modelling program, one that permits only materials for which raw material type data is available, the data structure and naming of variables is straightforward. For instance, consider this table summarizing types of data for a modelling procedure that calculates the pigment-to-binder ratio (PBR) of a coating. Source of formula and raw material data for a non-recursive modelling procedure Formula characteristics Retrieved from database Calculated in program

No. Yes. (eg. pigment-tobinder ratio)

Raw material characteristics Yes. (eg. solids, cost) No.

Provder; Computer Applications in Applied Polymer Science II ACS Symposium Series; American Chemical Society: Washington, DC, 1989.

55

56

COMPUTER APPLICATIONS IN APPLIED POLYMER SCIENCE II

Downloaded by RUTGERS UNIV on December 28, 2017 | http://pubs.acs.org Publication Date: August 29, 1989 | doi: 10.1021/bk-1989-0404.ch006

GET C0DE(1..N), QUANTITY!1..N), N>=1 FOR FORMULA

±

,

RETRIEVE RAW MATERIAL DATA FOR CODE(ROW) FROM DATABASE

±

i

1 CALL THIS PROCEDURE WITH I FORMULA = CODE(ROW). RETURN j DATA NEEDED FOR CALCULATIONS

!~ROW = ROW + 1

I

yes CACULCATE FORMULA PROPERTIES FROM RAW MATERIAL DATA. ALSO, CALCULATE DATA NEEDED TO TREAT FORMULA AS RAW MATERIAL. Figure 1: Flow chart of recursive procedure, accepting both raw material and formulas as inputs.

Provder; Computer Applications in Applied Polymer Science II ACS Symposium Series; American Chemical Society: Washington, DC, 1989.

Information Retrieval and Calculation

Downloaded by RUTGERS UNIV on December 28, 2017 | http://pubs.acs.org Publication Date: August 29, 1989 | doi: 10.1021/bk-1989-0404.ch006

ALBRECHT-MALLINGER & GIVENS

I GET C0DE(1..N), QUANTITY(1..N), N>=li

I SET ROW = 1 i

<

)

I RETRIEVE RAW MATERIAL DATA FOR CODE(ROW)! i j

j ROW =

ROW + 1

j

yes CACULCATE FORMULA PROPERTIES FROM RAW MATERIAL DATA Figure 2: Flow chart of simple procedure to calculate formula properties.

Provder; Computer Applications in Applied Polymer Science II ACS Symposium Series; American Chemical Society: Washington, DC, 1989.

57

58

COMPUTER APPLICATIONS IN APPLIED POLYMER SCIENCE II

Downloaded by RUTGERS UNIV on December 28, 2017 | http://pubs.acs.org Publication Date: August 29, 1989 | doi: 10.1021/bk-1989-0404.ch006

By contrast, in a recursive modelling procedure the program designer must distinguish between variables used in different invocations of the procedure for a single concept, such as a characteristic value of a formula. For instance, the formula entered by the user has a PBR, and so will any intermediates used in the formula. Thus, the recursive call to calculate characteristic values of the intermediate will return a PBR in a variable which must have a different name than the variable used for the current formula's PBR. The table below illustrates these issues by comparing how a recursive subroutine must handle data which is available from a database, such as the cost of a raw material, data that is calculated for the formulated product, such as PBR, and data for intermediate products. (The variable names shown in the table are part of the example procedure given in the appendix.) Compare with the previous table for a non-recursive modelling procedure's data structure. Source and use of data within recursive modelling procedure Examples of variable names shown in ITALICS "Formula characteristics" Pigment/Binder Ratio

"Raw material data" Cost, solids, densitv. etc.

Retrieved from raw material database

No.

Values for rows containing true raw mat­ erials retrieved from database. (Row__soiids(i) Row__cost(i))

Received from recursive call

Ignored. (Pass___pigtnent_Values for intermediate formula (Pass_cost) calculated in recursive binder__ratio) call and used as would be values retrieved from database. Values for rows within intermediate formula (Pass row cost(L.Pass nrows))i gn o re d.

Calculated within the procedure

t

Passed back to the calling procedure, (Pigment_binder_ratio)

Composite formula values (Solids, Cost) calculated from row data, concentration.

A potentially confusing aspect of recursive formula calculations is that the procedure often "throws away" calculated data for intermediates. Consider that normally the models predict other properties for formulated products beyond those which are stored in raw material databases. For instance, the packing factor of a particulate blend may be predicted, even though packing factor is not part of raw material database. (Obviously, supporting data about particle size distribution and adsorption properties must be.) The packing factor is calculated and returned by the modelling procedure and made available to the chemist. However, the packing factors of any intermediates are ignored by the procedure when it calls itself to get the intermediates' properties; only the particulate characteristics, those normally retrieved from a raw material database, are required. In a polycondensation modelling system that draws on a database containing the number of acid and base groups per molecule of monomer, a recursive procedure must return the calculated acid and base group concentration for the polymer. It may also return such items as solubility parameter, even though these will be ignored at all but the top level call to the procedure.

Provder; Computer Applications in Applied Polymer Science II ACS Symposium Series; American Chemical Society: Washington, DC, 1989.

6. ALBRECHT-MALLINGER & GFVENS

Information Retrieval and Calculation

Downloaded by RUTGERS UNIV on December 28, 2017 | http://pubs.acs.org Publication Date: August 29, 1989 | doi: 10.1021/bk-1989-0404.ch006

AN EXAMPLE The appendix shows an example of a recursive procedure to calculate the pigment to binder ratio for a non-reactive coating formulation. We assume that a raw material data base contains raw material code, density, cost per unit weight, solids volume fraction, and pigment solids volume fraction. Formulas are stored in files that contain the identification and amount of each constituent. Finally, there are procedures, Raw Material Lookup and Formula_File Read can read records from these files. The recursive procedure, named Formula Lookup, accepts the name of the formula file as input. It returns the pigment to binder ratio for the coating, along with the code, density, cost, solids and pigment fraction for each constituent and for the coating itself. The appendix begins with a main procedure which asks the user for the name of a formula file, calls the recursive procedure Formula Lookup, and upon successful return displays the results of the calculations. The recursive procedure follows. Data is passed between levels of recursive procedures in variables cited in the C A L L statement for the procedure. As indicated in the section on programming issues, care must be taken in assigning names to the data variables so that values returning from a recursive C A L L do not overwrite the values in variables within the calling procedure. Note the difference between the variable names in the SUBROUTINE statement of Formula Lookup (first statement) and the names used in the internal C A L L (at /*Ref#l*/ in the appendix). For instance, while Pigment Binder Ratio is calculated and returned in the C A L L statement, within the program the variable name Pass__Pigment Binder__Ratio is used to receive the PBR of a formula used within a formula. AN EXTENSION: LISTING ALL COMPONENTS Imagine wanting to construct a list of all raw materials involved in a formula containing formulated intermediates. Such a list is a pair of arrays showing all basic raw material codes and amounts. For instance, if a formula contains components A and B, where B is a raw material and A is a formulated intermediate containing raw materials X, Y and Z, then the list would contain data for X, Y, Z and B. In this case, rather than keeping separate data areas for each call to the procedure, we must create a data structure that can be shared by all occurrences. FORTRAN'S COMMON can be used for this purpose. To implement the list in our example, make the following changes to the pseudo-code in the appendix: 1. Declare Nrow_exp, Code exp (n), Weight__exp (n) as COMMON to both the main program and subroutine, where n is some appropriately large number. 2. Set Nrow exp to zero just before calling Formula lookup from the main procedure, to reset the list to show no materials. 3. Insert the following code at /*Ref#2*/: Last exp = Nrow exp This points to the last raw material preceding a group returned from a recursive call. The weights of this group must be adjusted based on the concentration of the intermediate in the current formula. 4. Insert the following code at /*Ref#3*/: Nrow exp = Nrow exp+1 Code__exp(Nrow__exp) « Code(Row) Weight__exp(Nrow_exp) = Weight(Row)

Provder; Computer Applications in Applied Polymer Science II ACS Symposium Series; American Chemical Society: Washington, DC, 1989.

59

60

COMPUTER APPLICATIONS IN APPLIED POLYMER SCIENCE II

Downloaded by RUTGERS UNIV on December 28, 2017 | http://pubs.acs.org Publication Date: August 29, 1989 | doi: 10.1021/bk-1989-0404.ch006

This appends the code and weight of the current row of the current formula to the list. 5. Insert the following code at /*Ref#4*/: Temp weight = 0. DO I = Last exp+1, Nrow exp Temp weight = Temp weight+weight exp(I) END DO Exp_factor = Wt(Row)/Temp__weight DO I = Last exp+1, Nrow exp Weight_exp(I) = Weight_exp(I) *Exp_factor END DO This pair of loops adjusts the weight of a group of materials returned from a recursive call so that the weights total to the weight of the intermediate used in the current formula. CONCLUSION Recursive techniques have been in use since the early days of digital computers. However, we have seen little use of this technique in formula calculations. Implementation requires the ability to calculate and return the same type of data for a formula as is normally retrieved from a raw material database. We have outlined some of the difficulties in using recursion. None of them present insurmountable difficulties if the programming environment being used supports recursion. The benefit of implementation is in the power of the software to simplify experimentation and formula development. The procedure illustrated here, besides containing only trivial technical calculations, lacks important features that are required in production programs. Extensive error checking and recovery must be performed. The procedure must detect the occurrence of a self-referential system of formulas, which would result in attempting endless recursive calls. Access to multiple raw material and formula databases adds power to the program, but must be implemented by complex code to allow flexible control of that access. The structural and input/output statements to support these features may greatly exceed the number of statements that perform modelling calculations.

LITERATURE CITED 1. Nelen, P., Proceedings Of The ACS Division Of Polymeric Material: Science And Engineering. Vol. 58, p. 811. 2. Kaelble, D. H., Computer-Aided Design Of Polymers AndComposites,M. Dekker, New York, NY (1985). 3. Sedgewick, R., Algorithms - 2d ed. Addison-Wesley, Reading, MA (1988). 4. Wirth, N., Algorithms + Data Structures = Programs. Prentice-Hall, Englewood Cliffs, NJ (1976). APPENDIX: EXAMPLE OF RECURSIVE LOOKUP PROCEDURE PROGRAM Master CHARACTER Formula_file__name REAL Density, Solids, Pigment_solids, Cost, Pigment_binder_ratio REAL Code(10), Weight(lO), Row__density(10), Row_solids(10), Row__pigment__solids(10), Row_cost(10) INTEGER Nrows,Error_Code READ Formula file name CALL Formula___lookup (Formula file_name, Density, Solids, Pigment_solids, Cost, Pigment binder ratio, Code, Weight, Row density, Row_solids, Row pigment solids, Row_Cost, N rows, Error code) IF (Error__code = 0) DISPLAY ...Show Formula...

Provder; Computer Applications in Applied Polymer Science II ACS Symposium Series; American Chemical Society: Washington, DC, 1989.

6. ALBRECHT-MALLINGER & GIVENS

Information Retrieval and Calculation 61

Downloaded by RUTGERS UNIV on December 28, 2017 | http://pubs.acs.org Publication Date: August 29, 1989 | doi: 10.1021/bk-1989-0404.ch006

STOP END PROCEDURE Formula lookup (Formula file name, Density, Solids, Pigment solids, Cost, Pigment binder ratio, Code, Weight, Row__density, Row solids, Row pigment solids, Row_cost, N rows, Error code) /* Variables Row found and Row control the flow of execution of the lookup procedure and calculations within this procedure */ INTEGER Row LOGICAL Row__found /* These variables are used to store items that are either passed into or calculated within this procedure. Each variable has a corresponding variable defined further below used to exchange information with a recursive call */ CHARACTER Formula__file__name, REAL Density, Solids, Pigment__solids, Cost REAL Pigment__binder ratio, Row_density(10), Row_solids(10), Row___pigment__solids(10), Row_cost(10) INTEGER Nrows /* These variables are used to store items that are sent to and returned from a recursive call to this procedure. Note the one-to-one correspondence with the variables defined above */ CHARACTER Pass_formula__file__name REAL Pass density, Pass solids, Pass pigment solids, Pass cost REAL Pass pigment_binder__ratio,Pass code(10), Pass__weight(10), Pass row_solids(10), Pass_Row pigment solids(10),Pass row_cost(10),Pass_row__cost(10) INTEGER Pass_nrows /* Variable Error code passes back information about the success or failure of operations within this procedure and returned from any called procedures */ INTEGER Error_code /* Read Formula.*/ CALL Formula file read (Formula file name, Code, Weight, N rows, Error code) IF (Error_code. Ne.O) RETURN /* Begin Search.*/ Row = 0 DO WHILE (Row.lt.n_rows) Row = Row+1; Row__found = .False. DO WHILE (Row__found.eqv..false) /* Try Raw Material Files First.*/ /*Ref#2*/ CALL Raw material lookup Code (Row), Row_density(Row), Solids(Row), Row pigment solids(Row), Row_cost(Row), Error Code) IF (Error_code.eq.O) THEN Row__found = .True. /*Ref#4*/ ELSE IF (Error_code.eq.-l) THEN /* If Not Found in Raw Material File, Search Formula Files.*/ Pass__formula__file__name = Code (Row) CALL Formula_lookup (Pass formula_file name,Row_density(Row), Row solids(Row), Row pigment solids(Row), Row__cost (Row),Pass_ pigment binder ratio,Pass code, Pass__weight, Pass row density, Pass row_solids, Pass row pigment solids, Pass row cost, Pass n rows, Error Code) /*Ref#l*/ IF (Error_Code.eq.O) THEN Row_found = .True. /*Ref#3*/ END IF END IF IF (Error_code.gt.O) RETURN IF (Row__found.eqv..false.) ...Get Correction from User... END DO END DO

Provder; Computer Applications in Applied Polymer Science II ACS Symposium Series; American Chemical Society: Washington, DC, 1989.

Downloaded by RUTGERS UNIV on December 28, 2017 | http://pubs.acs.org Publication Date: August 29, 1989 | doi: 10.1021/bk-1989-0404.ch006

COMPUTER

APPLICATIONS IN APPLIED POLYMER

SCIENCE

/* Calculate Formula Characteristics.*/ Total_pigment = 0.; Total_solids = 0.; Total__volume = 0.; Total__weight = 0.; Total_cost = 0. DO I = l,N_rows Total_weight = Total__weight + Weight(Row) Temp vol = Weight (Row)/Row density(Row) Total volume = Total volume + Temp vol Total_solids = Total_solids + Temp__vol*Row_solids(Row) Total pigment = Total_pigment + Temp vol *Row pigment_solids(Row) Total_cost = Total__cost + Weight(Row)*Row__cost(Row) END DO Density = Total_weight/Total volume SoUds = Total solids/Total volume Pigment solids = Total pigment/Total volume Cost = Total_cost/Total__weight Pigment binder ratio = Total pigment/(Total solids-total pigment) RETURN END RECEIVED

February 14,1989

Provder; Computer Applications in Applied Polymer Science II ACS Symposium Series; American Chemical Society: Washington, DC, 1989.