1!
2! Copyright (C) 1996-2016	The SIESTA group
3!  This file is distributed under the terms of the
4!  GNU General Public License: see COPYING in the top directory
5!  or http://www.gnu.org/copyleft/gpl.txt.
6! See Docs/Contributors.txt for a list of contributors.
7!
8      subroutine iomd( na, isa, iza, xa, va, cell, vcell, varcel, istep,
9     .                 istep0, temp, eks, getot, volume, Psol)
10c *******************************************************************
11c Saves positions, cell, and energies in a MD run (accumulative)
12c J.Kohanoff August 1998, slightly modified by E. Artacho, Feb. 1999
13c Modified to open and close each time by E. Artacho, Aug 2002
14c *******************************************************************
15c Two possibilities: |*everything into one single unformatted file
16c                    | (for postprocessing programs, space saving)
17c (formtt parameter) |*separated ascii files
18c ********** INPUT **************************************************
19c real*8  cell(3,3)  : Unit cell vectors
20c real*8  vcell(3,3) : Velocities thereof
21c integer na         : Number of atoms
22c integer isa(na)    : Atomic species index
23c integer iza(na)    : Atomic numbers
24c real*8  xa(3,na)   : Atomic positions
25c real*8  va(3,na)   : Velocities thereof
26c logical varcel     : .true. when variable cell
27c real*8  temp       : Temperature of ions
28c real*8  eks        : Kohn-Sham energy
29c real*8  getot      : Total energy
30c integer istep      : Present time step
31c integer istep0     : First time step
32c real*8  volume     : cell volume in Ang**3
33c real*8  Psol       : total pressure (static plus kinetik) in kBar
34c *******************************************************************
35
36      use precision,  only : dp
37      use files,      only : slabel, label_length
38      use units, only: eV
39
40      implicit          none
41
42      integer           na, isa(na), iza(na)
43      integer           istep, istep0
44      logical           varcel
45      real(dp)          cell(3,3), xa(3,na), va(3,na), vcell(3,3),
46     .                  temp, eks, getot, volume, Psol
47
48c Internal variables and arrays
49      logical, parameter :: IS_FORMATTED = .false.
50      character(len=label_length+4) :: fncel
51      character(len=label_length+4) :: fnene
52      character(len=label_length+4) :: fnpos
53
54      integer :: ia, iv, ix
55      integer :: iucel, iuene, iupos
56
57      external          io_assign, io_close
58
59      ! Find name of file
60      fnene = trim(slabel) // '.MDE'
61      if ( IS_FORMATTED ) then
62         fnpos = trim(slabel) // '.MDX'
63         if (varcel) fncel = trim(slabel) // '.MDC'
64      else
65         fnpos = trim(slabel) // '.MD'
66      end if
67
68c Open file
69
70      call io_assign( iuene )
71      open(iuene, file=fnene, form='formatted', position='append',
72     .     status='unknown')
73      if ( IS_FORMATTED ) then
74         call io_assign( iupos )
75         open(iupos, file=fnpos, form='formatted', position='append',
76     .        status='unknown')
77         if ( varcel ) then
78            call io_assign( iucel )
79            open(iucel,file=fncel,form='formatted',position='append',
80     .           status='unknown' )
81         end if
82      else
83         call io_assign( iupos )
84         open(iupos,file=fnpos,form='unformatted',status='unknown',
85     $        position="append")
86      end if
87
88      if(istep .eq. istep0) then
89        write(iuene,"(6a)") '# Step','     T (K)','     E_KS (eV)',
90     .     '    E_tot (eV)','   Vol (A^3)','    P (kBar)'
91      endif
92
93C Write data on files
94
95      write(iuene,'(i6,1x,f9.2,2(1x,f13.5),1x,f11.3,1x,f11.3)')
96     .            istep, temp, eks/eV, getot/eV, volume, Psol
97      if ( IS_FORMATTED ) then
98        write(iupos,*) istep
99        do ia = 1,na
100          write(iupos,'(i3,i6,3f13.6,3x,3f13.6)')
101     .      isa(ia),iza(ia),(xa(ix,ia),ix=1,3),(va(ix,ia),ix=1,3)
102        enddo
103        if ( varcel ) then
104          write(iucel,*) istep
105          write(iucel,'(2(3x,3f13.6))')
106     .      ((cell(ix,iv),ix=1,3),(vcell(ix,iv),ix=1,3),iv=1,3)
107        endif
108      else
109        write(iupos) istep, xa, va
110        if ( varcel ) write(iupos) cell, vcell
111      endif
112
113C Close files
114
115      call io_close( iuene )
116      call io_close( iupos )
117      if ( IS_FORMATTED .and. varcel ) call io_close( iucel )
118
119      end
120