(* 
This notebook is supposed to calculate magnetic hysteresis of two coupled layers. Each layer is represented by a single magnetic moment with constant magnitude.
It is based on Stoner-Wohlfahrt model. With RKKY coupling set to zero it corresponds to classic S-W model.
Note that on slow computers sliders should be used with caution.

In some cases (parallel orientation of the field and ferromagnetic coupling) one may have to fine-tune 
FindMinimum parameters!!!


please report any errors/suggestions to urbaniak@ifmpan.poznan.pl


*) 
Manipulate[

b2=-b1;
zb=b1-b2;
n=200;(*number of field steps*)
M=1;
K=1;
M2=1;
K2=1;
(*RKKY coupling: j=>0 - ferro*)

db=zb/(n-1);



beta=kat*Pi/180;(*kat in degrees*)
minimum=0.9999999999*beta+0.0001;
minimum2=0.9999999999*beta+0.0001;
modh=Table[0,{i,2 n},{j,2}];
For[i=1,i<n+1,i++,
 B=b1-db*(i-1);
 
 m=FindMinimum[-B M Cos[a-beta]-B M2 Cos[a2-beta]-K Cos[a]^2-K2 Cos[a2]^2-Jc Cos[a-a2],{a,0.9999*minimum+0.0001},{a2,0.9999*minimum2+0.0001},Method->"PrincipalAxis"];
  minimum=m[[2,1,2]];
minimum2=m[[2,2,2]];
 modh[[i,1]]=B;
modh[[i,2]]=(Cos[beta-minimum]+Cos[beta-minimum2])/(M+M2);
 ]

For[i=n+1,i<2n,i++,
 modh[[i,2]]= -modh[[i-n,2]];
modh[[i,1]]= -modh[[i-n,1]];

];
modh[[2n,1]]= modh[[2n-1,1]];
modh[[2n,2]]= modh[[2n-1,2]];
If[Jc==0,vv="Classic Stoner-Wohlfahrt model"];
If[Jc<0,vv="Stoner-Wohlfahrt model with AF-coupling"];
If[Jc>0,vv="Stoner-Wohlfahrt model with ferromagnetic coupling"];
If[line,
ListPlot[modh,PlotJoined->True, Frame->True, PlotStyle->{RGBColor[0,0,1],Thickness[0.007]},PlotRange->All,ImageSize->450,AxesLabel->{"B[a.u.]","M/Ms"},PlotLabel->Style[Framed[vv],16,Blue,Background->Lighter[Yellow]]],

ListPlot[modh,PlotJoined->False, Frame->True, PlotStyle->{RGBColor[1,0,0],Thickness[0.007]},PlotRange->All,ImageSize->450,AxesLabel->{"B[a.u.]","M/Ms"},PlotLabel->Style[Framed[vv],16,Blue,Background->Lighter[Yellow]]]

]



,{{Jc,-4,"Coupling strength"},-5,5,1},{{kat,0,"Angle field-EA"},0,90,5},{{b1,15,"B-range"},5,30,5},{{line,True},{True,False}},FrameLabel->Style["Hysteresis of two coupled magnetic moments",Large],ContinuousAction->True]