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

Solving Poisson Equation with no source and surface normals in C++

0 votes

Hello All,

I am trying to solve the Poisson equation in electrostatics:
$$ -\nabla^2 \varphi(r) = 0 $$

so there is no source term, and no surface normals.

My python code works fine, but converting it to C++ generates an error.

*** Error: Unable to assemble form.
*** Reason: Geometric dimension of Mesh does not match value shape of coordinate element in form.
*** Where: This error was encountered inside AssemblerBase.cpp.
*** Process: 0

Here are the relevant snippets of my code:

    int nx=30,ny=30,nz=30;
    Point a0(-halfxlength,-halfylength,-halfzlength);
    Point a1(halfxlength, halfylength, halfzlength);

    // mesh and function space
    auto mesh = std::make_shared<BoxMesh>(a0,a1,nx,ny,nz);
    auto V    = std::make_shared<Poisson::FunctionSpace>(mesh);

    // Define boundary conditions
    auto u0 = std::make_shared<Constant>(0.0);
    auto u1 = std::make_shared<Constant>(1.0);

    auto rboundary = std::make_shared<red>();
    auto bboundary = std::make_shared<blue>();

    DirichletBC bc1(V, u0, bboundary);   // bc 1
    DirichletBC bc2(V, u1, rboundary);   // bc 2
    std::vector<const DirichletBC*> bcs; // Create an array of boundary conditions
    bcs.push_back(&bc1);
    bcs.push_back(&bc2);

    Poisson::BilinearForm a(V,V);
    Poisson::LinearForm L(V);
    auto f = std::make_shared<Constant>(0.0);
    L.f = f;
    //L = std::make_shared<Constant>(0.0);
    Function u(V);
    solve(a == L, u, bcs);

and my Poisson.ufl file is this:

element = FiniteElement("Lagrange", triangle, 1)
u = TrialFunction(element)
v = TestFunction(element)
f = Coefficient(element)
a = inner(grad(u), grad(v))*dx
L = f*v*dx

Thanks,
Victor.

asked Aug 13, 2016 by wtpot FEniCS Novice (450 points)

1 Answer

0 votes
 
Best answer

You need to use tetrahedral elements in your finite element definition (in the ufl file). Try this:

element = FiniteElement("Lagrange", tetrahedron, 1)
answered Aug 13, 2016 by hernan_mella FEniCS Expert (19,460 points)
selected Aug 17, 2016 by johannr

Thanks, that fixed the problem..

Another question: since there is no source term, is it ok to just write?

 solve(a == 0, u, bcs);

no, the above implementation that you have presented is the correct way to do it.

...