Skip to content

Commit 5ef8a8d

Browse files
Enable force elements for all Object3Ds
- Enable velocity and force&torque computation for all Object3Ds referenced by force elements. - Update test BoxSpringDamperPtP. - Remove unused argument time from evaluateForceElement. - Fix some error messages, comments and indentations.
1 parent 3a9ff08 commit 5ef8a8d

File tree

8 files changed

+47
-39
lines changed

8 files changed

+47
-39
lines changed

src/Composition/ForceElements/Bushing.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ end
3030

3131

3232
function initializeForceElement(force::Bushing)
33+
force.obj1.hasForceElement = true
34+
force.obj2.hasForceElement = true
3335
return nothing
3436
end
3537

36-
function evaluateForceElement(time, force::Bushing)
38+
function evaluateForceElement(force::Bushing)
3739
r12 = measFramePosition(force.obj2; frameOrig=force.obj1, frameCoord=force.obj1)
3840
v12 = measFrameTransVelocity(force.obj2; frameOrig=force.obj1, frameCoord=force.obj1, frameObsrv=force.obj1)
3941
f12 = force.stiffness .* r12 + force.damping .* v12

src/Composition/ForceElements/SpringDamperPtP.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@ end
2929

3030

3131
function initializeForceElement(force::SpringDamperPtP)
32+
force.obj1.hasForceElement = true
33+
force.obj2.hasForceElement = true
3234
return nothing
3335
end
3436

35-
function evaluateForceElement(time, force::SpringDamperPtP)
37+
function evaluateForceElement(force::SpringDamperPtP)
3638
(pos, norm) = measFrameDistance(force.obj2; frameOrig=force.obj1)
3739
vel = measFrameDistVelocity(force.obj2; frameOrig=force.obj1)
3840
frc = force.stiffness * pos + force.damping * vel

src/Composition/assignObjects.jl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,13 @@ end
7979

8080

8181
function assignAccVelo(tree::Vector{Object3D}, obj::Object3D)
82-
# compute velocity of this object
83-
if canCollide(obj) && !hasChildJoint(obj)
84-
push!(tree, obj)
85-
end
86-
# compute acceleration of this object
8782
if hasChildJoint(obj) #|| hasCutJoint(obj)
83+
# compute acceleration of this object
8884
obj.computeAcceleration = true
8985
push!(tree, obj)
86+
elseif hasForceElement(obj) || canCollide(obj)
87+
# compute velocity of this object
88+
push!(tree, obj)
9089
end
9190
end
9291

src/Composition/dynamics.jl

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
function getJointsAndForceElementsAndObject3DsWithoutParents!(evaluatedParameters,
2-
object3DWithoutParents::Vector{Object3D},
3-
jointObjects::Vector{Object3D},
4-
forceElements::Vector{Modia3D.AbstractForceElement},
5-
path::String)::Nothing
2+
object3DWithoutParents::Vector{Object3D},
3+
jointObjects::Vector{Object3D},
4+
forceElements::Vector{Modia3D.AbstractForceElement},
5+
path::String)::Nothing
66
for (key,value) in evaluatedParameters # zip(keys(evaluatedParameters), evaluatedParameters)
77
if typeof(value) == Object3D
88
if value.parent === value
@@ -139,14 +139,16 @@ function setModiaJointVariables!(id::Int, _leq_mode, instantiatedModel::ModiaLan
139139
end
140140
nqdd = j-1
141141

142+
# Initialize force elements
143+
for force in forceElements
144+
initializeForceElement(force)
145+
end
146+
142147
# Construct MultibodyData
143148
scene = initAnalysis2!(world)
144149
residuals = zeros(FloatType,nqdd)
145150
cache_h = zeros(FloatType,nqdd)
146151
scene.forceElements = forceElements
147-
for force in forceElements
148-
initializeForceElement(force)
149-
end
150152
if scene.options.enableContactDetection
151153
nz = 2
152154
zStartIndex = ModiaLang.addZeroCrossings(instantiatedModel, nz)
@@ -321,7 +323,7 @@ function multibodyResiduals3!(sim, scene, world, time, storeResults, isTerminal,
321323

322324
# Evaluate force elements
323325
for force in forceElements
324-
evaluateForceElement(time, force)
326+
evaluateForceElement(force)
325327
end
326328

327329
# Compute contact forces/torques

src/Composition/handler.jl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ end
128128

129129
# the indices of super objects, which can't collide, are stored in a list
130130
function fillStackOrBuffer!(scene::Scene, superObj::SuperObjsRow, obj::Object3D, rootSuperObj::Object3D)::Nothing
131-
n_children = length(obj.children)
131+
n_children = length(obj.children)
132132
help = fill(false, n_children)
133133

134134
for i = 1:n_children
@@ -142,16 +142,16 @@ function fillStackOrBuffer!(scene::Scene, superObj::SuperObjsRow, obj::Object3D,
142142
push!(superObj.noCPair, length(scene.buffer))
143143
end
144144
else
145-
if isMovable(child)
146-
push!(scene.buffer, child)
147-
child.isRootObj = true
148-
child.computeAcceleration = true
149-
else
150-
push!(scene.stack, child)
151-
assignAccVelo(scene.treeAccVelo, child)
152-
if !(obj == rootSuperObj)
153-
changeParentToRootObj(rootSuperObj, child)
154-
help[i] = true
145+
if isMovable(child)
146+
push!(scene.buffer, child)
147+
child.isRootObj = true
148+
child.computeAcceleration = true
149+
else
150+
push!(scene.stack, child)
151+
assignAccVelo(scene.treeAccVelo, child)
152+
if !(obj == rootSuperObj)
153+
changeParentToRootObj(rootSuperObj, child)
154+
help[i] = true
155155
end; end; end; end; end
156156

157157
if !isempty(obj.children)

src/Composition/joints/joints.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ function computeKinematics!(scene::Scene, tree::Vector{Object3D}, time)::Nothing
341341
obj.z = parent.R_abs*parent.z + freeMotion.z
342342

343343
else
344-
error("Bug in Modia3D/src/Composition/specifics/specifics.jl (computeKinematics!): jointKind = $jointKind is not known")
344+
error("Bug in Modia3D/src/Composition/joints/joints.jl (computeKinematics!): jointKind = $jointKind is not known.")
345345
end
346346
end
347347
return nothing
@@ -396,7 +396,7 @@ function computeKinematics_for_leq_mode_pos!(scene::Scene, tree::Vector{Object3D
396396
obj.z = parent.R_abs*parent.z + freeMotion.z
397397

398398
else
399-
error("Bug in Modia3D/src/Composition/specifics/specifics.jl (computeKinematics_for_leq_mode_pos!): jointKind = $jointKind is not known")
399+
error("Bug in Modia3D/src/Composition/joints/joints.jl (computeKinematics_for_leq_mode_pos!): jointKind = $jointKind is not known.")
400400
end
401401
end
402402
return nothing
@@ -448,7 +448,7 @@ function computeForcesTorquesAndResiduals!(scene::Scene, tree::Vector{Object3D},
448448
freeMotion.residue_t = obj.t
449449

450450
else
451-
error("Bug in Modia3D/src/Composition/specifics/specifics.jl (computeForcesTorquesAndResiduals!): jointKind = $jointKind is not known")
451+
error("Bug in Modia3D/src/Composition/joints/joints.jl (computeForcesTorquesAndResiduals!): jointKind = $jointKind is not known.")
452452
end
453453
end
454454
return nothing
@@ -492,7 +492,7 @@ function setJointVariables_q_qd_f!(scene::Scene, objects::Vector{Object3D}, star
492492
freeMotion.isrot123 = args_i[5]
493493

494494
else
495-
error("Bug in Modia3D/src/Composition/specifics/specifics.jl (setJointVariables!): jointKind = $jointKind is not known")
495+
error("Bug in Modia3D/src/Composition/joints/joints.jl (setJointVariables_q_qd_f!): jointKind = $jointKind is not known.")
496496
end
497497
end
498498
return nothing
@@ -531,7 +531,7 @@ function setJointVariables_qdd!(scene::Scene, objects::Vector{Object3D}, startIn
531531
freeMotion.z = SVector{3,Float64}(qdd2[beg+3], qdd2[beg+4], qdd2[beg+5])
532532

533533
else
534-
error("Bug in Modia3D/src/Composition/specifics/specifics.jl (setJointVariables!): jointKind = $jointKind is not known")
534+
error("Bug in Modia3D/src/Composition/joints/joints.jl (setJointVariables_qdd!): jointKind = $jointKind is not known.")
535535
end
536536
end
537537
return nothing
@@ -571,7 +571,7 @@ function getJointResiduals_for_leq_mode_0!(scene::Scene, objects::Vector{Object3
571571
residuals[beg+3:beg+5] = freeMotion.residue_t
572572

573573
else
574-
error("Bug in Modia3D/src/Composition/specifics/specifics.jl (getJointResiduals_for_leq_mode_0!): jointKind = $jointKind is not known")
574+
error("Bug in Modia3D/src/Composition/joints/joints.jl (getJointResiduals_for_leq_mode_0!): jointKind = $jointKind is not known.")
575575
end
576576
end
577577
return nothing
@@ -604,7 +604,7 @@ function getJointResiduals_for_leq_mode_pos!(scene::Scene, objects::Vector{Objec
604604
residuals[beg+3:beg+5] = freeMotion.residue_t + cache_h[beg+3:beg+5]
605605

606606
else
607-
error("Bug in Modia3D/src/Composition/specifics/specifics.jl (getJointResiduals_for_leq_mode_0!): jointKind = $jointKind is not known")
607+
error("Bug in Modia3D/src/Composition/joints/joints.jl (getJointResiduals_for_leq_mode_pos!): jointKind = $jointKind is not known.")
608608
end
609609
end
610610
return nothing

src/Composition/object3D.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ mutable struct Object3D <: Modia3D.AbstractObject3D
177177
end
178178

179179
if !isnothing(parent)
180-
# with parent -> call constructor 3 or 5
180+
# with parent -> call constructor 3
181181
if !isnothing(rotation) && (typeof(rotation) != Frames.RotationMatrix)
182182
rotation = Frames.rot123(rotation[1], rotation[2], rotation[3])
183183
end

test/Basic/BoxSpringDamperPtP.jl

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,21 @@ SpringDamper = Model(
1717
box = Object3D(feature=Solid(shape=Box(lengthX=:Length, lengthY=:Length, lengthZ=:Length),
1818
massProperties=MassProperties(; mass=1.0, Ixx=0.1, Iyy=0.1, Izz=0.1),
1919
visualMaterial=:(visualMaterial))),
20-
joint = FreeMotion(obj1=:world, obj2=:box, r=Var(init=[0.1, 0.2, 0.3])),
21-
force = SpringDamperPtP(obj1=:world, obj2=:box, stiffness=:Stiffness, damping=:Damping)
20+
boxCornerFrame = Object3D(parent=:box,
21+
feature=Visual(shape=CoordinateSystem(length=:(Length/2))),
22+
translation=:[Length/2, Length/2, Length/2]),
23+
joint = FreeMotion(obj1=:world, obj2=:box),
24+
force = SpringDamperPtP(obj1=:world, obj2=:boxCornerFrame, stiffness=:Stiffness, damping=:Damping)
2225
)
2326

2427
springDamper = @instantiateModel(buildModia3D(SpringDamper), aliasReduction=false, unitless=true)
2528

26-
stopTime = 6.0
29+
stopTime = 5.0
2730
dtmax = 0.1
28-
requiredFinalStates = [-0.026549009773552276, -0.05309801954749593, -0.10117097852565102, 0.0144068779028959, 0.028813755813570235, 0.02595802582858583, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
31+
requiredFinalStates = [-0.031789887795694936, -0.03179006302845493, -0.13960438757282567, -0.22639388796813553, -0.226393551737028, -0.0958553592848107, -0.04370189950475126, 0.0436602955184432, 0.0009569259867732385, 0.8435396531516037, -0.8435431841758767, 3.531021863184031e-6]
2932
simulate!(springDamper, stopTime=stopTime, dtmax=dtmax, log=true, requiredFinalStates=requiredFinalStates)
3033

3134
@usingModiaPlot
32-
plot(springDamper, ["joint.r", "joint.v"], figure=1)
35+
plot(springDamper, ["joint.r", "joint.v", "joint.rot", "joint.w"], figure=1)
3336

3437
end

0 commit comments

Comments
 (0)