using macsyma to write fortran subroutines

4
a. ~ymbolicComputation(1986)2,213-216 UsingMACSYMAtoWriteFORTRANSubroutinest STANLYSTEINBERG DepartmentofMathematicsandStatistics, University ofNewMexico,AlbuquerqueNM87131,U .S.A . PATRICKJ .ROACHE EcodynarnicsResearchAssociates,Inc ., P.O .Box8172,AlbuquerqueNM87198,U.S .A . (Received2January1986) 1 .Introduction Thisletterdescribessomeongoingworkthatusesthesymbolmanipulator MACSYMA to write FORTRAN subroutineswhichareincorporatedintoalargefinitedifferencecode that isusedtosolveboundaryvalueproblemsforellipticpartialdifferential equations .The boundaryvalueproblemsmodelthesteadystateofcertainphysicaldevices suchasthe electricfieldinalasercavityortheflowaroundabladeinaturbine . Thedevicesthat interestusarethosethatoccupyafairlycomplicatedregionin three-dimensionalspace or,atbest,havesufficientsymmetrysothattheycanbemodelled usingacomplicated two-dimensionalregion .Thepartialdifferentialequations thatareusedtomodelthe physicsofthedevicearecalledthehostedequations .Thehostedequations tendtobe rathersimplenon-linearellipticequations .However,ourmethodswillapply tothemost complicatedhostedequations .Theproblemiscomplete whenappropriateboundary conditionsarespecified :Theseproblemsare difficult tosolvebecauseofthecomplicated geometryoftheregion . 2 .OverviewoftheProblem Severalimportantapproachestosolvingsuchproblemsarebasedonfinding achange ofcoordinatesthatmapsthegivenphysicalregionintoarectangular regioninlogical space,Suchacoordinatechangethengenerates agridinthephysicalregionthat correspondstoarectangulargridinlogicalspace .Thisapproachnow hasalonghistory thatisreferencedinthepaperslistedbelow .Oncesuchatransformationhas beenfound, thehostedequationandtheboundaryconditionsaretransformedtothenew coordinate systemandthenstandardfinitedifferencemethodsareapplied tosolvetheproblem . Thesenumericalmethodsaredescribedinourpapers .Whatwewilldescribehere ishow asymbolmanipulatorisusedtogeneratethe FORTRAN subroutinesthatareusedbythe finitedifferencecodes . Theregionsthatinterestusaresocomplicatedthatitisoftenimpracticalorimpossible tofindananalyticchangeofcoordinatesthattransformsthe regiontoarectangular region .Historically,practitionersinthisareahaveusedfinite differenceapproximations tWorksupportedpartiallybytheU .S .ArmyResearch Office andtheU .S .AirForceOfficeofScientific Research . 0747-7171/86/020213+04$03 .00/0 OL . 1986AcademicPressInc.(London)Ltd .

Upload: stanly-steinberg

Post on 04-Jul-2016

216 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Using MACSYMA to write FORTRAN subroutines

a. ~ymbolic Computation (1986) 2, 213-216

Using MACSYMA to Write FORTRAN Subroutines t

STANLY STEINBERGDepartment of Mathematics and Statistics,

University of New Mexico, Albuquerque NM87131, U.S.A .

PATRICK J. ROACHEEcodynarnics Research Associates, Inc .,

P.O. Box 8172, Albuquerque NM 87198, U.S.A .

(Received 2 January 1986)

1. Introduction

This letter describes some ongoing work that uses the symbol manipulator MACSYMA towrite FORTRAN subroutines which are incorporated into a large finite difference code thatis used to solve boundary value problems for elliptic partial differential equations. Theboundary value problems model the steady state of certain physical devices such as theelectric field in a laser cavity or the flow around a blade in a turbine . The devices thatinterest us are those that occupy a fairly complicated region in three-dimensional spaceor, at best, have sufficient symmetry so that they can be modelled using a complicatedtwo-dimensional region . The partial differential equations that are used to model thephysics of the device are called the hosted equations. The hosted equations tend to berather simple non-linear elliptic equations . However, our methods will apply to the mostcomplicated hosted equations . The problem is complete when appropriate boundaryconditions are specified: These problems are difficult to solve because of the complicatedgeometry of the region .

2. Overview of the Problem

Several important approaches to solving such problems are based on finding a changeof coordinates that maps the given physical region into a rectangular region in logicalspace, Such a coordinate change then generates a grid in the physical region thatcorresponds to a rectangular grid in logical space . This approach now has a long historythat is referenced in the papers listed below . Once such a transformation has been found,the hosted equation and the boundary conditions are transformed to the new coordinatesystem and then standard finite difference methods are applied to solve the problem .These numerical methods are described in our papers . What we will describe here is howa symbol manipulator is used to generate the FORTRAN subroutines that are used by thefinite difference codes .

The regions that interest us are so complicated that it is often impractical or impossibleto find an analytic change of coordinates that transforms the region to a rectangularregion. Historically, practitioners in this area have used finite difference approximations

t Work supported partially by the U .S . Army Research Office and the U .S . Air Force Office of ScientificResearch .

0747-7171/86/020213+04 $03 .00/0

OL . 1986 Academic Press Inc. (London) Ltd .

Page 2: Using MACSYMA to write FORTRAN subroutines

214

S. Steinberg and P . J . Roache

to a system of elliptic equations to numerically compute a coordinate change . Except inthe simplest of cases, the equations used are considered rather ad hoc and the parametersused to control the grid in the physical region have no clear geometric meaning . In spiteof this, these methods have been used to solve many important problems . Some of ourearly work has been based on these ideas .

More recently, variational methods have been introduced as a way of determining thetransformations. The idea is to require the transformation to minimise a linearcombination of integrals where each integral is used to control a geometric property ofthe grid. Integrals that control the smoothness of the grid spacing, volumes of the gridcells, and the angles between the grid lines are now routinely used . In some simple case,the variational methods are closely related to the elliptic equation methods . Our currentwork is based on what we believe to be an improved variational formulation .

3. The Need for Computer Symbol Manipulation

The need to use a symbol manipulator arises at several points . First, since thetransformation is not known analytically, the hosted equations must be transformed to ageneral, not necessarily orthogonal, coordinate system . To indicate the complexity of thisproblem we note that the Laplacian in three dimensions, when transformed to a generalcoordinate frame, contains 1611 terms in fully expanded form, This is the simplestequation of interest! It turns out to be important, from an efficiency point of view, not touse the fully expanded form . In any case, MACSYMA is used to compute the transformedhosted equation . Then MACSYMA is used to compute the finite difference form of thehosted equations . MACSYMA then uses this information to write a FORTRAN subroutine thatdescribes the finite difference form of the hosted equations . The variational methodsproduce differential equations, similar to the hosted equation, for determining the grid inthe physical region . MACSYMA is also used to compute the finite difference form of theseequations and then write a subroutine that describes these equations . These subroutinesare combined with a finite difference equation solver, resulting in a code that can be usedto solve the hosted equation .

4. The Symbol Code

When we want to create a code for studying some physical model, we must first decidewhat variational integrals will be used to determine the grids. This decision requires someunderstanding of the physical problem and experience with the existing grid generationcodes. Once this decision has been made the computer takes over . The integrand of thevariational integral is given to a set of MACSYMA programs that converts the integral intothe subroutine required by the elliptic equation solver . The elliptic equation solver isrecompiled to incorporate the new subroutine. The numerical code is then ready toproduce plots of both the grid and the solutions of the hosted equations ; the results of thenumerical computations are then used to make decisions about the design of the physicaldevice .

Let us be a bit more precise about what the symbol code does . The MACSYMA coderequires as input the dimensions of logical and physical space, two lists of variables, one tobe used in logical space and one to be used in physical space, and the integrand of thevariational integral . The integrand can be any function of the logical variables, thephysical variables, and the first derivatives of the physical variables with respect to thelogical variables . The output of the MACSYMA code is a complete FORTRAN subroutine,

Page 3: Using MACSYMA to write FORTRAN subroutines

Using MACSYMA to Write FORTRAN Subroutines

215

The MACSYMA code contains several steps . The first step is to calculate the Eulerequations for the integral . This is done by taking functional derivatives of the integrandand this, in turn, involves several integrations by parts . The Euler equations, which aretypically second order partial differential equations, are then converted to finite differenceform, typically by using centred second-order differences . The coefficients of the finitedifference equations contain expressions that involve the coordinate transformation andits first derivatives which must be transformed into suitable FORTRAN notation . As thesecomputations proceed, MACSYMA collects the information needed to write the FORTRANsubroutine. In the final step, MACSYMA writes the subroutine; to assist anyone daringenough to try to read the subroutine, MACSYMA produces a file containing all formulaswritten in a two-dimensional format that is easier to read than the FORTRAN code .

5. Discussion

We may have made all this seem a bit easier than it really is . If we introduce a newvariational integral, then we expect to have to change both the MACSYMA code and theelliptic equation solver to accommodate the new information . However, these changes areusually minor . Recently we decided a new variational integral showed some promise ofbeing simpler than one of the integrals that we were using and still being capable ofcontrolling the property that interested us . Less than 24 hours later, we were presenting,in a local applied mathematics seminar, graphs of grids generated using the new integral .Incidentally, the idea worked! In fact, the MACSYMA run was straightforward and much ofthe time was used in transferring the subroutine over telephone lines and fixing up thesolver so that it would run on a small workstation . Without the symbol manipulationtechnology this project would have taken several weeks .The symbol manipulation technology is essential because the hosted and Euler

equations can be very complicated . In the case where the variational integral is used tocontrol grid spacing in two dimensions, the Euler equations can be easily derived by handand the resulting subroutine can be easily hand coded . Both the algebra and coding forthe integral that controls the angle between grid lines in three-dimensional space areprobably impossible to do by hand .The MACSYMA code being used is version 309 .1 released by Symbolics, Inc . It is being

run on a VAX 11/780 running UNIX with the Berkeley 4 .2 enhancements . The machinehas 4 megabytes of main memory . The maximum virtual job size is 6 megabytes, 3 . 3 ofwhich is used for the MACSYMA object code leaving about 2 .7 megabytes of data space .Short jobs are run interactively while longer jobs are run as low priority backgroundprocesses. The machine is reasonable loaded during the day and lightly loaded at night .We have experienced no difficulty in running MACSYMA under these conditions .

Here is some data on running various jobs . The problem name tells what property ofthe grid is being controlled, the dimension variable is the geometric dimension of bothlogical and physical space, the cpu time is given in minutes and seconds, and the memoryvariable is the number of 1024 byte blocks of virtual memory used .

Problem Dimension cpu time Blocks

Smoothness 1 1 :20 3296Smoothness 2 2 :27 3310Smoothness 3 4 :37 3348Volume 2 4 : 34 3343Volume 3 50 :28 3607

Page 4: Using MACSYMA to write FORTRAN subroutines

216

S. Steinberg and P . J. Roache

The previous data is for the simplest possible versions of the variational integrals . Thesmoothness control is particularly simple in our formulation . The volume control issubstantially more complex . We have computed with several generalisations of theseproperties; some of the more interesting examples use many cpu hours and causeMACSYMA to run out of memory. We could increase the virtual memory allocated toMACSYMA but the resulting FORTRAN subroutines would be very complicated andconsequently would run slowly . We prefer to reformulate the mathematics and theMACSYMA code so that the resulting FORTRAN code can be run on large microcomputers .We note that we do not describe these problems as intermediate expression swell ; theformulation of the variational integrals can be done in a few lines, so the input to theproblems is small. However, the resulting codes are at least as large as any intermediateexpression; we do not have the results of our computations simplifying to expressionsthat are much smaller than the largest intermediate expression .

Our ongoing research is devoted to several problems : finding new variational integralsthat will control important properties of the grid, generalising the MACSYMA code so that itwill handle all integrals that may be important, reorganising the way in which theMACSYMA code generates its formulas so that we do not get such large expressions,optimising the FORTRAN code so that it is both stable and efficient, and working out thebasic existence and uniqueness theory for, at least, the most commonly used variationalformulations .

BibliographyCastillo, J . E., Steinberg, S ., Roache, P . J . (1985) . On the folding of numerically generated grids . Appl. Math .

Comput . (submitted).Eiseman, P . R . (1985) . Grid generation for fluid mechanics computations . Ann. Rev. Fluid Mech . 17, 487-522 .Harten, L . P . (1985) . US1OgMAcSYMA to generate (somewhat) optimized FORTRANcode . The 1984 MACSYMA Users'

Conference, General Electric Research and Development Center, Schenectady, New York, July 1984 .Roache, P . J ., Steinberg, S ., Happ, H . J ., Money, W. M. (1983) . 3D electric field solutions in boundary fitted

coordinates . In : Proceedings of the 4th IEEE Pulsed Power Conference . Albuquerque, NM .Roache, P . J ., Steinberg, S . (1983) . Symbolic manipulation and compuational fluid dynamics, Proceedings of

AIAA 6th Computational Fluid Dynamics Conference . AIAA, New York, Paper 83-1952 .Roache, P . J ., Money, W . M ., Steinberg, S . (1984) . Interactive electric field calculations for lasers . In : AIAA

17th Fluid Dynamics, Plasma Dynamics and Lasers Conference . Snowmass, CO ., pp . 25-27 .Roache, P . J ., Steinberg, S . (1984) . Symbolic manipulation and computational fluid dynamics . AIAA J. 22,

1390-1394.Roache, P . J., Steinberg, S . (1985) . A new approach to grid generation using a variational formulation .

Proceedings of the AIAA 7th Computational Fluid Dynamics Conference . Cincinnati, Ohio .Steinberg, S ., Roache, P . J . (1985) . Using VAXIMA to write FORTRAN code . The 1984 MACSYMA Users'

Conference, General Electric Research and Development Center, Schenectady, New York, July 1984 .(Also appeared in : Applications of Computer Algebra, edited by R. Pavelle, Kluwer, 1985, pp . 74-93 .)

Steinberg, S ., Roache, P. J. (1985) . Symbolic manipulation and computational fluid dynamics . J. Comput. Phys.57, 251-284 .

Steinberg, S ., Roache, P . J. (1985). Using MACSYMA to write FORTRAN subroutines (letter) . MACSYMA Newsletter 11-2, 10-12 .

Steinberg, S ., Roache, P . J . (1986) . Variation grid generation . Numerical Methods for Partial DifferentialEquations 2, 71-96 .

Wang, P. S ., Chang, T . Y . P ., van Hulzen, J . A . (1984). Code generation and optimization for finite element analysis .Proceedings ofEUROSAM84 . Lecture Notes in Computer Science, pp . 237-247 . New York : Springer-Verlag .