← Back to team overview

dolfin team mailing list archive

Re: [Question #156272]: Computing numerical integral of a function on a cell

 

Put this in a file say Foo.ufl:

DG0 = FiniteElement("DG", triangle, 0)
element = FiniteElement("CG", triangle, 2)

v = TestFunction(DG0)
f = Coefficient(element)
L = f*v*dx

Compile it with FFC, don't forget the '-l dolfin' argument:

$ ffc -l dolfin Foo.ufl

which will produce Foo.h

Then put the following in a main.cpp file:


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

using namespace dolfin;

class Source : public Expression
{
  public:

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

int main()
{
  UnitSquare mesh(3,3);

  Avg::FunctionSpace V(mesh);
  Avg::LinearForm L(V);

  Source f;
  L.f = f;

  Vector b;
  assemble(b, L);
  info(b, true);

  return 0;
}

Now you have your values in the vector b. What you'll do with it is up to you.

Kristian


On 6 May 2011 16:31, James Avery <question156272@xxxxxxxxxxxxxxxxxxxxx> wrote:
> Question #156272 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/156272
>
>    Status: Answered => Open
>
> James Avery is still having a problem:
> Hi Marie, thanks so much for your swift reply!
>
> I'm not quite sure I understand, though: Once this bit of UFL is FFC'ed,
> how do I then compute the cell integrals of the density from Dolfin/C++?
>
> Just to make sure we're on the same page:
>
> 1. 'density' is a spatial scalar function given at runtime (I suppose the UFL data type would be Coefficient(mesh)?).
> 2. For each cell, I need to evaluate the cell integral of 'density'. If there is more than a given amount in a cell, I mark it for subdivision. This proceeds iteratively until each cell contains no more than a given amount of density.
>
> Now, in your above example, I understand that 'b' defines a vector of
> length (number of cells), the entries being the cell integrals of
> 'density'.
>
> 1. This is UFL-code, which is compiled with FFC, is that correct? From the C++-program, how do I then execute the cell-integration and access the results?
> 2. How are the integrals calculated, then?
> 3. Can't I just get the quadrature points and quadrature weights in an easy way? Then the operation is essentially trivial.
>
> I think I may still be harboring some confusion about how UFL/FFC and
> Dolfin interoperate -- about what is done offf-line by FFC, and what can
> be done on-line by Dolfin. I hope the confusion doesn't make it too
> difficult to answer my question.
>
> Thanks very much for your help!
>
> Cheers,
> James
>
> --
> You received this question notification because you are a member of
> DOLFIN Team, which is an answer contact for DOLFIN.
>
> _______________________________________________
> Mailing list: https://launchpad.net/~dolfin
> Post to     : dolfin@xxxxxxxxxxxxxxxxxxx
> Unsubscribe : https://launchpad.net/~dolfin
> More help   : https://help.launchpad.net/ListHelp
>



Follow ups

References