Skip to content
Snippets Groups Projects
Select Git revision
  • 3cb50a6ee5b3be8955b6d83eaaa180a0b5fd8edd
  • master default protected
  • devel
  • adaptive_step_size
4 results

ackwork_test.go

Blame
  • user avatar
    Andrew E. Torda authored
    3cb50a6e
    History
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    ackwork_test.go 3.84 KiB
    // Aug 2021
    
    package ackwork
    
    import (
    	"strings"
    	"testing"
    )
    
    func TestErr(t *testing.T) {
    	rdrErr := strings.NewReader("\nrubbish")
    	if err := realmain(rdrErr); err == nil {
    		t.Fatal("line with rubbish should provoke error")
    	}
    }
    
    var sTypo = `
    ini_temp = 1
    final_temp 0
    x_ini 0.8,0.8,0.8
    x_delta, "0.4"
    n_step = 100000
    fOutNamen testReal`
    
    func TestTypo(t *testing.T) { // Make sure a typo in input does provoke an error
    	if err := realmain(strings.NewReader(sTypo)); err == nil {
    		t.Fatal("line with typo should provoke error\n", err, "\nInput", sTypo)
    	}
    }
    
    var s1d = `
    ini_temp 1
    final_temp 0.05
    x_ini 15
    n_step 10000
    fOutName testanneal1d`
    
    var s16d = `
    ini_temp 0.02
    final_temp 0.001
    x_ini 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
    n_step 1000000
    x_delta 1
    fPltName anneal16d.svg
    xPltName anneal16dtrajplt
    fOutName anneal16d`
    
    var sCold = `
    ini_temp 1e-10
    final_temp 1e-12
    x_ini 1,1,1
    n_step 10000
    fOutName testcold`
    
    var sHot = `
    ini_temp 1
    final_temp 1
    x_ini 1,1,1
    n_step 10000
    fOutName testhot`
    
    var sRealAnneal = `
    ini_temp 0.5
    final_temp 0.05
    x_ini 7,1,7
    n_step 100000
    fOutName ""`
    
    // These values seem to work well. There is something of a phase
    // transition near temperature 0.09. Good values for an optimisation
    // seem to be 0.17 and 0.01 (initial and final temperatures), but this
    // needs a few steps (500000).
    
    // constant temp..
    // 0.75 jump around and nicely visits all locations
    // 0.1 jumps around, acceptance is too high and really uncontrolled.
    // 0.09375 visit 0, 1, 2, 3, 4, 5, 6, .. Can fully explore local minima and sometimes get to next
    // 0.0875 visits 6.7, 6.8, .. 7.2
    // 0.075 only see points at 6.8, 6.9, 7.0, 7.1
    // 0.05 only see points at 6.8, 6.9, 7.0, 7.1
    var sPhaseTrans = `
    ini_temp 0.18
    final_temp 0.01
    x_ini 7,1,7
    n_step 100000
    fOutName testphase`
    
    var csv_test = []string{
    	s1d, sHot, sCold, sRealAnneal, sPhaseTrans, s16d}
    
    func TestCsv(t *testing.T) {
    	for _, s := range csv_test {
    		if err := realmain(strings.NewReader(s)); err != nil {
    			t.Fatal("csv failed with\n", err, "\nInput:", s)
    		}
    	}
    }
    
    var s9dplot = `
    ini_temp 0.0925
    final_temp 0.09
    x_ini 15,10,11,12,14,18,-12,-8,-9
    n_step 50000
    x_delta 0.2
    fOutName test9d
    fPltName testplot.svg
    xPltName testrajplt`
    
    var s9bdplot = `
    ini_temp 0.0925
    final_temp 0.05
    x_ini 15,10,11,12,14,0,-12,-8,-9
    n_step 50000
    x_delta 0.2
    fOutName test9db
    fPltName test9dbf
    xPltName test9dbtraj`
    
    var plotTest = []string{
    	s9dplot, s9bdplot,
    }
    
    func TestPlot(t *testing.T) {
    	for _, s := range plotTest {
    		if err := realmain(strings.NewReader(s)); err != nil {
    			t.Fatal("plot test failed with\n", err, "\nInput", s)
    		}
    	}
    }
    
    func TestSetSuffix(t *testing.T) {
    	var tdata = []struct {
    		in, suffix, want string
    	}{
    		{"boo.", ".csv", "boo.csv"},
    		{"boo", "csv", "boo.csv"},
    		{"boo.", "csv", "boo.csv"},
    		{"boo", ".csv", "boo.csv"},
    		{"boo.csv", ".csv", "boo.csv"},
    		{"/boo/blah/boo", ".csv", "/boo/blah/boo.csv"},
    		{"/a/b/c.svg", ".png", "/a/b/c.png"},
    		{"boo.", "", "boo"},
    		{"boo", "", "boo"},
    	}
    
    	for _, s := range tdata {
    		if tmp, err := SetSuffix(s.in, s.suffix); err != nil {
    			t.Fatal("setSuffix error handling", s)
    		} else {
    			if tmp != s.want {
    				t.Fatal("Wanted", s.want, "got", tmp, "working on", s)
    			}
    		}
    	}
    }
    
    func TestRemoveQuotes(t *testing.T) {
    	var tdata = []struct {
    		in, out string
    	} {
    		{"hello", "hello"},
    		{"", ""},
    		{`"`,  ""},
    		{`""`, ""},
    		{`"""`, ""},
    		{`""a"`, "a"},
    		{`""aa"`, "aa"},
    		{`""a""`, "a"},
    		{`"a"`, "a"},
    		{`a"`, "a"},
    		{`a""`, "a"},
    	}
    	for _, ss := range tdata {
    		if out := RemoveQuotes (ss.in); out != ss.out {
    			t.Fatalf ("Wanted <%s>, got <%s>. Input was <%s>", ss.out, out, ss.in)
    		}
    	}
    }
    
    // Check that errors are generated
    func TestSetSuffixErr(t *testing.T) {
    	var tdata = []struct{ in, suffix string }{
    		{"", ""},
    		{".", ""}}
    	for _, s := range tdata {
    		if _, err := SetSuffix(s.in, s.suffix); err == nil {
    			t.Fatal("setSuffix should return error on", s)
    		}
    	}
    }