1
00:00:00,240 --> 00:00:07,450
So now that we have our function for calculating the energy, according to this formula here, we can

2
00:00:07,470 --> 00:00:13,860
calculate it really for every configuration and we can just call this function whenever we slightly

3
00:00:13,860 --> 00:00:18,720
change this configuration of the magnetic moments, and it will update the energy immediately.

4
00:00:19,350 --> 00:00:26,460
So we know if the energy decreases or increases, this means now we are ready to implement the actual

5
00:00:26,460 --> 00:00:27,880
Monte Carlo algorithm.

6
00:00:28,620 --> 00:00:31,170
And I was teasing this already to you.

7
00:00:31,680 --> 00:00:35,190
This is basically just a loop over several metropolis steps.

8
00:00:35,370 --> 00:00:41,520
So I think this lecture is really the most important one because here you really learn the core feature

9
00:00:41,550 --> 00:00:42,750
of the Monte Carlo.

10
00:00:43,800 --> 00:00:48,840
So I was reading this already briefly to you, but here it's in detail.

11
00:00:49,140 --> 00:00:54,090
So what happens is at first we will randomly select a magnetic moment.

12
00:00:54,960 --> 00:01:01,170
So right now we have a grid of 40 times 40 more moons, so 1600.

13
00:01:01,650 --> 00:01:07,890
So we will generate two random indices between zero and 40 and then select a single one of these.

14
00:01:08,190 --> 00:01:09,300
For example, this one.

15
00:01:10,380 --> 00:01:16,410
And then we go to the second step, which will be this particular moment is reoriented along just some

16
00:01:16,410 --> 00:01:17,430
random direction.

17
00:01:17,910 --> 00:01:24,270
So as an as in the beginning, we will once again generate a random and Delphi and the random angle

18
00:01:24,270 --> 00:01:24,810
Fita.

19
00:01:25,200 --> 00:01:31,080
And then we will just change the orientation of this moment completely and we will do this randomly.

20
00:01:32,490 --> 00:01:40,290
And what's very important is we must still store the old orientation for now, because now we will calculate

21
00:01:40,380 --> 00:01:44,880
the new energy and we will compare the energy with the old energy.

22
00:01:45,330 --> 00:01:51,330
So with the new moment and the old moment, and now we must use an if statement and if the energy is

23
00:01:51,330 --> 00:01:56,640
decreased by those random change, then the change of the magnetic moment is accepted.

24
00:01:56,880 --> 00:02:05,670
So we accept it and we can just delete the old magnetic moment and when the energy is increased.

25
00:02:06,060 --> 00:02:13,860
This is considered a bat metropolis step, so we will just ignore it and we will restore the old magnetic

26
00:02:13,860 --> 00:02:14,280
moment.

27
00:02:15,810 --> 00:02:20,700
OK, so we know from this algorithm that two things can happen.

28
00:02:21,270 --> 00:02:27,270
Either the energy is decreased, which is good, then the magnetization is updated or the magnetic moments

29
00:02:27,270 --> 00:02:33,030
are updated, or we try to change it and we see the energy is increasing.

30
00:02:33,030 --> 00:02:38,910
And then basically nothing happens because the old Sputnik moment is just restored.

31
00:02:40,200 --> 00:02:42,390
So let's go ahead and implement this.

32
00:02:43,350 --> 00:02:45,750
So we implement a single step.

33
00:02:46,500 --> 00:02:54,630
And since we will add later on more energy terms here, I will call this function here step exchange

34
00:02:56,550 --> 00:02:59,820
so that we know that we only consider the exchange interaction for now.

35
00:03:01,560 --> 00:03:08,190
So as an argument, we only need the magnetization and we begin with the first step here.

36
00:03:08,430 --> 00:03:10,260
A random magnetic moment is selected.

37
00:03:10,980 --> 00:03:13,680
That's of course, pretty similar to what we have done previously.

38
00:03:14,820 --> 00:03:23,210
First of all, we just generate two indices X and Y and P Dot Random Dot brand.

39
00:03:23,580 --> 00:03:29,400
And since it's an integer, we write around it and when we write length, then it will be an integer

40
00:03:29,400 --> 00:03:32,220
between zero and thirty nine in our case.

41
00:03:33,270 --> 00:03:37,950
Then we do the same thing for why, because it must be a different random number.

42
00:03:38,520 --> 00:03:45,340
And now we can write an energy old is equal to.

43
00:03:45,600 --> 00:03:51,420
And now we could, of course, just write energy exchange magnetization.

44
00:03:53,370 --> 00:03:56,220
So we could do this, as I said.

45
00:03:56,550 --> 00:04:00,060
But it turns out that this is not really the ideal thing to do.

46
00:04:00,180 --> 00:04:01,530
I will come back to this later.

47
00:04:01,530 --> 00:04:02,730
We will change this later on.

48
00:04:02,820 --> 00:04:05,280
It would work, but it's pretty slow to do it that way.

49
00:04:06,630 --> 00:04:08,820
So now we come to the second step.

50
00:04:09,330 --> 00:04:12,180
So this would be first and this would be second.

51
00:04:12,810 --> 00:04:15,450
And this is exactly as we did previously.

52
00:04:15,600 --> 00:04:17,160
So I can scroll up.

53
00:04:18,120 --> 00:04:21,180
We will load these random values here.

54
00:04:23,040 --> 00:04:31,260
So here in this case, we were generating an array of 40 times 40 random angles.

55
00:04:31,590 --> 00:04:33,330
And here we only need a single one.

56
00:04:33,780 --> 00:04:38,970
So basically, we can just say, delete all of this stuff and do it like this.

57
00:04:40,260 --> 00:04:44,640
So now we have just a single random value phi and a single random all your theta.

58
00:04:45,870 --> 00:04:50,640
And now we generate the magnetization.

59
00:04:52,140 --> 00:04:54,420
So this we can also do as we did before.

60
00:04:59,850 --> 00:05:04,800
And we can say magnetization.

61
00:05:05,940 --> 00:05:12,120
And now we must specify which of these moments we will change because we will not randomly recreate

62
00:05:12,120 --> 00:05:13,020
the whole era.

63
00:05:13,200 --> 00:05:21,120
We would just change a single one of them and we will address here the moment x y zero.

64
00:05:21,390 --> 00:05:28,410
So the moment with the coordinates x, y and zero and this will be the X, Y and Z coordinates, according

65
00:05:28,410 --> 00:05:36,480
to the X, Y and Z coordinates, according to our two angles random fi and random Fita that we have

66
00:05:36,480 --> 00:05:38,790
just randomly generated.

67
00:05:40,500 --> 00:05:48,030
So one more thing that is really important that I didn't wrote explicitly here, but you see we had

68
00:05:48,030 --> 00:05:53,730
to calculate our old energy and likewise we have to store our old magnetic moment.

69
00:05:54,990 --> 00:05:58,530
So I will write here, save mark.

70
00:06:00,570 --> 00:06:03,660
Is equal to and p dot array.

71
00:06:06,090 --> 00:06:08,640
And then the magnetization.

72
00:06:11,720 --> 00:06:14,060
X my zero.

73
00:06:15,260 --> 00:06:20,660
So basically, we take this one, we store it in a different variable save mark.

74
00:06:21,650 --> 00:06:29,000
And now you may wonder, why don't you just write the following Why don't you just write safe markers

75
00:06:29,000 --> 00:06:31,550
equal to magnetization?

76
00:06:32,270 --> 00:06:37,250
And to be fair to you, this is what I did in the beginning, but I noticed it doesn't work.

77
00:06:37,340 --> 00:06:39,950
And this is because of some pointer issue.

78
00:06:40,640 --> 00:06:49,430
So if I would ride it like this, then it would assign the same pointer to save mark as this one or

79
00:06:49,430 --> 00:06:50,470
the same idea.

80
00:06:50,480 --> 00:06:53,030
I saw the same ID in the storage.

81
00:06:53,750 --> 00:06:57,880
And what happens then when we change this value here?

82
00:06:57,890 --> 00:07:03,500
So basically, once we run this line of code, it also changes the safe mark variable.

83
00:07:03,920 --> 00:07:05,750
This is exactly what we don't want.

84
00:07:06,110 --> 00:07:08,780
We don't want to overwrite to save Mach variable.

85
00:07:08,780 --> 00:07:11,150
We want to have it as an independent variable.

86
00:07:11,960 --> 00:07:14,300
We want the right and pitot array of this.

87
00:07:14,300 --> 00:07:16,850
Then it saves it as an independent variable.

88
00:07:17,510 --> 00:07:24,260
So this was a bit surprising to me, to be fair, but it makes sense because this is how Python works

89
00:07:24,260 --> 00:07:27,140
with the IDs and the pointer, you could say.

90
00:07:27,890 --> 00:07:29,570
So let me right here.

91
00:07:29,570 --> 00:07:35,300
Just as a reminder problem with the IDs save macro.

92
00:07:35,870 --> 00:07:36,890
This would lead to.

93
00:07:38,700 --> 00:07:43,620
Save mark will be changed once.

94
00:07:46,480 --> 00:07:49,750
This change in the next line.

95
00:07:51,380 --> 00:07:53,120
OK, so that's just a comment.

96
00:07:53,990 --> 00:07:56,690
All right, so what we have now is we have the old energy.

97
00:07:56,750 --> 00:08:04,970
We have the old magnetic moment saved and we have generated a new magnetization, a new magnetic magnetic

98
00:08:04,970 --> 00:08:07,610
moment at at this particular point in space.

99
00:08:08,900 --> 00:08:10,820
Now we can calculate the new energy.

100
00:08:13,340 --> 00:08:14,330
So that's pretty easy.

101
00:08:14,660 --> 00:08:21,800
We just write energy new is equal to and then we can just copy this because we have updated Mac.

102
00:08:22,310 --> 00:08:24,350
So this will now be our new magnetization.

103
00:08:24,980 --> 00:08:27,770
So as I said, this would work, but it's pretty slow.

104
00:08:27,770 --> 00:08:30,050
So we will change this in a few minutes.

105
00:08:31,010 --> 00:08:37,309
But let's continue for now, because now comes the if statement, the fourth part where we must compare

106
00:08:37,309 --> 00:08:38,030
the energies.

107
00:08:38,909 --> 00:08:50,210
So as I said, if and then rewrite if the energy new is smaller than the energy old, then do the following.

108
00:08:51,620 --> 00:09:04,310
So we in this case accept the change and we update the energy and we can write basically energy

109
00:09:07,280 --> 00:09:07,730
change.

110
00:09:07,760 --> 00:09:10,490
Or we could say we will.

111
00:09:10,580 --> 00:09:13,580
We will come to this in a second because you are on to do something special.

112
00:09:14,000 --> 00:09:18,620
So I will write your energy just to remind us that we have to do something here still.

113
00:09:20,510 --> 00:09:23,450
So just comment this and.

114
00:09:25,470 --> 00:09:28,740
If this is not true, then we will use ours.

115
00:09:30,060 --> 00:09:37,740
This would mean we decline and restore hold moment.

116
00:09:42,870 --> 00:09:46,230
And we would then say magnetization.

117
00:09:47,990 --> 00:09:54,500
I have the position X-Y Zero is equal to our safe mark magnetization or the magnetic moment that we

118
00:09:54,500 --> 00:09:55,730
have saved previously.

119
00:09:57,410 --> 00:09:57,820
OK.

120
00:09:58,940 --> 00:10:06,890
And then we basically return the compact to magnetization.

121
00:10:06,890 --> 00:10:16,460
We could say, OK, so the thing is now in the next step, we will loop over this many, many times,

122
00:10:16,700 --> 00:10:18,500
maybe one million times.

123
00:10:19,310 --> 00:10:26,120
And you see, we calculate here the energy once we calculate it here another time and we do this one

124
00:10:26,120 --> 00:10:26,940
million times.

125
00:10:27,830 --> 00:10:34,220
And the thing is, it takes a lot of time to do this, especially if the array or if the number of moments

126
00:10:34,220 --> 00:10:35,810
that we consider is very large.

127
00:10:36,620 --> 00:10:44,570
And it doesn't really make sense to do this because in each of these steps, we only change one single

128
00:10:44,570 --> 00:10:45,470
magnetic moment.

129
00:10:45,620 --> 00:10:51,380
And if you look at it here, we just change one particular moment, and the only energy terms that are

130
00:10:51,380 --> 00:10:54,650
affected are the ones interacting with the four neighbors.

131
00:10:55,100 --> 00:11:00,290
So why should we calculate the energy of the whole remaining thing over and over again?

132
00:11:00,620 --> 00:11:04,580
This loses us so much time and slows down our coat so much.

133
00:11:05,330 --> 00:11:10,700
So at this point, I think it makes much more sense to not calculate the energy every time, but to

134
00:11:10,730 --> 00:11:13,400
calculate the change of the energy every time.

135
00:11:14,390 --> 00:11:21,500
So we will only store here the energy that corresponds to this particular moment that we will change

136
00:11:21,860 --> 00:11:24,020
and the interaction with the neighbors.

137
00:11:25,250 --> 00:11:27,050
So let me show you how this works.

138
00:11:27,620 --> 00:11:34,310
So we will store here energy old and now we will not store total energy, but we will only store the

139
00:11:34,310 --> 00:11:35,210
contribution.

140
00:11:36,500 --> 00:11:43,880
This is why I programmed this here in these two individual functions, so we can now say the old energy

141
00:11:43,880 --> 00:11:46,160
would be energy exchange contribution.

142
00:11:46,940 --> 00:11:52,100
And to be honest, or maybe this is a bit difficult now, but you see, we have here.

143
00:11:52,760 --> 00:11:53,180
Where is it?

144
00:11:53,180 --> 00:11:53,600
Sorry.

145
00:11:53,930 --> 00:11:56,960
Here it is, the sum over Paris.

146
00:11:57,290 --> 00:12:04,880
So, for example, one moment interacts with its right neighbor, but then in the sum, there is also

147
00:12:04,880 --> 00:12:08,630
determined occluded where the right neighbor interacts with the left neighbor again.

148
00:12:09,230 --> 00:12:12,200
So basically, all of these terms occur twice.

149
00:12:12,980 --> 00:12:19,670
So this is why when we change one particular moment, it will not just be the four terms with the neighbors,

150
00:12:20,030 --> 00:12:24,470
but also determine where the neighbors interact with the moment itself.

151
00:12:24,860 --> 00:12:27,740
And this will, of course, just give us a factor of two.

152
00:12:29,930 --> 00:12:36,710
So the old energy we should actually may be called as energy or will change, but let's let's leave

153
00:12:36,710 --> 00:12:40,670
it at that is energy exchange contribution times two.

154
00:12:41,450 --> 00:12:45,860
And here the same thing, of course, two times energy exchange.

155
00:12:46,400 --> 00:12:47,540
And that's it.

156
00:12:49,830 --> 00:12:55,650
OK, so now we compare the energies, this is all fine, and now the only thing that we have to do is

157
00:12:56,010 --> 00:13:02,100
we have to see how this the energy change, so we write energy.

158
00:13:04,140 --> 00:13:08,730
Change is equal to energy.

159
00:13:08,730 --> 00:13:11,520
New minus energy.

160
00:13:12,030 --> 00:13:12,420
Old.

161
00:13:15,100 --> 00:13:18,560
Because here we accept the change and we update the energy.

162
00:13:18,640 --> 00:13:19,750
So we have to write this.

163
00:13:20,260 --> 00:13:23,710
And here in this case, we have to say energy

164
00:13:26,200 --> 00:13:28,150
change is equal to zero.

165
00:13:28,690 --> 00:13:35,500
And then we must return not only the magnetization, but we must also return to change of the energy.

166
00:13:38,100 --> 00:13:45,300
So the thing is we could now even increase the speed of the code even further, because you see in every

167
00:13:45,300 --> 00:13:47,560
step we calculate here the old energy.

168
00:13:47,910 --> 00:13:49,810
And then we calculate the new energy.

169
00:13:50,340 --> 00:13:55,590
And then in the next step, we should already know what the new energy is, but we calculated once again.

170
00:13:56,250 --> 00:14:02,550
So we could save this by adding here another parameter, which is basically the current energy that

171
00:14:02,970 --> 00:14:04,380
is given here.

172
00:14:04,380 --> 00:14:09,690
But I think this would now be maybe a bit too much and a bit too much optimization.

173
00:14:10,020 --> 00:14:16,020
This would increase our speed of the code by a factor of two, which is, of course, nice, and we

174
00:14:16,020 --> 00:14:19,740
could do this later on to improve the code, but it's not really necessary here.

175
00:14:20,280 --> 00:14:26,910
What was really necessary or what was really, really useful, was using these contributions only because

176
00:14:26,910 --> 00:14:35,220
this really, really increases the speed of our code, because now the energy calculation will just

177
00:14:35,220 --> 00:14:39,030
contain for four terms here.

178
00:14:39,030 --> 00:14:46,350
And then when we calculate the whole thing, it would contain so, so many terms, it would contain

179
00:14:46,710 --> 00:14:50,520
four times the number of moments in our sample.

180
00:14:50,520 --> 00:14:56,250
So this would make our code especially slow if we would consider very, very large samples.

181
00:14:56,880 --> 00:15:02,250
So I think this change was really worth it for the other change is nice, but not really necessary.

182
00:15:03,270 --> 00:15:08,070
OK, so let's see if it worked because it was quite a lot of code here.

183
00:15:08,580 --> 00:15:12,720
I think it wasn't really that difficult because it's just what written what's written down here, but

184
00:15:12,720 --> 00:15:15,580
still could happen that I made some error.

185
00:15:15,600 --> 00:15:16,320
So let's see.

186
00:15:16,830 --> 00:15:23,730
We just right step exchange magnetization and it gives him that role, of course.

187
00:15:25,410 --> 00:15:26,550
So where's the error?

188
00:15:27,770 --> 00:15:29,400
Oh, yeah, of course here.

189
00:15:30,060 --> 00:15:33,750
So you see, the error messages are pretty good in Python.

190
00:15:34,380 --> 00:15:43,470
So it says the function energy exchange contribution that we call here misses two required positional

191
00:15:43,470 --> 00:15:45,870
arguments, which are called X and Y.

192
00:15:46,500 --> 00:15:48,890
And of course, this is because I have changed it.

193
00:15:48,900 --> 00:15:54,480
First, I wanted to calculate only the total energy, but now I only want to calculate the contribution.

194
00:15:54,660 --> 00:15:57,990
So we need the other two arguments here.

195
00:15:58,830 --> 00:16:03,090
So we just write X come away and here as well.

196
00:16:04,410 --> 00:16:07,980
So let's try again and we have another error.

197
00:16:11,820 --> 00:16:18,540
Energy exchange takes one positional argument, but three were given, I think, yeah, I missed here.

198
00:16:19,020 --> 00:16:20,700
The update for the contribution?

199
00:16:21,600 --> 00:16:21,990
OK.

200
00:16:23,700 --> 00:16:24,990
And this time it worked.

201
00:16:25,290 --> 00:16:25,660
Nice.

202
00:16:25,680 --> 00:16:31,800
OK, so we have no more output of stuff exchange Mac, which are these two values, so we could now

203
00:16:31,800 --> 00:16:39,630
write zero, which would be the updated magnetization or one which would be the change of the of the

204
00:16:39,630 --> 00:16:40,110
energy.

205
00:16:40,680 --> 00:16:42,690
So you see, in this case, the change is zero.

206
00:16:42,720 --> 00:16:49,150
This is probably because we generated a change that increased the energy, so we declined it.

207
00:16:49,170 --> 00:16:51,060
So here it would give us zero.

208
00:16:51,600 --> 00:16:57,750
And if we repeat this, OK, yeah, you see, now the energy decreased by zero point six five.

209
00:16:58,180 --> 00:16:59,100
Let's do it again.

210
00:16:59,250 --> 00:17:07,140
Zero declined, except that negative energy, except that negative energy, except the negative energy

211
00:17:07,560 --> 00:17:08,040
and so on.

212
00:17:08,160 --> 00:17:10,920
So you see the energy change either zero.

213
00:17:10,920 --> 00:17:17,280
This is when it's declined or it's negative where it's accepted, but it will never be positive.

214
00:17:18,450 --> 00:17:20,640
OK, so now this was the most difficult part.

215
00:17:20,880 --> 00:17:22,770
We have implemented the metropolis step.

216
00:17:23,160 --> 00:17:27,660
Now we can run the Monte-Carlo, which is really not, not much left to do.

217
00:17:27,660 --> 00:17:34,560
We will just loop over the metropolis up and then see how will this configuration change and how it

218
00:17:34,560 --> 00:17:36,240
will turn into a fairer mechanism.

