From 15f82dd20cae2b944b42eeace00fe8dd58ef11e1 Mon Sep 17 00:00:00 2001 From: Gerhard Hippmann Date: Thu, 11 Jan 2024 17:38:07 +0100 Subject: [PATCH 1/2] Speed up Bushing - Avoid function code generation for linear stiffness and damping in Bushing initialisation - Make SpringDamperPtP implementation consistent --- docs/src/index.md | 1 + src/Composition/ForceElements/Bushing.jl | 20 +++++++++---------- .../ForceElements/SpringDamperPtP.jl | 18 ++++++++--------- 3 files changed, 20 insertions(+), 19 deletions(-) diff --git a/docs/src/index.md b/docs/src/index.md index 5e50256..d461513 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -97,6 +97,7 @@ julia -JModia3D_sysimage.so (otherwise) ### Forthcoming version - Enable kinematic chain flipping for structure variable systems +- Avoid function code generation for linear stiffness and damping in SpringDamperPtP and Bushing ### Version 0.12.2 diff --git a/src/Composition/ForceElements/Bushing.jl b/src/Composition/ForceElements/Bushing.jl index 7645568..278c06f 100644 --- a/src/Composition/ForceElements/Bushing.jl +++ b/src/Composition/ForceElements/Bushing.jl @@ -123,29 +123,29 @@ mutable struct Bushing{F <: Modia3D.VarFloatType} <: Modia3D.AbstractForceElemen springForceFunction[dir] = springForceLaw[dir] else stiffness = Modia3D.convertAndStripUnit(F, u"N/m", springForceLaw[dir]) - fsymb = Symbol(path, "_", "fc", dir) - springForceFunction[dir] = eval(:($fsymb(pos) = $stiffness * pos)) + stiffnessFunction(pos::F) = stiffness * pos + springForceFunction[dir] = stiffnessFunction end if (isa(damperForceLaw[dir], Function)) damperForceFunction[dir] = damperForceLaw[dir] else damping = Modia3D.convertAndStripUnit(F, u"N*s/m", damperForceLaw[dir]) - fsymb = Symbol(path, "_", "fd", dir) - damperForceFunction[dir] = eval(:($fsymb(vel) = $damping * vel)) + dampingFunction(vel::F) = damping * vel + damperForceFunction[dir] = dampingFunction end if (isa(rotSpringForceLaw[dir], Function)) rotSpringForceFunction[dir] = rotSpringForceLaw[dir] else - stiffness = Modia3D.convertAndStripUnit(F, u"N*m/rad", rotSpringForceLaw[dir]) - fsymb = Symbol(path, "_", "mc", dir) - rotSpringForceFunction[dir] = eval(:($fsymb(ang) = $stiffness * ang)) + rotStiffness = Modia3D.convertAndStripUnit(F, u"N*m/rad", rotSpringForceLaw[dir]) + rotStiffnessFunction(ang::F) = rotStiffness * ang + rotSpringForceFunction[dir] = rotStiffnessFunction end if (isa(rotDamperForceLaw[dir], Function)) rotDamperForceFunction[dir] = rotDamperForceLaw[dir] else - damping = Modia3D.convertAndStripUnit(F, u"N*m*s/rad", rotDamperForceLaw[dir]) - fsymb = Symbol(path, "_", "md", dir) - rotDamperForceFunction[dir] = eval(:($fsymb(angd) = $damping * angd)) + rotDamping = Modia3D.convertAndStripUnit(F, u"N*m*s/rad", rotDamperForceLaw[dir]) + rotDampingFunction(om::F) = rotDamping * om + rotDamperForceFunction[dir] = rotDampingFunction end end diff --git a/src/Composition/ForceElements/SpringDamperPtP.jl b/src/Composition/ForceElements/SpringDamperPtP.jl index 6ebf6e5..97aba03 100644 --- a/src/Composition/ForceElements/SpringDamperPtP.jl +++ b/src/Composition/ForceElements/SpringDamperPtP.jl @@ -67,21 +67,21 @@ mutable struct SpringDamperPtP{F <: Modia3D.VarFloatType} <: Modia3D.AbstractFor nomLength = Modia3D.convertAndStripUnit(F, u"m", nominalLength) nomForce = Modia3D.convertAndStripUnit(F, u"N", nominalForce) - if (!isa(springForceLaw, Function)) - stiffness = Modia3D.convertAndStripUnit(F, u"N/m", springForceLaw) - fkt1(_x) = stiffness*_x + if (isa(springForceLaw, Function)) + springForceFunction = springForceLaw else - fkt1 = springForceLaw + stiffness = Modia3D.convertAndStripUnit(F, u"N/m", springForceLaw) + springForceFunction(pos::F) = stiffness * pos end - if (!isa(damperForceLaw, Function)) - damping = Modia3D.convertAndStripUnit(F, u"N*s/m", damperForceLaw) - fkt2(_x) = damping*_x + if (isa(damperForceLaw, Function)) + damperForceFunction = damperForceLaw else - fkt2 = damperForceLaw + damping = Modia3D.convertAndStripUnit(F, u"N*s/m", damperForceLaw) + damperForceFunction(vel::F) = damping * vel end - return new(path, obj1, obj2, nomLength, nomForce, fkt1, fkt2) + return new(path, obj1, obj2, nomLength, nomForce, springForceFunction, damperForceFunction) end end SpringDamperPtP(; kwargs...) = SpringDamperPtP{Float64}(; kwargs...) From 4cdc5ea0ae03319e8ae9b60d1c874c4ea1ab0702 Mon Sep 17 00:00:00 2001 From: Gerhard Hippmann Date: Mon, 15 Jan 2024 09:35:12 +0100 Subject: [PATCH 2/2] Explicitly define float type of local variables in SpringDamper and Bushing --- src/Composition/ForceElements/Bushing.jl | 8 ++++---- src/Composition/ForceElements/SpringDamperPtP.jl | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Composition/ForceElements/Bushing.jl b/src/Composition/ForceElements/Bushing.jl index 278c06f..e93146c 100644 --- a/src/Composition/ForceElements/Bushing.jl +++ b/src/Composition/ForceElements/Bushing.jl @@ -122,28 +122,28 @@ mutable struct Bushing{F <: Modia3D.VarFloatType} <: Modia3D.AbstractForceElemen if (isa(springForceLaw[dir], Function)) springForceFunction[dir] = springForceLaw[dir] else - stiffness = Modia3D.convertAndStripUnit(F, u"N/m", springForceLaw[dir]) + stiffness::F = Modia3D.convertAndStripUnit(F, u"N/m", springForceLaw[dir]) stiffnessFunction(pos::F) = stiffness * pos springForceFunction[dir] = stiffnessFunction end if (isa(damperForceLaw[dir], Function)) damperForceFunction[dir] = damperForceLaw[dir] else - damping = Modia3D.convertAndStripUnit(F, u"N*s/m", damperForceLaw[dir]) + damping::F = Modia3D.convertAndStripUnit(F, u"N*s/m", damperForceLaw[dir]) dampingFunction(vel::F) = damping * vel damperForceFunction[dir] = dampingFunction end if (isa(rotSpringForceLaw[dir], Function)) rotSpringForceFunction[dir] = rotSpringForceLaw[dir] else - rotStiffness = Modia3D.convertAndStripUnit(F, u"N*m/rad", rotSpringForceLaw[dir]) + rotStiffness::F = Modia3D.convertAndStripUnit(F, u"N*m/rad", rotSpringForceLaw[dir]) rotStiffnessFunction(ang::F) = rotStiffness * ang rotSpringForceFunction[dir] = rotStiffnessFunction end if (isa(rotDamperForceLaw[dir], Function)) rotDamperForceFunction[dir] = rotDamperForceLaw[dir] else - rotDamping = Modia3D.convertAndStripUnit(F, u"N*m*s/rad", rotDamperForceLaw[dir]) + rotDamping::F = Modia3D.convertAndStripUnit(F, u"N*m*s/rad", rotDamperForceLaw[dir]) rotDampingFunction(om::F) = rotDamping * om rotDamperForceFunction[dir] = rotDampingFunction end diff --git a/src/Composition/ForceElements/SpringDamperPtP.jl b/src/Composition/ForceElements/SpringDamperPtP.jl index 97aba03..1ca12ea 100644 --- a/src/Composition/ForceElements/SpringDamperPtP.jl +++ b/src/Composition/ForceElements/SpringDamperPtP.jl @@ -70,14 +70,14 @@ mutable struct SpringDamperPtP{F <: Modia3D.VarFloatType} <: Modia3D.AbstractFor if (isa(springForceLaw, Function)) springForceFunction = springForceLaw else - stiffness = Modia3D.convertAndStripUnit(F, u"N/m", springForceLaw) + stiffness::F = Modia3D.convertAndStripUnit(F, u"N/m", springForceLaw) springForceFunction(pos::F) = stiffness * pos end if (isa(damperForceLaw, Function)) damperForceFunction = damperForceLaw else - damping = Modia3D.convertAndStripUnit(F, u"N*s/m", damperForceLaw) + damping::F = Modia3D.convertAndStripUnit(F, u"N*s/m", damperForceLaw) damperForceFunction(vel::F) = damping * vel end