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

Extract solution over a subdomain of the mesh in C++

0 votes

Hello,

Suppose I have calculated my solution all over a mesh, for instance the solution of the Poisson equation demo.
I would like to retain certain particular values of the solution, for instance those on the line x == 0.5 or maybe those in the circle centered in (0.5, 0.5) with radius 0.1.

Here there seems to be a possible solution in Python, which however doesn't translate in C++ since the dofmap() function doesn't seem to be implemented in C++.

error: ‘class std::shared_ptr<Poisson::Form_a_FunctionSpace_0>’ has no member named ‘dofmap’
auto dofmap = V.dofmap()
                ^
asked Nov 11, 2016 by anfneub FEniCS Novice (580 points)

Nevermind, I was able to extract a narrow stripe of solution by slightly modifying the "non-matching meshes" demo.

However, now that I can plot it, can I extract just the values of the solution?
I mean, I know the x varies from 0.0 to 0.5, so would I be able to somehow extract the values of the solution and maybe import them in matlab to visualize it like a usual vector?

By writing f0 in a pvd files I still get plenty of numbers in the .vtu


#include <dolfin.h>
#include "P1.h"
#include "P3.h"
#include <mshr.h>

using namespace dolfin;

class MyExpression : public Expression
{
public:

  void eval(Array<double>& values, const Array<double>& x) const
  {
    values[0] = sin(10.0*x[0])*sin(10.0*x[1]);
  }

};

int main()
{
  // Create meshes
  auto test0 = std::make_shared<mshr::Rectangle>(dolfin::Point(0.0, 0.0, 0.0), dolfin::Point(1.0, 1.0, 0.0));
  auto test1 = std::make_shared<mshr::Rectangle>(dolfin::Point(0.0, 0.0, 0.0), dolfin::Point(0.001, 0.5, 0.0));
    auto mesh0 = mshr::generate_mesh(*test0, 250);
    auto mesh1 = mshr::generate_mesh(*test1, 250);

  // Create function spaces
  auto V0 = std::make_shared<P3::FunctionSpace>(mesh0);
  auto V1 = std::make_shared<P3::FunctionSpace>(mesh1);

  // Create functions
  Function f0(V0);
  Function f1(V1);

  // Interpolate expression into V0
  MyExpression e;
  f0.interpolate(e);

  // Interpolate V0 function (coarse mesh) into V1 function space (fine mesh)
  f1.interpolate(f0);

  // Plot results
  plot(f0);
  plot(f1);
  interactive();

  return 0;
}
...