Skip to content

Commit e65a210

Browse files
committed
Added array copying hints
1 parent 1b79a78 commit e65a210

2 files changed

Lines changed: 77 additions & 1 deletion

File tree

CH40208/_toc.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,15 @@ parts:
8989
- file: monte_carlo/metropolis_monte_carlo.md
9090
- file: monte_carlo/the_metropolis_algorithm.md
9191
- file: monte_carlo/ising_model_simulation.md
92+
#- file: vectors_and_matrics/working_with_vectors_and_matrices.md
93+
#- file: comp_chem_methods/vectors_and_matrices
94+
#sections:
95+
#- file: comp_chem_methods/vectors
96+
#- file: comp_chem_methods/vectors_in_python
97+
#- file: comp_chem_methods/matrices
98+
#- file: comp_chem_methods/matrices_in_python
99+
#- file: comp_chem_methods/eigenvalues_and_eigenvectors
100+
#- file: comp_chem_methods/moments_of_inertia
92101
- caption: Example Coursework Notebooks
93102
chapters:
94103
- file: example_coursework_notebooks/ir_spectra.ipynb

CH40208/monte_carlo/ising_model_simulation.md

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
---
2+
jupytext:
3+
formats: md:myst
4+
text_representation:
5+
extension: .md
6+
format_name: myst
7+
kernelspec:
8+
display_name: Python 3
9+
language: python
10+
name: python3
11+
---
12+
113
## Monte Carlo Simulation of the 1D Ising Model
214

315
In this exercise, you'll implement a Monte Carlo simulation of a 1D Ising model with 4 spins and periodic boundary conditions. We'll build this up in steps, eventually calculating the mean magnetisation as a function of temperature.
@@ -45,14 +57,69 @@ Implement a single MC step that:
4557
2. Calculates $\Delta E$ for flipping this spin.
4658
3. Accepts/rejects the flip using the Metropolis criterion.
4759

48-
One way that you can select a random element from a numpy array is using `np.random.randint()`, which generates random integers from a specified range. By setting that range to the length of an array, you can use the result to pick a random array element.
60+
#### Python Hints
61+
62+
**1.** One way that you can select a random element from a numpy array is using `np.random.randint()`, which generates random integers from a specified range. By setting that range to the length of an array, you can use the result to pick a random array element.
4963

5064
```python
5165
my_array = np.array(['a', 'b', 'c', 'd']) # create an length-4 array
5266
random_index = np.random.randint(len(my_array)) # generate a random integer between 0 and 3
5367
print(my_array[random_index]) # print the corresponding array element
5468
```
5569

70+
**2.** Remember that a spin flip corresponds to changing a spin from $+1$ to $-1$, or from $-1$ to $+1$. In both cases, this corresponds to multiplying that element of your spin-state array by $-1$.
71+
72+
**3.** Creating copies of numpy arrays requires care:
73+
74+
```{code-cell} python
75+
import numpy as np
76+
77+
original_array = np.array(['a', 'b', 'c'])
78+
copied_array = original_array
79+
copied_array[0] = 'z'
80+
81+
print(f'original array: {original_array}')
82+
print(f'copied array: {copied_array}')
83+
```
84+
85+
Changing the first element in `copied_array` changed `original_array` too. Why does this happen?
86+
87+
The assignment `copied_array = original_array` does not create a new array. Instead, both it creates a new variable name that points at the _same_ data in memory. Because `original_array` and `copied_array` both point to the same data, any changes to one array appear to be mirrored by the other array.
88+
89+
For a true copy, use the `copy()` method:
90+
91+
```{code-cell} python
92+
original_array = np.array(['a', 'b', 'c'])
93+
copied_array = original_array.copy() # Create a true copy
94+
copied_array[0] = 'z'
95+
96+
print(f'original array: {original_array}')
97+
print(f'copied array: {copied_array}')
98+
```
99+
100+
For the Ising model, you'll need to:
101+
1. Try flipping a spin
102+
2. Calculate the energy change
103+
3. Accept the flip or keep the previous state
104+
105+
Without `copy()`, you can run into trouble:
106+
107+
```{code-cell} python
108+
# Problem: both variables point to same data
109+
spins = np.ones(4)
110+
new_spins = spins # Seems like we are copying the spin state, but we are not
111+
new_spins[0] *= -1 # Flip a spin
112+
print(f'proposed move: {new_spins}')
113+
print(f'old spins: {spins}') # Changed too!
114+
115+
# Solution: make a true copy
116+
spins = np.ones(4)
117+
new_spins = spins.copy()
118+
new_spins[0] *= -1
119+
print(f'proposed move: {new_spins}')
120+
print(f'old spins: {spins}') # Stays unchanged
121+
```
122+
56123
### Part 3: Magnetisation
57124

58125
The magnetisation is simply the sum of the spins. Implement a function to calculate the absolute magnetisation per spin. Your function should take a single numpy array describing the spin-state as the argument.

0 commit comments

Comments
 (0)