How to Write Fortran Expressions
Not sure how to use Fortran? This topic explains the basics of writing code to perform simple calculations in Fortran. For a complete example of specifying a Calculator block and writing the Fortran code for it, see Defining a Calculator Block in the Modeling Coal Combustion example.
Fortran used in Aspen Plus is limited to Fortran 77 syntax, which the following sections describe. Fortran variable names and function names are not case sensitive; PRES, Pres, and pres all refer to the same variable.(函数名和变量名不区分大小。)
No Fortran Compiler Necessary
All of the syntax described on this page can be interpreted, which means that you do not need to have a Fortran compiler installed to use it. Some other Fortran statements can also be interpreted; for a full list, see About the Interpreter.
Assignments and Arithmetic Operators
The most commonly used Fortran statements are assignment statements, which have the form(赋值语句形式)
variable = Fortran expression
The variable can be one you have defined as a variable in a Calculator block, or one defined in a declaration statement. The Fortran expression can be a number, another variable, a function call, or an arithmetic operation combining two or more expressions of these types(表达式可以是一个数字,其他变量,函数调用或以上几种通过运算组合的形式). The value resulting from evaluating the Fortran expression is assigned to the variable when this statement is executed[ˈeksɪkjuːtɪd]. In some cases in Aspen Plus, such as the Spec, Target, and Tolerance of a Design-Spec and the limits on manipulated variables, you will enter only a Fortran expression. In this case, there is an implicit[ɪmˈplɪsɪt/] assignment to the indicated attribute (in the same way that you would simply[/ˈsɪmpli/] enter a number in most fields), but the expression is evaluated each time the value of the attribute is required.
The basic arithmetic [/əˈrɪθmətɪk/] operators are:
Addition(加法)
The + sign is used for addition. The following statement adds 1 to the value of B and assigns the result to A:
A = B + 1
Subtraction(减法)
The - sign (ASCII hyphen, not an em dash or en dash character) is used for subtraction. The following statement subtracts 1 from the value of B and assigns the result to A:
A = B - 1
Multiplication(乘法)
The * character (asterisk) is used to represent multiplication. The following statement multiplies the value of B by 2 and assigns the result to A:
A = B * 2
Division(除法)
The / character (slash) is used to represent division. The following statement divides the value of B by 2 and assigns the result to A.
A = B / 2
Note: Division of one integer-type variable by another is integer division: the numbers are divided, the whole part of the result is kept (as an integer-type value), and the remainder is ignored. See Variable Types and Declarations, below. |
Exponentiation[ˌekspoʊˌnenʃɪˈeɪʃn](幂运算)
Two asterisks ( ** ) are used to represent exponentiation. Keep in mind the standard mathematical restrictions on exponentiation. The following statement squares the value of B and assigns the result to A:
A = B ** 2
Order of Operations
You can combine multiple arithmetic operations into a single expression. When you do so, Fortran has a specific order in which it performs the operations:
Exponentiations are performed first, right to left.
Multiplications and divisions are performed next, left to right.
Addition and subtraction is performed last, left to right.
A comment line, which is ignored during calculations, can be indicated by placing a C or c in the first column.(注释语句可以在第1列写个C)
In standard Fortran, line numbers can be indicated by writing the numbers into the first five columns. In inline Fortran in Aspen Plus (such as in Calculator blocks), the first two columns are reserved and line numbers can only appear in columns 3 through 5. These can be used in certain kinds of statements to refer to another line. In lines which are neither comments nor numbered, you should leave the first five columns blank (spaces).(第3~5列用于写行号。)
The sixth column is used only for the continuation character, to indicate that the line is a continuation of the previous line when expressions are very long. Fortran lines must not be longer than 72 characters, including the initial 6 spaces. Any character other than a space in the sixth column will make the line a continuation, but it is traditional to use a plus sign or to use digits (2 for the second line, 3 for the third line, and so forth).(第6列用于延续上一行公式用。)
Integer: A variable that holds a whole number such as 0, 1, or -2. Integers are stored in 8 bytes or 64 bits, one of which is used to store the sign, so they can hold values between 263 and -(263), or about 9,000,000,000,000,000,000 and -9,000,000,000,000,000,000. Note, though, that you cannot enter commas when writing large numbers into your Fortran program; just write 10000 instead of 10,000.
Real*8: A real variable which can hold a whole or fractional (decimal) value. The *8 indicates that the variable uses 8 bytes or 64 bits. This kind of variable (also called double precision) can store about 14 digits of accuracy and can store numbers up to about 10308 or as small as 10-308, as well as the negatives of this range. When you enter decimal numbers directly into your Fortran program, be sure to use a period or full-stop ( . ) as the decimal separator, even if Aspen Plus is configured to use a comma for the decimal separator elsewhere. For example, 1.5 is the correct way to write the number one-and-a-half.
Character*n: A character variable can store a string of text. The n indicates the maximum length of the string which the variable can hold.
Logical: A variable which can store a true or false value. If you want to write literal true and false values in your Fortran program, they have to be enclosed in period or full-stop characters (.TRUE. or .FALSE.). You can also generate Logical data as the result of using comparison operators.
Numbers without a decimal point or scientific notation indicator are interpreted as integers. For example, 42, -9, and 1234567890 are integers.
Caution: Be careful when multiplying or dividing integers. If the result of a multiplication overflows the limit 263, it will wrap around without warning. If a division leads to a fractional result, the fraction is lost. Dividing by zero generates an error; be careful to avoid doing so.(使用整数型变量需要注意乘法不要超过上限,除法不要出现小数。)
If you are writing a user model in Excel rather than in Fortran, this does not apply. Excel automatically converts large integers to real numbers and all values read from Excel back into Aspen Plus are taken as real numbers.
Numbers with a decimal point but without a scientific notation indicator are taken as real numbers. Check your compiler documentation on the size of the real for such numbers; they are typically taken as single-precision real numbers which have about 7 digits of accuracy and support a range of numbers approximately from 10-38 to 10308 as well as their negatives.
Numbers with a scientific notation indicator (an E or D in the number) are taken as single-precision real numbers if they include an E, and double-precision if they include a D. The number after the E or D indicates that the number to its left is multiplied by this power of ten. For instance, 10, 1.0E1, 1.0D1, and 10D0 are all ways to write the number ten, but the first is an integer, the second is a single-precision real number, and the last two are double-precision real numbers.
Note: If a real number calculation overflows the limit, it becomes infinity, which will lead to anomalous results and perhaps errors when the number is used subsequently. If it underflows the 10-38 or 10-308 limit, it becomes zero without warning. Division by zero may lead to infinity or NaN (not a number) which will impede further calculations and perhaps cause errors.
For example, 2+5*3 evaluates to 17 because the product 5*3 is calculated first, and then 2 is added to it.
You can group expressions in parentheses to specify a different order of calculations. Everything in parentheses is evaluated before anything outside the parentheses. If parentheses are nested, operations inside the inner parentheses are performed before those in the outer parentheses. For example, (2+5)*3 evaluates to 21.
Comments, Line Numbers, Continuation, and Indentation
You may notice how the examples in this topic are all indented. In Fortran statements, the first 6 columns are special.
The built-in editor in the Calculator | Input | Calculate sheet automatically leaves 6 blank spaces at the start of each line. If you need to make comments, line numbers, or continuations, you can delete these spaces.(软件默认留出6个空格)
Variable Types and Declarations[/ˌdekləˈreɪʃn/](变量类型与声明)
Fortran variables have explicit types indicating the kind of data they can hold. The most common variable types found in Fortran used within Aspen Plus are:
Two less commonly used types are:
When you perform arithmetic operations on two numbers, the result is the same type as the numbers you are operating on. If one number is real*8 and the other is integer, the result will be real*8. If the numbers are literal constants, their types are determined by the way they are written:
Variables you define with a reference to an Aspen Plus variable are automatically declared with the type appropriate to the Aspen Plus variable. This is usually Real*8, but countable items such as stage numbers are of type Integer. If you want to make intermediate variables used during calculations, enter declarations for these variables in the Fortran Declarations dialog box of a Calculator block, or at the start of an external Fortran subroutine. Declarations consist of the variable type followed by the variable, separated by a space. You can declare multiple variables of the same type by separating them with commas. To declare an array variable, enter the dimensions in parentheses, with multiple dimensions separated by commas, after the variable name in a declaration. Example declarations:
INTEGER I,J(2)
REAL*8 TIME,VOLUME,PRES(7,10)
LOGICAL C
CHARACTER*10 NAME
See Using the Define Sheet to Identify Flowsheet Variables for limitations on Fortran variable names that apply to Fortran code used in Aspen Plus.
Conditions and Branching
You can write IF statements to perform certain operations only in certain conditions. The format of an IF statement is:
IF (logical expression) THEN
conditional code
END IF
The logical expression can be a logical variable or the result of a comparison or logical operator. The parentheses around the logical expression are required. If the logical expression evaluates to true, then the conditional code is executed, and otherwise it is skipped.(括号内的表达式逻辑值为真,则执行条件语句,否则跳过。)
Comparison Operators
Comparison operators can be used in decision statements, or to store a value in a logical variable which may be used in a decision statement later. The comparison operators in Fortran are:
Operator | Meaning |
---|---|
.LT. | Less than |
.GT. | Greater than |
.EQ. | Equal |
.LE. | Less than or equal |
.GE. | Greater than or equal |
.NE. | Not equal |
For example, the expression B .LT. 3 is true if B is less than 3.
Logical Operators
For complicated logical expressions you can use logical operators to combine multiple logical expressions. The logical operators in Fortran are:
Operator | Meaning |
---|---|
.AND. | And (true only if both expressions are true) |
.OR. | Or (true if either or both expressions are true) |
.NOT. | Not (reverses result of following logical expression) |
For example, the expression A.EQ.3 .OR. B.LT.2 is true whenever A equals 3, B is less than 2, or both.
You can use parentheses to group parts of expressions involving comparison and logical operators. All arithmetic operations are performed first, then comparisons, and logical operators are last, with .NOT. evaluated before .AND., then .OR. is evaluated last.
Function Calls
You can call functions by typing the function name followed by its arguments in parentheses. If the function takes more than one argument, separate the arguments with commas ( , ).
Most commonly you will call the following built-in Fortran functions (the ones beginning with D return a double precision or real*8 result, while the others return an integer result):
Function | Meaning |
---|---|
DABS(X), IABS(J) | Absolute value绝对值 |
DSIN(X), DCOS(X), DTAN(X) | Sine, cosine, and tangent functions of X in radians.三角函数 |
DASIN(X), DACOS(X), DATAN(X) | Inverse sine, cosine, and tangent functions, with the result returned in radians.反三角函数 |
DEXP(X) | Exponential function (ex)自然指数 |
DLOG(X) | Natural logarithm of X自然对数 |
DLOG10(X) | Base 10 logarithm of X以10为底的对数 |
DSQRT(X) | Square root of X开方 |
DMIN1(X1,X2,...), DMAX1(...) | Minimum and maximum of the arguments (two or more real*8 arguments)最大/小值(浮点数类型) |
MIN0(J1,J2,...), MAX0(...) | Minimum and maximum of the arguments (two or more integer arguments)最大/小值(整数) |
DFLOAT(J) | Converts an integer value to a real*8 value 整数转化为浮点数 |
IDINT(X) | Converts a real value to an integer, truncating the fractional part. IDINT(1.3)=1; IDINT(-2.7)=-2. 取下整函数 |