AmesimKnowledge

Implementation in a submodel

Example of time discontinuity

Discontinuous curves do not always depend on external variables (x), but simply on time (t). For instance a square wave signal generator knows precisely the times at which the signal will switch from 0 to 1 and vice versa.

In this case, the methodology is simpler and the submodel can directly indicate to the solver the time at which the discontinuity will occur. The solver procedure is simplified since it can directly adapt (reduce) the integration step size to match the next discontinuity point.

A specific function “distim” is created for that purpose. This function simply takes the time (in second) of the next discontinuity as argument.

The following code example is a sampler. The sampling period is a real parameter and is fixed during the simulation. The input signal is x, and the sampled output is y.

Figure 18: sampler input and output

2 real stores are necessary. The first one c[0] contains the time of the next sampling; it is called TNEXT to clarify the code. The second real store c[1] remembers the value of x at the last sampling time. This constant value is assigned to y.

Copy

/* preprocessor define used to clarify the code. */
#define TNEXT   c[0]
#define XSAMPLE c[1]

void sampler_(int *n, double *y, double *x, double rp[1],
              double c[2], int *flag, double *t)

{
   int loop, logi;
   double period;

   period = rp[0];
   logi = 0;
   loop = 0;

   /* Discontinuity restart. */

   if (*flag == 0)
   {
      if (*t >= TNEXT) /* Restarting just after TNEXT. Update of
                          TNEXT and XSAMPLE is needed. */
      {
         TNEXT = TNEXT + period;
         XSAMPLE = *x;
      }
   }
   /* Set the output y to a piecewise constant value. */

   *y = XSAMPLE;

   /* Indicate to the solver the time of the next sample. */
   distim_(&TNEXT);
}

Source: https://docs.sw.siemens.com/en-US/doc/254352342/PL20250521841123434.amesim_collection.TB121_Discontinuity_Handling/xid1849193 · retrieved 2026-07-17