tsdev / spinw

SpinW Matlab library for spin wave calculation
www.psi.ch/spinw
GNU General Public License v3.0
30 stars 35 forks source link

Usage of transplant.MatlabStruct #43

Open mincentatties opened 6 years ago

mincentatties commented 6 years ago

{mac OS 10.12.6, pyspinw}

Hi I can see structures within spinw objects ok. For example:

from transplant import Matlab
from transplant import MatlabStruct
m=Matlab('/Applications/pyspinw.app/pyspinw.sh')

gto=m.spinw()
Flist = MatlabStruct(gto.mag_str)['F']

What I haven't figured out yet is how to write to the spinw object structures directly. (I would like to - in this case - manually change some of the spins in the 'F' array)

Cheers Ross

tsdev commented 6 years ago

MatlabStruct() is used to make sure that a Python list is converted into a struct type in MATLAB, by default a Python list is converted into containers.Map in MATLAB. This might change in the future, as we are dicsussing this issue with the author of transplant. For more details check the transplant repo: https://github.com/bastibe/transplant. Now, you can still modify the spinw structure, with the limitation that you can assign values of field down to 1 level. Let me show what this means:

from transplant import Matlab as pyspinw
from transplant import MatlabStruct as struct
import numpy as np

m = pyspinw(executable='/Applications/Pyspinw.app/pyspinw.sh')
tri = m.sw_model('triAF',1.)
F = tri.mag_str
F['k'][2,0] = 1.
tri.mag_str = struct(F)

The above example showed how to change the magnetic propagation vector. You might try to change the k-vector directly: tri.mag_str['k'][2,0] = 1. but this will not work, you can check by calling tri.mag_str['k'][2,0] and you will get the original unmodified value. In any case I suggest to change the spinw properties via its methods, the equivalent to the above code is

M = tri.magstr()
tri.genmagstr('mode','helical','S',M['S'],'n',M['n'],'k',np.array([1./3.,1./3.,1.]))

This might be a bit cumbersome, but might help to build your own commands.