How can I perform operations on dolfin functions?


I am not able to find a complete documentation about dolfin (might not be available yet).

My question is the following: Once I obtain the dolfin function u after solving the FE problem, how could I obtain the square of all the values? This will be useful since I am solving a differential equation for a field (u), but I am only interested in the field intensity (u^2) at each point.

Thank you,


If you want to square each entry of the vector of degrees of freedom for a Function, you can do that through the petsc4py API, using the pointwiseMult method of Vec:

from dolfin import *
mesh = UnitIntervalMesh(128)

# Nonzero Function for testing:
V = FunctionSpace(mesh,"CG",1)
x = SpatialCoordinate(mesh)
u = project(sin(2*pi*x[0]),V)

# Function to hold $u^2$:
u2 = Function(V)

# Get petsc4py Vec objects behind the Functions:
u_vec = as_backend_type(u.vector()).vec()
u2_vec = as_backend_type(u2.vector()).vec()

# Use the petsc4py pointwise multiplication method:

# Check results visually:
from matplotlib import pyplot as plt

Amazing! @kamensky , it works! Thank you very much!

Hello again, @kamensky . I have a similar issue now. This time I want to run the program several times and in order to compare the different ā€œuā€, I would like to normalize them, that is to divide each u by its own maximum value. How could I do that?