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

Elementwise Minimum of a Function

0 votes

Hello,

I'm trying to compute the element wise minimum of a function, that is I need a P_0 function which is smaller than the original function.

mesh = UnitSquareMesh( 10, 10 )
space = FunctionSpace( mesh, "CG", 1 )
spaceP0 = FunctionSpace( mesh, "DG", 0 )
fun = interpolate( Expression( "sin( x[0] * pi ) * sin( x[1] * pi )" ), space )
funP0 = Function( spaceP0 )
# compute values of funP0 such that funP0 <= fun

Has anybody an idea how to do this fast and high level?

Thanks in advance,
Jo

asked Jan 13, 2014 by jenom FEniCS Novice (690 points)

right now i'm using something like this as a workaround:

mesh = UnitSquareMesh( 3, 3 )
space = FunctionSpace( mesh, "CG", 1 )
space0 = FunctionSpace( mesh, "DG", 0 )
fun = interpolate( Expression( "sin( x[0] * pi ) * sin( x[1] * pi )" ), space )

funVals = fun.vector().array()
vert2dofP1 = vertex_to_dof_map( space )
vertVals = funVals[vert2dofP1]

cells = mesh.cells()
cellVals = vertVals[ cells ]
minVals = numpy.min( cellVals, 1 )

funMin = Function( space0 )
funMin.vector()[:] = minVals

plot( funMin )
interactive()

The problem is, that this only works for P1 and not for Pk (no dof map as dofs are not on vertices). Furthermore this assumes the ordering of the P0 dofs (again no dof map).

1 Answer

+3 votes
 
Best answer

Hi, here's a way to solve your problem using dofmaps

from dolfin import *

mesh = UnitSquareMesh(20, 20)
space = FunctionSpace(mesh, "CG", 4)
space0 = FunctionSpace(mesh, "DG", 0)

fun = interpolate(Expression("sin(4*x[0]*pi)*sin(4*x[1]* pi)"), space)
fun0 = Function(space0)

vec = fun.vector()
vec0 = fun0.vector()

dofmap = space.dofmap()
dofmap0 = space0.dofmap()

for cell in cells(mesh):
    i = cell.index()
    vec0[dofmap0.cell_dofs(i)] = vec[dofmap.cell_dofs(i)].min()

plot(fun, interactive=True)
plot(fun0, interactive=True)
answered Jan 13, 2014 by MiroK FEniCS Expert (80,920 points)
selected Jan 13, 2014 by jenom

Thanks! This works like a charm. And now I have learned how to properly use dofmaps!

...