From c7f23622a0879df30bcf2ad2fb47d55928d53ed4 Mon Sep 17 00:00:00 2001
From: "Andrew E. Torda" <torda@zbh.uni-hamburg.de>
Date: Tue, 4 Jan 2022 17:12:35 +0100
Subject: [PATCH] Simplifying main loop - moved print statements to their own
 function.

---
 ackwork/dorun.go | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)

diff --git a/ackwork/dorun.go b/ackwork/dorun.go
index 4e4b4d4..511fd73 100644
--- a/ackwork/dorun.go
+++ b/ackwork/dorun.go
@@ -134,6 +134,16 @@ func nRunAdj(mcPrm *mcPrm, runAcc float64, xDlta float32) float32 {
 	return xDlta
 }
 
+// printfVal is just the loop to print out the function value and coordinates
+// probably for later plotting.
+func printfVal (fOut io.Writer, x []float32, n uint32, tmprtr float64, fOld float64) {
+	fmt.Fprintf(fOut, "%d,%.4g,%.5g", n, tmprtr, fOld)
+	for _, xx := range x {
+		fmt.Fprintf(fOut, ",%.2g", xx)
+	}
+	fmt.Fprintln(fOut)
+}
+
 // doRun does a Monte Carlo run. Although single precision is fine for the
 // coordinates and function, we use double precision for the temperature.
 func doRun(mcPrm *mcPrm) error {
@@ -166,17 +176,13 @@ func doRun(mcPrm *mcPrm) error {
 		var acc bool
 		nout--
 		if nout == 0 { // Do we want to print out results on this step ?
-			fmt.Fprintf(cprm.fOut, "%d,%.4g,%.5g", n, tmprtr, fOld)
-			for _, xx := range x {
-				fmt.Fprintf(cprm.fOut, ",%.2g", xx)
-			}
-			fmt.Fprintln(cprm.fOut)
+			printfVal (cprm.fOut, x, n, tmprtr, fOld)
 			nout = cprm.nEvery
 		}
 		nRunAcc--
 		if nRunAcc == 0 { // Do we want to try adjusting the step size ?
-			xDlta = nRunAdj(mcPrm, runAcc, xDlta) // mucking about here
-			nRunAcc = nRunAccAdj                  // Reset the counter
+			xDlta = nRunAdj(mcPrm, runAcc, xDlta)
+			nRunAcc = nRunAccAdj // Reset the counter
 		}
 		newx(x, xT, cprm.rand, xDlta)
 		fTrial := ackley.Ackley(xT)
-- 
GitLab