Skip to content
Snippets Groups Projects
Commit 20d8b583 authored by Andrew E. Torda's avatar Andrew E. Torda
Browse files

Files made during testing are put in a temporary directory. Writing csv files...

Files made during testing are put in a temporary directory. Writing csv files is now an option, not compulsory.
parent c05dd5b4
No related branches found
No related tags found
No related merge requests found
...@@ -3,10 +3,23 @@ ...@@ -3,10 +3,23 @@
package ackwork package ackwork
import ( import (
"fmt"
"os"
"strings" "strings"
"testing" "testing"
) )
var tstTmpDir string
func init() {
var err error
if tstTmpDir, err = os.MkdirTemp(".", "test_tmp*"); err != nil {
fmt.Fprintln (os.Stderr, "Failed to create temporary directory", tstTmpDir)
os.Exit(1)
}
}
func TestErr(t *testing.T) { func TestErr(t *testing.T) {
rdrErr := strings.NewReader("\nrubbish") rdrErr := strings.NewReader("\nrubbish")
if err := realmain(rdrErr); err == nil { if err := realmain(rdrErr); err == nil {
...@@ -59,16 +72,20 @@ n_step 100000`, "phasetrans"}, ...@@ -59,16 +72,20 @@ n_step 100000`, "phasetrans"},
} }
// addTestPath makes some of the loops below a bit more readable
func addTestPath (s string) string {
return tstTmpDir + string (os.PathSeparator) + s
}
// add OutNames uses the basename field and makes output names for // add OutNames uses the basename field and makes output names for
// the value and x trajectory plot files and the csv file // the value and x trajectory plot files and the csv file
func addOutNames (s gentest) string { func addOutNames (s gentest) string {
// a :=
ret := s.params ret := s.params
for _, ss := range []string {"xPltName", "fOutName"} { for _, ss := range []string {"xPltName", "fOutName"} {
ret += "\n" + ss + " " + s.basename ret += "\n" + ss + " " + addTestPath(s.basename)
} }
ret += "\nfPltName" + " " + s.basename + "_fval" ret += "\nfPltName" + " " + addTestPath(s.basename) + "_fval"
return ret + "\n" return ret + "\n"
} }
func Test1(t *testing.T) { func Test1(t *testing.T) {
......
...@@ -45,6 +45,17 @@ type cprm struct { // parameters calculated from input ...@@ -45,6 +45,17 @@ type cprm struct { // parameters calculated from input
xplotme bool // Are we plotting X trajectories xplotme bool // Are we plotting X trajectories
} }
// isNotSane checks for obviously silly parameters
func isNotSane (mcPrm *mcPrm) error {
if mcPrm.fOutName == "" && mcPrm.fPltName == "" && mcPrm.xPltName == "" {
return fmt.Errorf ("All output files are empty. You would not see anything")
}
if mcPrm.iniTmp < 0 {
return fmt.Errorf ("negative initial temperature %g", mcPrm.iniTmp)
}
return nil
}
// setupRun does things like get the cooling rate, seed the random numbers. // setupRun does things like get the cooling rate, seed the random numbers.
func setupRun(mcPrm *mcPrm, cprm *cprm) error { func setupRun(mcPrm *mcPrm, cprm *cprm) error {
const finalTempErr = "Final temp %g higher than initial temp %g" const finalTempErr = "Final temp %g higher than initial temp %g"
...@@ -69,13 +80,14 @@ func setupRun(mcPrm *mcPrm, cprm *cprm) error { ...@@ -69,13 +80,14 @@ func setupRun(mcPrm *mcPrm, cprm *cprm) error {
coolrate *= math.Log(fnlTmp / iniTmp) coolrate *= math.Log(fnlTmp / iniTmp)
cprm.coolMult = math.Exp(-coolrate) cprm.coolMult = math.Exp(-coolrate)
} }
if mcPrm.fOutName != "" {
if mcPrm.fOutName, err = setSuffix(mcPrm.fOutName, ".csv"); err != nil { if mcPrm.fOutName, err = setSuffix(mcPrm.fOutName, ".csv"); err != nil {
return err return err
} }
if cprm.fOutRaw, err = os.Create(mcPrm.fOutName); err != nil { if cprm.fOutRaw, err = os.Create(mcPrm.fOutName); err != nil {
return err return err
} }
}
cprm.fOut = bufio.NewWriter(cprm.fOutRaw) cprm.fOut = bufio.NewWriter(cprm.fOutRaw)
if mcPrm.fPltName != "" { if mcPrm.fPltName != "" {
var err error var err error
...@@ -100,6 +112,7 @@ func setupRun(mcPrm *mcPrm, cprm *cprm) error { ...@@ -100,6 +112,7 @@ func setupRun(mcPrm *mcPrm, cprm *cprm) error {
n_dim := len(mcPrm.xIni) n_dim := len(mcPrm.xIni)
cprm.plotXtrj = make([]float32, 0, int(n_alloc)*n_dim) cprm.plotXtrj = make([]float32, 0, int(n_alloc)*n_dim)
} }
if err != isNotSane(mcPrm) { return err }
return nil return nil
} }
...@@ -147,8 +160,10 @@ func saveStep(cprm *cprm, n uint32, tmprtr float64, x []float32, fTrial float64) ...@@ -147,8 +160,10 @@ func saveStep(cprm *cprm, n uint32, tmprtr float64, x []float32, fTrial float64)
if cprm.xplotme { // The trajectory if cprm.xplotme { // The trajectory
cprm.plotXtrj = append(cprm.plotXtrj, x...) cprm.plotXtrj = append(cprm.plotXtrj, x...)
} }
if cprm.fOut != nil {
printfVal(cprm.fOut, x, n, tmprtr, fTrial) printfVal(cprm.fOut, x, n, tmprtr, fTrial)
} }
}
// nRunAdj will try to adjust the step size, given our history of accept/reject. // nRunAdj will try to adjust the step size, given our history of accept/reject.
func nRunAdj(mcPrm *mcPrm, nstep uint32, runAcc float64, xDlta float32) float32 { func nRunAdj(mcPrm *mcPrm, nstep uint32, runAcc float64, xDlta float32) float32 {
......
...@@ -27,7 +27,7 @@ var cmdDflt = []struct { ...@@ -27,7 +27,7 @@ var cmdDflt = []struct {
{"x_ini", "3,4,5"}, {"x_ini", "3,4,5"},
{"x_delta", "0.5"}, {"x_delta", "0.5"},
{"seed", "1637"}, {"seed", "1637"},
{"foutname", "mcrun"}, {"foutname", ""},
{"fpltname", ""}, // empty means no plots of function {"fpltname", ""}, // empty means no plots of function
{"xpltname", ""}, {"xpltname", ""},
{"verbose", ""}, {"verbose", ""},
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment