random function

Asked by Achim Schroll on 2010-03-25

why does my random function return just zero?

from dolfin import *
from random import *

# random funtion
class myrand(Function):
    def __init__(self, V):
        Function.__init__(self, V)
    def eval(self, values, x):
        values[0] = gauss(0,1)

# Create mesh and finite element
mesh = UnitSquare(20, 20)
V = FunctionSpace(mesh, "CG", 1)

randf = myrand(V)

Question information

Language:
English Edit question
Status:
Solved
For:
DOLFIN Edit question
Assignee:
No assignee Edit question
Solved by:
Achim Schroll
Solved:
2010-03-25
Last query:
2010-03-25
Last reply:
2010-03-25
Johan Hake (johan-hake) said : #1

Achim!

Just change Function -> Expression and you are good to go!

Johan

On Thursday March 25 2010 05:21:34 Achim Schroll wrote:
> New question #105494 on DOLFIN:
> https://answers.launchpad.net/dolfin/+question/105494
>
> why does my random function return just zero?
>
> from dolfin import *
> from random import *
>
> # random funtion
> class myrand(Function):
> def __init__(self, V):
> Function.__init__(self, V)
> def eval(self, values, x):
> values[0] = gauss(0,1)
>
> # Create mesh and finite element
> mesh = UnitSquare(20, 20)
> V = FunctionSpace(mesh, "CG", 1)
>
> randf = myrand(V)

Hi Johan, I don't get it. Expression gives the error...

class myrand(Expression):
    def eval(self, values, x):
        values[0] = gauss(0,1)

mesh = UnitSquare(20, 20)
V = FunctionSpace(mesh, "CG", 1)

randf1 = myrand(V)

Traceback (most recent call last):
  File "addnoise_ny.py", line 37, in <module>
    randf1 = myrand(V)
  File "/usr/lib/python2.5/site-packages/dolfin/expression.py", line 120, in __init__
    raise TypeError, "Expression need to be initialized using either a 'cpp.FunctionSpace', using kwarg 'V', or an 'ufl.FiniteElement' using kwarg 'element'."
TypeError: Expression need to be initialized using either a 'cpp.FunctionSpace', using kwarg 'V', or an 'ufl.FiniteElement' using kwarg 'element'.

it's ok, I got it now - sorry!

Johan Hake (johan-hake) said : #4

Hi again!

Sorry for the incomplete answer. In dolfin 0.9.4 I think you have to pass the
Function space using the keyword argument V=V when you instantiate your
Expression.

  randf1 = myrand(V=V)

It's actually there in the error message ;)

In later dolfin version you only need to instantiate your Expression using

  randf1 = myrand()

Johan

On Thursday March 25 2010 08:33:48 Achim Schroll wrote:
> Question #105494 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/105494
>
> Status: Answered => Open
>
> Achim Schroll is still having a problem:
> Hi Johan, I don't get it. Expression gives the error...
>
> class myrand(Expression):
> def eval(self, values, x):
> values[0] = gauss(0,1)
>
> mesh = UnitSquare(20, 20)
> V = FunctionSpace(mesh, "CG", 1)
>
> randf1 = myrand(V)
>
> Traceback (most recent call last):
> File "addnoise_ny.py", line 37, in <module>
> randf1 = myrand(V)
> File "/usr/lib/python2.5/site-packages/dolfin/expression.py", line 120,
> in __init__ raise TypeError, "Expression need to be initialized using
> either a 'cpp.FunctionSpace', using kwarg 'V', or an 'ufl.FiniteElement'
> using kwarg 'element'." TypeError: Expression need to be initialized using
> either a 'cpp.FunctionSpace', using kwarg 'V', or an 'ufl.FiniteElement'
> using kwarg 'element'.