blob: 9b8e78e023eb08f1090767423f030a081e6e0aaf [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
Adrià Vilanova Martínezc102e962022-06-04 23:53:44 +020020 read(nml = DADES, unit = 5)
avm9996345a8a462022-06-04 12:41:03 +020021
22 N = L*L
23
avm9996345a8a462022-06-04 12:41:03 +020024 ! 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ínezebb87792022-06-04 20:07:20 +020041 call generateSpinMatrix(S, L, SEED)
avm9996345a8a462022-06-04 12:41:03 +020042
Adrià Vilanova Martínezebb87792022-06-04 20:07:20 +020043 E = energ(S, L)
44 M = magne(S, L)
avm9996345a8a462022-06-04 12:41:03 +020045
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ínezebb87792022-06-04 20:07:20 +020050 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))
avm9996345a8a462022-06-04 12:41:03 +020056 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ínezc102e962022-06-04 23:53:44 +020072 !if (mod(IMC, 10000) == 0) then
73 ! print *, "Iter:", IMC, "Energia:", E, "Magn:", INT(M)
74 !endif
avm9996345a8a462022-06-04 12:41:03 +020075
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ínezc102e962022-06-04 23:53:44 +0200102 !write(13, *) "L, T, <E>, <E**2>, Var(E), <M>, <|M|>, <M**2>, Var(M), C_V, CHI"
avm9996345a8a462022-06-04 12:41:03 +0200103 write(13, *) L, TEMP, SUME, SUME2, VARE, SUMM, SUMAM, SUMM2, VARM, CV, CHI
104
105 ! Tanquem els fitxers de sortida
avm9996345a8a462022-06-04 12:41:03 +0200106 close(13)
107endprogram main