0% found this document useful (0 votes)
8 views21 pages

CH332 15jan

The document outlines a course on Computational Chemistry, focusing on the use of built-in functions in FORTRAN for various mathematical operations and input/output statements. It discusses how to handle data through variables, terminal input, and file reading, as well as the representation of data using arrays. Additionally, it includes examples of calculating the volume of an ideal gas and plotting experimental data against theoretical models.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views21 pages

CH332 15jan

The document outlines a course on Computational Chemistry, focusing on the use of built-in functions in FORTRAN for various mathematical operations and input/output statements. It discusses how to handle data through variables, terminal input, and file reading, as well as the representation of data using arrays. Additionally, it includes examples of calculating the volume of an ideal gas and plotting experimental data against theoretical models.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

CH 332

Computational Chemistry

Course Instructor: Dr. Debdas Dhabal

Department of Chemistry, IIT Guwahati


15 January 2024
Outline

ØBuilt-in Functions in FORTRAN


ØInput/Output Statement
ØSome examples
A few built-in functions in FORTRAN

Types of argument the function is


Function What it does
applied to
ABS (x) Absolute value of x integer, real, double precision
DPROD(x,y) Double precision product of x and y real
MAX(x1,x2,. . . , xn) Maximum of x1,x2 . . .,xn integer, real, double precision
MIN(x1, x2,. . . , xn) Minimum of x1,x2 . . ., xn integer, real, double precision
MOD(x,y) Remainder of the division of x by y integer, real, double precision
difference x-y if the result is
DIM(x,y) integer, real, double precision
positive
LOG(x) Natural logarithm of x real, double precision
LOG10(x) Base 10 logarithm of x real, double precision
EXP(x) Exponential function real, double precision
SIN(x) Sine of x radians real, double precision
COS(x) Cosine of x radians real, double precision
SQRT(x) Square root of x real, double precision
A few built-in functions in FORTRAN
Types of argument the
Function What it does
function is applied to
SINH (x) Hyperbolic sine of x real, double precision
COSH(x,y) Hyperbolic cos of x Real, double precision
ASIN(x) Arcsine of its x, sin-1(x) real, double precision
Try
ACOS(x) Arcsine of its x, cos-1(x) real, double precision these at
FRACTION(x) Fractional part of x real, double precision home/
practical
FLOOR(x) Largest integer less than or equal to x real, double precision
class
CEILING(x) Least integer greater than or equal to x real, double precision
SIGN(x,y) Transfer the sign of y to x real, double precision
Hands-on: FORTRAN functions

Output:
A few widely used functions in FORTRAN

Note that some of these functions can only be applied to a certain type
of argument: For example, you can not use SQRT on an integer-type
variable

Error message upon compilation:

v If you want this program to work, then you must convert


the type of 'a' to real.

sqrt(float(a))
Input-output statement

Fortran allows three ways to input the data


1. Provide the data into the code as a variable
2. Feed the data through the terminal
3. Read data from a file

1. Provide the data into the code as a variable

Not very flexible! Need to open the code every


time you want to change the values
Input-output statement

2. Feed the data through the terminal

Now, if you want to work with a large number of data


It will be a tedious task!
Input-output statement

3. Read data from a file


v 10 is the unit of the file: could be
any positive number
v By default, if you don’t use the
“open” statement to read a file,
you must name the file as
“fort.unit” (unit=10 in this
example)

fort.10 file should look like


7
4
Input-output statement

v You can name your input file as you want and read that by the code
open (unit = number, file = "name”, status=“old”)

could be any File name of Whether an existing


positive your choice file or you are
number
creating a new file

value.data file should look like


7
4
Input-output statement

Fortran allows two ways to output the data


1. Display the results in the terminal
2. Write results to a file
Write results to a file
Display the results in the terminal
Open the file using:

v write(*,*) a will display the value of open (unit = number, file = "name”, status=“new”)
“a” on the terminal
v Most of the codes written above or
display the result on the screen
v write(50,*) a will write the value of “a” in a file
named “fort.50”
A sample program to calculate the volume of an ideal gas

Calculate the volume in c.c. of 3 moles of an ideal gas at pressure 1 bar and temperature 298 K.
To make the code more general

We could ask the user to enter the value of pressure or the program can read it from a file
Arrays

o The use of vectors and matrices in scientific computing is common.


o Such objects are represented as an array in FORTRAN.
o A one-dimensional array corresponds to a vector, while a two-dimensional array
corresponds to a matrix.
One Dimensional Arrays:

Ø A linear sequence of elements stored consecutively in memory

real a(20) A real array of length 20, consists of 20 real numbers stored contiguously in
memory
Average of a set of numbers: You can read them individually

o Let’s say you have a series of numbers, and you want to calculate the mean.

1. One way is to define them with different variable 2. Or you may want to read it from a file
a= 2.0 real a,b,c,d,e,f,g,h,i
b= 4.0 read(10,*)a
c= 5.0 read(10,*)b
read(10,*)c
d= 1.0 read(10,*)d
e= 6.0 read(10,*)e
f= 12.0 read(10,*)f
g= 1.0 read(10,*)g
h= 5.0 read(10,*)h
read(10,*)i
i= 34.0

Mean= (a+b+c+d+e+f+g+h+i)/9
Or you can represent them in an Array

real a,b,c,d,e,f,g,h,i real a(9) consists of 9 real numbers: a(1), a(2), a(3)……a(9)

do i=1,9 By convention, Fortran arrays are indexed from 1 and up


read(10,*) a(i) But you may use an arbitrary array range
enddo
real a(0:8) numbers in the array are denoted as a(0),
a(1)…a(8)
real a(-4:4) numbers in the array are denoted as a(-4),
a(-3)…..a(4)
Two-dimensional Arrays

real a(3,4) defines a two-dimensional array of 3*4=12 real numbers

a(1,1) a(1,2) a(1,3) a(1,4)


a(2,1) a(2,2) a(2,3) a(2,4)
Where did you see this kind of
a(3,1) a(3,2) a(3,3) a(3,4) representation?

Matrices are usually represented by two-dimensional arrays

1 2 3
a11=2, a12=7, a13=-6
2 7 −6 1
a= 2×3 matrix a21=6, a23=3, a33=5
6 3 5 2 2 rows × 3 column
Let’s discuss ideal gas problem
The input file looks like

Have we used an array in this code?

No! The pressure was


calculated at a single point
(volume)

Now, if we are interested in


plotting how pressure
changes with the volume of
CO2?
Experimental data vs theory

Change in volume of CO2 as a function of pressure


Ideal and Van der
Waals Eqn., 373 K
Experimental Data
600
Ideal
500 Vander Waals

400

V(cc)
300

200

100

0
0 100 200 300 400 500
p(bar)
A FORTRAN code that takes volume as an array and output
pressure as an array

Vdata.dat
536
506
376
346
316
……. Volume is now represented as
……. an array
56
Process and Job-control

THANK YOU!

You might also like