Skip to content

Commit

Permalink
Add vector to scope, fix quantity saving
Browse files Browse the repository at this point in the history
  • Loading branch information
MathieuMoalic committed Dec 2, 2024
1 parent 55a6581 commit 2b7576f
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 13 deletions.
2 changes: 0 additions & 2 deletions src/geometry/geom.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,7 @@ func (g *Geometry) SetGeom(s shape.Shape) {
// M inside geom but previously outside needs to be re-inited
needupload := false
geomlist := CpuSlice.Host()[0]
g.log.Debug("2")
mhost := g.mag_slice.HostCopy()
g.log.Debug("3")
m := mhost.Host()
rng := rand.New(rand.NewSource(0))
for i := range m[0] {
Expand Down
2 changes: 1 addition & 1 deletion src/metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (m *Metadata) Add(key string, val interface{}) {
// ignore functions
return
default:
m.log.Debug("Metadata key %s has invalid type %s: %v", key, valType, val)
m.log.Warn("Not adding this metadata key %s because it has an invalid type %s: %v", key, valType, val)
}
}

Expand Down
25 changes: 17 additions & 8 deletions src/saved_quantities/saved_quantities.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ type SavedQuantity struct {
nextTime float64 // Next time when autosave should trigger
}

func NewSavedQuantity(log *log.Logs, q quantity.Quantity, name string, rchunks chunk.RequestedChunking, period float64) *SavedQuantity {
func NewSavedQuantity(log *log.Logs, fs *fsutil.FileSystem, solver *solver.Solver, q quantity.Quantity, name string, rchunks chunk.RequestedChunking, period float64) *SavedQuantity {
return &SavedQuantity{
log: log,
fs: fs,
solver: solver,
name: name,
q: q,
period: period,
Expand Down Expand Up @@ -69,30 +72,34 @@ func (sq *SavedQuantity) save() {
sq.q.EvalTo(buffer)
defer cuda.Recycle(buffer)
dataSlice := buffer.HostCopy()
// Saving the step so that it doesn't change during the save process
step := len(sq.times) - 1
sq.fs.QueueOutput(func() {
err := sq.syncSave(dataSlice, sq.name, len(sq.times), sq.chunks)
err := sq.syncSave(dataSlice, sq.name, step, sq.chunks)
sq.log.PanicIfError(err)
})
}

// syncSave writes the data slice into chunked, compressed files compatible with the Zarr format.
func (sq *SavedQuantity) syncSave(array *slice.Slice, qname string, step int, chunks chunk.Chunks) error {
path := qname + "/.zarray"
data := array.Tensors()
size := array.Size()
ncomp := array.NComp()

// Save .zarray metadata
err := sq.fs.SaveFileZarray(
fmt.Sprintf("%s/.zarray", qname),
path,
size,
ncomp,
step,
chunks.Z.Len, chunks.Y.Len, chunks.X.Len, chunks.C.Len,
)
if err != nil {
sq.log.Err("Error saving .zarray file: %v", err)
return err
}

var compressedData []byte
// Iterate over chunks and save data
for icx := 0; icx < chunks.X.Count; icx++ {
for icy := 0; icy < chunks.Y.Count; icy++ {
Expand All @@ -108,21 +115,23 @@ func (sq *SavedQuantity) syncSave(array *slice.Slice, qname string, step int, ch
for ic := 0; ic < chunks.C.Len; ic++ {
c := icc*chunks.C.Len + ic
value := data[c][z][y][x]
err := binary.Write(&bdata, binary.LittleEndian, value)
err = binary.Write(&bdata, binary.LittleEndian, value)
if err != nil {
return err
}
}
}
}
}
compressedData, err := zstd.Compress(nil, bdata.Bytes())
compressedData, err = zstd.Compress(nil, bdata.Bytes())
if err != nil {
sq.log.Err("Error saving .zarray file: %v", err)
return err
}
filename := fmt.Sprintf("%s/%d.%d.%d.%d.%d", qname, step+1, icz, icy, icx, icc)
filename := fmt.Sprintf("%s/%d.%d.%d.%d.%d", qname, step, icz, icy, icx, icc)
err = sq.fs.Put(filename, compressedData)
if err != nil {
sq.log.Err("Error saving .zarray file: %v", err)
return err
}
}
Expand Down Expand Up @@ -173,7 +182,7 @@ func (sqs *SavedQuantities) createSavedQuantity(q quantity.Quantity, name string
}
err := sqs.fs.Mkdir(name)
sqs.log.PanicIfError(err)
sq := NewSavedQuantity(sqs.log, q, name, rchunks, period)
sq := NewSavedQuantity(sqs.log, sqs.fs, sqs.solver, q, name, rchunks, period)
sqs.Quantities = append(sqs.Quantities, *sq)
return sq

Expand Down
3 changes: 2 additions & 1 deletion src/script/scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/MathieuMoalic/amumax/src/solver"
"github.com/MathieuMoalic/amumax/src/table"
"github.com/MathieuMoalic/amumax/src/utils"
"github.com/MathieuMoalic/amumax/src/vector"
"github.com/MathieuMoalic/amumax/src/window_shift"
)

Expand Down Expand Up @@ -149,7 +150,7 @@ func (p *ScriptParser) AddToScopeAll(
// p.RegisterFunction("ExpectV", expectV, "Used for automated tests: checks if a vector is close enough to the expected value")
// p.RegisterFunction("Fprintln", fprintln, "Print to file")
// p.RegisterFunction("Sign", sign, "Signum function")
// p.RegisterFunction("Vector", vector, "Constructs a vector with given components")
p.RegisterFunction("Vector", vector.New, "Constructs a vector with given components")
p.RegisterFunction("Print", p.Print, "Print to standard output")
// p.RegisterFunction("LoadFile", loadFile, "Load a zarr data file")
// p.RegisterFunction("LoadOvfFile", loadOvfFile, "Load an ovf data file")
Expand Down
1 change: 0 additions & 1 deletion src/table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ func (ts *Table) FlushToFile() error {
// Check if the table state has changed
currentHash := ts.generateHash()
if currentHash == ts.lastSavedHash {
ts.log.Debug("Table state has not changed, skipping save.")
return nil
}
for i := range ts.columns {
Expand Down
2 changes: 2 additions & 0 deletions src/vector/vector.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package vector

import "math"

func New(x, y, z float64) Vector { return Vector{x, y, z} }

// 3-component vector
type Vector [3]float64

Expand Down

0 comments on commit 2b7576f

Please sign in to comment.