Feature: add extra FemMesh mesh file export format, potentially usable by other solvers

About the development of the FEM module/workbench.

Moderator: bernd

qingfeng.xia
Posts: 197
Joined: Tue Sep 22, 2015 1:47 pm
Location: Oxford Uk
Contact:

Feature: add extra FemMesh mesh file export format, potentially usable by other solvers

Postby qingfeng.xia » Wed Aug 02, 2017 8:04 pm

## Design:

Add one property, OutputFormat, based on Bernd's FemMeshGmsh and a new export_mesh() function in FemGmshTools.py
Potentially, boundary node groups can be export/convert for solvers like elmer, fenics, etc.

By fully controling `Mesh.Format` in gmsh input *.geo file, a few more mesh formats can be exported from FreeCAD:

```
Mesh output format (1=msh, 2=unv, 10=automatic, 19=vrml, 26=pos statistics,
27=stl, 30=mesh, 31=bdf, 32=cgns, 33=med, 39=inp, 40=ply2)

Mesh.Format Default value: 10
```
new mesh format export is controlled by file suffix, in `importGmshMesh.py`

Code: Select all

 output_format_suffix = {'Gmsh MSH':'.msh', 'I-Deas universal':'.unv', 'Automatic':'.msh',
  'STL surface':'.stl',   'INRIA medit':'.mesh', 'CGNS':'.cgns', 
  'Salome mesh':'med', 'Abaqus INP':'.inp', 'Ploy surface':'.ply2'}
A few formats have been supported by Fem module,
Gmsh_mesh_export_formats.png
Gmsh_mesh_export_formats.png (64.21 KiB) Viewed 1745 times
## testing

For Fenics mesh support, only first order element triangle2D or tetra 3D element is supported, 2D and 3D mesh exporting is possible. However, boundary mesh is not supported yet, it is bit tricky but achievable. see http://www.iesensor.com/blog/2017/05/24 ... s_meshing/. This could be another route as Joha2's fenincs mesh import and export feature.

*. CGNS: gmsh is not compiled with CGNS support, hence not tested

*,msh, the GMSH native format, test passed for 2D and 3D mesh

*.ply2, tested passed for 2D mesh

the feature branch:
https://github.com/qingfengxia/FreeCAD/tree/fenicsmesh

testing FreeCAD file for 2D, 3D is too large to upload here.


## todo

I will try to export fenics itnerior mesh and boundary mesh, then send a PR soon.

Previously, I write a script to export only 3D mesh for OpenFOAM (CfdWorkbench), now it is possible to export 2D mesh for OpenFOAM
using gmshToFoam, see tutorial https://openfoamwiki.net/index.php/2D_M ... using_GMSH

If you hope some specific mesh format to be supprted and tested, please let me know.

Thanks
Last edited by qingfeng.xia on Thu Aug 17, 2017 8:17 am, edited 1 time in total.
Ubuntu 18.04 LTS 64bit, python3, always work with latest FreeCAD daily build
Currently I mainly work on OpenFOAM + Fenics for CFD, FSI
https://github.com/qingfengxia/FreeCAD_Mod_Dev_Guide
https://github.com/qingfengxia/Cfd
qingfeng.xia
Posts: 197
Joined: Tue Sep 22, 2015 1:47 pm
Location: Oxford Uk
Contact:

Re: Feature: add extra FemMesh mesh file export format, potentially usable by other solvers

Postby qingfeng.xia » Tue Aug 15, 2017 11:01 pm

This feature is completed and tested on my ubuntu .

Fenics 2D and 3D mesh with boundary mesh can be exported now, enable the GUI setup for my FenicsSolver. which can solve fluidic and mechanical problems within FreeCAD.

https://github.com/qingfengxia/FenicsSolver

This mesh export function can also suport Elmer solver, which accept gmsh *.msh mesh with boundary info.


For "dolfin-convert" command for mesh conversion from gmsh to fenics xml file , I must run it with Popen() shell=True, I do not know why,
Ubuntu 18.04 LTS 64bit, python3, always work with latest FreeCAD daily build
Currently I mainly work on OpenFOAM + Fenics for CFD, FSI
https://github.com/qingfengxia/FreeCAD_Mod_Dev_Guide
https://github.com/qingfengxia/Cfd
qingfeng.xia
Posts: 197
Joined: Tue Sep 22, 2015 1:47 pm
Location: Oxford Uk
Contact:

Re: Feature: add extra FemMesh mesh file export format, potentially usable by other solvers

Postby qingfeng.xia » Thu Aug 17, 2017 8:15 am

PR sent in:

https://github.com/FreeCAD/FreeCAD/pull/931

I will explain my design in the following posts
Last edited by qingfeng.xia on Thu Aug 17, 2017 8:42 am, edited 1 time in total.
Ubuntu 18.04 LTS 64bit, python3, always work with latest FreeCAD daily build
Currently I mainly work on OpenFOAM + Fenics for CFD, FSI
https://github.com/qingfengxia/FreeCAD_Mod_Dev_Guide
https://github.com/qingfengxia/Cfd
qingfeng.xia
Posts: 197
Joined: Tue Sep 22, 2015 1:47 pm
Location: Oxford Uk
Contact:

Re: Feature: add extra FemMesh mesh file export format, potentially usable by other solvers

Postby qingfeng.xia » Thu Aug 17, 2017 8:20 am

In order to support 2D mesh with boundary value and mesh exported for Fencis, Fem::Constraint objects are collected from Analysis and a new function
get_constraint_data(), then write together with write_group().

get_constraint_data(): As far as I know, most of constraints are defined in cpp, as a derived from Fem::Constraint

Code: Select all

            for m in self.analysis.Member:
                if m.isDerivedFrom("Fem::Constraint"):
                    if len(m.References):
			self.constraint_objects[m.Name] = list(m.References[0][1])

Before this PR, boundary mesh of fem::constraint objects of 3D case are exported, but not for 2D. Therefore, commit 1de232a is needed.

FEM: add Fem::Contraint group in geo file writing to support boundary…
Last edited by qingfeng.xia on Thu Aug 17, 2017 8:39 am, edited 1 time in total.
Ubuntu 18.04 LTS 64bit, python3, always work with latest FreeCAD daily build
Currently I mainly work on OpenFOAM + Fenics for CFD, FSI
https://github.com/qingfengxia/FreeCAD_Mod_Dev_Guide
https://github.com/qingfengxia/Cfd
qingfeng.xia
Posts: 197
Joined: Tue Sep 22, 2015 1:47 pm
Location: Oxford Uk
Contact:

Re: Feature: add extra FemMesh mesh file export format, potentially usable by other solvers

Postby qingfeng.xia » Thu Aug 17, 2017 8:38 am

A general "Mesh group" can be "defined by FemMeshGroup", "Fem::Constraint" derived classes, also possible FemMaterail, as pointed out by Bernd.

Bernd pointed out "FemMeshGroup" is used for boundary mesh export when no Constraint defined. It is equal to "name selection" in Ansys Workbench meshing module, it can be referred by Boundary/Constraint object (you have choice of select geometrical element or select named selection).

For the time being, I imagine if all necessary FemConstraint objects are defined, there is no needed to define FemMeshGroup.

For "mesh group" defined by material object "References", this is may be useful by multile-part case, porous etc. We will implement until it is needed.



Exampled by 2D case, once there is at least one Physical Line exported, there must be a Physical Surface defined, otherwise only boundary mesh is exported, not the volume mesh. Therefore, these code are necessary:

Code: Select all

    def write_group(self, geo):
        boundaries = 0
        domains = 0
        ...
        #counting the boundary and subdomains(on order higher than boundary)
        
        # if physical volume for 3D or physical surface for 2D mesh is NOT defined, define a default interior domain for Fenics mesh
        if domains == 0:
            if self.dimension == '3':
                geo.write('Physical Volume("Interior") = {1};\n')
            if self.dimension == '2':
                geo.write('Physical Surface("Interior") = {1};\n')
        else:
            print("Warning: no subdomain group data are written, thus subdomain nesh will not be exported")
        if boundaries == 0:
            print("Warning: no boundary group data are written, thus boundary mesh will not be exported")
	geo.write("\n\n")

Ubuntu 18.04 LTS 64bit, python3, always work with latest FreeCAD daily build
Currently I mainly work on OpenFOAM + Fenics for CFD, FSI
https://github.com/qingfengxia/FreeCAD_Mod_Dev_Guide
https://github.com/qingfengxia/Cfd
User avatar
bernd
Posts: 9247
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: Feature: add extra FemMesh mesh file export format, potentially usable by other solvers

Postby bernd » Fri Aug 18, 2017 5:39 am

continues from: https://github.com/FreeCAD/FreeCAD/pull ... -container

In general, as I wrote before it does make sense to make your code public on the forum before. We could discuss such problems on the forum much better here because dozens of people get flooded with emails from PR comments just because of minor FEM discussions.

Two minor things:
you should provide a valid email AFAIK: https://github.com/qingfengxia/FreeCAD/ ... Mesh.py#L3

misses a dot before med https://github.com/qingfengxia/FreeCAD/ ... ols.py#L43

and a bigger one for FemGmshTools:
I generally like to separate retrieving the data and writing the data because if they are in one def they could not be used somewhere else. In Elmer GSoC we gone use the get_group_data from FemGmshTools, but you removed this def.

Why do you prefer to write the data directly. In boundary you did the same?

Bernd
wmayer
Site Admin
Posts: 15471
Joined: Thu Feb 19, 2009 10:32 am

Re: Feature: add extra FemMesh mesh file export format, potentially usable by other solvers

Postby wmayer » Fri Aug 18, 2017 8:21 am

you should provide a valid email AFAIK: https://github.com/qingfengxia/FreeCAD/ ... Mesh.py#L3
It's also OK to replace @ with [at] and if desired the . with [dot] to avoid that spam bots get an email address so easily.
qingfeng.xia
Posts: 197
Joined: Tue Sep 22, 2015 1:47 pm
Location: Oxford Uk
Contact:

Re: Feature: add extra FemMesh mesh file export format, potentially usable by other solvers

Postby qingfeng.xia » Fri Aug 18, 2017 9:42 am

bernd wrote:
Fri Aug 18, 2017 5:39 am
continues from: https://github.com/FreeCAD/FreeCAD/pull ... -container

In general, as I wrote before it does make sense to make your code public on the forum before. We could discuss such problems on the forum much better here because dozens of people get flooded with emails from PR comments just because of minor FEM discussions.

Two minor things:
you should provide a valid email AFAIK: https://github.com/qingfengxia/FreeCAD/ ... Mesh.py#L3

misses a dot before med https://github.com/qingfengxia/FreeCAD/ ... ols.py#L43

and a bigger one for FemGmshTools:
I generally like to separate retrieving the data and writing the data because if they are in one def they could not be used somewhere else. In Elmer GSoC we gone use the get_group_data from FemGmshTools, but you removed this def.

Why do you prefer to write the data directly. In boundary you did the same?

Bernd
Thanks for pointing out 2 minor , for the major: see the post below


The definition of get_analysis_group_elements, It call any object with References properties is a "group".
For 3D case, 2D boundary are captured, but for 2D case, 1D boundary is NOT captured, I am not sure why?

However, there are some types is not captured by my implementation:

https://github.com/FreeCAD/FreeCAD/blob ... s.py#L1072

What I think:
1) it is better/safer to distinguish different types, constraint, material, FemGeometry*D.
2) this function should be put into FemTools.py, it has no relation with mesh
3) <Mod>Tools.py should be a file with static utility functions, without need to instantiate a class.

Or a quick but not sure if it is a decent solution:
I keep your code get_group_data(), add my get_constraint_data, I detect if dim =2, then I add an extra line.

Code: Select all

def get_analysis_group_elements(aAnalysis, aPart):
    ''' all Reference shapes of all Analysis member are searched in the Shape of aPart. If found in shape they are added to a dict
    {ConstraintName : ['ShapeType of the Elements'], [ElementID, ElementID, ...], ...}
    '''
    group_elements = {}  # { name : [element, element, ... , element]}
    empty_references = []
    for m in aAnalysis.Member:
        if hasattr(m, "References"):
            if m.References:
                grp_ele = get_reference_group_elements(m, aPart)
                group_elements[grp_ele[0]] = grp_ele[1]
            else:
                print('  Empty reference: ' + m.Name)
                empty_references.append(m)
    if empty_references:
        if len(empty_references) == 1:
            group_elements = get_anlysis_empty_references_group_elements(group_elements, aAnalysis, aPart.Shape)
        else:
            FreeCAD.Console.PrintError('Problem: more than one object with empty references.\n')
            print('We gone try to get the empty material references anyway.\n')
            # FemElementGeometry2D, ElementGeometry1D and FemElementFluid1D could have empty references, but on solid meshes only materials should have empty references
            for er in empty_references:
                print(er.Name)
            group_elements = get_anlysis_empty_references_group_elements(group_elements, aAnalysis, aPart.Shape)
    # check if all groups have at least one element, it does not mean ALL reference shapes for a group have been found
    for g in group_elements:
        # print(group_elements[g])
        if len(group_elements[g]) == 0:
            FreeCAD.Console.PrintError('Error: The shapes for the mesh group for the reference shapes of analysis member: ' + g + ' could not be found!\n')
return group_elements
Why do you prefer to write the data directly. In boundary you did the same?
I am not sure I get your meaning of "write the data directly". For write_geo file, the logic is identical.


"FEM: gmsh mesh tool, fix geo tolerance value", there may be a regression, the mesh is generated but there is a red warning by gmsh.
I check the tolerance line, there is a unit of mm, there.

Error : '/tmp/shape2mesh.geo', line 40 : syntax error (mm)
Last edited by qingfeng.xia on Sun Aug 20, 2017 7:45 am, edited 1 time in total.
Ubuntu 18.04 LTS 64bit, python3, always work with latest FreeCAD daily build
Currently I mainly work on OpenFOAM + Fenics for CFD, FSI
https://github.com/qingfengxia/FreeCAD_Mod_Dev_Guide
https://github.com/qingfengxia/Cfd
User avatar
bernd
Posts: 9247
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: Feature: add extra FemMesh mesh file export format, potentially usable by other solvers

Postby bernd » Sat Aug 19, 2017 2:21 pm

Hi qingfeng.xia

we are in the latest weeks of GSoC elmer project. I'm quite busy with this. Eventually we will get a new solver frame work for FEM which will be merged soon, hopefully. We will find some smart result which will fit for you and all other but it will take some days. I will come back as soon as the GSoC elmer new solver is tackled down.

bernd
qingfeng.xia
Posts: 197
Joined: Tue Sep 22, 2015 1:47 pm
Location: Oxford Uk
Contact:

Re: Feature: add extra FemMesh mesh file export format, potentially usable by other solvers

Postby qingfeng.xia » Sun Aug 20, 2017 7:40 am

@Bernd, Thanks for helping me review the code. I am looking forward to your Elmer solver, I will prepare my FenicsSolver accordingly later this year.

I have revised the code to be compatible with your previous one. I just split group into 3 instead of 2. Test passed for 2D and 3D.

In short, I got meshgroup, then get constraint. finally get all other excepts for the previous 2 types

reserving the key sequence is required by fenics mesh export. as boundary name can not been written, only a integer ID is written, also it should be written at first

Code: Select all


    def get_group_data(self):
        self.mesh_group_elements = OrderedDict()
        self.other_group_elements = OrderedDict()
        # TODO solid, face, edge seam not work together, some print or make it work together

        # mesh groups
        if not self.mesh_obj.MeshGroupList:
            print('  No mesh group objects.')
        else:
            print('  Mesh group objects, we need to get the elements.')
            for mg in self.mesh_obj.MeshGroupList:
                new_group_elements = FemMeshTools.get_mesh_group_elements(mg, self.part_obj)
                for ge in new_group_elements:
                    if ge not in self.mesh_group_elements:
                        self.mesh_group_elements[ge] = new_group_elements[ge]
                    else:
                        FreeCAD.Console.PrintError("  A group with this name exists already.\n")
        print('  {}'.format(self.mesh_group_elements))

        self.get_constraint_data()

        # collect other members with References property
        if self.analysis:
            print('  collect all other group except for GmshMeshGroup and Fem::Constraint types')
            new_group_elements = FemMeshTools.get_analysis_group_elements(self.analysis, self.part_obj)
            for ge in new_group_elements:
                if ge not in self.mesh_group_elements and ge not in self.constraint_objects:
                    self.other_group_elements[ge] = new_group_elements[ge]
                else:
                    FreeCAD.Console.PrintError("  A group with this name exists already.\n")
        else:
            print('  No anlysis members for group meshing.')
during wrting group:

Code: Select all


        import copy
        # OrderedDict is used to preserve the element sequence, Fem Constraint must be write first
        all_group_elements = copy.copy(self.constraint_objects)
        for k in self.mesh_group_elements:
            all_group_elements[k] = self.mesh_group_elements[k]
        for k in self.other_group_elements:
            all_group_elements[k] = self.other_group_elements[k]
        print(all_group_elements)
Ubuntu 18.04 LTS 64bit, python3, always work with latest FreeCAD daily build
Currently I mainly work on OpenFOAM + Fenics for CFD, FSI
https://github.com/qingfengxia/FreeCAD_Mod_Dev_Guide
https://github.com/qingfengxia/Cfd