Error function erf in a form

Asked by Praveen C

Hello

I want to use the error function erf (http://en.wikipedia.org/wiki/Error_function) in a form. Is it possible ? Using "erf" gives me an error.

praveen

Question information

Language:
English Edit question
Status:
Solved
For:
DOLFIN Edit question
Assignee:
No assignee Edit question
Solved by:
Martin Sandve Alnæs
Solved:
Last query:
Last reply:
Revision history for this message
Kristian B. Ølgaard (k.b.oelgaard) said :
#1

On 20 October 2011 06:45, Praveen C
<email address hidden> wrote:
> New question #175381 on DOLFIN:
> https://answers.launchpad.net/dolfin/+question/175381
>
> Hello
>
> I want to use the error function erf (http://en.wikipedia.org/wiki/Error_function) in a form. Is it possible ? Using "erf" gives me an error.

This function is currently not supported but it shouldn't be too
difficult to implement. I can add the pieces to get this working in
UFL and FFC (apart from handling automatic differentiation).
Should I go ahead Martin?

Kristian

> praveen
>
> --
> You received this question notification because you are a member of
> DOLFIN Team, which is an answer contact for DOLFIN.
>

Revision history for this message
Martin Sandve Alnæs (martinal) said :
#2

On 20 October 2011 10:45, Kristian B. Ølgaard
<email address hidden> wrote:
> Question #175381 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/175381
>
>    Status: Open => Answered
>
> Kristian B. Ølgaard proposed the following answer:
> On 20 October 2011 06:45, Praveen C
> <email address hidden> wrote:
>> New question #175381 on DOLFIN:
>> https://answers.launchpad.net/dolfin/+question/175381
>>
>> Hello
>>
>> I want to use the error function erf (http://en.wikipedia.org/wiki/Error_function) in a form. Is it possible ? Using "erf" gives me an error.
>
> This function is currently not supported but it shouldn't be too
> difficult to implement. I can add the pieces to get this working in
> UFL and FFC (apart from handling automatic differentiation).
> Should I go ahead Martin?

I don't mind. The derivative of erf is easy to add in pure UFL.
The fast and accurate C++ implementation of the actual
function is the hardest part for this kind of functions.
Search for e.g. "Cos" and "cos" in all UFL files to find
relevant places to change.

Should UFL simply treat this kind of functions as named scalar functions
along the lines of cos, sin, etc.? So far all the special functions are
found in <cmath>, while this one must be implemented by the
form compiler. We may want to add other special functions later,
so we could f.ex. provide reference implementations of them bundled
with UFL.

Martin

Revision history for this message
Praveen C (cpraveen) said :
#3

That would be great.

Revision history for this message
Martin Sandve Alnæs (martinal) said :
#4

You could also take a look at the FEniCS book, there's a chapter there
about some geology application where they use erf, so they must have
solved it some way.

Martin

On 20 October 2011 11:15, Praveen C
<email address hidden> wrote:
> Question #175381 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/175381
>
> Praveen C posted a new comment:
> That would be great.
>
> --
> You received this question notification because you are a member of
> DOLFIN Team, which is an answer contact for DOLFIN.
>

Revision history for this message
Kristian B. Ølgaard (k.b.oelgaard) said :
#5

On 20 October 2011 11:10, Martin Sandve Alnæs
<email address hidden> wrote:
> Question #175381 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/175381
>
> Martin Sandve Alnæs proposed the following answer:
> On 20 October 2011 10:45, Kristian B. Ølgaard
> <email address hidden> wrote:
>> Question #175381 on DOLFIN changed:
>> https://answers.launchpad.net/dolfin/+question/175381
>>
>>    Status: Open => Answered
>>
>> Kristian B. Ølgaard proposed the following answer:
>> On 20 October 2011 06:45, Praveen C
>> <email address hidden> wrote:
>>> New question #175381 on DOLFIN:
>>> https://answers.launchpad.net/dolfin/+question/175381
>>>
>>> Hello
>>>
>>> I want to use the error function erf (http://en.wikipedia.org/wiki/Error_function) in a form. Is it possible ? Using "erf" gives me an error.
>>
>> This function is currently not supported but it shouldn't be too
>> difficult to implement. I can add the pieces to get this working in
>> UFL and FFC (apart from handling automatic differentiation).
>> Should I go ahead Martin?
>
> I don't mind. The derivative of erf is easy to add in pure UFL.
> The fast and accurate C++ implementation of the actual
> function is the hardest part for this kind of functions.
> Search for e.g. "Cos" and "cos" in all UFL files to find
> relevant places to change.

OK.

> Should UFL simply treat this kind of functions as named scalar functions
> along the lines of cos, sin, etc.? So far all the special functions are
> found in <cmath>, while this one must be implemented by the
> form compiler. We may want to add other special functions later,
> so we could f.ex. provide reference implementations of them bundled
> with UFL.

I was thinking of just using the one from <cmath>, a quick test
appears to be working but our test volunteer will soon find out :)

Kristian

> Martin
>
> --
> You received this question notification because you are a member of
> DOLFIN Team, which is an answer contact for DOLFIN.
>

Revision history for this message
Praveen C (cpraveen) said :
#6

Can I use the snapshot from tomorrow to try the erf function ? Or do I have to build from sources ?

Thanks
praveen

Revision history for this message
Marie Rognes (meg-simula) said :
#7

On 10/20/2011 11:25 AM, Martin Sandve Alnæs wrote:
> Question #175381 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/175381
>
> Martin Sandve Alnæs proposed the following answer:
> You could also take a look at the FEniCS book, there's a chapter there
> about some geology application where they use erf, so they must have
> solved it some way.

That chapter uses 'erf' when defining an Expression. If that if what
you want, go ahead and take a look.

--
Marie

> Martin
>
> On 20 October 2011 11:15, Praveen C
> <email address hidden> wrote:
>> Question #175381 on DOLFIN changed:
>> https://answers.launchpad.net/dolfin/+question/175381
>>
>> Praveen C posted a new comment:
>> That would be great.
>>
>> --
>> You received this question notification because you are a member of
>> DOLFIN Team, which is an answer contact for DOLFIN.
>>

Revision history for this message
Praveen C (cpraveen) said :
#8

Marie is right. Expression is not a problem (I think) since I can use erf from python (2.7 has erf but not older versions of python). But I want to do something like erf(u) in a form where u = Function(some function space).

Revision history for this message
Martin Sandve Alnæs (martinal) said :
#9

On 20 October 2011 11:25, Kristian B. Ølgaard
<email address hidden> wrote:
> Question #175381 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/175381
>
> Kristian B. Ølgaard proposed the following answer:
> On 20 October 2011 11:10, Martin Sandve Alnæs
> <email address hidden> wrote:
>> Question #175381 on DOLFIN changed:
>> https://answers.launchpad.net/dolfin/+question/175381
>>
>> Martin Sandve Alnæs proposed the following answer:
>> On 20 October 2011 10:45, Kristian B. Ølgaard
>> <email address hidden> wrote:
>>> Question #175381 on DOLFIN changed:
>>> https://answers.launchpad.net/dolfin/+question/175381
>>>
>>>    Status: Open => Answered
>>>
>>> Kristian B. Ølgaard proposed the following answer:
>>> On 20 October 2011 06:45, Praveen C
>>> <email address hidden> wrote:
>>>> New question #175381 on DOLFIN:
>>>> https://answers.launchpad.net/dolfin/+question/175381
>>>>
>>>> Hello
>>>>
>>>> I want to use the error function erf (http://en.wikipedia.org/wiki/Error_function) in a form. Is it possible ? Using "erf" gives me an error.
>>>
>>> This function is currently not supported but it shouldn't be too
>>> difficult to implement. I can add the pieces to get this working in
>>> UFL and FFC (apart from handling automatic differentiation).
>>> Should I go ahead Martin?
>>
>> I don't mind. The derivative of erf is easy to add in pure UFL.
>> The fast and accurate C++ implementation of the actual
>> function is the hardest part for this kind of functions.
>> Search for e.g. "Cos" and "cos" in all UFL files to find
>> relevant places to change.
>
> OK.
>
>> Should UFL simply treat this kind of functions as named scalar functions
>> along the lines of cos, sin, etc.? So far all the special functions are
>> found in <cmath>, while this one must be implemented by the
>> form compiler. We may want to add other special functions later,
>> so we could f.ex. provide reference implementations of them bundled
>> with UFL.
>
> I was thinking of just using the one from <cmath>, a quick test
> appears to be working but our test volunteer will soon find out :)

Ok. But just so you know, that's C++11, so
even quite new compilers may not have it.
Seems that bessel functions are part of TR1,
so we could add those as well quite easily (later).

Martin

Revision history for this message
Kristian B. Ølgaard (k.b.oelgaard) said :
#10

On 20 October 2011 11:30, Praveen C
<email address hidden> wrote:
> Question #175381 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/175381
>
> Praveen C posted a new comment:
> Can I use the snapshot from tomorrow to try the erf function ? Or do I
> have to build from sources ?

It should be available now if you get the development version using Dorsal.
Can you please test it and report back if it behaves as you expect it to?

Kristian

> Thanks
> praveen
>
> --
> You received this question notification because you are a member of
> DOLFIN Team, which is an answer contact for DOLFIN.
>

Revision history for this message
Praveen C (cpraveen) said :
#11

I have never built with dorsal before. I tried it on many of my computers but all of them failed with different errors.

Will the nightly binary snapshots include the updated code ?

praveen

Revision history for this message
Johannes Ring (johannr) said :
#12

Yes, the binary snapshots will include this update. What platform are you on?

Revision history for this message
Praveen C (cpraveen) said :
#13

Snow Leopard with system python (2.6)

Revision history for this message
Harish Narayanan (hnarayanan) said :
#14

Dorsal should work with Snow Leopard, as it is a reasonably well-tested platform. If you're interested in getting it to work, please file a separate bug report indicating what the problem is, and I will help you fix it. That way, you don't need to wait for binary snapshots in order to get new features/bug fixes.

Revision history for this message
Praveen C (cpraveen) said :
#15

I tried with dorsal-1.0-beta and get problems with petsc/slepc. Should I try dorsal-0.8.2

Basically, I see that PETSC_ARCH=installed-petsc is used and it is looking for file .../FEniCS/installed-petsc/conf/petscvariables but the file is located at .../FEniCS/conf/petscvariables.

================================================================================
SLEPc Configuration
================================================================================

SLEPc source directory:
 /Users/praveen/Applications/FEniCS/src/slepc-3.1-p6
SLEPc install directory:
 /Users/praveen/Applications/FEniCS
PETSc directory:
 /Users/praveen/Applications/FEniCS
Architecture "installed-petsc" with double precision real numbers

  **
  ** Before running "make" your PETSC_ARCH must be specified with:
  ** ** setenv PETSC_ARCH installed-petsc (csh/tcsh)
  ** ** PETSC_ARCH=installed-petsc ; export PETSC_ARCH (sh/bash)
  **

/Users/praveen/Applications/FEniCS/conf/variables:119: /Users/praveen/Applications/FEniCS/installed-petsc/conf/petscvariables: No such file or directory
/Users/praveen/Applications/FEniCS/conf/rules:1005: /Users/praveen/Applications/FEniCS/installed-petsc/conf/petscrules: No such file or directory
make: *** No rule to make target `/Users/praveen/Applications/FEniCS/installed-petsc/conf/petscrules'. Stop.
Failure with exit status: 2
Exit message: There was a problem building slepc-3.1-p6.

Revision history for this message
Praveen C (cpraveen) said :
#16

I got the snapshot few minutes back and tried it. But the erf function is not found.

praveen

PS: Snow leopard

Revision history for this message
Johannes Ring (johannr) said :
#17

I have rebuilt the binary snapshot for Snow Leopard and it seems that the erf function is in place. At least I can do "pydoc ufl.erf".

Could you download a new copy and try again?

Revision history for this message
Praveen C (cpraveen) said :
#18

The snow leopard binary is using python2.6 but that version of python does not have math.erf so I get an error

  File "/Users/praveen/Applications/FEniCS.app/Contents/Resources/lib/python2.6/site-packages/ufl/operators.py", line 456, in erf
    return _mathfunction(f, Erf, math.erf)
AttributeError: 'module' object has no attribute 'erf'

Revision history for this message
Best Martin Sandve Alnæs (martinal) said :
#19

I've pushed a fix for this.

Praveen, you can do a quick fix manually in your installed UFL source
code in your installation by doing this:

In 'ufl/operators.py', just replace 'math.erf' with None.

In 'ufl/mathfunctions.py', delete the lines:
            if isinstance(argument, (ScalarValue, Zero)):
                return FloatValue(math.erf(float(argument)))
This last step may not be necessary, depending on how you use it.

Martin

On 21 October 2011 14:10, Praveen C
<email address hidden> wrote:
> Question #175381 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/175381
>
> Praveen C posted a new comment:
> The snow leopard binary is using python2.6 but that version of python
> does not have math.erf so I get an error
>
>  File "/Users/praveen/Applications/FEniCS.app/Contents/Resources/lib/python2.6/site-packages/ufl/operators.py", line 456, in erf
>    return _mathfunction(f, Erf, math.erf)
> AttributeError: 'module' object has no attribute 'erf'
>
> --
> You received this question notification because you are a member of
> DOLFIN Team, which is an answer contact for DOLFIN.
>

Revision history for this message
Praveen C (cpraveen) said :
#20

Thanks Martin, that seems to have worked. I am still resolving other issues in this problem. Once I get the results, I will report back here.

Revision history for this message
Praveen C (cpraveen) said :
#21

Thanks Martin Sandve Alnæs, that solved my question.