\chapter{Methods}
\label{ch:methods}

\section{Simulation Software}

Simulation is performed using the OpenSim\footnote{Not to be confused with the 3D virtual world software 
OpenSimulator, which is sometimes also abbreviated as OpenSim.} software \cite{Delp2007}. OpenSim is an open-
source platform for modeling and analyzing neuromusculoskeletal structures and simulating their dynamic 
movement behavior. It's developed and maintained at the NIH Center for Biomedical Computation at Stanford 
University and is distributed as part of the Simtk.org repository.

In OpenSim, a simulation typically consists of a musculoskeletal model and a controller. The model can either 
be built individually or one of the models provided with OpenSim can be used. The model contains any 
combination of rigid bodies (such as bones), joints (simple or complex), constraints, springs, dampers, 
muscles and other actuators. A model is then assigned a controller which defines its dynamic behavior during 
simulation. There are predefined controllers available for forward dynamics (drive simulation with given 
muscle excitations), inverse kinematics (position model in simulation according to experimental markers and 
coordinate data at each time step) and inverse dynamics (determine generalized forces at each joint 
responsible for a given movement).

The software provides an extensive C++ application programming interface (API) in order to develop custom 
simulation controllers. The SimTK multi body dynamics API (Simbody) is used as the basic physics engine. 
Additionally OpenSim contains a graphical user interface (GUI) used to visualize models and simulations, edit 
models and muscle excitation data, as well as to access predefined simulation controllers. The GUI can be used 
independently from the simulation and the visualization is done using the data previously generated in the 
offline simulation.

The controllers provided with the OpenSim distribution turned out to require a very specific data collection 
as their input. This type of data is typically generated during biomechanics experiments, where muscle activations are measured using electromyography (EMG), the kinematics are tracked using a motion 
capturing system and force plates are used to register ground contact forces.

In the context of this project, no such data was available or could easily be acquired. Instead we rather 
relied on relatively sparse muscle activation patterns and have no ground force measurements or tracked 
kinematic paths available. Also the interest is in general principles of movement rather than the detailed 
analysis of the walking behavior of an individual subject.

Thus a custom controller making use of the provided OpenSIM C++ API was developed which actuates the model 
based on the basic activation patterns extracted from \cite{Dominici2011}. This approach also allows to 
specifically tune the simulation behavior to the requirements of this project and the data available.

For all tasks performed as part of this project, version 3.0 of OpenSim was used as provided on
\url{http://www.simtk.org}. The source version was used and built on Ubuntu Linux according to the provided 
instructions. Since the Linux version of OpenSim does not provide the GUI, the Windows version of OpenSim was 
used for visualizations.

\section{Musculoskeletal Model}

\subsection{Simple Leg Model}
\label{subsec:simple_leg_model}

In order to get familiar with modeling and controlling models in OpenSim, a simple and abstracted skeletal 
model of a human leg was created. It consists of three rigid bodies -- \emph{pelvis}, \emph{femur} and 
\emph{tibia} -- and two joints -- hip and knee. The pelvis was fixed in space, the femur attached to it using 
a pin joint (one degree-of-freedom) and the \emph{tibia} attached to the \emph{femur} using a pin joint. The 
model can be actuated using two muscles: \emph{Rectus Femoris} (anterior) and \emph{Biceps Femoris} 
(posterior), which are in this abstract model an agonist/antagonist pair of muscles and represent the largest 
muscles in the human lower extremity. They're attached to the pelvis on one end and to the femur and the tibia 
on the other end respectively. See figure \ref{fig:screenshot_opensim_simpleleg} for a visualization of the 
model.

\begin{figure}[htb!]
\centering
\includegraphics[scale=0.8]{images/screenshot_opensim_simpleleg.png}
\caption{Visualization of the SimpleLeg model. The topmost bone (\emph{pelvis}) is fixed in space, with the \emph{femur} and \emph{tibia} attached using pin joints. Actuation is achieved using the \emph{Rectus Femoris} (right) and \emph{Biceps Femoris} (left) muscles.}
\label{fig:screenshot_opensim_simpleleg}
\end{figure}

\subsection{Two-legged Model}
\label{subsec:two_legged_model}

In the study by Dominici et al. \cite{Dominici2011} the EMG activity of a selected set of 12 muscles was recorded for each leg of the subject. The following muscles were included in the publication:

\begin{itemize}
	\item \emph{Tibialis Anterior (TA)}
	\item \emph{Gastrocnemius Lateralis (LG)}
	\item \emph{Gastrocnemius Medialis (MG)}
	\item \emph{Soleus (Sol)}
	\item \emph{Vastus Lateralis (VL)}
	\item \emph{Vastus Medialis (VM)}
	\item \emph{Rectus Femoris (RF)}
	\item \emph{Hamstring (HS)} -- a group of four tigh muscles and their respective tendons, namely \emph{Semitendinosus}, \emph{Semimembranosus}, \emph{Biceps Femoris-Long Head} and \emph{Biceps Femoris-Short Head}
	\item \emph{Adductor Longus (Add)}
	\item \emph{Tensor Fascia Latae (TFL)}
	\item \emph{Gluteus Maximus (GM)}
	\item \emph{Erector Spinae (ES)}
\end{itemize}

The musculoskeletal model to be used thus had to incorporate all these muscles for each leg, so the proper activation patterns could be applied.

Several musculoskeletal models of human legs are already provided with OpenSim 3.0. For this project the \emph{Gait2354} model was used as a basis. This is a 23-degree-of-freedom model of the musculoskeletal system of the human leg. By default it contains 54 musculo-tendon actuators and represents a subject with a height of 1.8 m and a mass of 75.16 kg\footnote{More information on the \emph{Gait2354} model and its kinematic and dynamic properties as well as references to the studies the model is based on can be found on \url{http://simtk-confluence.stanford.edu:8080/display/OpenSim/Gait+2392+and+2354+Models}}. See figure \ref{fig:screenshot_opensim_gait2354} for a visualization of the model. Since the model represents an adult subject, the adult data from \cite{Dominici2011} was of primary interest. However OpenSim provides means to scale a given musculoskeletal model to different sizes, so the model could eventually be adapted to represent a neonate, toddler or preschooler respectively.

\begin{figure}[htb!]
\centering
\includegraphics[scale=1.0]{images/screenshot_opensim_gait2354.png}
\caption{Visualization of the \emph{Gait2354} model}
\label{fig:screenshot_opensim_gait2354}
\end{figure}

The model was then adapted in order for its final version to only incorporate the muscles for which publication data was available. Table \ref{table:muscles_correspondence} shows the muscles from the study and the \emph{Gait2354} musculoskeletal model respectively and how their activation was determined from the study data in the final model. Some of the muscles (the \emph{Hamstring} and \emph{Gluteus Maximus} specifically) were measured in combination for the study, but the muscles were present individually in the model. They were left as is and later on the controller was adapted to distribute the entire activation among the individual muscles (see \ref{subsec:locomotor_primitives_controller} for details). Furthermore for some of the muscles in the original \emph{Gait2354} model no activation data was available from the study, thus the respective muscles were removed from the model. The table shows only the muscles for one leg, the muscles of the second leg were handled correspondingly. The leg to which each muscle in the model belongs, is signified by it having a postfix \emph{\_l} for the left leg, or a postfix \emph{\_r} for the right leg.

\begin{center}
\begin{longtable}{llll}
\label{table:muscles_correspondence}

\textbf{Muscle} & \textbf{Name in model} & \textbf{Name in \cite{Dominici2011}} & \textbf{Activation Usage} \\
[1ex] \hline \\ [-1.5ex]
Adductor Longus & add\_long & Add & used directly \\
[1ex] \hline \\ [-1.5ex]
Gastrocnemius Lateralis & lat\_gas & LG & used directly \\
[1ex] \hline \\ [-1.5ex]
Gastrocnemius Medialis & med\_gas & MG & used directly \\
[1ex] \hline \\ [-1.5ex]
Rectus Femoris & rect\_fem & RF & used directly \\
[1ex] \hline \\ [-1.5ex]
Soleus & soleus & Sol & used directly \\
[1ex] \hline \\ [-1.5ex]
Tensor Fasciae Latae & tfl & TFL & used directly \\
[1ex] \hline \\ [-1.5ex]
Tibialis Anterior & tib\_ant & TA & used directly \\
[1ex] \hline \\ [-1.5ex]
Vastus Lateralis & vas\_lat & VL & used directly \\
[1ex] \hline \\ [-1.5ex]
Vastus Medialis & vas\_med & VM & used directly \\
[1ex] \hline \\ [-1.5ex]
Biceps Femoris-Long Head & bifemlh\_r & part of HS & $\frac{1}{4}$ of HS activation \\
[1ex] \hline \\ [-1.5ex]
Biceps Femoris-Short Head & bifemsh\_r & part of HS & $\frac{1}{4}$ of HS activation \\
[1ex] \hline \\ [-1.5ex]
Semimembranosus & semimem & part of HS & $\frac{1}{4}$ of HS activation \\
[1ex] \hline \\ [-1.5ex]
Semitendinosus & semiten & part of HS & $\frac{1}{4}$ of HS activation \\
[1ex] \hline \\ [-1.5ex]
Gluteus Maximus 1 & glut\_max1 & part of GM & $\frac{1}{3}$ of GM activation \\
[1ex] \hline \\ [-1.5ex]
Gluteus Maximus 2 & glut\_max2 & part of GM & $\frac{1}{3}$ of GM activation \\
[1ex] \hline \\ [-1.5ex]
Gluteus Maximus 3 & glut\_max3 & part of GM & $\frac{1}{3}$ of GM activation \\
[1ex] \hline \\ [-1.5ex]
Adductor Magnus 2 & add\_mag2 & - & removed from model \\
[1ex] \hline \\ [-1.5ex]
Erector Spinae & ercspn & ES & removed from model \\
[1ex] \hline \\ [-1.5ex]
Gluteus Medius 1 & glut\_med1 & - & removed from model \\
[1ex] \hline \\ [-1.5ex]
Gluteus Medius 2 & glut\_med2 & - & removed from model \\
[1ex] \hline \\ [-1.5ex]
Gluteus Medius 3 & glut\_med3 & - & removed from model \\
[1ex] \hline \\ [-1.5ex]
Gracilis & grac & - & removed from model \\
[1ex] \hline \\ [-1.5ex]
Iliacus & iliacus & - & removed from model \\
[1ex] \hline \\ [-1.5ex]
Internal Oblique & intobl & - & removed from model \\
[1ex] \hline \\ [-1.5ex]
Inferio gemellus & gem & - & removed from model \\
[1ex] \hline \\ [-1.5ex]
Pectineus & pect & - & removed from model \\
[1ex] \hline \\ [-1.5ex]
Piriformis & peri & - & removed from model \\
[1ex] \hline \\ [-1.5ex]
Psoas Major & psoas & - & removed from model \\
[1ex] \hline \\ [-1.5ex]
Quadratus Femoris & quad\_fem & - & removed from model \\
[1ex] \hline \\ [-1.5ex]
Sartorius & sar & - & removed from model \\
[1ex] \hline \\ [-1.5ex]
Tibialis Posterior & tib\_post & - & removed from model \\
[1ex] \hline \\ [-1.5ex]
Vastus Intermedius & vas\_int & - & removed from model \\
[1ex] \hline \\ [-1.5ex]
\caption{Muscles in the \emph{Gait2354} model and their correspondents in \cite{Dominici2011}.}
\end{longtable}
\end{center}

Because no data was available for the \emph{External Oblique (OE)} muscle -- one of the muscles which connects the torso to the pelvis -- the torso was removed from the model entirely, because otherwise only the back muscles -- the left and right \emph{Erector Spinae (ES)} -- would connect the torso to the pelvis, but would have no antagonist. Therefor \emph{OE} and \emph{ES} were removed, resulting in a total of 16 muscles per leg (32 total) in the final version of the model. 

The joints were left as in the original \emph{Gait2354 models}. For initial experiments the ``joint'' 
connecting the pelvis to the world was fixed in space using the zero-degree-of-freedom \emph{WeldJoint} joint 
type of OpenSim.

Figure \ref{fig:screenshot_opensim_locomotorprimitives} shows the resulting musculoskeletal model.

\begin{figure}[htb!]
\centering
\includegraphics[scale=1.0]{images/screenshot_opensim_locomotorprimitives.png}
\caption{Visualization of the final musculoskeletal model used for the project.}
\label{fig:screenshot_opensim_locomotorprimitives}
\end{figure}

\section{Muscle Model}

\subsection{Thelen Muscle Model}

For all muscles in the model, the Thelen mathematical muscle model \cite{Thelen2003} was used. This model is the default muscle model used by OpenSim 3.0\footnote{Note that up to version 2.4 OpenSim contained numerical errors in the implementation of the Thelen muscle model. This deprecated version is still available in the software in the C++ class \texttt{OpenSim::Thelen2003Muscle\_Deprecated} but shouldn't be used for actual simulations.} and is available in the C++ class \texttt{OpenSim::Thelen2003Muscle}. This muscle model is employed in all musculoskeletal models provided with OpenSim.

The Thelen muscle model is based on the well-known and widely used Hill-type muscle-tendon model \cite{Hill1922,Hill1970,Shadmehr2005,Zajac1989}, but incorporates adjustments in order to better account for age-related changes in the muscle properties.

\begin{figure}[htb!]
\centering
\includegraphics[scale=2.0]{images/hill_muscle_model.pdf}
\caption{Schematic of the Hill-type mathematical muscle model. Illustration taken from \cite{Shadmehr2005}}
\label{fig:hill_muscle_model}
\end{figure}

In the Thelen as well as in the Hill model, the muscle-tendon complex of an actuator consists of three components: a \emph{contractile element (CE)}, a \emph{passive element (PE)}, and a \emph{series elastic element (SEE or SE)}. See figure \ref{fig:hill_muscle_model} for an illustration, where $T$ is the tension, $K_{SE}$ the spring constant of the \emph{SE}, $K_{PE}$ the spring constant of the \emph{PE}, $b$ the damping coefficient of the \emph{CE} and $A$ the active force of the muscle.

The following parameters are used to characterize each Thelen-type muscle: maximum isometric force, optimal muscle fiber length, tendon slack length, maximum contraction velocity, and pennation angle. During  simulation, the muscle force is calculated using two states: the activation value and the muscle fiber length. All these parameters were left intact for the adjusted model.

\section{Simulation Controller}

\subsection{Simple Leg Controller}

The \emph{Simple Leg Controller} was used in order to create a simulation using the Simple Leg model described in \ref{subsec:simple_leg_model} in order to get familiar with the OpenSim workflow.

It implements a behavior similar to the stretch reflex (\emph{myotatic reflex}): An increase of muscle length 
causes the muscle spindles to be stretched, leading them to increase neural activity. This increases the 
activity of alpha motor neurons, which cause the muscle fibers to contract  in order to regain the original 
muscle length. Using a second set of neurons, the antagonist muscle is caused to relax \cite{Bear2007}.

OpenSim provides access to the lengthening speed of each muscle during simulation. This was used in the 
controller to deduce increases in muscle length and cause the corresponding reflex.

\subsection{Locomotor Primitives Controller}
\label{subsec:locomotor_primitives_controller}

All the predefined controllers available with OpenSim require a specific set of input data typically resulting 
from biomechanics studies using EMG, tracker systems and force plates. Since such data was not available in 
the context of this project, a custom controller for the simulation of the musculoskeletal model was developed 
in order to account for the limited availability of data.

The \emph{Locomotor Primitives Controller} allows to actuate the model described in section 
\ref{subsec:two_legged_model} using only the activation patterns for its 32 muscles (16 per leg). The data can 
be fed into the controller using the \emph{OpenSim Storage file format} (file extension \texttt{.sto}), which 
is the standard format to store and read data in OpenSim\footnote{The file format is explained in \url{http://
simtk-confluence.stanford.edu:8080/display/OpenSim/Storage+(.sto)+Files}, but there are some subtleties to the 
be considered when generating \texttt{.sto} files outside of OpenSim. Thus looking at the implementation of 
the class \texttt{OpenSim::Storage} is suggested when generating \texttt{.sto} files using external scripts.}.

All the activation data from the storage file are read into the controller and are accessible using the column 
name, which corresponds to the muscle name according to table \ref{table:muscles_correspondence} with the 
proper postfix. The controller checks the availability of activation data for all muscles for the entire 
simulation time and is then able to retrieve the corresponding activation for each muscle at each time step.

The time step resolution in OpenSim by default is dynamic and determined by the integrator used. For this 
project a Runge-Kutta integrator in the implementation of \texttt{SimTK::RungeKuttaMersonIntegrator} was used. 
This is the suggested default integrator in OpenSim.

Since the time step is not constant when using this integration method, OpenSim allows to interpolate the data 
read from the storage file in order to provide activation values at all possible time steps during the 
simulation. The total duration of the simulation can be specified as a parameter to the controller, so 
interpolation of the data points is also crucial for that matter.

For most of the muscles in the model, the \emph{Locomotor Primitives Controller} applies the activation value 
directly. For some special cases however (see column ``Activation Usage'' in table 
\ref{table:muscles_correspondence}), the muscle activation is split among a group of muscles, because they 
were measured in combination. Furthermore, the controller allows to scale the activation for each muscle 
individually by a constant value.

\section{Data Preparation}

\begin{figure}[htb!]
\centering
\includegraphics[width=\textwidth]{images/dominici_patterns_graphs.pdf}
\caption{Muscle activation pattern curves for the 24 muscles (EMG profiles averaged over all subjects). 
         Illustration extracted from \cite{Dominici2011}}
\label{fig:dominici_patterns_graphs}
\end{figure}

Since the original data from the publication by Dominici et al. \cite{Dominici2011} could not be used, the respective pattern curves had to be extracted from the paper (see figure \ref{fig:dominici_patterns_graphs}). This also lead to the fact, that neither data on ground contact forces nor kinematic tracker data was available, since they're not published.

In order to gather the activation data from the publication, a graph digitizer software (GraphClick by Arizona Software) was used. The data was then preprocessed by applying a curve interpolation in order to allow arbitrary, linearly spaced sampling of data points later on in the OpenSim model controller. Curve interpolation was done in a Python script using the \emph{NumPy} and \emph{SciPy} toolboxes.