Fenics as Solver

About the development of the FEM module/workbench.

Moderator: bernd

joha2
Posts: 215
Joined: Tue Oct 11, 2016 9:48 pm

Re: Fenics as Solver

Postby joha2 » Sun Mar 25, 2018 9:59 pm

Thanks @bernd! I would suggest for the unit test to export some parts and reimport them and afterwards verifying that the mesh points are nearly the same. What do you think?

Best regards
Johannes
User avatar
bernd
Posts: 7749
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: Fenics as Solver

Postby bernd » Tue Mar 27, 2018 4:08 pm

to start really simply you could just extend the test for one tetra10 element See https://github.com/FreeCAD/FreeCAD/blob ... y#L76-L153 ATM unv and inp export and import are supported.

*.inp --> https://github.com/FreeCAD/FreeCAD/blob ... #L135-L144
*.unv --> https://github.com/FreeCAD/FreeCAD/blob ... #L146-L153

just add your fenics file formats. The aim is to make such a simple test for every possible mesh element.

bernd
joha2
Posts: 215
Joined: Tue Oct 11, 2016 9:48 pm

Re: Fenics as Solver

Postby joha2 » Mon Apr 02, 2018 9:40 pm

Heyhey,

@bernd I've just submitted a PR on your femdev branch where I fixed the XML import. For the unit test we only could use tet4 elements since XML only supports linear geometry.

BTW: How is the development cycle for the tests? Do I have to perform the ./FreeCAD --run-tests 0 command every time?

Best wishes
Johannes
User avatar
bernd
Posts: 7749
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: Fenics as Solver

Postby bernd » Tue Apr 03, 2018 4:12 am

joha2 wrote:
Mon Apr 02, 2018 9:40 pm
For the unit test we only could use tet4 elements since XML only supports linear geometry.
We could add a test for tet4 too. Just as a copy of tet10 and some changes ...


joha2 wrote:
Mon Apr 02, 2018 9:40 pm
Do I have to perform the ./FreeCAD --run-tests 0 command every time?
No, https://github.com/FreeCAD/FreeCAD/blob ... py#L37-L78
joha2
Posts: 215
Joined: Tue Oct 11, 2016 9:48 pm

Re: Fenics as Solver

Postby joha2 » Thu Apr 05, 2018 9:58 pm

Thanks @bernd. I asked another question concerning the FC python interpreter in https://forum.freecadweb.org/viewtopic.php?f=22&t=27883
for making Fenics macros feasible. A solution was implemented by Werner: https://forum.freecadweb.org/viewtopic. ... 94#p229094
The trick was to force the FreeCAD binary to act like Python for the -c switch.

Best wishes
Johannes
HoWil
Posts: 816
Joined: Sun Jun 14, 2015 7:31 pm
Location: Austria

Re: Fenics as Solver

Postby HoWil » Fri Apr 27, 2018 6:24 am

joha2 wrote:
Thu Apr 05, 2018 9:58 pm
Thanks @bernd. I asked another question concerning the FC python interpreter in https://forum.freecadweb.org/viewtopic.php?f=22&t=27883
for making Fenics macros feasible. A solution was implemented by Werner: https://forum.freecadweb.org/viewtopic. ... 94#p229094
The trick was to force the FreeCAD binary to act like Python for the -c switch.

Best wishes
Johannes
Hi Johannes,
At https://forum.freecadweb.org/viewtopic. ... 94#p229094
You are talking about running fenics scripts in combination with Freecad.
Could you please give a basic tutorial what is run how.
Are you doing parameter-studies?
I am very interested in running fenics on jupyter and changing the geometry/mesh via fc.
If I remember correct you are working on electrostatic problems? If so this would be exactly what I also what to investigate. As a detail... I need do compute forces on a specific boundary of my geometry. Is it possible to define such a boundary in Fc and evaluate it in with fenics?
Br Howil
joha2
Posts: 215
Joined: Tue Oct 11, 2016 9:48 pm

Re: Fenics as Solver

Postby joha2 » Fri Apr 27, 2018 4:42 pm

HoWil wrote:
Fri Apr 27, 2018 6:24 am
Hi Johannes,
At https://forum.freecadweb.org/viewtopic. ... 94#p229094
You are talking about running fenics scripts in combination with Freecad.
Could you please give a basic tutorial what is run how.
Hi HoWil,
the only thing working until now is the mesh export and the execution of Fenics script as a macro within Python.
For this purpose you may just copy the simple Poisson solution script from the Fenics tutorial into your FreeCAD macro
directory and press the play button. I also wrote some interface classes to get a coupling between the GMSH mesh and Fenics.
HoWil wrote:
Fri Apr 27, 2018 6:24 am
Are you doing parameter-studies?
No, not yet.
HoWil wrote:
Fri Apr 27, 2018 6:24 am
I am very interested in running fenics on jupyter and changing the geometry/mesh via fc.
If I remember correct you are working on electrostatic problems? If so this would be exactly what I also what to investigate. As a detail... I need do compute forces on a specific boundary of my geometry. Is it possible to define such a boundary in Fc and evaluate it in with fenics?
Br Howil
I think you always can create a geometry by FreeCAD, mesh it and export its mesh groups into an XDMF file and read it into Fenics. Until now there is no reimport of the results into the FC post processing chain. I'm running out of time right now. I will come back later and add a few things to this post.

Best regards
Johannes
HoWil
Posts: 816
Joined: Sun Jun 14, 2015 7:31 pm
Location: Austria

Re: Fenics as Solver

Postby HoWil » Fri Apr 27, 2018 5:09 pm

joha2 wrote:
Fri Apr 27, 2018 4:42 pm
I'm running out of time right now. I will come back later and add a few things to this post.
Thanks.
BTW: Do you know meshio .... https://pypi.org/project/meshio/

It says

Code: Select all

There are various mesh formats available for representing unstructured meshes, e.g.,

    ANSYS msh
    DOLFIN XML
    Exodus
    H5M
    Medit
    MED/Salome
    Gmsh
    OFF
    PERMAS
    STL
    VTK
    VTU
    XDMF

meshio can read and write all of these formats and smoothly converts between them. 
BR,
HoWil
joha2
Posts: 215
Joined: Tue Oct 11, 2016 9:48 pm

Re: Fenics as Solver

Postby joha2 » Fri Apr 27, 2018 11:35 pm

HoWil wrote:
Fri Apr 27, 2018 5:09 pm
BTW: Do you know meshio .... https://pypi.org/project/meshio/
Hey HoWil, yeah I was aware of that project, since one of the project developers is a regular guest at the Fenics development Slack.
I did not integrate it into FreeCAD since it is another dependency which I don't have under control. I also had no idee how actively this
project is developed. Therefore I wrote the XDMF export myself.

Best wishes
Johannes
joha2
Posts: 215
Joined: Tue Oct 11, 2016 9:48 pm

Re: Fenics as Solver

Postby joha2 » Sat Apr 28, 2018 3:04 pm

HoWil wrote:
Fri Apr 27, 2018 6:24 am
Could you please give a basic tutorial what is run how.
First of all the obligatoric FreeCAD version information:

OS: Ubuntu 16.04.3 LTS
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.18.13622 (Git)
Build type: Unknown
Branch: master
Hash: 31cee58df6558f29908684c4d8273061c4440a54
Python version: 2.7.12
Qt version: 4.8.7
Coin version: 4.0.0a
OCC version: 6.8.0.oce-0.17
Locale: C/Default (C)

In the following I will provide a simple tutorial on how to use the usable features of FreeCAD combined with Fenics. At the moment there is no GUI implementation. So the only possibility to use Fenics is via macros (which IMHO makes most sense; only pre and post processing should be done via FreeCAD). In a first step I created a geometry containing a cube of 200x200x200 mm and subtracted two spheres at (+/-90, 0, 0) with radius 20 to have two distinct surfaces where to provide boundary conditions.

fc_fenics_demo1_screenshot.png
fc_fenics_demo1_screenshot.png (229.8 KiB) Viewed 445 times

Then I created a mesh (1st order elements! 15 mm max element size. Notice: labels of the meshgroups are used for referring to the meshgroups within the Fenics macro script; thanks HoWil for pointing this out!) and two mesh groups at the spherical boundaries with the labels 'positive' and 'negative'. The macro is given by the following code, it is a complete Fenics script with some additional ingredients to provide the FC interface.

Code: Select all

# import Fenics main module

import fenics as fe

# import FreeCAD modules

import FreeCADGui
import FreeCAD

# import Fenics interface classes

from feminout.importFenicsMesh import writeFenicsXDMF
from femsolver.fenics.fenics_tools import CellExpressionXDMF, FacetFunctionXDMF

"""
set Python executable to an appropriate value to maintain instant functionality
during the make process
"""

import sys
sys.executable = '/usr/bin/python'

#fe.set_debug_level(fe.DBG)

"""
Select mesh in active document and export it to a temporary file
"""

sel = FreeCADGui.Selection.getSelection()
if len(sel) == 1 and FreeCAD.ActiveDocument is not None:
	sel = sel[0]	
	writeFenicsXDMF.write_fenics_mesh_xdmf(sel, "mesh.xdmf")

"""
Choose charge expressions via Fenics' expressions classes
"""

e1 = fe.Expression("1", degree=1)
e2 = fe.Expression("-1", degree=1)

"""
Use Fenics interface class to import temporary mesh.
Use labels from mesh groups and use expressions for Dirichlet boundary conditions.
"""

ff = FacetFunctionXDMF("mesh.xdmf", 
	{"positive":{"type":"Dirichlet", "value":e1},
	"negative":{"type":"Dirichlet", "value":e2}})

"""
In the following use simple Fenics example for Poisson equation to calculate
an electrostatic type potential on the mesh and with the mentioned boundary conditions.
"""

V = fe.FunctionSpace(ff.mesh, "Lagrange", 1)

u = fe.TrialFunction(V)
v = fe.TestFunction(V)
f = fe.Constant(0.0)
a = fe.inner(fe.grad(u), fe.grad(v))*fe.dx
L = f*v*fe.dx

u = fe.Function(V)
fe.solve(a == L, u, ff.getDirichletBCs(V))

"""
PVD file can be inspected by using Paraview
"""


fe.File("sol_fc_fenics_poisson.pvd") << u
The macro code is performed and the result saved in "sol_fc_fenics_poisson.pvd". It can be inspected by using Paraview giving the following picture:
fc_fenics_demo1_screenshot2.png
fc_fenics_demo1_screenshot2.png (314.25 KiB) Viewed 445 times
As you can see the field distribution is as expected. Notice that we do not need the analysis container like for the other solvers.

Best wishes
Johannes
Last edited by joha2 on Sun May 06, 2018 7:20 pm, edited 1 time in total.