Page 1 of 3

tendon compliance runtime error

Posted: Thu May 12, 2022 9:05 am
by grevec
Dear community,

I am performing an optimization with MocoInverse and EMGTracking and try to incorporate tendon compliance. The problem: the solution would not start running and report the following error:

[/code]std::exception in 'OpenSim::MocoSolution OpenSim::MocoStudy::solve() const': casVector should be 1-dimensional, but has size 0 x 0.
Thrown at MocoCasOCProblem.h:108 in convertToSimTKVector()

The code I am using is below. When ignoring tendon compliance with modelProcessor2.append(osim.ModOpIgnoreTendonCompliance()) the code runs fine.....do you see any odd things in the code?

Thanks Christian

Code: Select all

modelProcessor2 = osim.ModelProcessor(Scaled_modelFileName)
modelProcessor2.append(osim.ModOpAddExternalLoads(GRF_SourceFilename))
modelProcessor2.append(osim.ModOpUseImplicitTendonComplianceDynamicsDGF())
modelProcessor2.append(osim.ModOpReplaceMusclesWithDeGrooteFregly2016())
modelProcessor2.append(osim.ModOpAddReserves(ReserveActuatorStrength))


#%% Construct MocoInverse
inverse2 = osim.MocoInverse()
inverse2.setModel(modelProcessor2)
inverse2.setKinematics(osim.TableProcessor(Coordinates_Filename))
inverse2.set_minimize_sum_squared_activations(True)
inverse2.set_initial_time(start_time)
inverse2.set_final_time(end_time)
inverse2.set_mesh_interval(0.02)
inverse2.set_kinematics_allow_extra_columns(True)
study2 = inverse2.initialize()
problem2 = study2.updProblem()


# %% modify effort goal; put more effort on muscles which are close to activation 1....this needs individual adjustments.
controlEffortWeight = 50.0
effort2 = osim.MocoControlGoal("control_effort2") #.safeDownCast(problem.updGoal("control_effort"))
effort2.setName('effort2')
effort2.setWeight(controlEffortWeight)
Muscles = ClubfootModel.updMuscles()
problem2.addGoal(effort2)

#%% Tendon force and velocity equilibrium
TendonVGoal2 = osim.MocoInitialVelocityEquilibriumDGFGoal()
TendonVGoal2.setName("initial_velocity_equilibrium2")
# The problem converges in fewer iterations when this goal is in cost mode.
TendonVGoal2.setMode("cost")
TendonVGoal2.setWeight(0.001)
problem2.addGoal(TendonVGoal2)

#%% Add electromyography tracking.
emgTracking2 = osim.MocoControlTrackingGoal('emg_tracking2')
emgTracking2.setWeight(100)
controlsRef = osim.TimeSeriesTable(EMG_Filename)
emgTracking2.setReference(osim.TableProcessor(controlsRef))
emgTracking2.setReferenceLabel('/forceset/soleus_r', 'soleus_r_activation')
emgTracking2.setReferenceLabel('/forceset/med_gas_r', 'med_gas_r_activation')
emgTracking2.setReferenceLabel('/forceset/lat_gas_r', 'med_gas_r_activation')
emgTracking2.setReferenceLabel('/forceset/tib_ant_r', 'tib_ant_r_activation')
emgTracking2.setReferenceLabel('/forceset/per_brev_r', 'per_brev_r_activation')
emgTracking2.setReferenceLabel('/forceset/per_long_r', 'per_long_r_activation')# brev en long are same data
emgTracking2.setWeightForControl('/forceset/soleus_r',10)
emgTracking2.setWeightForControl('/forceset/med_gas_r',10)
emgTracking2.setWeightForControl('/forceset/lat_gas_r',10)
emgTracking2.setWeightForControl('/forceset/tib_ant_r',10)
emgTracking2.setWeightForControl('/forceset/per_brev_r',5)
emgTracking2.setWeightForControl('/forceset/per_long_r',5)
problem2.addGoal(emgTracking2)


#%% Solve the problem and write the solution to a Storage file.
solver2 = osim.MocoCasADiSolver.safeDownCast(study2.updSolver())
solver2.resetProblem(problem2)
solver2.set_multibody_dynamics_mode('implicit')
solver2.set_num_mesh_intervals(100)
solver2.set_optim_convergence_tolerance(.01)#.....10^-1
solver2.set_optim_constraint_tolerance(1e-4)#1e-4 is default
solver2.set_optim_max_iterations(4000)
solver2.set_parameters_require_initsystem(False)
solution2 = study2.solve()
solution2.unseal()
solution2.write(Output_Filename.replace('.sto','_tendonCompliance.sto'))

Re: tendon compliance runtime error

Posted: Thu May 12, 2022 11:02 am
by nbianco
Hi Christian,

I think the problem is failing because the muscles are using explicit tendon dynamics. It looks like you tried to use the implicit dynamics mode for tendon dynamics:

Code: Select all

modelProcessor2.append(osim.ModOpUseImplicitTendonComplianceDynamicsDGF())
modelProcessor2.append(osim.ModOpReplaceMusclesWithDeGrooteFregly2016())
But these two lines need to be flipped, since ModOpUseImplicitTendonComplianceDynamicsDGF will only work after the muscles have been converted to DeGrooteFregly2016Muscles (i.e., the order matters when using model operators).

Best,
Nick

Re: tendon compliance runtime error

Posted: Thu May 12, 2022 11:02 am
by nbianco
Hi Christian,

I think the problem is failing because the muscles are using explicit tendon dynamics. It looks like you tried to use the implicit dynamics mode for tendon dynamics:

Code: Select all

modelProcessor2.append(osim.ModOpUseImplicitTendonComplianceDynamicsDGF())
modelProcessor2.append(osim.ModOpReplaceMusclesWithDeGrooteFregly2016())
But these two lines need to be flipped, since ModOpUseImplicitTendonComplianceDynamicsDGF will only work after the muscles have been converted to DeGrooteFregly2016Muscles (i.e., the order matters when using model operators).

Best,
Nick

Re: tendon compliance runtime error

Posted: Thu May 19, 2022 11:43 am
by grevec
Thanks Nick,

that was indeed the problem. The simulation starts running fine now. But is seems that I increased complexity so much that the solution won't converge. I am running on an HPC cluster with 20 cores but still requires > 12 hrs and > 6 thousand iterations....have not managed to let it finish yet but will give it another try with a 24h runtime allowance.

Any suggestions how to facilitate convergence without sacrificing model complexity? how could I tune the solvers parameter? Below are my current default settings

Thanks

Christian

Code: Select all

    solver = osim.MocoCasADiSolver.safeDownCast(study.updSolver())
    solver.resetProblem(problem)
    solver.set_multibody_dynamics_mode('implicit')
    solver.set_num_mesh_intervals(40)
    solver.set_optim_convergence_tolerance(.01)
    solver.set_optim_constraint_tolerance(1e-4)
    solver.set_optim_max_iterations(10000)
    solver.set_parameters_require_initsystem(False)
    solver.setGuessFile(guessFilename)
    solution = study.solve()
    solution.unseal()
    solution.write(Output_Filename)

Re: tendon compliance runtime error

Posted: Thu May 19, 2022 4:05 pm
by rosshm
Hi Christian,

Try adding the following solver settings if you don't have them already:

solver.set_minimize_implicit_auxiliary_derivatives(true)
solver.set_implicit_auxiliary_derivatives_weight(waux)

This minimizes the time derivatives of the tendon forces and is supposed to help with convergence when using implicit muscles. Unless you want that to be a big part of the problem, waux is usually set to some small weight, much smaller than the weights on the "important" terms in your cost function. I use something like waux = 0.0001/N where N is the number of muscles.

Ross

Re: tendon compliance runtime error

Posted: Sat May 28, 2022 4:32 am
by grevec
Dear Ross and Nick,

I added solver.set_minimize_implicit_auxiliary_derivatives(true)
and solver.set_implicit_auxiliary_derivatives_weight(waux) to the solver as recommended. However, the Casadi solver fails to find a solution and reports the Warning: Restoration failed (after 12 hours and >4000 iterations).

Do you have any other suggestions to tweak the solver and facilitate convergence? Maybe decreasing complexity by removing some muscles with very short tendons?

Thanks and regards

Christian

Re: tendon compliance runtime error

Posted: Sat May 28, 2022 10:57 am
by pvb
Hi Christian,

Are you sure your model is physically capable of performing the movement you're trying to get it do perform? Combinations of F_max, optimal fiber length & tendon length of the individual muscles could lead to specific kinematics being difficult to achieve for a model. You could try making the reserve actuators stronger to see if this leads to convergence (if so, if a joint actuator is very active it might mean the muscles crossing that joint need to be made stronger).

Cheers,
Pasha

Re: tendon compliance runtime error

Posted: Mon May 30, 2022 11:40 pm
by grevec
Dear Pasha,

thanks for your reply. I think the model is strong enough.....it does converge once tendon compliance is ignored. Only when adding tendon compliance it does not converge. Is it maybe possible to switch off tendon compliance for individual muscles? Then I could systematically increase complexity and check which muscles cause trouble.

Thanks and regards

Christian

Re: tendon compliance runtime error

Posted: Tue May 31, 2022 9:17 am
by rosshm
Very short tendons can be problematic in my experience.

I find it's helpful to examine what tendon lengths would be required of the muscles in my model across the ranges of motion I'm expecting and make sure weird things like negative tendon lengths aren't happening.

Ross

Re: tendon compliance runtime error

Posted: Tue May 31, 2022 10:46 am
by nbianco
Hi Christian,
Is it maybe possible to switch off tendon compliance for individual muscles?
Yes, this is possible. For each muscle, you can set "set_ignore_tendon_compliance(false)" to ignore tendon dynamics.

-Nick