1*
2* $Id$
3
4*  Note - The format of the MOTION file was changed on 5-8-2002.
5*
6* Format of MOTION file:
7*
8*  time nion omega
9*   x(1) y(1) z(1) vx(1) vy(1) vz(1)
10*   x(2) y(2) z(2) vx(2) vy(2) vz(2)
11*   x(3) y(3) z(3) vx(3) vy(3) vz(3)
12*   ...
13*   x(nion) y(nion) z(nion) vx(nion) vy(nion) vz(nion)
14*  time2 nion omega
15*   ...
16*
17
18*     ***************************
19*     *				*
20*     *	      MOTION_init	*
21*     *				*
22*     ***************************
23*
24*  This routine initializes the MOTION file, which
25* is used to keep track of ion positions and velocities.
26*
27
28      subroutine MOTION_init(rtdb)
29      implicit none
30      integer   rtdb
31
32#include "btdb.fh"
33
34
35      integer   MASTER
36      parameter (MASTER=0)
37
38      logical value,found,found_bak
39      integer taskid,l1,l2
40      real*8 ch_tmp
41      character*50 filename
42      character*255 full_filename,full_bak
43
44
45*     **** external functions ***
46      real*8   lattice_omega
47      integer  ion_nion,control_it_out
48      external lattice_omega
49      external ion_nion,control_it_out
50
51
52      call Parallel_taskid(taskid)
53
54      if (.not.btdb_cget(rtdb,'cpmd:ion_motion_filename',1,filename))
55     > call util_file_prefix('ion_motion',filename)
56      call util_file_name_noprefix(filename,.false.,
57     >                             .false.,
58     >                    full_filename)
59
60*     **** produce MOTION FILE ****
61      if (taskid.eq.MASTER) then
62
63*        **** check for backup file ****
64         call util_file_name_noprefix('MOTION99-bak',.false.,
65     >                                .false.,
66     >                                full_bak)
67         inquire(file=full_bak,exist=found_bak)
68         if (found_bak) then
69            write(*,*)
70            write(*,*) "MOTION99-bak exists:"
71            l1=index(full_bak,' ')
72            l2=index(full_filename,' ')
73            write(*,*) "   Copying ",full_bak(1:l2),
74     >                 " to ",full_filename(1:l2)
75            write(*,*)
76            call util_file_copy(full_bak,full_filename)
77         end if
78
79
80*        **** MOTION FILE already exists - parse to EOF ****
81         inquire(file=full_filename,exist=found)
82         if (found) then
83
84*          **** make a new backup file ***
85           call util_file_copy(full_filename,full_bak)
86
87           open(unit=19,file=full_filename,form='formatted',
88     >          status='old')
89           do while(.true.)
90             read(19,*,ERR=30,END=30) ch_tmp
91           end do
92 30        continue
93#if defined(FUJITSU_SOLARIS) || defined(PSCALE) || defined(__crayx1)||defined(GCC46)
94           backspace 19
95#endif
96
97*        **** MOTION FILE does not exist ****
98         else
99            open(unit=19,file=full_filename,form='formatted',
100     >           status='new')
101         end if
102
103      end if
104
105      return
106      end
107
108
109*     ***************************
110*     *				*
111*     *		MOTION_end 	*
112*     *				*
113*     ***************************
114      subroutine MOTION_end()
115      implicit none
116
117      integer   MASTER
118      parameter (MASTER=0)
119
120      integer taskid
121      character*255 full_bak
122
123      call Parallel_taskid(taskid)
124
125      if (taskid.eq.MASTER) then
126         close(unit=19)
127
128*        **** remove backup file ***
129         call util_file_name_noprefix('MOTION99-bak',.false.,
130     >                                .false.,
131     >                                full_bak)
132         call util_file_unlink(full_bak)
133      end if
134
135      return
136      end
137
138*     ***************************
139*     *				*
140*     *		MOTION_write    *
141*     *				*
142*     ***************************
143      subroutine MOTION_write(time)
144      implicit none
145      real*8 time
146
147      integer   MASTER,taskid
148      parameter (MASTER=0)
149
150      integer i
151
152*     **** external functions ***
153      integer     ion_nion
154      external    ion_nion
155      real*8      ion_rion,ion_vion,lattice_omega,lattice_unita
156      external    ion_rion,ion_vion,lattice_omega,lattice_unita
157      character*2 ion_aname_nocarat
158      external    ion_aname_nocarat
159      character*4 ion_aname
160      external    ion_aname
161
162      call Parallel_taskid(taskid)
163
164      if (taskid.eq.MASTER) then
165         write(19,110) time,
166     >                 ion_nion(),
167     >                 lattice_omega(),
168     >     lattice_unita(1,1),lattice_unita(2,1),lattice_unita(3,1),
169     >     lattice_unita(1,2),lattice_unita(2,2),lattice_unita(3,2),
170     >     lattice_unita(1,3),lattice_unita(2,3),lattice_unita(3,3)
171         do i=1,ion_nion()
172            write(19,111) i,ion_aname_nocarat(i),ion_aname(i),
173     >                    ion_rion(1,i),
174     >                    ion_rion(2,i),
175     >                    ion_rion(3,i),
176     >                    ion_vion(1,i),
177     >                    ion_vion(2,i),
178     >                    ion_vion(3,i)
179         end do
180         call util_flush(19)
181      end if
182  110 format(e14.6,I6,e14.6,9e14.6)
183  111 format(i6,a3,a5,6e14.6)
184
185      return
186      end
187
188