Coverage for gpaw/test/solvation/test_spinpol.py: 100%

28 statements  

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

1import pytest 

2from gpaw.utilities.adjust_cell import adjust_cell 

3from ase.build import molecule 

4from ase.units import Pascal, m 

5from gpaw.solvation import ( 

6 SolvationGPAW, 

7 EffectivePotentialCavity, 

8 Power12Potential, 

9 LinearDielectric, 

10 SurfaceInteraction, 

11 VolumeInteraction, 

12 LeakedDensityInteraction, 

13 GradientSurface, 

14 KB51Volume) 

15import numpy as np 

16 

17 

18def test_solvation_spinpol(): 

19 h = 0.3 

20 vac = 3.0 

21 u0 = .180 

22 epsinf = 80. 

23 T = 298.15 

24 atomic_radii = {'H': 1.09} 

25 

26 atoms = molecule('CN') 

27 adjust_cell(atoms, vac, h) 

28 atoms2 = atoms.copy() 

29 atoms2.set_initial_magnetic_moments(None) 

30 

31 atomss = (atoms, atoms2) 

32 Es = [] 

33 Fs = [] 

34 

35 for atoms in atomss: 

36 atoms.calc = SolvationGPAW( 

37 mode='fd', xc='LDA', h=h, charge=-1, 

38 cavity=EffectivePotentialCavity( 

39 effective_potential=Power12Potential(atomic_radii, u0), 

40 temperature=T, 

41 surface_calculator=GradientSurface(), 

42 volume_calculator=KB51Volume() 

43 ), 

44 dielectric=LinearDielectric(epsinf=epsinf), 

45 interactions=[ 

46 SurfaceInteraction( 

47 surface_tension=100. * 1e-3 * Pascal * m 

48 ), 

49 VolumeInteraction( 

50 pressure=-1.0 * 1e9 * Pascal 

51 ), 

52 LeakedDensityInteraction( 

53 voltage=1.0 

54 ) 

55 ] 

56 ) 

57 Es.append(atoms.get_potential_energy()) 

58 Fs.append(atoms.get_forces()) 

59 

60 # compare to expected difference of a gas phase calc 

61 print('difference E: ', Es[0] - Es[1]) 

62 assert Es[0] == pytest.approx(Es[1], abs=0.0002) 

63 print('difference F: ', np.abs(Fs[0] - Fs[1]).max()) 

64 assert Fs[0] == pytest.approx(Fs[1], abs=0.003) 

65 

66 # XXX add test case where spin matters, e.g. charge=0 for CN?