0

I've got a script that takes the output of a separate C++ executable and creates a scatter plot/bifurcation diagram of the resulting data. The application context is to look at angle values versus the driving force by iterating through multiple values of a driving force to get the resulting angle and stroboscopically sampling the results, as a problem regarding a nonlinearly damped driven pendulum from a course on computational physics

import os
import sys
import numpy as np
import matplotlib.pyplot as plt

gbl      = 1.0
kappa    = 0.5
T_D      = 9.424778
ic_ang   = 0.1
ic_avel  = 0.0
t_final  = 200
Nstep    = 7500
method   = "runge_kutta"

ic_ang   = 0.1
Fmin     = 0.8
Fmax     = 1.6
F_D      = float(Fmin)

tstep    = T_D/(t_final/Nstep)
Nrep     = 3 * tstep
select   =[]
step     = 0.01
Nite     = (Fmax-Fmin)/step
rng      = int(Nite-1)

for i in range(rng):
    pfile= open('param.dat','w')
    pfile.write('%f %f %f %f\n' %(gbl,kappa,F_D,T_D))
    pfile.write('%f %f %f\n'%(ic_ang,ic_avel,t_final))
    pfile.write('%d %s\n'%(Nstep,method))
    pfile.close()

os.system('./a.out > bif.log')

with open("data.out",'r') as datafile:
    data=datafile.readlines()

select=data[-Nrep:Nstep:int(tstep)]

for j in select:
    plt.plot(F_D, j, "o", color='b', markersize=0.3)
    print(F_D,j)
F_D += step
plt.xlabel(r'$F_D$')
plt.ylabel(r'$\theta_{repeat}$')
#plt.xticks([])
plt.yticks([])
plt.show()

However, when I try to run the script I get

Traceback (most recent call last):
File "bif.py", line 45, in <module>
plt.plot(F_D, j, "o", color='b',markersize=0.3)

File"/System/Library/Frameworks/Python.framework/Versions/2.7/
Extras/lib/pyt hon/matplotlib/pyplot.py", line 2987, in plot
ret = ax.plot(*args, **kwargs)

File "/System/Library/Frameworks/Python.framework/Versions/2.7/
Extras/lib/python/matplotlib/axes.py", line 4138, in plot
self.add_line(line)

File "/System/Library/Frameworks/Python.framework/Versions/2.7/
Extras/lib/python/matplotlib/axes.py", line 1497, in add_line
self._update_line_limits(line)

File "/System/Library/Frameworks/Python.framework/Versions/2.7/
Extras/lib/python/matplotlib/axes.py", line 1508, in 
_update_line_limits
path = line.get_path()

File "/System/Library/Frameworks/Python.framework/Versions/2.7/
Extras/lib/python/matplotlib/lines.py", line 743, in get_path
self.recache()

File "/System/Library/Frameworks/Python.framework/Versions/2.7/
Extras/lib/python/matplotlib/lines.py", line 429, in recache
y = np.asarray(yconv, np.float_)

File "/System/Library/Frameworks/Python.framework/Versions/2.7/
Extras/lib/python/numpy/core/numeric.py", line 460, in asarray
return array(a, dtype, copy=False, order=order)

ValueError: invalid literal for float(): 0 0.1 0 0.004995834722

Modifying some of the values to try and debug the script raises a separate exception

Traceback (most recent call last):
File "bif.py", line 24, in <module>
tstep    = T_D/(t_final/Nstep)
ZeroDivisionError: float division by zero

I am extremely new to Python so neither one of these exceptions makes much sense to me. However, as Nstep, t_final, and T_D all have finite values, there is no reason (that I can see anyhow) for a dividing by zero error.

I see possible errors for the ValueError as well, as the output in the 1st and 3rd columns (time and angular velocity) aren't float values as they should be. I don't, however, know why these values aren't being converted to a float as they should be.

Any help would be very much appreciated.

EDIT:THIS ISSUE HAS BEEN SOLVED

  • Error messages are usually directly telling you what's wrong. So `0 0.1 0 0.004995834722` is clearly not a float, but rather a long string. For the second error, `200/7500` results in `0` in python 2, if that is what you are using. – ImportanceOfBeingErnest Feb 12 '18 at 16:57

1 Answers1

0

I think you're asking two questions here, and as I can see the last one about division by zero is the easier one. Namely, the expression t_final/Nstep, as it stands now in your code, is an integer division expression, and the result is 0. Thus the line

tstep    = T_D/(t_final/Nstep)

divides by zero.

The second question is why matplotlib complains about the data. To really diagnose this problem we need to look at the content of the data file read by your program. However, I think the problem stems from your attempt to pass text (Python string) to a function expecting numeric data type. When you readlines() the input file, I don't think you're doing any conversion. As a result, a slice of text string is passed to plt.plot and matplotlib struggled to construct a numeric data type from this representation. It would be much better if you read the data, do the proper conversion according to the file format and the logic of your analysis. You may want to look into numpy.loadtxt if it's the case that you're dealing with a text data file.

Cong Ma
  • 10,692
  • 3
  • 31
  • 47
  • Thank you for your reply sir. I fixed the divide by zero error. Like you said I overlooked my input variables and was doing integer division. Stupid mistake on my part. For the second question, I think I understand what you are getting at. Data.out is a text array of numeric values, so if I add a float conversion command for my datafile.readlines(), it should convert the entirety of the array to float, and then I can analyze the data as is, right? I apologize if this is a poor question, as I stated earlier I am extremely new to programming in python. – Zack Thompson Feb 12 '18 at 17:09