forked from jaheyns/CfdOF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CfdSolverFoam.py
186 lines (154 loc) · 8.23 KB
/
CfdSolverFoam.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# ***************************************************************************
# * *
# * Copyright (c) 2013-2015 - Juergen Riegel <[email protected]> *
# * Copyright (c) 2015 - Qingfeng Xia <qingfeng.xia()eng.ox.ac.uk> *
# * Copyright (c) 2017 Johan Heyns (CSIR) <[email protected]> *
# * Copyright (c) 2017 Oliver Oxtoby (CSIR) <[email protected]> *
# * Copyright (c) 2017 Alfred Bogaers (CSIR) <[email protected]> *
# * Copyright (c) 2019 Oliver Oxtoby <[email protected]> *
# * *
# * This program is free software; you can redistribute it and/or modify *
# * it under the terms of the GNU Lesser General Public License (LGPL) *
# * as published by the Free Software Foundation; either version 2 of *
# * the License, or (at your option) any later version. *
# * for detail see the LICENCE text file. *
# * *
# * This program is distributed in the hope that it will be useful, *
# * but WITHOUT ANY WARRANTY; without even the implied warranty of *
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
# * GNU Library General Public License for more details. *
# * *
# * You should have received a copy of the GNU Library General Public *
# * License along with this program; if not, write to the Free Software *
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
# * USA *
# * *
# ***************************************************************************
import FreeCAD
import CfdTools
from CfdTools import addObjectProperty
import os
import os.path
if FreeCAD.GuiUp:
import FreeCADGui
from PySide import QtCore
def makeCfdSolverFoam(name="CfdSolver"):
obj = FreeCAD.ActiveDocument.addObject("Part::FeaturePython", name)
_CfdSolverFoam(obj)
if FreeCAD.GuiUp:
_ViewProviderCfdSolverFoam(obj.ViewObject)
return obj
class _CommandCfdSolverFoam:
def GetResources(self):
icon_path = os.path.join(CfdTools.get_module_path(), "Gui", "Resources", "icons", "solver.png")
return {'Pixmap': icon_path,
'MenuText': QtCore.QT_TRANSLATE_NOOP("Cfd_SolverControl", "Solver job control"),
'Accel': "S, C",
'ToolTip': QtCore.QT_TRANSLATE_NOOP("Cfd_SolverControl", "Edit properties and run solver")}
def IsActive(self):
return CfdTools.getActiveAnalysis() is not None
def Activated(self):
CfdTools.hide_parts_show_meshes()
isPresent = False
members = CfdTools.getActiveAnalysis().Group
for i in members:
if isinstance(i.Proxy, _CfdSolverFoam):
FreeCADGui.activeDocument().setEdit(i.Name)
isPresent = True
# Allowing user to re-create if CFDSolver was deleted.
if not isPresent:
FreeCADGui.addModule("CfdTools")
FreeCADGui.addModule("CfdSolverFoam")
FreeCADGui.doCommand("CfdTools.getActiveAnalysis().addObject(CfdSolverFoam.makeCfdSolverFoam())")
FreeCADGui.doCommand("Gui.activeDocument().setEdit(App.ActiveDocument.ActiveObject.Name)")
if FreeCAD.GuiUp:
FreeCADGui.addCommand('Cfd_SolverControl', _CommandCfdSolverFoam())
class _CfdSolverFoam(object):
""" Solver-specific properties """
def __init__(self, obj):
self.Type = "CfdSolverFoam"
self.Object = obj # keep a ref to the DocObj for nonGui usage
obj.Proxy = self # link between App::DocumentObject to this object
addObjectProperty(obj, "InputCaseName", "case", "App::PropertyFile", "Solver",
"Name of case directory where the input files are written")
addObjectProperty(obj, "Parallel", True, "App::PropertyBool", "Solver",
"Parallel analysis on multiple CPU cores")
addObjectProperty(obj, "ParallelCores", 4, "App::PropertyInteger", "Solver",
"Number of cores on which to run parallel analysis")
addObjectProperty(obj, "MaxIterations", 2000, "App::PropertyInteger", "IterationControl",
"Maximum number of iterations to run steady-state analysis")
addObjectProperty(obj, "SteadyWriteInterval", 100, "App::PropertyFloat", "IterationControl",
"Iteration output interval")
addObjectProperty(obj, "ConvergenceTol", 1e-4, "App::PropertyFloat", "IterationControl",
"Global absolute solution convergence criterion")
addObjectProperty(obj, "EndTime", "1 s", "App::PropertyQuantity", "TimeStepControl",
"Total time to run transient solution")
addObjectProperty(obj, "TimeStep", "0.001 s", "App::PropertyQuantity", "TimeStepControl",
"Time step increment")
addObjectProperty(obj, "TransientWriteInterval", "0.1 s", "App::PropertyQuantity", "IterationControl",
"Iteration output interval")
def execute(self, obj):
return
def onChanged(self, obj, prop):
return
def __getstate__(self):
return self.Type
def __setstate__(self, state):
if state:
self.Type = state
class _ViewProviderCfdSolverFoam:
"""A View Provider for the Solver object, base class for all derived solver
derived solver should implement a specific TaskPanel and set up solver and override setEdit()"""
def __init__(self, vobj):
vobj.Proxy = self
def getIcon(self):
# """after load from FCStd file, self.icon does not exist, return constant path instead"""
# return ":/icons/fem-solver.svg"
icon_path = os.path.join(CfdTools.get_module_path(), "Gui", "Resources", "icons", "solver.png")
return icon_path
def attach(self, vobj):
self.ViewObject = vobj
self.Object = vobj.Object
def updateData(self, obj, prop):
return
def onChanged(self, vobj, prop):
return
def doubleClicked(self, vobj):
if FreeCADGui.activeWorkbench().name() != 'CfdOFWorkbench':
FreeCADGui.activateWorkbench("CfdOFWorkbench")
doc = FreeCADGui.getDocument(vobj.Object.Document)
# it should be possible to find the AnalysisObject although it is not a documentObjectGroup
if not CfdTools.getActiveAnalysis():
analysis_obj = CfdTools.getParentAnalysisObject(self.Object)
if analysis_obj:
CfdTools.setActiveAnalysis(analysis_obj)
else:
FreeCAD.Console.PrintError(
'No Active Analysis is detected from solver object in the active Document!\n')
if not doc.getInEdit():
if CfdTools.getActiveAnalysis().Document is FreeCAD.ActiveDocument:
if self.Object in CfdTools.getActiveAnalysis().Group:
doc.setEdit(vobj.Object.Name)
else:
FreeCAD.Console.PrintError('Activate the analysis this solver belongs to!\n')
else:
FreeCAD.Console.PrintError('Active Analysis is not in active Document!\n')
else:
FreeCAD.Console.PrintError('Task dialog already open\n')
return True
def setEdit(self, vobj, mode):
if CfdTools.getActiveAnalysis():
from CfdRunnableFoam import CfdRunnableFoam
foamRunnable = CfdRunnableFoam(CfdTools.getActiveAnalysis(), self.Object)
from _TaskPanelCfdSolverControl import _TaskPanelCfdSolverControl
taskd = _TaskPanelCfdSolverControl(foamRunnable)
taskd.obj = vobj.Object
FreeCADGui.Control.showDialog(taskd)
return True
def unsetEdit(self, vobj, mode):
FreeCADGui.Control.closeDialog()
return
def __getstate__(self):
return None
def __setstate__(self, state):
return None