Add FEM Constraint Tutorial/ro: Difference between revisions

From FreeCAD Documentation
No edit summary
No edit summary
Line 113: Line 113:
== Creara Task Panel ==
== Creara Task Panel ==


În acest pas vom modifica următorul fișier:
In this step we are going to modify the following file:
* src/Mod/Fem/PyGui/_ViewProviderFemConstraintFlowVelocity.py
* src/Mod/Fem/PyGui/_ViewProviderFemConstraintFlowVelocity.py
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.
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.

Revision as of 07:45, 6 September 2018

Tutorial
Topic
Level
Time to complete
Authors
M42kus
FreeCAD version
Example files
See also
None

În acest tutorial vom adăuga constrângerea vitezei fluxului la FreeCAD și vom implementa suportul pentru elmer solver. Asigurați-vă că ați citit și ați înțeles Extend FEM Module înainte de a citi acest tutorial.

Acest tutorial acoperă doar modul de implementare a constrângerilor în Python. Spre deosebire de constrângerile solver și ecuații, urmați structura modulului FEM clasic. Adică toate modulele unei constrângeri au loc în pachetul PyObjects sau PyGui.

Sumar

  1. Create document object: Obiectul documentului care se află în analiză și în care constrângerea poate fi parametrizată și legată de limite.
  2. Create GUI command: Adăugați o comandă la atelierul FEM care adaugă o constrângere a fluxului la analiza activă.
  3. Create a task panel: Panoul de sarcini este necesar pentru a permite utilizatorului să definească limitele la care dorește să definească constrângerea de viteză. Acest lucru face ca intrarea parametrilor să fie mai ușor de utilizat.
  4. Extend elmers writer: Adăugați suport pentru noua constrângere modulul Elmer FEM solver prin dezactivarea exportatorului de fișiere sif.

Crearea Documentului Obiect

În acest pas vom modifica următoarele fișiere:

  • src/Mod/Fem/CMakeLists.txt
  • src/Mod/Fem/App/CMakeLists.txt
  • src/Mod/Fem/ObjectsFem.py

și adăugați următoarele fișiere:

  • src/Mod/Fem/PyObjects/_FemConstraintFlowVelocity.py
  • src/Mod/Fem/PyGui/_ViewProviderFemConstraintFlowVelocity.py

Un document proxy și o vedere proxy sunt necesare pentru noua constrângere. Acestea se află în module separate. Proxy documentul în PyObjects și vederea proxy în PyGui. Trebuie doar să copiați modulele dintr-o constrângere existentă, de ex.

  • PyObjects/_FemConstraintSelfWeight.py
  • PyGui/_ViewProviderFemConstraintSelfWeight.py

Reglați tipul de variabilă și proprietățile la nevoile dvs. Proxy-ul documentului al constrângerii fluxului arată după cum urmează:

class Proxy(FemConstraint.Proxy):
    Type = "Fem::ConstraintFlowVelocity"
    def __init__(self, obj):
        super(Proxy, self).__init__(obj)
        obj.addProperty(
            "App::PropertyFloat", "VelocityX",
            "Parameter", "Body heat flux")
        obj.addProperty(
            "App::PropertyBool", "VelocityXEnabled",
            "Parameter", "Body heat flux")
        obj.addProperty(
            "App::PropertyFloat", "VelocityY",
            "Parameter", "Body heat flux")
        obj.addProperty(
            "App::PropertyBool", "VelocityYEnabled",
            "Parameter", "Body heat flux")
        obj.addProperty(
            "App::PropertyFloat", "VelocityZ",
            "Parameter", "Body heat flux")
        obj.addProperty(
            "App::PropertyBool", "VelocityZEnabled",
            "Parameter", "Body heat flux")
        obj.addProperty(
            "App::PropertyBool", "NormalToBoundary",
            "Parameter", "Body heat flux")

Modulul care conține proxy-ul ar putea părea puțin mai complicat. Dar pentru moment ajustați calea pictogramelor. Vom reveni la acest dosar.

class ViewProxy(FemConstraint.ViewProxy):
    def getIcon(self):
        return ":/icons/fem-constraint-flow-velocity.svg"

Adăugați cele două module noi la construirea sistemului descrisă în Extend FEM Module. Găsiți lista de căutare corectă pentru modulele de constrângere.

Ca toate obiectele din bancul de lucru FEM, constrângerile de viteză trebuie să fie înregistrate ObjectsFem.py. Următoarea metodă adaugă viteza documentului activ. Această metodă va fi utilizată de comanda GUI pentru a adăuga constrângerea. Trebuie să fie inserat undeva în ObjectsFem.py.

def makeConstraintFlowVelocity(name="FlowVelocity"):
    obj = FreeCAD.ActiveDocument.addObject("Fem::ConstraintPython", name)
    import PyObjects._FemConstraintFlowVelocity
    PyObjects._FemConstraintFlowVelocity.Proxy(obj)
    if FreeCAD.GuiUp:
        import PyGui._ViewProviderFemConstraintFlowVelocity
        PyGui._ViewProviderFemConstraintFlowVelocity.ViewProxy(obj.ViewObject)
    return obj

Creează o camandă GUI

În acest pas vom modifica următoarele fișiere:

  • src/Mod/Fem/CMakeLists.txt
  • src/Mod/Fem/App/CMakeLists.txt
  • src/Mod/Fem/Gui/Workbench.cpp

și adăugați următorul fișier nou:

  • src/Mod/Fem/PyGui/_CommandFemConstraintFlowVelocity.py

Comanda permite utilizatorului să adauge constrângerea la analiza activă. Trebuie doar să copiați o comandă dintr-o constrângere existentă. Commands reside in the PyGui package. Ajustați resursele și efectuați metoda numită Activat la nevoile dvs. Utilizați, de asemenea, o comandă diferită id in the addCommand apel în partea de jos a modulului. Următoarea clasă este clasa constrângerii vitezei.

class Command(FemCommands.FemCommands):

    def __init__(self):
        super(Command, self).__init__()
        self.resources = {
            'Pixmap': 'fem-constraint-flow-velocity',
            'MenuText': QtCore.QT_TRANSLATE_NOOP(
                "FEM_ConstraintFlowVelocity",
                "Constraint Velocity"),
            'ToolTip': QtCore.QT_TRANSLATE_NOOP(
                "FEM_ConstraintFlowVelocity",
                "Creates a FEM constraint body heat flux")}
        self.is_active = 'with_analysis'

    def Activated(self):
        App.ActiveDocument.openTransaction(
            "Create FemConstraintFlowVelocity")
        Gui.addModule("ObjectsFem")
        Gui.doCommand(
            "FemGui.getActiveAnalysis().Member += "
            "[ObjectsFem.makeConstraintFlowVelocity()]")

Gui.addCommand('FEM_AddConstraintFlowVelocity', Command())

Adăugați noua comandă în sistem așa cum este încorporată Extend FEM Module. Identificați lista corectă pentru a căuta modulele de comandă existente.

Puneți o comandă în Gui/Workbench.cpp pentru a o adăuga în bara de instrumente și în meniu. Căutați o constrângere existentă din aceeași categorie ca cea nouă (de exemplu, Flow) copiați-o și adaptați id-ul comenzii. Acest lucru ar trebui făcut de două ori. Odată pentru meniu și din nou pentru bara de instrumente.

Creara Task Panel

În acest pas vom modifica următorul fișier:

  • src/Mod/Fem/PyGui/_ViewProviderFemConstraintFlowVelocity.py

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.

class _TaskPanel(object):

    def __init__(self, obj):
        self._obj = obj
        self._refWidget = FemSelectionWidgets.BoundarySelector()
        # self._refWidget = FemSelectionWidgets.SolidSelector()
        self._refWidget.setReferences(obj.References)
        self._paramWidget = Gui.PySideUic.loadUi(
            App.getHomePath() + "Mod/Fem/PyGui/TaskPanelFemFlowVelocity.ui")
        self._initParamWidget()
        self.form = [self._refWidget, self._paramWidget]
        analysis = FemMisc.findAnalysisOfMember(obj)
        self._mesh = FemMisc.getSingleMember(analysis, "Fem::FemMeshObject")
        self._part = self._mesh.Part if self._mesh is not None else None
        self._partVisible = None
        self._meshVisible = None

    def open(self):
        if self._mesh is not None and self._part is not None:
            self._meshVisible = self._mesh.ViewObject.isVisible()
            self._partVisible = self._part.ViewObject.isVisible()
            self._mesh.ViewObject.hide()
            self._part.ViewObject.show()

    def reject(self):
        self._restoreVisibility()
        return True

    def accept(self):
        if self._obj.References != self._refWidget.references():
            self._obj.References = self._refWidget.references()
        self._applyWidgetChanges()
        self._obj.Document.recompute()
        self._restoreVisibility()
        return True

    def _restoreVisibility(self):
        if self._mesh is not None and self._part is not None:
            if self._meshVisible:
                self._mesh.ViewObject.show()
            else:
                self._mesh.ViewObject.hide()
            if self._partVisible:
                self._part.ViewObject.show()
            else:
                self._part.ViewObject.hide()

    def _initParamWidget(self):
        unit = "m/s"
        self._paramWidget.velocityXTxt.setText(
            str(self._obj.VelocityX) + unit)
        self._paramWidget.velocityYTxt.setText(
            str(self._obj.VelocityY) + unit)
        self._paramWidget.velocityZTxt.setText(
            str(self._obj.VelocityZ) + unit)
        self._paramWidget.velocityXBox.setChecked(
            not self._obj.VelocityXEnabled)
        self._paramWidget.velocityYBox.setChecked(
            not self._obj.VelocityYEnabled)
        self._paramWidget.velocityZBox.setChecked(
            not self._obj.VelocityZEnabled)
        self._paramWidget.normalBox.setChecked(
            self._obj.NormalToBoundary)

    def _applyWidgetChanges(self):
        unit = "m/s"
        self._obj.VelocityXEnabled = \
            not self._paramWidget.velocityXBox.isChecked()
        if self._obj.VelocityXEnabled:
            quantity = Units.Quantity(self._paramWidget.velocityXTxt.text())
            self._obj.VelocityX = float(quantity.getValueAs(unit))
        self._obj.VelocityYEnabled = \
            not self._paramWidget.velocityYBox.isChecked()
        if self._obj.VelocityYEnabled:
            quantity = Units.Quantity(self._paramWidget.velocityYTxt.text())
            self._obj.VelocityY = float(quantity.getValueAs(unit))
        self._obj.VelocityZEnabled = \
            not self._paramWidget.velocityZBox.isChecked()
        if self._obj.VelocityZEnabled:
            quantity = Units.Quantity(self._paramWidget.velocityZTxt.text())
            self._obj.VelocityZ = float(quantity.getValueAs(unit))
        self._obj.NormalToBoundary = self._paramWidget.normalBox.isChecked()

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.

class ViewProxy(FemConstraint.ViewProxy):

    def getIcon(self):
        return ":/icons/fem-constraint-flow-velocity.svg"

    def setEdit(self, vobj, mode=0):
        task = _TaskPanel(vobj.Object)
        Gui.Control.showDialog(task)

    def unsetEdit(self, vobj, mode=0):
        Gui.Control.closeDialog()

    def doubleClicked(self, vobj):
        if Gui.Control.activeDialog():
            Gui.Control.closeDialog()
        Gui.ActiveDocument.setEdit(vobj.Object.Name)
        return True

Extend Elmers Writer

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

  • src/Mod/Fem/FemSolver/Elmer/Writer.py

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

def _handleFlowBndConditions(self):
    for obj in self._getMember("Fem::ConstraintFlowVelocity"):
        if obj.References:
            for name in obj.References[0][1]:
                if obj.VelocityXEnabled:
                    velocity = getFromUi(obj.VelocityX, "m/s", "L/T")
                    self._boundary(name, "Velocity 1", velocity)
                if obj.VelocityYEnabled:
                    velocity = getFromUi(obj.VelocityY, "m/s", "L/T")
                    self._boundary(name, "Velocity 2", velocity)
                if obj.VelocityZEnabled:
                    velocity = getFromUi(obj.VelocityZ, "m/s", "L/T")
                    self._boundary(name, "Velocity 3", velocity)
                if obj.NormalToBoundary:
                    self._boundary(name, "Normal-Tangential Velocity", True)
            self._handled(obj)