So now everything is prepared.
We have our array of the coordinates and of the magnetic moments that we have generated randomly and
we have defined our function for a single metropolis step, which includes the information about the
energy that we have implemented according to the exchange interaction.
So now we can go ahead and run the actual one to kind of.
So I said already, this is pretty simple.
It's just a loop, basically.
So we must specify how many steps we will do.
So a number of steps and we can do really many of those.
We will go up to a million other things.
So let's start with 10K and then we loop for I in range.
No steps.
And then what we do is we just write this one.
And the first output or with the zero index will be the magnetization, which is updated.
And the second outputs with the index one will be the energy so and g change.
And this will be like this.
OK, so the thing is, we don't really need the information about the change of the energy because the
way that reprogrammed it is that we calculate the energy two times anyway, so we don't need to load
it here.
So this means we don't actually have to have the information here about the energy.
So it turns out that this would already be sufficient.
And the only thing the energy information is nice forward to really track our progress.
And so I will write here that this is optional.
So it is optional in the sense that we just write the energy in the beginning is just an energy exchange
off mark.
So this is really calculating once the total energy of the whole sample.
And then we will store your energy list, as is an empty list.
Or we could also add the particular value here from the start, but we could also make it empty for
the beginning.
And now we say in the loop, it's also optional.
Of course, we you update the list or we will add the values to the list.
So we say energy is equal to energy plus energy change and then energy list dot append energy.
But as I said, these took two months.
Commands are optional and these two commands are optional, basically, this one is the only one we
need.
OK, so now let's run it and you see it's already finished.
These are 10000 loops.
And let's see what happens.
And before we look at the magnetization, let us look at the energy.
So Peel Dot plot will be range 40.
So for for the x axis, this will be just indices or in the index for the number of to step.
So range no steps.
And then we write here plus one because we included the starting value here and for the y axis we plot,
of course, the energy so energy list.
And you see, this is now the energy and this is the number of the step.
So I will not add labels here.
You can do this yourself.
It's pretty easy to do.
We have done it so many times already and you see at the beginning, we start at an energy that's very
close to zero because at the beginning everything was randomly.
And then the energy decreases and decreases and decreases.
And sometimes it stays maybe constant as here, but it never increases.
And you see, we really go to a very, very negative energy here.
So it looks pretty good.
But also, you see that the energy is still decreasing.
So it seems as if the simulation isn't finished yet.
So let's see what the profile actually looks like.
So I will just scroll up here and copy this code from the plot.
And since we have updated the magnetization, we can just run it as it is.
And here is our new magnetization.
So I think it doesn't look as confusing as before, but it doesn't look like a ferromagnetic yet.
So probably it's just not finished yet.
So I would say, let's go to 100000 steps, and if we would run rather just right away, we would continue
from this point.
This would also be OK, but I want to start from the beginning, so I just restart and run the whole
notebook.
So now, of course, it takes a bit of time.
And here you see on the energy once again, starting at zero and you see this time we approach a stage
where the energy almost remains constant, so it still keeps on decreasing a bit.
So maybe we are not perfectly at the minimum yet, but it looks much better this time.
But still, it is kind of difficult.
Still, it looks non linear, but we have here some areas where it is already quite thorough magnetic.
So maybe the problem is just that this system size is still too large and we could not just increase
the number of steps, but maybe it's easier to just decrease the size of the system and go to a length
equal to 20.
So once again, restart and run all.
OK.
Still didn't converge, so you see, it sometimes is really tricky to arrive at a good solution in the
Monte Carlo because everything is totally random and it's never guaranteed that you get a good result.
You have to really play with the parameters and run these things many times.
So you see, I have added here another zero.
We are now at one million steps and this will now probably take a few seconds.
But let's see.
We are now running the loop.
And it's still running, so you see, it's not anymore a question of a few seconds, but it takes a
bit of time.
But I think should finish pretty soon.
And then I hope we will get a good result this time.
And you see here, the line is pretty much straight at the end.
So I'm pretty confident that this time it works.
Better still didn't work out.
Oh man.
OK, but you see here it's it's getting somewhere very audits.
So in neighboring moments are almost parallel here.
So it looks pretty good already, but not perfect.
So the thing is, you now have to either increase the number of steps even further or you have to just
rerun the whole thing many, many times and then look every time at the energy.
So for example, here what we can do is we can see an energy list and then minus one, which will be
the last entry from the energy list.
And we can now also check if we did everything correctly by writing Energy mix change.
Not sorry, not with these brackets, but with these ones.
Yeah, OK, at least we did everything correctly.
So on the magnetization, the energy is updated every time in our list and we did it correctly because
when we recalculate the energy at the end, it gives us the correct results.
So we what we would have to do is we would have to rerun this whole simulation many, many times.
And then the correct solution would be the one with the lowest energy.
So I will not stop the recording and run this a few times and see if I will get a better result in minus
seven six three.
So I have rerun this whole thing several times, and the best result that I got was an energy, which
is even lower.
It's minus seven seven nine.
From our initial thought about the energy, we can actually tell what the correct solution would be.
It would be minus eight hundreds.
So we are still pretty far from the correct solution.
And you see, this is what we end up with.
It looks pretty ferromagnetic at some regions, for example, here looks ferromagnetic even here.
But I think the main problem is that we always end up with some strange structure like this world here
and these worlds, and they are actually very hard to get rid of.
So just by changing here, a single magnetic moment will never really help that much to get really to
the energetic minimum.
And this is the problem of our very first version of the of the Monte Carlo algorithm.
It is that we can get stuck in some energetic minimum that is just a local minimum.
So it describes some rather stable configuration, but not the actual global energetic minimum.
And so in the following lecture, I will show you an update to our code that helps us to better converge
the result and to get better results and also in the shorter amount of time.
So let's stay tuned and let's improve our code in the next lecture.