0

I'm trying to use scipy.fmin (docs here) and I can't figure out why it isn't working. My code:

def moveMolecule(array):
    x0 = [0, 0, 0]
    minV = fmin(calculateV,x0)

Where calculateV is:

def calculateV(array):
# Calculation of LJ potential using possible radii
# Returns potential in kJ/mol as an list with closest neighbor(s)
points = tuple(map(tuple, array))
radius = []
V = []

# Query tree for nearest neighbors using KD tree
# Returns indices of NN
for n in points:
    output = [i for i in points if i != n]
    tree = spatial.KDTree(output)
    index = tree.query_ball_point(x=n, r=3)
    for i in index:
        radius.append(euclideanDist(tree.data[i], n))

# Calculate potential for NNs
for r in radius:
    V.append((4 * _e) * (((_d / r) ** 12) - ((_d / r) ** 6)))
return V

I keep getting the error TypeError: 'numpy.float64' object is not iterable. CalculateV runs fine by itself. I thought the error was that I wasn't returning a function, so I tried doing:

# Calculate potential for NNs
for r in radius:
    val = ((4 * _e) * (((_d / r) ** 12) - ((_d / r) ** 6)))
    V.append(val)
return val

But I'm still getting the same error. Seems the issues is with:

points = tuple(map(tuple, array))

But I don't understand why. Any help would be greatly appreciated!

Monica
  • 27
  • 3
  • 8
  • I'm guessing your variable, array, is not an array, but a number of type np.float64 – Bob Haffner Nov 19 '16 at 17:49
  • I should have clarified--'calculateV' runs fine by itself but doesn't whenever I need to use it with fmin. That's why I'm confused – Monica Nov 19 '16 at 17:52
  • Oh ok, i didn't read carefully enough. What happens you change your fmin call to `minV = fmin((calculateV,array),x0)` – Bob Haffner Nov 19 '16 at 18:04
  • Also, please add an example of the data in array – Bob Haffner Nov 19 '16 at 18:17
  • No you're good--I edited it! – Monica Nov 19 '16 at 18:27
  • The data in the array is cartesian coordinates. so just integers in a 3xn array – Monica Nov 19 '16 at 18:27
  • If I do that it says "'tuple' object is not callable." So it doesn't like that format. I tried inputting array as args=array too and that didn't work – Monica Nov 19 '16 at 18:29
  • 1
    Your cost function to fmin needs to take a list of parameters and return a floating point value. You are giving it a function that takes a list of points and returns a list of floats. If what you are trying to adjust the x,y,z coords of a set of points, you will have to pass them in as a flat list, which you can then resize to 3xn inside your calculateV function. You will still need to convert your cost to single value, such as the total energy, not a list of potentials. – Neapolitan Nov 19 '16 at 18:35
  • Another issue: the 'array' parameter in moveMolecule(array) is not used. – Neapolitan Nov 19 '16 at 18:37
  • Assuming array is in moveMolecule is the initial set of points, set `x0 = np.reshape(array, -1)` in moveModelcule and set `points = np.reshape(array, (-1, 3))` in calculateV. – Neapolitan Nov 19 '16 at 18:43
  • Okay, that makes sense! Thank you! I'll have to work on it but I think I know where to go from here – Monica Nov 19 '16 at 18:50

0 Answers0