User defined functions

367 views
Skip to first unread message

Rory Evans

unread,
Feb 17, 2016, 11:57:22 AM2/17/16
to mumax2
I'm trying to define my own function in my mumax3 script called spectrum. I've followed the syntax for go (https://www.golang-book.com/books/intro/7), but I'm getting this error:

script line 28:6: expected '(', found 'IDENT' spectrum (and 1 more errors):

line 28 is where I've defined the function. What I'm trying to do is create a time dependent external field, but one that oscillates with a full spectrum of frequencies.

Script attached. 

I'm quite new to this field so any help offered would be much appreciated,
Thank you. 
B-spectrum.txt

Ahmad Syukri bin Abdollah

unread,
Feb 17, 2016, 1:22:10 PM2/17/16
to mumax2
In order to use all golang features such as defining arbitrary functions, you need to import mumax3 as a package from within golang code.
See an example in mumax3 source code (actually, a test file, but whatever): https://github.com/mumax/3/blob/arne/test/standardproblem4.go

While there are many similarities with mumax3 script, certain things must be done in a different way. For example, in .mx3 (note: .mx3 is case-insensitive)
m = uniform(1, .1, 0)
autosave
(m, 100e-12)
But in .go (note: case-sensitive!), it would become
M.Set(Uniform(1, .1, 0))
AutoSave(&M, 100e-12)

You can copy that example file to serve as a starting template, remove the last two lines in the main function (which is only used for testing), and modify accordingly the lines starting from SetGridSize until Run.
------------------------------------------
As a side note, it is technically possible to do in mx3 script what you're trying achieve, though somewhat impractical:
Aex = 3.05e-13
Dbulk = 2.75e-4
Msat = 160e3
Ku1 = -0.8e4
AnisU = Vector(0, 0, 1)
alpha = 0.1
H := 18e-9

Nx := 2
Ny := 2
Nz := 64

CellSize := H/Nz

Cx := CellSize
Cy := CellSize
Cz := CellSize

SetGridSize(Nx, Ny, Nz)
SetCellSize(Cx, Cy, Cz)
SetPBC(10, 10, 0)
TableAdd(B_ext)
TableAutoSave(1e-15)

B_ext = Vector(Sin(2*pi*1e9*t) +
 Sin(2*pi*2e9*t) +
 Sin(2*pi*3e9*t) +
 Sin(2*pi*4e9*t) +
 Sin(2*pi*5e9*t) +
 Sin(2*pi*6e9*t) +
 Sin(2*pi*7e9*t) +
 Sin(2*pi*8e9*t) +
 Sin(2*pi*9e9*t) +
 Sin(2*pi*10e9*t), 0, 0)
Run(1)

As you may have already tried and found out, the following doesn't work:
spectrum := Vector(Sin(2*pi*10e9*t), 0, 0) // Becomes constant Vector(0, 0, 0) if spectrum assigned at t=0
B_ext = spectrum // B_ext becomes zero all the time
because during assignment, the right hand value is evaluated, turned into a constant. 
mx3 doesn't support function literals either, so neither can you do this:
spectrum := func () { return 2*pi*1e9*t }
Currently, these statements are allowed in script:
  • Assignment/Definition
  • if (and optionally, else)
  • for
    You can't use goto-like statements in for, such as break and continue.
    You can instead use if/else to skip parts of code.
  • Block scoping using braces.
    You can define or redefine a variable that will exist only inside the scope.
    jkl := 1010
    mno := "whee"
    {
      jkl := 1515 // jkl redefined as new variable
      mno = "opoo" // modifies mno defined outside scope
      pqr := 10e-9
      print(jkl) // prints 1515
      print(mno) // prints opoo
      print(pqr) // prints 10e-9
    }
    print(jkl) // prints 1010
    print(mno) // prints opoo
    print(pqr) // this line causes error, code will not run unless commented out


Although lacking in flexibility, mx3 scripts has the benefit of being able to be queued up in mumax3-server.

Felipe Garcia

unread,
Feb 17, 2016, 2:58:47 PM2/17/16
to mumax2
Hi Rory,Why are you trying to do like that? Doing that you will excite 500 harmonics of the main frequency but you will not get the bands of the spectrum. If your modes do not coincide with those frequency you will not excite them properly. I recommend you to use a function whose fourier transform is well known and gives the range of frequency you want to study. For example sine cardinal or gaussian. If your excitation remains in the linear regime, then the spectrum is just easy to compute. In mumax you can also compute the spectrum directly using mumax-fft ( I think), which uses that idea to compute directly the spectrum of your system, comparing the excitation and the output signal. Therefore I think it is easier than that.

Regards,
Felipe


--
You received this message because you are subscribed to the Google Groups "mumax2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mumax2+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Rory Evans

unread,
Feb 17, 2016, 3:36:08 PM2/17/16
to mumax2
Yes, I'm realizing now that I was thinking about this incorrectly, and I'm now experimenting using a Gaussian signal. I'm really only trying to find the resonant frequencies for this system, and the way I've been doing that in the past has been sort of crude.

Thanks for the advice.
Reply all
Reply to author
Forward
0 new messages