This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

How to extract value of function in current point?

+2 votes

For take coordinates of point i can use x[i] (x[0] for example). How to extract value of function in current point?
I need to extract value in expression class, because i try to define Neumann boundary like u'=k*u on boundary.

asked Aug 23, 2013 by Sheva FEniCS Novice (910 points)
edited Aug 24, 2013 by Sheva

1 Answer

+4 votes
 
Best answer
p = Point()
f = Function(V)
try:
    print f(p)
except TypeError: # issue 68
    print f(p.x(), p.y(), p.z())

Issue68 has been already fixed but affects version $\le$ 1.2.0.

answered Aug 24, 2013 by Jan Blechta FEniCS Expert (51,420 points)
selected Aug 25, 2013 by Sheva

I need extract value in expression class, because i try to define Neumann boundary like u'=k*u on boundary. If i use your method i have "error: ‘u’ was not declared in this scope".

Please, provide short and complete code producing this error.

I use u in expression class, but u define in main function, and when class creating u is not declared. May be i should define u as global function?

Please, provide short and complete code producing this error.$ $

Function u();
class dUdN : public Expression
{
public:

dUdN() : Expression() {}

void eval(Array& values, const Array& x) const
{
values[0] = (x[2]<=DOLFIN_EPS)?(u(x[0],x[1],x[2])):0.0;
}
};
int main()
{


Poisson::FunctionSpace V(mesh);
Poisson::BilinearForm a(V, V);
Poisson::LinearForm L(V);
Function u(V);


}

In member function ‘virtual void dUdN::eval(dolfin::Array&, const dolfin::Array&) const’:
/home/sheva/cpp/main.cpp:48:53: error: too many arguments to function ‘dolfin::Function u()’

and when i write
class dUdN : public Expression
{
public:

dUdN() : Expression() {}
void eval(Array& values, const Array& x) const
{
Point p(x[0],x[1],x[2]);
values[0] = (x[2]<=DOLFIN_EPS)?(u(p)):0.0;
}
};
i have this error again/

Ok, syntax

u(x[0], x[1], x[2])
u(p)

works only in python. In C++ you need to use Function::eval method to evaluate function. Although your approach could work (in fixed-point iteration manner) it is in fact very poor implementation (in a view of available tools within DOLFIN). Robin condition can be implemented directly into variational form. For linear Poisson problem this way

v = TestFunction(element)
u = TrialFunction(element)
k = Constant(element)
#k = Coefficient(element) # for non-constant k
a = ... - k*u*v*ds

(not-tested).

thx. It's realy better write boudary condition in UFL.

Hi. How to extract value of function in current point from main function? For example, i have this code.
Function u;
for (CellIterator c(mesh); !c.end(); ++c)
for (VertexIterator v0(
c); !v0.end(); ++v0)
for (VertexIterator v1(*v0); !v1.end(); ++v1)
cout << *v1 << endl;
How i can get value of u in v1?

...