This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

Assigning piece-wise constant function to tensor function

+2 votes

Hi!

I want to assign piece-wise constant functions to the components of a tensor. If I wanted to do this for a scalar function I would do the following

  • define a mesh and subdomains therein
  • define a 'DG' FunctionSpace of order 0 on the mesh
  • define a function test
  • loop over all cells and depending on the subdomain that the cell is in assign a value x to the function using test.vector()[cell_no] = x[subdomain]

This works fine for scalar functions. However, if I use a tensor function space how do I know which index of vector() corresponds to which cell AND component of my tensor?

Thank you very much for any help!

asked May 12, 2014 by torsten FEniCS Novice (240 points)

1 Answer

+1 vote

I use something like that in my script. I start off with a scalar function that labels the areas that I want to have a given constant vector. The mesh is created externally, but basically just labels material 1 with a 1, material 2 with a 2, material 3 with a 3. In the snippet below, I am trying to create the constant vector field for material 3:

D = FunctionSpace(mesh, "DG", 0)
cell_labels = MeshFunction("size_t", mesh, "material_type.xml.gz")
helper    = numpy.asarray(cell_labels.array(), dtype=numpy.int32) - 1
dm        = D.dofmap()
for cell in cells(mesh):
  helper[dm.cell_dofs(cell.index())] = cell_labels[cell] - 1
material_3_location = Function(D)
material_3_location.vector()[:] = numpy.choose(helper, [0., 0., 1.])

You can then make your piecewise constant function by projecting or interpolating.

V = FunctionSpace(mesh, "N1curl", 1)
V3 = project(Constant((0., 1., 0.))*material_3_location, V)
answered May 12, 2014 by timm FEniCS User (2,100 points)
...