You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LiebLin_Bethe_State.cc 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. /**********************************************************
  2. This software is part of J.-S. Caux's ABACUS library.
  3. Copyright (c) J.-S. Caux.
  4. -----------------------------------------------------------
  5. File: LiebLin_Bethe_State.cc
  6. Purpose: Definitions for LiebLin_Bethe_State class.
  7. ***********************************************************/
  8. #include "ABACUS.h"
  9. using namespace std;
  10. namespace ABACUS {
  11. //***************************************************************************************************
  12. // Function definitions: class LiebLin_Bethe_State
  13. LiebLin_Bethe_State::LiebLin_Bethe_State ()
  14. : c_int (0.0), L(0.0), cxL(0.0), N(0),
  15. Ix2_available(Vect<int>(0, 1)), index_first_hole_to_right (Vect<int>(0,1)), displacement (Vect<int>(0,1)),
  16. Ix2(Vect<int>(0, 1)), lambdaoc(Vect<DP>(0.0, 1)),
  17. S(Vect<DP>(0.0, 1)), dSdlambdaoc(Vect<DP>(0.0, 1)),
  18. diffsq(0.0), prec(ITER_REQ_PREC_LIEBLIN), conv(0), iter_Newton(0), E(0.0), iK(0), K(0.0), lnnorm(-100.0)
  19. {
  20. stringstream Nout; Nout << N; label = Nout.str() + LABELSEP + ABACUScoding[0] + LABELSEP;
  21. }
  22. LiebLin_Bethe_State::LiebLin_Bethe_State (DP c_int_ref, DP L_ref, int N_ref)
  23. : c_int(c_int_ref), L(L_ref), cxL(c_int_ref * L_ref), N(N_ref),
  24. Ix2_available(Vect<int>(0, 2)), index_first_hole_to_right (Vect<int>(0,N)), displacement (Vect<int>(0,N)),
  25. Ix2(Vect<int>(0, N)), lambdaoc(Vect<DP>(0.0, N)),
  26. S(Vect<DP>(0.0, N)), dSdlambdaoc(Vect<DP>(0.0, N)),
  27. diffsq(0.0), prec(ABACUS::max(1.0, 1.0/(c_int * c_int)) * ITER_REQ_PREC_LIEBLIN),
  28. conv(0), iter_Newton(0), E(0.0), iK(0), K(0.0), lnnorm(-100.0)
  29. {
  30. if (c_int < 0.0) ABACUSerror("You must use a positive interaction parameter !");
  31. if (N < 0) ABACUSerror("Particle number must be strictly positive.");
  32. stringstream Nout; Nout << N; label = Nout.str() + LABELSEP + ABACUScoding[0] + LABELSEP;
  33. // Set quantum numbers to ground-state configuration:
  34. for (int i = 0; i < N; ++i) Ix2[i] = -(N-1) + 2*i;
  35. Vect<int> OriginIx2 = Ix2;
  36. (*this).Set_Label_from_Ix2 (OriginIx2);
  37. }
  38. LiebLin_Bethe_State& LiebLin_Bethe_State::operator= (const LiebLin_Bethe_State& RefState)
  39. {
  40. if (this != &RefState) {
  41. c_int = RefState.c_int;
  42. L = RefState.L;
  43. cxL = RefState.cxL;
  44. N = RefState.N;
  45. label = RefState.label;
  46. Ix2_available = RefState.Ix2_available;
  47. index_first_hole_to_right = RefState.index_first_hole_to_right;
  48. displacement = RefState.displacement;
  49. Ix2 = RefState.Ix2;
  50. lambdaoc = RefState.lambdaoc;
  51. S = RefState.S;
  52. dSdlambdaoc = RefState.dSdlambdaoc;
  53. diffsq = RefState.diffsq;
  54. prec = RefState.prec;
  55. conv = RefState.conv;
  56. iter_Newton = RefState.iter_Newton;
  57. E = RefState.E;
  58. iK = RefState.iK;
  59. K = RefState.K;
  60. lnnorm = RefState.lnnorm;
  61. }
  62. return(*this);
  63. }
  64. void LiebLin_Bethe_State::Set_to_Label (string label_ref, const Vect<int>& OriginStateIx2)
  65. {
  66. State_Label_Data labeldata = Read_State_Label (label_ref, OriginStateIx2);
  67. if (N != labeldata.M[0]) {
  68. cout << label_ref << endl;
  69. cout << labeldata.M << endl;
  70. ABACUSerror("Trying to set an incorrect label on LiebLin_Bethe_State: N != M[0].");
  71. }
  72. if (N != OriginStateIx2.size()) {
  73. cout << label_ref << endl;
  74. cout << labeldata.M << endl;
  75. ABACUSerror("Trying to set an incorrect label on LiebLin_Bethe_State: N != OriginStateIx2.size().");
  76. }
  77. label = label_ref;
  78. Vect<int> OriginStateIx2ordered = OriginStateIx2;
  79. OriginStateIx2ordered.QuickSort();
  80. // Set all Ix2 to OriginState's Ix2:
  81. for (int i = 0; i < N; ++i) Ix2[i] = OriginStateIx2ordered[i];
  82. // Now set the excitations:
  83. for (int iexc = 0; iexc < labeldata.nexc[0]; ++iexc)
  84. for (int i = 0; i < N; ++i) if (Ix2[i] == labeldata.Ix2old[0][iexc]) Ix2[i] = labeldata.Ix2exc[0][iexc];
  85. // Now reorder the Ix2 to follow convention:
  86. Ix2.QuickSort();
  87. (*this).Set_Label_from_Ix2 (OriginStateIx2ordered);
  88. }
  89. void LiebLin_Bethe_State::Set_to_Label (string label_ref)
  90. {
  91. // This function assumes that OriginState is the ground state.
  92. Vect<int> OriginStateIx2(N);
  93. for (int i = 0; i < N; ++i) OriginStateIx2[i] = -N + 1 + 2*i;
  94. (*this).Set_to_Label(label_ref, OriginStateIx2);
  95. }
  96. void LiebLin_Bethe_State::Set_Label_from_Ix2 (const Vect<int>& OriginStateIx2)
  97. {
  98. // This function does not assume any ordering of the Ix2.
  99. if (N != OriginStateIx2.size()) ABACUSerror("N != OriginStateIx2.size() in Set_Label_from_Ix2.");
  100. // Set the state label:
  101. Vect<int> type_ref(0,1);
  102. Vect<int> M_ref(N, 1);
  103. Vect<int> nexc_ref(0, 1);
  104. // Count nr of particle-holes:
  105. for (int i = 0; i < N; ++i) if (!OriginStateIx2.includes(Ix2[i])) nexc_ref[0] += 1;
  106. Vect<Vect<int> > Ix2old_ref(1);
  107. Vect<Vect<int> > Ix2exc_ref(1);
  108. Ix2old_ref[0] = Vect<int>(ABACUS::max(nexc_ref[0],1));
  109. Ix2exc_ref[0] = Vect<int>(ABACUS::max(nexc_ref[0],1));
  110. int nexccheck = 0;
  111. for (int i = 0; i < N; ++i) if (!OriginStateIx2.includes(Ix2[i])) Ix2exc_ref[0][nexccheck++] = Ix2[i];
  112. if (nexccheck != nexc_ref[0]) ABACUSerror("Counting excitations wrong (1) in LiebLin_Bethe_State::Set_Label_from_Ix2");
  113. nexccheck = 0;
  114. for (int i = 0; i < N; ++i) if (!Ix2.includes (OriginStateIx2[i])) Ix2old_ref[0][nexccheck++] = OriginStateIx2[i];
  115. if (nexccheck != nexc_ref[0]) {
  116. cout << "nexc_ref[0] = " << nexc_ref[0] << "\tnexccheck = " << nexccheck << endl;
  117. cout << OriginStateIx2 << endl;
  118. cout << Ix2 << endl;
  119. cout << nexc_ref[0] << endl;
  120. cout << Ix2old_ref[0] << endl;
  121. cout << Ix2exc_ref[0] << endl;
  122. ABACUSerror("Counting excitations wrong (2) in LiebLin_Bethe_State::Set_Label_from_Ix2");
  123. }
  124. // Now order the Ix2old_ref and Ix2exc_ref:
  125. Ix2old_ref[0].QuickSort();
  126. Ix2exc_ref[0].QuickSort();
  127. State_Label_Data labeldata(type_ref, M_ref, nexc_ref, Ix2old_ref, Ix2exc_ref);
  128. label = Return_State_Label (labeldata, OriginStateIx2);
  129. }
  130. void LiebLin_Bethe_State::Set_Label_Internals_from_Ix2 (const Vect<int>& OriginStateIx2)
  131. {
  132. if (N != OriginStateIx2.size()) ABACUSerror("N != OriginStateIx2.size() in Set_Label_Internals_from_Ix2.");
  133. Vect<int> OriginStateIx2ordered = OriginStateIx2;
  134. OriginStateIx2ordered.QuickSort();
  135. // Set the state label:
  136. Vect<int> type_ref(0,1);
  137. Vect<int> M_ref(N, 1);
  138. Vect<int> nexc_ref(0, 1);
  139. // Count nr of particle-holes:
  140. for (int i = 0; i < N; ++i) if (!OriginStateIx2.includes(Ix2[i])) nexc_ref[0] += 1;
  141. Vect<Vect<int> > Ix2old_ref(1);
  142. Vect<Vect<int> > Ix2exc_ref(1);
  143. Ix2old_ref[0] = Vect<int>(ABACUS::max(nexc_ref[0],1));
  144. Ix2exc_ref[0] = Vect<int>(ABACUS::max(nexc_ref[0],1));
  145. int nexccheck = 0;
  146. for (int i = 0; i < N; ++i)
  147. if (Ix2[i] != OriginStateIx2ordered[i]) {
  148. Ix2old_ref[0][nexccheck] = OriginStateIx2ordered[i];
  149. Ix2exc_ref[0][nexccheck++] = Ix2[i];
  150. }
  151. State_Label_Data labeldata(type_ref, M_ref, nexc_ref, Ix2old_ref, Ix2exc_ref);
  152. label = Return_State_Label (labeldata, OriginStateIx2);
  153. // Construct the Ix2_available vector: we give one more quantum number on left and right:
  154. int navailable = 2 + (ABACUS::max(Ix2.max(), OriginStateIx2.max())
  155. - ABACUS::min(Ix2.min(), OriginStateIx2.min()))/2 - N + 1;
  156. Ix2_available = Vect<int>(navailable);
  157. index_first_hole_to_right = Vect<int>(N);
  158. // First set Ix2_available to all holes from left
  159. for (int i = 0; i < Ix2_available.size(); ++i)
  160. Ix2_available[i] = ABACUS::min(Ix2.min(), OriginStateIx2.min()) - 2 + 2*i;
  161. // Now shift according to Ix2 of OriginState:
  162. for (int j = 0; j < N; ++j) {
  163. int i = 0;
  164. while (Ix2_available[i] < OriginStateIx2ordered[j]) i++;
  165. // We now have Ix2_available[i] == OriginStateIx2[j]. Shift all Ix2_available to the right of this by 2;
  166. for (int i1 = i; i1 < navailable; ++i1) Ix2_available[i1] += 2;
  167. index_first_hole_to_right[j] = i;
  168. }
  169. // Ix2_available and index_first_hole_to_right are now fully defined.
  170. // Now set displacement vector:
  171. displacement = Vect<int>(0, N);
  172. // Set displacement vector from the Ix2:
  173. for (int j = 0; j < N; ++j) {
  174. if (Ix2[j] < OriginStateIx2ordered[j]) {
  175. // Ix2[j] must be equal to some OriginState_Ix2_available[i] for i < OriginState_index_first_hole_to_right[j]
  176. while (Ix2[j] != Ix2_available[index_first_hole_to_right[j] + displacement[j] ]) {
  177. if (index_first_hole_to_right[j] + displacement[j] == 0) {
  178. cout << label << endl << j << endl << OriginStateIx2 << endl << Ix2 << endl << Ix2_available
  179. << endl << index_first_hole_to_right << endl << displacement << endl;
  180. ABACUSerror("Going down too far in Set_Label_Internals...");
  181. }
  182. displacement[j]--;
  183. }
  184. }
  185. if (Ix2[j] > OriginStateIx2ordered[j]) {
  186. // Ix2[j] must be equal to some Ix2_available[i] for i >= index_first_hole_to_right[j]
  187. displacement[j] = 1; // start with this value to prevent segfault
  188. while (Ix2[j] != Ix2_available[index_first_hole_to_right[j] - 1 + displacement[j] ]) {
  189. if (index_first_hole_to_right[j] + displacement[j] == Ix2_available.size() - 1) {
  190. cout << label << endl << j << endl << OriginStateIx2 << endl << Ix2 << endl << Ix2_available
  191. << endl << index_first_hole_to_right << endl << displacement << endl;
  192. ABACUSerror("Going up too far in Set_Label_Internals...");
  193. }
  194. displacement[j]++;
  195. }
  196. }
  197. }
  198. }
  199. bool LiebLin_Bethe_State::Check_Admissibility (char whichDSF)
  200. {
  201. //if (Ix2.min() < -13 || Ix2.max() > 13) return(false); // For testing with restricted Hilbert space
  202. return(true);
  203. }
  204. void LiebLin_Bethe_State::Find_Rapidities (bool reset_rapidities)
  205. {
  206. // This function finds the rapidities of the eigenstate
  207. lnnorm = -100.0; // sentinel value, recalculated if Newton method used in the last step of iteration.
  208. diffsq = 1.0;
  209. if (reset_rapidities) (*this).Set_Free_lambdaocs();
  210. iter_Newton = 0;
  211. DP damping = 1.0;
  212. DP diffsq_prev = 1.0e+6;
  213. while (diffsq > prec && !is_nan(diffsq) && iter_Newton < 100) {
  214. (*this).Iterate_BAE_Newton(damping);
  215. if (diffsq > diffsq_prev && damping > 0.5) damping /= 2.0;
  216. else if (diffsq < diffsq_prev) damping = 1.0;
  217. diffsq_prev = diffsq;
  218. }
  219. conv = ((diffsq < prec) && (*this).Check_Rapidities()) ? 1 : 0;
  220. if (!conv) {
  221. cout << "Alert! State " << label << " did not converge... diffsq " << diffsq
  222. << "\titer_Newton " << iter_Newton << (*this) << endl;
  223. }
  224. return;
  225. }
  226. bool LiebLin_Bethe_State::Check_Rapidities()
  227. {
  228. bool nonan = true;
  229. for (int j = 0; j < N; ++j) nonan *= !is_nan(lambdaoc[j]);
  230. return nonan;
  231. }
  232. DP LiebLin_Bethe_State::String_delta()
  233. {
  234. return(0.0); // no strings (thus no deviations) in replusive LiebLin
  235. }
  236. bool LiebLin_Bethe_State::Check_Symmetry ()
  237. {
  238. // Checks whether the I's are symmetrically distributed.
  239. bool symmetric_state = true;
  240. Vect<int> Ix2check = Ix2;
  241. Ix2check.QuickSort();
  242. for (int alpha = 0; alpha <= N/2; ++alpha)
  243. symmetric_state = symmetric_state && (Ix2check[alpha] == -Ix2check[N - 1 - alpha]);
  244. return(symmetric_state);
  245. }
  246. void LiebLin_Bethe_State::Compute_lnnorm ()
  247. {
  248. if (lnnorm == -100.0) { // else Gaudin part already calculated by Newton method
  249. SQMat_DP Gaudin_Red(N);
  250. (*this).Build_Reduced_Gaudin_Matrix(Gaudin_Red);
  251. lnnorm = real(lndet_LU_dstry(Gaudin_Red));
  252. // Add the pieces outside of Gaudin determinant
  253. for (int j = 0; j < N - 1; ++j) for (int k = j+1; k < N; ++k)
  254. lnnorm += log(1.0 + 1.0/pow(lambdaoc[j] - lambdaoc[k], 2.0));
  255. }
  256. return;
  257. }
  258. void LiebLin_Bethe_State::Compute_All (bool reset_rapidities) // solves BAE, computes E, K and lnnorm
  259. {
  260. (*this).Find_Rapidities (reset_rapidities);
  261. if (conv == 1) {
  262. (*this).Compute_Energy ();
  263. (*this).Compute_Momentum ();
  264. (*this).Compute_lnnorm ();
  265. }
  266. return;
  267. }
  268. void LiebLin_Bethe_State::Set_Free_lambdaocs()
  269. {
  270. if (cxL >= 1.0)
  271. for (int a = 0; a < N; ++a) lambdaoc[a] = PI * Ix2[a]/cxL;
  272. // For small values of c, use better approximation using approximate zeroes of Hermite polynomials: see Gaudin eqn 4.71.
  273. if (cxL < 1.0) {
  274. DP oneoversqrtcLN = 1.0/pow(cxL * N, 0.5);
  275. for (int a = 0; a < N; ++a) lambdaoc[a] = oneoversqrtcLN * PI * Ix2[a];
  276. }
  277. return;
  278. }
  279. void LiebLin_Bethe_State::Iterate_BAE (DP damping)
  280. {
  281. // does one step of simple iterations
  282. DP sumtheta = 0.0;
  283. Vect_DP dlambdaoc (0.0, N);
  284. for (int j = 0; j < N; ++j) {
  285. sumtheta = 0.0;
  286. for (int k = 0; k < N; ++k) sumtheta += atan((lambdaoc[j] - lambdaoc[k]));
  287. sumtheta *= 2.0;
  288. dlambdaoc[j] = damping * ((PI*Ix2[j] - sumtheta)/cxL - lambdaoc[j]);
  289. }
  290. diffsq = 0.0;
  291. for (int i = 0; i < N; ++i) {
  292. lambdaoc[i] += dlambdaoc[i];
  293. // Normalize the diffsq by the typical value of the rapidities:
  294. if (cxL > 1.0) diffsq += dlambdaoc[i] * dlambdaoc[i]/(lambdaoc[i] * lambdaoc[i] + 1.0e-6);
  295. else diffsq += cxL * cxL * dlambdaoc[i] * dlambdaoc[i]/(lambdaoc[i] * lambdaoc[i] + 1.0e-6);
  296. }
  297. diffsq /= DP(N);
  298. return;
  299. }
  300. void LiebLin_Bethe_State::Iterate_BAE_S (DP damping)
  301. {
  302. // This is essentially Newton's method but only in one variable.
  303. // The logic is that the derivative of the LHS of the BE_j w/r to lambdaoc_j is much larger
  304. // than with respect to lambdaoc_l with l != j.
  305. Vect_DP dlambdaoc (0.0, N);
  306. // Start by calculating S and dSdlambdaoc:
  307. for (int j = 0; j < N; ++j) {
  308. S[j] = 0.0;
  309. for (int k = 0; k < N; ++k) S[j] += atan((lambdaoc[j] - lambdaoc[k]));
  310. S[j] *= 2.0/cxL;
  311. dSdlambdaoc[j] = 0.0;
  312. for (int k = 0; k < N; ++k)
  313. dSdlambdaoc[j] += 1.0/((lambdaoc[j] - lambdaoc[k]) * (lambdaoc[j] - lambdaoc[k]) + 1.0);
  314. dSdlambdaoc[j] *= 2.0/(PI * cxL);
  315. dlambdaoc[j] = (PI*Ix2[j]/cxL - S[j] + lambdaoc[j] * dSdlambdaoc[j])/(1.0 + dSdlambdaoc[j]) - lambdaoc[j];
  316. }
  317. diffsq = 0.0;
  318. for (int i = 0; i < N; ++i) {
  319. lambdaoc[i] += damping * dlambdaoc[i];
  320. // Normalize the diffsq by the typical value of the rapidities:
  321. if (cxL > 1.0) diffsq += dlambdaoc[i] * dlambdaoc[i]/(lambdaoc[i] * lambdaoc[i] + 1.0e-6);
  322. else diffsq += cxL * cxL * dlambdaoc[i] * dlambdaoc[i]/(lambdaoc[i] * lambdaoc[i] + 1.0e-6);
  323. }
  324. diffsq /= DP(N);
  325. iter_Newton++;
  326. return;
  327. }
  328. void LiebLin_Bethe_State::Iterate_BAE_Newton (DP damping)
  329. {
  330. // does one step of a Newton method on the rapidities...
  331. Vect_DP RHSBAE (0.0, N); // contains RHS of BAEs
  332. Vect_DP dlambdaoc (0.0, N); // contains delta lambdaoc computed from Newton's method
  333. SQMat_DP Gaudin (0.0, N);
  334. Vect_INT indx (N);
  335. DP sumtheta = 0.0;
  336. int atanintshift = 0; // for large |lambda|, use atan (lambda) = sgn(lambda) pi/2 - atan(1/lambda)
  337. DP lambdahere = 0.0;
  338. // Compute the RHS of the BAEs:
  339. for (int j = 0; j < N; ++j) {
  340. sumtheta = 0.0;
  341. atanintshift = 0;
  342. for (int k = 0; k < N; ++k)
  343. if (j != k) { // otherwise 0
  344. if (fabs(lambdahere = lambdaoc[j] - lambdaoc[k]) < 1.0) { // use straight atan
  345. sumtheta += atan(lambdahere);
  346. }
  347. else { // for large rapidities, use dual form of atan, extracting pi/2 factors
  348. atanintshift += sgn_DP(lambdahere);
  349. sumtheta -= atan(1.0/lambdahere);
  350. }
  351. }
  352. sumtheta *= 2.0;
  353. RHSBAE[j] = cxL * lambdaoc[j] + sumtheta - PI*(Ix2[j] - atanintshift);
  354. }
  355. (*this).Build_Reduced_Gaudin_Matrix (Gaudin);
  356. for (int j = 0; j < N; ++j) dlambdaoc[j] = - RHSBAE[j];
  357. DP d;
  358. ludcmp (Gaudin, indx, d);
  359. lubksb (Gaudin, indx, dlambdaoc);
  360. bool ordering_changed = false;
  361. for (int j = 0; j < N-1; ++j) if (lambdaoc[j] + dlambdaoc[j] > lambdaoc[j+1] + dlambdaoc[j+1]) ordering_changed = true;
  362. // To prevent Newton from diverging, we limit the size of the rapidity changes.
  363. // The leftmost and rightmost rapidities can grow by one order of magnitude per iteration step.
  364. if (ordering_changed) { // We explicitly ensure that the ordering remains correct after the iteration step.
  365. bool ordering_still_changed = false;
  366. DP maxdlambdaoc = 0.0;
  367. do {
  368. ordering_still_changed = false;
  369. if (dlambdaoc[0] < 0.0 && fabs(dlambdaoc[0])
  370. > (maxdlambdaoc = 10.0*ABACUS::max(fabs(lambdaoc[0]), fabs(lambdaoc[N-1]))))
  371. dlambdaoc[0] = -maxdlambdaoc;
  372. if (lambdaoc[0] + dlambdaoc[0] > lambdaoc[1] + dlambdaoc[1]) {
  373. dlambdaoc[0] = 0.25 * (lambdaoc[1] + dlambdaoc[1] - lambdaoc[0] ); // max quarter distance
  374. ordering_still_changed = true;
  375. }
  376. if (dlambdaoc[N-1] > 0.0 && fabs(dlambdaoc[N-1])
  377. > (maxdlambdaoc = 10.0*ABACUS::max(fabs(lambdaoc[0]), fabs(lambdaoc[N-1]))))
  378. dlambdaoc[N-1] = maxdlambdaoc;
  379. if (lambdaoc[N-1] + dlambdaoc[N-1] < lambdaoc[N-2] + dlambdaoc[N-2]) {
  380. dlambdaoc[N-1] = 0.25 * (lambdaoc[N-2] + dlambdaoc[N-2] - lambdaoc[N-1]);
  381. ordering_still_changed = true;
  382. }
  383. for (int j = 1; j < N-1; ++j) {
  384. if (lambdaoc[j] + dlambdaoc[j] > lambdaoc[j+1] + dlambdaoc[j+1]) {
  385. dlambdaoc[j] = 0.25 * (lambdaoc[j+1] + dlambdaoc[j+1] - lambdaoc[j]);
  386. ordering_still_changed = true;
  387. }
  388. if (lambdaoc[j] + dlambdaoc[j] < lambdaoc[j-1] + dlambdaoc[j-1]) {
  389. dlambdaoc[j] = 0.25 * (lambdaoc[j-1] + dlambdaoc[j-1] - lambdaoc[j]);
  390. ordering_still_changed = true;
  391. }
  392. }
  393. } while (ordering_still_changed);
  394. }
  395. diffsq = 0.0;
  396. for (int i = 0; i < N; ++i) {
  397. // Normalize the diffsq by the typical value of the rapidities:
  398. if (cxL > 1.0) diffsq += dlambdaoc[i] * dlambdaoc[i]/(lambdaoc[i] * lambdaoc[i] + 1.0e-6);
  399. else diffsq += cxL * cxL * dlambdaoc[i] * dlambdaoc[i]/(lambdaoc[i] * lambdaoc[i] + 1.0e-6);
  400. }
  401. diffsq /= DP(N);
  402. if (ordering_changed) diffsq = 1.0; // reset if Newton wanted to change ordering
  403. for (int j = 0; j < N; ++j) lambdaoc[j] += damping * dlambdaoc[j];
  404. iter_Newton++;
  405. // if we've converged, calculate the norm here, since the work has been done...
  406. if (diffsq < prec && !ordering_changed) {
  407. lnnorm = 0.0;
  408. for (int j = 0; j < N; j++) lnnorm += log(fabs(Gaudin[j][j]));
  409. // Add the pieces outside of Gaudin determinant
  410. for (int j = 0; j < N - 1; ++j)
  411. for (int k = j+1; k < N; ++k)
  412. lnnorm += log(1.0 + 1.0/pow(lambdaoc[j] - lambdaoc[k], 2.0));
  413. }
  414. return;
  415. }
  416. void LiebLin_Bethe_State::Compute_Energy()
  417. {
  418. E = 0.0;
  419. for (int j = 0; j < N; ++j) E += lambdaoc[j] * lambdaoc[j];
  420. E *= c_int * c_int;
  421. }
  422. void LiebLin_Bethe_State::Compute_Momentum()
  423. {
  424. iK = 0;
  425. for (int j = 0; j < N; ++j) {
  426. iK += Ix2[j];
  427. }
  428. if (iK % 2) {
  429. cout << Ix2 << endl;
  430. cout << iK << "\t" << iK % 2 << endl;
  431. ABACUSerror("Sum of Ix2 is not even: inconsistency.");
  432. }
  433. iK /= 2; // sum of Ix2 is guaranteed even.
  434. K = 2.0 * iK * PI/L;
  435. }
  436. DP LiebLin_Bethe_State::Kernel (int a, int b)
  437. {
  438. return(2.0/(pow(lambdaoc[a] - lambdaoc[b], 2.0) + 1.0));
  439. }
  440. DP LiebLin_Bethe_State::Kernel (DP lambdaoc_ref)
  441. {
  442. return(2.0/(lambdaoc_ref * lambdaoc_ref + 1.0));
  443. }
  444. void LiebLin_Bethe_State::Build_Reduced_Gaudin_Matrix (SQMat<DP>& Gaudin_Red)
  445. {
  446. if (Gaudin_Red.size() != N)
  447. ABACUSerror("Passing matrix of wrong size in Build_Reduced_Gaudin_Matrix.");
  448. DP sum_Kernel = 0.0;
  449. for (int j = 0; j < N; ++j)
  450. for (int k = 0; k < N; ++k) {
  451. if (j == k) {
  452. sum_Kernel = 0.0;
  453. for (int kp = 0; kp < N; ++kp) if (j != kp) sum_Kernel += Kernel (lambdaoc[j] - lambdaoc[kp]);
  454. Gaudin_Red[j][k] = cxL + sum_Kernel;
  455. }
  456. else Gaudin_Red[j][k] = - Kernel (lambdaoc[j] - lambdaoc[k]);
  457. }
  458. return;
  459. }
  460. void LiebLin_Bethe_State::Build_Reduced_BEC_Quench_Gaudin_Matrix (SQMat<DP>& Gaudin_Red)
  461. {
  462. // Passing a matrix of dimension N/2
  463. if (N % 2 != 0) ABACUSerror("Choose a state with even numer of particles please");
  464. // Check Parity invariant
  465. bool ck = true;
  466. for (int j = 0; j < N/2; ++j){ if(Ix2[j] != - Ix2[N-j-1]) ck = false;}
  467. if (!ck) ABACUSerror("Choose a parity invariant state please");
  468. if (Gaudin_Red.size() != N/2)
  469. ABACUSerror("Passing matrix of wrong size in Build_Reduced_Gaudin_Matrix.");
  470. DP sum_Kernel = 0.0;
  471. for (int j = 0; j < N/2; ++j)
  472. for (int k = 0; k < N/2; ++k) {
  473. if (j == k) {
  474. sum_Kernel = 0.0;
  475. for (int kp = N/2; kp < N; ++kp)
  476. if (j + N/2 != kp)
  477. sum_Kernel += Kernel (lambdaoc[j+N/2] - lambdaoc[kp]) + Kernel (lambdaoc[j+N/2] + lambdaoc[kp]);
  478. Gaudin_Red[j][k] = cxL + sum_Kernel;
  479. }
  480. else Gaudin_Red[j][k] = - (Kernel (lambdaoc[j+ N/2] - lambdaoc[k+ N/2])
  481. + Kernel (lambdaoc[j+ N/2] + lambdaoc[k+ N/2]) );
  482. }
  483. return;
  484. }
  485. void LiebLin_Bethe_State::Annihilate_ph_pair (int ipart, int ihole, const Vect<int>& OriginStateIx2)
  486. {
  487. // This function changes the Ix2 of a given state by annihilating a particle and hole
  488. // pair specified by ipart and ihole (counting from the left, starting with index 0).
  489. State_Label_Data currentdata = Read_State_Label ((*this).label, OriginStateIx2);
  490. if (ipart >= currentdata.nexc[0])
  491. ABACUSerror("Particle label too large in LiebLin_Bethe_State::Annihilate_ph_pair.");
  492. if (ihole >= currentdata.nexc[0])
  493. ABACUSerror("Hole label too large in LiebLin_Bethe_State::Annihilate_ph_pair.");
  494. // Simply remove the given pair:
  495. Vect<int> type_new = currentdata.type;
  496. Vect<int> M_new = currentdata.M;
  497. Vect<int> nexc_new = currentdata.nexc;
  498. nexc_new[0] -= 1; // we drill one more particle-hole pair at level 0
  499. int ntypespresent = 1; // only one type for LiebLin
  500. Vect<Vect<int> > Ix2old_new(ntypespresent);
  501. Vect<Vect<int> > Ix2exc_new(ntypespresent);
  502. for (int it = 0; it < ntypespresent; ++it) Ix2old_new[it] = Vect<int>(ABACUS::max(nexc_new[it],1));
  503. for (int it = 0; it < ntypespresent; ++it) Ix2exc_new[it] = Vect<int>(ABACUS::max(nexc_new[it],1));
  504. // Copy earlier data in, leaving out ipart and ihole:
  505. for (int it = 0; it < ntypespresent; ++it) {
  506. for (int i = 0; i < nexc_new[it]; ++i) {
  507. Ix2old_new[it][i] = currentdata.Ix2old[it][i + (i >= ihole)];
  508. Ix2exc_new[it][i] = currentdata.Ix2exc[it][i + (i >= ipart)];
  509. }
  510. }
  511. State_Label_Data newdata (type_new, M_new, nexc_new, Ix2old_new, Ix2exc_new);
  512. (*this).Set_to_Label (Return_State_Label(newdata, OriginStateIx2));
  513. }
  514. void LiebLin_Bethe_State::Parity_Flip ()
  515. {
  516. // For simplicity, we don't redo base_id, type_id, id.
  517. Vect_INT Ix2buff = Ix2;
  518. Vect_DP lambdaocbuff = lambdaoc;
  519. for (int i = 0; i < N; ++i) Ix2[i] = -Ix2buff[N - 1 - i];
  520. for (int i = 0; i < N; ++i) lambdaoc[i] = -lambdaocbuff[N - 1 - i];
  521. iK = -iK;
  522. K = -K;
  523. }
  524. std::ostream& operator<< (std::ostream& s, const LiebLin_Bethe_State& state)
  525. {
  526. s << endl << "******** State for c = " << state.c_int << " L = " << state.L << " N = " << state.N
  527. << " with label " << state.label << " ********" << endl;
  528. s << "Ix2:" << endl;
  529. for (int j = 0; j < state.N; ++j) s << state.Ix2[j] << " ";
  530. s << endl << "lambdaocs:" << endl;
  531. for (int j = 0; j < state.N; ++j) s << state.lambdaoc[j] << " ";
  532. s << endl << "conv = " << state.conv << " iter_Newton = " << state.iter_Newton << endl;
  533. s << "E = " << state.E << " iK = " << state.iK << " K = " << state.K << " lnnorm = " << state.lnnorm << endl;
  534. return(s);
  535. }
  536. } // namespace ABACUS