Advanced Fortran: Exercices

Try to directly use best practices: program in Fortran90 😄

Exercice 1

Write a program in Fortran90 that computes the average of a data set.

The program can be decomposed in the following parts:

  1. ask the user for the number of data
  2. create a vector to contain these data
  3. ask the user for all data elements
  4. compute the data average
  5. output the answer

Exercice 2

Write a program to display all prime number between 0 and N (N being asked to the user). To do so, use the sieve of Eratosthenes.

The program can be decomposed in the following parts:

  1. ask the user for N
  2. initialize an integer array containing indexes from 1 to N
  3. starting from I=2, put to zero elements that are multiple of I
  4. display all non-zero elements of the array

Exercice 3

Write a program that converts cartesian coordinates to internal coordinates. :-)

The input will be a file containing the number of atoms in the first line, then one line per atom. A line is composed of a character (the element), x, y, and z cartesian coordinates. The output should be a file containing a Z-matrix.

The program can be decomposed in the following parts:

  1. open the input file
  2. read the first line and store the atom number
  3. allocate an array to store x, y, z coordinates
  4. read the other lines and store x, y, z coordinates
  5. allocate an array to store r, angle, dihedral internal coordinates (distance, angle and dihedral angles with previous 1, 2, and 3 atoms, respectively)
  6. compute and store r, angle, and dihedral coordinates ((Remember that the first atom in a Z-matrix does not have any coordinates, the second atom does not have any angle and dihedral coordinates, and that the third atom does not have any dihedral coordinate.))
  7. display the Z-matrix ((Here again, make a special treatment for the first three atoms.)).

As an example for the cartesian coordinate file, one can take:

 H     0.917613    0.886488    0.414187
 O     0.000000    0.733119   -0.051773
 O     0.000000   -0.733119   -0.051773
 H    -0.917613   -0.886488    0.414187

A copy of this file is available in Gerald.Monard/ directory on sam.lctn.uhp-nancy.fr.

Exercice 4

Write a program that performs operations on symmetrical matrices. Symmetrical matrices will be represented by vectors.

For example, the symetrical 4x4 matrix:

\left( \begin{array}{cccc} 1 & 2 & 3 & 4 \\ 2 & 5 & 7 & 8 \\ 3 & 7 & -10 & -3 \\ 4 & 8 & -3 & 12 \\ \end{array} \right)

can be represented by a 1D vector:

\begin{array}{cccccccccc} 1 & 2 & 3 & 4 & 5 & 7 & 8 & -10 & -3 & 12 \end{array}

You will define two operations: addition and multiplication.

Possible decomposition of the program:

  1. write a function that converts the position (i,j) in a symmetrical matrix in a position k in the 1D-vector
  2. write a function that converts the position k in a 1D-vector in position (i,j) in a symmetrical matrix
  3. write a function that returns the symmetrical matrix whose elements are the sum of the row number and the column number (A_{i,j} = i+j)
  4. write a function that returns the symmetrical matrix whose elements are the product of the row number and the column number (B_{i,j}= i*j)
  5. write a function that performs the addition of two symmetrical matrices represented as 1D-vector
  6. write a function that performs the multiplication of two symmetrical matrices represented as 1D-vector
  7. test your program

Exercice 5

Transform this file:

   29
1HH3   CH3  2HH3  3HH3   C     O     N     H     CA    HA 
 CB   2HB   3HB    CG   2HG   3HG    SD    CE   1HE   2HE 
3HE    C     O     N     H     CH3  1HH3  2HH3  3HH3

ACE   ACE   ACE   ACE   ACE   ACE   MET   MET   MET   MET 
MET   MET   MET   MET   MET   MET   MET   MET   MET   MET 
MET   MET   MET   NME   NME   NME   NME   NME   NME 

   1   1   1   1   1   1   2   2   2   2
   2   2   2   2   2   2   2   2   2   2
   2   2   2   3   3   3   3   3   3

   2.000   2.000   1.486   1.486   3.427
   4.391   3.555   2.733   4.853   5.408
   5.661   5.809   5.123   7.017   6.871
   7.556   7.989   9.471   9.912  10.183
   9.226   4.713   3.601   5.846   6.737
   5.846   4.819   6.360   6.360

   1.000   2.090   2.454   2.454   2.641
   1.877   3.970   4.556   4.614   4.316
   4.221   3.141   4.521   4.918   5.998
   4.618   4.461   5.397   5.077   5.230
   6.458   6.129   6.653   6.835   6.359
   8.284   8.648   8.648   8.648

   0.000   0.000   0.890  -0.890   0.000
   0.000   0.000   0.000   0.000   0.890
 1. 1.232  -1.241  -2.131  -1.195  -1.186
 2. 0.296  -2.652  -2.341  -1.397  -3.149
 3. 2.287   0.000   0.000   0.000   0.000
   0.000   0.000   0.890  -0.890

into this file:

ATOM      1 1HH3 ACE     1       2.000   1.000   0.000
ATOM      2  CH3 ACE     1       2.000   2.090   0.000
ATOM      3 2HH3 ACE     1       1.486   2.454   0.890
ATOM      4 3HH3 ACE     1       1.486   2.454  -0.890
ATOM      5  C   ACE     1       3.427   2.641   0.000
ATOM      6  O   ACE     1       4.391   1.877   0.000
ATOM      7  N   MET     2       3.555   3.970   0.000
ATOM      8  H   MET     2       2.733   4.556   0.000
ATOM      9  CA  MET     2       4.853   4.614   0.000
ATOM     10  HA  MET     2       5.408   4.316   0.890
ATOM     11  CB  MET     2       5.661   4.221  -1.232
ATOM     12 2HB  MET     2       5.809   3.141  -1.241
ATOM     13 3HB  MET     2       5.123   4.521  -2.131
ATOM     14  CG  MET     2       7.017   4.918  -1.195
ATOM     15 2HG  MET     2       6.871   5.998  -1.186
ATOM     16 3HG  MET     2       7.556   4.618  -0.296
ATOM     17  SD  MET     2       7.989   4.461  -2.652
ATOM     18  CE  MET     2       9.471   5.397  -2.341
ATOM     19 1HE  MET     2       9.912   5.077  -1.397
ATOM     20 2HE  MET     2      10.183   5.230  -3.149
ATOM     21 3HE  MET     2       9.226   6.458  -2.287
ATOM     22  C   MET     2       4.713   6.129   0.000
ATOM     23  O   MET     2       3.601   6.653   0.000
ATOM     24  N   NME     3       5.846   6.835   0.000
ATOM     25  H   NME     3       6.737   6.359   0.000
ATOM     26  CH3 NME     3       5.846   8.284   0.000
ATOM     27 1HH3 NME     3       4.819   8.648   0.000
ATOM     28 2HH3 NME     3       6.360   8.648   0.890
ATOM     29 3HH3 NME     3       6.360   8.648  -0.890

Exercice 6

Write a program that defines polar coordinates ( (r, theta) points in 2D space) and the operation that can be performed with polar points:

  • distance between two points

  • symmetry with an x'Ox axis (transformation of one polar point to another)

  • symmetry with an y'Oy axis (transformation of one polar point to another) Polar points will be define as new type in a Fortran90 module.