Sample file with simulation object interface

The following C file shows the interface to Embed simulation object code generation. This is a generic wrapper for a single instantiation of a simulation object. You will replace this file with your own user interface that can instantiate any number of simulation objects.

#include <math.h>

#include <memory.h>

#include <stdlib.h>

#include "vsuser.h"

#include "cgen.h"

// Sample file to show how to create and interface to a simulation object

#include "vsmApp.h" // This file contains the vsmCgRuntimeEvent() commands

#include "cmdApi.h"

#define MAX_VSM_ARG 64

#define TIME_END 1

 

int main(int argc, char **argv)

{

  SIM_STATE *hSim;  // Declare a handle to the sim

  double inSig[MAX_VSM_ARG], outSig[MAX_VSM_ARG], simTime,T, timeStep=.05;

  int a, retVal;

  double endTime=TIME_END*1.000000001;

 

  hSim = cgMainCreateSim(); // Create sim, return handle (Instantiate a simObject)

 

  vsmCgRuntimeCommand(hSim,RTE_GET_TIME_STEP,&timeStep,0,0); // Get sim timestep (optional)

  vsmCgRuntimeCommand(hSim,RTE_RESET,0,0,0);                 // Reset the sim (required before each run)

  for (T=0; T <= endTime; T+=timeStep)

    {

      inSig[0] = 5; // To do: supply your arguments here

      inSig[1] = 5; // We supply some arg values here

      inSig[2] = 0;

      retVal = vsmCgRuntimeCommand(hSim,RTE_RUN_TO_TIME,inSig,outSig,&T);

      if (retVal != VSM_SUCCESSFUL)

        {

          printf("vsmCgRuntimeCommand() returns '%s': ", vsmCgGetLastErrorString(hSim));

          break; // Problem in sim, stop simulating

        }

      printf("T=%g:ST=%g: %g,%g\n”, T,simTime, outSig[0],outSig[1]); // To do: make use of Embed results

    }

  return 0;

}