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

Swapped to buffered i/o for the csv files. cprm no longer holds the open file...

Swapped to buffered i/o for the csv files. cprm no longer holds the open file handles for plotting. The are opened and closed as necessary. Trying out a different tactic for move sets. Just move one dimension at a time.
parent c619eb28
No related branches found
No related tags found
No related merge requests found
In dorun.go
* remove the writeclosers from the cprm structure. Just open and close the file when we have to plot or write.
* make the csv files optional
* If the last step was not accepted, we do not write a value out.
* is there some way to clean up, using the go build tools ?
......@@ -120,7 +120,7 @@ xPltName testrajplt`
var s9bdplot = `
ini_temp 0.0925
final_temp 0.09
final_temp 0.05
x_ini 15,10,11,12,14,0,-12,-8,-9
n_step 50000
x_delta 0.2
......
......@@ -2,6 +2,7 @@
package ackwork
import (
"bufio"
"fmt"
"io"
"math"
......@@ -36,9 +37,8 @@ type cprm struct { // parameters calculated from input
coolme bool // Are we cooling or just doing constant temperature ?
fplotme bool // Are we making output for plotting func values
xplotme bool // Are we plotting X trajectories
fOut io.WriteCloser // csv file for plotting
fPlt io.WriteCloser // for function values
xPlt io.WriteCloser // for x trajectories
fOutRaw io.WriteCloser // the raw file for csv files
fOut *bufio.Writer // buffered version of fOutRaw
plotnstp []float64 // Why float 64 ? Because the plotting libraries
plotf []float64 // Function values for plotting
plotTmprtr []float64 // Temperature values for plotting
......@@ -73,17 +73,15 @@ func setupRun(mcPrm *mcPrm, cprm *cprm) error {
if mcPrm.fOutName, err = setSuffix(mcPrm.fOutName, ".csv"); err != nil {
return err
}
if cprm.fOut, err = os.Create(mcPrm.fOutName); err != nil {
if cprm.fOutRaw, err = os.Create(mcPrm.fOutName); err != nil {
return err
}
cprm.fOut = bufio.NewWriter(cprm.fOutRaw)
if mcPrm.fPltName != "" {
var err error
if mcPrm.fPltName, err = setSuffix(mcPrm.fPltName, ".png"); err != nil {
return fmt.Errorf("plot filename: %w", err)
}
if cprm.fPlt, err = os.Create(mcPrm.fPltName); err != nil {
return err
}
cprm.fplotme = true
n_alloc := mcPrm.nStep / 5 // About a fifth of the number of steps
cprm.plotnstp = make([]float64, 0, n_alloc)
......@@ -97,21 +95,17 @@ func setupRun(mcPrm *mcPrm, cprm *cprm) error {
if mcPrm.xPltName, err = setSuffix(mcPrm.xPltName, ".png"); err != nil {
return fmt.Errorf("plot filename: %w", err)
}
if cprm.xPlt, err = os.Create(mcPrm.xPltName); err != nil {
return err
}
cprm.xplotme = true
n_alloc := mcPrm.nStep / 5
n_dim := len(mcPrm.xIni)
cprm.plotXtrj = make([]float32, 0, int(n_alloc)*n_dim)
// Should check that cprm.plotx is allocated for the step number
}
return nil
}
// newx gets a candidate x slice. We have n dimensions, so a candidate
// move is a slice of length n.
func newx(xold []float32, xT []float32, rand *rand.Rand, xDlta float32) {
func nnewx(xold []float32, xT []float32, rand *rand.Rand, xDlta float32) {
for i, x := range xold {
t := 2.*rand.Float32() - 1
t *= xDlta
......@@ -119,6 +113,17 @@ func newx(xold []float32, xT []float32, rand *rand.Rand, xDlta float32) {
}
}
// newx will move just one coordinate at a time
func newx(xold []float32, xT []float32, rand *rand.Rand, xDlta float32) {
var iDim int
if (len(xold)) > 1 {
iDim = rand.Intn(len(xold)) // pick one dimension to change
}
t := 2.*rand.Float32() - 1
t *= xDlta
xT[iDim] = xold[iDim] + t
}
// 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) {
......@@ -173,14 +178,10 @@ func doRun(mcPrm *mcPrm) error {
return err
}
if cprm.fOut != nil {
defer cprm.fOut.Close()
}
if cprm.fPlt != nil {
defer cprm.fPlt.Close()
}
if cprm.xPlt != nil {
defer cprm.xPlt.Close()
defer cprm.fOut.Flush()
defer cprm.fOutRaw.Close()
}
var nAcc int // Counter of number of accepted moves
x := make([]float32, len(mcPrm.xIni)) // current position
xT := make([]float32, len(mcPrm.xIni)) // trial position
......@@ -226,6 +227,7 @@ func doRun(mcPrm *mcPrm) error {
saveStep(&cprm, n+1, tmprtr, x, fTrial)
} else { // update the running estimate of acceptance
runAcc = runMult * runAcc
// saveStep(&cprm, n+1, tmprtr, x, fTrial) // debugging delete me
}
if cprm.coolme {
tmprtr *= cprm.coolMult
......@@ -239,11 +241,11 @@ func doRun(mcPrm *mcPrm) error {
if fTrial != fOld { // last move was not accepted, so we have to add last step
saveStep(&cprm, mcPrm.nStep, tmprtr, x, fOld)
}
err := plotfWrt(cprm.plotnstp, cprm.plotf, cprm.plotTmprtr, cprm.fPlt, cprm.fplotme)
err := plotfWrt(cprm.plotnstp, cprm.plotf, cprm.plotTmprtr, mcPrm.fPltName, cprm.fplotme)
if err != nil {
return err
}
err = plotxWrt(&cprm, len(mcPrm.xIni))
err = plotxWrt(&cprm, mcPrm.xPltName, len(mcPrm.xIni))
fmt.Println("n accepted:", nAcc, "of", mcPrm.nStep+1)
return err
......
......@@ -14,7 +14,6 @@ package ackwork
import (
"fmt"
"io"
"math"
"os"
......@@ -63,7 +62,7 @@ func (rng *range2) GetTicks(r chart.Renderer, cs chart.Style, vf chart.ValueForm
// io.writer. We do not close the file. The caller opened it,
// so he should close it.
func plotfWrt(xdata, ydata []float64, tmprtrData []float64,
plotOut io.Writer, plotme bool) error {
fname string, plotme bool) error {
if !plotme {
return nil
}
......@@ -117,7 +116,12 @@ func plotfWrt(xdata, ydata []float64, tmprtrData []float64,
FillColor: drawing.ColorTransparent,
},
}
if err := graph.Render(chart.PNG, plotOut); err != nil {
fPlt, err := os.Create(fname)
if err != nil {
return err
}
defer fPlt.Close()
if err := graph.Render(chart.PNG, fPlt); err != nil {
return fmt.Errorf("Render: %w", err)
}
......@@ -129,7 +133,7 @@ func breaker() {}
// For each dimension, allocate space. Copy elements over from the long array.
// Then call the plotter on each series in turn. I think I have to make a
// slice of continuous series and then add them into the chart structure.
func plotxWrt(cprm *cprm, ndim int) error {
func plotxWrt(cprm *cprm, xPltName string, ndim int) error {
if !cprm.xplotme {
return nil
}
......@@ -137,11 +141,9 @@ func plotxWrt(cprm *cprm, ndim int) error {
type f64 []float64
len_used := len(cprm.plotnstp)
xdata := make([]f64, ndim)
fmt.Println("plotxWrt has", ndim, "dimensions and len_used is", len_used)
for i := 0; i < ndim; i++ {
xdata[i] = make([]float64, len_used)
}
breaker()
var n int
for i := 0; i < len_used; i++ {
for j := 0; j < ndim; j++ {
......@@ -173,7 +175,12 @@ func plotxWrt(cprm *cprm, ndim int) error {
},
},
}
if err := graph.Render(chart.PNG, cprm.xPlt); err != nil {
xPlt, err := os.Create(xPltName)
if err != nil {
return err
}
defer xPlt.Close()
if err := graph.Render(chart.PNG, xPlt); err != nil {
return fmt.Errorf("plotting X trajectories: %w", err)
}
return nil
......
......@@ -11,7 +11,6 @@ import (
"errors"
"fmt"
"io"
"strconv"
"strings"
)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment