Compiling MapleSim C-Code in Visual C++ 2010 Express Introduction This tutorial will guide you through the steps involved in generating C code from a MapleSim 5 model, and then compiling the code in Visual C++ 2010 Express. Specifically, we will generate an executable which writes simulation results to a data file. The same principles can be applied to using the code generated by MapleSim in other development projects. Notes on the C code The code generated by MapleSim includes a solver, and only needs a main() function written by the user. The main() function will call ParamDriverC() in the generated code, which runs the simulation given the inputs given by inpfn(). Prerequisites You will need MapleSim 5 Visual C++ 2010 Express (available free from microsoft.com) 1 Prepare the MapleSim model Create this MapleSim model (or any other similar MapleSim model). It contains a subsystem (for which we will generate C code) with one real-valued inputs and two real-valued outputs
Note that the results exported from the C code will simply be the two outputs from the subsystem, and not the probe results. 2 Attach the Code Generation template 1. Go to View > Create Attachment 2. Select Code Generation from the list of templates 3. Click Create Attachment 3 The Code Generation template You should now be in the Code Generation Template.
Select the RLC subsystem You should now see the RLC subsystem in the MapleSim component Click Load Selected Subsystem 4 Pick the solver Make sure the Euler Solver is selected (although the following steps should work with any solver).
5 Generate and save the C Code Choose where you want the code to be saved (make the location already exists), and the name of the file. The file will automatically be given a c prefix and a c extension (e.g. the c file specified above will be called crlc.c) Click Generate C Code The C Code will be displayed at the bottom of the template.
The C code will also be saved in your specified location NOTES. The simulation inputs are given by inpfn() in the code. By default, inpfn() has the following form static void inpfn(double T, double *U) { U[0] = sin(t); } The default input is a sinusoid, but can be changed as required). If the MapleSim model has two or more inputs, they would be specified by U[0], U[1] U[n] in the code
6 Create a new project in Visual C++ 2010 Express In Visual C++ 2010 Express, go to File>New>Project, and select a Win32 Console Application. Give your project a name and save location, and then click OK > Next In the Application Settings window, check Empty project, and ensure Precompiled header is not checked Click Finish.
7 Add the C Code generated by MapleSim to the project In the Solution Explorer, right-click Source Files, and select Add>Existing Item Browse and select the C code file that MapleSim generated (from the location specified in Step 5). You should have the C code in Visual Studio 8 Specify the Include files and the compiler settings Go to Project>Properties Select VC++ Directories, and on the right-hand pane, click Include Directories
Add C:\Program Files\Maple 15\extern\include to the path. Your Include Directories path should now look like this Go to Configuration Properties > C/C++ > Advanced. On the right-hand pane, change the Compile As setting to Compile As C Code (/TC). Click OK
9 Add a main() function to the code Now return to the source code in Visual Studio. Add a main() function Add the following main() function to the code (i.e. the crlc.c file)
int main (void) { FILE *fd; double *ic, *p, *out; char errbuf[100]; long i, j, outd; long internal = 0; double t0 = 0.0; double dt = 0.1; double tf = 20.0; long npts = (long) ceil((tf+1e-10-t0)/dt)+1; out = (double *)malloc(((npts)*(nout+1)+nout+1)*sizeof(double)); fd = fopen("output.dat", "w"); outd = ParamDriverC(t0, dt, npts, NULL, NULL, out, &errbuf, internal); for(i=0;i<npts;i++) { fprintf(fd,"%lf ",out[i*(nout+1)]); for(j=0;j<nout;j++) { fprintf(fd,"%lf ",out[i*(nout+1)+j+1]); } fprintf(fd, "\n"); } fclose(fd); } return 0; The simulation start time, time step and end time are given by t0, dt and tf. The call to ParamDriverC() integrates the state variables, given the inputs specified by inpfn(). The simulation output will be written to a file called output.dat. 10 Compile the code Click Debug > Build Solution. Check the Output window at the bottom of Visual Studio. It will indicate if the Build process has succeeded.
11 Run the executable Now check the Debug folder of the Project directory. You should now find you have an executable. Double-click the executable to run it. You should now see an extra file called output.dat Output.dat contains the results of the simulation. The first column is simulation time, while the second two columns are the two outputs from the MapleSim subsystem for which we generated C code.