Add FEM Equation Tutorial

In this tutorial we are going to add the Flow equation to FreeCAD and implement support for elmer solver. Please make sure you have read and understood Extend FEM Module before reading this tutorial.

The task can be split into five parts:
 * New equation type. This step must only be done if the equation doesn't exist in FreeCAD yet (as opposed to a equation that is already in FreeCAD but not supported by the target solver).
 * New equation object. Adding a concrete document object representing the elmer specific equation.
 * Extend solver object. Adding support for the new equation to the solver object of elmer.
 * Extend analysis export. Extending the analysis export of elmer to support the new equation type.
 * Gui tool to create a equation. Access the new equation function through workbench Gui.

New Equation Type
In this step we are going to modify the following files:


 * src/Mod/Fem/femsolver/equationbase.py

The equation type is shared among all equation objects of the different solver. Each type has a string specifier (e.g. &quot;Heat&quot;) and a dedicated command that adds the equation to the selected solver. This allows for a simpler GUI where we have only one button for the heat equation which is used for all supported solver.

First add the new equation to the module. Each equation requires two classes. A document proxy and a view proxy. Those two classes will later be used as base classes for the Elmer specific equation classes. Just copy-paste them from an existing equation type and adjust the icon path inside getIcon(self) of the view proxy.

class FlowProxy(BaseProxy): pass

class FlowViewProxy(BaseViewProxy): def getIcon(self): return &quot;:/icons/FEM_EquationFlow.svg&quot;

New Elmer's Equation Object
In this step we are going to implement the document object. We need to add a new flow.py file at:
 * src/Mod/Fem/femsolver/elmer/equations/flow.py

and modify the following file:
 * src/Mod/Fem/ObjectsFem.py
 * src/Mod/Fem/CMakeLists.txt

Let's start with adding the new flow.py file. This file can be copied from an existing equation. If the new equation only supports keywords for linear systems copy the module. If it supports non-linear keywords too copy. The flow equation in Elmer is a potentially non-linear equation. This means that we are going to base our work on.

After copying to, adjust  at this places: - the name argument of the create module function, - the base classes of the class, - the Type attribute of the Proxy class, - the classes.

def create(doc, name=&quot;Flow&quot;): return femutils.createObject(       doc, name, Proxy, ViewProxy)

class Proxy(nonlinear.Proxy, equationbase.FlowProxy):

Type = &quot;Fem::EquationElmerFlow&quot;

def __init__(self, obj): super(Proxy, self).__init__(obj) obj.Priority = 10

class ViewProxy(nonlinear.ViewProxy, equationbase.FlowViewProxy): pass Then you need to change the properties added via the function to those needed by the equation.

At the moment of writing this tutorial Elmer flow equation doesn't have any special properties. See Elmer elasticity equation for an example with properties.

Finally one has to register a makeEquationFlow definition in by duplicating an available entry.

FreeCAD uses make to build the program. So we need to register the new module file in  the way described in Extend FEM Module. The suitable lists can be easily found by searching for existing equation modules files of Elmer.

Extend Solver Object
In this step we are going to modify the following file:
 * src/Mod/Fem/femsolver/elmer/solver.py

Right now we made FreeCAD aware that there is a new type of equation and even added a command that adds this equation to the selected solver object. We also implemented a concrete equation object for Elmer. Whats left to do now is to make the connection between Elmer and the flow equation. This must be done directly in Elmer solver object.

Register the module in which we just implemented our new equation object with the equation specifier from step 1 (&quot;Flow&quot;) in the  list in. from .equations import electrostatic +from .equations import flow

...

_EQUATIONS = { &quot;Heat&quot;: heat, &quot;Elasticity&quot;: elasticity, +   &quot;Flow&quot;: flow, }

Extend Analysis Export
In this step we are going to modify the following file:
 * src/Mod/Fem/femsolver/elmer/writer.py

This is the most demanding part of implementing a new equation. This file contains the class which exports the analysis into Elmer sif format.

For every supported equation there are two main methods handling the export of the respective equation. Just copy all of them from an existing equation and adjust them to your needs.
 * _getFlowSolver
 * _handleFlow

You need to register _handleFlow method inside class Writer:

class Writer(object): ...   def write(self): ...       self._handleFlow

...

_handleFlow can control a series of other detailed methods. Our flow equation uses the following detailed methods::
 * _handleFlowConstants
 * _handleFlowMaterial
 * _handleFlowInitialVelocity
 * _handleFlowBndConditions
 * _handleFlowEquation

We now finished the function part of the new equation. Next we'll connect the new equation through the GUI.

Gui tool to create an equation
In this step we are going to modify the following files: We also need to create an Icon file:
 * src/Mod/Fem/femcommands/commands.py
 * src/Mod/Fem/Gui/Workbench.cpp
 * src/Mod/Fem/Gui/Resources/Fem.qrc
 * src/Mod/Fem/Gui/Resources/icons/FEM_EquationFlow.svg

We need a FEM_EquationFlow.svg file and has to be put into. The new FEM_EquationFlow.svg icon has to be registered for the GUI-button with in  (in ).

In addition to those base classes we have to create a new command class that adds a flow equation to the selected solver object. Next, the command/equation has to be added to the module. Just copy an existing command and adjust the icon, menu text and tool-tip in __init__(self). Don't forget to register the command at the bottom of the module file by using the addCommand(...) method. Please see the discussion in the forum at https://forum.freecadweb.org/viewtopic.php?f=18&t=46693&start=10#p402004 if icons are involved. class _EquationFlow(CommandManager): "The FEM_EquationFlow command definition"

def __init__(self): super(_EquationFlow, self).__init__ self.menuetext = "Flow equation" self.tooltip = "Creates a FEM equation for flow" self.is_active = "with_solver_elmer" self.do_activated = "add_obj_on_gui_selobj_noset_edit" ... FreeCADGui.addCommand(   "FEM_EquationFlow",    _EquationFlow )

The FEM workbench has several toolbars and menus. We will add the new command on to both the solve toolbar and the solve menu.

To add it to the solve toolbar, search for the following code snippet in and add the new command to the rest of the equation commands. Gui::ToolBarItem* solve = new Gui::ToolBarItem(root); solve->setCommand("Solve"); *solve << "FEM_SolverCalculixCxxtools" << "FEM_SolverCalculiX" << "FEM_SolverElmer" << "Separator" << "FEM_EquationElasticity" << "FEM_EquationElectrostatic" +         << "FEM_EquationFlow" << "FEM_EquationFluxsolver" << "FEM_EquationElectricforce" << "FEM_EquationHeat" << "Separator" << "FEM_SolverControl" << "FEM_SolverRun";

To add the flow equation command to the solve menu of the FEM workbench, search for the following code snippet in. Gui::MenuItem* solve = new Gui::MenuItem; root->insertItem(item, solve); solve->setCommand("&Solve"); *solve << "FEM_SolverCalculixCxxtools" << "FEM_SolverCalculiX" << "FEM_SolverElmer" << "FEM_SolverZ88" << "Separator" << "FEM_EquationElasticity" << "FEM_EquationElectrostatic" +         << "FEM_EquationFlow" << "FEM_EquationFluxsolver" << "FEM_EquationElectricforce" << "FEM_EquationHeat" << "Separator" << "FEM_SolverControl" << "FEM_SolverRun";