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
2933Recursively 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 )
6469end
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)
@@ -134,10 +139,16 @@ function setModiaJointVariables!(id::Int, _leq_mode, instantiatedModel::ModiaLan
134139 end
135140 nqdd = j- 1
136141
142+ # Initialize force elements
143+ for force in forceElements
144+ initializeForceElement (force)
145+ end
146+
137147 # Construct MultibodyData
138148 scene = initAnalysis2! (world)
139149 residuals = zeros (FloatType,nqdd)
140150 cache_h = zeros (FloatType,nqdd)
151+ scene. forceElements = forceElements
141152 if scene. options. enableContactDetection
142153 nz = 2
143154 zStartIndex = ModiaLang. addZeroCrossings (instantiatedModel, nz)
@@ -256,11 +267,15 @@ For Modia3D:
256267
257268function multibodyResiduals3! (sim, scene, world, time, storeResults, isTerminal, leq_mode)
258269 tree = scene. treeForComputation
270+ forceElements = scene. forceElements
259271 visualize = scene. visualize # && sim.model.visualiz
260272 exportAnimation = scene. exportAnimation
261273
262274 if isTerminal # if Modia.isTerminalOfAllSegments(sim)
263275 TimerOutputs. @timeit sim. timer " Modia3D_4 isTerminal" begin
276+ for force in forceElements
277+ terminateForceElement (force)
278+ end
264279 if exportAnimation
265280 Modia3D. exportAnimation (scene)
266281 end
@@ -306,6 +321,11 @@ function multibodyResiduals3!(sim, scene, world, time, storeResults, isTerminal,
306321 end
307322 end # end forward recursion
308323
324+ # Evaluate force elements
325+ for force in forceElements
326+ evaluateForceElement (force)
327+ end
328+
309329 # Compute contact forces/torques
310330 if scene. collide
311331 computeContactForcesAndTorques (sim, scene, world, time, nothing )
0 commit comments