Add FEM Equation Tutorial: Difference between revisions

From FreeCAD Documentation
(removed old entries for Workbench.cpp which were left in the text for comparison)
mNo edit summary
 
(119 intermediate revisions by 7 users not shown)
Line 1: Line 1:
<languages/>
<languages/>
<translate>
<translate>

<!--T:1-->
<!--T:1-->
{{TutorialInfo
{{TutorialInfo
|Topic=
|Topic=Add FEM Equation
|Level=
|Level=Advanced
|Time=
|Time=1 day
|Author=[[User:M42kus|M42kus]]
|Author=[[User:JohnWang|JohnWang]]
|FCVersion=
|FCVersion=0.19
|Files=
}}
}}

== Introduction == <!--T:28-->


<!--T:2-->
<!--T:2-->
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| Extend FEM Module]] before reading this tutorial.
In this tutorial, we are going to add the '''Flow''' equation to FreeCAD and implement support for the Elmer solver. Please make sure you have read and understood [[Extend_FEM_Module|Extend FEM Module]] before reading this tutorial.


<!--T:3-->
<!--T:3-->
The task can be split into five parts:
The task can be split into four parts. The first step is to make the FEM workbench aware of a new type of equation. This step must only be done if the equation doesn't exist in FreeCAD jet (as opposed to a equation that is already in FreeCAD but not supported by the target solver). The secound step is to add a concrete document object representing the elmer specific equation. The third step is to add support for the new equation to the solver object of elmer. After that the analysis export of elmer must be extended to support the new equation type.
* '''New equation type'''. This step must only be done if the equation doesn't exist in FreeCAD yet (as opposed to an 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 writer object'''. Extending the analysis export of Elmer to support the new equation type.
* '''Gui tool to create an equation'''. Access the new equation function through workbench Gui.


== New Equation Type == <!--T:4-->
== New equation type == <!--T:4-->

<!--T:29-->
In this step we are going to modify the following file:


<!--T:5-->
In this step we are going to modify the following files:
</translate>
</translate>
* src/Mod/Fem/femsolver/equationbase.py
* {{FileName|src/Mod/Fem/femsolver/equationbase.py}}
* src/Mod/Fem/femcommands/commands.py
* src/Mod/Fem/Gui/Workbench.cpp
* src/Mod/Fem/Gui/Resources/Fem.qrc
<translate>
<translate>
<!--T:6-->
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.


<!--T:7-->
<!--T:30-->
The equation type is shared among all equation objects of the different solvers. Each type has a string specifier (e.g. "Heat") 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 {{incode|equationbase.py}} module. Each equation requires two classes. A document proxy and a view proxy. Just copy-paste them from an existing equation type and adjust the icon path inside getIcon(self) of the view proxy.

<!--T:31-->
First, add the new equation to the {{Incode|equationbase.py}} 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 {{Incode|getIcon(self)}} of the view proxy.

</translate>
</translate>
{{Code|code=
<pre>class FlowProxy(BaseProxy):
class FlowProxy(BaseProxy):
pass
pass


class FlowViewProxy(BaseViewProxy):
class FlowViewProxy(BaseViewProxy):
def getIcon(self):
def getIcon(self):
return &quot;:/icons/FEM_EquationFlow.svg&quot;</pre>
return ":/icons/FEM_EquationFlow.svg"
}}
<translate>
<translate>


== New Elmer's equation object == <!--T:12-->
<!--T:8-->
Those two classes will later be used as base classes for the Elmer specific equation classes. In addition to those base classes we have to create a new command class that adds a flow equation to the selected solver object. Additionally, the new .svg image has to be registered for the GUI-button with {{incode|<file>icons/FEM_EquationFlow.svg</file>}} in {{incode|Fem.qrc} (in {{incode|/Gui/Resources/}}). The related .svg has to be put into {{incode|/Gui/Resources/icons/}}.


<!--T:9-->
<!--T:13-->
In this step, we are going to implement the document object. We need to add a new {{Incode|flow.py}} file at:
The command should be added to the {{incode|femcommands/commands.py}} 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 involed.
</translate>
<pre>
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()
)
</pre>

<translate>
<!--T:10-->
Our newly created command still needs to be made accessable via the GUI of the FEM workbench. To add it to the toolbar search for the following code snippet in Workbench.cpp and add the new command to the rest of the equation commands.
</translate>
</translate>
* {{FileName|src/Mod/Fem/femsolver/elmer/equations/flow.py}}
<pre>
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";
</pre>
<translate>
<translate>
<!--T:11-->
We are also going to add the flow equation command to the solve menu of the FEM workbench. To do this insert our equation into the following code snippet in Workbench.cpp.
</translate>
<pre>
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";
</pre>


<!--T:14-->
<translate>
and modify the following files:


== Elmers Equation Object == <!--T:12-->

<!--T:13-->
In this step we are going to modify the following files:
</translate>
* src/Mod/Fem/CMakeLists.txt
* src/Mod/Fem/App/CMakeLists.txt
<translate>
<!--T:14-->
and add the following new file:
</translate>
</translate>
* src/Mod/Fem/FemSolver/Elmer/Equations/Flow.py
* {{FileName|src/Mod/Fem/ObjectsFem.py}}
* {{FileName|src/Mod/Fem/CMakeLists.txt}}
<translate>
<translate>

<!--T:15-->
<!--T:15-->
Let's start with adding the new {{Incode|flow.py}} file. This file can be copied from an existing equation.
Lets start with the module that implements the document object. In can be copied from an existing equation. If the new equation only supports keywords for linear systems copy the FemSolver/Elmer/Equations/Elasticity.py module. If it supports non-linear keywords too copy FemSolver/Elmer/Equations/Heat.py. The flow equation in elmer is a potentially non-linear equation. This means that we are going to base our work on Heat.py.

=== Keywords === <!--T:32-->

<!--T:33-->
* If the new equation only supports keywords for '''linear''' systems copy the {{Incode|femsolver/elmer/equations/elasticity.py}} module.
* If the new equation supports keywords for both '''linear''' and '''non-linear''' systems, copy {{Incode|femsolver/elmer/equations/heat.py}}.

<!--T:34-->
The flow equation in Elmer is a potentially non-linear equation. This means that we are going to base our work on {{Incode|heat.py}}.

=== Editing files === <!--T:35-->


<!--T:16-->
<!--T:16-->
After copying {{Incode|heat.py}} to {{Incode|flow.py}}, adjust {{Incode|flow.py}} in these locations:
After copying Heat.py to Flow.py adjust - the name argument of the create module function, - the Type attribute of the Proxy class, - the base classes of the Proxy and the ViewProxy classes, - and the properties added via the obj.addProperty(..) function to those needed by the equation.
* the name argument of the {{Incode|create}} module function,
* the base classes of the {{Incode|Proxy}} class,
* the {{Incode|Type}} attribute of the {{Incode|Proxy}} class,
* the {{Incode|ViewProxy}} classes.

</translate>
</translate>
{{Code|code=
<pre>def create(doc, name=&quot;Flow&quot;):
def create(doc, name="'''Flow'''"):
return FemMisc.createObject(
return femutils.createObject(
doc, name, Proxy, ViewProxy)
doc, name, Proxy, ViewProxy)


class Proxy(Nonlinear.Proxy, FemEquation.FlowProxy):
class Proxy(nonlinear.Proxy, equationbase.'''Flow'''Proxy):

Type = &quot;Fem::FemEquationElmerFlow&quot;
Type = "Fem::EquationElmer'''Flow'''"

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


class ViewProxy(Nonlinear.ViewProxy, FemEquation.FlowViewProxy):
class ViewProxy(nonlinear.ViewProxy, equationbase.'''Flow'''ViewProxy):
pass</pre>
pass
}}
<translate>
<translate>

<!--T:36-->
Then you need to change the properties added via the {{Incode|obj.addProperty(..)}} function to those needed by the equation.

<!--T:17-->
<!--T:17-->
At the moment of writing this tutorial elmers flow equation doesn't have any special properties. See elmers elasticity equation for an example with properties.
At the moment of writing this tutorial Elmer flow equation doesn't have any special properties. See the Elmer elasticity equation for an example with properties.

<!--T:27-->
Finally one has to register a '''makeEquationFlow''' definition in {{Incode|src/Mod/Fem/ObjectsFem.py}} by duplicating an available entry.


<!--T:18-->
<!--T:18-->
Last but not least register the new module file (Flow.py) in both CMakeLists.txt files the way descripted in [https://www.freecadweb.org/wiki/Extend_FEM_Module Extend FEM Module]. The suitable lists can be easily found by searching for existing equation modules files of elmer.
FreeCAD uses '''make''' to build the program. So we need to register the new module file ({{Incode|flow.py}}) in {{Incode|src/Mod/Fem/CMakeLists.txt}} the way described in [https://www.freecadweb.org/wiki/Extend_FEM_Module Extend FEM Module]. The suitable lists can be easily found by searching for existing equation module files of Elmer.


== Extend Solver Object == <!--T:19-->
== Extend Solver Object == <!--T:19-->
Line 153: Line 123:
<!--T:20-->
<!--T:20-->
In this step we are going to modify the following file:
In this step we are going to modify the following file:

</translate>
</translate>
* src/Mod/Fem/femsolver/elmer/Object.py
* {{FileName|src/Mod/Fem/femsolver/elmer/solver.py}}
<translate>
<translate>

<!--T:21-->
<!--T:21-->
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 it to make the connection between elmer and the flow equation. This must be done directly in elmers solver object.
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. What's left to do now is to make the connection between Elmer and the flow equation. This must be done directly in the Elmer solver object.


<!--T:22-->
<!--T:22-->
Register the module in which we just implemented our new equation object (flow.py) with the equation specifier from step 1 (&quot;Flow&quot;) in the Proxy._EQUATIONS list in elmer/solver.py.
Register the module in which we just implemented our new equation object ({{Incode|flow.py}}) with the equation specifier from step 1 ("Flow") in the {{Incode|_EQUATIONS}} list in {{Incode|elmer/solver.py}}.

</translate>
</translate>
{{Code|code=
<pre>_EQUATIONS = {
from .equations import electrostatic
&quot;Heat&quot;: Equations.Heat,
+from .equations import flow
&quot;Elasticity&quot;: Equations.Elasticity,

+ &quot;Flow&quot;: Equations.Flow,
...
}</pre>

_EQUATIONS = {
"Heat": heat,
"Elasticity": elasticity,
+ "Flow": flow,
}
}}
<translate>
<translate>


== Extend Analysis Export == <!--T:23-->
== Extend writer object == <!--T:23-->


<!--T:24-->
<!--T:24-->
In this step we are going to modify the following file:
In this step we are going to modify the following file:

</translate>
</translate>
* src/Mod/Fem/femsolver/elmer/writer.py
* {{FileName|src/Mod/Fem/femsolver/elmer/writer.py}}
<translate>
<translate>

<!--T:25-->
<!--T:25-->
This is the most demanding part of implementing a new equation. This file contains a Writer class which exports the analysis into elmers sif format.
This file contains the {{Incode|Writer}} class which exports the analysis into Elmer SIF format.


<!--T:26-->
<!--T:26-->
For every supported equation there are a series of methods handling the export of the respective equation. Just copy all of them from an existing equation and adjust them to your needs. Our Flow equation uses the following methods:
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.

</translate>
</translate>
* {{Incode|_getFlowSolver}}
* _handleFlow
* {{Incode|_handleFlow}}
* _getFlowSolver
<translate>
* _handleFlowConstants
* _handleFlowBndConditions
* _handleFlowInitial
* _handleFlowBodyForces
* _handleFlowMaterial
* _handleFlowEquation


<!--T:37-->
{{clear}}
You need to register the {{Incode|_handleFlow}} method inside the {{Incode|Writer}} class:

</translate>
{{Code|code=
class Writer(object):
...
def write(self):
...
self._handleFlow()

...

}}
<translate>

<!--T:38-->
{{Incode|_handleFlow}} can control a series of other detailed methods. Our flow equation uses the following detailed methods:

</translate>
* {{Incode|_handleFlowConstants}}
* {{Incode|_handleFlowMaterial}}
* {{Incode|_handleFlowInitialVelocity}}
* {{Incode|_handleFlowBndConditions}}
* {{Incode|_handleFlowEquation}}
<translate>

<!--T:39-->
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 == <!--T:40-->

<!--T:41-->
We have just created a new equation class. To access it from the FEM GUI, we need to create a button and link it to the new equation class. Here is a tutorial: [[Add_Button_to_FEM_Toolbar_Tutorial|Add Button to FEM Toolbar Tutorial]].

</translate>
[[Category:FEM{{#translation:}}]]
[[Category:FEM{{#translation:}}]]
[[Category:Developer Documentation{{#translation:}}]]

Latest revision as of 18:01, 12 January 2024

Tutorial
Topic
Add FEM Equation
Level
Advanced
Time to complete
1 day
Authors
JohnWang
FreeCAD version
0.19
Example files
None
See also
None

Introduction

In this tutorial, we are going to add the Flow equation to FreeCAD and implement support for the 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 an 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 writer object. Extending the analysis export of Elmer to support the new equation type.
  • Gui tool to create an equation. Access the new equation function through workbench Gui.

New equation type

In this step we are going to modify the following file:

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

The equation type is shared among all equation objects of the different solvers. Each type has a string specifier (e.g. "Heat") 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 equationbase.py 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 ":/icons/FEM_EquationFlow.svg"

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 files:

  • 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.

Keywords

  • If the new equation only supports keywords for linear systems copy the femsolver/elmer/equations/elasticity.py module.
  • If the new equation supports keywords for both linear and non-linear systems, copy femsolver/elmer/equations/heat.py.

The flow equation in Elmer is a potentially non-linear equation. This means that we are going to base our work on heat.py.

Editing files

After copying heat.py to flow.py, adjust flow.py in these locations:

  • the name argument of the create module function,
  • the base classes of the Proxy class,
  • the Type attribute of the Proxy class,
  • the ViewProxy classes.
def create(doc, name="'''Flow'''"):
    return femutils.createObject(
        doc, name, Proxy, ViewProxy)

class Proxy(nonlinear.Proxy, equationbase.'''Flow'''Proxy):

    Type = "Fem::EquationElmer'''Flow'''"

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

class ViewProxy(nonlinear.ViewProxy, equationbase.'''Flow'''ViewProxy):
    pass

Then you need to change the properties added via the obj.addProperty(..) function to those needed by the equation.

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

Finally one has to register a makeEquationFlow definition in src/Mod/Fem/ObjectsFem.py by duplicating an available entry.

FreeCAD uses make to build the program. So we need to register the new module file (flow.py) in src/Mod/Fem/CMakeLists.txt the way described in Extend FEM Module. The suitable lists can be easily found by searching for existing equation module 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. What's left to do now is to make the connection between Elmer and the flow equation. This must be done directly in the Elmer solver object.

Register the module in which we just implemented our new equation object (flow.py) with the equation specifier from step 1 ("Flow") in the _EQUATIONS list in elmer/solver.py.

from .equations import electrostatic
+from .equations import flow

...

_EQUATIONS = {
    "Heat": heat,
    "Elasticity": elasticity,
+    "Flow": flow,
}

Extend writer object

In this step we are going to modify the following file:

  • src/Mod/Fem/femsolver/elmer/writer.py

This file contains the Writer 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 the _handleFlow method inside the Writer class:

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

We have just created a new equation class. To access it from the FEM GUI, we need to create a button and link it to the new equation class. Here is a tutorial: Add Button to FEM Toolbar Tutorial.