### Error when writing a combination of functions in a VTK file

95

views

0

Hello, I would like to save post processed data of a simulation I performed using a VTK file. My problem enables me to find a velocity field (u_), a pressure field (p_) and a temperature field (theta_).

As it is a time dependent issue, I save data at each time step.

I would ike now to save a function of the temperature called phi and defined below in the code

As it is a time dependent issue, I save data at each time step.

I would ike now to save a function of the temperature called phi and defined below in the code

```
def phi(temp):
Tr = Constant(0.01)
r = Constant(0.05)
return(0.5+0.5*tanh((Tr-temp)/r))
```

I created a VTK file and I used the command :`vtkfile << (phi(theta_), t)`

But I obtain the following error :`in method 'File___lshift__', argument 2 of type 'dolfin::Function const &'`

Can someone help me with this issue ? Thank you in advance

Community: FEniCS Project

### 1 Answer

0

I believe you have to solve for the values in order to save them. Here is an example:

The reason you are getting that error is because the object returned by

This would be much easier to do if the result of phi(theta_) was a linear combination of function objects, but it's not in your case due to the hyperbolic tangent.

```
import numpy as np
import dolfin as dlf
mesh = dlf.UnitSquareMesh(10, 10)
V = dlf.FunctionSpace(mesh, "CG", 1)
# Function where solution to original problem will be stored.
u = dlf.Function(V)
# Test and Trial Functions
du = dlf.TrialFunction(V)
xi = dlf.TestFunction(V)
# Assigning random values as an example.
u.vector()[:] = np.random.rand(u.vector().size())
def phi(temp):
Tr = dlf.Constant(0.01)
r = dlf.Constant(0.05)
return 0.5*(1.0 + dlf.tanh((Tr - temp)/r))
z = phi(u)
# New function to store values.
new_function = dlf.Function(V)
# Defining and solving linear problem to approximate values of phi.
a = du*xi*dlf.dx
L = z*xi*dlf.dx
dlf.solve(a == L, new_function)
# Save solution at time t = 0.0.
dlf.File("derived-values.pvd") << (new_function, 0.0)
```

`phi(theta_)`

is of type `ufl.algebra.Sum`

. The `VTKFile`

objects don't know how to write a `ufl.algebra.Sum`

of this type to a file. You can see this by looking at the overloaded function `operator<<`

in the source code, and you see that the only objects it knows how to write to a VTK file are `dolfin.Mesh`

, `dolfin.MeshFunction`

, and `dolfin.Function`

(and some other stuff of the form `std::pair`

which I believe is to specify the time as you have done)

. Thus, you have to represent the result that `phi(theta_)`

gives in a `dolfin.Function`

object, which I called `new_function`

. Then you solve the problem `new_function = phi(theta_)`

, which in variational form is as I have defined in the example I provided above.This would be much easier to do if the result of phi(theta_) was a linear combination of function objects, but it's not in your case due to the hyperbolic tangent.

written
12 weeks ago by
Miguel Rodriguez

Please login to add an answer/comment or follow this question.

My problem is solved in terms of velocity, pressure and temperature. Actually, I don't care about them, I want to save a function of the temperature but the following code doesn't work

As I said before I have te following error :

that is entirely due to the fact I want to save phi(theta_) but I don't know how to fix it