How to subtract one cellFunction/meshFunction from another without iterating

Asked by Swarbhanu Chatterjee

Is it possible to do something like, result = a - b , where a, b and result are cellFunctions or meshFunctions? The way I have implemented the same is by iterating over the cells,

for cell in cells(mesh):
 result[cell] = a[cell] - b[cell]

But, I want to know if there is a more generic way of doing it, i.e. result = a - b, and whether the dolfin framework can possibly optimize such a computation.

Thanks,
S.

Question information

Language:
English Edit question
Status:
Answered
For:
DOLFIN Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:

This question was reopened

Revision history for this message
Anders Logg (logg) said :
#1

On Mon, Dec 12, 2011 at 08:00:57PM -0000, Swarbhanu Chatterjee wrote:
> Question #181679 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/181679
>
> Summary changed to:
> How to subtract one cellFunction/meshFunction from another without iterating

There's no subtraction operator for MeshFunction (it could be added)
but you can call the array() member function and subtract the NumPy
arrays.

--
Anders

Revision history for this message
Swarbhanu Chatterjee (schatterjee) said :
#2

Thanks Anders Logg, that solved my question.

Revision history for this message
David (dstuebe) said :
#3

Hi Anders, Fenics

Thanks for your reply. We are really looking for a more general solution. We would like to be able to write complex operators which can be evaluated against arbitrary mesh data. The fenics expressions look promising, but don't quiet seem to fit. Solving A - B seems like a simple test case. Essentially we are trying to define a operator that takes one or more Functions as an argument.

As I understand it a Fenics Function is backed by a mesh with data - a field such as speed or temperature defined values on each mesh entity.

We would like to define operators which evaluate an expression on one or more such Functions defined on the same mesh.

For instance if we have conductivity, temperature and pressure defined on the same mesh, we would like to define an operator which will calculate the salinity using the equation of state for sea water and return a result defined on the same mesh.

Is this a good fit for fenics?

David

Revision history for this message
Anders Logg (logg) said :
#4

On Tue, Dec 13, 2011 at 05:35:39PM -0000, David wrote:
> Question #181679 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/181679
>
> David posted a new comment:
>
> Hi Anders, Fenics
>
> Thanks for your reply. We are really looking for a more general
> solution. We would like to be able to write complex operators which can
> be evaluated against arbitrary mesh data. The fenics expressions look
> promising, but don't quiet seem to fit. Solving A - B seems like a
> simple test case. Essentially we are trying to define a operator that
> takes one or more Functions as an argument.
>
> As I understand it a Fenics Function is backed by a mesh with data - a
> field such as speed or temperature defined values on each mesh entity.
>
> We would like to define operators which evaluate an expression on one or
> more such Functions defined on the same mesh.
>
> For instance if we have conductivity, temperature and pressure defined
> on the same mesh, we would like to define an operator which will
> calculate the salinity using the equation of state for sea water and
> return a result defined on the same mesh.
>
> Is this a good fit for fenics?

Yes, but you should use Functions or Expressions to represent fields,
not MeshFunctions. Then you can add and subtract as much as you like
as part of a form.

--
Anders

Revision history for this message
Swarbhanu Chatterjee (schatterjee) said :
#5

Thanks Anders.

From what I understand, the Functions or Expressions are evaluated over points in space. However, we need to define functions which take values over cells, faces or edges (discrete entities, and not points in space for example). We will be having our data which take values at these entities. We need to have functions defined on these entities and we should be able to define complex mathematical operations over these functions.

Is it possible to define a Function or Expression that only take values over cells? We do not want to have to average over all points in the cell to do the same. I could not find an example anywhere which allows one to do this. The MeshFunctions seem to be the only things capable of taking values over entities like cells or facets (and not points), but they do not seem to be tailored for complex operations. But maybe I am wrong.

It will be a great help if you can write a one liner example code of how to do this. Thanks much in advance.

Swarbhanu.

Revision history for this message
Swarbhanu Chatterjee (schatterjee) said :
#6

On a more general note, if we have a function, fs, defined as follows,

Vs = FunctionSpace(mesh, "CG", 2)

fs = Expression("sin(x)", degree=2)
g = project(fs, V=Vs)

What can we pass in the most general situation as the argument, x?
Can it be only a spatial point? Can x be such that it is a tuple such as (temperature, depth,..) etc?

Thanks,
Swarbhanu.

Revision history for this message
Anders Logg (logg) said :
#7

On Tue, Dec 13, 2011 at 09:31:00PM -0000, Swarbhanu Chatterjee wrote:
> Question #181679 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/181679
>
> Status: Solved => Open
>
> Swarbhanu Chatterjee is still having a problem:
> Thanks Anders.
>
> >From what I understand, the Functions or Expressions are evaluated
> over points in space. However, we need to define functions which
> take values over cells, faces or edges (discrete entities, and not
> points in space for example). We will be having our data which take
> values at these entities. We need to have functions defined on these
> entities and we should be able to define complex mathematical
> operations over these functions.
>
> Is it possible to define a Function or Expression that only take
> values over cells? We do not want to have to average over all points
> in the cell to do the same. I could not find an example anywhere
> which allows one to do this. The MeshFunctions seem to be the only
> things capable of taking values over entities like cells or facets
> (and not points), but they do not seem to be tailored for complex
> operations. But maybe I am wrong.
>
> It will be a great help if you can write a one liner example code of
> how to do this. Thanks much in advance.

Use a DG(0) function space (piecewise constants) for something that
takes a value on each cell.

For something that takes a value on each facet, use a Crouzeix-Raviart
element.

--
Anders

Revision history for this message
Anders Logg (logg) said :
#8

On Tue, Dec 13, 2011 at 10:41:09PM -0000, Swarbhanu Chatterjee wrote:
> Question #181679 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/181679
>
> Swarbhanu Chatterjee gave more information on the question:
> On a more general note, if we have a function, fs, defined as follows,
>
> Vs = FunctionSpace(mesh, "CG", 2)
>
> fs = Expression("sin(x)", degree=2)
> g = project(fs, V=Vs)
>
> What can we pass in the most general situation as the argument, x?
> Can it be only a spatial point? Can x be such that it is a tuple such as (temperature, depth,..) etc?

Not as part of an Expression, but you can have things like

 sin(u*h)

or whatever you like if 'u' is the temperature field and 'h' is the
depth field.

--
Anders

Can you help with this problem?

Provide an answer of your own, or ask Swarbhanu Chatterjee for more information if necessary.

To post a message you must log in.