-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcutmod.f
613 lines (583 loc) · 20.6 KB
/
cutmod.f
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
module cutmod
contains
subroutine cut(cutinfile,cutoutfile,cutinform,cutoutform,
& above,axisint,cuthere,origin,nndist,hdist,hydr)
use defs
use readcoords
use writecoords
use misc
implicit none
! variables
integer axisint
double precision cuthere,origin(1:3),nndist,hdist
character(len=*) cutinfile,cutoutfile,cutinform,cutoutform,
& above
logical, optional :: hydr
! internal variables
logical isopen12,newspecies
type(atom), allocatable :: atoms(:),keepatoms(:),rmatoms(:)
type(atom),allocatable :: atomsH(:),keepatomsH(:)
type(element), allocatable :: species(:),keepspecies(:),
& keepspeciesH(:)
integer natoms,nspecies,nkeep,nspeckeep,
& nspeckeepH
integer nremove,jrm
double precision vecs(1:3,1:3),distvec(1:3),dist,gvecs(1:3,1:3)
double precision coords1(3),coords2(3)
integer i,j,k,l,ndangb ! i1,i2,i3,n_NN
integer k1,k2,k3
!character FMT1*1024,FMT2*1024,FMT3*1024
character filename*256
talk=.true.
! write output to unit 12, if open, else print to terminal
INQUIRE (unit=12, opened=isopen12)
if(isopen12) then
if(talk)write(12,fsubstart) trim('cut')
else
if(talk)print fsubstart, trim(adjustl('cut'))
end if
! read in coordinates ...
if (talk) then
print '(" ")'
print '(8x,"calling read_coords...")'
end if
call read_coords(cutinfile,cutinform,atoms,natoms,species,
& nspecies,vecs)
if(talk)print '(8x,"Coords read in.")'
! remove atoms below/above threshold
select case(above)
case("above","ABOVE")
nkeep=0
do i=1,natoms
if(atoms(i)%where(axisint).le.cuthere) then
nkeep=nkeep+1
end if
end do
nremove=natoms-nkeep
allocate(keepatoms(nkeep),rmatoms(nremove))
j=0
jrm=0
do i=1,natoms
if(atoms(i)%where(axisint).le.cuthere) then
j=j+1
keepatoms(j)=atoms(i)
end if
if(atoms(i)%where(axisint).gt.cuthere) then
jrm=jrm+1
rmatoms(jrm)=atoms(i)
end if
end do
case("below","BELOW")
nkeep=0
do i=1,natoms
if(atoms(i)%where(axisint).ge.cuthere) then
nkeep=nkeep+1
end if
end do
nremove=natoms-nkeep
allocate(keepatoms(nkeep),rmatoms(nremove))
j=0
jrm=0
do i=1,natoms
if(atoms(i)%where(axisint).gt.cuthere) then
j=j+1
keepatoms(j)=atoms(i)
end if
if(atoms(i)%where(axisint).le.cuthere) then
jrm=jrm+1
rmatoms(jrm)=atoms(i)
end if
end do
case("sphere","SPHERE")
nkeep=0
select case(axisint)
case(0)
! keep atoms inside sphere
do i=1,natoms
!call frac2abs(atoms(i)%where,vecs,distvec)
distvec=atoms(i)%abswhere
distvec=distvec-origin
dist=absvec(distvec)
if(dist.le.cuthere) then
nkeep=nkeep+1
end if
end do
nremove=natoms-nkeep
allocate(keepatoms(nkeep),rmatoms(nremove))
j=0
jrm=0
do i=1,natoms
!call frac2abs(atoms(i)%where,vecs,distvec)
distvec=atoms(i)%abswhere
distvec=distvec-origin
dist=absvec(distvec)
if(dist.le.cuthere) then
j=j+1
keepatoms(j)=atoms(i)
end if
if(dist.gt.cuthere) then
jrm=jrm+1
rmatoms(jrm)=atoms(i)
end if
end do
case(1)
! keep atoms outside sphere
do i=1,natoms
!call frac2abs(atoms(i)%where,vecs,distvec)
distvec=atoms(i)%abswhere
distvec=distvec-origin
dist=absvec(distvec)
if(dist.gt.cuthere) then
nkeep=nkeep+1
end if
end do
nremove=natoms-nkeep
allocate(keepatoms(nkeep),rmatoms(nremove))
j=0
jrm=0
do i=1,natoms
!call frac2abs(atoms(i)%where,vecs,distvec)
distvec=atoms(i)%abswhere
distvec=distvec-origin
dist=absvec(distvec)
if(dist.gt.cuthere) then
j=j+1
keepatoms(j)=atoms(i)
end if
if(dist.le.cuthere) then
jrm=jrm+1
rmatoms(jrm)=atoms(i)
end if
end do
case default
nerr=nerr+1
print ferrmssg,
& "Unknown parameter. Use 'above' or 'below'"
if(isopen12) write(12,ferrmssg)
& "Unknown parameter. Use 'above' or 'below'"
return
end select
case("plane","PLANE")
nkeep=0
if (talk) print'(8x,"normal vector:",3(F12.6))',origin
select case(axisint)
case(0)
! keep atoms below plane
! "radius" is now distance from plane
! "origin" is now n1,n2,n3 (vector normal to plane)
do i=1,natoms
distvec=atoms(i)%abswhere
distvec=distvec-cuthere*origin/norm2(origin)
dist=dot_product(distvec,origin/norm2(origin))
if(dist.le.0) then
nkeep=nkeep+1
end if
end do
nremove=natoms-nkeep
allocate(keepatoms(nkeep),rmatoms(nremove))
j=0
jrm=0
do i=1,natoms
distvec=atoms(i)%abswhere
distvec=distvec-cuthere*origin/norm2(origin)
dist=dot_product(distvec,origin/norm2(origin))
if(dist.le.0) then
j=j+1
keepatoms(j)=atoms(i)
end if
if(dist.gt.0) then
jrm=jrm+1
rmatoms(jrm)=atoms(i)
end if
end do
case(1)
! keep atoms above plane
do i=1,natoms
distvec=atoms(i)%abswhere
distvec=distvec-cuthere*origin/norm2(origin)
dist=dot_product(distvec,origin/norm2(origin))
if(dist.gt.0) then
nkeep=nkeep+1
end if
end do
nremove=natoms-nkeep
allocate(keepatoms(nkeep),rmatoms(nremove))
j=0
jrm=0
do i=1,natoms
distvec=atoms(i)%abswhere
distvec=distvec-cuthere*origin/norm2(origin)
dist=dot_product(distvec,origin/norm2(origin))
if(dist.gt.0) then
j=j+1
keepatoms(j)=atoms(i)
end if
if(dist.le.0) then
jrm=jrm+1
rmatoms(jrm)=atoms(i)
end if
end do
case default
nerr=nerr+1
print ferrmssg,
& "Unknown parameter. cofima --help prints possible param &
&eters"
if(isopen12) write(12,ferrmssg)
& "Unknown parameter. cofima --help prints possible param &
&eters"
return
end select
case("dirplane","DIRPLANE")
nkeep=0
origin=origin(1)*vecs(1,:)+origin(2)*vecs(2,:) &
& +origin(3)*vecs(3,:)
if (talk) print'(8x,"normal vector:",3(F12.6))',origin
select case(axisint)
case(0)
! keep atoms below plane
! "radius" is now distance from plane
! "origin" is now n1,n2,n3 (vector normal to plane)
do i=1,natoms
distvec=atoms(i)%abswhere
distvec=distvec-cuthere*origin/norm2(origin)
dist=dot_product(distvec,origin/norm2(origin))
if(dist.le.0) then
nkeep=nkeep+1
end if
end do
nremove=natoms-nkeep
allocate(keepatoms(nkeep),rmatoms(nremove))
j=0
jrm=0
do i=1,natoms
distvec=atoms(i)%abswhere
distvec=distvec-cuthere*origin/norm2(origin)
dist=dot_product(distvec,origin/norm2(origin))
if(dist.le.0) then
j=j+1
keepatoms(j)=atoms(i)
end if
if(dist.gt.0) then
jrm=jrm+1
rmatoms(jrm)=atoms(i)
end if
end do
case(1)
! keep atoms above plane
do i=1,natoms
distvec=atoms(i)%abswhere
distvec=distvec-cuthere*origin/norm2(origin)
dist=dot_product(distvec,origin/norm2(origin))
if(dist.gt.0) then
nkeep=nkeep+1
end if
end do
nremove=natoms-nkeep
allocate(keepatoms(nkeep),rmatoms(nremove))
j=0
jrm=0
do i=1,natoms
distvec=atoms(i)%abswhere
distvec=distvec-cuthere*origin/norm2(origin)
dist=dot_product(distvec,origin/norm2(origin))
if(dist.gt.0) then
j=j+1
keepatoms(j)=atoms(i)
end if
if(dist.le.0) then
jrm=jrm+1
rmatoms(jrm)=atoms(i)
end if
end do
case default
nerr=nerr+1
print ferrmssg,
& "Unknown parameter. cofima --help prints possible param &
&eters"
if(isopen12) write(12,ferrmssg)
& "Unknown parameter. cofima --help prints possible param &
&eters"
return
end select
case("hklplane","HKLPLANE")
nkeep=0
call recipr_latt_vecs(vecs,gvecs)
if (talk) print '(8x,"rec. latt. vecs:",/,8x,3(F12.6),/,8x, &
& 3(F12.6),/,8x,3(F12.6))',gvecs(1,:),gvecs(2,:),gvecs(3,:)
if (talk) print '(8x,"h,k,l=",3(F12.6))',origin
origin=origin(1)*gvecs(1,:)+origin(2)*gvecs(2,:) &
& +origin(3)*gvecs(3,:)
if (talk) print'(8x,"normal vector:",3(F12.6))',origin
select case(axisint)
case(0)
! keep atoms below plane
! "radius" is now distance from plane
! "origin" is now h*b1,k*b2,l*b3 (b recipr latt vecs, vector normal to plane)
do i=1,natoms
distvec=atoms(i)%abswhere
distvec=distvec-cuthere*origin/norm2(origin)
dist=dot_product(distvec,origin/norm2(origin))
if(dist.le.0) then
nkeep=nkeep+1
end if
end do
nremove=natoms-nkeep
allocate(keepatoms(nkeep),rmatoms(nremove))
j=0
jrm=0
do i=1,natoms
distvec=atoms(i)%abswhere
distvec=distvec-cuthere*origin/norm2(origin)
dist=dot_product(distvec,origin/norm2(origin))
if(dist.le.0) then
j=j+1
keepatoms(j)=atoms(i)
end if
if(dist.gt.0) then
jrm=jrm+1
rmatoms(jrm)=atoms(i)
end if
end do
case(1)
! keep atoms above plane
do i=1,natoms
distvec=atoms(i)%abswhere
distvec=distvec-cuthere*origin/norm2(origin)
dist=dot_product(distvec,origin/norm2(origin))
if(dist.gt.0) then
nkeep=nkeep+1
end if
end do
nremove=natoms-nkeep
allocate(keepatoms(nkeep),rmatoms(nremove))
j=0
jrm=0
do i=1,natoms
distvec=atoms(i)%abswhere
distvec=distvec-cuthere*origin/norm2(origin)
dist=dot_product(distvec,origin/norm2(origin))
if(dist.gt.0) then
j=j+1
keepatoms(j)=atoms(i)
end if
if(dist.le.0) then
jrm=jrm+1
rmatoms(jrm)=atoms(i)
end if
end do
case default
nerr=nerr+1
print ferrmssg,
& "Unknown parameter. cofima --help prints possible param &
&eters"
if(isopen12) write(12,ferrmssg)
& "Unknown parameter. cofima --help prints possible param &
&eters"
return
end select
case default
nerr=nerr+1
print ferrmssg,
& "Unknown parameter. cofima --help prints possible param &
&eters"
if(isopen12) write(12,ferrmssg)
& "Unknown parameter. cofima --help prints possible param &
&eters"
return
end select
! determine new numbers of species asf
! get number of species
nspeckeep=0
do i=1,nkeep
newspecies=.true.
do j=1,i-1
if (keepatoms(j)%name(1:2).eq.
& keepatoms(i)%name(1:2))newspecies=.false.
end do
if(newspecies) nspeckeep=nspeckeep+1
end do
if(talk) then
if (isopen12) then
! write(12,'(8x,"Number of elements and atoms kept: ",I,I)'),
! & nspeckeep,nkeep
WRITE(FMT1,*) '(8x,"Number of elements and atoms kept: ",I',
& len(trim(FMT2)),',I',len(trim(FMT3)),')'
write(12,FMT1) nspeckeep,nkeep
else
! print '(8x,"Number of elements and atoms kept: ",I,I)',
! & nspeckeep,nkeep
WRITE(FMT1,*) '(8x,"Number of elements and atoms kept: ",I',
& len(trim(FMT2)),',I',len(trim(FMT3)),')'
print FMT1, nspeckeep,nkeep
end if
end if
close(21)
!get name and number of atoms of each species
allocate(keepspecies(nspeckeep))
k=1
do j=1,nkeep
newspecies=.true.
do i=1,j-1
if (keepatoms(i)%name(1:2).eq.keepatoms(j)%name(1:2))
& newspecies=.false.
end do
if(newspecies) then
keepspecies(k)%name=keepatoms(j)%name
k=k+1
end if
end do
! get numbers of atoms of each species
do k=1,nspeckeep
keepspecies(k)%howmany=0
do i=1,nkeep
if(keepatoms(i)%name(1:2).eq.keepspecies(k)%name(1:2))
& keepspecies(k)%howmany=keepspecies(k)%howmany+1
end do
end do
! write
call write_coords(cutoutfile,cutoutform,keepatoms,nkeep,
& keepspecies,nspeckeep,vecs)
if (hydr) then
!!! hydrogenate dangling bonds
allocate(atomsH(natoms))
atomsH(1:nkeep)=keepatoms(1:nkeep)
atomsH(nkeep+1:natoms)=rmatoms(1:nremove)
!atomsH(nkeep+1:natoms)%name(1:2)="H "
! 1. determine number of H atoms (=number of dangling bonds)
ndangb=0
do i=1,nkeep
call frac2abs(atomsH(i)%where,vecs,coords1)
do j=nkeep+1,natoms
call frac2abs(atomsH(j)%where,vecs,coords2)
do k1=-1,1,1
do k2=-1,1,1
do k3=-1,1,1
distvec=coords2-coords1+dble(k1)*vecs(1,:) &
& +dble(k2)*vecs(2,:)+dble(k3)*vecs(3,:)
if (absvec(distvec).le.nndist) ndangb=ndangb+1
end do ! k3
end do ! k2
end do ! k1
end do
end do
if(talk)
& print '(8x,"number of dangling bonds to hydrogenize:",I7)',ndangb
! place H at each dangling bond
allocate(keepatomsH(nkeep+ndangb))
keepatomsH(1:nkeep)=keepatoms(1:nkeep)
keepatomsH(nkeep+1:nkeep+ndangb)%name(1:2)="H "
k=nkeep+1
do i=1,nkeep
call frac2abs(atomsH(i)%where,vecs,coords1)
do j=nkeep+1,natoms
do k1=-1,1,1
do k2=-1,1,1
do k3=-1,1,1
call frac2abs(atomsH(j)%where,vecs,coords2)
distvec=coords2-coords1+dble(k1)*vecs(1,:) &
& +dble(k2)*vecs(2,:)+dble(k3)*vecs(3,:)
if (absvec(distvec).le.nndist) then
! place a H at a fraction of the bond distance
if(hdist.le.0.0D0) coords2=coords1+abs(hdist)*distvec
! place a H at a fixed distance (default: 1.25 Angs) along the former bond
if(hdist.gt.0.0D0) coords2=coords1+hdist*distvec
& /absvec(distvec)
call abs2frac(coords2,vecs,keepatomsH(k)%where)
! print*,"coords1,coords2,keepatomsH(k)%where:",
! & coords1,coords2,keepatomsH(k)%where
k=k+1
end if
end do ! k3
end do ! k2
end do ! k1
end do !j
end do ! i
! write
call getspecies(keepatomsH,keepspeciesH)
nspeckeepH=size(keepspeciesH)
filename(1:256)=" "
write(filename(1:5),'(A5)') "HYDR."
write(FMT1,*) '(A',len_trim(cutoutfile),')'
write(filename(6:5+len_trim(cutoutfile)),FMT1)
& trim(adjustl(cutoutfile))
!print*,"outfile:",filename
call write_coords(filename,cutoutform,keepatomsH,nkeep+ndangb,
& keepspeciesH,nspeckeepH,vecs)
end if ! hydr
if(talk) then
if(isopen12) then
write(12,fsubendext) 'cut'
else
print fsubendext, 'cut'
end if
end if
return
end subroutine cut
c--------------------------------------------------------------------
subroutine mergefiles(filename1,format1,filename2,format2,
& filename3,format3)
use defs
use readcoords
use writecoords
use misc
use replace
implicit none
! variables
character(len=*) filename1,filename2,filename3,format1,format2,
& format3
! internal variables
logical isopen12,newspecies
type(atom), allocatable :: atoms1(:),atoms2(:),atoms3(:)
type(element), allocatable :: species1(:),species2(:),species3(:)
integer natoms1,nspecies1,natoms2,nspecies2,
& natoms3,nspecies3
double precision vecs(1:3,1:3)
integer i,j,k,l
talk=.true.
! write output to unit 12, if open, else print to terminal
INQUIRE (unit=12, opened=isopen12)
if(talk) then
if(isopen12) then
write(12,fsubstart) trim('merge')
else
print fsubstart, trim(adjustl('merge'))
end if
end if
! read in first coordinates ...
print '(" ")'
print '(8x,"calling read_coords...")'
call read_coords(filename1,format1,atoms1,natoms1,species1,
& nspecies1,vecs)
print '(8x,"First coords read in.")'
! read in second coordinates ...
print '(" ")'
print '(8x,"calling read_coords...")'
call read_coords(filename2,format2,atoms2,natoms2,species2,
& nspecies2,vecs)
print '(8x,"Second coords read in.")'
print '(8x,"Merging ....")'
! old:
!natoms3=natoms1+natoms2
!allocate(atoms3(1:natoms3))
!atoms3(1:natoms1)=atoms1(1:natoms1)
!atoms3(natoms1+1:natoms3)=atoms2(1:natoms2)
! end old
! new:
call addatoms(atoms1,atoms2,atoms3)
natoms3=size(atoms3)
! end new
! get the number of species of the merged structure
call getspecies(atoms3,species3)
nspecies3=size(species3)
! write
call write_coords(filename3,format3,atoms3,natoms3,species3,
& nspecies3,vecs)
if(isopen12) then
write(12,fsubendext) 'merge'
else
print fsubendext, 'merge'
end if
return
end subroutine
c---------------------------------------------------------------------
end module