-
Notifications
You must be signed in to change notification settings - Fork 0
/
cell.f90
1076 lines (924 loc) · 48.3 KB
/
cell.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
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
!****m* cell/cell =============================================================!
! NAME !
! cell !
!------------------------------------------------------------------------------!
! DESCRIPTION !
! Module containing the cell type and routines for manipulating cells. !
! !
! The cell contains the atomic species, masses and positions (in cartesian !
! and fractional spaces), as well as the lattice and reciprocal lattice !
! vectors. !
!------------------------------------------------------------------------------!
! AUTHOR !
! Aaron Hopkinson !
!****==========================================================================!
module cell
use constants, only: dp
implicit none
private
type, public :: cell_type
integer :: natoms
real(kind=dp) :: volume
real(kind=dp), dimension(3,3) :: lattice_vectors ! columns are a, b, c
real(kind=dp), dimension(3,3) :: recip_lattice
real(kind=dp), dimension(:,:), allocatable :: atom_frac_pos ! ndims, natoms
real(kind=dp), dimension(:,:), allocatable :: atom_cart_pos ! ndims, natoms
real(kind=dp), dimension(:), allocatable :: atom_mass
integer, dimension(:), allocatable :: atom_species
real(kind=dp) :: min_img_cutoff
end type cell_type
! cell independent min image vars:
logical, save :: min_img_initialized = .false.
integer, save, dimension(3,27) :: min_img_c_vectors
! temporary cell perturbation vars (not required after init):
real(kind=dp) :: lattice_perturb, atom_perturb
! public routines:
public :: cell_init
public :: cell_write
public :: cell_allocate
public :: cell_deallocate
public :: cell_copy
public :: cell_read_input
public :: cell_shift_to_unit_cell
public :: cell_frac_to_cart
public :: cell_cart_to_frac
public :: cell_get_recip
public :: cell_min_img_init
public :: cell_min_img
public :: cell_min_img_vec
public :: cell_calc_vol
public :: cell_supercell
public :: cell_perturb
public :: cell_reallocate
public :: cell_read_checkpoint
public :: cell_write_checkpoint
contains
! checkpointing things.. checkpoint_open must be called first! these are low level...
subroutine cell_read_checkpoint(cell)
use checkpoint, only: checkpoint_unit
implicit none
type(cell_type), intent(inout) :: cell
integer :: i, j
read(checkpoint_unit) cell%natoms
call cell_reallocate(cell)
read(checkpoint_unit) cell%volume
do i = 1, 3
do j = 1, 3
read(checkpoint_unit) cell%lattice_vectors(j,i)
end do
end do
do i = 1, 3
do j = 1, 3
read(checkpoint_unit) cell%recip_lattice(j,i)
end do
end do
do i = 1, cell%natoms
do j = 1, 3
read(checkpoint_unit) cell%atom_frac_pos(j,i)
end do
end do
do i = 1, cell%natoms
do j = 1, 3
read(checkpoint_unit) cell%atom_cart_pos(j,i)
end do
end do
do i = 1, cell%natoms
read(checkpoint_unit) cell%atom_mass(i)
end do
do i = 1, cell%natoms
read(checkpoint_unit) cell%atom_species(i)
end do
read(checkpoint_unit) cell%min_img_cutoff
end subroutine cell_read_checkpoint
subroutine cell_write_checkpoint(cell)
use checkpoint, only: checkpoint_unit
implicit none
type(cell_type), intent(in) :: cell
integer :: i, j
write(checkpoint_unit) cell%natoms
write(checkpoint_unit) cell%volume
do i = 1, 3
do j = 1, 3
write(checkpoint_unit) cell%lattice_vectors(j,i)
end do
end do
do i = 1, 3
do j = 1, 3
write(checkpoint_unit) cell%recip_lattice(j,i)
end do
end do
do i = 1, cell%natoms
do j = 1, 3
write(checkpoint_unit) cell%atom_frac_pos(j,i)
end do
end do
do i = 1, cell%natoms
do j = 1, 3
write(checkpoint_unit) cell%atom_cart_pos(j,i)
end do
end do
do i = 1, cell%natoms
write(checkpoint_unit) cell%atom_mass(i)
end do
do i = 1, cell%natoms
write(checkpoint_unit) cell%atom_species(i)
end do
write(checkpoint_unit) cell%min_img_cutoff
end subroutine cell_write_checkpoint
!****s* cell/perturb ==========================================================!
! NAME !
! cell_perturb (PUBLIC) !
!------------------------------------------------------------------------------!
! ARGUMENTS !
! type(cell_type), intent(inout) :: cell !
! real(kind=dp), intent(in) :: atomic_pert !
! real(kind=dp), intent(in) :: lattice_pert !
!------------------------------------------------------------------------------!
! DESCRPTION !
! A subroutine to perturb the atomic positions and/or lattice parameters of !
! a given cell. !
!------------------------------------------------------------------------------!
! AUTHOR !
! Edward Higgins !
!==============================================================================!
subroutine cell_perturb(cell, atomic_pert, lattice_pert)
use constants, only: pi
use io, only: io_err
use algor, only: algor_uniform_rand
implicit none
type(cell_type), intent(inout) :: cell
real(kind=dp), optional, intent(in) :: atomic_pert
real(kind=dp), optional, intent(in) :: lattice_pert
real(kind=dp), dimension(3,3) :: new_lattice
real(kind=dp) :: r, theta, phi
integer :: iatom
if ((.not. present(atomic_pert)) .and. (.not. present(lattice_pert))) &
call io_err("cell_perturb: No perturbation to apply")
if (present(lattice_pert)) then
if (lattice_pert .gt. 0.0_dp) then
new_lattice(1,:) = [ cell%lattice_vectors(1,1) + algor_uniform_rand(-lattice_pert/2, lattice_pert/2), &
cell%lattice_vectors(1,2) + algor_uniform_rand(-lattice_pert/2, lattice_pert/2), &
cell%lattice_vectors(1,3) + algor_uniform_rand(-lattice_pert/2, lattice_pert/2) ]
new_lattice(2,:) = [ cell%lattice_vectors(2,1) + algor_uniform_rand(-lattice_pert/2, lattice_pert/2), &
cell%lattice_vectors(2,2) + algor_uniform_rand(-lattice_pert/2, lattice_pert/2), &
cell%lattice_vectors(2,3) + algor_uniform_rand(-lattice_pert/2, lattice_pert/2) ]
new_lattice(3,:) = [ cell%lattice_vectors(3,1) + algor_uniform_rand(-lattice_pert/2, lattice_pert/2), &
cell%lattice_vectors(3,2) + algor_uniform_rand(-lattice_pert/2, lattice_pert/2), &
cell%lattice_vectors(3,3) + algor_uniform_rand(-lattice_pert/2, lattice_pert/2) ]
cell%lattice_vectors = new_lattice
! Sync the cell
call cell_calc_vol(cell)
call cell_get_recip(cell)
call cell_frac_to_cart(cell)
call cell_min_img_init(cell)
end if
end if
if (present(atomic_pert)) then
if (atomic_pert .gt. 0.0_dp) then
do iatom = 1, cell%natoms
r = algor_uniform_rand(0.0_dp, 1.0_dp) ** (1.0_dp/3.0_dp)
r = r * atomic_pert
theta = algor_uniform_rand(-pi, pi)
phi = algor_uniform_rand(0.0_dp, 1.0_dp)
phi = acos(2.0_dp*phi - 1.0_dp)
cell%atom_cart_pos(1,iatom) = cell%atom_cart_pos(1,iatom) + r * cos(theta) * sin(phi)
cell%atom_cart_pos(2,iatom) = cell%atom_cart_pos(2,iatom) + r * sin(theta) * sin(phi)
cell%atom_cart_pos(3,iatom) = cell%atom_cart_pos(3,iatom) + r * cos(phi)
end do
! Sync the cell
call cell_cart_to_frac(cell)
call cell_shift_to_unit_cell(cell)
call cell_frac_to_cart(cell)
end if
end if
end subroutine cell_perturb
subroutine cell_supercell(cell, supercell_a, supercell_b, supercell_c)
use io, only: io_err
implicit none
type(cell_type), intent(inout) :: cell
integer, intent(in) :: supercell_a
integer, intent(in) :: supercell_b
integer, intent(in) :: supercell_c
real(kind=dp), allocatable, dimension(:,:) :: tmp_atom_frac_pos
real(kind=dp), allocatable, dimension(:) :: tmp_atom_mass
integer, allocatable, dimension(:) :: tmp_atom_species
integer, dimension(3) :: nsc, isc
real(kind=dp), dimension(3) :: dx_frac
integer :: icell, jcell, kcell, tmp_natoms
integer :: iatom, icomp, old_atom, new_atom, istat
! error checking (valid number supercells)
if ((supercell_a .lt. 1) .or. (supercell_b .lt. 1) .or. (supercell_c .lt. 1)) then
call io_err("cell_supercell: Supercell size less than 1 along one or more dimensions")
end if
nsc(:) = (/ supercell_a, supercell_b, supercell_c /)
dx_frac(:) = (/ 1.0_dp/real(nsc(1),dp), 1.0_dp/real(nsc(2),dp), 1.0_dp/real(nsc(3),dp) /)
tmp_natoms = cell%natoms*nsc(1)*nsc(2)*nsc(3)
! allocate tmp arrays
allocate(tmp_atom_frac_pos(3, tmp_natoms), stat=istat)
if (istat .ne. 0) call io_err("cell_supercell: Could not allocate tmp_atom_frac_pos array")
allocate(tmp_atom_mass(tmp_natoms), stat=istat)
if (istat .ne. 0) call io_err("cell_supercell: Could not allocate tmp_atom_mass array")
allocate(tmp_atom_species(tmp_natoms), stat=istat)
if (istat .ne. 0) call io_err("cell_supercell: Could not allocate tmp_atom_species array")
! expand lattice vectors - icomp runs over a, b, c
do icomp = 1, 3
cell%lattice_vectors(:,icomp) = cell%lattice_vectors(:,icomp)*real(nsc(icomp),dp)
end do
! shift everything down to 'corner' of cell
do iatom = 1, cell%natoms
do icomp = 1, 3
cell%atom_frac_pos(icomp, iatom) = cell%atom_frac_pos(icomp, iatom)*dx_frac(icomp)
end do
end do
! add in extra atoms to tmp arrays
new_atom = 1
do icell = 1, nsc(1)
do jcell = 1, nsc(2)
do kcell = 1, nsc(3)
isc(:) = (/ icell-1, jcell-1, kcell-1 /)
do old_atom = 1, cell%natoms
do icomp = 1, 3
tmp_atom_frac_pos(icomp, new_atom) = cell%atom_frac_pos(icomp, old_atom) + real(isc(icomp),dp)*dx_frac(icomp)
end do
tmp_atom_mass(new_atom) = cell%atom_mass(old_atom)
tmp_atom_species(new_atom) = cell%atom_species(old_atom)
new_atom = new_atom + 1
end do
end do
end do
end do
! give cell more atoms and reallocate to correct size
cell%natoms = tmp_natoms
call cell_reallocate(cell)
! copy tmp data over - deep copy
do iatom = 1, cell%natoms
cell%atom_frac_pos(:, iatom) = tmp_atom_frac_pos(:, iatom)
cell%atom_mass(iatom) = tmp_atom_mass(iatom)
cell%atom_species(iatom) = tmp_atom_species(iatom)
end do
! update rest of cell - order important
call cell_calc_vol(cell)
call cell_get_recip(cell)
call cell_frac_to_cart(cell)
call cell_min_img_init(cell)
! deallocate tmp arrays
deallocate(tmp_atom_frac_pos, stat=istat)
if (istat .ne. 0) call io_err("cell_supercell: Could not deallocate tmp_atom_frac_pos array")
deallocate(tmp_atom_mass, stat=istat)
if (istat .ne. 0) call io_err("cell_supercell: Could not deallocate tmp_atom_mass array")
deallocate(tmp_atom_species, stat=istat)
if (istat .ne. 0) call io_err("cell_supercell: Could not deallocate tmp_atom_species array")
end subroutine cell_supercell
!****s* cell/cell_write =======================================================!
! NAME !
! cell_write (PUBLIC) !
!------------------------------------------------------------------------------!
! ARGUMENTS !
! type(cell_type), intent(in) :: cell !
! !
! logical, optional, intent(in) :: suppress_output_file !
!------------------------------------------------------------------------------!
! DESCRIPTION !
! Writes the contents of the cell to file seedname.cell (unless !
! suppress_output_file is present and .true., in which case it outputs to !
! stdout unit). !
! !
! If there is no seedname (very unlikely), we instead write to whatever !
! stdout is connected to. !
!==============================================================================!
subroutine cell_write(cell, suppress_output_file)
use io, only: seedname, stdout, io_open_file, io_close_file
use constants, only: element_symbol
implicit none
type(cell_type), intent(in) :: cell
logical, optional, intent(in) :: suppress_output_file
integer :: out_unit, i, j
!+----------------------------------------------------------------------------------------------------------------------+
!| CELL |
!+-----------------------------------------------------------+----------------------------------------------------------+
!| lattice vectors | reciprocal lattice vectors |
!| a b c | a* b* c* |
!| fffff.ffffffffffff fffff.ffffffffffff fffff.ffffffffffff | fffff.ffffffffffff fffff.ffffffffffff fffff.ffffffffffff |
!+-----------------------------------------------------------+----------------------------------------------------------+
!| volume fffffffff.ffffffff |
!+----------------------------------------------------------------------------------------------------------------------+
!| num atoms iiiiiiiiii |
!+----------+---------------+---------------------------------------------+---------------------------------------------+
!| species | mass | cartesian position | fractional position |
!| | | x y z | u v w |
!| aaa | ffffff.ffffff | fffff.fffffff fffff.fffffff fffff.fffffff | fff.fffffffff fff.fffffffff fff.fffffffff |
!+----------+---------------+---------------------------------------------+---------------------------------------------+
out_unit = stdout
if ((.not. present(suppress_output_file)) .or. (.not. suppress_output_file)) then
! only open file if we have a seedname, regardless of suppress_output_file
if (len_trim(seedname) .ge. 1) then
call io_open_file(trim(seedname)//'.cell', out_unit, 'replace')
end if
end if
write(out_unit, "('+', 118('-'), '+')")
write(out_unit, "('|', 57X, 'CELL', 57X, '|')")
write(out_unit, "('+', 59('-'), '+', 58('-'), '+')")
write(out_unit, "('|', 22X, 'lattice vectors', 22X, '|', 16X, 'reciprocal lattice vectors', 16X, '|')")
write(out_unit, "('|', 11X, 'a', 18X, 'b', 18X, 'c', 9X, '|', 10X, 'a*', 17X, 'b*', 17X, 'c*', 8X, '|')")
do i = 1, 3
write(out_unit, "('|', 1X, 3(F18.12, 1X), 1X, '|', 1X, 3(F18.12, 1X), '|')") &
& (cell%lattice_vectors(i,j), j=1,3), (cell%recip_lattice(i,j), j=1,3) ! might need to transpose recip_lattice
end do
write(out_unit, "('+', 59('-'), '+', 58('-'), '+')")
write(out_unit, "('|', 1X, 'volume', 1X, F18.8, 92X, '|')") cell%volume
write(out_unit, "('+', 118('-'), '+')")
write(out_unit, "('|', 1X, 'num atoms', 1X, I10, 97X, '|')") cell%natoms
write(out_unit, "('+', 10('-'), '+', 15('-'), '+', 45('-'), '+', 45('-'), '+')")
write(out_unit, "('|', 1X, 'species', 2X, '|', 5X, 'mass', 6X, '|', 14X, &
& 'cartesian position', 13X, '|', 13X, 'fractional position', 13X, '|')")
write(out_unit, "('|', 10X, '|', 15X, '|', 10X, 'x', 13X, 'y', 13X, 'z', 6X, '|', 9X, 'u', 13X, 'v', 13X, 'w', 7X, '|')")
do i = 1, cell%natoms
write(out_unit, "('|', 3X, A3, 4X, '| ', F13.6, 1X, '|', 2X, 3(F13.7, 1X), 1X, '|', 2X, 3(F13.9, 1X), 1X, '|')") &
& element_symbol(cell%atom_species(i)), cell%atom_mass(i), (cell%atom_cart_pos(j,i), j=1,3), (cell%atom_frac_pos(j,i), j=1,3)
end do
write(out_unit, "('+', 10('-'), '+', 15('-'), '+', 45('-'), '+', 45('-'), '+')")
! only close file if we opened a new one
if (out_unit .ne. stdout) call io_close_file(out_unit)
end subroutine cell_write
!****s* cell/cell_init ========================================================!
! NAME !
! cell_init (PUBLIC) !
!------------------------------------------------------------------------------!
! ARGUMENTS !
! type(cell_type), intent(inout) :: cell !
!------------------------------------------------------------------------------!
! DESCRIPTION !
! Initializes cell ready for use. !
! !
! Gets lattice vectors, atomic species and fractional positions from input !
! (via cell_read_input) then converts everything to atomic units and !
! populates the rest of the cell (reciprocal lattice vectors, cartesian !
! positions, masses etc). !
!------------------------------------------------------------------------------!
! NOTES !
! If we allow for atomic positions to be inputted in cartesian form, this !
! routine will need updating. !
!==============================================================================!
subroutine cell_init(cell)
use constants, only: element_mass, units_natural_to_atomic
implicit none
type(cell_type), intent(inout) :: cell
integer :: i
cell%natoms = 0
! get cell from input - this gets the lattice vectors, atomic species and positions (in fractional coordinates)
call cell_read_input(cell)
! with known atomic species, can populate mass array
do i = 1, cell%natoms
! mass units are in amu, we want to rescale such that electron_mass = 1
cell%atom_mass(i) = units_natural_to_atomic(mass=element_mass(cell%atom_species(i)))
end do
! rescale lattice vectors from "natural" units to atomic units
cell%lattice_vectors = units_natural_to_atomic(length=cell%lattice_vectors)
call cell_calc_vol(cell)
! must be done after lattice vectors have had units converted:
call cell_get_recip(cell) ! need vol first
call cell_shift_to_unit_cell(cell)
call cell_frac_to_cart(cell)
call cell_min_img_init(cell)
! perform perturbation if necessary
atom_perturb = units_natural_to_atomic(length=atom_perturb)
lattice_perturb = units_natural_to_atomic(length=lattice_perturb)
call cell_perturb(cell, atom_perturb, lattice_perturb)
end subroutine cell_init
!****s* cell/cell_allocate ====================================================!
! NAME !
! cell_allocate (PUBLIC) !
!------------------------------------------------------------------------------!
! ARGUMENTS !
! type(cell_type), intent(inout) :: cell !
!------------------------------------------------------------------------------!
! DESCRIPTION !
! Allocates all of the allocatable arrays within the cell given the number !
! of atoms contained within the cell. (Not the lattice_vectors and !
! recip_lattice arrays, which are statically allocated.) !
!------------------------------------------------------------------------------!
! NOTES !
! cell%natoms must be > 0. !
! !
! None of the arrays must already be allocated. !
!==============================================================================!
subroutine cell_allocate(cell)
use io, only: io_err
implicit none
type(cell_type), intent(inout) :: cell
integer :: istat
if (cell%natoms .lt. 1) call io_err("cell_allocate: Cell contains no atoms")
! only allocated if not already
if (.not. allocated(cell%atom_frac_pos)) then
allocate(cell%atom_frac_pos(3, cell%natoms), stat=istat)
if (istat .ne. 0) call io_err("cell_allocate: Could not allocate cell%atom_frac_pos")
else
call io_err("cell_allocate: cell%atom_frac_pos already allocated")
end if
if (.not. allocated(cell%atom_cart_pos)) then
allocate(cell%atom_cart_pos(3, cell%natoms), stat=istat)
if (istat .ne. 0) call io_err("cell_allocate: Could not allocate cell%atom_cart_pos")
else
call io_err("cell_allocate: cell%atom_cart_pos already allocated")
end if
if (.not. allocated(cell%atom_species)) then
allocate(cell%atom_species(cell%natoms), stat=istat)
if (istat .ne. 0) call io_err("cell_allocate: Could not allocate cell%atom_species")
else
call io_err("cell_allocate: cell%atom_species already allocated")
end if
if (.not. allocated(cell%atom_mass)) then
allocate(cell%atom_mass(cell%natoms), stat=istat)
if (istat .ne. 0) call io_err("cell_allocate: Could not allocate cell%atom_mass")
else
call io_err("cell_allocate: cell%atom_mass already allocated")
end if
end subroutine cell_allocate
!****s* cell/cell_deallocate ==================================================!
! NAME !
! cell_deallocate (PUBLIC) !
!------------------------------------------------------------------------------!
! ARGUMENTS !
! type(cell_type), intent(inout) :: cell !
!------------------------------------------------------------------------------!
! DESCRIPTION !
! Deallocates all of the allocatable arrays within the cell. !
! (Not the lattice_vectors and recip_lattice arrays, which are statically !
! allocated.) !
!------------------------------------------------------------------------------!
! NOTES !
! None of the arrays must already be unallocated. !
!==============================================================================!
subroutine cell_deallocate(cell)
use io, only: io_err
implicit none
type(cell_type), intent(inout) :: cell
integer :: istat
if (allocated(cell%atom_frac_pos)) then
deallocate(cell%atom_frac_pos, stat=istat)
if (istat .ne. 0) call io_err("cell_deallocate: Could not deallocate cell%atom_frac_pos")
end if
if (allocated(cell%atom_cart_pos)) then
deallocate(cell%atom_cart_pos, stat=istat)
if (istat .ne. 0) call io_err("cell_deallocate: Could not deallocate cell%atom_cart_pos")
end if
if (allocated(cell%atom_species)) then
deallocate(cell%atom_species, stat=istat)
if (istat .ne. 0) call io_err("cell_deallocate: Could not deallocate cell%atom_species")
end if
if (allocated(cell%atom_mass)) then
deallocate(cell%atom_mass, stat=istat)
if (istat .ne. 0) call io_err("cell_deallocate: Could not deallocate cell%atom_mass")
end if
cell%natoms = 0 ! might as well
end subroutine cell_deallocate
subroutine cell_reallocate(cell)
use io, only: io_err
implicit none
type(cell_type), intent(inout) :: cell
logical :: do_alloc
integer :: istat
do_alloc = .true.
if (allocated(cell%atom_frac_pos)) then
do_alloc = .false.
if (size(cell%atom_frac_pos, 2) .ne. cell%natoms) then
deallocate(cell%atom_frac_pos, stat=istat)
if (istat .ne. 0) call io_err("cell_reallocate: Could not deallocate cell%atom_frac_pos")
do_alloc = .true.
end if
end if
if (do_alloc) then
allocate(cell%atom_frac_pos(3, cell%natoms), stat=istat)
if (istat .ne. 0) call io_err("cell_reallocate: Could not allocate cell%atom_frac_pos")
end if
do_alloc = .true.
if (allocated(cell%atom_cart_pos)) then
do_alloc = .false.
if (size(cell%atom_cart_pos, 2) .ne. cell%natoms) then
deallocate(cell%atom_cart_pos, stat=istat)
if (istat .ne. 0) call io_err("cell_reallocate: Could not deallocate cell%atom_cart_pos")
do_alloc = .true.
end if
end if
if (do_alloc) then
allocate(cell%atom_cart_pos(3, cell%natoms), stat=istat)
if (istat .ne. 0) call io_err("cell_reallocate: Could not allocate cell%atom_cart_pos")
end if
do_alloc = .true.
if (allocated(cell%atom_species)) then
do_alloc = .false.
if (size(cell%atom_species, 1) .ne. cell%natoms) then
deallocate(cell%atom_species, stat=istat)
if (istat .ne. 0) call io_err("cell_reallocate: Could not deallocate cell%atom_species")
do_alloc = .true.
end if
end if
if (do_alloc) then
allocate(cell%atom_species(cell%natoms), stat=istat)
if (istat .ne. 0) call io_err("cell_reallocate: Could not allocate cell%atom_species")
end if
do_alloc = .true.
if (allocated(cell%atom_mass)) then
do_alloc = .false.
if (size(cell%atom_mass, 1) .ne. cell%natoms) then
deallocate(cell%atom_mass, stat=istat)
if (istat .ne. 0) call io_err("cell_reallocate: Could not deallocate cell%atom_mass")
do_alloc = .true.
end if
end if
if (do_alloc) then
allocate(cell%atom_mass(cell%natoms), stat=istat)
if (istat .ne. 0) call io_err("cell_reallocate: Could not allocate cell%atom_mass")
end if
end subroutine cell_reallocate
!****s* cell/cell_copy ========================================================!
! NAME !
! cell_copy (PUBLIC) !
!------------------------------------------------------------------------------!
! ARGUMENTS !
! type(cell_type), intent(in) :: incell !
! type(cell_type), intent(inout) :: outcell !
!------------------------------------------------------------------------------!
! DESCRIPTION !
! Copies the contents of incell to outcell on a component by component basis !
! (a deep copy). !
!==============================================================================!
subroutine cell_copy(incell, outcell)
use io, only: io_err
implicit none
type(cell_type), intent(in) :: incell
type(cell_type), intent(inout) :: outcell
outcell%natoms = incell%natoms
outcell%volume = incell%volume
outcell%lattice_vectors = incell%lattice_vectors
outcell%recip_lattice = incell%recip_lattice
outcell%min_img_cutoff = incell%min_img_cutoff
! can only allocate new cell (and copy these arrays if natoms > 0)
if (outcell%natoms .gt. 0) then
call cell_reallocate(outcell)
outcell%atom_frac_pos = incell%atom_frac_pos
outcell%atom_cart_pos = incell%atom_cart_pos
outcell%atom_mass = incell%atom_mass
outcell%atom_species = incell%atom_species
end if
end subroutine cell_copy
!****s* cell/cell_read_input ==================================================!
! NAME !
! cell_read_input (PRIVATE) !
!------------------------------------------------------------------------------!
! ARGUMENTS !
! type(cell_type), intent(inout) :: cell !
!------------------------------------------------------------------------------!
! DESCRIPTION !
! Sets the lattice vectors, allocates the cell and stores the atomic species !
! and atomic positions (in fractional space) based on the contents of the !
! input hash table. !
!==============================================================================!
subroutine cell_read_input(cell)
use io, only: max_line_len, io_input_get_data, io_str_get_num_tokens, io_str_get_token, io_str_to_real, &
& io_input_get_single_value, io_err
use constants, only: element_symbol_to_z
implicit none
type(cell_type), intent(inout) :: cell
character(len=max_line_len) :: keyword
character(len=max_line_len), allocatable, dimension(:) :: values
logical :: found
integer :: iline, itoken, species
integer :: nlines, ntokens
integer :: istat
! begin lattice vectors --------------------------------------------------------------------------
keyword = 'lattice_vectors'
call io_input_get_data(trim(keyword), values, found)
if (.not. found) call io_err("cell_read_input: Did not find "//trim(keyword)//" in input file")
nlines = size(values,1)
if (nlines .ne. 3) call io_err("cell_read_input: Expected "//trim(keyword)//" block to be 3 lines")
do iline = 1, nlines
ntokens = io_str_get_num_tokens(values(iline))
if (ntokens .ne. 3) call io_err("cell_read_input: Expected 3 values per line for "//trim(keyword)//" block")
do itoken = 1, ntokens
cell%lattice_vectors(itoken, iline) = io_str_to_real(io_str_get_token(values(iline), itoken))
end do
end do
! end lattice vectors ----------------------------------------------------------------------------
! begin atomic postions --------------------------------------------------------------------------
keyword = 'atomic_positions'
call io_input_get_data(trim(keyword), values, found)
if (.not. found) call io_err("cell_read_input: No "//trim(keyword)//" in input file")
nlines = size(values,1)
cell%natoms = nlines
call cell_allocate(cell)
do iline = 1, nlines
ntokens = io_str_get_num_tokens(values(iline))
if (ntokens .ne. 4) call io_err("cell_read_input: Expected 4 values per line for "//trim(keyword)//" block")
! first token is atom species
species = element_symbol_to_z(trim(io_str_get_token(values(iline), 1)))
if (species .lt. 1) call io_err("cell_read_input: Unexpected atom species")
cell%atom_species(iline) = species ! iatom = iline
do itoken = 2, ntokens !2,4
! itoken-1 is coordinate index, iatom = iline
cell%atom_frac_pos(itoken-1, iline) = io_str_to_real(io_str_get_token(values(iline), itoken))
end do
end do
! end atomic positions ---------------------------------------------------------------------------
! begin cell_perturb ----------------------------------------------------------------------------
call io_input_get_single_value('cell_perturb_lattice', lattice_perturb, -1.0_dp)
call io_input_get_single_value('cell_perturb_atoms', atom_perturb, -1.0_dp)
! end cell_perturb ------------------------------------------------------------------------------
! finalization - should always be allocated to get this far, but no harm in checking...
if (allocated(values)) then
deallocate(values, stat=istat)
if (istat .ne. 0) call io_err("cell_read_input: Could not deallocate values array")
end if
end subroutine cell_read_input
!****s* cell/cell_shift_to_unit_cell ==========================================!
! NAME !
! cell_shift_to_unit_cell (PUBLIC) !
!------------------------------------------------------------------------------!
! ARGUMENTS !
! type(cell_type), intent(inout) :: cell !
!------------------------------------------------------------------------------!
! DESCRIPTION !
! Enforces periodic boundary conditions such that anything leaving one side !
! of the cell will return through the other side. !
!------------------------------------------------------------------------------!
! NOTES !
! This operation is carried out in fractional space, therefore the !
! fractional positions must be correct. The result is that the fractional !
! positions are always shifted into the unit cell. !
!==============================================================================!
subroutine cell_shift_to_unit_cell(cell)
implicit none
type(cell_type), intent(inout) :: cell
! enforces periodic boundary conditions
! anything leaving from one side of the cell will return through the other side
cell%atom_frac_pos(:,:) = cell%atom_frac_pos(:,:) - real(floor(cell%atom_frac_pos(:,:)),dp)
end subroutine cell_shift_to_unit_cell
!****s* cell/cell_frac_to_cart ================================================!
! NAME !
! cell_frac_to_cart (PUBLIC) !
!------------------------------------------------------------------------------!
! ARGUMENTS !
! type(cell_type), intent(inout) :: cell !
!------------------------------------------------------------------------------!
! DESCRIPTION !
! Updates the cartesian atomic positions from the fractional positions. !
!==============================================================================!
subroutine cell_frac_to_cart(cell)
implicit none
type(cell_type), intent(inout) :: cell
cell%atom_cart_pos=matmul(cell%lattice_vectors, cell%atom_frac_pos)
end subroutine cell_frac_to_cart
!****s* cell/cell_cart_to_frac=================================================!
! NAME !
! cell_cart_to_frac (PUBLIC) !
!------------------------------------------------------------------------------!
! ARGUMENTS !
! type(cell_type), intent(inout) :: cell !
!------------------------------------------------------------------------------!
! DESCRIPTION !
! Updates the fractional atomic positions from the cartesian positions. !
!==============================================================================!
subroutine cell_cart_to_frac(cell)
use constants, only: two_pi
implicit none
type(cell_type), intent(inout) :: cell
cell%atom_frac_pos=matmul(cell%recip_lattice/two_pi, cell%atom_cart_pos) ! need divide by two_pi because of definition..
end subroutine cell_cart_to_frac
!****s* cell/cell_get_recip ===================================================!
! NAME !
! cell_get_recip (PUBLIC) !
!------------------------------------------------------------------------------!
! ARGUMENTS !
! type(cell_type), intent(inout) :: cell !
!------------------------------------------------------------------------------!
! DESCRIPTION !
! Calculates the reciprocal lattice vectors from the lattice vectors. !
!==============================================================================!
subroutine cell_get_recip(cell)
use constants, only: two_pi
use algor, only: algor_cross_product
implicit none
type(cell_type), intent(inout) :: cell
real(kind=dp) :: two_pi_over_vol
! calc recip_lattice:
two_pi_over_vol = two_pi/cell%volume ! might not want the two_pi here...
cell%recip_lattice(:,1) = two_pi_over_vol*algor_cross_product(cell%lattice_vectors(:,2), cell%lattice_vectors(:,3))
cell%recip_lattice(:,2) = two_pi_over_vol*algor_cross_product(cell%lattice_vectors(:,3), cell%lattice_vectors(:,1))
cell%recip_lattice(:,3) = two_pi_over_vol*algor_cross_product(cell%lattice_vectors(:,1), cell%lattice_vectors(:,2))
! transpose matrix
cell%recip_lattice(:,:) = transpose(cell%recip_lattice(:,:))
end subroutine cell_get_recip
!****s* cell/cell_min_img_init ================================================!
! NAME !
! cell_min_img_init (PUBLIC) !
!------------------------------------------------------------------------------!
! ARGUMENTS !
! type(cell_type), intent(inout) :: cell !
!------------------------------------------------------------------------------!
! DESCRIPTION !
! Initializes cell_min_img - on first pass we need to calculate all of the !
! "c_vectors" (these are used for all possible corners of adjacent cells) !
! - this is indepedent of any one particular cell. !
! !
! Next we calculate the perpendicular cell widths and store the square of !
! half of minimum width, which is used as a cutoff for cell_min_img !
! - anything within this distance is guaranteed to be the minimum image. !
!==============================================================================!
subroutine cell_min_img_init(cell)
use algor, only: algor_cross_product
implicit none
type(cell_type), intent(inout) :: cell
integer, parameter, dimension(3) :: ck_values = (/ -1, 0, 1 /)
real(kind=dp), dimension(3) :: axb, bxc, cxa
real(kind=dp) :: w_a, w_b, w_c
integer :: i, j, k, l
! this part is universal for all cells - only do this once, ever:
if (.not. min_img_initialized) then
! fill up the matrix with all of the possible c_vectors
! there will be 27 possible c vectors
l = 1
do i = 1,3
do j = 1,3
do k = 1,3
min_img_c_vectors(:,l) = (/ ck_values(i), ck_values(j), ck_values(k) /)
l = l+1
end do
end do
end do
min_img_initialized = .true.
end if
! get perpendicular cell widths
bxc = algor_cross_product(cell%lattice_vectors(:,2), cell%lattice_vectors(:,3))
cxa = algor_cross_product(cell%lattice_vectors(:,3), cell%lattice_vectors(:,1))
axb = algor_cross_product(cell%lattice_vectors(:,1), cell%lattice_vectors(:,2))
w_a = abs(dot_product(cell%lattice_vectors(:,1), bxc))/sqrt(dot_product(bxc,bxc))
w_b = abs(dot_product(cell%lattice_vectors(:,2), cxa))/sqrt(dot_product(cxa,cxa))
w_c = abs(dot_product(cell%lattice_vectors(:,3), axb))/sqrt(dot_product(axb,axb))
cell%min_img_cutoff = 0.5_dp*min(w_a, w_b, w_c)
cell%min_img_cutoff = cell%min_img_cutoff*cell%min_img_cutoff ! square to save a sqrt in cell_min_img
end subroutine cell_min_img_init
!****s* cell/cell_min_img =====================================================!
! NAME !
! cell_min_img (PUBLIC) !
!------------------------------------------------------------------------------!
! ARGUMENTS !
! type(cell_type), intent(in) :: cell !
! integer, intent(in) :: iatom, jatom !
! real(kind=dp), dimension(3), intent(out) :: rij_out !
! !
! real(kind=dp), intent(out), optional :: distance !
! character(len=4), intent(in), optional :: space !
!------------------------------------------------------------------------------!
! DESCRIPTION !
! Returns the vector between the closest image of atoms under periodic !
! boundary conditions. !
! !
! Optionally we can also return the length of this vector (which must be in !
! cartesian space) - and/or change the space that the vector between the !
! atoms is defined in (default is cartesian). !
!==============================================================================!
subroutine cell_min_img(cell, iatom, jatom, rij_out, distance, space)
use io, only: io_err
implicit none
type(cell_type), intent(in) :: cell
integer, intent(in) :: iatom, jatom
real(kind=dp), dimension(3), intent(out) :: rij_out
real(kind=dp), intent(out), optional :: distance
character(len=4), intent(in), optional :: space ! either 'frac' or 'cart', space to output rij_out
character(len=4) :: int_space
real(kind=dp) :: int_dist
real(kind=dp), dimension(3) :: ri, rj
if (iatom .eq. jatom) then
rij_out = 0.0_dp
if (present(distance)) distance = 0.0_dp
return
end if
int_space = 'cart'
if (present(space)) then
int_space = space
end if
select case (int_space)
case ('cart')
ri = cell%atom_cart_pos(:,iatom)
rj = cell%atom_cart_pos(:,jatom)
case ('frac')
ri = cell%atom_frac_pos(:,iatom)
rj = cell%atom_frac_pos(:,jatom)
case default
call io_err("cell_min_img: Unrecognised space")
end select
call cell_min_img_vec(cell, ri, rj, rij_out, int_dist, int_space)
if (present(distance)) distance = int_dist
end subroutine cell_min_img
subroutine cell_min_img_vec(cell, ri, rj, rij_out, distance, space)
use io, only: io_err
use constants, only: two_pi
! 'space' variable determines input space of ri and rj, as well as output space of rij_out
! these are in cartesian space by default (ie: unless space == 'frac')
! distance is optional, real space distance
implicit none
type(cell_type), intent(in) :: cell
real(kind=dp), dimension(3), intent(in) :: ri, rj
real(kind=dp), dimension(3), intent(out) :: rij_out
real(kind=dp), intent(out), optional :: distance
character(len=4), intent(in), optional :: space ! either 'frac' or 'cart'
character(len=4) :: int_space
real(kind=dp), dimension(3) :: int_ri, int_rj, rij, fij, rij_min
real(kind=dp) :: smallestdist, currentdist
integer :: k, l, cmin
! for speedup - just care about 8 vectors, not all 27
logical, dimension(27) :: is_c_important
integer, dimension(3) :: disallowed_c
int_space = 'cart'
if (present(space)) then
int_space = space
end if
! don't check whether ri == rj in vector routine
! set up internal position variables
select case (int_space)
case ('cart')
int_ri = ri
int_rj = rj
fij = ri - rj ! fij = rij
fij = matmul(cell%recip_lattice/two_pi, fij) ! convert rij to fractionals
case ('frac')
! convert ri, rj to cartesian
int_ri = matmul(cell%lattice_vectors, ri)
int_rj = matmul(cell%lattice_vectors, rj)
fij = ri - rj
case default
call io_err("cell_min_img_vec: Unrecognised space")
end select
! fij = fi - fj (frac coords)
! rij = ri - rj (cart coords)
rij = int_ri - int_rj
rij_min = rij
! calc distance**2 for within unit cell
currentdist = dot_product(rij,rij)
! if sqrt of this is less than smallest half width then we have minimum image already
! (cell%min_img_cutoff is this width**2)
if (currentdist .gt. cell%min_img_cutoff) then