Skip to content

Commit 05d6ad0

Browse files
committed
Add more tests, minor cleanups
1 parent 55307b9 commit 05d6ad0

2 files changed

Lines changed: 213 additions & 14 deletions

File tree

src/mctc/io/read/pymatgen.F90

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,17 @@ subroutine read_pymatgen(self, unit, error)
4848

4949
#if WITH_JSON
5050
class(json_value), allocatable :: root
51-
type(json_object), pointer :: object, child, extras, child2
51+
type(json_object), pointer :: object, child
5252
type(json_array), pointer :: array, child_array
5353
type(json_error), allocatable :: parse_error
5454
type(json_keyval), pointer :: val
5555
type(json_context) :: ctx
5656

57-
integer :: stat, origin, charge, multiplicity, iat, ilt
58-
integer :: origin_symbols, origin_geometry
59-
character(len=:), allocatable :: symbol, message, module_name, class_name, comment
57+
integer :: stat, origin, multiplicity, iat, ilt
58+
character(len=:), allocatable :: symbol, module_name, class_name
6059
character(len=symbol_length), allocatable :: sym(:)
6160
logical :: periodic
62-
integer, allocatable :: bond(:, :), list(:)
61+
real(wp) :: charge
6362
real(wp), allocatable :: xyz(:, :), lattice(:, :), vec(:)
6463

6564
call json_load(root, unit, config=json_parser_config(context_detail=1), &
@@ -101,13 +100,14 @@ subroutine read_pymatgen(self, unit, error)
101100
end if
102101
periodic = class_name == "Structure"
103102

103+
call get_value(object, "charge", charge, default=0.0_wp, stat=stat, origin=origin)
104+
if (stat /= json_stat%success) then
105+
call fatal_error(error, ctx%report("Could not read charge", origin=origin, &
106+
& label="Expected integer value"))
107+
return
108+
end if
109+
104110
if (.not.periodic) then
105-
call get_value(object, "charge", charge, default=0, stat=stat, origin=origin)
106-
if (stat /= json_stat%success) then
107-
call fatal_error(error, ctx%report("Could not read charge", origin=origin, &
108-
& label="Expected integer value"))
109-
return
110-
end if
111111
call get_value(object, "spin_multiplicity", multiplicity, default=1, &
112112
& stat=stat, origin=origin)
113113
if (stat /= json_stat%success) then
@@ -213,8 +213,7 @@ subroutine read_pymatgen(self, unit, error)
213213
end do
214214
end if
215215

216-
call new(self, sym, xyz, charge=real(charge, wp), &
217-
& uhf=multiplicity-1, lattice=lattice)
216+
call new(self, sym, xyz, charge=charge, uhf=multiplicity-1, lattice=lattice)
218217

219218
#else
220219
call fatal_error(error, "JSON support not enabled")

test/test_read_pymatgen.f90

Lines changed: 201 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,13 @@ subroutine collect_read_pymatgen(testsuite)
5252
& new_unittest("incorrect-xyz", test_incorrect_xyz, should_fail=.true.), &
5353
& new_unittest("incorrect-xyz-value", test_incorrect_xyz_value, should_fail=.true.), &
5454
& new_unittest("incorrect-xyz-size", test_incorrect_xyz_size, should_fail=.true.), &
55+
& new_unittest("incorrect-root", test_incorrect_root, should_fail=.true.), &
5556
& new_unittest("incorrect-lattice", test_incorrect_lattice, should_fail=.true.), &
57+
& new_unittest("incorrect-lattice-table", test_incorrect_lattice_table, should_fail=.true.), &
5658
& new_unittest("incorrect-lattice-value", test_incorrect_lattice_value, should_fail=.true.), &
5759
& new_unittest("incorrect-lattice-size", test_incorrect_lattice_size, should_fail=.true.), &
58-
& new_unittest("incorrect-lattice-dim", test_incorrect_lattice_dim, should_fail=.true.) &
60+
& new_unittest("incorrect-lattice-dim", test_incorrect_lattice_dim, should_fail=.true.), &
61+
& new_unittest("incorrect-lattice-rank", test_incorrect_lattice_rank, should_fail=.true.) &
5962
& ]
6063

6164
end subroutine collect_read_pymatgen
@@ -801,6 +804,51 @@ subroutine test_incorrect_xyz_value(error)
801804

802805
end subroutine test_incorrect_xyz_value
803806

807+
subroutine test_incorrect_root(error)
808+
809+
!> Error handling
810+
type(error_type), allocatable, intent(out) :: error
811+
812+
character(len=*), parameter :: filename = ".test-invalid-pmg-mol13.json"
813+
type(structure_type) :: struc
814+
integer :: unit
815+
816+
open(file=filename, newunit=unit)
817+
write(unit, '(a)') &
818+
'[{', &
819+
' "@module": "pymatgen.core.structure",', &
820+
' "@class": "Molecule",', &
821+
' "charge": 0,', &
822+
' "spin_multiplicity": 1,', &
823+
' "sites": [', &
824+
' {', &
825+
' "name": "O", "label": "O",', &
826+
' "species": [{"element": "O", "occu": 1}],', &
827+
' "xyz": [1.1847029, 1.1150792, -0.0344641],', &
828+
' "properties": {}', &
829+
' },', &
830+
' {', &
831+
' "name": "H", "label": "H",', &
832+
' "species": [{"element": "H", "occu": 1}],', &
833+
' "xyz": [0.4939088, 0.9563767, 0.6340089],', &
834+
' "properties": {}', &
835+
' },', &
836+
' {', &
837+
' "name": "H", "label": "H",', &
838+
' "species": [{"element": "H", "occu": 1}],', &
839+
' "xyz": [2.0242676, 1.0811246, 0.4301417],', &
840+
' "properties": {}', &
841+
' }', &
842+
' ],', &
843+
' "properties": {}', &
844+
'}]'
845+
rewind(unit)
846+
847+
call read_pymatgen(struc, unit, error)
848+
close(unit, status='delete')
849+
850+
end subroutine test_incorrect_root
851+
804852
subroutine test_incorrect_lattice(error)
805853

806854
!> Error handling
@@ -1128,4 +1176,156 @@ subroutine test_incorrect_lattice_dim(error)
11281176

11291177
end subroutine test_incorrect_lattice_dim
11301178

1179+
subroutine test_incorrect_lattice_rank(error)
1180+
1181+
!> Error handling
1182+
type(error_type), allocatable, intent(out) :: error
1183+
1184+
character(len=*), parameter :: filename = ".test-invalid-pmg-sol5.json"
1185+
type(structure_type) :: struc
1186+
integer :: unit
1187+
1188+
open(file=filename, newunit=unit)
1189+
write(unit, '(a)') &
1190+
'{', &
1191+
' "@module": "pymatgen.core.structure",', &
1192+
' "@class": "Structure",', &
1193+
' "charge": 0.0,', &
1194+
' "lattice": {', &
1195+
' "matrix": [', &
1196+
' 5.59003664376222,', &
1197+
' 8.68089159045265,', &
1198+
' 8.68089159045265', &
1199+
' ],', &
1200+
' "pbc": [true, true, true],', &
1201+
' "a": 5.59003664376222,', &
1202+
' "b": 8.68089159045265,', &
1203+
' "c": 8.68089159045265,', &
1204+
' "alpha": 90.0,', &
1205+
' "beta": 90.0,', &
1206+
' "gamma": 90.0,', &
1207+
' "volume": 421.253303917213', &
1208+
' },', &
1209+
' "properties": {},', &
1210+
' "sites": [', &
1211+
' {', &
1212+
' "species": [{"element": "Ti", "occu": 1}],', &
1213+
' "abc": [0.0, 0.0, 0.0],', &
1214+
' "properties": {},', &
1215+
' "label": "Ti",', &
1216+
' "xyz": [0.0, 0.0, 0.0]', &
1217+
' },', &
1218+
' {', &
1219+
' "species": [{"element": "Ti", "occu": 1}],', &
1220+
' "abc": [0.5, 0.5000000000000007, 0.5000000000000007],', &
1221+
' "properties": {},', &
1222+
' "label": "Ti",', &
1223+
' "xyz": [2.79501832188111, 4.340445795226331, 4.340445795226331]', &
1224+
' },', &
1225+
' {', &
1226+
' "species": [{"element": "O", "occu": 1}], ', &
1227+
' "abc": [0.0, 0.30530000000000074, 0.30530000000000074],', &
1228+
' "properties": {},', &
1229+
' "label": "O",', &
1230+
' "xyz": [0.0, 2.6502762025652005, 2.6502762025652005]', &
1231+
' },', &
1232+
' {', &
1233+
' "species": [{"element": "O", "occu": 1}], ', &
1234+
' "abc": [0.0, 0.6947000000000005, 0.6947000000000005],', &
1235+
' "properties": {},', &
1236+
' "label": "O",', &
1237+
' "xyz": [0.0, 6.03061538788746, 6.03061538788746]', &
1238+
' },', &
1239+
' {', &
1240+
' "species": [{"element": "O", "occu": 1}], ', &
1241+
' "abc": [0.5, 0.1946999999999999, 0.8053000000000002],', &
1242+
' "properties": {},', &
1243+
' "label": "O",', &
1244+
' "xyz": [2.79501832188111, 1.69016959266113, 6.99072199779152]', &
1245+
' },', &
1246+
' {', &
1247+
' "species": [{"element": "O", "occu": 1}], ', &
1248+
' "abc": [0.5, 0.8053000000000002, 0.1946999999999999],', &
1249+
' "properties": {},', &
1250+
' "label": "O",', &
1251+
' "xyz": [2.79501832188111, 6.99072199779152, 1.69016959266113]', &
1252+
' }', &
1253+
' ]', &
1254+
'}'
1255+
rewind(unit)
1256+
1257+
call read_pymatgen(struc, unit, error)
1258+
close(unit, status='delete')
1259+
1260+
end subroutine test_incorrect_lattice_rank
1261+
1262+
subroutine test_incorrect_lattice_table(error)
1263+
1264+
!> Error handling
1265+
type(error_type), allocatable, intent(out) :: error
1266+
1267+
character(len=*), parameter :: filename = ".test-invalid-pmg-sol6.json"
1268+
type(structure_type) :: struc
1269+
integer :: unit
1270+
1271+
open(file=filename, newunit=unit)
1272+
write(unit, '(a)') &
1273+
'{', &
1274+
' "@module": "pymatgen.core.structure",', &
1275+
' "@class": "Structure",', &
1276+
' "charge": 0.0,', &
1277+
' "lattice": [],', &
1278+
' "properties": {},', &
1279+
' "sites": [', &
1280+
' {', &
1281+
' "species": [{"element": "Ti", "occu": 1}],', &
1282+
' "abc": [0.0, 0.0, 0.0],', &
1283+
' "properties": {},', &
1284+
' "label": "Ti",', &
1285+
' "xyz": [0.0, 0.0, 0.0]', &
1286+
' },', &
1287+
' {', &
1288+
' "species": [{"element": "Ti", "occu": 1}],', &
1289+
' "abc": [0.5, 0.5000000000000007, 0.5000000000000007],', &
1290+
' "properties": {},', &
1291+
' "label": "Ti",', &
1292+
' "xyz": [2.79501832188111, 4.340445795226331, 4.340445795226331]', &
1293+
' },', &
1294+
' {', &
1295+
' "species": [{"element": "O", "occu": 1}], ', &
1296+
' "abc": [0.0, 0.30530000000000074, 0.30530000000000074],', &
1297+
' "properties": {},', &
1298+
' "label": "O",', &
1299+
' "xyz": [0.0, 2.6502762025652005, 2.6502762025652005]', &
1300+
' },', &
1301+
' {', &
1302+
' "species": [{"element": "O", "occu": 1}], ', &
1303+
' "abc": [0.0, 0.6947000000000005, 0.6947000000000005],', &
1304+
' "properties": {},', &
1305+
' "label": "O",', &
1306+
' "xyz": [0.0, 6.03061538788746, 6.03061538788746]', &
1307+
' },', &
1308+
' {', &
1309+
' "species": [{"element": "O", "occu": 1}], ', &
1310+
' "abc": [0.5, 0.1946999999999999, 0.8053000000000002],', &
1311+
' "properties": {},', &
1312+
' "label": "O",', &
1313+
' "xyz": [2.79501832188111, 1.69016959266113, 6.99072199779152]', &
1314+
' },', &
1315+
' {', &
1316+
' "species": [{"element": "O", "occu": 1}], ', &
1317+
' "abc": [0.5, 0.8053000000000002, 0.1946999999999999],', &
1318+
' "properties": {},', &
1319+
' "label": "O",', &
1320+
' "xyz": [2.79501832188111, 6.99072199779152, 1.69016959266113]', &
1321+
' }', &
1322+
' ]', &
1323+
'}'
1324+
rewind(unit)
1325+
1326+
call read_pymatgen(struc, unit, error)
1327+
close(unit, status='delete')
1328+
1329+
end subroutine test_incorrect_lattice_table
1330+
11311331
end module test_read_pymatgen

0 commit comments

Comments
 (0)