AmesimKnowledge

Implementation in a submodel

Saturation with discontinuity handling

The second piece of code handles the discontinuity. The algorithm is divided into 2 distinct parts.

  • The first part is the “discontinuity restart”. It is the part of code inside the test if(*flag == 0). This piece of code checks what is the current state of the equation and records this state in an integer store ic[0]. Note that the tests defining the state of x must be mutually exclusive and cover the whole range of possible values for x (see Troubleshooting).

  • The second part applies the equation corresponding to the state stored in ic[0] and checks whether the input x did not change of state. The result of the check is a Boolean value stored in the “logi” integer (1 for true, 0 for false). When logi is true, the call to the disloc function will alert the solver that a discontinuity has been reached.

Copy

void saturation_(int *n, double *y, double *x, double rp[2],
                 int ic[1], int *flag)
{
   int logi;
   double xmax, xmin;

   xmax = rp[0];
   xmin = rp[1];
   logi = 0;

   /* Discontinuity restart: check current state. */

   if(*flag == 0)
   {
      if (*x > xmax) /* Limited to higher value. */
      {
         ic[0] = 1;
      }
      else if (*x < xmin) /* Limited to lower value. */
      {
         ic[0] = -1;
      }
      else /* Unlimited. */
      {
         ic[0] = 0;
      }
   }

   /* Calculation part: apply value to y. */

   if (ic[0] == 1) /* Limited to higher value. */
   {
      *y = xmax;

      /* Check if becoming free of upper limit. */
      logi = (*x <= xmax);
   }
   else if (ic[0] == -1) /* Limited to lower value. */
   {
      *y = xmin;

      /* Check if becoming free of lower limit. */
      logi = (*x >= xmin);
   }
   else /* Unlimited. */
   {
      *y = *x;

      /* Check if becoming limited at upper value ... */
      logi = (*x > xmax) || (*x < xmin);
   }

   if (logi) /* logi is true (1) when using wrong equation. */
   {
      /* Send a warning to the solver. */
      disloc_(&logi);
   }
/* <<<<<<<<<<<<End of Calculation Executable Statements. */
}

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