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

Got rid of no-op Close function and the complicated types. Now use a type assertion.

parent 20a32ab9
No related branches found
No related tags found
No related merge requests found
...@@ -12,7 +12,7 @@ import ( ...@@ -12,7 +12,7 @@ import (
"sync" "sync"
"example.com/ackley_mc/ackley" "example.com/ackley_mc/ackley"
"example.com/ackley_mc/ui" // "example.com/ackley_mc/ui"
) )
const ( const (
...@@ -33,9 +33,9 @@ func getSeed() int64 { ...@@ -33,9 +33,9 @@ func getSeed() int64 {
return r return r
} }
type withCloser struct{ io.Writer } // We have to promote him to have a Close() type withBytes interface {
func (withCloser) Close() error { return nil } Bytes() []byte
func addClose(b io.Writer) withCloser { return withCloser{b} } }
type cprm struct { // parameters calculated from input type cprm struct { // parameters calculated from input
rand *rand.Rand rand *rand.Rand
...@@ -48,8 +48,7 @@ type cprm struct { // parameters calculated from input ...@@ -48,8 +48,7 @@ type cprm struct { // parameters calculated from input
plotTmprtr []float64 // Temperature values for plotting plotTmprtr []float64 // Temperature values for plotting
plotXtrj []float32 // for plotting trajectories plotXtrj []float32 // for plotting trajectories
coolme bool // Cooling ? or just constant temperature ? coolme bool // Cooling ? or just constant temperature ?
fplotWrt io.WriteCloser // Where to write the plot of function values fplotWrt io.Writer // Where to write the plot of function values
fplotbuf *bytes.Buffer // The buffer possibly behind fplotWrt
xplotme bool // Are we plotting X trajectories xplotme bool // Are we plotting X trajectories
} }
...@@ -110,8 +109,7 @@ func setupRun(mcPrm *mcPrm, cprm *cprm) error { ...@@ -110,8 +109,7 @@ func setupRun(mcPrm *mcPrm, cprm *cprm) error {
} }
} else { // Must be writing to a screen } else { // Must be writing to a screen
var w bytes.Buffer // could be fancy, preallocate and use bytes.Newbuffer var w bytes.Buffer // could be fancy, preallocate and use bytes.Newbuffer
cprm.fplotWrt = addClose(&w) // Fix this, effectively two copies cprm.fplotWrt = &w
cprm.fplotbuf = &w // of the pointer
} }
n_alloc := mcPrm.nStep / 5 // About a fifth of the number of steps n_alloc := mcPrm.nStep / 5 // About a fifth of the number of steps
cprm.plotnstp = make([]float64, 0, n_alloc) cprm.plotnstp = make([]float64, 0, n_alloc)
...@@ -199,7 +197,7 @@ func nRunAdj(mcPrm *mcPrm, nstep uint32, runAcc float64, xDlta float32) float32 ...@@ -199,7 +197,7 @@ func nRunAdj(mcPrm *mcPrm, nstep uint32, runAcc float64, xDlta float32) float32
} }
return xDlta return xDlta
} }
func breaker() {} func breaker(...interface{}) {}
// doRun does a Monte Carlo run. Although single precision is fine for the // doRun does a Monte Carlo run. Although single precision is fine for the
// coordinates and function, we use double precision for the temperature. // coordinates and function, we use double precision for the temperature.
...@@ -215,8 +213,8 @@ func doRun(mcPrm *mcPrm) error { ...@@ -215,8 +213,8 @@ func doRun(mcPrm *mcPrm) error {
defer cprm.fOut.Flush() defer cprm.fOut.Flush()
} }
if cprm.fplotWrt != nil { if c, ok := cprm.fplotWrt.(io.Closer); ok == true { // will also have to do for X values
defer cprm.fplotWrt.Close() defer c.Close()
} }
var nAcc int // Counter, number of accepted moves var nAcc int // Counter, number of accepted moves
...@@ -273,9 +271,8 @@ func doRun(mcPrm *mcPrm) error { ...@@ -273,9 +271,8 @@ func doRun(mcPrm *mcPrm) error {
return err return err
} }
if cprm.fplotbuf != nil { if bBuf, ok := cprm.fplotWrt.(withBytes); ok == true {
fmt.Println("len", cprm.fplotbuf.Len()) fmt.Println("bBuf for plotting is this big", len(bBuf.Bytes()))
ui.Scrnplt(cprm.fplotbuf.Bytes())
} }
if err := plotxWrt(&cprm, mcPrm.xPltName, len(mcPrm.xIni)); err != nil { if err := plotxWrt(&cprm, mcPrm.xPltName, len(mcPrm.xIni)); err != nil {
return err return err
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment