-
Notifications
You must be signed in to change notification settings - Fork 0
/
columnar_builder.f90
685 lines (529 loc) · 17.4 KB
/
columnar_builder.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
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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
! Module containing global variables
module global
integer*8 :: seed
real*8 :: r
end module
! Main program
program columnar_builder
use global
implicit none
character*10 :: fileformat
character*4,allocatable :: t(:),ti(:,:),tcurr(:),tucell(:)
integer :: atom_index=0,index0,index1,cellindex
integer :: i,j,h,k,l,m,n,nrepz
integer :: ngrains,natucell,natoms,natomsf,ncells,npartitions,cell,maxatcell=0
integer,allocatable :: natomscell(:),index(:,:),neighborscell(:,:),gindex(:)
integer :: incx,incy,incz
integer,parameter :: MIN_NGRAINS=4,NQUADRANTS=4,NNCELLS=9
real*4 :: xtrans,ytrans,ztrans
real*4 :: latpar,a0(3),dr0,dr1,dx0,dy0,dz0,dx1,dy1,dmin,gdist
real*4 :: box_length,column_length,dispx,dispy,dispz
real*4,allocatable :: gbx(:),gby(:)
real*4,allocatable :: x(:),y(:),z(:),xi(:,:),yi(:,:),zi(:,:),xcurr(:),ycurr(:),zcurr(:),xcell(:),ycell(:)
real*4,allocatable :: xucell(:),yucell(:),zucell(:)
real*4 :: dcell,max_rot_angle
real*4,allocatable :: theta(:)
real*4,parameter :: PARTITION_SIZE=10.0
real*8,parameter :: pi=3.14159265,degree2radian=0.0174532925
logical :: rnd_grain_nuclei,rnd_rot_angle
logical,allocatable :: writable(:)
open(10,file='polycrystal.tmp')
natoms=0
write(6,*) '-----------------------------------------------------------'
write(6,*) ' COLUMNAR_BUILDER '
write(6,*) 'A program for generating columnar polycrystals with tilt '
write(6,*) 'boundaries developed by Dr. Roberto Gomes de Aguiar Veiga '
write(6,*) ' at Universidade de São Paulo, Brazil (2012) '
write(6,*) '-----------------------------------------------------------'
write(6,*)
! User input
1 write(6,*) '===> Randomly determine the position of grain nuclei (true/false) and/or rotation angle (true/false)?'
read(*,*) rnd_grain_nuclei,rnd_rot_angle
2 write(6,*) '===> Number of atoms in the unit cell:'
read(*,*) natucell
if(natucell<=0)then
write(6,*) ' ERROR: Number of atoms in the unit cell must be a positive number greater than zero!'
goto 2
endif
allocate(xucell(natucell))
allocate(yucell(natucell))
allocate(zucell(natucell))
allocate(tucell(natucell))
3 write(6,*) '===> Lattice parameter, in Angstroms:'
read(*,*) latpar
if(latpar<=0.0)then
write(6,*) ' ERROR: The lattice parameter must be greater than zero!'
goto 3
endif
4 write(6,*) '===> Crystallographic parameters A, B, and C, in units of the lattice parameter:'
read(*,*) a0(1),a0(2),a0(3)
if(a0(1)<1.0.or.a0(2)<1.0.or.a0(3)<1.0)then
write(6,*) ' ERROR: The crystallographic parameters must be greater than or equal to one!'
goto 4
endif
a0(1)=a0(1)*latpar
a0(2)=a0(2)*latpar
a0(3)=a0(3)*latpar
5 write(6,*) '===> Provide the atom type (string, 4 chars maximum) and the coordinates (x,y,z), in units of'
write(6,*) ' the lattice parameter, for each atom in the unit cell:'
do i=1,natucell
read(*,*) tucell(i),xucell(i),yucell(i),zucell(i)
if(xucell(1)/=0.0.or.yucell(1)/=0.0.or.zucell(1)/=0.0)then
write(6,*) ' ERROR: First atom of the unit cell must be at (0,0,0)!'
goto 5
endif
xucell(i)=xucell(i)*latpar
yucell(i)=yucell(i)*latpar
zucell(i)=zucell(i)*latpar
enddo
6 write(6,*) '===> Box length in X and Y, in Angstroms:'
read(*,*) box_length
if(box_length<a0(1).or.box_length<a0(2))then
write(6,*) ' ERROR: Box length must be greater than the unit cell length in X and Y!'
goto 6
endif
7 write(6,*) '===> Number of unit cells repeated in the Z (textured) direction:'
read(*,*) nrepz
if(nrepz<=0)then
write(6,*) ' ERROR: The number of cells in Z must be greater than zero!'
goto 7
endif
column_length=real(nrepz)*a0(3)
write(6,*) ' Box volume is ',box_length*box_length*column_length,' Angs^3.'
8 write(6,*) '===> Number of grains:'
read(*,*) ngrains
if(ngrains<MIN_NGRAINS)then
write(6,*) ' ERROR: At least ',MIN_NGRAINS,' grains are required!'
goto 8
endif
allocate(gbx(ngrains))
allocate(gby(ngrains))
allocate(theta(ngrains))
allocate(xi(natucell,ngrains))
allocate(yi(natucell,ngrains))
allocate(zi(natucell,ngrains))
allocate(ti(natucell,ngrains))
allocate(xcurr(natucell))
allocate(ycurr(natucell))
allocate(zcurr(natucell))
allocate(tcurr(natucell))
9 write(6,*) '===> Format of the output file (lammps or xyz):'
read(*,*) fileformat
if(.not.(fileformat=='lammps'.or.fileformat=='xyz'))then
write(6,*) ' ERROR: Wrong file format!'
goto 9
endif
10 if(rnd_grain_nuclei.or.rnd_rot_angle)then
write(6,*) '===> This code uses a random number generator. Please provide a seed for the generator (an integer):'
read(*,*) seed
if(seed<=0)then
write(6,*) ' ERROR: The seed must be a positive integer!'
goto 10
endif
if(rnd_grain_nuclei)then
write(6,*) '===> Minimum distance between two grain nuclei, in Angstroms:'
read(*,*) gdist
if(gdist<=0.0)then
write(6,*) ' ERROR: The distance between grain nuclei must be grater than zero!'
goto 10
endif
endif
if(rnd_rot_angle)then
write(6,*) '===> You have to provide the maximum rotation angle, in degrees:'
read(*,*) max_rot_angle
if(max_rot_angle<0.0.or.max_rot_angle>180.0)then
write(6,*) ' ERROR: The maximum rotation angle must be within the range 0-180!'
goto 10
endif
max_rot_angle=max_rot_angle*degree2radian
endif
endif
! Manually define the position of grain nuclei and/or the rotation angle
if((.not.rnd_grain_nuclei).and.(.not.rnd_rot_angle))then
write(6,*) '===> You have to provide the positions of grain nuclei (x,y), in Angstroms, and the rotation angle about z,'
write(6,*) ' in degrees, for each grain:'
do i=1,ngrains
read(*,*) gbx(i),gby(i),theta(i)
theta(i)=theta(i)*degree2radian
enddo
elseif(.not.rnd_grain_nuclei.and.rnd_rot_angle)then
write(6,*) '===> You have to provide the positions of grain nuclei (x,y) for each grain, in Angstroms:'
do i=1,ngrains
read(*,*) gbx(i),gby(i)
enddo
elseif(.not.rnd_rot_angle.and.rnd_grain_nuclei)then
write(6,*) '===> You have to provide the rotation angle for each grain, in degrees:'
do i=1,ngrains
read(*,*) theta(i)
theta(i)=theta(i)*degree2radian
enddo
endif
! Randomly generate the positions of grain nuclei
do i=1,ngrains
if(rnd_grain_nuclei)then
if(i==1)then
gbx(i)=box_length/2.0
gby(i)=box_length/2.0
else
50 call rand
gbx(i)=r*box_length
call rand
gby(i)=r*box_length
do j=1,i-1
dx0=gbx(j)-gbx(i)
dy0=gby(j)-gby(i)
if(dx0>box_length/2.0)then
dx0=dx0-box_length
elseif(dx0<-box_length/2.0)then
dx0=dx0+box_length
endif
if(dy0>box_length/2.0)then
dy0=dy0-box_length
elseif(dy0<-box_length/2.0)then
dy0=dy0+box_length
endif
dr0=sqrt(dx0*dx0+dy0*dy0)
if(dr0<gdist)then
goto 50
endif
enddo
endif
endif
! Create the first cell, the one that will be repeated in 3 dimensions
do j=1,natucell
ti(j,i)=tucell(j)
xi(j,i)=gbx(i)+xucell(j)
yi(j,i)=gby(i)+yucell(j)
zi(j,i)=zucell(j)
end do
enddo
!!! A ser modificado
! In order to compare the distance between atoms in large simulations boxes efficiently, the simulation box should be partitioned
npartitions=nint(box_length/PARTITION_SIZE)
ncells=npartitions*npartitions
write(6,*) ' The system will be divided into ',ncells,' cells for comparing interatomic distances more efficiently.'
allocate(neighborscell(ncells,NNCELLS))
allocate(natomscell(ncells))
allocate(xcell(ncells))
allocate(ycell(ncells))
natomscell(:)=0
dcell=box_length/npartitions
k=0
! Here the center of each cell is assigned
do i=1,npartitions
do j=1,npartitions
k=k+1
xcell(k)=i*dcell-dcell/2.0
ycell(k)=j*dcell-dcell/2.0
enddo
enddo
dr1=sqrt(2.0)*dcell
! In the next loop, the first and second nearest neighbors of each cell are determined
do i=1,ncells
l=0
do j=1,ncells
dx0=xcell(j)-xcell(i)
dy0=ycell(j)-ycell(i)
if(dx0>box_length/2.0)then
dx0=dx0-box_length
elseif(dx0<-box_length/2.0)then
dx0=dx0+box_length
endif
if(dy0>box_length/2.0)then
dy0=dy0-box_length
elseif(dy0<-box_length/2.0)then
dy0=dy0+box_length
endif
dr0=sqrt(dx0*dx0+dy0*dy0)
if(dr0<=dr1)then
l=l+1
neighborscell(i,l)=j
endif
enddo
enddo
! Grains grow from their respective nuclei
do i=1,ngrains
write(6,*) ' Generating grain',i
write(6,*) ' From the grain nucleus at (',gbx(i),gby(i),')'
! The rotation angle is randomly defined...
if(rnd_rot_angle)then
call rand
theta(i)=r*max_rot_angle
endif
write(6,*) ' Rotation angle:',theta(i)/pi,'*pi'
! Fill a cylinder around the grain nucleus with atoms
do j=1,NQUADRANTS
if(j==1)then
incx=1
incy=1
write(6,*) ' 25%...'
elseif(j==2)then
incx=-1
incy=1
write(6,*) ' 50%...'
elseif(j==3)then
incx=-1
incy=-1
write(6,*) ' 75%...'
elseif(j==4)then
incx=1
incy=-1
write(6,*) ' Done!'
endif
! Begin: Z direction
do l=1,nrepz
dispz=(l-1)*a0(3)
! Begin: X direction
do h=1,int(box_length/(2.0*a0(1)))
dispx=(h-1)*a0(1)*incx
! Begin: Y direction
do k=1,int(box_length/(2.0*a0(2)))
dispy=(k-1)*a0(2)*incy
! Begin: Current atom
do m=1,natucell
xtrans=xi(m,i)+dispx-gbx(i)
ytrans=yi(m,i)+dispy-gby(i)
ztrans=zi(m,i)+dispz
xcurr(m)=(xtrans*cos(theta(i))-ytrans*sin(theta(i)))+gbx(i)
ycurr(m)=(xtrans*sin(theta(i))+ytrans*cos(theta(i)))+gby(i)
zcurr(m)=ztrans
tcurr(m)=ti(m,i)
! Put the atom back into the box, if necessary
do n=1,2
if(xcurr(m)>box_length)then
xcurr(m)=xcurr(m)-box_length
elseif(xcurr(m)<0.0)then
xcurr(m)=xcurr(m)+box_length
endif
if(ycurr(m)>box_length)then
ycurr(m)=ycurr(m)-box_length
elseif(ycurr(m)<0.0)then
ycurr(m)=ycurr(m)+box_length
endif
if(zcurr(m)>column_length)then
zcurr(m)=zcurr(m)-column_length
elseif(zcurr(m)<0.0)then
zcurr(m)=zcurr(m)+column_length
endif
enddo
! Check distances from the current grain
dx0=xcurr(m)-gbx(i)
dy0=ycurr(m)-gby(i)
if(dx0>box_length/2.0)then
dx0=dx0-box_length
elseif(dx0<-box_length/2.0)then
dx0=dx0+box_length
endif
if(dy0>box_length/2.0)then
dy0=dy0-box_length
elseif(dy0<-box_length/2.0)then
dy0=dy0+box_length
endif
dr0=sqrt(dx0*dx0+dy0*dy0)
! Begin: Check distance from the other grains
if(m==1)then
do n=1,ngrains
if(n/=i)then
dx1=xcurr(m)-gbx(n)
dy1=ycurr(m)-gby(n)
if(dx1>box_length/2.0)then
dx1=dx1-box_length
elseif(dx1<-box_length/2.0)then
dx1=dx1+box_length
endif
if(dy1>box_length/2.0)then
dy1=dy1-box_length
elseif(dy1<-box_length/2.0)then
dy1=dy1+box_length
endif
dr1=sqrt(dx1*dx1+dy1*dy1)
! Check if this atom is closer to another grain nucleus
if(dr1<dr0)then
goto 500
endif
endif
700 end do ! End: Check distance to other grains
endif
natoms=natoms+1
! Begin: Assign a cell to the current atom
dr1=box_length
do n=1,ncells
dx0=xcurr(m)-xcell(n)
dy0=ycurr(m)-ycell(n)
if(dx0>box_length/2.0)then
dx0=dx0-box_length
elseif(dx0<-box_length/2.0)then
dx0=dx0+box_length
endif
if(dy0>box_length/2.0)then
dy0=dy0-box_length
elseif(dy0<-box_length/2.0)then
dy0=dy0+box_length
endif
dr0=sqrt(dx0*dx0+dy0*dy0)
if(dr0<=dcell/2.0)then
cell=n
exit
elseif(dr0<=dr1)then
dr1=dr0
cell=n
endif
enddo ! End: Assign a cell to the current atom
natomscell(cell)=natomscell(cell)+1
write(10,*) tcurr(m),xcurr(m),ycurr(m),zcurr(m),cell,i
600 enddo ! End: Current atom
500 enddo ! End: Y direction
400 enddo ! End: X direction
300 enddo ! End: Z direction
200 enddo ! End: Quadrant
100 enddo ! End: Grain
write(6,*) ' Until now, ',natoms,' atoms were created. Some may be too close to other atoms.'
deallocate(gbx)
deallocate(gby)
deallocate(theta)
deallocate(xi)
deallocate(yi)
deallocate(zi)
deallocate(ti)
deallocate(xucell)
deallocate(yucell)
deallocate(tucell)
deallocate(xcurr)
deallocate(ycurr)
deallocate(zcurr)
deallocate(tcurr)
! Check the maximum number of atoms in a cell
do i=1,ncells
if(natomscell(i)>maxatcell) maxatcell=natomscell(i)
enddo
write(6,*) ' The maximum number of atoms per partition cell is ',maxatcell,'.'
rewind(10)
! Use an arbitrarily defined cutoff distance to remove atoms too close to other atoms
11 write(6,*) '===> Minimum distance between two atoms, in Angstroms:'
read(*,*) dmin
if(dmin<0.0)then
write(6,*) 'Minimum distance must be greater than zero!'
goto 11
endif
allocate(x(natoms))
allocate(y(natoms))
allocate(z(natoms))
allocate(t(natoms))
allocate(gindex(natoms))
allocate(writable(natoms))
allocate(index(ncells,maxatcell))
writable(:)=.true.
index(:,:)=0
! Store in matrices all information about the atoms in the grains
do i=1,natoms
read(10,*) t(i),x(i),y(i),z(i),cellindex,gindex(i)
do j=1,natomscell(cellindex)
if(index(cellindex,j)==0)then
index(cellindex,j)=i
exit
endif
enddo
enddo
natomsf=natoms
! Check distances between atoms of adjacent cells
write(6,*) ' Checking distances between atoms in adjacent cells...'
! Begin: Cell
do i=1,ncells
! Begin: Atom in the cell
do j=1,natomscell(i)
index0=index(i,j)
if(.not.writable(index0))then
goto 900
endif
! Begin: Neighbor cell
do k=1,NNCELLS
cellindex=neighborscell(i,k)
if(cellindex<i)then
goto 1000
endif
! Begin: Atom in the neighbor cell
do l=1,natomscell(cellindex)
index1=index(cellindex,l)
if((cellindex==i.and.index1<=index0).or.(.not.writable(index1)))then
goto 1100
endif
dx0=x(index1)-x(index0)
dy0=y(index1)-y(index0)
dz0=z(index1)-z(index0)
if(dx0>box_length/2.0)then
dx0=dx0-box_length
elseif(dx0<-box_length/2.0)then
dx0=dx0+box_length
endif
if(dy0>box_length/2.0)then
dy0=dy0-box_length
elseif(dy0<-box_length/2.0)then
dy0=dy0+box_length
endif
if(dz0>column_length/2.0)then
dz0=dz0-column_length
elseif(dz0<-column_length/2.0)then
dz0=dz0+column_length
endif
dr0=sqrt(dx0*dx0+dy0*dy0+dz0*dz0)
if(dr0<=dmin)then
writable(index1)=.false.
natomsf=natomsf-1
endif
1100 enddo ! End: Atom in the neighbor cell
1000 enddo ! End: Neighbor cell
900 enddo ! End: Atom in the cell
800 end do ! End: Cell
! Write the coordinates
write(6,*) ' Writing coordinates...',natomsf,'atoms left!'
if(fileformat=='lammps')then
open(11,file='polycrystal.lammpstrj')
write(11,'(a)') 'ITEM: TIMESTEP'
write(11,'(a)') '0'
write(11,'(a)') 'ITEM: NUMBER OF ATOMS'
write(11,*) natomsf
write(11,'(a)') 'ITEM: BOX BOUNDS'
write(11,*) '0.0',box_length
write(11,*) '0.0',box_length
write(11,*) '0.0',column_length
write(11,'(a)') 'ITEM: ATOMS id type xs ys zs grain'
elseif(fileformat=='xyz')then
open(11,file='polycrystal.xyz')
write(11,*) natomsf
write(11,*) 'Box dimensions:',box_length,box_length,column_length
endif
do i=1,natoms
if(writable(i))then
if(fileformat=='lammps')then
atom_index=atom_index+1
write(11,*) atom_index,t(i),x(i)/box_length,y(i)/box_length,z(i)/column_length,gindex(i)
elseif(fileformat=='xyz')then
write(11,*) t(i),x(i),y(i),z(i),gindex(i)
endif
endif
enddo
deallocate(x)
deallocate(y)
deallocate(z)
deallocate(t)
deallocate(writable)
deallocate(index)
deallocate(natomscell)
deallocate(neighborscell)
close(10)
close(11)
stop 'Done!'
end program columnar_builder
! Subroutine that generates random numbers
subroutine rand()
use global
implicit none
integer*8, parameter :: m=714025
integer*8, parameter :: a=150889
integer*8, parameter :: c=1366
real*8, parameter :: d=30629.0
real*8 :: tmp
seed=mod((a*seed+c),m)
tmp=real(seed)/d
r=tmp-int(tmp)
end subroutine