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

Filling Function object

0 votes

Hi,
I have been using COMSOL previously. Now I want to compare results from COMSOL to those computed in FeniCS. I would like to work with a solution from COMSOL in the same way as with the FeniCS result for example for comparison purposes or even visualization.

I think it would be vital if there is the way how to manually fill the Function object with the data for particular DOF base functions. If I will obtain DOF related values with proper ordering and if I am able to export mesh and specify correctly function space, how I can fill up the Function object with the data?

In the manual http://fenicsproject.org/documentation/dolfin/dev/python/programmers-reference/cpp/function/Function.html there is a section

Function(V, filename)

Create function from vector of dofs stored to file

Arguments

    V (FunctionSpace)

        The function space.
    filename_vector (str)

        The name of the file containing the vector.
    filename_dofdata (str)

        The name of the file containing the dofmap data.

I do not know what is the proper structure of the file which could be used as a argument for this constructor? In C++ this function is probably that with a signature

Function::Function(const FunctionSpace& V, std::string filename)
  : Hierarchical<Function>(*this),
    _function_space(reference_to_no_delete_pointer(V)),
    allow_extrapolation(dolfin::parameters["allow_extrapolation"])

maybe it is also possible to use function

Function::Function(boost::shared_ptr<const FunctionSpace> V, boost::shared_ptr<GenericVector> x) function 

What is a proper format of the file to pass to the constructor? Can I save this file somehow from my solution object? What is a DOF ordering in boost::shared_ptr x? Is it possible to do all the operations also from Python?

Thanks Vojtech.

asked Apr 28, 2014 by kulvv1am FEniCS Novice (270 points)

3 Answers

0 votes

I'm far from an expert on this, but it seemed to me that there was a similar question here that discussed doing this with Matlab results.

answered Apr 28, 2014 by timm FEniCS User (2,100 points)
0 votes

assume that the order of the basis functions in V is the same though. you can do as follow

from future import division
from dolfin import *
import numpy
mesh=UnitSquareMesh(10, 10)

V = VectorFunctionSpace(mesh, "Lagrange", 1)
U = Function(V)
U.vector()[:] = vector_from_Comsol

answered Apr 28, 2014 by BrianB FEniCS Novice (190 points)

Well the problem is that I do not know what the order of basis functions is in the FeniCS . Even though the Comsol is proprietary software, by means of xmeshinfo function I can figure out proper DOF ordering, however the DOF mapping in FeniCS is quite contraintuitive for me.

+1 vote

a) The file should be in for instance DOLFIN XML format
b) Yes, it should be possible to do this from Python
c) The key is to utilize the information in the local-to-global mappings of the degrees of freedom (DofMaps), take a look at the answer here:
http://fenicsproject.org/qa/2715/coordinates-u_nodal_values-using-numerical-source-function?show=2721#a2721

answered Apr 29, 2014 by Marie E. Rognes FEniCS User (5,380 points)

This seems to be a good solution for those finite element spaces where there is no more than one DOF value for certain nodal point. It is a case of my CG spaces so I will go this way. In COMSOL moreover there is no clear (extractable) correspondence between DOFs and triangles meaning Dolfin XML format is hard for me to assemble.

So I will now do the trick using V.dofmap().tabulate_all_coordinates(mesh) function. However I just wonder what I would do if there are more DOF values for one nodal point.

Thank you for your answer.

...