updating default coordinate values

Provide easy-to-use, extensible software for modeling, simulating, controlling, and analyzing the neuromusculoskeletal system.
POST REPLY
User avatar
Riza Bayoglu
Posts: 14
Joined: Wed Aug 02, 2023 8:54 am

updating default coordinate values

Post by Riza Bayoglu » Wed Mar 27, 2024 4:05 pm

Hi,

I like to change the coordinate values in my model using Python scripting. I am analyzing only one state. Reading through the API documents and the forum questions, I used setValue() property of the coordinates and assemble() afterwards. However, my code below does not seem to update the default coordinate values.

Code: Select all

model     = osim.Model(modelpath)
actuators = osim.ForceSet('mymodel.xml')
forceset  = model.updForceSet()

for i in range(0, actuators.getSize() -1 ):
            
    forceset.cloneAndAppend(actuators.get(i))
    
analyze_tool = osim.AnalyzeTool('staticOpt_Setup.xml')
analyze_tool.setModel(model)
analyze_tool.setStartTime(0)
analyze_tool.setFinalTime(0)  

state      = model.initSystem()
coord_set  = model.getCoordinateSet()
coords     = ['pelvis_tilt',	'Abs_FE',	'Abs_LB',	'Abs_AR',	'L5_S1_FE',	'L5_S1_LB',	'L5_S1_AR',	'L4_L5_FE',	'L4_L5_LB',	'L4_L5_AR',	'L3_L4_FE',	'L3_L4_LB',	'L3_L4_AR',	'L2_L3_FE', 'L2_L3_LB',	'L2_L3_AR',	'L1_L2_FE',	'L1_L2_LB',	'L1_L2_AR',	'T12_L1_FE',	'T12_L1_LB',	'T12_L1_AR',	'T11_T12_FE',	'T11_T12_LB', 'T11_T12_AR',	'T10_T11_FE', 'T10_T11_LB',	'T10_T11_AR', 'T9_T10_FE',	'T9_T10_LB', 'T9_T10_AR',	'T8_T9_FE',	'T8_T9_LB',	'T8_T9_AR',	'T7_T8_FE',	'T7_T8_LB',	'T7_T8_AR',	'T6_T7_FE',	'T6_T7_LB',	'T6_T7_AR',	'T5_T6_FE', 'T5_T6_LB',	'T5_T6_AR',	'T4_T5_FE',	'T4_T5_LB',	'T4_T5_AR',	'T3_T4_FE',	'T3_T4_LB',	'T3_T4_AR',	'T2_T3_FE',	'T2_T3_LB',	'T2_T3_AR',	'T1_T2_FE',	'T1_T2_LB',	'T1_T2_AR',	'T1_head_neck_FE',	'T1_head_neck_LB',	'T1_head_neck_AR',	'T12_r12R_X',	'T12_r12R_Y',	'T12_r12R_Z',	'T11_r11R_X',	'T11_r11R_Y',	'T11_r11R_Z',	'T10_r10R_X',	'T10_r10R_Y',	'T10_r10R_Z',	'T9_r9R_X',	'T9_r9R_Y',	'T9_r9R_Z',	'T8_r8R_X',	'T8_r8R_Y',	'T8_r8R_Z',	'T7_r7R_X',	'T7_r7R_Y',	'T7_r7R_Z',	'T6_r6R_X',	'T6_r6R_Y',	'T6_r6R_Z',	'T5_r5R_X',	'T5_r5R_Y',	'T5_r5R_Z',	'T4_r4R_X',	'T4_r4R_Y',	'T4_r4R_Z',	'T3_r3R_X',	'T3_r3R_Y',	'T3_r3R_Z',	'T2_r2R_X',	'T2_r2R_Y',	'T2_r2R_Z',	'T1_r1R_X',	'T1_r1R_Y',	'T1_r1R_Z',	'T12_r12L_X',	'T12_r12L_Y',	'T12_r12L_Z',	'T11_r11L_X',	'T11_r11L_Y',	'T11_r11L_Z',	'T10_r10L_X',	'T10_r10L_Y',	'T10_r10L_Z',	'T9_r9L_X',	'T9_r9L_Y',	'T9_r9L_Z',	'T8_r8L_X',	'T8_r8L_Y',	'T8_r8L_Z',	'T7_r7L_X',	'T7_r7L_Y',	'T7_r7L_Z',	'T6_r6L_X',	'T6_r6L_Y',	'T6_r6L_Z',	'T5_r5L_X',	'T5_r5L_Y',	'T5_r5L_Z',	'T4_r4L_X',	'T4_r4L_Y',	'T4_r4L_Z',	'T3_r3L_X',	'T3_r3L_Y',	'T3_r3L_Z',	'T2_r2L_X',	'T2_r2L_Y',	'T2_r2L_Z',	'T1_r1L_X',	'T1_r1L_Y',	'T1_r1L_Z',	'SternumX',	'SternumY',	'SternumZ',	'SternumRotX',	'SternumRotY',	'SternumRotZ',	'shoulder_elv_r',	'shoulder_rot_r',	'elv_angle_r',	'elbow_flexion_r',	'pro_sup_r',	'wrist_dev_r',	'wrist_flex_r',	'shoulder_elv_l',	'shoulder_rot_l',	'elv_angle_l',	'elbow_flexion_l',	'pro_sup_l',	'wrist_dev_l',	'wrist_flex_l']
coord_vals = [0,0,0,0,-0.01745,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] 

for i in range(len(coords)):

    curr_coord = coord_set.get(coords[i])
    curr_coord.setValue(state, coord_vals[i], False)

state = model.initSystem()
model.assemble(state)
analyze_tool.run()
[/size]

Afterwards, I use these codes to display if the values are updated, but they do not.

Code: Select all

coord_set = model.getCoordinateSet()     
print(coord_set.get(coords[4]).getName())    
print(coord_set.get(coords[4]).getValue(state))
Any help is appreciated.

Tags:

User avatar
Thomas Uchida
Posts: 1777
Joined: Wed May 16, 2012 11:40 am

Re: updating default coordinate values

Post by Thomas Uchida » Wed Mar 27, 2024 4:23 pm

The line "state = model.initSystem()" immediately after the "for" loop is creating a fresh underlying computational system and assigning it to the variable "state" in Matlab, so anything that was done to the "state" variable before that line will be lost. Please try removing that line.

User avatar
Riza Bayoglu
Posts: 14
Joined: Wed Aug 02, 2023 8:54 am

Re: updating default coordinate values

Post by Riza Bayoglu » Thu Mar 28, 2024 8:27 am

Hi Thomas,

Thanks for the help. My original scripts do not include the code piece, state = model.initSystem() after the second for loop. I accidentally copied that line when I was copying the clean version of my codes here, my bad. For the sake of clarity, my current codes are below. I am using Python v3.11 and Opensim v4.4 that I installed with Conda. I still can't get the joint coordinate values updated in the model.

Code: Select all

model      = osim.Model(modelpath)
actuators = osim.ForceSet('mymodel.xml')
forceset   = model.updForceSet()

for i in range(0, actuators.getSize() -1 ):
            
    forceset.cloneAndAppend(actuators.get(i))
    
analyze_tool = osim.AnalyzeTool('staticOpt_Setup.xml')
analyze_tool.setModel(model)
analyze_tool.setStartTime(0)
analyze_tool.setFinalTime(0)  

state         = model.initSystem()
coord_set  = model.getCoordinateSet()
coords       = ['pelvis_tilt',	'Abs_FE',	'Abs_LB',	'Abs_AR',	'L5_S1_FE',	'L5_S1_LB',	'L5_S1_AR',	'L4_L5_FE',	'L4_L5_LB',	'L4_L5_AR',	'L3_L4_FE',	'L3_L4_LB',	'L3_L4_AR',	'L2_L3_FE', 'L2_L3_LB',	'L2_L3_AR',	'L1_L2_FE',	'L1_L2_LB',	'L1_L2_AR',	'T12_L1_FE',	'T12_L1_LB',	'T12_L1_AR',	'T11_T12_FE',	'T11_T12_LB', 'T11_T12_AR',	'T10_T11_FE', 'T10_T11_LB',	'T10_T11_AR', 'T9_T10_FE',	'T9_T10_LB', 'T9_T10_AR',	'T8_T9_FE',	'T8_T9_LB',	'T8_T9_AR',	'T7_T8_FE',	'T7_T8_LB',	'T7_T8_AR',	'T6_T7_FE',	'T6_T7_LB',	'T6_T7_AR',	'T5_T6_FE', 'T5_T6_LB',	'T5_T6_AR',	'T4_T5_FE',	'T4_T5_LB',	'T4_T5_AR',	'T3_T4_FE',	'T3_T4_LB',	'T3_T4_AR',	'T2_T3_FE',	'T2_T3_LB',	'T2_T3_AR',	'T1_T2_FE',	'T1_T2_LB',	'T1_T2_AR',	'T1_head_neck_FE',	'T1_head_neck_LB',	'T1_head_neck_AR',	'T12_r12R_X',	'T12_r12R_Y',	'T12_r12R_Z',	'T11_r11R_X',	'T11_r11R_Y',	'T11_r11R_Z',	'T10_r10R_X',	'T10_r10R_Y',	'T10_r10R_Z',	'T9_r9R_X',	'T9_r9R_Y',	'T9_r9R_Z',	'T8_r8R_X',	'T8_r8R_Y',	'T8_r8R_Z',	'T7_r7R_X',	'T7_r7R_Y',	'T7_r7R_Z',	'T6_r6R_X',	'T6_r6R_Y',	'T6_r6R_Z',	'T5_r5R_X',	'T5_r5R_Y',	'T5_r5R_Z',	'T4_r4R_X',	'T4_r4R_Y',	'T4_r4R_Z',	'T3_r3R_X',	'T3_r3R_Y',	'T3_r3R_Z',	'T2_r2R_X',	'T2_r2R_Y',	'T2_r2R_Z',	'T1_r1R_X',	'T1_r1R_Y',	'T1_r1R_Z',	'T12_r12L_X',	'T12_r12L_Y',	'T12_r12L_Z',	'T11_r11L_X',	'T11_r11L_Y',	'T11_r11L_Z',	'T10_r10L_X',	'T10_r10L_Y',	'T10_r10L_Z',	'T9_r9L_X',	'T9_r9L_Y',	'T9_r9L_Z',	'T8_r8L_X',	'T8_r8L_Y',	'T8_r8L_Z',	'T7_r7L_X',	'T7_r7L_Y',	'T7_r7L_Z',	'T6_r6L_X',	'T6_r6L_Y',	'T6_r6L_Z',	'T5_r5L_X',	'T5_r5L_Y',	'T5_r5L_Z',	'T4_r4L_X',	'T4_r4L_Y',	'T4_r4L_Z',	'T3_r3L_X',	'T3_r3L_Y',	'T3_r3L_Z',	'T2_r2L_X',	'T2_r2L_Y',	'T2_r2L_Z',	'T1_r1L_X',	'T1_r1L_Y',	'T1_r1L_Z',	'SternumX',	'SternumY',	'SternumZ',	'SternumRotX',	'SternumRotY',	'SternumRotZ',	'shoulder_elv_r',	'shoulder_rot_r',	'elv_angle_r',	'elbow_flexion_r',	'pro_sup_r',	'wrist_dev_r',	'wrist_flex_r',	'shoulder_elv_l',	'shoulder_rot_l',	'elv_angle_l',	'elbow_flexion_l',	'pro_sup_l',	'wrist_dev_l',	'wrist_flex_l']
coord_vals = [0,0,0,0,-0.01745,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] 

for i in range(len(coords)):

    curr_coord = coord_set.get(coords[i])
    curr_coord.setValue(state, coord_vals[i], False)

model.assemble(state)
analyze_tool.run()

User avatar
Riza Bayoglu
Posts: 14
Joined: Wed Aug 02, 2023 8:54 am

Re: updating default coordinate values

Post by Riza Bayoglu » Mon Apr 01, 2024 8:29 am

I still need help with this. Does anyone have suggestions? Thank you.

User avatar
Mohammadreza Rezaie
Posts: 367
Joined: Fri Nov 24, 2017 12:48 am

Re: updating default coordinate values

Post by Mohammadreza Rezaie » Mon Apr 01, 2024 12:51 pm

Hi, I never used AnalyzeTool in that way. You can set your motion file using setCoordinatesFileName.

These may help:
  1. update coordinates values followed by model.assemble(state) and model.realizePosition(state) before analyze_tool.setModel(model) line.
  2. use setLoadModelAndInput before analyze_tool.run().

POST REPLY