Load time series array with Expression

Hello!

So months ago i asked about mesh formulation for use in hydrodynamic simulation using OpenTidalFarm that depends on Fenics. I could simulate the hydrodynamic model with JIT-compiled sinusoidal equation at the boundary. However, i want to use real time-series data as a boundary condition.

Suppose i have time-series data such as :

0.3209
0.3209
0.1209
-0.1291
-0.3791
...

with specified time interval per data.
Then suppose i have a time variable t that increase by dt over time. How can i load this data in the Expression function so that when the time variable updates it reads to the next index?

Thank you in advance!

Consider the following minimal example:

from dolfin import *
import numpy as np

mesh = UnitSquareMesh(10, 10)

time_series = [0, 0.3, -0.4, 0.2, 0.5]

expr = Expression("t", t=time_series[0], degree=1)

a = expr * dx(domain=mesh)

for i, time in enumerate(time_series):
    expr.t = time
    A = assemble(a)
    print(A)

returns:

0.0
0.3000000000000002
-0.4000000000000003
0.20000000000000015
0.5000000000000003

Hi, dokken. Sorry for replying late as i only got to try it now!

Thank you for your suggestion! Although the assemble() result doesn’t show the exact time series data as the input…

This is what happens when i print assemble(a)

1188286524.36
1429657224.62
1726728855.71
2037725719.5
2288379908.23

I wonder what makes this happen…

(Edit : i tried this directly using my own domain mesh, would that be the problem?)

The expected output should be the volume of your mesh times the input value. The only reason for assembling it in a loop was to highlight that the form changes in time dependent problems without redefining it explicitly

I see, so does that mean just defining the list of data should do the trick?

Although, i tried just directly using the list and it shows an error like this :

TypeError: in method 'Expression_097464b5e61d068b0849dc47522abe90cd94dd9a_t_set', argument 2 of type 'double'

This is all you need to update your expression.

Thank you for your answers, dokken!
Yes you’re correct, i did try with a simpler mesh and using only Fenics module.
The problem i had was that OpenTidalFarm has its own boundary condition reading mechanics from Expression so somehow i had to get the time update inside the Expression.

Which lead me to use subclass Expression such as follows :

class TopExpr(Expression):
    def eval(self,value,x):
        value[0]=top[0] #top is the timeseries variable i used
    def update(self, t):
        top[0].t = t

But it doesn’t update the Expression value.
I tried reading other posts about updating UserExpression but i can’t really comprehend the implementation on my own…

You need to produce a minimal working example, i.e. A short code that reproduces the issue. It can use builtnin meshes, and made up data.