|
| 1 | +! ********************************************************* |
| 2 | +! * An awkwardly simple code for the dynamics of an * |
| 3 | +! * an n-dimensional harmonic oscillator, to demonstrate * |
| 4 | +! * the use of colored-noise Langevin equation in * |
| 5 | +! * thermostatting. * |
| 6 | +! * * |
| 7 | +! * Code has been kept as modular as possible, and it * |
| 8 | +! * should be relatively straightforward to adapt it for * |
| 9 | +! * other serial MD codes. In parallel codes using domain * |
| 10 | +! * decomposition, the additional degrees of freedom * |
| 11 | +! * should "follow" the corresponding atoms. * |
| 12 | +! * * |
| 13 | +! * Feel free to copy, modify, lend, borrow, nuke this * |
| 14 | +! * code, which is licensed under GPLv3 [www.gnu.org] * |
| 15 | +! * e-mail me at michele dot ceriotti at gmail dot com * |
| 16 | +! ********************************************************* |
| 17 | + |
| 18 | +program harmonic |
| 19 | + use md_tools |
| 20 | + use md_nose |
| 21 | + use md_gle |
| 22 | + |
| 23 | + implicit none |
| 24 | + |
| 25 | + ! * options to be given in input file * |
| 26 | + ! seed initial seed for PRNG |
| 27 | + ! temp target temperature |
| 28 | + ! dt timestep |
| 29 | + ! nstep number of steps to be performed |
| 30 | + ! stride output every stride frames |
| 31 | + ! ndof dimensionality of the oscillator |
| 32 | + ! wfile name of the file to read the hessian |
| 33 | + ! decomposition from (see below) |
| 34 | + ! traj logical, whether to output p,q trajectory |
| 35 | + ! * thermostatting options * |
| 36 | + ! wnw "optimal" frequency for langevin (0. 0-> WN off) |
| 37 | + ! glew range shift freq. for GLE (0. 0-> GLE off) |
| 38 | + ! nchains number of NH chains to be used (0 -> NHC off) |
| 39 | + ! nhmts timestep subdivision for NHC integration |
| 40 | + ! nhw "optimal" frequency for NHC (Q=kt/nhw**2) |
| 41 | + ! * a note on thermostats: all the thermostats can be used at once, |
| 42 | + ! even if that makes not much sense. again, this is just a small |
| 43 | + ! test code |
| 44 | + ! * a note on units: mass is set to one, and k_B as well, so that |
| 45 | + ! at equilibrium <p^2>=omega^2 <q^2>=temp |
| 46 | + |
| 47 | + real*8 dum, dt, temp, nhw, wnw, glew |
| 48 | + integer argc, seed, nstep, stride, ndof, nchains, nhmts, tstride |
| 49 | + character *256 fname, wfile, prefix |
| 50 | + namelist /inp/ seed, wfile, dt, temp, nstep, stride, tstride, ndof, nchains, nhmts, nhw, wnw, glew |
| 51 | + |
| 52 | + real*8, allocatable :: q(:), p(:), f(:), wm(:,:) |
| 53 | + real*8 v, k, h ! yes, it's all we need! |
| 54 | + real*8 dt2 |
| 55 | +#ifdef USELIBS |
| 56 | + integer, allocatable :: ipiv(:) ! needed to invert matrix |
| 57 | +#endif |
| 58 | + integer irnd, istep, i, j |
| 59 | + |
| 60 | + ! reads command line |
| 61 | + argc = iargc() |
| 62 | + if ( argc .ne. 1 ) then |
| 63 | + write(6,*) '* Call me as: harmonic <input>' |
| 64 | + stop |
| 65 | + endif |
| 66 | + |
| 67 | + ! reads input file |
| 68 | + tstride=0 |
| 69 | + call getarg (1,fname) |
| 70 | + open(101,file=fname) |
| 71 | + read(101,inp) |
| 72 | + close (unit=101) |
| 73 | + irnd=-seed |
| 74 | + dt2=dt*0.5 |
| 75 | + kt=temp |
| 76 | + ndim=ndof |
| 77 | + allocate(q(ndof)) |
| 78 | + allocate(p(ndof)) |
| 79 | + allocate(f(ndof)) |
| 80 | + |
| 81 | + allocate(wtw(ndof,ndof)) |
| 82 | + allocate(wm(ndof,ndof)) |
| 83 | +#ifdef USELIBS |
| 84 | + allocate(ipiv(ndof)) |
| 85 | +#endif |
| 86 | + |
| 87 | + ! init random seed |
| 88 | + dum=ran2(irnd) |
| 89 | + |
| 90 | + ! reads square root of the hessian (v(q)=1/2 q^T W^T W q) |
| 91 | + open(101,file=wfile) |
| 92 | + read(101,*) wm |
| 93 | + close (unit=101) |
| 94 | + wtw=matmul(transpose(wm),wm) |
| 95 | + |
| 96 | + ! init momenta |
| 97 | + do i=1,ndof |
| 98 | + p(i)=rang(irnd)*sqrt(temp) |
| 99 | + enddo |
| 100 | + ! init coordinates (requires inverting wm ONLY WORKS IF WM IS SQRT(HESSIAN), NOT IF DONE WITH CHOLESKY!!!) |
| 101 | +#ifdef USELIBS |
| 102 | + do i=1,ndof |
| 103 | + q(i)=rang(irnd)*sqrt(temp) |
| 104 | + enddo |
| 105 | + call dgesv(ndof, 1, wm, ndof, ipiv, q, ndof, i) |
| 106 | +#else |
| 107 | + ! define your own inversion if you wish. |
| 108 | + ! I am lazy and just init to zero. |
| 109 | + q=0. |
| 110 | +#endif |
| 111 | + call force(q,f) |
| 112 | + |
| 113 | + !initializes thermostats |
| 114 | + if(nchains .gt. 0) call nhc_init(nchains, nhw, irnd) |
| 115 | + if(glew .gt. 0.d0) call gle_init(dt2,glew,irnd) |
| 116 | + if(wnw .gt. 0.d0) call wn_init(dt2,wnw) |
| 117 | + |
| 118 | + ! opens file for output |
| 119 | + if (tstride>0) open(102,file='traj-p.out') |
| 120 | + if (tstride>0) open(103,file='traj-q.out') |
| 121 | + open(104,file='statis.out') |
| 122 | + |
| 123 | + ! we are already at the main dynamics loop! |
| 124 | + do istep=1,nstep |
| 125 | + |
| 126 | + !calls the active thermostats |
| 127 | + if(nchains .gt. 0) call nhc_step(p,dt2,nhmts) |
| 128 | + if (wnw .gt. 0.d0 .or. glew .gt. 0.d0) then |
| 129 | + call kin(p, k) |
| 130 | + langham=langham+k |
| 131 | + if (wnw .gt. 0.d0) call wn_step(p,irnd) |
| 132 | + if (glew .gt. 0.d0) call gle_step(p,irnd) |
| 133 | + call kin(p, k) |
| 134 | + langham=langham-k |
| 135 | + endif |
| 136 | + |
| 137 | + ! hamiltonian step for dynamics |
| 138 | + p=p+f*dt2 |
| 139 | + q=q+p*dt |
| 140 | + call force(q,f) |
| 141 | + p=p+f*dt2 |
| 142 | + |
| 143 | + ! thermostats, second bit |
| 144 | + if (wnw .gt. 0.d0 .or. glew .gt. 0.d0) then |
| 145 | + call kin(p, k) |
| 146 | + langham=langham+k |
| 147 | + if (wnw .gt. 0.d0) call wn_step(p,irnd) |
| 148 | + if (glew .gt. 0.d0) call gle_step(p,irnd) |
| 149 | + call kin(p, k) |
| 150 | + langham=langham-k |
| 151 | + endif |
| 152 | + if(nchains .gt. 0) call nhc_step(p,dt2,nhmts) |
| 153 | + |
| 154 | + ! computes properties & outputs |
| 155 | + if (mod(istep,stride).eq.0) then |
| 156 | + call pot(q, v) |
| 157 | + call kin(p, k) |
| 158 | + h=k+v |
| 159 | + if (nchains.gt.0) then |
| 160 | + call nhc_cons() |
| 161 | + h=h+nhcham |
| 162 | + end if |
| 163 | + if (wnw .gt. 0.d0 .or. glew .gt. 0.d0) then |
| 164 | + h=h+langham |
| 165 | + end if |
| 166 | + if (tstride.gt.0 .and. mod(istep,tstride).eq.0) then |
| 167 | + write(102,'(1e17.8 )', advance='NO') istep*dt |
| 168 | + write(103,'(1e17.8 )', advance='NO') istep*dt |
| 169 | + do i=1,ndof |
| 170 | + write(102,'( 1e17.8 )', advance='NO') p(i) |
| 171 | + write(103,'( 1e17.8 )', advance='NO') q(i) |
| 172 | + enddo |
| 173 | + write(102,*) "" |
| 174 | + write(103,*) "" |
| 175 | + endif |
| 176 | + write(104,'(4e17.8)') istep*dt, v/ndof, k/ndof, h/ndof |
| 177 | + endif |
| 178 | + enddo |
| 179 | + |
| 180 | + if (tstride>0) close(102) |
| 181 | + if (tstride>0) close(103) |
| 182 | + close(104) |
| 183 | +end program harmonic |
0 commit comments