Error when writing a combination of functions in a VTK file

12 weeks ago by
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
def phi(temp):
    Tr = Constant(0.01)
    r = Constant(0.05)
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

12 weeks ago by
I believe you have to solve for the values in order to save them. Here is an example:
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)
I think that is not that.
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
# Save solution to file in VTK format 
vtkfile1 = File('Phase Change/Velocity/velocity.pvd')
vtkfile2 = File('Phase Change/Pressure/pressure.pvd')
vtkfile3 = File('Phase Change/Temperature/temperature.pvd')
vtkfile4 = File('Phase Change/Concentration/concentration.pvd')

# Form and solve the linear system
for n in range(num_steps):

    # Update current time
    t += dt

    # Solve the variational problem
    (u_, p_, theta_) = w_.split()
    # Save data
    vtkfile1 << (u_, t)
    vtkfile2 << (p_, t)
    vtkfile3 << (theta_, t)
    vtkfile4 << (phi(theta_), t)   ######################## HERE ###################

    # Update the previous solution
    w_n.vector()[:] = w_.vector()

    # Update the progress bar
As I said before I have te following error :
in method 'File___lshift__', argument 2 of type 'dolfin::Function const &'​

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

written 12 weeks ago by Matthieu Diaz  
The reason you are getting that error is because the object returned by 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.

Similar posts:
Search »