Skip to content

Instantly share code, notes, and snippets.

@salviati
Last active June 30, 2016 22:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save salviati/da3edc4cd643749cbcf0a8c85c8b67b4 to your computer and use it in GitHub Desktop.
Save salviati/da3edc4cd643749cbcf0a8c85c8b67b4 to your computer and use it in GitHub Desktop.
extracts magnetization from an .ovf file as a Mathematica array
/*
ovf2mathematica: extracts magnetization from an .ovf file as a Mathematica array
Copyright (c) 20016, Utkan Güngördü <utkan@freeconsole.org>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"bufio"
"flag"
"fmt"
"github.com/mumax/3/oommf"
"log"
"os"
)
var (
in = flag.String("i", "", "input file name")
out = flag.String("o", "", "output file name")
)
func main() {
flag.Parse()
if *in == "" || *out == "" {
flag.PrintDefaults()
os.Exit(0)
}
slice, meta, err := oommf.ReadFile(*in)
if err != nil {
log.Fatalln(err)
}
m := slice.Tensors()
size := slice.Size()
cell := meta.CellSize
if slice.NComp() != 3 {
log.Fatalln("magnetization has ", slice.NComp(), " components instead of 3.")
}
f, err := os.OpenFile(*out, os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
log.Fatalln(err)
}
defer f.Close()
o := bufio.NewWriter(f)
defer o.Flush()
fmt.Fprint(o, "m = {")
for iz := 0; iz < size[oommf.Z]; iz++ {
for iy := 0; iy < size[oommf.Y]; iy++ {
for ix := 0; ix < size[oommf.X]; ix++ {
fmt.Fprintf(o, "{ {%.12f, %.12f, %.12f}, {%f, %f, %f} }", float64(ix)*cell[oommf.X], float64(iy)*cell[oommf.Y], float64(iz)*cell[oommf.Z], m[oommf.X][iz][iy][ix], m[oommf.Y][iz][iy][ix], m[oommf.Z][iz][iy][ix])
if isLast := iz == size[oommf.Z]-1 && iy == size[oommf.Y]-1 && ix == size[oommf.X]-1; isLast == false {
fmt.Fprint(o, ", ")
} else {
fmt.Fprint(o, " ")
}
}
}
}
fmt.Fprint(o, "};")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment