Guest User

Untitled

a guest
Jan 18th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. "use strict";
  2. /* simulation parameters */
  3. const NMESH = 512; // mesh points
  4. const DT = 0.1; // time step
  5. const NSTEP = 50000; // number of steps
  6. const NECAL = 1;
  7. const NNCAL = 1000;
  8.  
  9. /* system parameters */
  10. const M = 2000; // mass
  11. const LX = 50; // box length
  12. const initCond = {
  13. x: 19, //position
  14. spread: 1, // spread
  15. p: 20, // momentum
  16. };
  17.  
  18. let dx = LX / NMESH;
  19.  
  20. let hamEl = (x) => {
  21. let hmat = [[],[]];
  22. let A=0.01;
  23. let B=1.6;
  24. let C=0.005; //SB: 0.005
  25. let D=1.0;
  26.  
  27. x = x-0.5*LX;
  28.  
  29. hmat[0][0] = x!==0 ? A*(1-Math.exp(-B*Math.abs(x)))*x/Math.abs(x) : 0;
  30. hmat[0][1] = C*Math.exp(-D*x*x);
  31. hmat[1][0] = hmat[0][1];
  32. hmat[1][1] = -hmat[0][0];
  33.  
  34. return hmat;
  35. }
  36.  
  37. let ergEl = (hmat) => {
  38. let erg = [];
  39.  
  40.  
  41. let discr = Math.sqrt((hmat[0][0] - hmat[1][1])*(hmat[0][0] - hmat[1][1])+4*hmat[0][1]*hmat[1][0]);
  42. erg[0] = 0.5*(hmat[0][0] + hmat[1][1] + discr);
  43. erg[1] = 0.5*(hmat[0][0] + hmat[1][1] - discr);
  44.  
  45. return erg;
  46. }
  47.  
  48. const EIGVEC = (x) => {
  49. let hmat = hamEl(x);
  50. return [[ hmat[0][1] / (ergEl(hmat)[0] - hmat[0][0]),
  51. 1
  52. ],
  53. [ hmat[0][1] / (ergEl(hmat)[1] - hmat[0][0]),
  54. 1
  55. ]]
  56. }
  57.  
  58. let initWavefn = (x) => {
  59.  
  60. if (x > LX) {
  61. return initWavefn(x - LX);
  62. }
  63.  
  64. let gauss = Math.exp(-(x-initCond.x)*(x-initCond.x)/4.0/(initCond.spread*initCond.spread));
  65.  
  66. let comps = [[],[]];
  67.  
  68. comps[0][0] = gauss*Math.cos(initCond.p*(x-initCond.x));
  69. comps[0][1] = gauss*Math.sin(initCond.p*(x-initCond.x));
  70. comps[1][0] = 0;
  71. comps[1][1] = 0;
  72.  
  73. return comps;
  74. }
  75.  
  76. let computeNormFac = (wavefn) => {
  77. let sumsq = 0;
  78. for (let i = 0; i < NMESH; i++) {
  79. let wf = wavefn(i*dx)
  80. sumsq += wf[0]*wf[0]+wf[1]*wf[1];
  81. }
  82.  
  83. if (sumsq > 0) {
  84. return 1/Math.sqrt(sumsq);
  85. } else if (sumsq === 0) {
  86. return 1;
  87. } else {
  88. throw new Error('noooooo!');
  89. }
  90. }
  91.  
  92. let normFac = [computeNormFac((x)=>initWavefn(x)[0]),
  93. computeNormFac((x)=>initWavefn(x)[1])];
  94.  
  95. let normWavefn = (x) => {
  96. return [initWavefn(x)[0].map((v)=>v*normFac[0]),initWavefn(x)[1].map((v)=>v*normFac[1])];
  97. }
  98.  
  99. let vverlet = (x, p, pot) => {
  100. let vel = p/M;
  101.  
  102. let force = (x) => {
  103. if (x >= dx) return - 0.5*(pot(x+dx)-pot(x-dx))/dx;
  104. else return 0;
  105. }
  106.  
  107. let xnew = x + vel*DT+0.5*force(x)/M*DT*DT;
  108.  
  109. let fnew = force(xnew);
  110. let vnew = vel + 0.5*(fnew+force(x))/M*DT;
  111. let pnew = vnew*M;
  112.  
  113. return [xnew, pnew, vnew]
  114. }
  115.  
  116. let matD = (s1, s2) => {
  117. let termD = 0;
  118. let diffS2 = (x) => {
  119. if (x > dx)
  120. return 0.5*(EIGVEC(x+dx)[s2] - EIGVEC(x-dx)[s2]) / dx;
  121.  
  122. return (EIGVEC(x+dx)[s2] - EIGVEC(x)[s2]) / dx;
  123. }
  124. return (x) => EIGVEC(x)[s1]*diffS2(x);
  125. };
  126.  
  127. matD = (s1, s2) => {
  128. let completeWf = [];
  129. completeWf[0] =
  130.  
  131. return (x) => {
  132.  
  133. }
  134. }
  135.  
  136. let singleStep = ({x, p, state, coeffs}) => {
  137. let potcurr = (xv) => {return ergEl(hamEl(xv))[state]};
  138. let potother = (xv) => {return ergEl(hamEl(xv))[+!state]};
  139. let [xnew, pnew, vel] = vverlet(x, p, potcurr);
  140.  
  141.  
  142. let coeffsnew = coeffs;
  143.  
  144. let matA = (s1, s2) => {
  145. return coeffsnew[s1]*coeffsnew[s2]
  146. };
  147.  
  148. let matB = (s1, s2) => {
  149. return -2*matA(s2,s1)*matD(s1,s2)(xnew)*vel;
  150. };
  151.  
  152. let probg = DT*matB(state, +!state) / matA(state, state); // g12
  153.  
  154. let ifHop = Math.random() < probg;
  155.  
  156. if (ifHop && (pnew*pnew*0.5/M < (potother(xnew) - potcurr(xnew)))) {
  157. ifHop = false;
  158. }
  159.  
  160. if (ifHop) {
  161. pnew = Math.sqrt(pnew*pnew+2*M*(potcurr(xnew) - potother(xnew)));
  162. }
  163. return {xnew, pnew, ifHop, coeffsnew};
  164. }
  165.  
  166. /*----------------*/
  167.  
  168. let coeffs = normWavefn(initCond.x)[0];
  169.  
  170. initCond.state = 0;
  171. initCond.coeffs = normWavefn(initCond.x)[initCond.state];
  172.  
  173. let currCond = initCond;
  174.  
  175. for (let i = 0; i < 5; i++) {
  176. let newCond = {};
  177. let res = singleStep(currCond);
  178. //console.log(res);
  179.  
  180. [newCond.x, newCond.p] = [res.xnew, res.pnew];
  181.  
  182. let newNormWavefn;
  183.  
  184. if (res.ifHop) {
  185. newCond.state = +!currCond.state;
  186. newCond.coeffs[newCond.state] = res.coeffsnew[currCond.state];
  187. newCond.coeffs[currCond.state] = [0,0];
  188. } else {
  189. newCond.state = currCond.state;
  190. newCond.coeffs = res.coeffsnew;
  191. }
  192.  
  193. currCond = newCond;
  194. }
  195.  
  196. for (let i = 0; i < 1; i++) {
  197. console.log((matD(0,1))(2));
  198. }
Add Comment
Please, Sign In to add comment