Page 1 of 1

Compute magnitude of ExpressionBasedCoordinateForce

Posted: Fri Apr 23, 2021 3:24 pm
by rosshm
Hi all,

I'm trying to compute the magnitude of an ExpressionBasedCoordinateForce in my model in Matlab, given the model's state. I can extract the force object of interest like this, for the case of the EBCF that defines the passive hip flexion moment:

Code: Select all

forceSet = model.getForceSet();
force = forceSet.get('PasMom_hip_flexion_r');
and try to compute the force magnitude for a certain state:

Code: Select all

current_force_mag = force.computeForce(state)
This produces an error "Check for missing argument or incorrect argument data type in call to function 'computeForce'.", which sounds like I'm missing arguments that computeForce wants. I checked the documentation and it looks like computeForce wants the following inputs: state, bodyForces, generalizedForces, and I'm unsure what to input for the latter two arguments.

From the documentation, it also looks like ExpressionBasedCoordinateForce has a function "getForceMagnitude" with state as its only input, which sounds like what I want and makes sense for this type of force (its inputs are a generalized coordinate and the associated generalized speed), but when I run force.getForceMagnitude(state), I get an error "Unrecognized function or variable 'getForceMagnitude'." I think it's because my force object is of variable type "Force" and not "ExpressionBasedCoordinateForce".

Summary questions:
(1) What inputs is computeForce expecting for bodyForces and generalizedForces arguments?
(2) Can an ExpressionBasedCoordinateForce be extracted from the model with variable type "ExpressionBasedCoordinateForce" so that it knows about getForceMagnitude?

Thanks much,
Ross

Re: Compute magnitude of ExpressionBasedCoordinateForce

Posted: Fri Apr 23, 2021 4:40 pm
by tkuchida
(1) What inputs is computeForce expecting for bodyForces and generalizedForces arguments?
ExpressionBasedCoordinateForce::computeForce() is a void method (doesn't return anything). Argument 1 is a const state; arguments 2 and 3 are non-const arguments that will be populated with the results of the calculations. However, the comment in the .cpp file (https://github.com/opensim-org/opensim- ... e.cpp#L126) is "Compute and apply the force"; I think you may want to use the calcExpressionForce() method instead (https://github.com/opensim-org/opensim- ... e.cpp#L134).
(2) Can an ExpressionBasedCoordinateForce be extracted from the model with variable type "ExpressionBasedCoordinateForce" so that it knows about getForceMagnitude?
Yes- you need to downcast. For details and sample code, please see the "Common Scripting Commands" page in the documentation (https://simtk-confluence.stanford.edu/d ... g+Commands) and search for "downcast".