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

Electric Field as a Gradient

0 votes

I have the following declaration in ufl form:

helem = FiniteElement("Lagrange", tetrahedron, 1)

u = TrialFunction(helem)
v = TestFunction(helem)


a = -(nabla_grad(u)*v)*dx 

Can only integrate scalar expressions. The integrand is a tensor expression with value rank 1 and free indices ().
An exception occured during evaluation of form file.

I understand that i can only integrate scalar values, but what i have to do in order to calculate the expression nabla_grad(u)?

Thank you

asked Jun 17, 2015 by paschal91 FEniCS Novice (240 points)
edited Jun 17, 2015 by chris_richardson

Hi, are you trying to compute gradient as a function in helem space from some scalar?

Yes,that is exactly what i'm trying to do.

1 Answer

0 votes

Hi, consider

#include <dolfin.h>
#include "potential.h"

/* potential.ufl
  cell = tetrahedron
  S = FiniteElement('Lagrange', cell, 1)
  V = VectorElement('Lagrange', cell, 1)

  u = TrialFunction(V)
  v = TestFunction(V)
  f = Coefficient(S)

  # Solve u = -nabla_grad(f) 
  a = inner(u, v)*dx
  L = inner(-nabla_grad(f), v)*dx
*/

using namespace dolfin;

class Source : public Expression
{
  void eval(Array<double>& values, const Array<double>& x) const
  {
    values[0] = x[0]*x[0] + x[1]*x[1] + x[2]*x[2];
  }
};

int main()
{
  BoxMesh mesh(-0.5, -0.5, -0.5, 0.5, 0.5, 0.5, 4, 4, 4);

  potential::FunctionSpace V(mesh);
  potential::CoefficientSpace_f S(mesh);

  Source f_exp;
  Function f(S);
  f.interpolate(f_exp);

  potential::BilinearForm a(V, V);
  potential::LinearForm L(V);
  L.f = f;

  Function u(V);
  solve(a == L, u);

  plot(f);
  plot(u);
  interactive();

  return 0;
} 
answered Jun 18, 2015 by MiroK FEniCS Expert (80,920 points)
...