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

How to pass a ufl form (e.g., grad(u)) into a cpp expression?

0 votes

I would like to pass an ufl form example grad(u) in which u is a vector function into the expression. Is there a way to do that? I tried the following and it doesn't work.

The reason for doing so is that we want to compute a fourth order elasticity tensor that depends on the strain from a displacement field to form the Jacobian for the Newton Solver.

import dolfin
import numpy as np

mesh = dolfin.UnitCubeMesh(1, 1, 1)
S = dolfin.FunctionSpace(mesh, "CG", 2)
u = dolfin.Function(S)
gradu = dolfin.grad(u)

dx = dolfin.Measure("dx", domain=mesh)

code2 = '''

namespace dolfin
{

  std::vector<std::size_t> shape = {3,3,3,3};

class MyFunc2 : public Expression
{

  mutable Array<double> UX;

public:

  std::shared_ptr<MeshFunction<std::size_t> > cell_data;
  std::shared_ptr<const Function> U;

  MyFunc2() : Expression(shape), UX(2)
  {
  }

  void eval(Array<double>& values, const Array<double>& x) const
  {
    for(int i=0; i<81;i++){
        values[i] = i;
    }
  }
};
}'''


Tensor4 = dolfin.Expression(code2, degree=1)
Tensor4.U = gradu
asked Sep 12, 2016 by lee FEniCS User (1,170 points)
...