Assigning new values

Asked by B. Emek Abali

Hi all,

i want to change the values on each node, but it seems only logical to me not to FEniCS:
-----------------------------------------------
from dolfin import *
import numpy
class before(Expression):
 def eval(self, out, x):
  out[0] = 1.0
  out[1] = x[0]
  out[2] = x[1]
  out[3] = x[2]
 def value_shape(self):
  return (4,)

class after(Expression):
 def __init__(self, c):
  self.a = c.split(deepcopy=True)[0]
  self.b = c.split(deepcopy=True)[1]
 def eval(self, out, x):
  out[0] = self.a**2
  out[1] = self.b[0] + 1.0
  out[2] = self.b[1] + 2.0
  out[3] = self.b[2] + 3.0
 def value_shape(self):
  return (4,)

mesh = Box(0.0,0.0,0.0 , 100.0,10.0,10.0 , 5,1,1)
a_space = FunctionSpace(mesh,'CG',1)
b_space = VectorFunctionSpace(mesh,'CG',1)
space = MixedFunctionSpace([a_space,b_space])
c=Function(space)

before_values=before()
c.interpolate(before_values)

after_values=after(c)
c.interpolate(after_values)
-----------------------------------------------

...
 out[0] = self.a**2
ValueError: setting an array element with a sequence.

-------------------------------------------------

any suggestions would help a lot! Thanks a lot...

Question information

Language:
English Edit question
Status:
Solved
For:
DOLFIN Edit question
Assignee:
No assignee Edit question
Solved by:
Johan Hake
Solved:
Last query:
Last reply:
Revision history for this message
Best Johan Hake (johan-hake) said :
#1

You are mixing UFL algebra with DOLFIN algebra.

Try this after Expression instead.

class after(Expression):
        def __init__(self, c):
                self.a = c.split(deepcopy=True)[0]
                self.b0, self.b1, self.b2= c.split()[1].split(deepcopy=True)
        def eval(self, out, x):
                out[0] = self.a(x)**2
                out[1] = self.b0(x) + 1.0
                out[2] = self.b1(x) + 2.0
                out[3] = self.b2(x) + 3.0
        def value_shape(self):
                return (4,)

Johan

On Thursday July 14 2011 12:35:54 B. Emek Abali wrote:
> New question #164816 on DOLFIN:
> https://answers.launchpad.net/dolfin/+question/164816
>
> Hi all,
>
> i want to change the values on each node, but it seems only logical to me
> not to FEniCS: -----------------------------------------------
> from dolfin import *
> import numpy
> class before(Expression):
> def eval(self, out, x):
> out[0] = 1.0
> out[1] = x[0]
> out[2] = x[1]
> out[3] = x[2]
> def value_shape(self):
> return (4,)
>
> class after(Expression):
> def __init__(self, c):
> self.a = c.split(deepcopy=True)[0]
> self.b = c.split(deepcopy=True)[1]
> def eval(self, out, x):
> out[0] = self.a**2
> out[1] = self.b[0] + 1.0
> out[2] = self.b[1] + 2.0
> out[3] = self.b[2] + 3.0
> def value_shape(self):
> return (4,)
>
> mesh = Box(0.0,0.0,0.0 , 100.0,10.0,10.0 , 5,1,1)
> a_space = FunctionSpace(mesh,'CG',1)
> b_space = VectorFunctionSpace(mesh,'CG',1)
> space = MixedFunctionSpace([a_space,b_space])
> c=Function(space)
>
> before_values=before()
> c.interpolate(before_values)
>
> after_values=after(c)
> c.interpolate(after_values)
> -----------------------------------------------
>
> ...
> out[0] = self.a**2
> ValueError: setting an array element with a sequence.
>
> -------------------------------------------------
>
> any suggestions would help a lot! Thanks a lot...

Revision history for this message
B. Emek Abali (bilenemek) said :
#2

Thanks Johan, i assume then eval evaluates a(x) in each node of the c's underlying space, thus a(x) do not have to be in the same space at all, or?

Let me try... dough!
--------------------------------------------------------------------------------
line 27, in <module>
    a_space = FunctionSpace(mesh,'CG',1)
  File "/usr/lib/python2.7/dist-packages/dolfin/function/functionspace.py", line 237, in __init__
    FunctionSpaceBase.__init__(self, mesh, element)
  File "/usr/lib/python2.7/dist-packages/dolfin/function/functionspace.py", line 44, in __init__
    ufc_element, ufc_dofmap = jit(self._ufl_element)
  File "/usr/lib/python2.7/dist-packages/dolfin/compilemodules/jit.py", line 47, in mpi_jit
    return local_jit(*args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/dolfin/compilemodules/jit.py", line 114, in jit
    return jit_compile(form, parameters=p, common_cell=common_cell)
  File "/usr/lib/python2.7/dist-packages/ffc/jitcompiler.py", line 62, in jit
    return jit_element(object, parameters)
  File "/usr/lib/python2.7/dist-packages/ffc/jitcompiler.py", line 162, in jit_element
    (compiled_form, module, form_data) = jit_form(form, parameters)
  File "/usr/lib/python2.7/dist-packages/ffc/jitcompiler.py", line 122, in jit_form
    compile_form(preprocessed_form, prefix=jit_object.signature(), parameters=parameters)
  File "/usr/lib/python2.7/dist-packages/ffc/compiler.py", line 135, in compile_form
    analysis = analyze_forms(forms, object_names, parameters)
  File "/usr/lib/python2.7/dist-packages/ffc/analysis.py", line 49, in analyze_forms
    forms = tuple(_analyze_form(form, object_names, parameters, common_cell) for form in forms)
  File "/usr/lib/python2.7/dist-packages/ffc/analysis.py", line 49, in <genexpr>
    forms = tuple(_analyze_form(form, object_names, parameters, common_cell) for form in forms)
  File "/usr/lib/python2.7/dist-packages/ffc/analysis.py", line 110, in _analyze_form
    ffc_assert(len(form.integrals()),
AttributeError: 'FormData' object has no attribute 'integrals'
---------------------------------------------------------------------------------------------
I am using dolfin 0.9.10 over the ppa:fenics/ppa on ubuntu 11.04

Revision history for this message
B. Emek Abali (bilenemek) said :
#3

Thanks Johan Hake, that solved my question.

Revision history for this message
B. Emek Abali (bilenemek) said :
#4

sorry, my fault of partial updating, it runs as suggested:

from dolfin import *
import numpy
class before(Expression):
 def eval(self, out, x):
  out[0] = 1.0
  out[1] = x[0]
  out[2] = x[1]
  out[3] = x[2]
 def value_shape(self):
  return (4,)

class after(Expression):
 def __init__(self, c):
  self.a = c.split(deepcopy=True)[0]
  self.b0 = c.split()[1].split(deepcopy=True)[0]
  self.b1 = c.split()[1].split(deepcopy=True)[1]
  self.b2 = c.split()[1].split(deepcopy=True)[2]
 def eval(self, out, x):
  out[0] = self.a(x)**2
  out[1] = self.b0(x) + 1.0
  out[2] = self.b1(x) + 2.0
  out[3] = self.b2(x) + 3.0
 def value_shape(self):
  return (4,)

mesh = Box(0.0,0.0,0.0 , 100.0,10.0,10.0 , 5,1,1)
a_space = FunctionSpace(mesh,'CG',1)
b_space = VectorFunctionSpace(mesh,'CG',1)
space = MixedFunctionSpace([a_space,b_space])
c=Function(space)

before_values=before()
c.interpolate(before_values)

after_values=after(c)
c.interpolate(after_values)