# Build Subroutines

## Build a Fortran User Subroutine Using Visual Fortran

To build a Fortran user subroutine using Visual Fortran, complete the steps below.

1. In <install-path>\hwsolvers\motionsolve\usersub\f_project, double-click ms_fsubdll.vfproj.
Based on your installation of Microsoft Visual Studio, the project may be converted to a compatible version automatically.
Note: This directory contains a list of all Fortran subroutines that can be called by MotionSolve. You may remove any from the project that are not needed.
2. Edit the Fortran subroutine(s).
3. From the Build menu, select Build.
Your subroutine(s) will be compiled and built as ms_fsubdll.dll and placed in the folder <install-path>\hwsolvers\motionsolve\usersub\f_project\x64\Release or <install-path>\hwsolvers\motionsolve\usersub\f_project\x64\Debug depending on the build settings.

## Build a C/C++ User Subroutine using Visual C/C++

To build a C/C++ user subroutine using Visual C/C++, complete the steps below.

1. In <install-path>\hwsolvers\motionsolve\usersub\c_project, double-click ms_csubdll.vfproj.
Based on your installation of Microsoft Visual Studio, the project may be converted to a compatible version automatically.
Note: This directory contains a list of all C/C++ subroutines that can be called by MotionSolve. You may remove any from the project that are not needed.
2. Edit the C/C++ subroutine(s).
3. From the Build menu, select Build.
Your subroutine(s) will be compiled and built as ms_csubdll.dll and placed in the folder <install-path>\hwsolvers\motionsolve\usersub\c_project\x64\Release or <install-path>\hwsolvers\motionsolve\usersub\c_project\x64\Debug depending on the build settings.

## Build Subroutines in Linux

User subroutines can be built in Linux using the MakeFile that is provided in the installation at <install-path>\hwsolvers\motionsolve\usersub\Makefile.

1. Navigate to the above path in a shell window. Locate the file Makefile.
2. Make changes to the Fortran or C/C++ source code in <install-path>\hwsolvers\motionsolve\usersub\f_src or <install-path>\hwsolvers\motionsolve\usersub\c_src.
3. Make a choice:
• To compile and link a C/C++ source code shared object, enter "make csub=1".
• To compile and link a Fortran source code shared object, enter "make fsub=1".
This command creates the shared object in a folder titled exec\Linux_g64\usercsub.so or exec\Linux_g64\userfsub.so.
The table below lists some other options that can be used with the make file:
make help
Prints out help text for different options available with the make file.
make csub=1
Compile and link all C/C++ source files located in <install-path>\hwsolvers\motionsolve\usersub\c_src
make fsub=1
Compile and link all Fortran source files located in <install-path>\hwsolvers\motionsolve\usersub\f_src
make clean
Clean object files and shared libraries only for the current platform.
make realclean
Clean all object files and shared libraries for all platforms.

When editing the Fortran or C/C++ subroutine(s), you have to observe certain guidelines regarding assigning arguments, variable declarations, and calls to utility functions.

User written subroutines have input and output arguments. Typically, the output arguments are the result of the computation performed in your routine, while input arguments provide a communication between the multibody model (and its solver representation, the MotionSolve XML file) and the routine itself.

You can use parameters as input arguments to make subroutines more general. In other words, do not use hard coded entity IDs, but instead use them as input parameters to your routine.

To make sure that your subroutine(s) are effectively interfaced with MotionSolve, you need to accurately declare the input and output variables according to the guidelines provided in the subroutine templates. In other words, you should use the supplied routine templates where variables are already declared directly in the subroutine declaration (C/C++):
void STDCALL REQSUB(int *id, double *time, double *par, int *npar, int *iflag, double *results)
Or, declared shortly after the subroutine declaration (Fortran):
SUBROUTINE REQSUB (ID, TIME, PAR, NPAR, IFLAG, RESULTS)
DOUBLE PRECISION STATES(6)
INTEGER ITYPE, IPAR(10), NSTATES
LOGICAL ERRFLG


In addition to subroutine templates, MotionSolve also offers utility subroutines if you need to call them from your routines. These utility subroutines provide system states like displacement, velocity, acceleration, and force on various entities of your mechanical multibody system, allowing your routine(s) to accurately define and compute outputs based on system states.

For information about the available utility functions and how to call them from your subroutine(s), please refer to Utility Subroutines in the XML Format Reference Guide.