-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathposcar2bs.f90
328 lines (307 loc) · 10.6 KB
/
poscar2bs.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
!Modified by Hyun-Jung Kim. Dept. of Physics Hanyang Univ.2008,1,21
!minor Modified by Hyun-Jung Kim. Dept. of Physics Hanyang Univ.2013,5,1 : radi_R added, bounding box added
!include shift option, by Hyun-Jung Kim, KIAS 2018.04.09 : shift_nx, shift_ny, shift_nz
!last update : 2018.05.16 by KHJ
!last update : 2020.04.21 by KHJ
program poscar2bs
implicit none
integer,parameter::matom=129024
integer,parameter::mtype=16
character(len=80)::title,fname,atom_name, title_
integer::nx,ny,nz, ishift
real*8 shift_nx,shift_ny,shift_nz
character(len=132)::line, desc_str
integer::lline,column,tend,i,j,k,ibound
real(8),dimension(3)::sc
real(8),dimension(3,3)::lattice,r_lattice
real(8)::vol
integer::ntype
integer,dimension(mtype)::natom
character(len=40)::TCHAR_
character(len=1) ::TCHAR
logical::selective,cartesian
integer::itype,iatom,iclass
real(8),dimension(3,matom,mtype)::coord
logical,dimension(3,matom,mtype)::movable
real(8),dimension(3)::c,tvec
logical,dimension(3)::m
character(len=5),dimension(mtype)::aname
real(8),dimension(0:mtype)::radius
real(8),dimension(0:mtype)::radi_R
real(8),dimension(3,mtype)::color
real(8),dimension(3)::bcolor
real(8) ::bcolor_type, radi_R_min, radi_RR
integer::idum, pid_atoms, pid_geom
real(8)::x_min,x_max,y_min,y_max,z_min,z_max,c_min,c_max
integer nitems
external nitems
ishift = 0
pid_atoms = 77
pid_geom = 78
bcolor_type=0.0 ! default
! read atoms.d header info.
open(pid_atoms,file='atoms.d',status='old',form='formatted')
rewind(pid_atoms)
read(pid_atoms,*)fname
read(pid_atoms,*)nx,ny,nz, desc_str
if (desc_str(1:1).ne.'#') then
rewind(pid_atoms)
read(pid_atoms,*)fname
read(pid_atoms,*)nx,ny,nz, shift_nx,shift_ny,shift_nz
ishift = 1
endif
! read POSCAR file
open(pid_geom,file=fname,status='old',form='formatted')
rewind(pid_geom)
! title
read(pid_geom,'(a)')title
write(0,'(a)')title(1:len_trim(title))
! scale factor(s)
read(pid_geom,'(a)')line
lline=len_trim(line); if(lline==0)call fatal
write(0,*)line(1:len_trim(line)),lline
!
column=1; call skipwhite(line,lline,column); if(column==0)call fatal
call gettoken(line,lline,column,tend); read(line(column:tend),*)sc(1)
write(0,*)line(column:tend),column,tend,sc(1)
!
column=tend+1; call skipwhite(line,lline,column)
if(column==0)then; sc(2)=sc(1); sc(3)=sc(1)
else
call gettoken(line,lline,column,tend); read(line(column:tend),*)sc(2)
write(0,*)line(column:tend),column,tend,sc(2)
!
column=tend+1; call skipwhite(line,lline,column); if(column==0)call fatal
call gettoken(line,lline,column,tend)
write(0,*)line(column:tend),column,tend
read(line(column:lline),*)sc(3)
end if
write(0,*)sc(1:3)
! lattice vectors
do i=1,3; read(pid_geom,*)lattice(1:3,i); end do
do i=1,3; lattice(i,1:3)=lattice(i,1:3)*sc(i); end do
r_lattice(1,1)=lattice(2,2)*lattice(3,3)-lattice(3,2)*lattice(2,3)
r_lattice(2,1)=lattice(3,2)*lattice(1,3)-lattice(1,2)*lattice(3,3)
r_lattice(3,1)=lattice(1,2)*lattice(2,3)-lattice(2,2)*lattice(1,3)
r_lattice(1,2)=lattice(2,3)*lattice(3,1)-lattice(3,3)*lattice(2,1)
r_lattice(2,2)=lattice(3,3)*lattice(1,1)-lattice(1,3)*lattice(3,1)
r_lattice(3,2)=lattice(1,3)*lattice(2,1)-lattice(2,3)*lattice(1,1)
r_lattice(1,3)=lattice(2,1)*lattice(3,2)-lattice(3,1)*lattice(2,2)
r_lattice(2,3)=lattice(3,1)*lattice(1,2)-lattice(1,1)*lattice(3,2)
r_lattice(3,3)=lattice(1,1)*lattice(2,2)-lattice(2,1)*lattice(1,2)
vol=dot_product(lattice(1:3,1),r_lattice(1:3,1))
r_lattice(1:3,1:3)=r_lattice(1:3,1:3)/vol
do i=1,3; write(0,*)lattice(1:3,i),r_lattice(1:3,i); end do
write(0,*)vol
! atom name : Infant added
read(pid_geom,*)atom_name
! natom(s)
read(pid_geom,'(a)')line
lline=len_trim(line); if(lline==0)call fatal
ntype=0
column=1
do
call skipwhite(line,lline,column); if(column==0)exit
call gettoken(line,lline,column,tend)
ntype=ntype+1; read(line(column:tend),*) natom(ntype)
column=tend+1
end do
if(ntype==0)call fatal
write(0,*)natom(1:ntype)
! format of coord
read(pid_geom,'(a)')tchar_
tchar_ = adjustl(trim(tchar_))
tchar = tchar_(1:1)
selective=.false.
if(adjustl(trim(tchar))=='s'.or.adjustl(trim(tchar))=='S')then
selective=.true.
read(pid_geom,'(a)')tchar_
tchar_ = adjustl(trim(tchar_))
tchar = tchar_(1:1)
end if
! type of coord
cartesian=.false.
if(adjustl(trim(tchar))=='c'.or.adjustl(trim(tchar))=='C'.or. &
adjustl(trim(tchar))=='k'.or.adjustl(trim(tchar))=='K')cartesian=.true.
! read atoms.d
do i=1,ntype
read(pid_atoms,*)idum,aname(i),radius(i),color(:,i),radi_R(i)
end do
read(pid_atoms,'(A)')title ; backspace(pid_atoms)
call take_before_comment(title,title_) ; title = title_
if(nitems(title) .eq. 4) then
read(pid_atoms,*)radius(0),bcolor(1:3) !bond width
elseif(nitems(title) .eq. 5) then
read(pid_atoms,*)radius(0),bcolor(1:3), bcolor_type !bond width
endif
read(pid_atoms,*)ibound !bounding box factor: 0(off), 1(on)
if((ibound .gt. 0) .AND. (ibound .lt. 4))then
read(pid_atoms,*)c_min,c_max
elseif(ibound .eq. 4)then
read(pid_atoms,*)x_min,x_max
read(pid_atoms,*)y_min,y_max
read(pid_atoms,*)z_min,z_max
endif
close(pid_atoms)
! coord info
do itype=1,ntype
do iatom=1,natom(itype)
if(selective)then
read(pid_geom,*)c(1:3),m(1:3)
else
read(pid_geom,*)c(1:3)
end if
if(cartesian)then
do i=1,3
tvec(i)=dot_product(r_lattice(1:3,i),c(1:3)*sc(1:3))
end do
else
tvec(1:3)=c(1:3)
end if
tvec(:)=tvec(:)+(/shift_nx,shift_ny,shift_nz/)
coord(1:3,iatom,itype)=tvec(1:3)-nint(tvec(1:3))
end do
end do
close(pid_geom)
! generate bs file
open(9,file='poscar.bs',status='replace',form='formatted')
do i=1,nx
do j=1,ny
do k=1,nz
do itype=1,ntype
do iatom=1,natom(itype)
c(1)=dot_product((/i-1,j-1,k-1/)+coord(1:3,iatom,itype),lattice(1,1:3))
c(2)=dot_product((/i-1,j-1,k-1/)+coord(1:3,iatom,itype),lattice(2,1:3))
c(3)=dot_product((/i-1,j-1,k-1/)+coord(1:3,iatom,itype),lattice(3,1:3))
if((c(3) .gt. z_min .AND. c(3) .lt. z_max) .AND. &
(c(2) .gt. y_min .AND. c(2) .lt. y_max) .AND. &
(c(1) .gt. x_min .AND. c(1) .lt. x_max) .AND. (ibound .eq. 4))then
write(*,'("atom ",a6,3f20.11," # ",i5)')aname(itype),c(1:3),iatom
write(9,'("atom ",a6,3f20.11," # ",i5)')aname(itype),c(1:3),iatom
ELSEIF(ibound .eq. 0)then
write(*,'("atom ",a6,3f20.11," # ",i5)')aname(itype),c(1:3),iatom
write(9,'("atom ",a6,3f20.11," # ",i5)')aname(itype),c(1:3),iatom
ELSEIF(ibound .gt. 0 .AND. ibound .lt. 4)then
if(c(ibound) .gt. c_min .AND. c(ibound) .lt. c_max)then
write(*,'("atom ",a6,3f20.11," # ",i5)')aname(itype),c(1:3),iatom
write(9,'("atom ",a6,3f20.11," # ",i5)')aname(itype),c(1:3),iatom
endif
endif
end do
end do
end do
end do
end do
do itype=1,ntype
write(*,'("spec ",a,1x,f9.5,1x,3(f8.4,1x))')&
! aname(itype)(1:len_trim(aname(itype))),radius(itype)*0.30d0,&
aname(itype)(1:len_trim(aname(itype))),radi_R(itype)*0.30d0,&
color(:,itype)
write(9,'("spec ",a,1x,f9.5,1x,3(f8.4,1x))')&
! aname(itype)(1:len_trim(aname(itype))),radius(itype)*0.30d0,&
aname(itype)(1:len_trim(aname(itype))),radi_R(itype)*0.30d0,&
color(:,itype)
end do
do itype=1,ntype
do i=itype,ntype
!write(*,'("bonds ",2a6," 0.0 3.0 0.1 1.0 1.0 1.0")')aname(itype),aname(i)
if(bcolor_type .gt. 0.0d0) then
bcolor(1:3) = (color(:,itype) + color(:,i)) * 0.5d0
radi_R_min = min(radi_R(itype)*0.30d0, radi_R(i)*0.30d0) ! minimum radius of atom ball
if( radius(0) .gt. radi_R_min) then ! if radi_R_min is narrower then cylinder radii
radi_RR = radi_R_min ! set cylinder radii as radi_R_min
else ! if radi_R_min is wider than cylinder radii
radi_RR = radius(0) ! use cylinder radii
endif
else
radi_R_min = min(radi_R(itype)*0.30d0, radi_R(i)*0.30d0) ! minimum radius of atom ball
if( radius(0) .gt. radi_R_min) then ! if radi_R_min is narrower then cylinder radii
radi_RR = radi_R_min ! set cylinder radii as radi_R_min
else ! if radi_R_min is wider than cylinder radii
radi_RR = radius(0) ! use cylinder radii
endif
endif
write(*,'("bonds ",2a6," 0.0 ",2(f9.5,1x),3(f8.4,1x))')&
aname(itype),aname(i),(radius(itype)+radius(i)),&
radi_RR,bcolor(1:3)
! radius(0),bcolor(1:3)
!0.5d0*(color(:,itype)+color(:,i))
write(9,'("bonds ",2a6," 0.0 ",2(f9.5,1x),3(f8.4,1x))')&
aname(itype),aname(i),(radius(itype)+radius(i)),&
radi_RR,bcolor(1:3)
! radius(0),bcolor(1:3)
end do
end do
contains
subroutine fatal; implicit none
write(0,*)'please check your poscar file'; stop
end subroutine fatal
end program poscar2bs
subroutine skipwhite(line,lline,column)
implicit none
character(len=*),intent(in)::line
integer,intent(in)::lline
integer,intent(inout)::column
integer::i
do i=column,lline
if(ichar(line(i:i))>32)then
column=i
return
end if
end do
column=0
return
end subroutine skipwhite
subroutine gettoken(line,lline,column,tend)
implicit none
character(len=*),intent(in)::line
integer,intent(in)::lline
integer,intent(in)::column
integer,intent(out)::tend
integer::i
do i=column,lline
if(ichar(line(i:i))<=32)then
tend = i - 1
return
end if
end do
tend=lline
return
end subroutine gettoken
! vim:shiftwidth=2:smarttab:autoindent:expandtab
function nitems(string)
implicit none
logical blank
integer*4 nitems,l,i
character(*),intent(in) :: string
nitems=0
l=len_trim(string)
blank = .true.
do i=1,l
if(string(i:i) .eq. '#' .or. string(i:i) .eq. '!') exit
if (blank .and. string(i:i) .ne. ' ' ) then
blank=.false.
nitems=nitems + 1
elseif( .not. blank .and. string(i:i) .eq. ' ') then
blank=.true.
endif
enddo
return
endfunction
subroutine take_before_comment(string, strip)
! comment off from the "string" and return as "strip".
implicit none
logical blank
character(*) string, strip
integer*4 l0, init
init = -1
strip = ''
l0 = len_trim(string)
init = index(string,'#',.FALSE.)
if(init .eq. 0) then
strip = string
elseif(init .ge. 1) then
strip = string(1:init-1)
endif
return
endsubroutine