The absolute function call outside the integration

Dear all,

Is there any method to apply a abs() to the integration?
For example, in FEniCS,
abs(1)*dx(mesh) is valid
while abs(1*dx(mesh)) would be invalid, and it would tell that TypeError: bad operand type for abs(): 'Form'.

Thanks a lot!

Consider the following, using numpy.abs:

from dolfin import *
import numpy as np
mesh = UnitSquareMesh(10, 10)
c = Constant(1)
a = c*dx(mesh)
print(-assemble(a), np.abs(-assemble(a)))

returning

-1.0000000000000007 1.0
1 Like

Dear Dokken,

Thanks for your reply.
The assemble() do work when we try to obtain a value.
However, in my case I tried to write it in the form (for solving).
For instance,

Pi = (psi)*dx + (1/2)*(pen)*ppos( dot(position,position)*ds(2) - dot(position,position)*ds(1) ) 
F = derivative(Pi, u, u_test)
J = derivative(F, u, u_tr)
fe.solve(F==0, u, bcs, J=J)

where

def ppos(x):
    return (x+abs(x))/2.

and the result would tell:

TypeError: bad operand type for abs(): 'Form'

So, maybe FEniCS just simply cannot implement these kind of code, and should start trying different approaches (?)

So are you trying to assemble \vert \int g(position) ~\mathrm{d}s\vert?
Are you sure you do not want \int \vert g(position)\vert\mathrm{d}s?
If you really want the first thing, you need to write out F and J explicitly, as dolfin does not know how to differentiate (or take the absolute value of the whole integral, only the integrand)

1 Like

Yes, I’m trying to implement the first equation you mentioned.
Thanks for your information and advice, I’ll try to carry out the F and J form explicitly.