Skip to content

Commit 259e5ed

Browse files
committed
exception handling and allocation improved
1 parent 21fb3d0 commit 259e5ed

10 files changed

Lines changed: 228 additions & 41 deletions

ML-FF/mlff_select.f90

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ program mlff_select
136136
logical::eval_stat(10) ! the progress for evaluation loops
137137
logical::no_adf ! if no angular distribution functions shall be calculated
138138
logical,allocatable::atom_used(:) ! boolean mask for blocking of treated atoms
139+
integer::alloc_stat ! if allocation of array was successful
140+
character(len=100)::alloc_err ! the error message for failed allocation
139141
! for time measurement
140142
character(8) :: date
141143
character(10) :: time
@@ -1036,9 +1038,20 @@ program mlff_select
10361038
allocate(el_nums_confs(nelems_glob,conf_num))
10371039
allocate(el_nums_tmp(nelems_glob))
10381040
! The geometries (cartesian coordinates)
1039-
allocate(xyz(3,natoms_max,conf_num))
1041+
allocate(xyz(3,natoms_max,conf_num),stat=alloc_stat,errmsg=alloc_err)
1042+
if (alloc_stat .ne. 0) then
1043+
write(*,*) "Allocation of xyz array failed:",trim(alloc_err)
1044+
write(*,*) "Please read in a smaller ML_AB or increase memory!"
1045+
stop
1046+
end if
10401047
! The geometries (direct coordinates
1041-
allocate(xyz_dir(3,natoms_max,conf_num))
1048+
allocate(xyz_dir(3,natoms_max,conf_num),stat=alloc_stat,errmsg=alloc_err)
1049+
if (alloc_stat .ne. 0) then
1050+
write(*,*) "Allocation of xyz_dir array failed:",trim(alloc_err)
1051+
write(*,*) "Please read in a smaller ML_AB or increase memory!"
1052+
stop
1053+
end if
1054+
10421055
! The unit cell shapes
10431056
allocate(cells(3,3,conf_num))
10441057
! The CTIFOR values
@@ -1048,7 +1061,13 @@ program mlff_select
10481061
! The reference energies
10491062
allocate(energies(conf_num))
10501063
! The reference gradients
1051-
allocate(grads(3,natoms_max,conf_num))
1064+
allocate(grads(3,natoms_max,conf_num),stat=alloc_stat,errmsg=alloc_err)
1065+
if (alloc_stat .ne. 0) then
1066+
write(*,*) "Allocation of grads array failed:",trim(alloc_err)
1067+
write(*,*) "Please read in a smaller ML_AB or increase memory!"
1068+
stop
1069+
end if
1070+
10521071
! The reference stress tensors
10531072
allocate(stress(6,conf_num))
10541073

@@ -1057,7 +1076,13 @@ program mlff_select
10571076
!
10581077
! Again open the ML_AB file
10591078
!
1060-
open(unit=56,file=mlab_list(l),status="old")
1079+
open(unit=56,file=mlab_list(l),status="old",iostat=readstat)
1080+
if (readstat .ne. 0) then
1081+
write(*,*)
1082+
write(*,*) "The file ",trim(mlab_list(l))," is not there!"
1083+
write(*,*)
1084+
stop
1085+
end if
10611086

10621087
write(*,*) "Read body of the file ",trim(mlab_list(l))," ..."
10631088
write(34,*) "Read body of the file ",trim(mlab_list(l))," ..."
@@ -1302,17 +1327,43 @@ program mlff_select
13021327
allocate(gradnorm_all(natoms_sum))
13031328
! The number of atoms in the surrounding (sorted by core charges)
13041329
allocate(num_around(nelems,natoms_sum))
1330+
allocate(num_around(nelems,natoms_sum),stat=alloc_stat,errmsg=alloc_err)
1331+
if (alloc_stat .ne. 0) then
1332+
write(*,*) "Allocation of num_around array failed:",trim(alloc_err)
1333+
write(*,*) "Please read in a smaller ML_AB or increase memory!"
1334+
stop
1335+
end if
13051336
! The coordinates of atoms in the surrounding (for xyz printout)
1306-
allocate(environ(3,max_environ,natoms_sum))
1337+
allocate(environ(3,max_environ,natoms_sum),stat=alloc_stat,errmsg=alloc_err)
1338+
if (alloc_stat .ne. 0) then
1339+
write(*,*) "Allocation of environ array failed:",trim(alloc_err)
1340+
write(*,*) "Please read in a smaller ML_AB or increase memory!"
1341+
stop
1342+
end if
13071343
! The core charges/elements of atoms in surrounding
1308-
allocate(ind_env(max_environ,natoms_sum))
1344+
allocate(ind_env(max_environ,natoms_sum),stat=alloc_stat,errmsg=alloc_err)
1345+
if (alloc_stat .ne. 0) then
1346+
write(*,*) "Allocation of ind_env array failed:",trim(alloc_err)
1347+
write(*,*) "Please read in a smaller ML_AB or increase memory!"
1348+
stop
1349+
end if
13091350
! The list of distances to atoms within surrounding (local)
13101351
allocate(dist_list(max_environ))
13111352
! The radial distribution functions around all atoms
1312-
allocate(rdf_all(ngrid,natoms_sum))
1353+
allocate(rdf_all(ngrid,natoms_sum),stat=alloc_stat,errmsg=alloc_err)
1354+
if (alloc_stat .ne. 0) then
1355+
write(*,*) "Allocation of rdf_all array failed:",trim(alloc_err)
1356+
write(*,*) "Please read in a smaller ML_AB or increase memory!"
1357+
stop
1358+
end if
13131359
! The angular distribution functions around all atoms
13141360
if (.not. no_adf) then
1315-
allocate(adf_all(ngrid,natoms_sum))
1361+
allocate(adf_all(ngrid,natoms_sum),stat=alloc_stat,errmsg=alloc_err)
1362+
if (alloc_stat .ne. 0) then
1363+
write(*,*) "Allocation of adf_all array failed:",trim(alloc_err)
1364+
write(*,*) "Please read in a smaller ML_AB or increase memory!"
1365+
stop
1366+
end if
13161367
end if
13171368
! The local list of angles in the environment
13181369
allocate(angles(max_environ*max_environ))
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
!
2-
! mlp_quality: Evaluate the quality of a MLP with respect to the
2+
! mlip_quality: Evaluate the quality of a MLP with respect to the
33
! reproduction of a given training or verification set.
44
! Currently, Behler neural networks, message-passing atomic cluster
55
! expansion (MACE) and VASP ML-FF production runs can be evaluated.
@@ -82,7 +82,7 @@ program mlp_quality
8282
!
8383
write(*,*)
8484
write(*,*) " *** utils4VASP -- utility scripts and programs for VASP ***"
85-
write(*,*) "PROGRAM mlp_quality: Evaluation of a MLP with respect to the "
85+
write(*,*) "PROGRAM mlip_quality: Evaluation of a MLP with respect to the "
8686
write(*,*) " reproduction of a given training or verification set."
8787
write(*,*) "The analysis can be done for Behler neural network potentials "
8888
write(*,*) " optimized with the aenet program (ANN) or for message-passing "

ML-FF/vasp2trainset.f90

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ program vasp2trainset
4646
character(len=2)::el_syms(10)
4747
character(len=150)::cdum
4848
real(kind=8)::factor,scale_dum
49-
integer::openstat
49+
integer::openstat,alloc_stat
50+
character(len=100)::alloc_err
5051
integer::traj_freq
5152
!
5253
! only print the overview of utils4VASP scripts/programs and stop
@@ -555,11 +556,27 @@ program vasp2trainset
555556
read(27,*)
556557
end do
557558
if (allocated(xyz)) deallocate(xyz)
558-
allocate(xyz(3,natoms))
559+
allocate(xyz(3,natoms),stat=alloc_stat,errmsg=alloc_err)
560+
if (alloc_stat .ne. 0) then
561+
write(*,*) "Allocation of xyz array failed:",trim(alloc_err)
562+
write(*,*) "Please read in a smaller ML_AB or increase memory!"
563+
stop
564+
end if
559565
if (allocated(grad)) deallocate(grad)
560-
allocate(grad(3,natoms))
566+
allocate(grad(3,natoms),stat=alloc_stat,errmsg=alloc_err)
567+
if (alloc_stat .ne. 0) then
568+
write(*,*) "Allocation of grad array failed:",trim(alloc_err)
569+
write(*,*) "Please read in a smaller ML_AB or increase memory!"
570+
stop
571+
end if
561572
if (allocated(at_names)) deallocate(at_names)
562-
allocate(at_names(natoms))
573+
allocate(at_names(natoms),stat=alloc_stat,errmsg=alloc_err)
574+
if (alloc_stat .ne. 0) then
575+
write(*,*) "Allocation of at_names array failed:",trim(alloc_err)
576+
write(*,*) "Please read in a smaller ML_AB or increase memory!"
577+
stop
578+
end if
579+
563580
!
564581
! Determine element symbols for all atoms
565582
!
@@ -740,7 +757,14 @@ program vasp2trainset
740757
write(*,*) " Number of frames: ",nframes
741758
write(*,*) " ... completed!"
742759
write(*,*) " Read XDATCAR content ..."
743-
allocate(xdat_content(xdat_lines))
760+
allocate(xdat_content(xdat_lines),stat=alloc_stat,errmsg=alloc_err)
761+
if (alloc_stat .ne. 0) then
762+
write(*,*) "Allocation of xdat_content array failed:",trim(alloc_err)
763+
write(*,*) "Please read in a smaller XDATCAR or increase memory!"
764+
stop
765+
end if
766+
767+
744768
open(unit=56,file="XDATCAR",status="old")
745769
do i=1,xdat_lines
746770
read(56,'(a)') xdat_content(i)

evaluation/analyze_md.f90

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ program analyze_md
9595
real(kind=8),allocatable::xlens_p(:),ylens_p(:),zlens_p(:) ! ... for call of subroutines
9696
character(len=2)::el_eval_list(20) ! the elements to be set to COM (density mode)
9797
integer::el_eval_num ! number of elements for the COM (density mode)
98-
integer::readstat,openstat
98+
integer::readstat,openstat,status,alloc_stat
99+
character(len=100)::alloc_err
99100
integer::counter,endl
100101
real(kind=8)::scale_dum
101102
logical::npt_format
@@ -226,7 +227,12 @@ program analyze_md
226227
write(*,*)
227228
stop
228229
end if
229-
call system("wc -l XDATCAR > xdat_length")
230+
status = system("wc -l XDATCAR > xdat_length")
231+
if (status .ne. 0) then
232+
write(*,*) "The number of atoms/frames could not be determined! XDATCAR missing?"
233+
stop
234+
end if
235+
230236
open(unit=45,file="xdat_length",status="old")
231237
read(45,*) xdat_lines
232238
close(45)
@@ -349,7 +355,13 @@ program analyze_md
349355
open(unit=14,file="XDATCAR",status="old")
350356
end if
351357

352-
allocate(xyz(3,natoms,nframes))
358+
allocate(xyz(3,natoms,nframes),stat=alloc_stat,errmsg=alloc_err)
359+
if (alloc_stat .ne. 0) then
360+
write(*,*) "Allocation of xyz array failed:",trim(alloc_err)
361+
write(*,*) "Please read in a smaller trajectory or increase memory!"
362+
stop
363+
end if
364+
353365
!
354366
! Read in the coordinates of the frames and correct for image flags
355367
!

evaluation/manage_neb.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,10 @@ def COM(xyz,natoms,names,elements_dict,move):
224224
sys.exit(1)
225225

226226
# Open POSCAR file of first frame
227+
if not os.path.isfile(poscar1):
228+
print("\n No "+poscar1+" file could be found! \n")
229+
sys.exit(1)
230+
227231
poscar_in = open(poscar1,"r")
228232

229233
cartesian=False
@@ -302,6 +306,10 @@ def COM(xyz,natoms,names,elements_dict,move):
302306
xyz1[i][j]=float(xyz_read[j])
303307

304308
# Open POSCAR file of last frame
309+
if not os.path.isfile(poscar2):
310+
print("\n No "+poscar2+" file could be found! \n")
311+
sys.exit(1)
312+
305313
poscar_in = open(poscar2,"r")
306314

307315
cartesian=False

evaluation/modify_xdatcar.f90

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ program modify_xdatcar
2323
real(kind=8)::shift_vec(3),act_val,xyz_print(3)
2424
integer::multiply_vec(3),pick_ind,pos_new,multiply_prod
2525
integer::frame_first,frame_last,line_num
26-
integer::read_freq
26+
integer::read_freq,status
2727
logical::npt,print_npt
2828
logical::remove_mode
2929
character(len=40)::remove_com
@@ -37,7 +37,8 @@ program modify_xdatcar
3737
character(len=120)::a120,cdum,arg,adum
3838
character(len=220)::a220
3939
character(len=50)::atest
40-
40+
integer::alloc_stat
41+
character(len=100)::alloc_err
4142
!
4243
! only print the overview of utils4VASP scripts/programs and stop
4344
!
@@ -301,7 +302,11 @@ program modify_xdatcar
301302
!
302303
write(*,*)
303304
write(*,*) "Determine number of atoms and frames in XDATCAR..."
304-
call system("wc -l XDATCAR > xdat_length")
305+
status = system("wc -l XDATCAR > xdat_length")
306+
if (status .ne. 0) then
307+
write(*,*) "The number of atoms/frames could not be determined! XDATCAR missing?"
308+
stop
309+
end if
305310
open(unit=45,file="xdat_length",status="old")
306311
read(45,*) xdat_lines
307312
close(45)
@@ -396,7 +401,12 @@ program modify_xdatcar
396401
!
397402
! Allocate global coordinate arrays
398403
!
399-
allocate(xyz(3,natoms,nframes))
404+
allocate(xyz(3,natoms,nframes),stat=alloc_stat,errmsg=alloc_err)
405+
if (alloc_stat .ne. 0) then
406+
write(*,*) "Allocation of xyz array failed:",trim(alloc_err)
407+
write(*,*) "Please read in a smaller trajectory or increase memory!"
408+
stop
409+
end if
400410
if (npt) then
401411
allocate(a_vecs(3,nframes))
402412
allocate(b_vecs(3,nframes))

setup/analyze_poscar.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,14 @@
134134
# Read in the POSCAR file
135135
poscar_name="POSCAR"
136136

137+
# Check first if it is there!
138+
139+
if os.path.isfile(poscar_name):
140+
print("\n The POSCAR file could be located.\n")
141+
else:
142+
print("\n No POSCAR file could be found! \n")
143+
sys.exit(1)
144+
137145
poscar_in = open(poscar_name,"r")
138146

139147
# array for selective dynamics specifiers
@@ -380,7 +388,13 @@ def vector_angle(v1, v2):
380388
if os.path.isfile("POTCAR"):
381389
os.system("rm POTCAR")
382390
for el in elements:
383-
os.system("cat "+pot_path+"potcar/PAW_PBE.52/"+el+"/POTCAR >> POTCAR")
391+
path_current=pot_path+"potcar/PAW_PBE.52/"+el+"/POTCAR"
392+
path_current = os.path.expanduser(path_current)
393+
if os.path.isfile(path_current):
394+
os.system("cat "+path_current+" >> POTCAR")
395+
else:
396+
print("\n No POTCAR template for "+el+" could be found! \n")
397+
sys.exit(1)
384398

385399
print(" The POTCAR file was written.")
386400
#

setup/build_adsorb.py

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -593,12 +593,24 @@ def ReadMolecule(self):
593593
#
594594
if self.type == "large":
595595
if self.spec == "local1":
596+
if not os.path.isfile("POSCAR_ads1"):
597+
print("\n No POSCAR_ads1 file could be found! \n")
598+
sys.exit(1)
596599
mol_in = open("POSCAR_ads1","r")
597600
elif self.spec == "local2":
601+
if not os.path.isfile("POSCAR_ads2"):
602+
print("\n No POSCAR_ads2 file could be found! \n")
603+
sys.exit(1)
598604
mol_in = open("POSCAR_ads2","r")
599605
elif self.spec == "local3":
606+
if not os.path.isfile("POSCAR_ads3"):
607+
print("\n No POSCAR_ads3 file could be found! \n")
608+
sys.exit(1)
600609
mol_in = open("POSCAR_ads3","r")
601610
elif self.spec == "local4":
611+
if not os.path.isfile("POSCAR_ads3"):
612+
print("\n No POSCAR_ads3 file could be found! \n")
613+
sys.exit(1)
602614
mol_in = open("POSCAR_ads4","r")
603615
else:
604616
mol_in = io.StringIO(adsorbate_dict[self.spec])
@@ -800,6 +812,12 @@ def Rot_Molecule(self,matrix):
800812
# First, read in the POSCAR file for the metal surface
801813
surface_name="POSCAR_surf"
802814

815+
if os.path.isfile(surface_name):
816+
print("\n The POSCAR_surf file could be located.\n")
817+
else:
818+
print("\n No POSCAR_surf file could be found! \n")
819+
sys.exit(1)
820+
803821
surface_in = open(surface_name,"r")
804822

805823
with surface_in as infile:
@@ -915,31 +933,24 @@ def Rot_Molecule(self,matrix):
915933
print(" ")
916934

917935
nspecs=0
918-
#while True:
919-
# try:
920-
# nspecs = int(input(" Number of ionic liquid molecules? "))
921-
# if nspecs <= 0 or nspecs > 8:
922-
# raise ValueError
923-
# break
924-
# except NameError:
925-
# print(" ERROR! No valid number given! Please try again!")
926-
# except ValueError:
927-
# print(" ERROR! Only numbers between 1 and 8 are allowed!")
928936

929937
# Read in the species and positions/rotations from file!
930938

931-
# Second, read in the data file with the bromine positions
932939
list_name="adsorb_list.dat"
933940

941+
942+
if not os.path.isfile(list_name):
943+
print("\n No adsorb_list.dat file could be found! \n")
944+
sys.exit(1)
945+
946+
934947
list_in = open(list_name,"r")
935948

949+
936950
with list_in as infile:
937951
IL_lines = list_in.readlines()
938952

939953
nspecs = len(IL_lines)
940-
#if len(IL_lines) != nspecs:
941-
# print("Error! The file IL_list.dat must contain as many lines as the")
942-
# print(" number of desired IL molecules.")
943954

944955
#
945956
# Now read in all surface molecules as class instances!

0 commit comments

Comments
 (0)