Interpolation support for GeometryPath's#11
Open
joris997 wants to merge 44 commits intoComputationalBiomechanicsLab:masterfrom
Open
Interpolation support for GeometryPath's#11joris997 wants to merge 44 commits intoComputationalBiomechanicsLab:masterfrom
joris997 wants to merge 44 commits intoComputationalBiomechanicsLab:masterfrom
Conversation
Initial refactor - compiles but needs fixing etc.
adamkewley
reviewed
Aug 16, 2021
| FunctionBasedPathModelTool RajagopalModel.osim RajagopalModel_Precomputed.osim | ||
| )"; | ||
|
|
||
| int main(int argc, char **argv) |
Collaborator
There was a problem hiding this comment.
This should probably be replaced with OpenSim's command-line parsing conventions. See: https://github.com/opensim-org/opensim-core/blob/master/Applications/opensim-cmd/opensim-cmd_run-tool.h#L70
Applications/FunctionBasedPathConversion/test/testFunctionBasedPathConversion.cpp
Outdated
Show resolved
Hide resolved
| cout << "\nsteps taken PBP: " << pbpStepsTaken << endl; | ||
| cout << "steps taken FBP: " << fbpStepsTaken << endl; | ||
|
|
||
| // test accuracy of FBP approximation |
Collaborator
There was a problem hiding this comment.
Do we test the accuracy?
Applications/FunctionBasedPathConversion/test/testFunctionBasedPathConversion.cpp
Outdated
Show resolved
Hide resolved
…an read files from any dir)
Interpolation updates (Sep 2021)
…ters externally modifiable
Interpolation (WIP)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary of changes
This pull request introduces the so-called "FunctionBasedPath" and "PointBasedPath". Currently, a muscle is described by a trajectory of points. Whether the trajectory is described by literal path points or by wrapping objects, the evaluation of lengths, lengthening speed, and moment arm can take considerable time (wrapping objects especially!). This PR, for each muscle, samples the affecting coordinates (unlock coordinate, perturb, see the change in muscle length), samples an n-dimensional grid of muscle lengths, and allows for interpolation to compute the length, lengthening speed, and moment arm.
An alias referring GeometryPath to PointBasedPath is set to make this update compatible with current models.
Inner workings
FunctionBasedPath (FBP) and PointBasedPath (PBP)
As mentioned, this PR introduces FBP and PBP. Both are sub-classes of GeometryPath. Currently, only GeometryPath is defined but this PR creates a distinction between a GeometryPath which is defined by a collection of points and one which is defined by sampling of a grid. PointBasedPath is the original GeometryPath implementation which contains all 'point-related' member functions. FunctionBasedPath contains the same methods but consists of an Interpolation object. The following methods are rewritten to be in accordance with the Interpolation object;
Other functions should not be callable for the FBP and therefore throw an OpenSim exception (OPENSIM_THROW)
Method details
getLength()
Using the Interpolator interpolation grid, a length is returned by using a 5th order spline. First, the state given as an argument to the FBP gets filtered of only the relevant affecting coordinates which get put in a vector and passed to getInterp(const std::vector).
getLengtheningSpeed()
The lengthening speed can be computed as the summation of the moment-arm times the coordinateSpeedValue (not changed in this PR) overall affecting coordinates.
computeMomentArm()
The moment-arm is the partial derivative of the length w.r.t. a coordinate. Therefore, the moment-arm is computed with the derivative (getInterpDer). Although initially it was thought out that this can be computed using the derivatives of the 5th order splines, this did not work in the current implementation. Instead, a finite difference derivative is computed. This has sufficient accuracy (step-size is 0.0001).
Others
other methods mentioned above are straightforward changes to the PointBasedPath implementation.
Usage
Accompanying this PR, an additional Tool has been developed (FunctionBasedPathConversionTool) which takes, as arguments, the std::string of a path to a PointBasedPath model, and a string indicating the output model name. You can then use tool.run() in order to generate a model in your build directory along with the accompanying text files containing the sampling data. The tool works as follows:
Interpolation Object
The PBP model, the argument, gets loaded and for each PointBasedPath an Interpolation object is made
Finding affecting coordinates
The n-dimensional grid is made up of a sampling grid of the n affecting coordinates. In FunctionBasedPath.cpp, an implementation is introduced where each coordinate is checked if it affects the GeometryPath under consideration (See coordinateAffectsPBP). These coordinates are then later used for the sampling.
Sampling
Using the affecting coordinates, an n-dimensional grid of GeometryPath lengths gets sampled and added to a 'grid' in the Interpolator constructor.
Print interpolation object
When the whole model has been build using FunctionBasedPath's (each containing an Interpolation object), the model get's printed along with a print-to-file for each Interpolation object. Inside the osim-file of the model, a path points towards the location of the Interpolation print file. This contains the affecting coordinates (with ranges, number of points for the range, etc.) and all the interpolated values).
Load FBP model
When the FBP model is to be loaded, the model-builder looks if it can find the specified interpolation text file it printed earlier. If this file exists, it gets read and the Interpolation object that is a member of a specific FBP gets loaded with the data of this file.
Testing
Speed-up
The main goal of this PR is to improve the computation speed of Forward Dynamic simulations. The Rajagopal model seems to be unfeasible for fast sampling with a sufficiently accurate grid on my machine. Instead, I tested the Arm26 and the Hopper model.
We obtain the following results table based on 23 interpolation pointer per affecting coordinate;
Accuracy
Based on 23 interpolation points per affecting coordinate;
Which looks a bit like this for the Arm26 model;

and like this for the Hopper model;
