← Back to team overview

dolfin team mailing list archive

Re: Plot a constant Expression with different values in the x and y directions

 

> Hi,
>
> Anna Blechingberg wrote:
>> Hi,
>>
>> I have difficulties with plotting a constant function in dolfin version
>> 0.9.4. I can do it in version 0.9.3, but need it to work in 0.9.4 too.
>>
>> The problem is that I have different values in the x and y directions, 0
>> and 9.82 respectively.
>>
>> The code is as follows:
>>
>> ------------
>>
>> mesh = Mesh("circle.xml.gz")
>>
>> # Define function space
>> V = VectorFunctionSpace(mesh, "CG", 1)
>> W = FunctionSpace(mesh, "CG", 1)
>>
>> # I have tried with two different definitions of the function, but don't
>> know which one is correct:
>>
>> # Expression for acceleration force
>> class Acceleration(Expression):
>>     def eval(self, value, x):
>>         value[0] = 0.0
>>         value[1] = -9.82 # 1g acceleration downwards
>>
>> f = Acceleration(V=W)
>>
>> g = Constant(mesh,(0, 9.82)) # I can plot g in dolfin version 0.9.3 and
>> it works correctly there
>>
>> -----------------------------------------
>>
>> Do the Constant definition of g also work in dolfin version 0.9.4 even
>> if I can't plot it?
>
> The function interface has changed a bit in 0.9.4, such that you don't
> define Constant function any longer with a mesh, you just pass in the
> desired value.
>
> If you want to plot it, just used the mesh argument of the plot
> function, e.g.
>
> c = Constant((0,1))
> m = UnitSquare(3,3)
> plot(c,mesh=m)

I'm sorry, but it doesn't work. I have tried with your and several other
different versions of defining a Constant:


simula@SRL0427:~/annablec/Elasticity/LinearElProgrammes$ python
Quasistatic_1g.py
Traceback (most recent call last):
  File "Quasistatic_1g.py", line 190, in <module>
    f = Constant((0,-9.82))
TypeError: Constant() takes exactly 2 arguments (1 given)
simula@SRL0427:~/annablec/Elasticity/LinearElProgrammes$ python
Quasistatic_1g.py
Traceback (most recent call last):
  File "Quasistatic_1g.py", line 190, in <module>
    f = Constant((0,-9.82), mesh)
  File "/usr/lib/python2.6/dist-packages/dolfin/constant.py", line 42, in
Constant
    cpp.error("Unable to create constant from expression, must be an int,
float, tuple or list: " + str(value) + " (of type " + str(type(value))
+ ")")
  File "/usr/lib/python2.6/dist-packages/dolfin/cpp.py", line 989, in error
    return _cpp.error(*args)
RuntimeError: *** Error: Unable to create constant from expression, must
be an int, float, tuple or list: <Mesh of topological dimension 2
(triangles) with 1366 vertices and 2606 cells, ordered> (of type <class
'dolfin.cpp.Mesh'>)
simula@SRL0427:~/annablec/Elasticity/LinearElProgrammes$ python
Quasistatic_1g.py
Object cannot be plotted directly, projecting to piecewise linears.
Traceback (most recent call last):
  File "Quasistatic_1g.py", line 191, in <module>
    plot(f,mesh=mesh)
  File "/usr/lib/python2.6/dist-packages/dolfin/plot.py", line 76, in
dolfin_plot
    raise RuntimeError, ("Don't know how to plot given object and
projection failed: " + str(object))
RuntimeError: Don't know how to plot given object and projection failed:
<Expression in <Mixed element: (<DG0 on a <triangle of degree 1>>, <DG0 on
a <triangle of degree 1>>)>>
simula@SRL0427:~/annablec/Elasticity/LinearElProgrammes$ python
Quasistatic_1g.py
Traceback (most recent call last):
  File "Quasistatic_1g.py", line 190, in <module>
    f = Constant(0,-9.82)
  File "/usr/lib/python2.6/dist-packages/dolfin/constant.py", line 26, in
Constant
    V = FunctionSpace(mesh, "Discontinuous Lagrange", 0)
  File "/usr/lib/python2.6/dist-packages/dolfin/functionspace.py", line
178, in __init__
    cpp.error("Illegal mesh for creation of function space, not a mesh: "
+ str(mesh))
  File "/usr/lib/python2.6/dist-packages/dolfin/cpp.py", line 989, in error
    return _cpp.error(*args)
RuntimeError: *** Error: Illegal mesh for creation of function space, not
a mesh: 0
simula@SRL0427:~/annablec/Elasticity/LinearElProgrammes$ python
ConstantFunctionTest.py
Traceback (most recent call last):
  File "ConstantFunctionTest.py", line 5, in <module>
    f = Constant((0, -9.82))
TypeError: Constant() takes exactly 2 arguments (1 given)
------------------------------

So I tried to do exactly what the Constant doc-string says:

-------------
Help on function Constant in module dolfin.constant:

Constant(mesh, value)
    Create constant-valued function with given value on mesh.

    The value may be either a single scalar value or a tuple/list of
    values for vector-valued functions.
-------------

Program:
------------
from dolfin import *
import sys

# Load mesh
mesh = Mesh("circle.xml.gz")

# Scale mesh according to real size of head (diameter 11 cm)
scaling_factor = 0.11/2/0.5
mesh.coordinates()[:] = mesh.coordinates()*scaling_factor

# Defining acceleration
f = Constant(mesh, (0, 9.82))

# Plots acceleration force
plot(f,mesh=mesh)
interactive()
--------------

Gives:
-----
simula@SRL0427:~/annablec/Elasticity/LinearElProgrammes$ python
ConstantFunctionTest.py
Object cannot be plotted directly, projecting to piecewise linears.
Traceback (most recent call last):
  File "ConstantFunctionTest.py", line 17, in <module>
    plot(f,mesh=mesh)
  File "/usr/lib/python2.6/dist-packages/dolfin/plot.py", line 76, in
dolfin_plot
    raise RuntimeError, ("Don't know how to plot given object and
projection failed: " + str(object))
RuntimeError: Don't know how to plot given object and projection failed:
<Expression in <Mixed element: (<DG0 on a <triangle of degree 1>>, <DG0 on
a <triangle of degree 1>>)>>
--------


So I believe that the problem is in the plot module.

Regards, Anna


> HTH,
> Andre
>
> _______________________________________________
> Mailing list: https://launchpad.net/~dolfin
> Post to     : dolfin@xxxxxxxxxxxxxxxxxxx
> Unsubscribe : https://launchpad.net/~dolfin
> More help   : https://help.launchpad.net/ListHelp
>





Follow ups