@@ -58,6 +58,103 @@ function get_WorldObject3D(assembly::Modia3D.AbstractAssembly)::Object3D
5858end
5959
6060
61+
62+ """
63+ model = Modia3D.Model(assembly::Modia3D.AbstractAssembly;
64+ analysis::ModiaMath.AnalysisType=ModiaMath.DynamicAnalysis)
65+
66+ Generate a `Modia3D.Model` from an `assembly` generated with macro [`Modia3D.@assembly`](@ref)
67+ and the type of `analysis` to be carried out on the `assembly`.
68+ """
69+ struct Model
70+ name:: String
71+ nz:: Int # Number of event indicators
72+ assembly:: Modia3D.AbstractAssembly
73+ var:: ModiaMath.ModelVariables
74+ analysis:: ModiaMath.AnalysisType
75+ x_start:: Vector{Float64}
76+ x_fixed:: Vector{Bool}
77+ x_nominal:: Vector{Float64}
78+ is_constraint:: Vector{Bool}
79+
80+ function Model (assembly:: Modia3D.AbstractAssembly ;
81+ analysis:: ModiaMath.AnalysisType = ModiaMath. DynamicAnalysis)
82+
83+ name = Modia3D. trailingPartOfName ( string ( typeof (assembly) ) )
84+ world = get_WorldObject3D (assembly)
85+ assembly. _internal. referenceObject3D = world
86+ # println("\n... world reference frame: ", ModiaMath.fullName(world))
87+
88+ # Determine Variables in assembly
89+ var = ModiaMath. ModelVariables (assembly, analysis= analysis)
90+ # ModiaMath.print_ModelVariables(var)
91+
92+ # Set initial values for x
93+ x_start = zeros (var. nx)
94+ x_fixed = fill (false ,var. nx)
95+ x_nominal = fill (1.0 ,var. nx)
96+ ModiaMath. copy_start_to_x! (var, x_start, x_fixed, x_nominal)
97+ # println("... x0 = ", x_start)
98+
99+ # Last nfc equations are the constraint equations
100+ is_constraint = fill (false , var. nx)
101+ for i = (var. nx- var. nfc+ 1 ): var. nx
102+ is_constraint[i] = true
103+ end
104+
105+ # Construct Scene(..) object
106+ so = assembly. _internal. sceneOptions
107+ sceneOptions:: SceneOptions = typeof (so) == NOTHING ? SceneOptions () : so
108+ scene = Scene (sceneOptions)
109+ scene. analysis = analysis
110+ assembly. _internal. scene = scene
111+ if ! scene. options. enableContactDetection
112+ scene. collide = false
113+ end
114+
115+ # Build tree for optimized structure or standard structure
116+ # collision handling is only available for optimized structure
117+ nz = 0
118+ if scene. options. useOptimizedStructure
119+ build_superObjs! (scene, world)
120+ if ! scene. options. enableContactDetection
121+ scene. collide = false
122+ end
123+
124+ if scene. collide
125+ initializeContactDetection! (world, scene)
126+ if scene. collide
127+ nz = scene. options. contactDetection. contactPairs. nz
128+ append! (scene. allVisuElements, world. contactVisuObj1)
129+ append! (scene. allVisuElements, world. contactVisuObj2)
130+ append! (scene. allVisuElements, world. supportVisuObj1A)
131+ append! (scene. allVisuElements, world. supportVisuObj1B)
132+ append! (scene. allVisuElements, world. supportVisuObj1C)
133+ append! (scene. allVisuElements, world. supportVisuObj2A)
134+ append! (scene. allVisuElements, world. supportVisuObj2B)
135+ append! (scene. allVisuElements, world. supportVisuObj2C)
136+ end
137+ end
138+ initializeMassComputation! (scene)
139+ else
140+ build_tree! (scene, world)
141+ if scene. options. enableContactDetection
142+ error (" Collision handling is only possible with the optimized structure. Please set useOptimizedStructure = true in Modia3D.SceneOptions." )
143+ end
144+ end
145+
146+ # Initialize connections between signals and frames, joints, ...
147+ build_SignalObject3DConnections! (assembly)
148+ scene. initAnalysis = true
149+
150+ # Generate Model object
151+ new (name, nz, assembly, var, analysis, x_start, x_fixed, x_nominal, is_constraint)
152+ end
153+ end
154+
155+
156+
157+
61158"""
62159 simModel = SimulationModel(assembly::Modia3D.AbstractAssembly;
63160 analysis::ModiaMath.AnalysisType=ModiaMath.DynamicAnalysis,
@@ -73,95 +170,27 @@ engine are defined. These values should be adapted so that assembly-specific, me
73170defaults are provided.
74171"""
75172struct SimulationModel <: ModiaMath.AbstractSimulationModel
76- modelName:: String
77- simulationState:: ModiaMath.SimulationState
78- assembly:: Modia3D.AbstractAssembly
79- var:: ModiaMath.ModelVariables
80- analysis:: ModiaMath.AnalysisType
81-
82- function SimulationModel (assembly:: Modia3D.AbstractAssembly ;
83- analysis:: ModiaMath.AnalysisType = ModiaMath. DynamicAnalysis,
84- startTime = 0.0 ,
85- stopTime = 1.0 ,
86- tolerance = 1e-4 ,
87- interval = (stopTime- startTime)/ 500.0 ,
88- maxStepSize= NaN ,
89- maxNumberOfSteps= missing ,
90- hev = 1e-8 ,
91- scaleConstraintsAtEvents:: Bool = true )
92- modelName = Modia3D. trailingPartOfName ( string ( typeof (assembly) ) )
93- world = get_WorldObject3D (assembly)
94- assembly. _internal. referenceObject3D = world
95- # println("\n... world reference frame: ", ModiaMath.fullName(world))
96-
97- # Determine Variables in assembly
98- var = ModiaMath. ModelVariables (assembly, analysis= analysis)
99- # ModiaMath.print_ModelVariables(var)
100-
101- # Set initial values for x
102- x = zeros (var. nx)
103- x_fixed = fill (false ,var. nx)
104- x_nominal = fill (1.0 ,var. nx)
105- ModiaMath. copy_start_to_x! (var, x, x_fixed, x_nominal)
106- # println("... x0 = ", x)
107-
108- # Last nfc equations are the constraint equations
109- is_constraint = fill (false , var. nx)
110- for i = (var. nx- var. nfc+ 1 ): var. nx
111- is_constraint[i] = true
112- end
113-
114- # Construct Scene(..) object
115- so = assembly. _internal. sceneOptions
116- sceneOptions:: SceneOptions = typeof (so) == NOTHING ? SceneOptions () : so
117- scene = Scene (sceneOptions)
118- scene. analysis = analysis
119- assembly. _internal. scene = scene
120- if ! scene. options. enableContactDetection
121- scene. collide = false
122- end
123-
124- # Build tree for optimized structure or standard structure
125- # collision handling is only available for optimized structure
126- nz = 0
127- if scene. options. useOptimizedStructure
128- build_superObjs! (scene, world)
129- if ! scene. options. enableContactDetection
130- scene. collide = false
131- end
132-
133- if scene. collide
134- initializeContactDetection! (world, scene)
135- if scene. collide
136- nz = scene. options. contactDetection. contactPairs. nz
137- append! (scene. allVisuElements, world. contactVisuObj1)
138- append! (scene. allVisuElements, world. contactVisuObj2)
139- append! (scene. allVisuElements, world. supportVisuObj1A)
140- append! (scene. allVisuElements, world. supportVisuObj1B)
141- append! (scene. allVisuElements, world. supportVisuObj1C)
142- append! (scene. allVisuElements, world. supportVisuObj2A)
143- append! (scene. allVisuElements, world. supportVisuObj2B)
144- append! (scene. allVisuElements, world. supportVisuObj2C)
145- end
146- end
147- initializeMassComputation! (scene)
148- else
149- build_tree! (scene, world)
150- if scene. options. enableContactDetection
151- error (" Collision handling is only possible with the optimized structure. Please set useOptimizedStructure = true in Modia3D.SceneOptions." )
152- end
153- end
154-
155- # Initialize connections between signals and frames, joints, ...
156- build_SignalObject3DConnections! (assembly)
157- scene. initAnalysis = true
158-
159- # Generate simulationState
160- simulationState = ModiaMath. SimulationState (modelName, getModelResidues!, x, ModiaMath. Variables. getVariableName;
161- x_fixed = x_fixed,
162- x_nominal = x_nominal,
163- is_constraint = is_constraint,
164- nz = nz,
173+ modelName:: String
174+ simulationState:: ModiaMath.SimulationState
175+ var:: ModiaMath.ModelVariables
176+ model:: Model
177+
178+ function SimulationModel (assembly:: Modia3D.AbstractAssembly ;
179+ analysis:: ModiaMath.AnalysisType = ModiaMath. DynamicAnalysis,
180+ startTime = 0.0 ,
181+ stopTime = 1.0 ,
182+ tolerance = 1e-4 ,
183+ interval = (stopTime- startTime)/ 500.0 ,
184+ maxStepSize= NaN ,
185+ maxNumberOfSteps= missing ,
186+ hev = 1e-8 ,
187+ scaleConstraintsAtEvents:: Bool = true )
188+ model = Model (assembly; analysis= analysis)
189+ simulationState = ModiaMath. SimulationState (model. name, getSimulationModelResidues!, model. x_start, ModiaMath. Variables. getVariableName;
190+ x_fixed = model. x_fixed,
191+ x_nominal = model. x_nominal,
192+ is_constraint = model. is_constraint,
193+ nz = model. nz,
165194 defaultStartTime = startTime,
166195 defaultStopTime = stopTime,
167196 defaultTolerance = tolerance,
@@ -206,14 +235,14 @@ struct SimulationModel <: ModiaMath.AbstractSimulationModel
206235 end
207236 println("... end allVisuElements")
208237=#
209- new (modelName , simulationState,assembly, var,analysis )
238+ new (model . name , simulationState, model . var, model )
210239 end
211240end
212241
213- ModiaMath. print_ModelVariables (model :: SimulationModel ) = ModiaMath. print_ModelVariables (model. var)
214- print_ModelVariables ( model :: SimulationModel ) = ModiaMath. print_ModelVariables (model. var)
242+ ModiaMath. print_ModelVariables (simModel :: SimulationModel ) = ModiaMath. print_ModelVariables (simModel . model. var)
243+ print_ModelVariables ( simModel :: SimulationModel ) = ModiaMath. print_ModelVariables (simModel . model. var)
215244
216- getResultNames (model :: SimulationModel ) = model. var. result_names
245+ getResultNames (simModel :: SimulationModel ) = simModel . model. var. result_names
217246
218247
219248function initializeFlowVariables (scene:: Scene )
@@ -258,22 +287,24 @@ function computationSignals(scene::Scene, sim::ModiaMath.SimulationState)
258287end
259288
260289# Return a table of actual variable and residue values from nonlinear solver in case of error
261- function ModiaMath. getVariableAndResidueValues (m :: SimulationModel )
262- var = m . var
290+ function ModiaMath. getVariableAndResidueValues (simModel :: SimulationModel )
291+ var = simModel . model . var
263292 v_table = ModiaMath. get_variableValueTable (var)
264- r_table = ModiaMath. get_residueValueTable (var, m . simulationState. derxev)
293+ r_table = ModiaMath. get_residueValueTable (var, simModel . simulationState. derxev)
265294 return (v_table, r_table)
266295end
267296
268297
269298const str_DUMMY = " dummyDistance(nothing,nothing)"
270299
300+ getSimulationModelResidues! (simModel:: SimulationModel , time:: Float64 , _x:: Vector{Float64} , _derx:: Vector{Float64} , _r:: Vector{Float64} , _w:: Vector{Float64} ) =
301+ getModelResidues! (simModel. model, simModel. simulationState, time, _x, _derx, _r, _w)
271302
272- function getModelResidues! (m:: SimulationModel , time:: Float64 , _x:: Vector{Float64} , _derx:: Vector{Float64} , _r:: Vector{Float64} , _w:: Vector{Float64} )
303+ function getModelResidues! (m:: Model , sim:: ModiaMath.SimulationState ,
304+ time:: Float64 , _x:: Vector{Float64} , _derx:: Vector{Float64} , _r:: Vector{Float64} , _w:: Vector{Float64} )
273305 # println("... time = ", time, ", x = ", _x, ", derx = ", _derx)
274- sim = m. simulationState
275- world = m. assembly. _internal. referenceObject3D
276- scene = m. assembly. _internal. scene
306+ world = m. assembly. _internal. referenceObject3D
307+ scene = m. assembly. _internal. scene
277308 if scene. options. useOptimizedStructure
278309 tree = scene. treeAccVelo
279310 else
0 commit comments