TimeSeries problem

Asked by Praveen C

I am trying to store data into a TimeSeries, retrieve it, modify and then store it back. But this is not working correctly. Below is a simple example.

from dolfin import *

mesh = Interval(3, 0, pi)

# Create Function Space

Q = FunctionSpace(mesh, "CG", 1)
u = Function(Q)

# Parameters
T = 2
nt = T
dt = T/nt

# Time Stepping
time_u = TimeSeries("U")

uex = Expression("sin(t)*sin(x[0])+cos(t)*sin(x[0])",t=0)

t = 0
while t <= T:
   uex.t = t
   u = project(uex, Q)
   time_u.store(u.vector(),t)
   t += dt

it = 0
while it < 5:
    t = 0
    while t <= T:
       print "Iter=", it, ", t = ", t
       time_u.retrieve(u.vector(),t)
       # Do something to u and store it back
       time_u.store(u.vector(),t)
       print u.vector().array()
       t += dt
    it += 1

I repeatedly retrieve and store u back to the TimeSeries. The output shows that there is something wrong

Iter= 0 , t = 0
[ 7.95138670e-17 8.66025404e-01 8.66025404e-01 0.00000000e+00]
Iter= 0 , t = 1
[ 7.95138670e-17 1.19665077e+00 1.19665077e+00 0.00000000e+00]
Iter= 0 , t = 2
[ 0. 0.42708094 0.42708094 0. ]
Iter= 1 , t = 0
[ 7.95138670e-17 8.66025404e-01 8.66025404e-01 0.00000000e+00]
Iter= 1 , t = 1
[ 7.95138670e-17 1.19665077e+00 1.19665077e+00 0.00000000e+00]
Iter= 1 , t = 2
[ 7.95138670e-17 1.19665077e+00 1.19665077e+00 0.00000000e+00]
Iter= 2 , t = 0
[ 7.95138670e-17 8.66025404e-01 8.66025404e-01 0.00000000e+00]
Iter= 2 , t = 1
[ 7.95138670e-17 1.19665077e+00 1.19665077e+00 0.00000000e+00]
Iter= 2 , t = 2
[ 0. 0.42708094 0.42708094 0. ]
Iter= 3 , t = 0
[ 7.95138670e-17 8.66025404e-01 8.66025404e-01 0.00000000e+00]
Iter= 3 , t = 1
[ 7.95138670e-17 1.19665077e+00 1.19665077e+00 0.00000000e+00]
Iter= 3 , t = 2
[ 0. 0.42708094 0.42708094 0. ]
Iter= 4 , t = 0
[ 7.95138670e-17 8.66025404e-01 8.66025404e-01 0.00000000e+00]
Iter= 4 , t = 1
[ 7.95138670e-17 1.19665077e+00 1.19665077e+00 0.00000000e+00]
Iter= 4 , t = 2
[ 7.95138670e-17 1.19665077e+00 1.19665077e+00 0.00000000e+00]

For example

"Iter=0, t=2" is not same as "Iter=1, t=2"

Also "Iter=1, t=1" and "Iter=1, t=2" are same which is not correct.

However during Iter=2, things are correct. Also correct for Iter=3, but wrong for Iter=4.

praveen

Question information

Language:
English Edit question
Status:
Answered
For:
DOLFIN Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Anders Logg (logg) said :
#1

It looks like you repeatedly store values at the same points in time.

TimeSeries assumes that the times given are strictly increasing. I'll
add a check for this.

--
Anders

On Sat, May 05, 2012 at 08:35:47AM -0000, Praveen C wrote:
> New question #196105 on DOLFIN:
> https://answers.launchpad.net/dolfin/+question/196105
>
> I am trying to store data into a TimeSeries, retrieve it, modify and then store it back. But this is not working correctly. Below is a simple example.
>
> from dolfin import *
>
> mesh = Interval(3, 0, pi)
>
> # Create Function Space
>
> Q = FunctionSpace(mesh, "CG", 1)
> u = Function(Q)
>
> # Parameters
> T = 2
> nt = T
> dt = T/nt
>
> # Time Stepping
> time_u = TimeSeries("U")
>
> uex = Expression("sin(t)*sin(x[0])+cos(t)*sin(x[0])",t=0)
>
> t = 0
> while t <= T:
> uex.t = t
> u = project(uex, Q)
> time_u.store(u.vector(),t)
> t += dt
>
> it = 0
> while it < 5:
> t = 0
> while t <= T:
> print "Iter=", it, ", t = ", t
> time_u.retrieve(u.vector(),t)
> # Do something to u and store it back
> time_u.store(u.vector(),t)
> print u.vector().array()
> t += dt
> it += 1
>
> I repeatedly retrieve and store u back to the TimeSeries. The output shows that there is something wrong
>
>
> Iter= 0 , t = 0
> [ 7.95138670e-17 8.66025404e-01 8.66025404e-01 0.00000000e+00]
> Iter= 0 , t = 1
> [ 7.95138670e-17 1.19665077e+00 1.19665077e+00 0.00000000e+00]
> Iter= 0 , t = 2
> [ 0. 0.42708094 0.42708094 0. ]
> Iter= 1 , t = 0
> [ 7.95138670e-17 8.66025404e-01 8.66025404e-01 0.00000000e+00]
> Iter= 1 , t = 1
> [ 7.95138670e-17 1.19665077e+00 1.19665077e+00 0.00000000e+00]
> Iter= 1 , t = 2
> [ 7.95138670e-17 1.19665077e+00 1.19665077e+00 0.00000000e+00]
> Iter= 2 , t = 0
> [ 7.95138670e-17 8.66025404e-01 8.66025404e-01 0.00000000e+00]
> Iter= 2 , t = 1
> [ 7.95138670e-17 1.19665077e+00 1.19665077e+00 0.00000000e+00]
> Iter= 2 , t = 2
> [ 0. 0.42708094 0.42708094 0. ]
> Iter= 3 , t = 0
> [ 7.95138670e-17 8.66025404e-01 8.66025404e-01 0.00000000e+00]
> Iter= 3 , t = 1
> [ 7.95138670e-17 1.19665077e+00 1.19665077e+00 0.00000000e+00]
> Iter= 3 , t = 2
> [ 0. 0.42708094 0.42708094 0. ]
> Iter= 4 , t = 0
> [ 7.95138670e-17 8.66025404e-01 8.66025404e-01 0.00000000e+00]
> Iter= 4 , t = 1
> [ 7.95138670e-17 1.19665077e+00 1.19665077e+00 0.00000000e+00]
> Iter= 4 , t = 2
> [ 7.95138670e-17 1.19665077e+00 1.19665077e+00 0.00000000e+00]
>
>
> For example
>
> "Iter=0, t=2" is not same as "Iter=1, t=2"
>
> Also "Iter=1, t=1" and "Iter=1, t=2" are same which is not correct.
>
> However during Iter=2, things are correct. Also correct for Iter=3, but wrong for Iter=4.
>
> praveen
>

Revision history for this message
Praveen C (cpraveen) said :
#2

Yes, I want to update u(t) and store it back into the same TimeSeries at the same time t. The problem involves optimization of a time dependent pde, and hence the need to do this.

The explanation here

http://fenicsproject.org/documentation/dolfin/dev/python/programmers-reference/cpp/TimeSeries.html

"A new time series will check if values have been stored to file before (for a series with the same name) and in that case reuse those values. If new values are stored, old values will be cleared."

made me think that if I store at a time which already has data, it will overwrite it.

praveen

Revision history for this message
Souvik Roy (1roysouvik) said :
#3

Anders,
             Can you please tell me how to use the time series with the above modification while storing and retrieving values?
                                                                                                                                                                                                Souvik

Revision history for this message
Johan Hake (johan-hake) said :
#4

You cannot store values at the same timestep twice. Then TimeSeries interprete this as adding a new value at the same timepoint, issuing an error. You need to store the new values in seperate timeseries:

while it < 5:
    t = 0
    time_u_updated = TimeSeries("U_updated_%d" % it)
    while t <= T:
       print "Iter=", it, ", t = ", t
       time_u.retrieve(u.vector(),t)
       # Do something to u and store it back
       time_u_updated.store(u.vector(),t)
       print u.vector().array()
       t += dt
    it += 1

Can you help with this problem?

Provide an answer of your own, or ask Praveen C for more information if necessary.

To post a message you must log in.