Coverage for gpaw/test/ext_potential/test_collection.py: 100%

19 statements  

« prev     ^ index     » next       coverage.py v7.7.1, created at 2025-07-14 00:18 +0000

1from gpaw.grid_descriptor import GridDescriptor 

2from gpaw.external import (ConstantElectricField, PointChargePotential, 

3 PotentialCollection, create_external_potential) 

4 

5 

6def test_collection(): 

7 a = 4.0 

8 N = 48 

9 gd = GridDescriptor((N, N, N), (a, a, a), 0) 

10 

11 ext1 = ConstantElectricField(1) 

12 ext2 = PointChargePotential([1, -5], positions=((0, 0, -10), (0, 0, 10))) 

13 collection = PotentialCollection([ext1, ext2]) 

14 

15 ext1.calculate_potential(gd) 

16 ext2.calculate_potential(gd) 

17 collection.calculate_potential(gd) 

18 

19 assert (collection.vext_g == ext1.vext_g + ext2.vext_g).all() 

20 

21 assert len(collection.todict()['potentials']) == 2 

22 for text in ['Constant electric', 'Point-charge', 

23 collection.__class__.__name__]: 

24 assert text in collection.__str__() 

25 

26 # reconstruct 

27 collection2 = create_external_potential(**collection.todict()) 

28 collection2.calculate_potential(gd) 

29 assert (collection2.vext_g == collection.vext_g).all()