### Install SpinW MATLAB Package Source: https://spinw.org/IntroToSpinW Steps to install the SpinW package in MATLAB. This involves downloading the release, unzipping it, and running the installation script. It also covers updating the package using `sw_update`. ```matlab %% Install SpinW % Download the latest release from https://www.github.com/spinw/spinw/releases/latest % Unzip the archive into your preferred directory % Open MATLAB and run 'install_spinw' from inside this directory % Verify installation with: s = spinw; ``` ```matlab %% Update SpinW % Use the self-update function to get the latest version sw_update; ``` ```matlab %% Install SpinW as a MATLAB Add-on % Search for SpinW in the MATLAB Add-on Explorer and install it. ``` -------------------------------- ### Getting Help for SpinW Functions in MATLAB Source: https://spinw.org/Tartu2019 To get help for functions starting with 'sw_*', use the 'help' command followed by the function name. For SpinW class methods, use 'help spinw.method_name'. Detailed documentation is also available online. ```matlab help sw_* help spinw.function_name doc spinw/addatom ``` -------------------------------- ### Create Ferromagnetic Kagome Lattice Source: https://spinw.org/IntroToSpinW Initializes a spinw object and defines a ferromagnetic Kagome lattice with specified lattice constants and space group. It adds magnetic atoms with spin information. ```matlab FMkagome = spinw; FMkagome.genlattice('lat_const',[6 6 5],'angled',[90 90 120],'spgr','P -3') FMkagome.addatom('r', [1/2 0 0], 'S', 1, 'label','MCu1','color','r') ``` -------------------------------- ### SpinW Installation and Verification (Matlab) Source: https://spinw.org/NBI_2019 Instructions for installing the SpinW library in Matlab. It involves unzipping the archive, running an installation script, and verifying the installation by creating a spinw object. ```matlab install_spinw s = spinw; ``` -------------------------------- ### Generate and Plot Spin Wave Data with dnd Object Source: https://spinw.org/IntroToSpinW Creates a dnd object for simulating spin wave data on a square lattice, fills it with calculated spin wave spectra using 'Sperp' component and magnetic form factors, and then plots a constant energy cut. ```matlab d3dobj = d3d(sq.abc, [1 0 0 0], [-2,0.01,2], [0 1 0 0], [-2,0.01,2], [0 0 0 1], [0,0.1,10]); d3dobj = disp2sqw_eval(d3dobj, @sq.horace, {'component', 'Sperp', 'useMex', false, 'formfact', true}, 0.3); plot(cut(d3dobj, [], [], [3.8 4.2])) ``` -------------------------------- ### Single Ion Anisotropy Setup in SpinW (MATLAB) - P0 Space Group Source: https://spinw.org/SymmetryInSpinW This example shows how to set up single-ion anisotropy using the P0 space group in SpinW with MATLAB. It details the process of creating a lattice, adding multiple magnetic atoms, generating couplings, defining an anisotropy matrix, and applying it to the system. A plot is generated to visualize the structure. ```matlab cryst=spinw; cryst.genlattice('lat_const',[8 8 6]) cryst.addatom('r',[1/4 1/4 0],'S',1) cryst.addatom('r',[3/4 1/4 0],'S',1) cryst.addatom('r',[1/4 3/4 0],'S',1) cryst.addatom('r',[3/4 3/4 0],'S',1) cryst.gencoupling cryst.addmatrix('label','A','value',1-eye(3)) cryst.addaniso('A') plot(cryst,'atomLegend',false) ``` -------------------------------- ### spinw.addatom Method Documentation Source: https://spinw.org/spinw_addatom Detailed documentation for the spinw.addatom method, including syntax, description, examples, input arguments, and output. ```APIDOC ## spinw.addatom ### Description Adds a new atom to the list of symmetry inequivalent sites together with its properties, such as position, spin quantum number, form factor, etc. ### Syntax `addatom(obj, Name, Value)` ### Parameters #### Input Arguments - **obj** (spinw object) - The spinw object to which the atom will be added. #### Name-Value Pair Arguments - **r** (matrix) - Atomic positions stored in a matrix with dimensions of `[3 x n_atom]`. - **label** (cell array of strings) - Names of the atoms in a cell for plotting and form factor calculations. Default is `atomi`, where `i` is the atom index. - **S** (row vector) - Spin quantum number stored in a row vector with `n_atom` elements. For non-magnetic atoms, set S to zero. If not given, the spin quantum number is guessed from the label. - **color** (matrix or string or cell array) - RGB color of the atoms for plotting stored in a matrix with dimensions of `[3 x n_atom]`, where each column describes an RGB color. Values are between 0 and 255. Can also be a color name string or a cell array of color names for multiple atoms. - **ox** (double) - Oxidation number. Default is 0. - **occ** (double) - Occupancy. Default is 1. - **formfact** (row vector or string) - Neutron scattering form factor. Can be a row vector with 9 numbers or a string label from magion.dat. - **formfactn** (row vector or string) - Same as `formfact`. - **formfactx** (row vector or string) - X-ray scattering form factor. Can be a row vector with 9 numbers or a string label from xrayion.dat. - **Z** (integer) - Atomic number. Default is 113 (Unobtanium). - **A** (integer) - Atomic mass. Default is -1 for the natural mixture of isotopes. - **bn** (double) - Neutron scattering length. Not implemented yet. - **bx** (double) - X-ray scattering length. - **biso** (double) - Isotropic displacement factors in units of Å^2. - **update** (logical) - If `true`, existing atoms with the same label and position will be updated. Default is `true`. ### Output Arguments Modifies the `spinw.unit_cell` property of the `obj` spinw object. ### Example ```matlab crystal = spinw; crystal.genlattice('lat_const',[4 3 3]) crystal.addatom('r',[0 1/2; 0 0; 0 0],'S',[1 0],'color',{'red' 'blue'}) crystal.plot ``` ### See Also spinw.genlattice | spinw.addmatrix | swplot.color | sw_mff | sw_cff ``` -------------------------------- ### Accessing Function Help in SpinW (Matlab) Source: https://spinw.org/NBI_2019 Explains how to access help documentation for SpinW functions in Matlab. Specifically, it shows how to use the `help` command for functions starting with `sw_*`. ```matlab help sw_* ``` -------------------------------- ### Open Help Files and Utilities in SpinW Source: https://spinw.org/tutorials/30tutorial This snippet demonstrates how to open help files, locate the SpinW root directory, and edit configuration files using MATLAB commands. It also shows how to view and manage SpinW preferences. ```matlab help swfiles % To find the location of the spinw library use sw_rootdir % To open any of the functions in the Matlab editor, use edit spinw.plot % To look at any of the spinw object properties, double click on the % "Workspace" view on the name of the object. Also the data files % (symmetry.dat, atom.dat, color.dat, magion.dat) can be easily edited, for % example edit symmetry.dat % Some settings are ekpt during the active Matlab session, these can be % set/get using the swpref.setpref and swpref.getpref commands. To change % the default values, it can be defined in the startup.m file. To get all % current value use swpref ``` -------------------------------- ### Add SpinW to Matlab Path Permanently Source: https://spinw.org/installation This snippet demonstrates how to add the SpinW installation directory and all its subdirectories to the permanent Matlab path by modifying the `startup.m` file. This ensures that SpinW functions are accessible every time Matlab is started. Replace `YOUR_PATH` with the actual directory where SpinW is unzipped. ```matlab addpath(genpath([YOUR_PATH '/spinw/spinw_2-0beta_rev195'])) ``` -------------------------------- ### Update SpinW Installation Source: https://spinw.org/installation This command is used to automatically check for, download, and install the latest version of SpinW. It also updates the Matlab search path to include the new files. After execution, it's recommended to clear Matlab's class cache. ```matlab sw_update() ``` -------------------------------- ### Compiling SpinW Mex Files Source: https://spinw.org/SpinWAndHorace Provides instructions for compiling SpinW's mex files, which are C libraries for speeding up calculations. This requires a Matlab-compatible compiler (e.g., Visual Studio, gcc, XCode) and involves running `mex -setup` followed by `sw_mex` or `sw_mex('test', true)` to compile and test. ```bash mex -setup ``` ```bash sw_mex ``` ```bash sw_mex('test', true) ``` -------------------------------- ### Define and Fit S(q,ω) Models in Horace using SpinW Source: https://spinw.org/SpinWAndHorace This example shows how to use SpinW's `horace_sqw` function within Horace's `multifit_sqw` for fitting experimental data. It details setting up the model, defining parameters to be varied (including matrix elements), and specifying simulation options like resolution functions. ```matlab J = 1.2; K = 0.1; tri = sw_model('triAF', J); tri.addmatrix('label', 'K', 'value', diag([0 0 K])); tri.addaniso('K'); fwhm = 0.75; scalefactor = 1; ws = cut_sqw(sqw_file, [0.05], [-0.1, 0.1], [-0.1, 0.1], [0.5]); fitobj = multifit_sqw(ws); fitobj.set_fun(@tri.horace_sqw); fitobj.set_pin({[J K fwhm scalefactor], 'mat', {'J_1', 'K(3,3)'}, ... 'hermit', false, 'useFast', true, 'formfact', true}); ws_sim = fitobj.simulate(); [ws_fit, fit_dat] = fitobj.fit() ``` -------------------------------- ### Calculate and Plot Powder Spectrum Source: https://spinw.org/IntroToSpinW Calculates the powder spectrum by averaging over random orientations and plots it in two ways: raw and convoluted with a Gaussian to simulate experimental broadening. ```matlab fmkPow = FMkagome.powspec(linspace(0,2.5,100),'Evect',linspace(0,7,250),'nRand',1000,'hermit',false); figure subplot(2,1,1) sw_plotspec(fmkPow,'colorbar',true,'axLim',[0 0.05]) ``` -------------------------------- ### spinw.optmagsteep Method Documentation Source: https://spinw.org/spinw_optmagsteep Details on the syntax, description, input arguments, name-value pair arguments, output arguments, and related functions for the spinw.optmagsteep method. ```APIDOC ## spinw.optmagsteep ### Description `optm = optmagsteep(obj,Name,Value)` determines the lowest energy magnetic configuration within a given magnetic supercell and previously fixed propagation (and normal) vector. It iteratively rotates each spin towards the local magnetic field, achieving a local energy minimum. This method often finds the global energy minimum, especially for small magnetic cells and non-frustrated structures, and is comparable to thermal quenching from the paramagnetic state. ### Method ```matlab optm = optmagsteep(obj,Name,Value) ``` ### Input Arguments * `obj` (spinw object): The spinw object containing the magnetic structure. ### Name-Value Pair Arguments * `'nRun'` (integer): Number of iterations. Default is 100. Each spin is quenched `nRun` times or until convergence. * `'boundary'` (string): Boundary conditions for the magnetic cell. Allowed values: `'free'` (omits interactions between extended unit cells) or `'per'` (retains interactions). Default is `{'per' 'per' 'per'}`. * `'nExt'` (integer): Size of the magnetic cell in number of crystal unit cells. Default is `obj.mag_str.nExt`. * `'fSub'` (function handle): Function to define non-interacting sublattices for parallelization. It should have the header `cGraph = fSub(conn,nExt)`. * `'subLat'` (vector): Vector assigning magnetic moments to non-interacting sublattices. If undefined, the `fSub` function is used. * `'random'` (boolean): If `true`, uses random initial spin orientations (paramagnetic state). If the initial spin configuration is undefined, it will always be random. Default is `false`. * `'TolX'` (double): Minimum change of the magnetic moment required for convergence. * `'saveAll'` (boolean): If `true`, saves moment directions for every loop. Default is `false`. * `'Hmin'` (double): Minimum field value on a spin required to move the spin. If the molecular field absolute value is below this, the spin will not be turned. Default is 0. * `'plot'` (boolean): If `true`, plots the magnetic structure in real time. Default is `false`. * `'pause'` (double): Time in seconds to pause after each optimization loop for slower plot movies. Default is 0. * `'fid'` (integer): Defines text output generation. Default is determined by the `fid` preference in `swpref`. Possible values: `0` (no output), `1` (MATLAB Command Window), or a file ID from `fopen`. ### Output Arguments * `optm` (struct): A struct containing the optimization results: * `obj` (spinw object): The spinw object with the optimized magnetic structure. * `M` (3D array): Magnetic moment directions. Dimensions: `[3 x n_magExt]` or `[3 x n_magExt x n_loop]` if `saveAll` is `true`. * `dM` (vector): The average change in the magnetic moment vector over all moments in the last loop. * `e` (double): Energy per spin in the optimized structure. * `param` (struct): Input parameters used for the optimization. * `nRun` (integer): Number of loops executed. * `datestart` (datetime): Starting time of the function execution. * `dateend` (datetime): Ending time of the function execution. * `title` (string): Title of the simulation, if provided. ### See Also * `spinw` * `spinw.anneal` * `sw_fsub` * `sw_fstat` ``` -------------------------------- ### swpref.import Method Source: https://spinw.org/swpref_import Imports preferences from a JSON file into the SpinW application. It can load from the default user's home folder or a specified location. ```APIDOC ## swpref.import Method ### Description Loads preferences from a JSON file. If no location is specified, it defaults to `swprefs.json` in the user's home folder. The loaded preferences are then applied, and a new preference object is returned. ### Method `obj = import(obj)` `obj = import(obj,location)` ### Parameters #### Path Parameters * **obj** (object) - Required - The preference object to be updated. * **location** (string) - Optional - The file path to the preferences JSON file. ### Request Example ```json { "obj": {}, "location": "/path/to/your/prefs.json" } ``` ### Response #### Success Response (200) * **obj** (object) - The updated preference object. #### Response Example ```json { "obj": { "preference1": "value1", "preference2": "value2" } } ``` ``` -------------------------------- ### Set Ferromagnetic Magnetic Structure Source: https://spinw.org/IntroToSpinW Sets a ferromagnetic magnetic structure where all spins are parallel along the y-axis. It uses the 'helical' mode of `genmagstr` with k=[0 0 0] to achieve this. ```matlab FMkagome.genmagstr('mode', 'helical', 'k', [0 0 0], 'n', [0 1 0], 'S', [0 1 0]) disp('Magnetic structure:') FMkagome.table('mag') FMkagome.energy plot(FMkagome,'range',[2 2 1]) ``` -------------------------------- ### Define Nearest Neighbor Couplings Source: https://spinw.org/IntroToSpinW Generates the nearest neighbor ferromagnetic couplings for the Kagome lattice. It uses `gencoupling` to identify symmetry-equivalent bonds up to a specified distance and displays bond information. ```matlab FMkagome.gencoupling('maxDistance',4) disp('Bonds (length in Angstrom):') FMkagome.table('bond',[]) ``` -------------------------------- ### Defining Matrix Parameters with 'mat' and 'selector' in SpinW Source: https://spinw.org/SpinWAndHorace Demonstrates how to define parameters for spin wave calculations using the 'mat' argument in SpinW. It shows how to add matrices, couple them, and use a 'selector' matrix for more complex parameter definitions. This is useful for specifying which elements of a matrix correspond to a given parameter. ```matlab Dvec = [0.1 0.2 0.3]; swobj.addmatrix('label', 'DM', 'value', Dvec); swobj.addcoupling('mat', 'DM', 'bond', 1); sel(:,:,1) = [0 0 0; 0 0 1; 0 -1 0]; % Dx sel(:,:,2) = [0 0 1; 0 0 0; -1 0 0]; % Dy sel(:,:,3) = [0 1 0; -1 0 0; 0 0 0]; % Dz fitobj.set_fun(@swobj.horace_sqw); fitobj.set_pin({Dvec, 'mat', {'DM', 'DM', 'DM'}, 'selector', sel, 'hermit', false}) fitobj.fit() ``` -------------------------------- ### Enabling Mex Files in SpinW for Faster Calculations Source: https://spinw.org/SpinWAndHorace Shows how to enable the use of compiled mex files in SpinW to accelerate spin wave calculations. This involves setting a preference using `swpref.setpref('usemex', true)`. If mex files are not compatible, an error will occur. ```matlab swpref.setpref('usemex',true) ``` -------------------------------- ### Convolute Spin Wave Spectra with Energy Resolution Source: https://spinw.org/IntroToSpinW Applies an energy resolution convolution to the previously calculated spin wave spectra using the `sw_instrument` function with a specified energy broadening `dE`. ```matlab spec = sw_instrument(spec,'dE',0.5); ``` -------------------------------- ### Plot Density of Magnon States Source: https://spinw.org/IntroToSpinW Calculates and plots the density of magnon states by sampling random points in the Brillouin Zone, followed by Gaussian convolution for smoothing. It uses the 'Sxx+Syy+Szz' component for the calculation. ```matlab nQ = 1E5; Q = rand(3, nQ); spec = FMkagome.spinwave(Q, 'hermit', false); spec = sw_egrid(spec, 'component', 'Sxx+Syy+Szz'); spec = sw_instrument(spec, 'dE', 0.1); I = sum(spec.swConv, 2); E = spec.Evect(2:end); ``` -------------------------------- ### SpinW Model Evaluation and Fitting Setup Source: https://spinw.org/Ispra2019 Sets up a multifit object using a 2D cut dataset, defines the SpinW model function with parameters and options, and enables the use of mex files for faster computation. It then times a single simulation iteration. ```matlab idx = 5; % EIs: [25 35 50 70 140], index 5 == 140meV fwhm = ei(idx)/30; % Typical resolution ~ 3% of Ei kk = multifit_sqw(d2d(ws_sub(idx))); kk = kk.set_fun (@pcsmo.horace_sqw, {[JF1 JA JF2 JF3 Jperp D fwhm] cpars{:}}); kk = kk.set_free ([1, 1, 1, 1, 1, 1, 1]); kk = kk.set_options ('list',2); swpref.setpref('usemex',true); % Time a single iteration tic wsim = kk.simulate; t_spinw_single = toc; ``` -------------------------------- ### Calculate and Plot Spin Wave Dispersion for Kagome Lattice Source: https://spinw.org/IntroToSpinW Calculates the spin wave dispersion for the ferromagnetic Kagome lattice along specified high-symmetry directions and plots the spectrum with energy grid. ```matlab fmkSpec = FMkagome.spinwave({[-1/2 0 0] [0 0 0] [1/2 1/2 0] 100},'hermit',false); fmkSpec = sw_neutron(fmkSpec); fmkSpec = sw_egrid(fmkSpec, 'Evect',linspace(0,6.5,100),'component','Sperp'); figure sw_plotspec(fmkSpec,'mode',1,'colorbar',false,'axLim',[0 8]) ``` -------------------------------- ### Import swpref Object from File (MATLAB) Source: https://spinw.org/swpref Demonstrates loading swpref preferences from a file using the swpref.import method. ```matlab pref = swpref filename = 'my_preferences.mat'; swpref.import(pref, filename) % Alternatively: % pref.import(filename) ``` -------------------------------- ### Clear Matlab Class Cache Source: https://spinw.org/installation This command clears the cache of user-defined classes in Matlab. It is often necessary after updating packages or making changes to class definitions to ensure that Matlab recognizes the latest versions of the classes. ```matlab clear classes ``` -------------------------------- ### Compare Spin Wave Intensities Along Different Directions Source: https://spinw.org/IntroToSpinW Compares the spin wave intensities along two different directions in reciprocal space. It generates plots for both directions, allowing for visual comparison of intensity profiles. ```matlab Fact = 1; amark o acolor r plot(cut(Fact*d3dobj, [-0.1 0.1], [], [0 5])) acolor k pm(cut(d3dobj, [], [-0.1 0.1], [0 5])) ``` -------------------------------- ### Powder Averaging of Spin Wave Spectra Source: https://spinw.org/IntroToSpinW Generates a powder-averaged spin wave spectrum. This involves creating a lattice, calculating the powder spectrum with specified energy and momentum ranges, applying an energy resolution convolution, and plotting the result. ```matlab sq.genlattice('lat_const', [6 6 9]); spec = sq.powspec(linspace(0, 2, 101), 'Evect', linspace(0, 5, 501), 'nRand', 3e3, 'useMex', false, 'formfact', true); spec = sw_instrument(spec, 'dE', 0.2, 'Ei', 8, 'thetaMin', 5); sw_plotspec(spec, 'mode', 'color', 'dE', 0.2) caxis([0 0.2]) ``` -------------------------------- ### Display Magnetic Structure Table Source: https://spinw.org/tutorials/08tutorial This snippet displays the generated magnetic structure in a tabular format. It shows details for each magnetic site, including atomic index, spin vector, real and imaginary components of the magnetic form factor, position, and k-vector. ```matlab Magnetic structure: ans = 27×8 table num matom idx S realFhat imagFhat pos kvect ___ ______ ___ _ ________________________ __________________________ _________________ ________________________________ 1 'MCu1' 1 1 -0.5 0.866 0 -0.866 -0.5 0 0.5 0 0 -0.33333 -0.33333 0 2 'MCu1' 2 1 -0.5 0.866 0 -0.866 -0.5 0 0 0.5 0 -0.33333 -0.33333 0 3 'MCu1' 3 1 -0.5 -0.866 0 0.866 -0.5 0 0.5 0.5 0 -0.33333 -0.33333 0 4 'MCu1' 1 1 1 0 0 0 1 0 1.5 0 0 -0.33333 -0.33333 0 5 'MCu1' 2 1 1 0 0 0 1 0 1 0.5 0 -0.33333 -0.33333 0 6 'MCu1' 3 1 -0.5 0.866 0 -0.866 -0.5 0 1.5 0.5 0 -0.33333 -0.33333 0 7 'MCu1' 1 1 -0.5 -0.866 0 0.866 -0.5 0 2.5 0 0 -0.33333 -0.33333 0 8 'MCu1' 2 1 -0.5 -0.866 0 0.866 -0.5 0 2 0.5 0 -0.33333 -0.33333 0 9 'MCu1' 3 1 1 0 0 0 1 0 2.5 0.5 0 -0.33333 -0.33333 0 10 'MCu1' 1 1 1 0 0 0 1 0 0.5 1 0 -0.33333 -0.33333 0 11 'MCu1' 2 1 1 0 0 0 1 0 0 1.5 0 -0.33333 -0.33333 0 12 'MCu1' 3 1 -0.5 0.866 0 -0.866 -0.5 0 0.5 1.5 0 -0.33333 -0.33333 0 13 'MCu1' 1 1 -0.5 -0.866 0 0.866 -0.5 0 1.5 1 0 -0.33333 -0.33333 0 14 'MCu1' 2 1 -0.5 -0.866 0 0.866 -0.5 0 1 1.5 0 -0.33333 -0.33333 0 15 'MCu1' 3 1 1 0 0 0 1 0 1.5 1.5 0 -0.33333 -0.33333 0 16 'MCu1' 1 1 -0.5 0.866 0 -0.866 -0.5 0 2.5 1 0 -0.33333 -0.33333 0 17 'MCu1' 2 1 -0.5 0.866 0 -0.866 -0.5 0 2 1.5 0 -0.33333 -0.33333 0 18 'MCu1' 3 1 -0.5 -0.866 0 0.866 -0.5 0 2.5 1.5 0 -0.33333 -0.33333 0 19 'MCu1' 1 1 -0.5 -0.866 0 0.866 -0.5 0 0.5 2 0 -0.33333 -0.33333 0 20 'MCu1' 2 1 -0.5 -0.866 0 0.866 -0.5 0 0 2.5 0 -0.33333 -0.33333 0 21 'MCu1' 3 1 1 0 0 0 1 0 0.5 2.5 0 -0.33333 -0.33333 0 22 'MCu1' 1 1 -0.5 0.866 0 -0.866 -0.5 0 1.5 2 0 -0.33333 -0.33333 0 23 'MCu1' 2 1 -0.5 0.866 0 -0.866 -0.5 0 1 2.5 0 -0.33333 -0.33333 0 24 'MCu1' 3 1 -0.5 -0.866 0 0.866 -0.5 0 1.5 2.5 0 -0.33333 -0.33333 0 25 'MCu1' 1 1 1 0 0 0 1 0 2.5 2 0 -0.33333 -0.33333 0 26 'MCu1' 2 1 1 0 0 0 1 0 2 2.5 0 -0.33333 -0.33333 0 27 'MCu1' 3 1 -0.5 0.866 0 -0.866 -0.5 0 2.5 2.5 0 -0.33333 -0.33333 0 ``` -------------------------------- ### Generate Random Cylinders with swplot.cylinder (MATLAB) Source: https://spinw.org/swplot_cylinder This example demonstrates how to generate 100 random cylinders within a specified cube using the swplot.cylinder function. It utilizes random start and end points, a defined radius, and specifies the number of patch faces and closure. ```matlab swplot.figure N = 100; swplot.cylinder(2*rand(3,N)-1,2*rand(3,N)-1,0.1,100,true) swplot.zoom(30) ``` -------------------------------- ### Calculate Spin Wave Dispersion Along a Defined Path Source: https://spinw.org/IntroToSpinW Calculates the spin wave dispersion along a specified path in reciprocal space connecting high-symmetry points (G, X, M). The path is defined by a series of points and the number of steps. ```matlab G = [0 0 0]; X = [1/2 0 0]; M = [1/2 1/2 0]; spec = sq.spinwave({G X M G 301}); ``` -------------------------------- ### Set up AFM Hamiltonian in SpinW Source: https://spinw.org/tutorials/08tutorial This code sets up an antiferromagnetic (AFM) first neighbor interaction in the SpinW model. It adds a matrix labeled 'J1' with a value of 1.00 and assigns this matrix to the first neighbor bonds using spinw.addmatrix and spinw.addcoupling. The lattice is then plotted again to visualize the Hamiltonian. ```matlab AF33kagome.addmatrix('label','J1','value',1.00,'color','g') AF33kagome.addcoupling('mat','J1','bond',1) plot(AF33kagome,'range',[2 2 1/2],'cellMode','inside') ``` -------------------------------- ### Create Square Lattice Heisenberg Antiferromagnet Model Source: https://spinw.org/IntroToSpinW Defines a square lattice Heisenberg antiferromagnet model with specified spin and exchange parameters using `sw_model`. It also adds an atom with its magnetic form factor. The resulting model can be visualized. ```matlab sq = sw_model('squareAF', 1); sq.addatom('label', 'atom_1', 'r', [0 0 0], 'formfactn', 'MCu2+', 'S', 1/2) plot(sq) ``` -------------------------------- ### Determine Propagation Vector of Cycloid in BiFeO3 using SpinW Source: https://spinw.org/magneticStructurePresentation A worked example demonstrating the use of optmagk in SpinW to determine the propagation vector 'k' of the spin cycloid in multiferroic BiFeO3. The script allows exploration of how DM and Heisenberg exchange interactions influence 'k'. ```matlab # Download the script here: `bifeo3.m` # BiFeO3 cycloid # Tutorial 1: Propagation vector of cycloid in BiFeO3 # This tutorial uses `optmagk` to determine the propagation vector kk of the spin cycloid in multiferroic BiFeO3 ``` -------------------------------- ### DM Interaction Setup in SpinW (MATLAB) Source: https://spinw.org/SymmetryInSpinW This snippet demonstrates how to set up a crystal structure with DM interaction using the P0 space group in SpinW. It involves generating a lattice, adding atoms with specified positions and spins, defining coupling matrices, and applying DM interactions. The output is a plot visualizing the magnetic structure. ```matlab cryst=spinw; cryst.genlattice('lat_const',[8 8 6]) cryst.addatom('r',[1/4 1/4 0],'S',1); cryst.addatom('r',[3/4 1/4 0],'S',1); cryst.addatom('r',[1/4 3/4 0],'S',1); cryst.addatom('r',[3/4 3/4 0],'S',1); cryst.gencoupling cryst.addmatrix('label','D','value',[1 -1 0]) cryst.addcoupling('mat','D','bond',1,'subIdx',[3 5 7:8]) plot(cryst,'range',[1 1 1/2],'atomLegend',false) ``` -------------------------------- ### Configure SpinW Model Parameters (MATLAB) Source: https://spinw.org/SpinWAndHorace This snippet shows how to set up specific parameters for a SpinW model, including coordinate transformations and general options like matrix definitions, hermeticity, speed optimizations, and resolution functions. It's used for preparing a model for evaluation or fitting. ```MATLAB cpars = {'coordtrans', diag([2 2 1 1])} cpars = {cpars{:}, 'mat', {'JF1', 'JA', 'JF2', 'JF3', 'Jperp', 'D(3,3)'}, ... 'hermit', false, 'optmem', 0, 'useFast', true, 'formfact', true, ... 'resfun', 'gauss'}; ``` -------------------------------- ### SpinW Object Creation Source: https://spinw.org/spinw_spinw This section details the different ways to instantiate a SpinW object, each serving a specific purpose from creating an empty object to importing data or cloning existing objects. ```APIDOC ## SpinW Object Creation and Initialization ### Description The `spinw` method provides multiple syntaxes for creating and initializing SpinW objects, allowing for flexibility in how you start working with spin wave data. ### Method Various constructors are available depending on the input arguments. ### Endpoint `spinw` (method) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```json { "example": "obj = spinw()" } ``` ### Response #### Success Response (200) - **obj** (SpinW object) - A SpinW object initialized according to the provided syntax. #### Response Example ```json { "example": "obj = spinw() // Creates an empty SpinW object" } ``` ### Detailed Syntaxes: 1. **`obj = spinw`** * **Description**: Creates an empty SpinW object with default values. * **Request Example**: `obj = spinw` 2. **`obj = spinw(struct)`** * **Description**: Creates a SpinW object from a structure (`struct`) which must have fields compatible with the SpinW property structure. * **Parameters**: `struct` (struct) - A structure containing SpinW properties. * **Request Example**: `obj = spinw(my_structure)` 3. **`obj = spinw(hFigure)`** * **Description**: Clones a SpinW object from an existing swplot figure (`hFigure`) or spectral plot figure. * **Parameters**: `hFigure` (figure handle) - Handle to a SpinW or spectral plot figure. * **Request Example**: `obj = spinw(gcf)` 4. **`obj = spinw(fName)`** * **Description**: Imports data from a file specified by `fName`. SpinW can import crystal or magnetic structure data from `.cif` or `.fts` files, accessible from local paths or web addresses. * **Parameters**: `fName` (string) - The filename or URL of the data file. * **Request Example**: `obj = spinw('https://example.com/my_structure.cif')` 5. **`obj = spinw(obj)`** * **Description**: Checks the input SpinW object (`obj`) for internal consistency. * **Parameters**: `obj` (SpinW object) - The SpinW object to check. * **Request Example**: `obj = spinw(existing_spinw_object)` ``` -------------------------------- ### Verifying SpinW Installation via MATLAB Add-on Source: https://spinw.org/Tartu2019 After installing SpinW as a MATLAB add-on from Mathworks, users can verify the installation by creating a SpinW object. This confirms that the package has been correctly integrated into the MATLAB environment. ```matlab s = spinw; ``` -------------------------------- ### SpinW Model Initialization - Coordinate Transformation Source: https://spinw.org/Ispra2019 Sets up coordinate transformation parameters for SpinW, specifically multiplying Qh and Qk by 2. This is necessary when the structural unit cell used in SpinW differs from that in Horace. ```matlab cpars = {'coordtrans', diag([2 2 1 1])} ``` -------------------------------- ### Get Anisotropy Matrix Elements in C4 Point Group Source: https://spinw.org/spinw_getmatrix This example demonstrates how to use `getmatrix` to find the allowed anisotropy matrix elements for an atom within the C4 point group. The code first sets up a crystal structure with C4 symmetry, adds an atom, assigns a matrix, generates couplings, adds anisotropy, and then calls `getmatrix` to retrieve the symmetry-allowed elements. The output shows the structure of the allowed symmetric matrix. ```matlab cryst = spinw cryst.genlattice('sym','P 4') cryst.addatom('r',[0 0 0],'label','MCu2') cryst.addmatrix('label','A','value',1) cryst.gencoupling cryst.addaniso('A') cryst.getmatrix('mat','A') ``` -------------------------------- ### Example: Helical Magnetic Structure Source: https://spinw.org/NBI_2019 Example of generating a helical magnetic structure for a Cr3 material. This involves setting up the lattice, adding atoms, and then calling genmagstr with the 'helical' mode. ```matlab tri = spinw; tri.genlattice('lat_const', [4 4 6], 'angled', [90 90 120]); tri.addatom('r', [0 0 0], 'S', 2, 'label', 'MCr3', 'color', 'gold'); tri.genmagstr('mode', 'helical', 'S', [1; 0; 0], 'n', [0 0 1], 'k', [1/3 1/3 0]) ``` -------------------------------- ### Perform SpinW Model Simulation and Fit (MATLAB) Source: https://spinw.org/SpinWAndHorace This code performs a simulation using a configured SpinW model and a multifit object. It selects a specific energy dataset, sets up a multifit object using a dnd object for efficiency, enables mex files for speed, and times a single simulation iteration. This is crucial for evaluating model performance against experimental data. ```MATLAB idx = 5; % EIs: [25 35 50 70 140], index 5 == 140meV fwhm = ei(idx)/30; % Typical resolution ~ 3% of Ei kk = multifit_sqw(d2d(ws_sub(idx))); kk = kk.set_fun (@pcsmo.horace_sqw, {[JF1 JA JF2 JF3 Jperp D fwhm] cpars{:}}); kk = kk.set_free ([1, 1, 1, 1, 1, 1, 1]); kk = kk.set_options ('list',2); swpref.setpref('usemex',true); % Time a single iteration tic wsim = kk.simulate; t_spinw_single = toc; ``` -------------------------------- ### Example: Random Magnetic Structure Source: https://spinw.org/NBI_2019 Example of generating a random magnetic structure for a Cr3 material. This involves setting up the lattice, adding an atom, and calling genmagstr with the 'random' mode and specified 'nExt'. ```matlab fct = spinw; fct.genlattice('lat_const', [4 4 6], 'angled', [90 90 90]); fct.addatom('r', [0.5 0.5 0], 'S', 2, 'label', 'MCr3', 'color', 'gold'); fct.genmagstr('mode', 'random', 'nExt', [2 2 1]) ``` -------------------------------- ### Example: Fourier Magnetic Structure Source: https://spinw.org/NBI_2019 Example of generating a magnetic structure using Fourier components for a Cr3 material. This demonstrates setting up the lattice, adding atoms, and using the 'fourier' mode in genmagstr. ```matlab mmod = spinw; mmod.genlattice('lat_const', [4 4 6], 'angled', [90 90 90]); mmod.addatom('r', [0.5 0.5 0.5], 'S', 2, 'label', 'MCr3', 'color', 'gold'); mmod.genmagstr('mode', 'fourier', 'S', [0; 1; 0], 'k', [0.07 0 0]) ``` -------------------------------- ### Calculate Powder Spectrum Source: https://spinw.org/tutorials/08tutorial Calculates the powder spectrum using a small magnetic cell for faster computation. The speed is dependent on the number of Q points and random orientations, with minimal dependence on energy bin size. ```matlab kag33Pow = AF33kagome.powspec(linspace(0,2.5,100),'Evect',linspace(0,3,500), 'hermit',false,'nRand',1e2); figure sw_plotspec(kag33Pow,'axLim',[0 0.2],'dE',0.05) ```