testDressedStates.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. from scipy.constants import pi
  2. import numpy as np
  3. from dressedstatefuncs import DressedFermionState, DressedFermionBasis
  4. from dressedstatefuncs import ZeroModeRaisingOperator, ZeroModeLoweringOperator
  5. from statefuncs import omega, NotInBasis
  6. from numpy import sqrt
  7. import unittest
  8. class TestDressedStateMethods(unittest.TestCase):
  9. def setUp(self):
  10. self.L = 2*pi
  11. self.nmax = 1
  12. self.m = 0.
  13. self.Emax = 2.
  14. #create two states, one with the zero mode in the ground state
  15. #and one with the zero mode in the first excited state
  16. #third argument is azeromode
  17. #and two more with particles/antiparticles
  18. # RMstate1 = DressedFermionState([0,0,1],[0,0,0],1,
  19. # self.nmax,self.L,self.m,
  20. # checkAtRest=False,checkChargeNeutral=False)
  21. # RMstate2 = DressedFermionState([0,0,0],[0,0,1],1,
  22. # self.nmax,self.L,self.m,
  23. # checkAtRest=False,checkChargeNeutral=False)
  24. def testSetGet(self):
  25. state1 = DressedFermionState([0,0,0],[0,0,0],0,
  26. self.nmax,self.L,self.m,
  27. checkAtRest=False,checkChargeNeutral=False)
  28. state2 = DressedFermionState([0,0,0],[0,0,0],1,
  29. self.nmax,self.L,self.m,
  30. checkAtRest=False,checkChargeNeutral=False)
  31. omega0 = 1/np.sqrt(pi)
  32. self.assertEqual(state1.getAZeroMode(),0)
  33. self.assertEqual(state2.getAZeroMode(),1)
  34. self.assertEqual(state1.energy,0)
  35. self.assertEqual(state2.energy,omega0)
  36. state1.setAZeroMode(1)
  37. self.assertEqual(state1,state2)
  38. #check that the energy has updated correctly
  39. self.assertAlmostEqual(state1.energy,omega0)
  40. state1.setAZeroMode(2)
  41. self.assertAlmostEqual(state1.energy,2*omega0)
  42. #reset the zero mode to the 1-state
  43. state1.setAZeroMode(1)
  44. #add a fermion with k=1
  45. state1[1] = [1,0]
  46. self.assertAlmostEqual(state1.energy,1+omega0)
  47. #add an antifermion with k=-1
  48. state1[-1] = [0,1]
  49. self.assertAlmostEqual(state1.energy,2+omega0)
  50. #print(state1)
  51. def testBasis(self):
  52. # periodic_basis = DressedFermionBasis(self.L, self.Emax,
  53. # self.m, bcs="periodic")
  54. antiperiodic_basis = DressedFermionBasis(self.L, self.Emax,
  55. self.m, bcs="antiperiodic")
  56. self.assertEqual(antiperiodic_basis.size,9)
  57. #optional: can print basis elements
  58. #expected output:
  59. # [(Particle occs: [0 0], antiparticle occs: [0 0], zero mode: 0),
  60. # (Particle occs: [0 0], antiparticle occs: [0 0], zero mode: 1),
  61. # (Particle occs: [1 0], antiparticle occs: [0 1], zero mode: 0),
  62. # (Particle occs: [0 1], antiparticle occs: [1 0], zero mode: 0),
  63. # (Particle occs: [0 0], antiparticle occs: [0 0], zero mode: 2),
  64. # (Particle occs: [1 0], antiparticle occs: [0 1], zero mode: 1),
  65. # (Particle occs: [0 1], antiparticle occs: [1 0], zero mode: 1),
  66. # (Particle occs: [0 0], antiparticle occs: [0 0], zero mode: 3),
  67. # (Particle occs: [1 1], antiparticle occs: [1 1], zero mode: 0)]
  68. # energies:
  69. # 0.0
  70. # 0.5641895835477563
  71. # 1.0
  72. # 1.0
  73. # 1.1283791670955126
  74. # 1.5641895835477562
  75. # 1.5641895835477562
  76. # 1.692568750643269
  77. # 2.0
  78. class TestRaisingLoweringOperators(unittest.TestCase):
  79. def testTransformState(self):
  80. raising_op = ZeroModeRaisingOperator()
  81. lowering_op = ZeroModeLoweringOperator()
  82. transformedstate = DressedFermionState([0,0],[0,0],1,0.5,L=2*pi,m=0)
  83. mybasis = DressedFermionBasis(2*pi, 1.5, 0, bcs="antiperiodic")
  84. #print(mybasis)
  85. # expected output:
  86. # [(Particle occs: [0 0], antiparticle occs: [0 0], zero mode: 0),
  87. # (Particle occs: [0 0], antiparticle occs: [0 0], zero mode: 1),
  88. # (Particle occs: [1 0], antiparticle occs: [0 1], zero mode: 0),
  89. # (Particle occs: [0 1], antiparticle occs: [1 0], zero mode: 0),
  90. # (Particle occs: [0 0], antiparticle occs: [0 0], zero mode: 2)]
  91. n, newstate = raising_op._transformState(mybasis[0])
  92. self.assertEqual(n, 1.0)
  93. self.assertEqual(newstate,transformedstate)
  94. n, newstate = lowering_op._transformState(newstate)
  95. self.assertEqual(n,1.0)
  96. self.assertEqual(newstate,mybasis[0])
  97. n, newstate = lowering_op._transformState(mybasis[0])
  98. self.assertEqual(n, 0)
  99. self.assertEqual(newstate, None)
  100. n, newstate = lowering_op._transformState(mybasis[4])
  101. self.assertAlmostEqual(n,sqrt(2))
  102. self.assertEqual(newstate,mybasis[1])
  103. n, index = raising_op.apply2(mybasis, mybasis[0])
  104. self.assertEqual(n,1.0)
  105. self.assertEqual(index,1)
  106. n, index = raising_op.apply2(mybasis, mybasis[1])
  107. self.assertEqual(n,sqrt(2))
  108. self.assertEqual(index,4)
  109. #raise a state out of the basis
  110. self.assertRaises(NotInBasis, raising_op.apply2, mybasis, mybasis[4])
  111. #annihilate a state
  112. n, index = lowering_op.apply2(mybasis, mybasis[0])
  113. self.assertEqual(n,0)
  114. self.assertEqual(index,None)