Dimension-Independence (C++)

Asked by Pietro Maximoff on 2009-12-30

Hello all

Is there a way to program dimension-independence.

I'm able to select a UnitInterval, etc based on the value of a variable 'dim' in main.cpp but the major problem is how to select the right shape for the element, i.e. triangle, tetrahedron, etc via the form (ufl) file based on the dimension.

Is this feasible?

Best wishes

Pietro

Question information

Language:
English Edit question
Status:
Solved
For:
DOLFIN Edit question
Assignee:
No assignee Edit question
Solved by:
Anders Logg
Solved:
2009-12-31
Last query:
2009-12-31
Last reply:
2009-12-30
Andy R Terrel (andy-terrel) said : #1

The easiest thing to do is just create multiple .ufl files. See dolfin/demo/pde/cahn-hilliard/cpp

But it is also possible to just do a switch inside the ufl file since ffc just wraps the ufl file as a python script. The problem with this approach is making sure you have the right dimensions everywhere. For simple things (like stokes) this is trivial, but as you add more complexity to your algorithms it begins to become a real pain.

Another option is to just use the python interface to dolfin, then every thing gets generated as necessary and can have a larger amount of flexibility for less code (that you write anyways).

Best Anders Logg (logg) said : #2

On Wed, Dec 30, 2009 at 07:58:15PM -0000, Pietro Maximoff wrote:
> New question #95695 on DOLFIN:
> https://answers.launchpad.net/dolfin/+question/95695
>
> Hello all
>
> Is there a way to program dimension-independence.
>
> I'm able to select a UnitInterval, etc based on the value of a variable 'dim' in main.cpp but the major problem is how to select the right shape for the element, i.e. triangle, tetrahedron, etc via the form (ufl) file based on the dimension.
>
> Is this feasible?
>
> Best wishes
>
> Pietro

No, the generated code is always for a specific form on a specific
element shape and specific finite element function space. So you need
to do something like this in your C++ program:

  FunctionSpace* V = 0;
  Form* form = 0;
  switch (d)
  {
  case 1:
    V = new Poisson1D::FunctionSpace(mesh);
    form = new Poisson1D::BilinearForm(*V, *V);
    break;
  case 2:
    V = new Poisson2D::FunctionSpace(mesh);
    form = new Poisson2D::BilinearForm(*V, *V);
    break;
  case 3:
    V = new Poisson3D::FunctionSpace(mesh);
    form = new Poisson3D::BilinearForm(*V, *V);
    break;
  default:
    error("Illegal mesh dimension %d.", d);
  };

But as Andy points out, you can get around this by using the Python
interface which will generate code for either 1D, 2D or 3D if you set
it up that way.

--
Anders

Pietro Maximoff (segment-x) said : #3

Thanks, Anders. That was quite helpful.

Yes, you're quite right, I can actually do this via the Python interface but ultimately, I'll need the raw power of C++ for the simulations I perform.

Pietro Maximoff (segment-x) said : #4

Thanks Anders Logg, that solved my question.