Fenòmens p4: allow to save final configuration

Change-Id: I9f14110403a8313357ab04df624373789de47233
diff --git a/quad10/fenomens/lab/p4/MC-2.f90 b/quad10/fenomens/lab/p4/MC-2.f90
index 8398997..554890d 100644
--- a/quad10/fenomens/lab/p4/MC-2.f90
+++ b/quad10/fenomens/lab/p4/MC-2.f90
@@ -7,6 +7,7 @@
   integer*4 :: I, J, IMC, MCTOT, MCINI, MCD, IPAS, N, SEED, NSEED, SEED0, pbc
   integer :: genrand_int31
   character(100) :: NOM
+  logical :: SAVEFINALCONF
 
   integer :: SUMI
   real(dp) :: SUME, SUME2, SUMM, SUMAM, SUMM2, VARE, VARM, CV, CHI
@@ -16,7 +17,7 @@
   integer*2 :: S(1:64, 1:64)
 
   ! Inicialitzem algunes variables sobre el problema
-  namelist /DADES/ L, NOM, TEMP, NSEED, SEED0, MCTOT, MCINI, MCD
+  namelist /DADES/ L, NOM, TEMP, NSEED, SEED0, MCTOT, MCINI, MCD, SAVEFINALCONF
   read(nml = DADES, unit = 5)
 
   N = L*L
@@ -95,9 +96,13 @@
 
   ! Guardem a un fiter els promitjos
   open(unit = 13, file = "data_out/" // trim(NOM) // ".res")
-  !write(13, *) "L,     T,     <E>,     <E**2>,     Var(E),     <M>,     <|M|>,     <M**2>,     Var(M),     C_V,     CHI"
+  ! Valors: L, T, <E>, <E**2>, Var(E), <M>, <|M|>, <M**2>, Var(M), C_V, CHI
   write(13, *) L, TEMP, SUME, SUME2, VARE, SUMM, SUMAM, SUMM2, VARM, CV, CHI
 
+  if (SAVEFINALCONF) then
+    call writeConfig(S, L, "data_out/" // trim(NOM) // ".conf")
+  endif
+
   ! Tanquem els fitxers de sortida
   close(13)
 endprogram main
diff --git a/quad10/fenomens/lab/p4/Makefile b/quad10/fenomens/lab/p4/Makefile
index e38dc46..21e5cbf 100644
--- a/quad10/fenomens/lab/p4/Makefile
+++ b/quad10/fenomens/lab/p4/Makefile
@@ -5,9 +5,9 @@
 all: MC-2.o
 make_out_folder:
 	mkdir -p out data_out
-MC-2.o: make_out_folder mt19937ar.o generateSpinMatrix.o magne.o energ.o pbc.o MC-2.f90
+MC-2.o: make_out_folder mt19937ar.o generateSpinMatrix.o magne.o energ.o pbc.o writeconfig.o MC-2.f90
 	$(FC) $(FC_FLAGS) -c MC-2.f90
-	$(FC) $(FC_FLAGS) MC-2.o generateSpinMatrix.o mt19937ar.o magne.o energ.o pbc.o -o out/mc2
+	$(FC) $(FC_FLAGS) MC-2.o generateSpinMatrix.o mt19937ar.o magne.o energ.o pbc.o writeconfig.o -o out/mc2
 mt19937ar.o: mt19937ar.f
 	$(FC) $(FC_FLAGS) -c mt19937ar.f
 generateSpinMatrix.o: generateSpinMatrix.f90
@@ -18,6 +18,8 @@
 	$(FC) $(FC_FLAGS) -c energ.f90
 pbc.o: pbc.f90
 	$(FC) $(FC_FLAGS) -c pbc.f90
+writeconfig.o: writeconfig.f90
+	$(FC) $(FC_FLAGS) -c writeconfig.f90
 
 clean:
 	rm -rf out *.o
diff --git a/quad10/fenomens/lab/p4/run.bash b/quad10/fenomens/lab/p4/run.bash
index 96122de..2c95bdd 100755
--- a/quad10/fenomens/lab/p4/run.bash
+++ b/quad10/fenomens/lab/p4/run.bash
@@ -5,7 +5,7 @@
 
   Usage: $progname T L [outFilePrefix [--help --skipIfComputed
              --nSeeds NSEEDS --mcTot MCTOT --mcIni MCINI --mcD MCD
-             --initialSeed INITIALSEED]]
+             --initialSeed INITIALSEED --saveFinalConf]]
 
   the output file will be saved at "data_out/{{outFilePrefix}}"
 
@@ -22,6 +22,8 @@
     -d, --mcD             the program will measure the physical
                           properties when iteration % MCD == 0.
     --initialSeed         initial seed for the Monte Carlo algorithm.
+    --saveFinalConf       save a file with the configuration which
+                          belongs to the last Montecarlo iteration.
 END
 }
 
@@ -35,7 +37,7 @@
 defaultOutFilePrefix="L$L-T$T"
 outFilePrefix="${3:-$defaultOutFilePrefix}"
 
-opts=$(getopt -l "help,skipIfComputed,nSeeds:,mcTot:,mcIni:,mcD:,initialSeed:" -o "hsn:m:i:d:" -n "$progname" -- "${@:4}")
+opts=$(getopt -l "help,skipIfComputed,nSeeds:,mcTot:,mcIni:,mcD:,initialSeed:,saveFinalConf" -o "hsn:m:i:d:" -n "$progname" -- "${@:4}")
 eval set -- "$opts"
 
 skipIfComputed=false
@@ -44,6 +46,7 @@
 mcIni=2000
 mcD=20
 initialSeed=117654
+saveFinalConf=F
 
 while true; do
   case "$1" in
@@ -75,6 +78,10 @@
       initialSeed="$2"
       shift 2
       ;;
+    --saveFinalConf)
+      saveFinalConf=T
+      shift
+      ;;
     *) break ;;
   esac
 done
@@ -96,5 +103,6 @@
 MCTOT=$mcTot,
 MCINI=$mcIni,
 MCD=$mcD
+SAVEFINALCONF=$saveFinalConf
 &END
 EOF
diff --git a/quad10/fenomens/lab/p4/writeconfig.f90 b/quad10/fenomens/lab/p4/writeconfig.f90
new file mode 100644
index 0000000..30de3c9
--- /dev/null
+++ b/quad10/fenomens/lab/p4/writeconfig.f90
@@ -0,0 +1,19 @@
+subroutine writeConfig(S, L, NOM)
+  implicit none
+  integer*4, intent(in) :: L
+  integer*2, intent(out) :: S(1:L,1:L)
+  integer*4 :: i, j
+  character(200) :: NOM
+
+  open(17, file = trim(NOM))
+
+  do i = 1, L
+    do j = 1, L
+      if (S(i, j) == 1) then
+        write(17, *) i, j
+      endif
+    enddo
+  enddo
+
+  close(17)
+end subroutine writeConfig