Add FEM Constraint Tutorial

Introduction
In this tutorial we are going to add the flow velocity constraint to FreeCAD and implement support for the Elmer solver. Please make sure you have read and understood Extend FEM Module before reading this tutorial.

This tutorial only covers how to implement constraints in python. In contrast to solver and equations constraints follow the classic FEM module structure. That is, all modules of a constraint have their place in either the or  package.

Summary

 * 1) Create document object: The document object that resides inside the analysis and though which the constraint can be parametrized and attached to boundaries.
 * 2) Create GUI command: Add a command to the FEM workbench that adds a flow constraint to the active analysis.
 * 3) Create a task panel: The task panel is necessary to allow the user to set the boundaries at which he wants to set the velocity constraint. It also makes entering the parameters a little more user friendly.
 * 4) Extend elmers writer: Add support for the new constraint to Elmer by extending its sif file exporter.

Create document object
In this step we are going to modify the following files:

And add the following files:

A document proxy and a view proxy are required for the new constraint. Those reside in separate modules. The document proxy in femobjects and the view proxy in femviewprovider. Just copy the modules from an existing constraint e.g.:

Adjust the Type variable and the properties to your needs. The document proxy of the flow constraint looks like the following:

The module containing the view proxy might look a little more complicated. But for now just adjust the icon path. We are going to come back to this file in later steps of the tutorial.

Add the two new modules to the build system like described in Extend FEM Module. Locate the correct list by searching for constraint modules.

As all objects of the FEM workbench the velocity constraint must be registered in. The following method adds a velocity constraint to the active document. This method will be used by the GUI command to add the constraint. It must be inserted somewhere in.

Create GUI command
In this step we are going to modify the following files:

And add the following new file:

The command allows the user to actually add the constraint to the active analysis. Just copy a command from an existing constraint. Commands reside in the package. Adjust the resources attribute and the make method called in Activated to your needs. Also use a different command id in the addCommand call on the bottom of the module. The following class is the command class of the velocity constraint.

Add the new command file to the build system as decripted in Extend FEM Module. Locate the correct list be searching for existing command modules.

Put the command into Gui/Workbench.cpp to add it to the toolbar and menu. Search for an existing constraint of the same category as the new one (e.g. Flow) copy-paste it and adjust the command id. This should be done two times. Once for the menu and again for the toolbar.

Create a task panel
In this step we are going to modify the following file:

In FreeCAD constraint objects benefit greatly from task panels. Task panels can make use of more powerful input widgets which expose the unit of entered values directely to the user. The velocity constraint even requires the use of a task panel since a task panel is the only way of specifieing the face(s) on which the constraint shall be applied.

The location of the module in which task panels are implemented is not strictely defined. For the velocity constraint we are just going to put the task panel in the same module we put the view proxy. The task panel is quite complicated. It makes use of the FemSolectionWidgets.BoundarySelector. Thats a qt widget which allows the user to select the boundaries on which the constraint shall be applied. In addition to this widget it generates another one by loading a ui file specifically created for the velocity constraint. Via this widget the velocity vector can be specified.

Most of the time is should be sufficient to just copy this class, use a suitable ui file (instead of TaskPanelFemFlowVelocity.ui) and adjust _initParamWidget as well as _applyWidgetChanges. If the new constraint requires bodies as references instead of boundaries just replace the BoundarySelector object with the SolidSelector.

The view proxy must be extended to support the task panel we just implemented. The following extended view proxy opens the task panel when the user makes a double click on the constraint object in the tree view.

Extend Elmer's writer
In this step we are going to modify the following file:

The writer module contains methods for all equation types. Depending on the type of the constraint, boundary condition, initial condition or body force one has to modify different methods. For our flow velocity we have to adjust.