From 40d29fe329be2b26fa3992e38ef3b1c5e8902caa Mon Sep 17 00:00:00 2001 From: Bernd Hahnebach Date: Tue, 17 Aug 2021 21:31:25 +0200 Subject: [PATCH] FEM: task panels, improve units value handling --- .../task_constraint_electrostaticpotential.py | 3 +- .../task_constraint_flowvelocity.py | 6 ++-- .../task_constraint_initialflowvelocity.py | 6 ++-- .../Fem/femtaskpanels/task_element_fluid1D.py | 17 +++------- .../Fem/femtaskpanels/task_material_common.py | 32 ++++++++++--------- 5 files changed, 30 insertions(+), 34 deletions(-) diff --git a/src/Mod/Fem/femtaskpanels/task_constraint_electrostaticpotential.py b/src/Mod/Fem/femtaskpanels/task_constraint_electrostaticpotential.py index 7e123db009..497da5153e 100644 --- a/src/Mod/Fem/femtaskpanels/task_constraint_electrostaticpotential.py +++ b/src/Mod/Fem/femtaskpanels/task_constraint_electrostaticpotential.py @@ -131,7 +131,8 @@ class _TaskPanel(object): .format(self._paramWidget.potentialTxt.text()) ) if quantity is not None: - self._obj.Potential = float(quantity.getValueAs(unit)) + self._obj.Potential = quantity.getValueAs(unit).Value + self._obj.PotentialConstant = self._paramWidget.potentialConstantBox.isChecked() self._obj.ElectricInfinity = self._paramWidget.electricInfinityBox.isChecked() diff --git a/src/Mod/Fem/femtaskpanels/task_constraint_flowvelocity.py b/src/Mod/Fem/femtaskpanels/task_constraint_flowvelocity.py index 6bb81b1a26..3669c63bd3 100644 --- a/src/Mod/Fem/femtaskpanels/task_constraint_flowvelocity.py +++ b/src/Mod/Fem/femtaskpanels/task_constraint_flowvelocity.py @@ -114,15 +114,15 @@ class _TaskPanel(object): 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.VelocityX = quantity.getValueAs(unit).Value 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.VelocityY = quantity.getValueAs(unit).Value 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.VelocityZ = quantity.getValueAs(unit).Value self._obj.NormalToBoundary = self._paramWidget.normalBox.isChecked() diff --git a/src/Mod/Fem/femtaskpanels/task_constraint_initialflowvelocity.py b/src/Mod/Fem/femtaskpanels/task_constraint_initialflowvelocity.py index 6cd7c3c2b4..d30b0254eb 100644 --- a/src/Mod/Fem/femtaskpanels/task_constraint_initialflowvelocity.py +++ b/src/Mod/Fem/femtaskpanels/task_constraint_initialflowvelocity.py @@ -107,14 +107,14 @@ class _TaskPanel(object): 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.VelocityX = quantity.getValueAs(unit).Value 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.VelocityY = quantity.getValueAs(unit).Value 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.VelocityZ = quantity.getValueAs(unit).Value diff --git a/src/Mod/Fem/femtaskpanels/task_element_fluid1D.py b/src/Mod/Fem/femtaskpanels/task_element_fluid1D.py index 6eaf6b2230..3557e24219 100644 --- a/src/Mod/Fem/femtaskpanels/task_element_fluid1D.py +++ b/src/Mod/Fem/femtaskpanels/task_element_fluid1D.py @@ -36,6 +36,7 @@ from PySide import QtGui import FreeCAD import FreeCADGui +from FreeCAD import Units from femguiutils import selection_widgets from femobjects import element_fluid1D @@ -422,24 +423,16 @@ class _TaskPanel: self.ContractArea2 = base_quantity_value def inlet_pressure_changed(self, base_quantity_value): - self.InletPressure = float( - FreeCAD.Units.Quantity(base_quantity_value).getValueAs("MPa") - ) + self.InletPressure = Units.Quantity(base_quantity_value).getValueAs("MPa").Value def outlet_pressure_changed(self, base_quantity_value): - self.OutletPressure = float( - FreeCAD.Units.Quantity(base_quantity_value).getValueAs("MPa") - ) + self.OutletPressure = Units.Quantity(base_quantity_value).getValueAs("MPa").Value def inlet_flowrate_changed(self, base_quantity_value): - self.InletFlowRate = float( - FreeCAD.Units.Quantity(base_quantity_value).getValueAs("kg/s") - ) + self.InletFlowRate = Units.Quantity(base_quantity_value).getValueAs("kg/s").Value def outlet_flowrate_changed(self, base_quantity_value): - self.OutletFlowRate = float( - FreeCAD.Units.Quantity(base_quantity_value).getValueAs("kg/s") - ) + self.OutletFlowRate = Units.Quantity(base_quantity_value).getValueAs("kg/s").Value def inlet_pressure_active(self, active): self.InletPressureActive = active diff --git a/src/Mod/Fem/femtaskpanels/task_material_common.py b/src/Mod/Fem/femtaskpanels/task_material_common.py index b25b376fb3..1ce0320928 100644 --- a/src/Mod/Fem/femtaskpanels/task_material_common.py +++ b/src/Mod/Fem/femtaskpanels/task_material_common.py @@ -427,8 +427,10 @@ class _TaskPanel: ) self.material["YoungsModulus"] = "0 MPa" if "PoissonRatio" in self.material: - # PoissonRatio does not have a unit, but it is checked it there is no value at all + # PoissonRatio does not have a unit, but it is checked if there is no value at all try: + # next line with float() is ok + # a quantity of a empty string returns very small value, would be wrong here float(self.material["PoissonRatio"]) except ValueError: FreeCAD.Console.PrintMessage( @@ -645,55 +647,55 @@ class _TaskPanel: def set_mat_params_in_input_fields(self, matmap): if "YoungsModulus" in matmap: ym_new_unit = "MPa" - ym = FreeCAD.Units.Quantity(matmap["YoungsModulus"]) + ym = Units.Quantity(matmap["YoungsModulus"]) ym_with_new_unit = ym.getValueAs(ym_new_unit) - q = FreeCAD.Units.Quantity("{} {}".format(ym_with_new_unit, ym_new_unit)) + q = Units.Quantity("{} {}".format(ym_with_new_unit, ym_new_unit)) self.parameterWidget.input_fd_young_modulus.setText(q.UserString) if "PoissonRatio" in matmap: self.parameterWidget.spinBox_poisson_ratio.setValue(float(matmap["PoissonRatio"])) # Fluidic properties if "KinematicViscosity" in matmap: nu_new_unit = "m^2/s" - nu = FreeCAD.Units.Quantity(matmap["KinematicViscosity"]) + nu = Units.Quantity(matmap["KinematicViscosity"]) nu_with_new_unit = nu.getValueAs(nu_new_unit) - q = FreeCAD.Units.Quantity("{} {}".format(nu_with_new_unit, nu_new_unit)) + q = Units.Quantity("{} {}".format(nu_with_new_unit, nu_new_unit)) self.parameterWidget.input_fd_kinematic_viscosity.setText(q.UserString) # For isotropic materials and fluidic material # use the volumetric thermal expansion coefficient # is approximately three times the linear coefficient for solids if "VolumetricThermalExpansionCoefficient" in matmap: vtec_new_unit = "m^3/m^3/K" - vtec = FreeCAD.Units.Quantity(matmap["VolumetricThermalExpansionCoefficient"]) + vtec = Units.Quantity(matmap["VolumetricThermalExpansionCoefficient"]) vtec_with_new_unit = vtec.getValueAs(vtec_new_unit) - q = FreeCAD.Units.Quantity("{} {}".format(vtec_with_new_unit, vtec_new_unit)) + q = Units.Quantity("{} {}".format(vtec_with_new_unit, vtec_new_unit)) self.parameterWidget.input_fd_vol_expansion_coefficient.setText(q.UserString) if "Density" in matmap: density_new_unit = "kg/m^3" - density = FreeCAD.Units.Quantity(matmap["Density"]) + density = Units.Quantity(matmap["Density"]) density_with_new_unit = density.getValueAs(density_new_unit) # self.parameterWidget.input_fd_density.setText( # "{} {}".format(density_with_new_unit, density_new_unit) # ) - q = FreeCAD.Units.Quantity("{} {}".format(density_with_new_unit, density_new_unit)) + q = Units.Quantity("{} {}".format(density_with_new_unit, density_new_unit)) self.parameterWidget.input_fd_density.setText(q.UserString) # thermal properties if "ThermalConductivity" in matmap: tc_new_unit = "W/m/K" - tc = FreeCAD.Units.Quantity(matmap["ThermalConductivity"]) + tc = Units.Quantity(matmap["ThermalConductivity"]) tc_with_new_unit = tc.getValueAs(tc_new_unit) - q = FreeCAD.Units.Quantity("{} {}".format(tc_with_new_unit, tc_new_unit)) + q = Units.Quantity("{} {}".format(tc_with_new_unit, tc_new_unit)) self.parameterWidget.input_fd_thermal_conductivity.setText(q.UserString) if "ThermalExpansionCoefficient" in matmap: # linear, only for solid tec_new_unit = "um/m/K" - tec = FreeCAD.Units.Quantity(matmap["ThermalExpansionCoefficient"]) + tec = Units.Quantity(matmap["ThermalExpansionCoefficient"]) tec_with_new_unit = tec.getValueAs(tec_new_unit) - q = FreeCAD.Units.Quantity("{} {}".format(tec_with_new_unit, tec_new_unit)) + q = Units.Quantity("{} {}".format(tec_with_new_unit, tec_new_unit)) self.parameterWidget.input_fd_expansion_coefficient.setText(q.UserString) if "SpecificHeat" in matmap: sh_new_unit = "J/kg/K" - sh = FreeCAD.Units.Quantity(matmap["SpecificHeat"]) + sh = Units.Quantity(matmap["SpecificHeat"]) sh_with_new_unit = sh.getValueAs(sh_new_unit) - q = FreeCAD.Units.Quantity("{} {}".format(sh_with_new_unit, sh_new_unit)) + q = Units.Quantity("{} {}".format(sh_with_new_unit, sh_new_unit)) self.parameterWidget.input_fd_specific_heat.setText(q.UserString) # fill the combo box with cards **************************************************************