Note
Go to the end to download the full example code.
Optimization of spin directions¶
Tutorial tasks
Display list of all available parameters for the script.
Optimize the Hamiltonian and inspect the output files.
Optimize the Hamiltonian on several super-cells. Are there any changes of the ground state?
Optimize with different directions and values of the external magnetic field. How does the result change?
For the guide on the script read magnopy-optimize-sd.
Below we give two examples that were run on the reference files from GROGU.
Two reference files can be found in the Tutorial_2/magnopy-as-a-black-box folder.
Here are the reference input files
Links for downloading reference output files can be found at the end of each section.
Optimization on the unit cell¶
First, let us optimize the spin directions on the given unit cell of the spin Hamiltonian.
To do so run the commands
magnopy-optimize-sd -ss GROGU -sf CrI3/reference-GROGU.txt -of CrI3/optimize-sd-output -hpd > console-output.txt
and
magnopy-optimize-sd -ss GROGU -sf CrI3_U/reference-GROGU.txt -of CrI3_U/optimize-sd-output -hpd > console-output.txt
in the terminal.
Input parameters¶
Let us go through the parameters
-ss GROGUTells magnopy that the Hamiltonian is coming from GROGU.
-sf CrI3/reference-GROGU.txtTells magnopy where the file with the Hamiltonian is located. The path, that is given for the
-sfargument have to be either absolute or relative to the folder from which you execute the script.-of CrI3/optimize-sd-outputTells magnopy to save all output files inside the "CrI3/optimize-sd-output" folder.
-of -hpdTells magnopy to hide some of the user's private data in the output. Do not affect the calculation of magnopy, simply a decoration for the output.
> console-output.txtRedirects text output of magnopy and saves it to the file "console-output.txt", that will be created in the folder from which you executed the script. To display the text in the terminal and to save it to the file simultaneously use
| tee console-output.txtinstead.
Output files¶
After the successful completion of the script you can find several files in the output folder. Below we list all those files and give links where you can download examples of those files.
"console-output.txt"
This is the main output file of magnopy that lists the steps of the calculations, gives comments, warnings and details of the files with data that were produced and where they have been saved.
Content of "console-output.txt" for CrI3¶███╗ ███╗ █████╗ ██████╗ ███╗ ██╗ ██████╗ ██████╗ ██╗ ██╗ ████╗ ████║ ██╔══██╗ ██╔════╝ ████╗ ██║ ██╔═══██╗ ██╔══██╗ ╚██╗ ██╔╝ ██╔████╔██║ ███████║ ██║ ███╗ ██╔██╗ ██║ ██║ ██║ ██████╔╝ ╚████╔╝ ██║╚██╔╝██║ ██╔══██║ ██║ ╚██║ ██║╚██╗██║ ██║ ██║ ██╔═══╝ ╚██╔╝ ██║ ╚═╝ ██║ ██║ ██║ ╚██████╔╝ ██║ ╚████║ ╚██████╔╝ ██║ ██║ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═══╝ ╚═════╝ ╚═╝ ╚═╝ Version: 0.2.0 ▄ ▄ Documentation: magnopy.org █▀█▀█ Release date: 10 September 2025 █▄█▄█ License: GNU GPLv3 ███ ▄▄ Copyright (C) 2023-2025 Magnopy Team ████ █ █ ████ █ Generated on 9 September 2025 at 23:4:54 ▀▀▀▀▀▀▀▀ ======================================== Comment ========================================= Source of the parameters is "GROGU". Loaded parameters of the spin Hamiltonian from the file ../reference-CrI3.txt. =================================== Start optimization =================================== Energy tolerance : 1.00000e-05 Torque tolerance : 1.00000e-05 Minimizing on the original unit cell of the Hamiltonian. Spin directions of the initial guess are saved in file optimize-sd/INITIAL_GUESS.TXT ─────┬─────────────┬─────────────┬───────────── step │ E_0 │ delta E_0 │ max torque ─────┴─────────────┴─────────────┴───────────── 1 -6.7894173 1.2755420 0.7409278 2 -7.1091190 0.3197016 0.4967565 3 -7.1948753 0.0857563 0.2745469 4 -7.2118658 0.0169905 0.1905692 5 -7.2234004 0.0115346 0.1033486 6 -7.2274969 0.0040965 0.0760144 7 -7.2296184 0.0021215 0.0506572 8 -7.2302553 0.0006369 0.0465425 9 -7.2306439 0.0003886 0.0285843 10 -7.2308236 0.0001798 0.0285196 11 -7.2309233 0.0000997 0.0164194 12 -7.2309756 0.0000523 0.0156967 13 -7.2310031 0.0000275 0.0096519 14 -7.2310172 0.0000141 0.0074042 15 -7.2310240 0.0000068 0.0047899 16 -7.2310271 0.0000031 0.0030472 17 -7.2310285 0.0000013 0.0020362 18 -7.2310290 0.0000005 0.0011559 19 -7.2310292 0.0000002 0.0007966 20 -7.2310293 0.0000001 0.0004288 21 -7.2310294 0.0000000 0.0003040 22 -7.2310294 0.0000000 0.0001607 23 -7.2310294 0.0000000 0.0001158 24 -7.2310294 0.0000000 0.0000609 25 -7.2310294 0.0000000 0.0000442 26 -7.2310294 0.0000000 0.0000232 27 -7.2310294 0.0000000 0.0000169 28 -7.2310294 0.0000000 0.0000089 ─────────────────────────────────────────────── Optimization is done. Classic ground state energy (E_0) is -7.231029 meV Optimized spin directions are saved in file optimize-sd/SPIN_DIRECTIONS.txt Spin positions are saved in file optimize-sd/SPIN_POSITIONS.txt Image of spin directions is saved in file optimize-sd/SPIN_DIRECTIONS.html ======================================== Finished ========================================"INITIAL_GUESS.txt"
This file describes the initial guess for spin directions that was used by the optimization routine. The initial guess is random, meaning that it is going to be different every time you run the program. In the file you will find one direction per line - initial guesses for each magnetic center. In the same order as they appear in the input file from GROGU.
"SPIN_POSITIONS.txt"
This file gives Cartesian (absolute) coordinates of every magnetic center in the unit cell. One magnetic center per line, order is the same as in the input file from GROGU.
Content of "SPIN_POSITIONS.txt" for CrI3¶0.01290667 -0.00745002 15.00002010 3.55803919 2.03918089 15.00000948
"SPIN_DIRECTIONS.txt"
This file gives final, optimized spin directions for each spin in the unit cell. One spin per line, order is the same as in the input file from GROGU.
Content of "SPIN_DIRECTIONS.txt" for CrI3¶-0.00226056 -0.00123298 -0.99999668 -0.00198239 -0.00075780 -0.99999775
"SPIN_DIRECTIONS.html"
This file visualizes the results of calculation in the interactive 3D picture. Unit cell, supercell (if any), initial guess and optimize spin directions are plotted.
Download the files for "CrI3/reference-GROGU.txt"¶
INITIAL_GUESS.txt (for CrI3.txt)
Download the files for "CrI3_U/reference-GROGU.txt"¶
INITIAL_GUESS.txt (for CrI3_U.txt)
Optimization on the supercell¶
For some systems the true ground state may not have the same periodicity as the unit cell of the underlying crystal. In that case one shall try to optimize the spin directions on the supercell and check if any new local minima can be found.
To do so run the commands
magnopy-optimize-sd -s 5 5 1 -ss GROGU -sf CrI3/reference-GROGU.txt -of CrI3/optimize-sd-output-5-5-1 -hpd > console-output.txt
and
magnopy-optimize-sd -s 5 5 1 -ss GROGU -sf CrI3_U/reference-GROGU.txt -of CrI3_U/optimize-sd-output-5-5-1 -hpd > console-output.txt
in the terminal.
Input parameters¶
There is one extra parameter in the input
-s 5 5 1Tells magnopy to construct a \((5,5,1)\) supercell and minimize spin directions by varying all spins in the supercell independently.
Output files¶
The set of output files is the same
"console-output.txt"
Content of "console-output.txt" for CrI3¶███╗ ███╗ █████╗ ██████╗ ███╗ ██╗ ██████╗ ██████╗ ██╗ ██╗ ████╗ ████║ ██╔══██╗ ██╔════╝ ████╗ ██║ ██╔═══██╗ ██╔══██╗ ╚██╗ ██╔╝ ██╔████╔██║ ███████║ ██║ ███╗ ██╔██╗ ██║ ██║ ██║ ██████╔╝ ╚████╔╝ ██║╚██╔╝██║ ██╔══██║ ██║ ╚██║ ██║╚██╗██║ ██║ ██║ ██╔═══╝ ╚██╔╝ ██║ ╚═╝ ██║ ██║ ██║ ╚██████╔╝ ██║ ╚████║ ╚██████╔╝ ██║ ██║ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═══╝ ╚═════╝ ╚═╝ ╚═╝ Version: 0.2.0 ▄ ▄ Documentation: magnopy.org █▀█▀█ Release date: 10 September 2025 █▄█▄█ License: GNU GPLv3 ███ ▄▄ Copyright (C) 2023-2025 Magnopy Team ████ █ █ ████ █ Generated on 9 September 2025 at 23:4:55 ▀▀▀▀▀▀▀▀ ======================================== Comment ========================================= Source of the parameters is "GROGU". Loaded parameters of the spin Hamiltonian from the file ../reference-CrI3.txt. =================================== Start optimization =================================== Energy tolerance : 1.00000e-05 Torque tolerance : 1.00000e-05 Minimizing on the supercell of 5 x 5 x 1 unit cells. Spin directions of the initial guess are saved in file optimize-sd-5-5-1/INITIAL_GUESS.TXT ─────┬─────────────┬─────────────┬───────────── step │ E_0 │ delta E_0 │ max torque ─────┴─────────────┴─────────────┴───────────── 1 -4.2997262 21.7614347 6.7134939 2 -27.1825148 22.8827885 6.0206569 3 -35.3617801 8.1792653 6.0162225 4 -42.5621347 7.2003546 5.4268267 5 -56.0045195 13.4423848 6.3370665 6 -56.0045252 0.0000058 6.3362432 7 -87.6407002 31.6361749 5.7593768 8 -88.3563889 0.7156888 5.7846429 9 -100.1739820 11.8175931 5.1211304 10 -101.5796831 1.4057011 6.0596057 11 -101.7786546 0.1989715 6.0549243 12 -127.7228404 25.9441858 5.0631266 13 -132.4215392 4.6986988 5.0142450 14 -137.1066148 4.6850756 5.2603847 15 -147.5717663 10.4651515 5.6721924 16 -147.6662350 0.0944687 5.6766814 17 -151.9083924 4.2421574 6.7697215 18 -154.2579507 2.3495583 6.7172453 19 -159.2432655 4.9853149 6.0665398 20 -164.9741854 5.7309199 5.0560574 21 -167.8412794 2.8670940 3.7208655 22 -169.4036213 1.5623418 3.1167707 23 -169.8335485 0.4299273 3.0705308 24 -170.8796833 1.0461348 3.2646862 25 -171.4113740 0.5316907 3.1585270 26 -172.1112883 0.6999142 3.0911572 27 -173.4911334 1.3798451 2.7333877 28 -174.6061828 1.1150494 2.6027866 29 -175.3524362 0.7462534 2.3880930 30 -175.8415877 0.4891515 2.3299883 31 -176.0791719 0.2375842 2.3738489 32 -176.2771838 0.1980119 2.2959737 33 -176.4674301 0.1902463 2.3051216 34 -176.6391920 0.1717620 2.1853904 35 -176.8885296 0.2493376 2.1508827 36 -177.2004611 0.3119315 2.0949046 37 -177.5092586 0.3087975 1.8800318 38 -177.7966405 0.2873819 2.0695870 39 -178.3346456 0.5380051 1.8937316 40 -178.5626929 0.2280474 1.8757025 41 -178.7126409 0.1499480 1.7026264 42 -178.7712684 0.0586275 1.7245089 43 -178.8986248 0.1273564 1.5667002 44 -179.0461777 0.1475528 1.4295406 45 -179.4005426 0.3543649 1.3790892 46 -179.6358889 0.2353463 1.3464535 47 -179.6922946 0.0564057 1.1781532 48 -179.8324384 0.1401438 1.1246187 49 -179.8532105 0.0207721 1.0308996 50 -179.9732242 0.1200138 1.0010584 51 -180.1957284 0.2225041 0.8209580 52 -180.3671625 0.1714342 0.7240895 53 -180.4624182 0.0952557 0.6155472 54 -180.5189313 0.0565131 0.6135685 55 -180.5372850 0.0183537 0.5642690 56 -180.5578052 0.0205202 0.5568032 57 -180.5799319 0.0221267 0.4846338 58 -180.6039213 0.0239894 0.4549710 59 -180.6268547 0.0229333 0.3626663 60 -180.6502752 0.0234206 0.3180725 61 -180.6722607 0.0219855 0.2954520 62 -180.6905591 0.0182984 0.2522672 63 -180.7032956 0.0127365 0.2665947 64 -180.7108576 0.0075621 0.2702812 65 -180.7153394 0.0044817 0.2731192 66 -180.7195702 0.0042308 0.2666394 67 -180.7226838 0.0031137 0.2569368 68 -180.7281837 0.0054998 0.2298264 69 -180.7342166 0.0060329 0.2198324 70 -180.7432580 0.0090414 0.1876137 71 -180.7477284 0.0044703 0.1650126 72 -180.7522177 0.0044893 0.1274855 73 -180.7548154 0.0025977 0.1180502 74 -180.7559506 0.0011352 0.1150879 75 -180.7571462 0.0011956 0.1101553 76 -180.7586611 0.0015149 0.1163565 77 -180.7603627 0.0017016 0.1055264 78 -180.7626657 0.0023029 0.1048425 79 -180.7649908 0.0023251 0.0819246 80 -180.7669288 0.0019380 0.0804529 81 -180.7680436 0.0011148 0.0861852 82 -180.7687120 0.0006684 0.0862051 83 -180.7691216 0.0004096 0.0863792 84 -180.7694545 0.0003329 0.0842005 85 -180.7697885 0.0003340 0.0819499 86 -180.7702138 0.0004253 0.0770479 87 -180.7707790 0.0005652 0.0704376 88 -180.7715245 0.0007455 0.0596544 89 -180.7723180 0.0007935 0.0471583 90 -180.7730057 0.0006877 0.0438185 91 -180.7734429 0.0004373 0.0381752 92 -180.7737124 0.0002695 0.0366832 93 -180.7738838 0.0001714 0.0360071 94 -180.7740261 0.0001423 0.0333357 95 -180.7741792 0.0001531 0.0340281 96 -180.7743762 0.0001969 0.0302193 97 -180.7746346 0.0002584 0.0305531 98 -180.7749401 0.0003056 0.0261120 99 -180.7752209 0.0002807 0.0230898 100 -180.7754084 0.0001875 0.0173134 101 -180.7755104 0.0001020 0.0140525 102 -180.7755576 0.0000473 0.0132595 103 -180.7755830 0.0000254 0.0142076 104 -180.7755995 0.0000165 0.0138104 105 -180.7756143 0.0000148 0.0138754 106 -180.7756307 0.0000164 0.0126569 107 -180.7756506 0.0000200 0.0116430 108 -180.7756735 0.0000228 0.0091258 109 -180.7756947 0.0000212 0.0070447 110 -180.7757095 0.0000149 0.0050026 111 -180.7757176 0.0000080 0.0043280 112 -180.7757214 0.0000038 0.0040115 113 -180.7757233 0.0000019 0.0037404 114 -180.7757246 0.0000012 0.0034885 115 -180.7757256 0.0000011 0.0033980 116 -180.7757268 0.0000012 0.0032550 117 -180.7757282 0.0000014 0.0030433 118 -180.7757298 0.0000016 0.0025086 119 -180.7757313 0.0000015 0.0019780 120 -180.7757324 0.0000011 0.0013242 121 -180.7757330 0.0000006 0.0009445 122 -180.7757333 0.0000003 0.0008980 123 -180.7757334 0.0000001 0.0009901 124 -180.7757335 0.0000001 0.0009788 125 -180.7757336 0.0000001 0.0009866 126 -180.7757337 0.0000001 0.0009017 127 -180.7757338 0.0000001 0.0008169 128 -180.7757339 0.0000001 0.0006253 129 -180.7757340 0.0000001 0.0004564 130 -180.7757341 0.0000001 0.0003825 131 -180.7757342 0.0000000 0.0003181 132 -180.7757342 0.0000000 0.0003165 133 -180.7757342 0.0000000 0.0002741 134 -180.7757342 0.0000000 0.0002699 135 -180.7757342 0.0000000 0.0002684 136 -180.7757342 0.0000000 0.0002505 137 -180.7757342 0.0000000 0.0002522 138 -180.7757342 0.0000000 0.0001978 139 -180.7757342 0.0000000 0.0001710 140 -180.7757343 0.0000000 0.0001210 141 -180.7757343 0.0000000 0.0001021 142 -180.7757343 0.0000000 0.0001005 143 -180.7757343 0.0000000 0.0000892 144 -180.7757343 0.0000000 0.0000876 145 -180.7757343 0.0000000 0.0000767 146 -180.7757343 0.0000000 0.0000731 147 -180.7757343 0.0000000 0.0000637 148 -180.7757343 0.0000000 0.0000584 149 -180.7757343 0.0000000 0.0000459 150 -180.7757343 0.0000000 0.0000346 151 -180.7757343 0.0000000 0.0000243 152 -180.7757343 0.0000000 0.0000257 153 -180.7757343 0.0000000 0.0000238 154 -180.7757343 0.0000000 0.0000246 155 -180.7757343 0.0000000 0.0000226 156 -180.7757343 0.0000000 0.0000224 157 -180.7757343 0.0000000 0.0000187 158 -180.7757343 0.0000000 0.0000171 159 -180.7757343 0.0000000 0.0000130 160 -180.7757343 0.0000000 0.0000104 161 -180.7757343 0.0000000 0.0000073 ─────────────────────────────────────────────── Optimization is done. Classic ground state energy (E_0) is -180.775734 meV Optimized spin directions are saved in file optimize-sd-5-5-1/SPIN_DIRECTIONS.txt Spin positions are saved in file optimize-sd-5-5-1/SPIN_POSITIONS.txt Image of spin directions is saved in file optimize-sd-5-5-1/SPIN_DIRECTIONS.html ======================================== Finished ========================================"INITIAL_GUESS.txt"
"SPIN_POSITIONS.txt"
Content of "SPIN_POSITIONS.txt" for CrI3¶0.01290667 -0.00745002 15.00002010 3.55803919 2.03918089 15.00000948 7.10290667 -0.00745002 15.00002010 10.64803919 2.03918089 15.00000948 14.19290667 -0.00745002 15.00002010 17.73803919 2.03918089 15.00000948 21.28290667 -0.00745002 15.00002010 24.82803919 2.03918089 15.00000948 28.37290667 -0.00745002 15.00002010 31.91803919 2.03918089 15.00000948 -3.53209333 6.13267009 15.00002010 0.01303919 8.17930100 15.00000948 3.55790667 6.13267009 15.00002010 7.10303919 8.17930100 15.00000948 10.64790667 6.13267009 15.00002010 14.19303919 8.17930100 15.00000948 17.73790667 6.13267009 15.00002010 21.28303919 8.17930100 15.00000948 24.82790667 6.13267009 15.00002010 28.37303919 8.17930100 15.00000948 -7.07709333 12.27279020 15.00002010 -3.53196081 14.31942111 15.00000948 0.01290667 12.27279020 15.00002010 3.55803919 14.31942111 15.00000948 7.10290667 12.27279020 15.00002010 10.64803919 14.31942111 15.00000948 14.19290667 12.27279020 15.00002010 17.73803919 14.31942111 15.00000948 21.28290667 12.27279020 15.00002010 24.82803919 14.31942111 15.00000948 -10.62209333 18.41291031 15.00002010 -7.07696081 20.45954122 15.00000948 -3.53209333 18.41291031 15.00002010 0.01303919 20.45954122 15.00000948 3.55790667 18.41291031 15.00002010 7.10303919 20.45954122 15.00000948 10.64790667 18.41291031 15.00002010 14.19303919 20.45954122 15.00000948 17.73790667 18.41291031 15.00002010 21.28303919 20.45954122 15.00000948 -14.16709333 24.55303042 15.00002010 -10.62196081 26.59966133 15.00000948 -7.07709333 24.55303042 15.00002010 -3.53196081 26.59966133 15.00000948 0.01290667 24.55303042 15.00002010 3.55803919 26.59966133 15.00000948 7.10290667 24.55303042 15.00002010 10.64803919 26.59966133 15.00000948 14.19290667 24.55303042 15.00002010 17.73803919 26.59966133 15.00000948
"SPIN_DIRECTIONS.txt"
Content of "SPIN_DIRECTIONS.txt" for CrI3¶0.00226348 0.00123488 0.99999668 0.00198558 0.00075498 0.99999774 0.00226363 0.00123452 0.99999668 0.00198571 0.00075577 0.99999774 0.00226364 0.00123420 0.99999668 0.00198483 0.00075599 0.99999774 0.00226302 0.00123449 0.99999668 0.00198494 0.00075535 0.99999774 0.00226310 0.00123490 0.99999668 0.00198547 0.00075525 0.99999774 0.00226350 0.00123419 0.99999668 0.00198569 0.00075489 0.99999774 0.00226328 0.00123456 0.99999668 0.00198566 0.00075578 0.99999774 0.00226322 0.00123417 0.99999668 0.00198473 0.00075541 0.99999775 0.00226267 0.00123393 0.99999668 0.00198460 0.00075543 0.99999775 0.00226305 0.00123462 0.99999668 0.00198520 0.00075499 0.99999774 0.00226287 0.00123429 0.99999668 0.00198456 0.00075476 0.99999775 0.00226324 0.00123428 0.99999668 0.00198549 0.00075493 0.99999774 0.00226311 0.00123399 0.99999668 0.00198488 0.00075520 0.99999774 0.00226274 0.00123415 0.99999668 0.00198426 0.00075500 0.99999775 0.00226239 0.00123425 0.99999668 0.00198522 0.00075467 0.99999774 0.00226358 0.00123446 0.99999668 0.00198542 0.00075495 0.99999774 0.00226370 0.00123433 0.99999668 0.00198541 0.00075553 0.99999774 0.00226316 0.00123429 0.99999668 0.00198507 0.00075561 0.99999774 0.00226287 0.00123435 0.99999668 0.00198502 0.00075514 0.99999774 0.00226321 0.00123437 0.99999668 0.00198539 0.00075518 0.99999774 0.00226364 0.00123445 0.99999668 0.00198594 0.00075513 0.99999774 0.00226405 0.00123450 0.99999668 0.00198595 0.00075589 0.99999774 0.00226323 0.00123469 0.99999668 0.00198494 0.00075571 0.99999774 0.00226313 0.00123493 0.99999668 0.00198514 0.00075564 0.99999774 0.00226345 0.00123464 0.99999668 0.00198567 0.00075537 0.99999774
"SPIN_DIRECTIONS.html"
Download the files for "CrI3/reference-GROGU.txt"¶
INITIAL_GUESS.txt (for CrI3.txt)
Download the files for "CrI3_U/reference-GROGU.txt"¶
INITIAL_GUESS.txt (for CrI3_U.txt)
Total running time of the script: (0 minutes 0.005 seconds)