Skip to content

Commit 923fe03

Browse files
Introduce basic force element infrastructure
- Add abstract type AbstractForceElement. - Generate list of force element objects during MBS initialization stored in scene. - Call basic force element functions: * initializeForceElement(force) once before simulation. * evaluateForceElement(time, force) in every evaluation of the equations of motion. * terminateForceElement(force) once after simulation.
1 parent 4e62b78 commit 923fe03

File tree

3 files changed

+32
-12
lines changed

3 files changed

+32
-12
lines changed

src/Composition/dynamics.jl

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
function getJointsAndObject3DsWithoutParents!(evaluatedParameters,
2-
object3DWithoutParents::Vector{Object3D},
3-
jointObjects::Vector{Object3D},
4-
path::String)::Nothing
1+
function getJointsAndForceElementsAndObject3DsWithoutParents!(evaluatedParameters,
2+
object3DWithoutParents::Vector{Object3D},
3+
jointObjects::Vector{Object3D},
4+
forceElements::Vector{Modia3D.AbstractForceElement},
5+
path::String)::Nothing
56
for (key,value) in evaluatedParameters # zip(keys(evaluatedParameters), evaluatedParameters)
67
if typeof(value) == Object3D
78
if value.parent === value
@@ -13,8 +14,11 @@ function getJointsAndObject3DsWithoutParents!(evaluatedParameters,
1314
elseif typeof(value) <: Modia3D.AbstractJoint
1415
push!(jointObjects, value.obj2)
1516

17+
elseif typeof(value) <: Modia3D.AbstractForceElement
18+
push!(forceElements, value)
19+
1620
elseif typeof(value) <: OrderedDict
17-
getJointsAndObject3DsWithoutParents!(value, object3DWithoutParents, jointObjects, path)
21+
getJointsAndForceElementsAndObject3DsWithoutParents!(value, object3DWithoutParents, jointObjects, forceElements, path)
1822
end
1923
end
2024
return nothing
@@ -24,21 +28,21 @@ multiBodyName(instantiatedModel, mbsPath) = mbsPath == "" ? instantiatedModel.mo
2428
instantiatedModel.modelName * "." * mbsPath
2529

2630
"""
27-
(world, jointObjects) = checkMultibodySystemAndGetWorldAndJoints(instantiatedModel, id)
31+
(world, jointObjects, forceElements) = checkMultibodySystemAndGetWorldAndJointsAndForceElements(instantiatedModel, id)
2832
2933
Recursively traverse model and perform the following actions:
3034
3135
- Search for an OrderedDict that has `key = :_id, value = id`
3236
(identifies the root of the multibody system).
33-
3437
- Search from the root of the multibody system and perform the following actions:
3538
- Check that from all Object3Ds exactly one of them has no parent.
3639
Return this parent as `world`.
3740
- Check that only `world` has potentially a `feature` entry that
3841
is a SceneOption.
3942
- Return a vector of joint objects as `joints`.
43+
- Return a vector of all force element objects.
4044
"""
41-
function checkMultibodySystemAndGetWorldAndJoints(instantiatedModel::ModiaLang.SimulationModel{FloatType,ParType,EvaluatedParType,TimeType}, id::Int) where {FloatType,ParType,EvaluatedParType,TimeType}
45+
function checkMultibodySystemAndGetWorldAndJointsAndForceElements(instantiatedModel::ModiaLang.SimulationModel{FloatType,ParType,EvaluatedParType,TimeType}, id::Int) where {FloatType,ParType,EvaluatedParType,TimeType}
4246
# Find root mbs of multibody system
4347
(mbsRoot, mbsPath) = ModiaLang.getIdParameter(instantiatedModel.evaluatedParameters, ParType, id)
4448
if isnothing(mbsRoot)
@@ -47,7 +51,8 @@ function checkMultibodySystemAndGetWorldAndJoints(instantiatedModel::ModiaLang.S
4751

4852
object3DWithoutParents = Object3D[]
4953
jointObjects = Object3D[]
50-
getJointsAndObject3DsWithoutParents!(mbsRoot, object3DWithoutParents, jointObjects, mbsPath)
54+
forceElements = Modia3D.AbstractForceElement[]
55+
getJointsAndForceElementsAndObject3DsWithoutParents!(mbsRoot, object3DWithoutParents, jointObjects, forceElements, mbsPath)
5156

5257
if length(object3DWithoutParents) == 0
5358
error("\n", multiBodyName(instantiatedModel, mbsPath), ": There is either no Object3D or all of them have a parent!\n",
@@ -60,7 +65,7 @@ function checkMultibodySystemAndGetWorldAndJoints(instantiatedModel::ModiaLang.S
6065
error("\n", instantiatedModel.modelName, ": The following ", length(object3DWithoutParents), " Object3Ds have no parent\n",
6166
"(note, there must be exactly one Object3D that has no parent):\n", object3DNames, "\n")
6267
end
63-
return (object3DWithoutParents[1], jointObjects)
68+
return (object3DWithoutParents[1], jointObjects, forceElements)
6469
end
6570

6671

@@ -120,7 +125,7 @@ function setModiaJointVariables!(id::Int, _leq_mode, instantiatedModel::ModiaLan
120125
# Instantiate the Modia3D system
121126
#mbsPar = getIdParameter(parameters, id)
122127
#mbsObj = instantiateDependentObjects(instantiatedModel.modelModule, mbsPar)
123-
(world, jointObjects) = checkMultibodySystemAndGetWorldAndJoints(instantiatedModel, id)
128+
(world, jointObjects, forceElements) = checkMultibodySystemAndGetWorldAndJointsAndForceElements(instantiatedModel, id)
124129

125130
# Construct startIndex vector and number of degrees of freedom per joint
126131
nJoints = length(jointObjects)
@@ -138,6 +143,10 @@ function setModiaJointVariables!(id::Int, _leq_mode, instantiatedModel::ModiaLan
138143
scene = initAnalysis2!(world)
139144
residuals = zeros(FloatType,nqdd)
140145
cache_h = zeros(FloatType,nqdd)
146+
scene.forceElements = forceElements
147+
for force in forceElements
148+
initializeForceElement(force)
149+
end
141150
if scene.options.enableContactDetection
142151
nz = 2
143152
zStartIndex = ModiaLang.addZeroCrossings(instantiatedModel, nz)
@@ -256,11 +265,15 @@ For Modia3D:
256265

257266
function multibodyResiduals3!(sim, scene, world, time, storeResults, isTerminal, leq_mode)
258267
tree = scene.treeForComputation
268+
forceElements = scene.forceElements
259269
visualize = scene.visualize # && sim.model.visualiz
260270
exportAnimation = scene.exportAnimation
261271

262272
if isTerminal #if Modia.isTerminalOfAllSegments(sim)
263273
TimerOutputs.@timeit sim.timer "Modia3D_4 isTerminal" begin
274+
for force in forceElements
275+
terminateForceElement(force)
276+
end
264277
if exportAnimation
265278
Modia3D.exportAnimation(scene)
266279
end
@@ -306,6 +319,11 @@ function multibodyResiduals3!(sim, scene, world, time, storeResults, isTerminal,
306319
end
307320
end # end forward recursion
308321

322+
# Evaluate force elements
323+
for force in forceElements
324+
evaluateForceElement(time, force)
325+
end
326+
309327
# Compute contact forces/torques
310328
if scene.collide
311329
computeContactForcesAndTorques(sim, scene, world, time, nothing)

src/Composition/scene.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ mutable struct Scene <: Modia3D.AbstractScene
407407
allowedToMove::Vector{Union{Bool,Nothing}}
408408
AABB::Vector{Vector{Basics.BoundingBox}} # Bounding boxes of elements that can collide
409409
zStartIndex::Int # start index of collision zero crossing functions
410-
#forceElements::Vector{Int64}
410+
forceElements::Vector{Modia3D.AbstractForceElement}
411411
exportAnimation::Bool # animation file export is enabled
412412
animation::Vector{animationStep} # animation data of visible Object3Ds
413413
outputCounter::Int64 # animation/visualization output step counter
@@ -497,6 +497,7 @@ mutable struct Scene <: Modia3D.AbstractScene
497497
Vector{Union{Bool}}[],
498498
Vector{Vector{Basics.BoundingBox}}[],
499499
1,
500+
Vector{Modia3D.AbstractForceElement}[],
500501
exportAnimation,
501502
Vector{animationStep}[],
502503
0,

src/Modia3D.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ abstract type AbstractContactPairMaterial end # Constants needed to compute the
3030
abstract type AbstractObject3D end
3131
abstract type AbstractTwoObject3DObject <: AbstractObject3D end # Object related to two Object3Ds
3232
abstract type AbstractJoint <: AbstractTwoObject3DObject end # Constraint between two Object3Ds
33+
abstract type AbstractForceElement <: AbstractObject3D end
3334

3435
abstract type AbstractScene end
3536

0 commit comments

Comments
 (0)