Tutoriel FEM Module d'extension

From FreeCAD Documentation
Revision as of 11:45, 23 March 2019 by David69 (talk | contribs) (Created page with "Pour organiser le code Python, le module FEM utilise une approche similaire à celle utilisée pour le code C ++ dans FreeCAD. Le module est divisé en deux packages. PyObject...")
Tutorial
Topic
Level
Time to complete
Authors
M42kus
FreeCAD version
Example files
See also
None

L'atelier FEM prend en charge déjà beaucoup de contraintes différentes et quelques solveurs. Malgré cela, les gens ont souvent besoin de contraintes non prises en charge par FreeCAD. Cette page est le point de départ d'une série de tutoriels et autres ressources décrivant comment étendre l'atelier FEM à l'aide du cadre existant. Bien que cette série puisse également s’avérer utile pour les développeurs de logiciels, l’idée est de permettre aux utilisateurs de FEM, interéssé par la programmation en Python, d’ajouter ce qu'ils ont besoin.  

Ajouter de nouvelles contraintes, équations ou solveurs est généralement un travail de routine. Le faire pour la première fois n'est pas aussi facile qu'il puisse paraître. Une compréhension des sujets suivants sera utile:

Le moteur de production (cmake)

Le moteur de production doit être modifié quels que soient les objets à ajouter à l’atelier FEM. Chaque module (fichier) Python doit être enregistré. L'atelier FEM demande même à chaque nouveau module Python d'être enregistré deux fois. Une fois dans Mod/Fem/CMakeLists.txt et une seconde dans Mod/Fem/App/CMakeLists.txt. Ceci est vrai sans distinction du type du module Python (graphique ou pas). Plus exactement, le module qui doit être ajouté dépend du rôle du module. Le solveur, les équations et les contraintes utilisent tous des listes différentes. La recherche de fichiers similaires et l'insertion du nouveau fichier dans la même liste fonctionnent la plupart du temps.

A titre d'exemple, ajoutons une nouvelle contrainte de pression. Une nouvelle contrainte nécessite au moins les nouveaux modules suivants: FemConstraint<name>.py, ViewProviderFemConstraint<name>.py, CommandFemConstraint<name>.py. Ces trois fichiers doivent être ajoutés à CMakeLists.txt ainsi qu’à App/MakeLists.txt.

Mod/Fem/CMakeLists.txt

INSTALL(
    FILES
        PyObjects/__init__.py
        PyObjects/_FemConstraintSelfWeight.py
        PyObjects/_FemConstraintBodyHeatFlux.py
        PyObjects/_FemConstraintFlowVelocity.py
+       PyObjects/_FemConstraintFlowPressure.py
        PyObjects/_FemElementFluid1D.py
        PyObjects/_FemElementGeometry1D.py
        PyObjects/_FemElementGeometry2D.py
        ...
    DESTINATION
        Mod/Fem/PyObjects
)

INSTALL(
    FILES
        PyGui/FemCommands.py
        PyGui/__init__.py
        PyGui/_CommandFemSolverElmer.py
        PyGui/_CommandFemEquation.py
        PyGui/_CommandFemConstraintBodyHeatFlux.py
        PyGui/_CommandFemConstraintFlowVelocity.py
+       PyGui/_CommandFemConstraintFlowPressure.py
        PyGui/_CommandFemAnalysis.py
        PyGui/_CommandFemElementFluid1D.py
        PyGui/_CommandFemElementGeometry1D.py
        ...
        PyGui/_ViewProviderFemConstraintSelfWeight.py
        PyGui/_ViewProviderFemConstraintBodyHeatFlux.py
        PyGui/_ViewProviderFemConstraintFlowVelocity.py
+       PyGui/_ViewProviderFemConstraintFlowPressure.py
        PyGui/_ViewProviderFemElementFluid1D.py
        PyGui/_ViewProviderFemElementGeometry1D.py
        PyGui/_ViewProviderFemElementGeometry2D.py
        ...
    DESTINATION
        Mod/Fem/PyGui
)

Mod/Fem/App/CMakeLists.txt

SET(FemObjectsScripts_SRCS
    PyObjects/__init__.py
    PyObjects/_FemConstraintSelfWeight.py
    PyObjects/_FemConstraintBodyHeatFlux.py
    PyObjects/_FemConstraintFlowVelocity.py
+   PyObjects/_FemConstraintFlowPressure.py
    PyObjects/_FemElementFluid1D.py
    PyObjects/_FemElementGeometry1D.py
    PyObjects/_FemElementGeometry2D.py
    PyObjects/_FemMaterialMechanicalNonlinear.py
    PyObjects/_FemMeshBoundaryLayer.py
    PyObjects/_FemMeshGmsh.py
    PyObjects/_FemMeshGroup.py
    PyObjects/_FemMeshResult.py
    PyObjects/_FemMeshRegion.py
    PyObjects/_FemResultMechanical.py
    PyObjects/_FemSolverCalculix.py
    PyObjects/_FemMaterial.py
)

SET(FemGuiScripts_SRCS
    PyGui/FemCommands.py
    PyGui/__init__.py
    PyGui/_CommandFemAnalysis.py
    PyGui/_CommandFemConstraintSelfWeight.py
    PyGui/_CommandFemConstraintBodyHeatFlux.py
    PyGui/_CommandFemConstraintFlowVelocity.py
+   PyGui/_CommandFemConstraintFlowPressure.py
    PyGui/_CommandFemElementFluid1D.py
    PyGui/_CommandFemElementGeometry1D.py
    ...
    PyGui/_ViewProviderFemConstraintBodyHeatFlux.py
    PyGui/_ViewProviderFemConstraintFlowVelocity.py
+   PyGui/_ViewProviderFemConstraintFlowPressure.py
    PyGui/_ViewProviderFemElementFluid1D.py
    PyGui/_ViewProviderFemElementGeometry1D.py
    PyGui/_ViewProviderFemElementGeometry2D.py
    ...
)

Organisation des sources

Pour organiser le code Python, le module FEM utilise une approche similaire à celle utilisée pour le code C ++ dans FreeCAD. Le module est divisé en deux packages. PyObjects, qui contient tous les proxies python de type non graphique pour les objets document et PyGui contenant tout ce qui est lié aux interfaces graphiques comme les proxies Python pour le fournisseur de vues, les panneaux de tâches, les fichiers ui et les commandes.

One package doesn't follow this pattern: FemSolver. It has its place on the same level as PyObjects and PyGui (src/Mod/Fem/FemSolver). The package contains solver and equation related packages and modules and it is organized the following way:

.FemSolver
.FemSolver.Elmer
.FemSolver.Elmer.Equations
.FemSolver.Calculix
.FemSolver.Calculix.Equations
.FemSolver.Z88
.FemSolver.Z88.Equations

Solver

In FreeCAD a solver can be split into two parts. One is the document object used by the user to interact with the solver. Though it solver parameter can be set and it is also used to control the solving process. The other one are the so called tasks of a solver. The solving process is split into those tasks, namely: check, prepare, solve and results. Those do the actual work of exporting the analysis into a format understood by the solver executable, starting the executable and loading the results back into FreeCAD.

Most files related to a solver reside in a sub-package of the FemSolver package (e.g. FemSolver.Elmer). The following list enumerates all files related to the implementation of a solver. Those are the files that need to be copied and modified to add support for a new solver to FreeCAD.

  • FemSolver/Elmer/Object.py: Document object visible in the tree-view. Implemented in python via a document proxy and view proxy.
  • FemSolver/Elmer/Tasks.py: Module containing one task class per task required for a solver implementation. Those tasks divide the process of solving a analysis into the following steps: check, prepare, solve, results.
  • PyGui/_CommandFemElmer.py: Adds the solver document object to the active document. Required to access the solver object from the GUI.

Equations

An equation represets a particular physics that shall be considered when solving the analysis (e.g. Flow, Heat). Not all solver in FreeCAD support equations. Equations are represented by child objects of the corresponding solver object. In the tree-view this looks like this:

  • ElmerSolver
    • Elasticity
    • Heat
    • Flow

Most solver specific options (max iterations, method of solving, etc) are set via the equation objects. One consequence of this is that each solver must have it's own implementation of "the same" equation. Calculix would have a different Heat object that Elmer. To avoid having multiple buttons for the same physics in the GUI each solver object adds it's equations itself.

The actual implementation can be split into the generic and the solver specific part. The generic part can be found in the FemSolver.EquationBase module. The solver specific part resides inside individual Equations sub-packages of the solver packages (e.g. FemSolver/Elmer/Equations).

Adding a new equations to elmer should be very easy. For newcomers there exists a tutorial which shows how to add a new equation to elmer by adding the existing elasticity solver to FreeCAD: Add FEM Equation Tutorial.

Contraintes

Les contraintes définissent les conditions aux limites du problème à résoudre. Dans FreeCAD, les contraintes ne sont pas spécifiques à un solveur particulier. Une configuration de problème peut être résolue par tous les résolveurs prenant en charge toutes les conditions de l'analyse.

L'ajout de nouvelles contraintes est assez simple. Pour les nouveaux arrivants, il existe un tutoriel:Add FEM Constraint Tutorial.