File size: 17,863 Bytes
985c397 | 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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 | # ***************************************************************************
# * Copyright (c) 2015 Bernd Hahnebach <bernd@bimstatik.org> *
# * *
# * This file is part of the FreeCAD CAx development system. *
# * *
# * 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 *
# * *
# ***************************************************************************
__title__ = "FreeCAD FEM solver calculix ccx tools task panel for the document object"
__author__ = "Bernd Hahnebach"
__url__ = "https://www.freecad.org"
## @package task_solver_ccxtools
# \ingroup FEM
# \brief task panel for solver ccx tools object
import os
import time
from PySide import QtCore
from PySide import QtGui
from PySide.QtCore import Qt
from PySide.QtGui import QApplication
import FreeCAD
import FreeCADGui
import FemGui
from femtools.femutils import getOutputWinColor
def unicode(text, *args):
return str(text)
class _TaskPanel:
"""
The TaskPanel for CalculiX ccx tools solver object
"""
PREFS_PATH = "User parameter:BaseApp/Preferences/Mod/Fem/Ccx"
def __init__(self, solver_object):
self.form = FreeCADGui.PySideUic.loadUi(
FreeCAD.getHomePath() + "Mod/Fem/Resources/ui/SolverCcxTools.ui"
)
from femtools.ccxtools import CcxTools as ccx
# we do not need to pass the analysis, it will be found on fea init
# TODO: if there is not analysis object in document init of fea
# will fail with an exception and task panel will not open
# handle more smart by a pop up error message and still open
# task panel, may be deactivate write and run button.
self.fea = ccx(solver_object)
self.fea.setup_working_dir()
try:
self.fea.setup_ccx()
except FileNotFoundError as e:
FreeCAD.Console.PrintWarning(e.args[0])
self.Calculix = QtCore.QProcess()
self.Timer = QtCore.QTimer()
self.Timer.start(300)
self.fem_console_message = ""
self.CCX_pipeline = None
self.CCX_mesh_visibility = False
# store visibility of possibly existing mesh object
CCX_mesh = self.fea.analysis.Document.getObject("CCX_Results_Mesh")
if CCX_mesh is not None:
self.CCX_mesh_visibility = CCX_mesh.ViewObject.Visibility
# Connect Signals and Slots
QtCore.QObject.connect(
self.form.tb_choose_working_dir, QtCore.SIGNAL("clicked()"), self.choose_working_dir
)
QtCore.QObject.connect(
self.form.pb_write_inp, QtCore.SIGNAL("clicked()"), self.write_input_file_handler
)
QtCore.QObject.connect(
self.form.pb_edit_inp, QtCore.SIGNAL("clicked()"), self.editCalculixInputFile
)
# connect stopCalculix before runCalculix
# see https://github.com/FreeCAD/FreeCAD/issues/12448
QtCore.QObject.connect(self.form.pb_run_ccx, QtCore.SIGNAL("clicked()"), self.stopCalculix)
QtCore.QObject.connect(self.form.pb_run_ccx, QtCore.SIGNAL("clicked()"), self.runCalculix)
QtCore.QObject.connect(
self.form.rb_static_analysis, QtCore.SIGNAL("clicked()"), self.select_static_analysis
)
QtCore.QObject.connect(
self.form.rb_frequency_analysis,
QtCore.SIGNAL("clicked()"),
self.select_frequency_analysis,
)
QtCore.QObject.connect(
self.form.rb_thermomech_analysis,
QtCore.SIGNAL("clicked()"),
self.select_thermomech_analysis,
)
QtCore.QObject.connect(
self.form.rb_check_mesh, QtCore.SIGNAL("clicked()"), self.select_check_mesh
)
QtCore.QObject.connect(
self.form.rb_buckling_analysis,
QtCore.SIGNAL("clicked()"),
self.select_buckling_analysis,
)
QtCore.QObject.connect(self.Calculix, QtCore.SIGNAL("started()"), self.calculixStarted)
QtCore.QObject.connect(
self.Calculix,
QtCore.SIGNAL("stateChanged(QProcess::ProcessState)"),
self.calculixStateChanged,
)
QtCore.QObject.connect(
self.Calculix, QtCore.SIGNAL("error(QProcess::ProcessError)"), self.calculixError
)
QtCore.QObject.connect(
self.Calculix,
QtCore.SIGNAL("finished(int, QProcess::ExitStatus)"),
self.calculixFinished,
)
QtCore.QObject.connect(self.Timer, QtCore.SIGNAL("timeout()"), self.UpdateText)
self.update()
def getStandardButtons(self):
# only show a close button
# def accept() in no longer needed, since there is no OK button
return QtGui.QDialogButtonBox.Close
def reject(self):
FreeCADGui.ActiveDocument.resetEdit()
def update(self):
"fills the widgets"
self.form.le_working_dir.setText(self.fea.working_dir)
if self.fea.solver.AnalysisType == "static":
self.form.rb_static_analysis.setChecked(True)
elif self.fea.solver.AnalysisType == "frequency":
self.form.rb_frequency_analysis.setChecked(True)
elif self.fea.solver.AnalysisType == "thermomech":
self.form.rb_thermomech_analysis.setChecked(True)
elif self.fea.solver.AnalysisType == "check":
self.form.rb_check_mesh.setChecked(True)
elif self.fea.solver.AnalysisType == "buckling":
self.form.rb_buckling_analysis.setChecked(True)
return
def femConsoleMessage(self, message="", outputwin_color_type=None):
self.fem_console_message = self.fem_console_message + (
'<font color="{}"><b>{:4.1f}:</b></font> '.format(
getOutputWinColor("Logging"), time.time() - self.Start
)
)
if outputwin_color_type:
if outputwin_color_type == "#00AA00": # Success is not part of output window parameters
self.fem_console_message += '<font color="{}">{}</font><br>'.format(
outputwin_color_type, message
)
else:
self.fem_console_message += '<font color="{}">{}</font><br>'.format(
getOutputWinColor(outputwin_color_type), message
)
else:
self.fem_console_message += message + "<br>"
self.form.textEdit_Output.setText(self.fem_console_message)
self.form.textEdit_Output.moveCursor(QtGui.QTextCursor.End)
def printCalculiXstdout(self):
out = self.Calculix.readAllStandardOutput()
# print(type(out))
# <class 'PySide2.QtCore.QByteArray'>
if out.isEmpty():
self.femConsoleMessage("CalculiX stdout is empty", "Error")
return False
# https://forum.freecad.org/viewtopic.php?f=18&t=39195
# convert QByteArray to a binary string an decode it to "utf-8"
out = out.data().decode() # "utf-8" can be omitted
out = os.linesep.join([s for s in out.splitlines() if s])
out = out.replace("\n", "<br>")
# print(out)
self.femConsoleMessage(out)
if "*ERROR in e_c3d: nonpositive jacobian" in out:
error_message = (
"\n\nCalculiX returned an error due to "
"nonpositive jacobian determinant in at least one element\n"
"Use the run button on selected solver to get a better error output.\n"
)
FreeCAD.Console.PrintError(error_message)
if "*ERROR" in out:
return False
else:
return True
def UpdateText(self):
if self.Calculix.state() == QtCore.QProcess.ProcessState.Running:
self.form.l_time.setText(f"Time: {time.time() - self.Start:4.1f}: ")
def calculixError(self, error=""):
print(f"Error() {error}")
self.femConsoleMessage(f"CalculiX execute error: {error}", "Error")
def calculixNoError(self):
print("CalculiX done without error!")
self.femConsoleMessage(
"CalculiX done without error!", "#00AA00"
) # Green leaving hard coded
def calculixStarted(self):
# print("calculixStarted()")
FreeCAD.Console.PrintLog(f"calculix state: {self.Calculix.state()}\n")
self.form.pb_run_ccx.setText("Stop CalculiX")
def calculixStateChanged(self, newState):
if newState == QtCore.QProcess.ProcessState.Starting:
self.femConsoleMessage("Starting CalculiX...")
elif newState == QtCore.QProcess.ProcessState.Running:
self.femConsoleMessage("CalculiX is running...")
elif newState == QtCore.QProcess.ProcessState.NotRunning:
self.femConsoleMessage("CalculiX stopped.")
else:
self.femConsoleMessage("Problems.", "Error")
def calculixFinished(self, exitCode, exitStatus):
# print("calculixFinished(), exit code: {}".format(exitCode))
FreeCAD.Console.PrintLog(f"calculix state: {self.Calculix.state()}\n")
# Restore previous cwd
QtCore.QDir.setCurrent(self.cwd)
self.Timer.stop()
self.form.pb_run_ccx.setText("Re-run CalculiX")
if exitStatus != QtCore.QProcess.ExitStatus.NormalExit:
return
if self.printCalculiXstdout():
self.calculixNoError()
else:
self.calculixError()
self.femConsoleMessage("Loading result sets...")
self.form.l_time.setText(f"Time: {time.time() - self.Start:4.1f}: ")
self.fea.reset_mesh_purge_results_checked()
self.fea.inp_file_name = self.fea.inp_file_name
# check if ccx is greater than 2.10, if not do not read results
# https://forum.freecad.org/viewtopic.php?f=18&t=23548#p183829 Point 3
# https://forum.freecad.org/viewtopic.php?f=18&t=23548&start=20#p183909
# https://forum.freecad.org/viewtopic.php?f=18&t=23548&start=30#p185027
# https://github.com/FreeCAD/FreeCAD/commit/3dd1c9f
majorVersion, minorVersion = self.fea.get_ccx_version()
if majorVersion == 2 and minorVersion <= 10:
message = (
"The used CalculiX version {}.{} creates broken output files. "
"The result file will not be read by FreeCAD FEM. "
"You still can try to read it stand alone with FreeCAD, but it is "
"strongly recommended to upgrade CalculiX to a newer version.\n".format(
majorVersion, minorVersion
)
)
QtGui.QMessageBox.warning(None, "Upgrade CalculiX", message)
raise
QApplication.setOverrideCursor(Qt.WaitCursor)
try:
self.fea.load_results()
except Exception as e:
FreeCAD.Console.PrintError("loading results failed\n")
FreeCAD.Console.PrintError(e)
QApplication.restoreOverrideCursor()
self.form.l_time.setText(f"Time: {time.time() - self.Start:4.1f}: ")
# restore mesh object visibility
CCX_mesh = self.fea.analysis.Document.getObject("ResultMesh")
if CCX_mesh is not None:
CCX_mesh.ViewObject.Visibility = self.CCX_mesh_visibility
def choose_working_dir(self):
wd = QtGui.QFileDialog.getExistingDirectory(
None, "Choose CalculiX working directory", self.fea.working_dir
)
if os.path.isdir(wd):
self.fea.setup_working_dir(wd)
self.form.le_working_dir.setText(self.fea.working_dir)
def write_input_file_handler(self):
self.Start = time.time()
self.form.l_time.setText(f"Time: {time.time() - self.Start:4.1f}: ")
QApplication.restoreOverrideCursor()
if self.check_prerequisites_helper():
QApplication.setOverrideCursor(Qt.WaitCursor)
self.fea.write_inp_file()
if self.fea.inp_file_name != "":
self.femConsoleMessage("Write completed.")
self.form.pb_edit_inp.setEnabled(True)
self.form.pb_run_ccx.setEnabled(True)
else:
self.femConsoleMessage("Write .inp file failed!", "Error")
QApplication.restoreOverrideCursor()
self.form.l_time.setText(f"Time: {time.time() - self.Start:4.1f}: ")
def check_prerequisites_helper(self):
self.Start = time.time()
self.femConsoleMessage("Check dependencies...")
self.form.l_time.setText(f"Time: {time.time() - self.Start:4.1f}: ")
self.fea.update_objects()
message = self.fea.check_prerequisites()
if message != "":
QtGui.QMessageBox.critical(None, "Missing prerequisite(s)", message)
return False
return True
def start_ext_editor(self, ext_editor_path, filename):
if not hasattr(self, "ext_editor_process"):
self.ext_editor_process = QtCore.QProcess()
if self.ext_editor_process.state() != QtCore.QProcess.Running:
self.ext_editor_process.start(ext_editor_path, [filename])
def editCalculixInputFile(self):
print(f"editCalculixInputFile {self.fea.inp_file_name}")
ccx_prefs = FreeCAD.ParamGet(self.PREFS_PATH)
if ccx_prefs.GetBool("UseInternalEditor", True):
FemGui.open(self.fea.inp_file_name)
else:
ext_editor_path = ccx_prefs.GetString("ExternalEditorPath", "")
if ext_editor_path:
self.start_ext_editor(ext_editor_path, self.fea.inp_file_name)
else:
print(
"External editor is not defined in FEM preferences. "
"Falling back to internal editor"
)
FemGui.open(self.fea.inp_file_name)
def runCalculix(self):
if self.Calculix.state() == QtCore.QProcess.ProcessState.NotRunning:
if self.fea.ccx_binary_present is False:
self.femConsoleMessage(
"CalculiX can not be started. Missing or incorrect CalculiX binary: {}".format(
self.fea.ccx_binary
)
)
# TODO deactivate the run button
return
# print("runCalculix")
self.Start = time.time()
self.femConsoleMessage(f"CalculiX binary: {self.fea.ccx_binary}")
self.femConsoleMessage(f"CalculiX input file: {self.fea.inp_file_name}")
self.femConsoleMessage("Run CalculiX...")
FreeCAD.Console.PrintMessage(
f"run CalculiX at: {self.fea.ccx_binary} with: {self.fea.inp_file_name}\n"
)
# change cwd because ccx may crash if directory has no write permission
# there is also a limit of the length of file names so jump to the document directory
# Set up for multi-threading. Note: same functionality as ccx_tools.py/start_ccx()
ccx_prefs = FreeCAD.ParamGet(self.PREFS_PATH)
env = QtCore.QProcessEnvironment.systemEnvironment()
num_cpu_pref = ccx_prefs.GetInt("AnalysisNumCPUs", QtCore.QThread.idealThreadCount())
env.insert("OMP_NUM_THREADS", str(num_cpu_pref))
pastix_prec = "1" if self.fea.solver.PastixMixedPrecision else "0"
env.insert("PASTIX_MIXED_PRECISION", pastix_prec)
self.Calculix.setProcessEnvironment(env)
self.cwd = QtCore.QDir.currentPath()
fi = QtCore.QFileInfo(self.fea.inp_file_name)
QtCore.QDir.setCurrent(fi.path())
self.Calculix.start(self.fea.ccx_binary, ["-i", fi.baseName()])
QApplication.restoreOverrideCursor()
def stopCalculix(self):
if self.Calculix.state() == QtCore.QProcess.ProcessState.Running:
self.Calculix.kill()
def select_analysis_type(self, analysis_type):
if self.fea.solver.AnalysisType != analysis_type:
self.fea.solver.AnalysisType = analysis_type
self.form.pb_edit_inp.setEnabled(False)
self.form.pb_run_ccx.setEnabled(False)
def select_static_analysis(self):
self.select_analysis_type("static")
def select_frequency_analysis(self):
self.select_analysis_type("frequency")
def select_thermomech_analysis(self):
self.select_analysis_type("thermomech")
def select_check_mesh(self):
self.select_analysis_type("check")
def select_buckling_analysis(self):
self.select_analysis_type("buckling")
|