dask / dask-tutorial

Dask tutorial
https://tutorial.dask.org
BSD 3-Clause "New" or "Revised" License
1.83k stars 702 forks source link

03_array Lennard Jones potential - NameError: name 'potential' is not defined #230

Closed jnywong closed 2 years ago

jnywong commented 2 years ago

What happened:

The user-defined function potential is not recognised during %time potential(cluster) in the non-Dask, numpy version. I think the problem arises in the function distances that is called within the potential function.

Minimal Complete Verifiable Example:


import numpy as np

# make a random collection of particles
def make_cluster(natoms, radius=40, seed=1981):
    np.random.seed(seed)
    cluster = np.random.normal(0, radius, (natoms,3))-0.5
    return cluster

def lj(r2):
    sr6 = (1./r2)**3
    pot = 4.*(sr6*sr6 - sr6)
    return pot

# build the matrix of distances
def distances(cluster):
    diff = cluster[:, np.newaxis, :] - cluster[np.newaxis, :, :]
    mat = (diff*diff).sum(-1)
    return mat

# the lj function is evaluated over the upper triangle
# after removing distances near zero
def potential(cluster):
    d2 = distances(cluster)
    dtri = np.triu(d2)
    energy = lj(dtri[dtri > 1e-6]).sum()
    return energy

cluster = make_cluster(int(7e3), radius=500)

%time potential(cluster)

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<timed eval> in <module>

NameError: name 'potential' is not defined

Environment:

I am using the binder notebook

jsignell commented 2 years ago

We have removed this example from the notebook, so I am closing this issue. But thank you for writing it up!