Skip to content

Commit b9f76e6

Browse files
committed
Improve file reading and writing
1 parent 5121d03 commit b9f76e6

1 file changed

Lines changed: 94 additions & 46 deletions

File tree

src/fortran/lib/mod_distribs_container.f90

Lines changed: 94 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -754,11 +754,15 @@ subroutine write_gdfs(this, file)
754754
return
755755
end if
756756
open(newunit=unit, file=file)
757+
write(unit, '("# history_len ",I0)') this%history_len
758+
write(fmt, '("(""# history_deltas""",I0,"(1X,ES0.8))")') &
759+
size(this%history_deltas)
760+
write(unit, fmt) this%history_deltas
757761
write(unit, '("# nbins",3(1X,I0))') this%nbins
758-
write(unit, '("# width",3(1X,ES0.4))') this%width
759-
write(unit, '("# sigma",3(1X,ES0.4))') this%sigma
760-
write(unit, '("# cutoff_min",3(1X,ES0.4))') this%cutoff_min
761-
write(unit, '("# cutoff_max",3(1X,ES0.4))') this%cutoff_max
762+
write(unit, '("# width",3(1X,ES0.8))') this%width
763+
write(unit, '("# sigma",3(1X,ES0.8))') this%sigma
764+
write(unit, '("# cutoff_min",3(1X,ES0.8))') this%cutoff_min
765+
write(unit, '("# cutoff_max",3(1X,ES0.8))') this%cutoff_max
762766
write(unit, '("# radius_distance_tol",4(1X,ES0.4))') &
763767
this%radius_distance_tol
764768
write(fmt, '("(""# "",A,",I0,"(1X,A))")') size(this%element_info)
@@ -836,14 +840,16 @@ subroutine read_gdfs(this, file)
836840
! Local variables
837841
integer :: unit
838842
!! File unit.
843+
integer :: iostat
844+
!! I/O status.
839845

840846
integer :: i
841847
!! Loop index.
842848
integer :: nspec
843849
!! Number of species.
844850
logical :: exist
845851
!! Boolean whether the file exists.
846-
character(256) :: buffer, buffer1, buffer2
852+
character(256) :: buffer, buffer1
847853
!! Buffer for reading lines.
848854

849855
! check if file exists
@@ -855,46 +861,90 @@ subroutine read_gdfs(this, file)
855861

856862
! read the file
857863
open(newunit=unit, file=file)
858-
read(unit, *) buffer1, buffer2, this%nbins
859-
read(unit, *) buffer1, buffer2, this%width
860-
read(unit, *) buffer1, buffer2, this%sigma
861-
read(unit, *) buffer1, buffer2, this%cutoff_min
862-
read(unit, *) buffer1, buffer2, this%cutoff_max
863-
read(unit, *) buffer1, buffer2, this%radius_distance_tol
864-
read(unit, '(A)') buffer
865-
nspec = icount(buffer(index(buffer,"elements")+8:))
866-
if(allocated(this%element_info)) deallocate(this%element_info)
867-
allocate(this%element_info(nspec))
868-
read(buffer, *) buffer1, buffer2, this%element_info(:)%name
869-
read(unit, *) buffer1, buffer2, this%element_info(:)%energy
870-
do i = 1, nspec
871-
call this%set_element_energy( &
872-
this%element_info(i)%name, &
873-
this%element_info(i)%energy &
874-
)
875-
call this%element_info(i)%set(this%element_info(i)%name)
864+
do
865+
read(unit, '(A)', iostat=iostat) buffer
866+
if(iostat.ne.0) exit
867+
buffer = trim(adjustl(buffer))
868+
if(trim(buffer) .eq. "") cycle
869+
if(buffer(1:1) .ne. "#") cycle
870+
! get the header
871+
buffer = trim(adjustl(buffer(2:)))
872+
if(trim(adjustl(buffer)) .eq. "2-body") exit
873+
if(index(buffer, "history_len") .ne. 0) then
874+
read(buffer, *) buffer1, this%history_len
875+
call this%set_history_len(this%history_len)
876+
else if(index(buffer, "history_deltas") .ne. 0) then
877+
read(buffer, *) buffer1, this%history_deltas
878+
else if(index(buffer, "nbins") .ne. 0) then
879+
read(buffer, *) buffer1, this%nbins
880+
else if(index(buffer, "width") .ne. 0) then
881+
read(buffer, *) buffer1, this%width
882+
else if(index(buffer, "sigma") .ne. 0) then
883+
read(buffer, *) buffer1, this%sigma
884+
else if(index(buffer, "cutoff_min") .ne. 0) then
885+
read(buffer, *) buffer1, this%cutoff_min
886+
else if(index(buffer, "cutoff_max") .ne. 0) then
887+
read(buffer, *) buffer1, this%cutoff_max
888+
else if(index(buffer, "radius_distance_tol") .ne. 0) then
889+
read(buffer, *) buffer1, this%radius_distance_tol
890+
else if(index(buffer, "elements") .ne. 0) then
891+
nspec = icount(buffer(index(buffer,"elements")+8:))
892+
if(allocated(this%element_info)) deallocate(this%element_info)
893+
allocate(this%element_info(nspec))
894+
read(buffer, *) buffer1, this%element_info(:)%name
895+
else if(index(buffer, "energies") .ne. 0) then
896+
read(buffer, *) buffer1, this%element_info(:)%energy
897+
do i = 1, nspec
898+
call this%set_element_energy( &
899+
this%element_info(i)%name, &
900+
this%element_info(i)%energy &
901+
)
902+
call this%element_info(i)%set(this%element_info(i)%name)
903+
end do
904+
call this%update_bond_info()
905+
allocate(this%best_energy_per_species(nspec))
906+
allocate(this%norm_3body(nspec))
907+
allocate(this%norm_4body(nspec))
908+
allocate(this%in_dataset_3body(nspec))
909+
allocate(this%in_dataset_4body(nspec))
910+
allocate(this%best_energy_pair(size(this%bond_info)))
911+
allocate(this%norm_2body(size(this%bond_info)))
912+
allocate(this%in_dataset_2body(size(this%bond_info)))
913+
else if(index(buffer, "best_energy_per_element") .ne. 0) then
914+
read(buffer, *) buffer1, this%best_energy_per_species
915+
else if(index(buffer, "3-body_norm") .ne. 0) then
916+
read(buffer, *) buffer1, this%norm_3body
917+
else if(index(buffer, "4-body_norm") .ne. 0) then
918+
read(buffer, *) buffer1, this%norm_4body
919+
else if(index(buffer, "in_dataset_3body") .ne. 0) then
920+
read(buffer, *) buffer1, this%in_dataset_3body
921+
else if(index(buffer, "in_dataset_4body") .ne. 0) then
922+
read(buffer, *) buffer1, this%in_dataset_4body
923+
else if(index(buffer, "element_pairs") .ne. 0) then
924+
read(buffer, *) buffer1, this%bond_info(:)%element(1)
925+
read(buffer, *) buffer1, this%bond_info(:)%element(2)
926+
else if(index(buffer, "radii") .ne. 0) then
927+
read(buffer, *) buffer1, this%bond_info(:)%radius_covalent
928+
else if(index(buffer, "best_energy_per_pair") .ne. 0) then
929+
read(buffer, *) buffer1, this%best_energy_pair
930+
else if(index(buffer, "3-body_norm") .ne. 0) then
931+
read(buffer, *) buffer1, this%norm_3body
932+
else if(index(buffer, "4-body_norm") .ne. 0) then
933+
read(buffer, *) buffer1, this%norm_4body
934+
else if(index(buffer, "in_dataset_3body") .ne. 0) then
935+
read(buffer, *) buffer1, this%in_dataset_3body
936+
else if(index(buffer, "in_dataset_4body") .ne. 0) then
937+
read(buffer, *) buffer1, this%in_dataset_4body
938+
else if(index(buffer, "2-body_norm") .ne. 0) then
939+
read(buffer, *) buffer1, this%norm_2body
940+
else if(index(buffer, "in_dataset_2body") .ne. 0) then
941+
read(buffer, *) buffer1, this%in_dataset_2body
942+
else
943+
write(0,*) "Unknown header: ", trim(buffer)
944+
cycle
945+
end if
876946
end do
877-
call this%update_bond_info()
878-
allocate(this%best_energy_per_species(nspec))
879-
allocate(this%norm_3body(nspec))
880-
allocate(this%norm_4body(nspec))
881-
allocate(this%in_dataset_3body(nspec))
882-
allocate(this%in_dataset_4body(nspec))
883-
read(unit, *) buffer1, buffer2, this%best_energy_per_species
884-
read(unit, *) buffer1, buffer2, this%norm_3body
885-
read(unit, *) buffer1, buffer2, this%norm_4body
886-
read(unit, *) buffer1, buffer2, this%in_dataset_3body
887-
read(unit, *) buffer1, buffer2, this%in_dataset_4body
888-
read(unit, *)
889-
allocate(this%best_energy_pair(size(this%bond_info)))
890-
allocate(this%norm_2body(size(this%bond_info)))
891-
allocate(this%in_dataset_2body(size(this%bond_info)))
892-
read(unit, *) buffer1, buffer2, this%bond_info(:)%radius_covalent
893-
read(unit, *) buffer1, buffer2, this%best_energy_pair
894-
read(unit, *) buffer1, buffer2, this%norm_2body
895-
read(unit, *) buffer1, buffer2, this%in_dataset_2body
896-
read(unit, *)
897-
read(unit, *)
947+
898948
read(unit, *)
899949
allocate(this%gdf%df_2body(this%nbins(1),size(this%bond_info)))
900950
do i = 1, this%nbins(1)
@@ -2803,8 +2853,6 @@ function is_converged(this, threshold) result(converged)
28032853
!! Convergence flag.
28042854

28052855
! Local variables
2806-
integer :: i, j
2807-
!! Loop index.
28082856
real(real32) :: threshold_
28092857
!! Threshold for convergence.
28102858

0 commit comments

Comments
 (0)