blob: 48c6a11d7121298e751bf572f75a602619cb5988 [file] [log] [blame]
avm9996345a8a462022-06-04 12:41:03 +02001program 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ínezebb87792022-06-04 20:07:20 +02006 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
avm9996345a8a462022-06-04 12:41:03 +02008 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
20 open(10, file="mc2.dat")
21 read(10, DADES)
22 close(10)
23
24 N = L*L
25
26 ! Obrim el fitxer on escriurem els resultats a cada iteració
27 open(unit = 12, file = "data_out/" // trim(NOM) // ".out")
28
29 ! Cache dels valors de l'exponencial
30 do I = 0, 8
31 W(I) = (2**30 - 1 + 2**30)*exp(-float(I)/TEMP)
32 enddo
33
34 SUMI = 0
35 SUME = 0
36 SUME2 = 0
37
38 SUMM = 0
39 SUMAM = 0
40 SUMM2 = 0
41
42 do SEED = SEED0, SEED0 + NSEED - 1
43 print *, "Seed: ", SEED
44
45 ! Inicialitzem la matriu d'spins aleatòriament
Adrià Vilanova Martínezebb87792022-06-04 20:07:20 +020046 call generateSpinMatrix(S, L, SEED)
avm9996345a8a462022-06-04 12:41:03 +020047
Adrià Vilanova Martínezebb87792022-06-04 20:07:20 +020048 E = energ(S, L)
49 M = magne(S, L)
avm9996345a8a462022-06-04 12:41:03 +020050 !write (12, *) "0", E, M
51
52 ! Iterem amb el mètode de Montecarlo
53 do IMC = 1, MCTOT
54 do IPAS = 1, N
55 ! LOS LOOPS HACEN COSAS
Adrià Vilanova Martínezebb87792022-06-04 20:07:20 +020056 I = mod(genrand_int31(), L) + 1
57 J = mod(genrand_int31(), L) + 1
58 SUMA = S(PBC(I - 1, L), J) + &
59 S(PBC(I + 1, L), J) + &
60 S(I, PBC(J - 1, L)) + &
61 S(I, PBC(J + 1, L))
avm9996345a8a462022-06-04 12:41:03 +020062 DIFE = 2*SUMA*S(I, J)
63
64 if (DIFE > 0) then
65 DELTA = genrand_int31()
66 if (DELTA >= W(int(DIFE))) then
67 ! NO ho acceptem
68 cycle
69 endif
70 endif
71
72 ! Sí ho acceptem:
73 S(I, J) = -S(I, J)
74 E = E + DIFE
75 M = M + 2*S(I, J)
76 enddo
77
78 if (mod(IMC, 10000) == 0) then
79 print *, "Iter:", IMC, "Energia:", E, "Magn:", INT(M)
80 endif
81 !write (12, *) IMC, E, M
82
83 if (IMC > MCINI .and. mod(IMC, MCD) == 0) then
84 SUMI = SUMI + 1
85
86 SUME = SUME + E
87 SUME2 = SUME2 + E*E
88
89 SUMM = SUMM + M
90 SUMAM = SUMAM + abs(M)
91 SUMM2 = SUMM2 + M*M
92 endif
93 enddo
94 enddo
95
96 ! Normalitzem els promitjos
97 SUME = SUME/SUMI
98 SUME2 = SUME2/SUMI
99 SUMM = SUMM/SUMI
100 SUMAM = SUMAM/SUMI
101 SUMM2 = SUMM2/SUMI
102 VARE = SUME2 - SUME**2
103 VARM = SUMM2 - SUMM**2
104 CV = (SUME2 - SUME**2)/real(N*TEMP**2)
105 CHI = (SUMM2 - SUMAM**2)/real(N*TEMP)
106
107 ! Guardem a un fiter els promitjos
108 open(unit = 13, file = "data_out/" // trim(NOM) // ".res")
109 !write(13, *) "L, T, <E>, <E**2>, Var(E), <M>, <M**2>, Var(M), C_V, CHI"
110 write(13, *) L, TEMP, SUME, SUME2, VARE, SUMM, SUMAM, SUMM2, VARM, CV, CHI
111
112 ! Tanquem els fitxers de sortida
113 close(12)
114 close(13)
115endprogram main