\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. Additionally it 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 its input. These types of data are typically generated during experiments using motion capturing systems and force plates. In the context of this project, no such data was or is available or could easily be acquired. Instead we rather rely on relatively sparse muscle activation data and have no ground force measurements available. Also we are interested 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 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 visualization.

\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 model of a leg was created. It consists of three rigid bodies -- pelvis, femur and tibia -- and two joints -- hip and knee. The pelvis was fixed in space, the femur attached to it and the tibia attached to the femur. The model can be actuated using two muscles: Rectus Femoris and Biceps Femoris, which are an agonist/antagonist pair of muscles. They're attached to the pelivs 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!]
\label{fig:screenshot_opensim_simpleleg}
\centering
\includegraphics[scale=0.6]{images/screenshot_opensim_simpleleg.png}
\caption{Visualization of the SimpleLeg model}
\end{figure}

\subsection{Two-legged Model}

In the study by Dominici et al. \cite{Dominici2011} EMG activity of the following set of 12 muscles was recorded for each leg of the subject:

\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 possibly be adapted to represent a neonate, toddler or preschooler respectively.

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

The model was then adapted in order to only in its final version incorporate only the muscles for which 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 as one in 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 they were removed in the model for this project. The table shows only the muscles for one leg, the muscles of 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 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 because otherwise only the back muscles, the \emph{Erector Spinae (ES)} would connect the torso to the pelvis, but would have no antagonist. Therefor OE and ES were removed for the final model, which then included 17 muscles for each leg. 

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.

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

\begin{figure}[htb!]
\label{fig:screenshot_opensim_locomotorprimitives}
\centering
\includegraphics[scale=1.0]{images/screenshot_opensim_locomotorprimitives.png}
\caption{Visualization of the final musculoskeletal model used for the project.}
\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 OpenSim version up to 2.4 included numerical errors in the implementation. This deprecated version of the muscle model 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!]
\label{fig:hill_muscle_model}
\centering
\includegraphics[scale=2.0]{images/hill_muscle_model.pdf}
\caption{Schematic of the Hill-type mathematical muscle model.Illustration taken from \cite{Shadmehr2005}}
\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 sprain 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.

\section{Simulation Controller}

\subsection{Simple Leg Controller}

The 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}.

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

\TODO{Describe ideas behind it}

\section{Data Preparation}

\begin{figure}[htb!]
\label{fig:dominici_patterns_graphs}
\centering
\includegraphics[scale=1.0]{images/dominici_patterns_graphs.pdf}
\caption{Muscle activation pattern curves for the 24 muscles (EMG profiles averaged over all subjects). Taken from \cite{Dominici2011}.}
\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.

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.