Add FEM Equation Tutorial: Difference between revisions
Tag: Undo |
(Fixing wiki code...) |
||
Line 1: | Line 1: | ||
<languages/> |
<languages/> |
||
<translate> |
<translate> |
||
<!--T:1--> |
<!--T:1--> |
||
{{TutorialInfo |
{{TutorialInfo |
||
Line 8: | Line 9: | ||
|Author=[[User:JohnWang|JohnWang]] |
|Author=[[User:JohnWang|JohnWang]] |
||
|FCVersion=0.19 |
|FCVersion=0.19 |
||
|Files= |
|||
}} |
}} |
||
== Introduction == |
|||
<!--T:2--> |
<!--T:2--> |
||
Line 22: | Line 24: | ||
* '''Gui tool to create a equation'''. Access the new equation function through workbench Gui. |
* '''Gui tool to create a equation'''. Access the new equation function through workbench Gui. |
||
== New |
== New equation type == <!--T:4--> |
||
⚫ | |||
⚫ | |||
⚫ | |||
<translate> |
|||
⚫ | The equation type is shared among all equation objects of the different solver. 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. 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. |
||
⚫ | |||
{{Code|code= |
|||
⚫ | The equation type is shared among all equation objects of the different solver. Each type has a string specifier (e.g. |
||
⚫ | |||
⚫ | First add the new equation to the {{ |
||
⚫ | |||
pass |
pass |
||
class FlowViewProxy(BaseViewProxy): |
class FlowViewProxy(BaseViewProxy): |
||
def getIcon(self): |
def getIcon(self): |
||
return |
return ":/icons/FEM_EquationFlow.svg" |
||
}} |
|||
</pre> |
|||
== New Elmer's |
== New Elmer's equation object == <!--T:12--> |
||
<!--T:13--> |
<!--T:13--> |
||
In this step we are going to implement the document object. We need to add a new flow.py file at: |
In this step we are going to implement the document object. We need to add a new {{FileName|flow.py}} file at: |
||
</translate> |
</translate> |
||
* src/Mod/Fem/femsolver/elmer/equations/flow.py |
* {{FileName|src/Mod/Fem/femsolver/elmer/equations/flow.py}} |
||
<translate> |
<translate> |
||
<!--T:14--> |
<!--T:14--> |
||
and modify the following file: |
and modify the following file: |
||
</translate> |
</translate> |
||
* src/Mod/Fem/ObjectsFem.py |
* {{FileName|src/Mod/Fem/ObjectsFem.py}} |
||
* src/Mod/Fem/CMakeLists.txt |
* {{FileName|src/Mod/Fem/CMakeLists.txt}} |
||
<translate> |
<translate> |
||
<!--T:15--> |
<!--T:15--> |
||
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 {{ |
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 {{Incode|femsolver/elmer/equations/elasticity.py}} module. If it supports '''non-linear''' keywords too copy {{Incode|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 {{Incode|heat.py}}. |
||
<!--T:16--> |
<!--T:16--> |
||
After copying {{ |
After copying {{Incode|heat.py}} to {{Incode|flow.py}}, adjust {{Incode|flow.py}} at this places: |
||
- the name argument of the create module function, |
- the name argument of the create module function, |
||
- the base classes of the {{ |
- the base classes of the {{Incode|Proxy}} class, |
||
- the Type attribute of the Proxy class, |
- the Type attribute of the Proxy class, |
||
- the {{ |
- the {{Incode|ViewProxy}} classes. |
||
⚫ | |||
</translate> |
|||
⚫ | |||
{{Code|code= |
|||
⚫ | |||
return femutils.createObject( |
return femutils.createObject( |
||
doc, name, Proxy, ViewProxy) |
doc, name, Proxy, ViewProxy) |
||
Line 72: | Line 80: | ||
class Proxy(nonlinear.Proxy, equationbase.'''Flow'''Proxy): |
class Proxy(nonlinear.Proxy, equationbase.'''Flow'''Proxy): |
||
Type = |
Type = "Fem::EquationElmer'''Flow'''" |
||
def __init__(self, obj): |
def __init__(self, obj): |
||
Line 80: | Line 88: | ||
class ViewProxy(nonlinear.ViewProxy, equationbase.'''Flow'''ViewProxy): |
class ViewProxy(nonlinear.ViewProxy, equationbase.'''Flow'''ViewProxy): |
||
pass |
pass |
||
}} |
|||
<translate> |
<translate> |
||
Then you need to change the properties added via the {{ |
Then you need to change the properties added via the {{Incode|obj.addProperty(..)}} function to those needed by the equation. |
||
<!--T:17--> |
<!--T:17--> |
||
Line 87: | Line 97: | ||
<!--T:27--> |
<!--T:27--> |
||
Finally one has to register a '''makeEquationFlow''' definition in {{ |
Finally one has to register a '''makeEquationFlow''' definition in {{Incode|src/Mod/Fem/ObjectsFem.py}} by duplicating an available entry. |
||
<!--T:18--> |
<!--T:18--> |
||
FreeCAD uses '''make''' to build the program. So we need to register the new module file ({{ |
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 modules files of Elmer. |
||
== Extend Solver Object == <!--T:19--> |
== Extend Solver Object == <!--T:19--> |
||
Line 96: | Line 106: | ||
<!--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/solver.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 is to make the connection between Elmer and the flow equation. This must be done directly in Elmer 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. 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. |
||
<!--T:22--> |
<!--T:22--> |
||
Register the module in which we just implemented our new equation object ({{ |
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> |
|||
from .equations import electrostatic |
from .equations import electrostatic |
||
+from .equations import flow |
+from .equations import flow |
||
Line 112: | Line 125: | ||
_EQUATIONS = { |
_EQUATIONS = { |
||
"Heat": heat, |
|||
"Elasticity": elasticity, |
|||
+ |
+ "Flow": flow, |
||
} |
} |
||
}} |
|||
</pre> |
|||
<translate> |
<translate> |
||
== Extend |
== 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 |
* src/Mod/Fem/femsolver/elmer/writer.py |
||
<translate> |
<translate> |
||
<!--T:25--> |
<!--T:25--> |
||
This file contains the {{ |
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 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. |
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> |
||
* _getFlowSolver |
* _getFlowSolver |
||
* _handleFlow |
* _handleFlow |
||
<translate> |
<translate> |
||
You need to register _handleFlow method inside class Writer: |
You need to register _handleFlow method inside class Writer: |
||
</translate> |
</translate> |
||
{{Code|code= |
|||
<pre> |
|||
class Writer(object): |
class Writer(object): |
||
... |
... |
||
Line 149: | Line 164: | ||
... |
... |
||
}} |
|||
</pre> |
|||
<translate> |
<translate> |
||
_handleFlow can control a series of other detailed methods. Our flow equation uses the following detailed methods |
_handleFlow can control a series of other detailed methods. Our flow equation uses the following detailed methods: |
||
</translate> |
</translate> |
||
* _handleFlowConstants |
* _handleFlowConstants |
||
Line 158: | Line 175: | ||
* _handleFlowBndConditions |
* _handleFlowBndConditions |
||
* _handleFlowEquation |
* _handleFlowEquation |
||
<translate> |
|||
We now finished the function part of the new equation. Next we'll connect the new equation through the GUI. |
We now finished the function part of the new equation. Next we'll connect the new equation through the GUI. |
||
{{clear}} |
|||
⚫ | |||
== Gui tool to create an equation == |
== Gui tool to create an equation == |
||
⚫ | |||
<!--T:4--> |
|||
⚫ | |||
</translate> |
|||
⚫ |
Revision as of 18:12, 20 August 2021
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 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 writer object. 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 file:
- 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. "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 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 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
.
After copying heat.py
to flow.py
, adjust flow.py
at this places:
- the name argument of the create module function, - the base classes of theProxy
class, - the Type attribute of the Proxy class, - theViewProxy
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 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 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 (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 _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
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.