avm99963 | 45a8a46 | 2022-06-04 12:41:03 +0200 | [diff] [blame] | 1 | program main |
| 2 | use, intrinsic :: iso_c_binding, only: sp=>c_float, dp=>c_double |
| 3 | use iso_fortran_env |
| 4 | implicit none |
| 5 | integer*4 :: L |
Adrià Vilanova Martínez | ebb8779 | 2022-06-04 20:07:20 +0200 | [diff] [blame] | 6 | real*8 :: magne, energ, TEMP, E, DIFE, DELTA, M, SUMA, W(-8:8) |
| 7 | integer*4 :: I, J, IMC, MCTOT, MCINI, MCD, IPAS, N, SEED, NSEED, SEED0, pbc |
avm99963 | 45a8a46 | 2022-06-04 12:41:03 +0200 | [diff] [blame] | 8 | integer :: genrand_int31 |
| 9 | character(100) :: NOM |
| 10 | |
| 11 | integer :: SUMI |
| 12 | real(dp) :: SUME, SUME2, SUMM, SUMAM, SUMM2, VARE, VARM, CV, CHI |
| 13 | |
| 14 | ! This should be replaced by L but it doesn't work dynamically, so we're |
| 15 | ! setting 64 as the upper limit which should be higher than L. |
| 16 | integer*2 :: S(1:64, 1:64) |
| 17 | |
| 18 | ! Inicialitzem algunes variables sobre el problema |
| 19 | namelist /DADES/ L, NOM, TEMP, NSEED, SEED0, MCTOT, MCINI, MCD |
Adrià Vilanova Martínez | c102e96 | 2022-06-04 23:53:44 +0200 | [diff] [blame^] | 20 | read(nml = DADES, unit = 5) |
avm99963 | 45a8a46 | 2022-06-04 12:41:03 +0200 | [diff] [blame] | 21 | |
| 22 | N = L*L |
| 23 | |
avm99963 | 45a8a46 | 2022-06-04 12:41:03 +0200 | [diff] [blame] | 24 | ! Cache dels valors de l'exponencial |
| 25 | do I = 0, 8 |
| 26 | W(I) = (2**30 - 1 + 2**30)*exp(-float(I)/TEMP) |
| 27 | enddo |
| 28 | |
| 29 | SUMI = 0 |
| 30 | SUME = 0 |
| 31 | SUME2 = 0 |
| 32 | |
| 33 | SUMM = 0 |
| 34 | SUMAM = 0 |
| 35 | SUMM2 = 0 |
| 36 | |
| 37 | do SEED = SEED0, SEED0 + NSEED - 1 |
| 38 | print *, "Seed: ", SEED |
| 39 | |
| 40 | ! Inicialitzem la matriu d'spins aleatòriament |
Adrià Vilanova Martínez | ebb8779 | 2022-06-04 20:07:20 +0200 | [diff] [blame] | 41 | call generateSpinMatrix(S, L, SEED) |
avm99963 | 45a8a46 | 2022-06-04 12:41:03 +0200 | [diff] [blame] | 42 | |
Adrià Vilanova Martínez | ebb8779 | 2022-06-04 20:07:20 +0200 | [diff] [blame] | 43 | E = energ(S, L) |
| 44 | M = magne(S, L) |
avm99963 | 45a8a46 | 2022-06-04 12:41:03 +0200 | [diff] [blame] | 45 | |
| 46 | ! Iterem amb el mètode de Montecarlo |
| 47 | do IMC = 1, MCTOT |
| 48 | do IPAS = 1, N |
| 49 | ! LOS LOOPS HACEN COSAS |
Adrià Vilanova Martínez | ebb8779 | 2022-06-04 20:07:20 +0200 | [diff] [blame] | 50 | I = mod(genrand_int31(), L) + 1 |
| 51 | J = mod(genrand_int31(), L) + 1 |
| 52 | SUMA = S(PBC(I - 1, L), J) + & |
| 53 | S(PBC(I + 1, L), J) + & |
| 54 | S(I, PBC(J - 1, L)) + & |
| 55 | S(I, PBC(J + 1, L)) |
avm99963 | 45a8a46 | 2022-06-04 12:41:03 +0200 | [diff] [blame] | 56 | DIFE = 2*SUMA*S(I, J) |
| 57 | |
| 58 | if (DIFE > 0) then |
| 59 | DELTA = genrand_int31() |
| 60 | if (DELTA >= W(int(DIFE))) then |
| 61 | ! NO ho acceptem |
| 62 | cycle |
| 63 | endif |
| 64 | endif |
| 65 | |
| 66 | ! Sí ho acceptem: |
| 67 | S(I, J) = -S(I, J) |
| 68 | E = E + DIFE |
| 69 | M = M + 2*S(I, J) |
| 70 | enddo |
| 71 | |
Adrià Vilanova Martínez | c102e96 | 2022-06-04 23:53:44 +0200 | [diff] [blame^] | 72 | !if (mod(IMC, 10000) == 0) then |
| 73 | ! print *, "Iter:", IMC, "Energia:", E, "Magn:", INT(M) |
| 74 | !endif |
avm99963 | 45a8a46 | 2022-06-04 12:41:03 +0200 | [diff] [blame] | 75 | |
| 76 | if (IMC > MCINI .and. mod(IMC, MCD) == 0) then |
| 77 | SUMI = SUMI + 1 |
| 78 | |
| 79 | SUME = SUME + E |
| 80 | SUME2 = SUME2 + E*E |
| 81 | |
| 82 | SUMM = SUMM + M |
| 83 | SUMAM = SUMAM + abs(M) |
| 84 | SUMM2 = SUMM2 + M*M |
| 85 | endif |
| 86 | enddo |
| 87 | enddo |
| 88 | |
| 89 | ! Normalitzem els promitjos |
| 90 | SUME = SUME/SUMI |
| 91 | SUME2 = SUME2/SUMI |
| 92 | SUMM = SUMM/SUMI |
| 93 | SUMAM = SUMAM/SUMI |
| 94 | SUMM2 = SUMM2/SUMI |
| 95 | VARE = SUME2 - SUME**2 |
| 96 | VARM = SUMM2 - SUMM**2 |
| 97 | CV = (SUME2 - SUME**2)/real(N*TEMP**2) |
| 98 | CHI = (SUMM2 - SUMAM**2)/real(N*TEMP) |
| 99 | |
| 100 | ! Guardem a un fiter els promitjos |
| 101 | open(unit = 13, file = "data_out/" // trim(NOM) // ".res") |
Adrià Vilanova Martínez | c102e96 | 2022-06-04 23:53:44 +0200 | [diff] [blame^] | 102 | !write(13, *) "L, T, <E>, <E**2>, Var(E), <M>, <|M|>, <M**2>, Var(M), C_V, CHI" |
avm99963 | 45a8a46 | 2022-06-04 12:41:03 +0200 | [diff] [blame] | 103 | write(13, *) L, TEMP, SUME, SUME2, VARE, SUMM, SUMAM, SUMM2, VARM, CV, CHI |
| 104 | |
| 105 | ! Tanquem els fitxers de sortida |
avm99963 | 45a8a46 | 2022-06-04 12:41:03 +0200 | [diff] [blame] | 106 | close(13) |
| 107 | endprogram main |