MXJK851 / SpinView

SpinView: General interactive visual analysis tool for multi-scale computational magnetism
GNU General Public License v3.0
23 stars 3 forks source link

OVF 2.0 UnicodeDecodeError #1

Open MathieuMoalic opened 1 year ago

MathieuMoalic commented 1 year ago

Hi there!

There looks to be a parsing error of OVF2.0 files. I'm testing by running mumax3 on the first example from the website:

SetGridsize(128, 32, 1)
SetCellsize(500e-9/128, 125e-9/32, 3e-9)

Msat  = 800e3
Aex   = 13e-12
alpha = 0.02

m = uniform(1, .1, 0)
relax()
save(m)    // relaxed state

autosave(m, 200e-12)
tableautosave(10e-12)

B_ext = vector(-24.6E-3, 4.3E-3, 0)
run(1e-9)

I run spinview start from within the .out folder, and I get this error:

/home/mat/dl/spinview/.venv/lib/python3.9/site-packages/spinview/spinview_parsers/parsers.py:270 │
│ in system_size_ovf_parser                                                                        │
│                                                                                                  │
│   267                                                                                            │
│   268 def system_size_ovf_parser(ovfpath):                                                       │
│   269 │   with open(ovfpath, "r") as infile:                                                     │
│ ❱ 270 │   │   lines = infile.readlines()                                                         │
│   271 │   │   stop_i = 0                                                                         │
│   272 │   │   for idx, line in enumerate(lines):                                                 │
│   273 │   │   │   stop_i = stop_i + 1                                                            │
│                                                                                                  │
│ ╭─────────────────────────────────────────── locals ───────────────────────────────────────────╮ │
│ │  infile = <_io.TextIOWrapper name='/home/mat/dl/spinview/t.out/m000004.ovf' mode='r'         │ │
│ │           encoding='UTF-8'>                                                                  │ │
│ │ ovfpath = '/home/mat/dl/spinview/t.out/m000004.ovf'                                          │ │
│ ╰──────────────────────────────────────────────────────────────────────────────────────────────╯ │
│                                                                                                  │
│ /usr/lib/python3.9/codecs.py:322 in decode                                                       │
│                                                                                                  │
│    319 │   def decode(self, input, final=False):                                                 │
│    320 │   │   # decode input (taking the buffer into account)                                   │
│    321 │   │   data = self.buffer + input                                                        │
│ ❱  322 │   │   (result, consumed) = self._buffer_decode(data, self.errors, final)                │
│    323 │   │   # keep undecoded input until the next call                                        │
│    324 │   │   self.buffer = data[consumed:]                                                     │
│    325 │   │   return result                                                                     │
│                                                                                                  │
│ ╭─────────────────────────────────────────── locals ───────────────────────────────────────────╮ │
│ │  data = b'# OOMMF OVF 2.0\n# Segment count: 1\n# Begin: Segment\n# Begin: Header\n# Title:   │ │
│ │         m\n#'+8112                                                                           │ │
│ │ final = False                                                                                │ │
│ │ input = b'# OOMMF OVF 2.0\n# Segment count: 1\n# Begin: Segment\n# Begin: Header\n# Title:   │ │
│ │         m\n#'+8112                                                                           │ │
│ │  self = <encodings.utf_8.IncrementalDecoder object at 0x7f09df1d8ee0>                        │ │
│ ╰──────────────────────────────────────────────────────────────────────────────────────────────╯ │
╰──────────────────────────────────────────────────────────────────────────────────────────────────╯
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb4 in position 517: invalid start byte

EDIT: I just noticed that you have example ovfs in this repo. They are all in the text format. I take it that you only support text ovf right now. Mumax will save by default in a binary format. Here is how I read ovf files into a numpy array if it can help you.

def load_ovf(path: str) -> NDArray[Shape["4"], Int]:
    with open(path, "rb") as f:
        dims = np.array([0, 0, 0, 0])
        while True:
            line = f.readline().strip().decode("ASCII")
            if "valuedim" in line:
                dims[3] = int(line.split(" ")[-1])
            if "xnodes" in line:
                dims[2] = int(line.split(" ")[-1])
            if "ynodes" in line:
                dims[1] = int(line.split(" ")[-1])
            if "znodes" in line:
                dims[0] = int(line.split(" ")[-1])
            if "Begin: Data" in line:
                break
        count = int(dims[0] * dims[1] * dims[2] * dims[3] + 1)
        arr = np.fromfile(f, "<f4", count=count)[1:].reshape(dims)
    return arr

Cheers

MXJK851 commented 1 year ago

Dear MathieuMoalic,

Thank you very much for using and contributing to SpinView! Knowing how to make SpinView compatible with the 'normal Mumax' workflow is great. 👍

I will get back to you in this thread this weekend!

Cheers, Qichen

MXJK851 commented 1 year ago

@MathieuMoalic

Hi MathieuMoalic,

I hope you are doing well, and thank you again for sharing your parser to help SpinView become better! (you can find the acknowledge in release page )

Now you can try version 1.0.2, SpinView can handle binary OVF files now! :-) (Pls feel free to let me know if you find any bugs)

Cheers,
Qichen