From 9da842d884c650da05e666bd9e59667cddc5dc53 Mon Sep 17 00:00:00 2001 From: marioalexis Date: Fri, 19 Jan 2024 17:54:16 -0300 Subject: [PATCH] Fem: Improve self weight constraint - fixes #11652 --- .../constraint_selfweight_cantilever.py | 2 +- .../Fem/femobjects/constraint_selfweight.py | 69 ++++++++++++------- .../calculix/write_constraint_selfweight.py | 8 +-- .../elmer/equations/deformation_writer.py | 8 +-- .../elmer/equations/elasticity_writer.py | 8 +-- .../constraint_selfweight_cantilever.inp | 2 +- 6 files changed, 59 insertions(+), 38 deletions(-) diff --git a/src/Mod/Fem/femexamples/constraint_selfweight_cantilever.py b/src/Mod/Fem/femexamples/constraint_selfweight_cantilever.py index ee6e3c572a..6473c0b959 100644 --- a/src/Mod/Fem/femexamples/constraint_selfweight_cantilever.py +++ b/src/Mod/Fem/femexamples/constraint_selfweight_cantilever.py @@ -124,7 +124,7 @@ def setup(doc=None, solvertype="ccxtools"): # constraint selfweight con_selfweight = ObjectsFem.makeConstraintSelfWeight(doc, "ConstraintSelfWeight") - con_selfweight.Gravity_z = -1.00 + con_selfweight.GravityDirection = (0, 0, -1) analysis.addObject(con_selfweight) # mesh diff --git a/src/Mod/Fem/femobjects/constraint_selfweight.py b/src/Mod/Fem/femobjects/constraint_selfweight.py index 843457bd38..6972ad2d9d 100644 --- a/src/Mod/Fem/femobjects/constraint_selfweight.py +++ b/src/Mod/Fem/femobjects/constraint_selfweight.py @@ -29,7 +29,9 @@ __url__ = "https://www.freecad.org" # \ingroup FEM # \brief constraint self weight object +import FreeCAD from . import base_fempythonobject +from femtools import constants class ConstraintSelfWeight(base_fempythonobject.BaseFemPythonObject): @@ -42,33 +44,52 @@ class ConstraintSelfWeight(base_fempythonobject.BaseFemPythonObject): def __init__(self, obj): super(ConstraintSelfWeight, self).__init__(obj) - obj.addProperty( - "App::PropertyFloat", - "Gravity_x", - "Gravity", - "Gravity direction: set the x-component of the normalized gravity vector" - ) - - obj.addProperty( - "App::PropertyFloat", - "Gravity_y", - "Gravity", - "Gravity direction: set the y-component of the normalized gravity vector" - ) - - obj.addProperty( - "App::PropertyFloat", - "Gravity_z", - "Gravity", - "Gravity direction: set the z-component of the normalized gravity vector" - ) - - obj.Gravity_x = 0.0 - obj.Gravity_y = 0.0 - obj.Gravity_z = -1.0 + self.addProperty(obj) # https://wiki.freecad.org/Scripted_objects#Property_Type # https://forum.freecad.org/viewtopic.php?f=18&t=13460&start=20#p109709 # https://forum.freecad.org/viewtopic.php?t=25524 # obj.setEditorMode("References", 1) # read only in PropertyEditor, but writeable by Python obj.setEditorMode("References", 2) # do not show in Editor + + def addProperty(self, obj): + obj.addProperty("App::PropertyAcceleration", + "GravityAcceleration", + "Gravity", + "Gravity acceleration") + obj.GravityAcceleration = constants.gravity() + + obj.addProperty("App::PropertyVector", + "GravityDirection", + "Gravity", + "Normalized gravity direction") + obj.GravityDirection = FreeCAD.Vector(0, 0, -1) + + obj.setPropertyStatus("NormalDirection", "Hidden") + + def onDocumentRestored(self, obj): + # migrate old App::PropertyFloat "Gravity_*" if exists + try: + grav_x = obj.getPropertyByName("Gravity_x") + grav_y = obj.getPropertyByName("Gravity_y") + grav_z = obj.getPropertyByName("Gravity_z") + grav = FreeCAD.Vector(grav_x, grav_y, grav_z) + + self.addProperty(obj) + obj.GravityAcceleration = constants.gravity() + obj.GravityAcceleration *= grav.Length + obj.GravityDirection = grav.normalize() + + obj.removeProperty("Gravity_x") + obj.removeProperty("Gravity_y") + obj.removeProperty("Gravity_z") + + return + + except: + return + + def execute(self, obj): + obj.GravityDirection.normalize() + + return False diff --git a/src/Mod/Fem/femsolver/calculix/write_constraint_selfweight.py b/src/Mod/Fem/femsolver/calculix/write_constraint_selfweight.py index a4eeeb495a..ee366df41c 100644 --- a/src/Mod/Fem/femsolver/calculix/write_constraint_selfweight.py +++ b/src/Mod/Fem/femsolver/calculix/write_constraint_selfweight.py @@ -52,10 +52,10 @@ def write_constraint(f, femobj, selwei_obj, ccxwriter): "{},GRAV,{:.13G},{:.13G},{:.13G},{:.13G}\n" .format( ccxwriter.ccx_eall, - ccxwriter.gravity, # actual magnitude of gravity vector - selwei_obj.Gravity_x, # coordinate x of normalized gravity vector - selwei_obj.Gravity_y, # y - selwei_obj.Gravity_z # z + selwei_obj.GravityAcceleration.getValueAs("mm/s^2").Value, # actual magnitude of gravity vector + selwei_obj.GravityDirection.x, # coordinate x of normalized gravity vector + selwei_obj.GravityDirection.y, # y + selwei_obj.GravityDirection.z # z ) ) f.write("\n") diff --git a/src/Mod/Fem/femsolver/elmer/equations/deformation_writer.py b/src/Mod/Fem/femsolver/elmer/equations/deformation_writer.py index 080976218d..f76dd39fe0 100644 --- a/src/Mod/Fem/femsolver/elmer/equations/deformation_writer.py +++ b/src/Mod/Fem/femsolver/elmer/equations/deformation_writer.py @@ -148,7 +148,7 @@ class DeformationWriter: obj = self.write.getSingleMember("Fem::ConstraintSelfWeight") if obj is not None: for name in bodies: - gravity = self.write.convert(self.write.constsdef["Gravity"], "L/T^2") + gravity = self.write.convert(obj.GravityAcceleration.toStr(), "L/T^2") if self.write.getBodyMaterial(name) is None: raise general_writer.WriteError( "The body {} is not referenced in any material.\n\n".format(name) @@ -164,9 +164,9 @@ class DeformationWriter: dimension = "M/L^2" density = self.write.convert(densityQuantity, dimension) - force1 = gravity * obj.Gravity_x * density - force2 = gravity * obj.Gravity_y * density - force3 = gravity * obj.Gravity_z * density + force1 = gravity * obj.GravityDirection.x * density + force2 = gravity * obj.GravityDirection.y * density + force3 = gravity * obj.GravityDirection.z * density self.write.bodyForce(name, "Stress Bodyforce 1", force1) self.write.bodyForce(name, "Stress Bodyforce 2", force2) self.write.bodyForce(name, "Stress Bodyforce 3", force3) diff --git a/src/Mod/Fem/femsolver/elmer/equations/elasticity_writer.py b/src/Mod/Fem/femsolver/elmer/equations/elasticity_writer.py index bc71a67368..4424e903e7 100644 --- a/src/Mod/Fem/femsolver/elmer/equations/elasticity_writer.py +++ b/src/Mod/Fem/femsolver/elmer/equations/elasticity_writer.py @@ -364,7 +364,7 @@ class ElasticityWriter: obj = self.write.getSingleMember("Fem::ConstraintSelfWeight") if obj is not None: for name in bodies: - gravity = self.write.convert(self.write.constsdef["Gravity"], "L/T^2") + gravity = self.write.convert(obj.GravityAcceleration.toStr(), "L/T^2") if self.write.getBodyMaterial(name) is None: raise general_writer.WriteError( "The body {} is not referenced in any material.\n\n".format(name) @@ -380,9 +380,9 @@ class ElasticityWriter: dimension = "M/L^2" density = self.write.convert(densityQuantity, dimension) - force1 = gravity * obj.Gravity_x * density - force2 = gravity * obj.Gravity_y * density - force3 = gravity * obj.Gravity_z * density + force1 = gravity * obj.GravityDirection.x * density + force2 = gravity * obj.GravityDirection.y * density + force3 = gravity * obj.GravityDirection.z * density self.write.bodyForce(name, "Stress Bodyforce 1", force1) self.write.bodyForce(name, "Stress Bodyforce 2", force2) self.write.bodyForce(name, "Stress Bodyforce 3", force3) diff --git a/src/Mod/Fem/femtest/data/calculix/constraint_selfweight_cantilever.inp b/src/Mod/Fem/femtest/data/calculix/constraint_selfweight_cantilever.inp index 4fda9cf903..1721a498ea 100644 --- a/src/Mod/Fem/femtest/data/calculix/constraint_selfweight_cantilever.inp +++ b/src/Mod/Fem/femtest/data/calculix/constraint_selfweight_cantilever.inp @@ -2170,7 +2170,7 @@ ConstraintFixed,3 ** Self weight Constraint ** ConstraintSelfWeight *DLOAD -Eall,GRAV,9806,0,0,-1 +Eall,GRAV,9806.65,0,0,-1 ***********************************************************