<jupyter_start><jupyter_text>Now that you've worked through the Python examples, you have some idea of how to save things as files and run them.We'll work through Schroeder's 2D Ising model today in class. You have most of the tools. Three things missing:1) range vs. xrange: We use range to make a list of numbers, and we then loop through that with a for loop. In this code, we'll make a list of a *lot* of numbers (N**2 or bigger, for N == 1000). In those cases, you need to use xrange. It's exactly the same as range except that it doesn't burn up the memory to make the list all at once. Making the lists involved here can easily burn up gigs of memory.2) plotting. Plotting is fairly easy, but you have enough on your plate with learning Python right now. So, I've written the colorsquare routine for you. The meat is the "pylab.imshow(s,interpolation='nearest',cmap=cm.Greys_r)" line, which looks like what we used before. The cmap argument tells it to use a specific color map to make everything come out in black and white. The rest of the code makes it update the figure nicely and keeps it from doing so too often in big simulations.2) The raw_input line at the very end tells python to ask for use input. This keeps it from closing the plot window.<jupyter_code>import numpy as np
import pylab
from matplotlib import pylab as plt
from numpy.random import random #import only one function from somewhere
from numpy.random import randint
import scipy
from time import sleep
size = 10 # lattice length
T = 2.5 # in units of epsilon/k

import matplotlib.cm as cm

def initialize():
    """
    Initialize a random array where our spins are all up or down.
    """
    myarray = random([size,size]) # initializes with random numbers from 0 to 1.
    myarray[myarray<0.5] = -1
    myarray[myarray>=0.5] = 1

    fig = pylab.figure()
    ax = ax = fig.add_subplot(111)    
    
    colorsquare(myarray,fig=fig)
    
    return myarray,fig,ax

def deltaU(i,j):
    """
    Compute delta U of flipping a given dipole at i,j
    Note periodic boundary conditions.
    """
    if i == size - 1: above = s[0,j]
    else:             above = s[i+1,j]

    if i == 0:        below = s[-1,j] # s[size,j]
    else:             below = s[i-1,j]

    if j == size - 1: right = s[i,0]
    else:             right = s[i,j+1]

    if j == 0:        left = s[i,-1]
    else:             left = s[i,j-1]

    return 2*s[i,j]*(above+below+left+right)
colorcounter = 0
def colorsquare(s,showevery=None,*,fig):
    global colorcounter
    delay = 0.0
    if showevery is None:
        if size <= 10:
            showevery = 1
            delay = 5
        elif size <= 100:
            showevery = int(size*size/2)
        else:
            showevery = size*size
    if divmod(colorcounter,showevery)[1] == 0:
        fig.clear()
        pylab.imshow(s,interpolation='nearest',cmap=cm.Greys_r)
        fig.canvas.draw()
        #sleep(delay)
        pylab.draw()
    colorcounter = colorcounter + 1

s,fig,ax = initialize()

numtrials = 100*size**2
print ("numtrials",numtrials)
plt.show()<jupyter_output>numtrials 10000
{'fraction_non_alphanumeric': 0.0901481004507405, 'fraction_numerical': 0.019317450096587252, 'mean_word_length': 2.8644278606965172, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '647939', 'license': 'MIT', 'n_tokens_mistral': 1008, 'n_tokens_neox': 953, 'n_words': 403, 'path': 'InNZ/Schroeder 2D Ising.ipynb', 'repo_name': 'mglerner/NZClimateCourse', 'size': '3106'}