Setting and Recording Module VariablesΒΆ
Sometimes it is convenient to record a Basilisk module variable, not just the input or output message. This can be done via a python command as shown below. However, note that such module variable recording will slow down the simulation as this is done in the python layer.
The simulation setup is shown in the figure below. Both a C and C++ module are created and added to the single task. However, no messages are connected here. Rather, this sample code illustrates how to record module internal variables. The variables are either public C++ class variables, or they are variables with the C module configuration structure. Both Module: cModuleTemplate and Module: cppModuleTemplate have the exact same public variables for easy comparison.
The sample code is shown below. The C and C++ modules are set up as before. The variable someVariable
of module someModule
is set in python using someModule.someVariable = ...
.
1
2from Basilisk.moduleTemplates import cModuleTemplate
3from Basilisk.moduleTemplates import cppModuleTemplate
4from Basilisk.utilities import SimulationBaseClass
5from Basilisk.utilities import macros
6
7
8def run():
9 """
10 Illustration of setting and recording module variables
11 """
12
13 # Create a sim module as an empty container
14 scSim = SimulationBaseClass.SimBaseClass()
15
16 # create the simulation process
17 dynProcess = scSim.CreateNewProcess("dynamicsProcess")
18
19 # create the dynamics task and specify the integration update time
20 dynProcess.addTask(scSim.CreateNewTask("dynamicsTask", macros.sec2nano(1.)))
21
22 # create modules
23 mod1 = cModuleTemplate.cModuleTemplateConfig()
24 mod1Wrap = scSim.setModelDataWrap(mod1)
25 mod1Wrap.ModelTag = "cModule1"
26 scSim.AddModelToTask("dynamicsTask", mod1Wrap, mod1)
27
28 mod2 = cppModuleTemplate.CppModuleTemplate()
29 mod2.ModelTag = "cppModule2"
30 scSim.AddModelToTask("dynamicsTask", mod2)
31
32 # set module variables
33 mod1.dummy = 1
34 mod1.dumVector = [1., 2., 3.]
35 mod2.dummy = 1
36 mod2.dumVector = [1., 2., 3.]
37
38 # request these module variables to be recorded
39 scSim.AddVariableForLogging(mod1Wrap.ModelTag + ".dummy", macros.sec2nano(1.))
40 scSim.AddVariableForLogging(mod1Wrap.ModelTag + ".dumVector", macros.sec2nano(1.), 0, 2)
41 scSim.AddVariableForLogging(mod2.ModelTag + ".dummy", macros.sec2nano(1.))
42 scSim.AddVariableForLogging(mod2.ModelTag + ".dumVector", macros.sec2nano(1.), 0, 2)
43
44 # initialize Simulation:
45 scSim.InitializeSimulation()
46
47 # configure a simulation stop time and execute the simulation run
48 scSim.ConfigureStopTime(macros.sec2nano(1.0))
49 scSim.ExecuteSimulation()
50
51 print("mod1.dummy:")
52 print(scSim.GetLogVariableData(mod1Wrap.ModelTag + ".dummy"))
53 print("mod1.dumVector:")
54 print(scSim.GetLogVariableData(mod1Wrap.ModelTag + ".dumVector"))
55 print("mod2.dummy:")
56 print(scSim.GetLogVariableData(mod2.ModelTag + ".dummy"))
57 print("mod2.dumVector:")
58 print(scSim.GetLogVariableData(mod2.ModelTag + ".dumVector"))
59
60 return
61
62
63if __name__ == "__main__":
64 run()
The SimulationBaseClass method to record a module variable is:
scSim.AddVariableForLogging( variableString, recordingTime, indexStart==0, indexStop==0)
Here variableString
must be composed of the module tag string, a period and the variable name. The examples above illustrate how to apply this method for a C or C++ module variable. Remember that with a C module the ModelTag
variable is defined with the module wrapper, while the C++ module has this ModelTag
variable defined within its class.
The recordingTime
variable is the minimum time that must pass, in nano-seconds again, before the module variable is recorded.
The optional integer arguments indexStart
and indexStop
are defaulted to zero, resulting in a single value being recorded. As this example is also recording a 3-dimensional array dumVector
, it is recorded by setting the start and end index to 0 and 2 respectively.
After executing the script, the recorded variables are retrieved in general using the SimulationBaseClass method:
scSim.GetLogVariableData(variableString)
Here variableString
is again composed of the ModelTag
and variable name as before. Note that the returned array has a first column that represents the time where the variable is recorded in nano-seconds. Executing the script you should thus see the following output:
source/codeSamples % python bsk-6.py
BSK_INFORMATION: Variable dummy set to 0.000000 in reset.
BSK_INFORMATION: Variable dummy set to 0.000000 in reset.
BSK_INFORMATION: C Module ID 1 ran Update at 0.000000s
BSK_INFORMATION: C++ Module ID 2 ran Update at 0.000000s
BSK_INFORMATION: C Module ID 1 ran Update at 1.000000s
BSK_INFORMATION: C++ Module ID 2 ran Update at 1.000000s
mod1.dummy:
[[0.e+00 1.e+00]
[1.e+09 2.e+00]]
mod1.dumVector:
[[0.e+00 1.e+00 2.e+00 3.e+00]
[1.e+09 1.e+00 2.e+00 3.e+00]]
mod2.dummy:
[[0.e+00 1.e+00]
[1.e+09 2.e+00]]
mod2.dumVector:
[[0.e+00 1.e+00 2.e+00 3.e+00]
[1.e+09 1.e+00 2.e+00 3.e+00]]
Note that both the C and C++ module variables are correctly being recorded.