Fenics as Solver
Moderator: bernd
Forum rules
and Helpful information for the FEM forum
and Helpful information for the FEM forum
Re: Fenics as Solver
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
Best regards
Johannes
Re: Fenics as Solver
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
*.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
Re: Fenics as Solver
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
@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
Re: Fenics as Solver
We could add a test for tet4 too. Just as a copy of tet10 and some changes ...
No, https://github.com/FreeCAD/FreeCAD/blob ... py#L37-L78
Re: Fenics as Solver
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
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
Re: Fenics as Solver
Hi Johannes,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
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
Re: Fenics as Solver
Hi HoWil,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.
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.
No, not yet.
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.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
Best regards
Johannes
Re: Fenics as Solver
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.
HoWil
Re: Fenics as Solver
Hey HoWil, yeah I was aware of that project, since one of the project developers is a regular guest at the Fenics development Slack.HoWil wrote: ↑Fri Apr 27, 2018 5:09 pm BTW: Do you know meshio .... https://pypi.org/project/meshio/
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
Re: Fenics as Solver
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.
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
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.