How to write a data file in Python

In summary, a user is seeking help with writing data for the Lorenz Equations in Python. They have created a Runge-Kutta script but are having trouble saving the data in a .dat file. They have tried using imported routines and hstack, but the script is taking too long to execute. They are also having difficulty saving the data in a single file instead of multiple files. They are seeking advice on how to improve the efficiency and effectiveness of their script.
  • #1
javiergra24
19
0
Hi guys

I've done a Runge-Kutta script for the Lorenz Equations in python, I need to write data for (t,x,y,z) in a .dat file in order to reprint in a table. How can a do it?

Imported routines are:

import sys, pylab, numpy
from pylab import *
from mpl_toolkits.mplot3d import Axes3D
from numpy import *
import matplotlib.pyplot as plt

Mi iteration code is:

for n in xrange(0,N):
# at each time step calculate new x(t),y(t),z(t)
# and append to lists x0, y0, z0
x,y,z = RKTwoD(x0[n],y0[n],z0[n],VDPXDot,VDPYDot,VDPZDot,dt)
x0.append(x)
y0.append(y)
z0.append(z)
t.append(t[n] + dt)
savetxt('output.dat', (t,x0,y0,z0))


But it doesn't work, file just contain six columns instead of four and it takes too long to execute the script.


Please can anyone help me?

Thank you
 
Technology news on Phys.org
  • #2
If you keep everything as an array instead of a list, basically take
x0.append(x)
y0.append(y)
z0.append(z)

and do a vstack or hstack at the end
so table = hstack(x0,y0,z0)
then you just need to write out
table.tofile('output.dat')

numpy lists don't write out straight to dats
 
  • #3
Thanks for your answer but it doesn't work

I'm trying

Code:
import sys, pylab, numpy
from pylab import *
from mpl_toolkits.mplot3d import Axes3D
from numpy import *
import matplotlib.pyplot as plt
#I'll use values of sigma = 6.0, beta = 3.0 and rho = 36
# The Lorenz Equations are dx/dt=sigma*(y-x) ; dy/dt=x*(rho-z)-y ; dz/dt=x*y-beta*z
def VDPXDot(x,y,z):
	return 6.0 * (y-x)

def VDPYDot(x,y,z):
	return x * (40.0 - z)- y

def VDPZDot(x,y,z):
	return x * y - 3.0 * z

# 3D Fourth-Order Runge-Kutta Integrator
def RKTwoD(x,y,z,f,g,m,dt):
	#9 operaciones
        k1x = dt * f(x,y,z)
	k1y = dt * g(x,y,z)
        k1z = dt * m(x,y,z)
	#15 operaciones
        k2x = dt * f(x + k1x / 2.0,y + k1y / 2.0,z + k1z / 2.0)
	k2y = dt * g(x + k1x / 2.0,y + k1y / 2.0,z + k1z / 2.0)
        k2z = dt * m(x + k1x / 2.0,y + k1y / 2.0,z + k1z / 2.0)
        #15 operaciones
        k3x = dt * f(x + k2x / 2.0,y + k2y / 2.0,z + k2z / 2.0)
	k3y = dt * g(x + k2x / 2.0,y + k2y / 2.0,z + k2z / 2.0)
        k3z = dt * m(x + k2x / 2.0,y + k2y / 2.0,z + k2z / 2.0)
	#12 operaciones
        k4x = dt * f(x + k3x,y + k3y,z + k3z)
	k4y = dt * g(x + k3x,y + k3y,z + k3z)
        k4z = dt * m(x + k3x,y + k3y,z + k3z)
        #12 operaciones
	x = x + ( k1x + 2.0 * k2x + 2.0 * k3x + k4x ) / 6.0
	y = y + ( k1y + 2.0 * k2y + 2.0 * k3y + k4y ) / 6.0
        z = z + ( k1z + 2.0 * k2z + 2.0 * k3z + k4z ) / 6.0
	return x,y,z

dt = 0.002
x0 = [0.0]
y0 = [1.0]
z0 = [0.0]
t  = [0.0]
N = 20000
for n in xrange(0,N):
  # at each time step calculate new x(t),y(t),z(t)
  # and append to lists x0, y0, z0
  x,y,z = RKTwoD(x0[n],y0[n],z0[n],VDPXDot,VDPYDot,VDPZDot,dt)
  x0.append(x)
  y0.append(y)
  z0.append(z)

 t.append(t[n] + dt)
  m1 = array(x0)
  m2 = array(y0)
  m3 = array(z0)
  m4 = array(t)
  m1.tofile("putamierdax.dat", sep='\n', format = "%e")
  m2.tofile("putamierday.dat", sep='\n', format = "%e") 
  m3.tofile("putamierdaz.dat", sep='\n', format = "%e")
It works but the script time is too long and I've got to work with three files
 
  • #4
My understanding is that Python is an interpreted language, so if you have a loop that runs for many iterations, it's going to take a long time. The way around that is to use a language that is compiled, such as C or C++.

I don't know enough about Python to say anything intelligible about your file I/O problem...
 
  • #5
Thanks for your answer Mark44

No matter if the script takes too much time (I'm doing the script also for C++ and Java), but there must be some way to save all data in a unique file. By splitting it in four I have to use Ultraedit and it's a bit tiring..
 
  • #6
look into numpy.savetxt(...)
 
  • #7
Thanks for your answer but it doesn't work
Sorry, I gave you the wrong args. It should be:
table = hstack([x0,y0,z0,t])

but when you open it, you need to specify the size of the array and make sure it's saved and unpacked as the right type.
you can also look into scipy.io.savemat

javiergra24 said:
It works but the script time is too long and I've got to work with three files
I just ran your script and it took supershort, so I'm thinking the bug is that you're running this:
Code:
  m1 = array(x0)
  m2 = array(y0)
  m3 = array(z0)
  m4 = array(t)
  m1.tofile("putamierdax.dat", sep='\n', format = "%e")
  m2.tofile("putamierday.dat", sep='\n', format = "%e") 
  m3.tofile("putamierdaz.dat", sep='\n', format = "%e")
in the for loop.
 
Last edited:
  • #8
story645 said:
Sorry, I gave you the wrong args. It should be:
table = hstack([x0,y0,z0,t])

but when you open it, you need to specify the size of the array and make sure it's saved and unpacked as the right type.
you can also look into scipy.io.savemat


I just ran your script and it took supershort, so I'm thinking the bug is that you're running this:
Code:
  m1 = array(x0)
  m2 = array(y0)
  m3 = array(z0)
  m4 = array(t)
  m1.tofile("putamierdax.dat", sep='\n', format = "%e")
  m2.tofile("putamierday.dat", sep='\n', format = "%e") 
  m3.tofile("putamierdaz.dat", sep='\n', format = "%e")
in the for loop.

How can I run the data saving script outside the for loop?

Thank you
 
  • #9
javiergra24 said:
How can I run the data saving script outside the for loop?

Just unindent it, so:
Code:
for n in xrange(0,N):
    x,y,z = RKTwoD(x0[n],y0[n],z0[n],VDPXDot,VDPYDot,VDPZDot,dt)
    x0.append(x)
    y0.append(y)
    z0.append(z)
    t.append(t[n] + dt)

m1 = array(x0)
m2 = array(y0)
m3 = array(z0)
m4 = array(t)
m1.tofile("putamierdax.dat", sep='\n', format = "%e")
m2.tofile("putamierday.dat", sep='\n', format = "%e") 
m3.tofile("putamierdaz.dat", sep='\n', format = "%e")
[/code]
 
  • #10
story645 said:
Just unindent it, so:
Code:
for n in xrange(0,N):
    x,y,z = RKTwoD(x0[n],y0[n],z0[n],VDPXDot,VDPYDot,VDPZDot,dt)
    x0.append(x)
    y0.append(y)
    z0.append(z)
    t.append(t[n] + dt)

m1 = array(x0)
m2 = array(y0)
m3 = array(z0)
m4 = array(t)
m1.tofile("putamierdax.dat", sep='\n', format = "%e")
m2.tofile("putamierday.dat", sep='\n', format = "%e") 
m3.tofile("putamierdaz.dat", sep='\n', format = "%e")
[/code]

Thank you very much! So simple and I didn't notice
 

1. How do I create a data file in Python?

In order to create a data file in Python, you will need to use the built-in open() function. This function takes in two arguments: the name of the file you want to create, and the mode in which you want to open the file. For example, to create a new file called my_data.txt in write mode, you would use the following code: f = open("my_data.txt", "w").

2. How do I write data to a file in Python?

Once you have opened a file in write mode, you can use the write() function to write data to the file. This function takes in a string as an argument and writes it to the file. For example, if you wanted to write the string "Hello World!" to your my_data.txt file, you would use the code f.write("Hello World!"). Don't forget to close the file after you are done writing to it with the close() function.

3. How do I add data to an existing file in Python?

In order to add data to an existing file, you will need to open the file in "append" mode instead of "write" mode. This will allow you to add new data to the end of the file without overwriting any existing data. To open a file in append mode, you would use the code f = open("my_data.txt", "a"). Then, you can use the write() function to add new data to the end of the file.

4. How do I read data from a file in Python?

To read data from a file in Python, you will need to use the read() function. This function takes in an optional argument that specifies the number of characters to read from the file. If no argument is given, it will read the entire file. For example, if you wanted to read the first 10 characters from my_data.txt, you would use the code f.read(10). Don't forget to close the file after you are done reading from it.

5. How do I save data to a specific format in a data file using Python?

In order to save data to a specific format, you will need to use the format() function. This function takes in a string as an argument and formats it according to the specified format. For example, if you wanted to save a number to your data file as currency, you would use the code f.write("The price is: ${}".format(10.50)). This will format the number to include a dollar sign and two decimal places. You can also use this function to save data in other formats such as dates or percentages.

Similar threads

  • Programming and Computer Science
Replies
14
Views
499
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
7
Views
3K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
16
Views
3K
Replies
3
Views
3K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
4
Views
4K
Back
Top