void _Init()
{
  state tmp1;
  tmp1 = get_memory_state();
  Init_ext();
  restore_memory_state(tmp1);
  return ;
}
/* for continuous time variables */
int functionAliasEquations()
{
  state mem_state;
  
  mem_state = get_memory_state();
  restore_memory_state(mem_state);
  
  return 0;
}
/* for continuous time variables */
int functionAlgebraics()
{
  state mem_state;
  
  mem_state = get_memory_state();
  restore_memory_state(mem_state);
  
  return 0;
}
int function_storeDelayed()
{
  state mem_state;
  
  mem_state = get_memory_state();
  restore_memory_state(mem_state);
  
  return 0;
}
int bound_parameters()
{
  state mem_state;
  
  mem_state = get_memory_state();
  restore_memory_state(mem_state);
  
  return 0;
}
int function_updateSample()
{
  state mem_state;
  
  mem_state = get_memory_state();
  restore_memory_state(mem_state);
  
  return 0;
}
int functionODE()
{
  state mem_state;
  
  mem_state = get_memory_state();
  $P$DER$Py = $Px; 
  $P$DER$Px = (-$Py); 
  restore_memory_state(mem_state);
  
  return 0;
}
int functionJacD( double *jac)
{
  state mem_state;


  
  mem_state = get_memory_state();
  restore_memory_state(mem_state);
  
  return 0;
}
int function_onlyZeroCrossings(double *gout,double *t)
{
  state mem_state;
  
  mem_state = get_memory_state();
  ZEROCROSSING(0, LessEq($Px, 0.0));
  ZEROCROSSING(1, Greater($Px, 0.0));
  restore_memory_state(mem_state);
  
  return 0;
}
ExternalFunc2_rettype _ExternalFunc2(modelica_real _x)
{
  ExternalFunc2_rettype out;
  state tmp1;
  double _x_ext;
  double _y_ext;
  tmp1 = get_memory_state();
  _x_ext = (double)_x;
  _y_ext = ExternalFunc2(_x_ext);
  out.targ1 = (modelica_real)_y_ext;
  restore_memory_state(tmp1);
  return out;
}
void _Print(modelica_real _x, modelica_real _x_pre, modelica_real _t)
{
  state tmp1;
  double _x_ext;
  double _x_pre_ext;
  double _t_ext;
  tmp1 = get_memory_state();
  _x_ext = (double)_x;
  _x_pre_ext = (double)_x_pre;
  _t_ext = (double)_t;
  Print_ext(_x_ext, _x_pre_ext, _t_ext);
  restore_memory_state(tmp1);
  return ;
}
示例#12
0
int functionODE_inline()
{
  state mem_state;
  
  mem_state = get_memory_state();
  begin_inline();
  $P$DER$Py = $Px; 
  inline_integrate($P$DER$Py);
  $P$DER$Px = (-$Py); 
  inline_integrate($P$DER$Px);
  end_inline();
  restore_memory_state(mem_state);
  
  return 0;
}
示例#13
0
int initial_residual()
{
  int i = 0;
  state mem_state;
  modelica_real tmp1012;
  modelica_real tmp1013;
  
  mem_state = get_memory_state();
  tmp1012 = pre($Py);
  localData->initialResiduals[i++] = ($Py - tmp1012);
  if (sim_verbose == LOG_RES_INIT) { printf(" Residual[%d] : y - pre(y) = %f\n",i,localData->initialResiduals[i-1]); }
  tmp1013 = pre($Px);
  localData->initialResiduals[i++] = ($Px - tmp1013);
  if (sim_verbose == LOG_RES_INIT) { printf(" Residual[%d] : x - pre(x) = %f\n",i,localData->initialResiduals[i-1]); }
  restore_memory_state(mem_state);
  
  return 0;
}
示例#14
0
void index_real_array(const real_array_t * source,
                      const index_spec_t* source_spec,
                      real_array_t* dest)
{
    _index_t* idx_vec1;
    _index_t* idx_size;
    int j;
    int i;
    state mem_state;

    assert(base_array_ok(source));
    assert(base_array_ok(dest));
    assert(index_spec_ok(source_spec));
    assert(index_spec_fit_base_array(source_spec,source));

    /*for (i = 0, j = 0; i < source->ndims; ++i)
    {
      printf("source_spec->index_type[%d] = %c\n", i, source_spec->index_type[i]);
        if ((source_spec->index_type[i] == 'W')
            ||
            (source_spec->index_type[i] == 'A'))
            ++j;
    }
    assert(j == dest->ndims);*/
    for (i = 0,j = 0; i < source_spec->ndims; ++i) {
        if (source_spec->dim_size[i] != 0) {
            ++j;
        }
    }
    assert(j == dest->ndims);

    mem_state = get_memory_state();
    idx_vec1 = size_alloc(0,source->ndims);  /*indices in the source array*/
    /* idx_vec2 = size_alloc(0,dest->ndims); / * indices in the destination array* / */
    idx_size = size_alloc(0,source_spec->ndims);

    for (i = 0; i < source->ndims; ++i) {
        idx_vec1[i] = 0;
    }
    for (i = 0; i < source_spec->ndims; ++i) {
        if (source_spec->index[i] != NULL) { /* is 'S' or 'A' */
            idx_size[i] = imax(source_spec->dim_size[i],1); /* the imax() is not needed, because there is (idx[d] >= size[d]) in the next_index(), but ... */
        } else { /* is 'W' */
            idx_size[i] = source->dim_size[i];
        }
    }

    j = 0;
    do {
        /*
        for (i = 0, j = 0; i < source->ndims; ++i) {
            if ((source_spec->index_type[i] == 'W')
                ||
                (source_spec->index_type[i] == 'A')) {
                idx_vec2[j] = idx_vec1[i];
                j++;
            }
        }
        */
        real_set(dest, j,  /* calc_base_index(dest->ndims, idx_vec2, dest), */
                 real_get(source,
                          calc_base_index_spec(source->ndims, idx_vec1,
                                               source, source_spec)));
        j++;

    } while (0 == next_index(source->ndims, idx_vec1, idx_size));

    assert(j == base_array_nr_of_elements(dest));
    restore_memory_state(mem_state);
}
示例#15
0
void indexed_assign_real_array(const real_array_t * source,
             real_array_t* dest,
             const index_spec_t* dest_spec)
{
    _index_t* idx_vec1;
    _index_t* idx_size;
    int i,j;
    state mem_state;

    assert(base_array_ok(source));
    assert(base_array_ok(dest));
    assert(index_spec_ok(dest_spec));
    assert(index_spec_fit_base_array(dest_spec, dest));
    for (i = 0,j = 0; i < dest_spec->ndims; ++i) {
        if (dest_spec->dim_size[i] != 0) {
            ++j;
        }
    }
    assert(j == source->ndims);

    mem_state = get_memory_state();
    idx_vec1 = size_alloc(0,dest->ndims);
    /* idx_vec2 = size_alloc(0,source->ndims); */
    idx_size = size_alloc(0,dest_spec->ndims);

    for (i = 0; i < dest_spec->ndims; ++i) {
        idx_vec1[i] = 0;

        if (dest_spec->index[i] != NULL) { /* is 'S' or 'A' */
            idx_size[i] = imax(dest_spec->dim_size[i],1);
        } else { /* is 'W' */
            idx_size[i] = dest->dim_size[i];
        }
    }

    j = 0;
    do {
    /*for (i = 0, j = 0; i < dest_spec->ndims; ++i) {
            if (dest_spec->dim_size[i] != 0) {
              idx_vec2[j] = idx_vec1[i];
              ++j;
            }
    }*/
    /*for (i = 0, j = 0; i < dest_spec->ndims; ++i) {
      if ((dest_spec->index_type[i] == 'W')
         ||
         (dest_spec->index_type[i] == 'A')) {
         idx_vec2[j] = idx_vec1[i];
        ++j;
      }
    }*/
        real_set(dest,
                 calc_base_index_spec(dest->ndims, idx_vec1, dest, dest_spec),
                 real_get(source, j));
        j++;
          /*   calc_base_index(source->ndims, idx_vec1, source)));*/
          /* calc_base_index(source->ndims, idx_vec2, source)));*/

    } while (0 == next_index(dest_spec->ndims, idx_vec1, idx_size));

    assert(j == base_array_nr_of_elements(source));
    restore_memory_state(mem_state);
}
示例#16
0
/*! \fn solve non-linear system with hybrd method
 *
 *  \param [in]  [data]
 *                [sysNumber] index of the corresponing non-linear system
 *
 *  \author wbraun
 */
int solveHybrd(DATA *data, int sysNumber)
{
  NONLINEAR_SYSTEM_DATA* systemData = &(data->simulationInfo.nonlinearSystemData[sysNumber]);
  DATA_HYBRD* solverData = (DATA_HYBRD*)systemData->solverData;
  /*
   * We are given the number of the non-linear system.
   * We want to look it up among all equations.
   */
  int eqSystemNumber = systemData->equationIndex;

  int i, j;
  integer iflag = 1;
  double xerror, xerror_scaled;
  int success = 0;
  double local_tol = 1e-12;
  double initial_factor = solverData->factor;
  int nfunc_evals = 0;
  int continuous = 1;
  int nonContinuousCase = 0;

  int giveUp = 0;
  int retries = 0;
  int retries2 = 0;
  int retries3 = 0;
  int assertCalled = 0;
  int assertRetries = 0;
  int assertMessage = 0;
  static state mem_state;

  modelica_boolean* relationsPreBackup;
  relationsPreBackup = (modelica_boolean*) malloc(data->modelData.nRelations*sizeof(modelica_boolean));

  /* debug output */
  if(ACTIVE_STREAM(LOG_NLS))
  {
    INFO2(LOG_NLS, "start solving non-linear system >>%s<< at time %g",
      modelInfoXmlGetEquation(&data->modelData.modelDataXml, eqSystemNumber).name,
      data->localData[0]->timeValue);
    INDENT(LOG_NLS);
    for(i=0; i<solverData->n; i++)
    {
      INFO2(LOG_NLS, "x[%d] = %f", i, systemData->nlsx[i]);
      INDENT(LOG_NLS);
      INFO3(LOG_NLS, "scaling = %f\nold = %f\nextrapolated = %f",
            systemData->nominal[i], systemData->nlsxOld[i], systemData->nlsxExtrapolation[i]);
      RELEASE(LOG_NLS);
    }
    RELEASE(LOG_NLS);
  }

  /* set x vector */
  if(data->simulationInfo.discreteCall)
    memcpy(solverData->x, systemData->nlsx, solverData->n*(sizeof(double)));
  else
    memcpy(solverData->x, systemData->nlsxExtrapolation, solverData->n*(sizeof(double)));

  for(i=0; i<solverData->n; i++)
    solverData->xScalefactors[i] = fmax(fabs(solverData->x[i]), systemData->nominal[i]);

  /* evaluate with discontinuities */
  {
    int scaling = solverData->useXScaling;
    if(scaling)
      solverData->useXScaling = 0;

    mem_state = get_memory_state();
    /* try */
    if(!setjmp(nonlinearJmpbuf))
    {
      wrapper_fvec_hybrj(&solverData->n, solverData->x, solverData->fvec, solverData->fjac, &solverData->ldfjac, &iflag, data, sysNumber);
      restore_memory_state(mem_state);
    } else { /* catch */
      restore_memory_state(mem_state);
      WARNING(LOG_STDOUT, "Non-Linear Solver try to handle a problem with a called assert.");
    }
    if(scaling) {
      solverData->useXScaling = 1;
    }
  }

  /* start solving loop */
  while(!giveUp && !success)
  {

    for(i=0; i<solverData->n; i++)
      solverData->xScalefactors[i] = fmax(fabs(solverData->x[i]), systemData->nominal[i]);

    /* debug output */
    if(ACTIVE_STREAM(LOG_NLS_V)) {
      printVector(solverData->xScalefactors, &(solverData->n), LOG_NLS_V, "scaling factors x vector");
      printVector(solverData->x, &(solverData->n), LOG_NLS_V, "Iteration variable values");
    }

    /* Scaling x vector */
    if(solverData->useXScaling) {
      for(i=0; i<solverData->n; i++) {
        solverData->x[i] = (1.0/solverData->xScalefactors[i]) * solverData->x[i];
      }
    }

    /* debug output */
    if(ACTIVE_STREAM(LOG_NLS_V))
    {
      printVector(solverData->x, &solverData->n, LOG_NLS_V, "Iteration variable values (scaled)");
    }

    /* set residual function continuous
     */
    if(continuous) {
      ((DATA*)data)->simulationInfo.solveContinuous = 1;
    } else {
      ((DATA*)data)->simulationInfo.solveContinuous = 0;
    }

    giveUp = 1;

    mem_state = get_memory_state();
    /* try */
    if(!setjmp(nonlinearJmpbuf))
    {
      _omc_hybrj_(wrapper_fvec_hybrj, &solverData->n, solverData->x,
          solverData->fvec, solverData->fjac, &solverData->ldfjac, &solverData->xtol,
          &solverData->maxfev, solverData->diag, &solverData->mode,
          &solverData->factor, &solverData->nprint, &solverData->info,
          &solverData->nfev, &solverData->njev, solverData->r__,
          &solverData->lr, solverData->qtf, solverData->wa1,
          solverData->wa2, solverData->wa3, solverData->wa4, data, sysNumber);
      restore_memory_state(mem_state);
      if(assertCalled)
      {
        INFO(LOG_NLS, "After asserts was called, values reached which avoided assert call.");
        memcpy(systemData->nlsxOld, solverData->x, solverData->n*(sizeof(double)));
      }
      assertRetries = 0;
      assertCalled = 0;
    }
    else
    { /* catch */
      restore_memory_state(mem_state);
      if(!assertMessage)
      {
        INDENT(LOG_STDOUT);
        WARNING(LOG_STDOUT, "While solving non-linear system an assert was called.");
        WARNING(LOG_STDOUT, "The non-linear solver tries to solve the problem that could take some time.");
        WARNING(LOG_STDOUT, "It could help to provide better start-values for the iteration variables.");
        WARNING(LOG_STDOUT, "For more information simulate with -lv LOG_NLS");
        RELEASE(LOG_STDOUT);
        assertMessage = 1;
      }

      solverData->info = -1;
      xerror_scaled = 1;
      xerror = 1;
      assertCalled = 1;
    }

    /* set residual function continuous */
    if(continuous)
    {
      ((DATA*)data)->simulationInfo.solveContinuous = 0;
    }
    else
    {
      ((DATA*)data)->simulationInfo.solveContinuous = 1;
    }

    /* re-scaling x vector */
    if(solverData->useXScaling)
      for(i=0; i<solverData->n; i++)
        solverData->x[i] = solverData->x[i]*solverData->xScalefactors[i];

    /* check for proper inputs */
    if(solverData->info == 0) {
      printErrorEqSyst(IMPROPER_INPUT, modelInfoXmlGetEquation(&data->modelData.modelDataXml, eqSystemNumber),
          data->localData[0]->timeValue);
    }

    if(solverData->info != -1)
    {
      /* evaluate with discontinuities */
      if(data->simulationInfo.discreteCall){
        int scaling = solverData->useXScaling;
        if(scaling)
          solverData->useXScaling = 0;

        ((DATA*)data)->simulationInfo.solveContinuous = 0;

        mem_state = get_memory_state();
        /* try */
        if(!setjmp(nonlinearJmpbuf))
        {
          wrapper_fvec_hybrj(&solverData->n, solverData->x, solverData->fvec, solverData->fjac, &solverData->ldfjac, &iflag, data, sysNumber);
          restore_memory_state(mem_state);
        } else { /* catch */
          restore_memory_state(mem_state);
          WARNING(LOG_STDOUT, "Non-Linear Solver try to handle a problem with a called assert.");

          solverData->info = -1;
          xerror_scaled = 1;
          xerror = 1;
          assertCalled = 1;
        }

        if(scaling)
          solverData->useXScaling = 1;

        storeRelations(data);
      }
    }

    if(solverData->info != -1)
    {
      /* scaling residual vector */
      {
        int l=0;
        for(i=0; i<solverData->n; i++){
          solverData->resScaling[i] = 1e-16;
          for(j=0; j<solverData->n; j++){
            solverData->resScaling[i] = (fabs(solverData->fjacobian[l]) > solverData->resScaling[i])
                ? fabs(solverData->fjacobian[l]) : solverData->resScaling[i];
            l++;
          }
          solverData->fvecScaled[i] = solverData->fvec[i] * (1 / solverData->resScaling[i]);
        }
        /* debug output */
        if(ACTIVE_STREAM(LOG_NLS_V))
        {
          INFO(LOG_NLS_V, "scaling factors for residual vector");
          INDENT(LOG_NLS_V);
          for(i=0; i<solverData->n; i++)
          {
            INFO2(LOG_NLS_V, "scaled residual [%d] : %.20e", i, solverData->fvecScaled[i]);
            INDENT(LOG_NLS_V);
            INFO2(LOG_NLS_V, "scaling factor [%d] : %.20e", i, solverData->resScaling[i]);
            RELEASE(LOG_NLS_V);
          }
          RELEASE(LOG_NLS_V);
        }

        /* debug output */
        if(ACTIVE_STREAM(LOG_NLS_JAC))
        {
          char buffer[4096];

          INFO2(LOG_NLS_JAC, "jacobian matrix [%dx%d]", (int)solverData->n, (int)solverData->n);
          INDENT(LOG_NLS_JAC);
          for(i=0; i<solverData->n; i++)
          {
            buffer[0] = 0;
            for(j=0; j<solverData->n; j++)
              sprintf(buffer, "%s%10g ", buffer, solverData->fjacobian[i*solverData->n+j]);
            INFO1(LOG_NLS_JAC, "%s", buffer);
          }
          RELEASE(LOG_NLS_JAC);
        }

        /* check for error  */
        xerror_scaled = enorm_(&solverData->n, solverData->fvecScaled);
        xerror = enorm_(&solverData->n, solverData->fvec);
      }
    }

    /* reset non-contunuousCase */
    if(nonContinuousCase && xerror > local_tol && xerror_scaled > local_tol)
    {
      memcpy(data->simulationInfo.relationsPre, relationsPreBackup, sizeof(modelica_boolean)*data->modelData.nRelations);
      nonContinuousCase = 0;
    }

    if(solverData->info < 4 && xerror > local_tol && xerror_scaled > local_tol)
      solverData->info = 4;

    /* solution found */
    if(solverData->info == 1 || xerror <= local_tol || xerror_scaled <= local_tol)
    {
    int scaling;
      
    success = 1;
      nfunc_evals += solverData->nfev;
      if(ACTIVE_STREAM(LOG_NLS))
      {
        INFO(LOG_NLS, "system solved");
        INDENT(LOG_NLS);
        INFO2(LOG_NLS, "%d retries\n%d restarts", retries, retries2+retries3);
        RELEASE(LOG_NLS);

        printStatus(solverData, &nfunc_evals, &xerror, &xerror_scaled, LOG_NLS);

      }
      scaling = solverData->useXScaling;
      if(scaling)
        solverData->useXScaling = 0;

      /* take the solution */
      memcpy(systemData->nlsx, solverData->x, solverData->n*(sizeof(double)));
 
      mem_state = get_memory_state();
      /* try */
      if(!setjmp(nonlinearJmpbuf))
      {
        wrapper_fvec_hybrj(&solverData->n, solverData->x, solverData->fvec, solverData->fjac, &solverData->ldfjac, &iflag, data, sysNumber);
        restore_memory_state(mem_state);
      }
      else
      { /* catch */
        restore_memory_state(mem_state);
        WARNING(LOG_STDOUT, "Non-Linear Solver try to handle a problem with a called assert.");

        solverData->info = 4;
        xerror_scaled = 1;
        xerror = 1;
        assertCalled = 1;
        success = 0;
        giveUp = 0;
      }
      if(scaling)
        solverData->useXScaling = 1;
    }
    else if((solverData->info == 4 || solverData->info == 5) && assertRetries < 1+solverData->n && assertCalled)
    {
      /* case only used, when the Modelica code called an assert
       * then, we try to modify start values to avoid the assert call.*/
      int i;

      memcpy(solverData->x, systemData->nlsxOld, solverData->n*(sizeof(double)));

      /* set all zero values to nominal values */
      if(assertRetries < 1)
      {
        for(i=0; i<solverData->n; i++)
        {
          if(systemData->nlsx[i] == 0)
          {
            systemData->nlsx[i] = systemData->nominal[i];
            solverData->x[i] = systemData->nominal[i];
          }
        }
      }
      /* change initial guess values one by one */
      else if(assertRetries < solverData->n+1)
      {
        i = assertRetries-1;
        solverData->x[i] += 0.01*systemData->nominal[i];
      }

      giveUp = 0;
      nfunc_evals += solverData->nfev;
      assertRetries++;
      if(ACTIVE_STREAM(LOG_NLS))
      {
        INFO1(LOG_NLS, " - try to handle a problem with a called assert vary initial value a bit. (Retry: %d)",assertRetries);
        printStatus(solverData, &nfunc_evals, &xerror, &xerror_scaled, LOG_NLS_V);
      }
    }
    else if((solverData->info == 4 || solverData->info == 5) && retries < 3)
    {
      /* first try to decrease factor */

      /* set x vector */
      if(data->simulationInfo.discreteCall)
        memcpy(solverData->x, systemData->nlsx, solverData->n*(sizeof(double)));
      else
        memcpy(solverData->x, systemData->nlsxExtrapolation, solverData->n*(sizeof(double)));

      solverData->factor = solverData->factor / 10.0;

      retries++;
      giveUp = 0;
      nfunc_evals += solverData->nfev;
      if(ACTIVE_STREAM(LOG_NLS))
      {
        INFO1(LOG_NLS, " - iteration making no progress:\t decreasing initial step bound to %f.", solverData->factor);
        printStatus(solverData, &nfunc_evals, &xerror, &xerror_scaled, LOG_NLS_V);
      }
    }
    else if((solverData->info == 4 || solverData->info == 5) && retries < 4)
    {
      /* try to vary the initial values */

      for(i = 0; i < solverData->n; i++)
        solverData->x[i] += systemData->nominal[i] * 0.1;

      solverData->factor = initial_factor;
      retries++;
      giveUp = 0;
      nfunc_evals += solverData->nfev;

      if(ACTIVE_STREAM(LOG_NLS))
      {
        INFO(LOG_NLS, "iteration making no progress:\t vary solution point by 1%%.");
        printStatus(solverData, &nfunc_evals, &xerror, &xerror_scaled, LOG_NLS_V);
      }
    }
    else if((solverData->info == 4 || solverData->info == 5) && retries < 5)
    {
      /* try old values as x-Scaling factors */

      /* set x vector */
      if(data->simulationInfo.discreteCall)
        memcpy(solverData->x, systemData->nlsx, solverData->n*(sizeof(double)));
      else
        memcpy(solverData->x, systemData->nlsxExtrapolation, solverData->n*(sizeof(double)));


      for(i=0; i<solverData->n; i++)
        solverData->xScalefactors[i] = fmax(fabs(systemData->nlsxOld[i]), systemData->nominal[i]);

      retries++;
      giveUp = 0;
      nfunc_evals += solverData->nfev;
      if(ACTIVE_STREAM(LOG_NLS))
      {
        INFO(LOG_NLS, "iteration making no progress:\t try old values as scaling factors.");
        printStatus(solverData, &nfunc_evals, &xerror, &xerror_scaled, LOG_NLS_V);
      }
    }
    else if((solverData->info == 4 || solverData->info == 5) && retries < 6)
    {
      int scaling = 0;
      /* try to disable x-Scaling */

      /* set x vector */
      if(data->simulationInfo.discreteCall)
        memcpy(solverData->x, systemData->nlsx, solverData->n*(sizeof(double)));
      else
        memcpy(solverData->x, systemData->nlsxExtrapolation, solverData->n*(sizeof(double)));

      scaling = solverData->useXScaling;
      if(scaling)
        solverData->useXScaling = 0;

      /* reset x-scalling factors */
      for(i=0; i<solverData->n; i++)
        solverData->xScalefactors[i] = fmax(fabs(solverData->x[i]), systemData->nominal[i]);

      retries++;
      giveUp = 0;
      nfunc_evals += solverData->nfev;

      if(ACTIVE_STREAM(LOG_NLS))
      {
        INFO(LOG_NLS, "iteration making no progress:\t try without scaling at all.");
        printStatus(solverData, &nfunc_evals, &xerror, &xerror_scaled, LOG_NLS_V);
      }
    }
    else if((solverData->info == 4 || solverData->info == 5) && retries < 7  && data->simulationInfo.discreteCall)
    {
      /* try to solve non-continuous
       * work-a-round: since other wise some model does
       * stuck in event iteration. e.g.: Modelica.Mechanics.Rotational.Examples.HeatLosses
       */

      memcpy(solverData->x, systemData->nlsxOld, solverData->n*(sizeof(double)));
      retries++;

      /* try to solve a discontinuous system */
      continuous = 0;

      nonContinuousCase = 1;
      memcpy(relationsPreBackup, data->simulationInfo.relationsPre, sizeof(modelica_boolean)*data->modelData.nRelations);

      giveUp = 0;
      nfunc_evals += solverData->nfev;
      if(ACTIVE_STREAM(LOG_NLS)) {
        INFO(LOG_NLS, " - iteration making no progress:\t try to solve a discontinuous system.");
        printStatus(solverData, &nfunc_evals, &xerror, &xerror_scaled, LOG_NLS_V);
      }
    /* Then try with old values (instead of extrapolating )*/
    } else if((solverData->info == 4 || solverData->info == 5) && retries2 < 1) {
      int scaling = 0;
      /* set x vector */
      memcpy(solverData->x, systemData->nlsxOld, solverData->n*(sizeof(double)));

      scaling = solverData->useXScaling;
      if(!scaling)
        solverData->useXScaling = 1;

      continuous = 1;
      solverData->factor = initial_factor;

      retries = 0;
      retries2++;
      giveUp = 0;
      nfunc_evals += solverData->nfev;
      if(ACTIVE_STREAM(LOG_NLS)) {
        INFO(LOG_NLS, " - iteration making no progress:\t use old values instead extrapolated.");
        printStatus(solverData, &nfunc_evals, &xerror, &xerror_scaled, LOG_NLS_V);
      }
    /* try to vary the initial values */
    } else if((solverData->info == 4 || solverData->info == 5) && retries2 < 2) {
      /* set x vector */
      if(data->simulationInfo.discreteCall)
        memcpy(solverData->x, systemData->nlsx, solverData->n*(sizeof(double)));
      else
        memcpy(solverData->x, systemData->nlsxExtrapolation, solverData->n*(sizeof(double)));
      for(i = 0; i < solverData->n; i++) {
        solverData->x[i] *= 1.01;
      };

      retries = 0;
      retries2++;
      giveUp = 0;
      nfunc_evals += solverData->nfev;
      if(ACTIVE_STREAM(LOG_NLS)) {
        INFO(LOG_NLS,
            " - iteration making no progress:\t vary initial point by adding 1%%.");
        printStatus(solverData, &nfunc_evals, &xerror, &xerror_scaled, LOG_NLS_V);
      }
    /* try to vary the initial values */
    } else if((solverData->info == 4 || solverData->info == 5) && retries2 < 3) {
      /* set x vector */
      if(data->simulationInfo.discreteCall)
        memcpy(solverData->x, systemData->nlsx, solverData->n*(sizeof(double)));
      else
        memcpy(solverData->x, systemData->nlsxExtrapolation, solverData->n*(sizeof(double)));
      for(i = 0; i < solverData->n; i++) {
        solverData->x[i] *= 0.99;
      };

      retries = 0;
      retries2++;
      giveUp = 0;
      nfunc_evals += solverData->nfev;
      if(ACTIVE_STREAM(LOG_NLS)) {
        INFO(LOG_NLS, " - iteration making no progress:\t vary initial point by -1%%.");
        printStatus(solverData, &nfunc_evals, &xerror, &xerror_scaled, LOG_NLS_V);
      }
    /* try to vary the initial values */
    } else if((solverData->info == 4 || solverData->info == 5) && retries2 < 4) {
      /* set x vector */
      memcpy(solverData->x, systemData->nominal, solverData->n*(sizeof(double)));
      retries = 0;
      retries2++;
      giveUp = 0;
      nfunc_evals += solverData->nfev;
      if(ACTIVE_STREAM(LOG_NLS)) {
        INFO(LOG_NLS, " - iteration making no progress:\t try scaling factor as initial point.");
        printStatus(solverData, &nfunc_evals, &xerror, &xerror_scaled, LOG_NLS_V);
      }
    /* try own scaling factors */
    } else if((solverData->info == 4 || solverData->info == 5) && retries2 < 5 && !assertCalled) {
      /* set x vector */
      if(data->simulationInfo.discreteCall)
        memcpy(solverData->x, systemData->nlsx, solverData->n*(sizeof(double)));
      else
        memcpy(solverData->x, systemData->nlsxExtrapolation, solverData->n*(sizeof(double)));

      for(i = 0; i < solverData->n; i++) {
        solverData->diag[i] = fabs(solverData->resScaling[i]);
        if(solverData->diag[i] <= 1e-16)
          solverData->diag[i] = 1e-16;
      }
      retries = 0;
      retries2++;
      giveUp = 0;
      solverData->mode = 2;
      nfunc_evals += solverData->nfev;
      if(ACTIVE_STREAM(LOG_NLS)) {
        INFO(LOG_NLS, " - iteration making no progress:\t try with own scaling factors.");
        printStatus(solverData, &nfunc_evals, &xerror, &xerror_scaled, LOG_NLS_V);
      }
    /* try without internal scaling */
    } else if((solverData->info == 4 || solverData->info == 5) && retries3 < 1) {
      /* set x vector */
      if(data->simulationInfo.discreteCall)
        memcpy(solverData->x, systemData->nlsx, solverData->n*(sizeof(double)));
      else
        memcpy(solverData->x, systemData->nlsxExtrapolation, solverData->n*(sizeof(double)));

      for(i = 0; i < solverData->n; i++)
        solverData->diag[i] = 1.0;

      solverData->useXScaling = 1;
      retries = 0;
      retries2 = 0;
      retries3++;
      solverData->mode = 2;
      giveUp = 0;
      nfunc_evals += solverData->nfev;
      if(ACTIVE_STREAM(LOG_NLS)) {
        INFO(LOG_NLS, " - iteration making no progress:\t disable solver internal scaling.");
        printStatus(solverData, &nfunc_evals, &xerror, &xerror_scaled, LOG_NLS_V);
      }
    /* try to reduce the tolerance a bit */
    } else if((solverData->info == 4 || solverData->info == 5) && retries3 < 6) {
      /* set x vector */
      if(data->simulationInfo.discreteCall)
        memcpy(solverData->x, systemData->nlsx, solverData->n*(sizeof(double)));
      else
        memcpy(solverData->x, systemData->nlsxExtrapolation, solverData->n*(sizeof(double)));

      /* reduce tolarance */
      local_tol = local_tol*10;

      solverData->factor = initial_factor;
      solverData->mode = 1;

      retries = 0;
      retries2 = 0;
      retries3++;

      giveUp = 0;
      nfunc_evals += solverData->nfev;
      if(ACTIVE_STREAM(LOG_NLS)) {
        INFO1(LOG_NLS, " - iteration making no progress:\t reduce the tolerance slightly to %e.", local_tol);
        printStatus(solverData, &nfunc_evals, &xerror, &xerror_scaled, LOG_NLS_V);
      }
    } else if(solverData->info >= 2 && solverData->info <= 5) {

      /* while the initialization it's ok to every time a solution */
      if(!data->simulationInfo.initial){
        printErrorEqSyst(ERROR_AT_TIME, modelInfoXmlGetEquation(&data->modelData.modelDataXml, eqSystemNumber), data->localData[0]->timeValue);
      }
      if(ACTIVE_STREAM(LOG_NLS)) {
        RELEASE(LOG_NLS);
        INFO1(LOG_NLS, "### No Solution! ###\n after %d restarts", retries*retries2*retries3);
        printStatus(solverData, &nfunc_evals, &xerror, &xerror_scaled, LOG_NLS);
      }
      /* take the best approximation */
      memcpy(systemData->nlsx, solverData->x, solverData->n*(sizeof(double)));
    }
  }

  /* reset some solving data */
  solverData->factor = initial_factor;
  solverData->mode = 1;

  free(relationsPreBackup);

  return success;
}
示例#17
0
int functionDAE(int *needToIterate)
{
  state mem_state;
  initial_rettype tmp1014;
  modelica_boolean tmp1015;
  modelica_boolean tmp1016;
  change_rettype tmp1017;
  modelica_real tmp1018;
  *needToIterate = 0;
  inUpdate=initial()?0:1;
  
  mem_state = get_memory_state();
  $P$DER$Py = $Px; 
  $P$DER$Px = (-$Py); 
  /*#modelicaLine [TestExt.mo:9:1-11:9]*/
  tmp1014 = initial();
  localData->helpVars[3] = tmp1014;
  if (edge(localData->helpVars[3])) {
    /*#modelicaLine [TestExt.mo:10:2-10:8]*/
    #ifdef _OMC_MEASURE_TIME
    SIM_PROF_TICK_FN(Init_index);
    #endif
    _Init();
    #ifdef _OMC_MEASURE_TIME
    SIM_PROF_ACC_FN(Init_index);
    #endif
    /* NORETCALL */;
    /*#endModelicaLine*/
  }
  /*#endModelicaLine*/
  /*#modelicaLine [TestExt.mo:12:3-16:11]*/
  SAVEZEROCROSS(tmp1016, $Px, 0.0, 1,Greater,>);
  localData->helpVars[1] = tmp1016;
  SAVEZEROCROSS(tmp1015, $Px, 0.0, -1,LessEq,<=);
  localData->helpVars[2] = tmp1015;
  if (edge(localData->helpVars[1])) {
    /*#modelicaLine [TestExt.mo:13:5-13:11]*/
    $Ps = 1.0;
    /*#endModelicaLine*/
  }
  else if (edge(localData->helpVars[2])) {
    /*#modelicaLine [TestExt.mo:15:5-15:11]*/
    $Ps = 0.0;
    /*#endModelicaLine*/
  }
  /*#endModelicaLine*/
  /*#modelicaLine [TestExt.mo:18:3-20:11]*/
  tmp1017 = change($Ps);
  localData->helpVars[0] = tmp1017;
  if (edge(localData->helpVars[0])) {
    /*#modelicaLine [TestExt.mo:19:4-19:24]*/
    tmp1018 = pre($Ps);
    #ifdef _OMC_MEASURE_TIME
    SIM_PROF_TICK_FN(Print_index);
    #endif
    _Print($Ps, tmp1018, time);
    #ifdef _OMC_MEASURE_TIME
    SIM_PROF_ACC_FN(Print_index);
    #endif
    /* NORETCALL */;
    /*#endModelicaLine*/
  }
  /*#endModelicaLine*/
  restore_memory_state(mem_state);
  
  inUpdate=0;
  
  return 0;
}