Пример #1
0
static void sf_c1_Pointing_Cntrl_Act(SFc1_Pointing_Cntrl_ActInstanceStruct
  *chartInstance)
{
  real_T c1_hoistedGlobal;
  real_T c1_b_hoistedGlobal;
  real_T c1_c_hoistedGlobal;
  real_T c1_u;
  real_T c1_Lmax;
  real_T c1_Lsat;
  uint32_T c1_debug_family_var_map[7];
  real_T c1_Llim;
  real_T c1_nargin = 3.0;
  real_T c1_nargout = 1.0;
  real_T c1_y;
  real_T c1_varargin_1;
  real_T c1_varargin_2;
  real_T c1_b_varargin_2;
  real_T c1_varargin_3;
  real_T c1_x;
  real_T c1_b_y;
  real_T c1_b_x;
  real_T c1_c_y;
  real_T c1_xk;
  real_T c1_yk;
  real_T c1_c_x;
  real_T c1_d_y;
  real_T c1_b_varargin_1;
  real_T c1_c_varargin_2;
  real_T c1_d_varargin_2;
  real_T c1_b_varargin_3;
  real_T c1_d_x;
  real_T c1_e_y;
  real_T c1_e_x;
  real_T c1_f_y;
  real_T c1_b_xk;
  real_T c1_b_yk;
  real_T c1_f_x;
  real_T c1_g_y;
  real_T *c1_b_u;
  real_T *c1_h_y;
  real_T *c1_b_Lmax;
  real_T *c1_b_Lsat;
  c1_b_Lsat = (real_T *)ssGetInputPortSignal(chartInstance->S, 2);
  c1_b_Lmax = (real_T *)ssGetInputPortSignal(chartInstance->S, 1);
  c1_h_y = (real_T *)ssGetOutputPortSignal(chartInstance->S, 1);
  c1_b_u = (real_T *)ssGetInputPortSignal(chartInstance->S, 0);
  _sfTime_ = (real_T)ssGetT(chartInstance->S);
  _SFD_CC_CALL(CHART_ENTER_SFUNCTION_TAG, 0U, chartInstance->c1_sfEvent);
  _SFD_DATA_RANGE_CHECK(*c1_b_u, 0U);
  _SFD_DATA_RANGE_CHECK(*c1_h_y, 1U);
  _SFD_DATA_RANGE_CHECK(*c1_b_Lmax, 2U);
  _SFD_DATA_RANGE_CHECK(*c1_b_Lsat, 3U);
  chartInstance->c1_sfEvent = CALL_EVENT;
  _SFD_CC_CALL(CHART_ENTER_DURING_FUNCTION_TAG, 0U, chartInstance->c1_sfEvent);
  c1_hoistedGlobal = *c1_b_u;
  c1_b_hoistedGlobal = *c1_b_Lmax;
  c1_c_hoistedGlobal = *c1_b_Lsat;
  c1_u = c1_hoistedGlobal;
  c1_Lmax = c1_b_hoistedGlobal;
  c1_Lsat = c1_c_hoistedGlobal;
  _SFD_SYMBOL_SCOPE_PUSH_EML(0U, 7U, 7U, c1_debug_family_names,
    c1_debug_family_var_map);
  _SFD_SYMBOL_SCOPE_ADD_EML_IMPORTABLE(&c1_Llim, 0U, c1_sf_marshallOut,
    c1_sf_marshallIn);
  _SFD_SYMBOL_SCOPE_ADD_EML_IMPORTABLE(&c1_nargin, 1U, c1_sf_marshallOut,
    c1_sf_marshallIn);
  _SFD_SYMBOL_SCOPE_ADD_EML_IMPORTABLE(&c1_nargout, 2U, c1_sf_marshallOut,
    c1_sf_marshallIn);
  _SFD_SYMBOL_SCOPE_ADD_EML(&c1_u, 3U, c1_sf_marshallOut);
  _SFD_SYMBOL_SCOPE_ADD_EML(&c1_Lmax, 4U, c1_sf_marshallOut);
  _SFD_SYMBOL_SCOPE_ADD_EML(&c1_Lsat, 5U, c1_sf_marshallOut);
  _SFD_SYMBOL_SCOPE_ADD_EML_IMPORTABLE(&c1_y, 6U, c1_sf_marshallOut,
    c1_sf_marshallIn);
  CV_EML_FCN(0, 0);
  _SFD_EML_CALL(0U, chartInstance->c1_sfEvent, 4);
  if (CV_EML_IF(0, 1, 0, c1_u >= 0.0)) {
    _SFD_EML_CALL(0U, chartInstance->c1_sfEvent, 5);
    c1_Llim = c1_Lmax - c1_Lsat;
    _SFD_EML_CALL(0U, chartInstance->c1_sfEvent, 6);
    if (CV_EML_IF(0, 1, 1, c1_Llim < 0.0)) {
      _SFD_EML_CALL(0U, chartInstance->c1_sfEvent, 7);
      c1_y = 0.0;
    } else {
      _SFD_EML_CALL(0U, chartInstance->c1_sfEvent, 9);
      c1_varargin_1 = c1_Llim;
      c1_varargin_2 = c1_u;
      c1_b_varargin_2 = c1_varargin_1;
      c1_varargin_3 = c1_varargin_2;
      c1_x = c1_b_varargin_2;
      c1_b_y = c1_varargin_3;
      c1_b_x = c1_x;
      c1_c_y = c1_b_y;
      c1_eml_scalar_eg(chartInstance);
      c1_xk = c1_b_x;
      c1_yk = c1_c_y;
      c1_c_x = c1_xk;
      c1_d_y = c1_yk;
      c1_eml_scalar_eg(chartInstance);
      c1_y = muDoubleScalarMin(c1_c_x, c1_d_y);
    }
  } else {
    _SFD_EML_CALL(0U, chartInstance->c1_sfEvent, 12);
    c1_Llim = -c1_Lmax - c1_Lsat;
    _SFD_EML_CALL(0U, chartInstance->c1_sfEvent, 13);
    if (CV_EML_IF(0, 1, 2, c1_Llim > 0.0)) {
      _SFD_EML_CALL(0U, chartInstance->c1_sfEvent, 14);
      c1_y = 0.0;
    } else {
      _SFD_EML_CALL(0U, chartInstance->c1_sfEvent, 16);
      c1_b_varargin_1 = c1_Llim;
      c1_c_varargin_2 = c1_u;
      c1_d_varargin_2 = c1_b_varargin_1;
      c1_b_varargin_3 = c1_c_varargin_2;
      c1_d_x = c1_d_varargin_2;
      c1_e_y = c1_b_varargin_3;
      c1_e_x = c1_d_x;
      c1_f_y = c1_e_y;
      c1_eml_scalar_eg(chartInstance);
      c1_b_xk = c1_e_x;
      c1_b_yk = c1_f_y;
      c1_f_x = c1_b_xk;
      c1_g_y = c1_b_yk;
      c1_eml_scalar_eg(chartInstance);
      c1_y = muDoubleScalarMax(c1_f_x, c1_g_y);
    }
  }

  _SFD_EML_CALL(0U, chartInstance->c1_sfEvent, -16);
  _SFD_SYMBOL_SCOPE_POP();
  *c1_h_y = c1_y;
  _SFD_CC_CALL(EXIT_OUT_OF_FUNCTION_TAG, 0U, chartInstance->c1_sfEvent);
  _SFD_CHECK_FOR_STATE_INCONSISTENCY(_Pointing_Cntrl_ActMachineNumber_,
    chartInstance->chartNumber, chartInstance->instanceNumber);
}
Пример #2
0
/* Function Definitions */
void closestPoint(const real_T p_test[3], const real_T p1[3], const real_T p2[3],
                  const real_T p3[3], real_T p_plane[3])
{
  real_T a;
  real_T b;
  real_T c;
  real_T d;
  real_T e;
  real_T E1[3];
  int32_T i;
  real_T b_p_plane;
  real_T b_E1;
  real_T D;
  real_T det;
  real_T s;
  real_T t;
  real_T invDet;
  real_T tmp0;
  real_T tmp1;

  /* CLOSESTPOINT returns the closest points to p_test on the triangle */
  /* defined by p1, p2, and p3. All points are column vectors. */
  /* This uses the algorithm from: */
  /* http://www.geometrictools.com/Documentation/DistancePoint3Triangle3.pdf */
  /*      a = dot(E0,E0); */
  /*      b = dot(E0,E1); */
  /*      c = dot(E1,E1); */
  /*      d = dot(E0,D); */
  /*      e = dot(E1,D); */
  a = 0.0;
  b = 0.0;
  c = 0.0;
  d = 0.0;
  e = 0.0;
  for (i = 0; i < 3; i++) {
    b_p_plane = p2[i] - p1[i];
    b_E1 = p3[i] - p1[i];
    D = p1[i] - p_test[i];
    a += b_p_plane * b_p_plane;
    b += b_p_plane * b_E1;
    c += b_E1 * b_E1;
    d += b_p_plane * D;
    e += b_E1 * D;
    p_plane[i] = b_p_plane;
    E1[i] = b_E1;
  }

  /*      f = dot(D,D); */
  det = a * c - b * b;
  s = b * e - c * d;
  t = b * d - a * e;
  if (s + t < det) {
    if (s < 0.0) {
      if (t < 0.0) {
        /*  region = 4 */
        if (d < 0.0) {
          s = muDoubleScalarMin(muDoubleScalarMax(-d / a, 0.0), 1.0);
          t = 0.0;
        } else {
          s = 0.0;
          t = muDoubleScalarMin(muDoubleScalarMax(-e / c, 0.0), 1.0);
        }
      } else {
        /*  region = 3 */
        s = 0.0;
        t = muDoubleScalarMin(muDoubleScalarMax(-e / c, 0.0), 1.0);
      }
    } else if (t < 0.0) {
      /*  region = 5 */
      s = muDoubleScalarMin(muDoubleScalarMax(-d / a, 0.0), 1.0);
      t = 0.0;
    } else {
      /*  region = 0 */
      invDet = 1.0 / det;
      s *= invDet;
      t *= invDet;
    }
  } else if (s < 0.0) {
    /*  region = 2 */
    tmp0 = b + d;
    tmp1 = c + e;
    if (tmp1 > tmp0) {
      s = muDoubleScalarMin(muDoubleScalarMax((tmp1 - tmp0) / ((a - 2.0 * b) + c),
        0.0), 1.0);
      t = 1.0 - s;
    } else {
      s = 0.0;
      t = muDoubleScalarMin(muDoubleScalarMax(-e / c, 0.0), 1.0);
    }
  } else if (t < 0.0) {
    /*  region = 6 */
    tmp0 = b + e;
    tmp1 = a + d;
    if (tmp1 > tmp0) {
      t = muDoubleScalarMin(muDoubleScalarMax((tmp1 - tmp0) / ((a - 2.0 * b) + c),
        0.0), 1.0);
      s = 1.0 - t;
    } else {
      t = 0.0;
      s = muDoubleScalarMin(muDoubleScalarMax(-d / a, 0.0), 1.0);
    }
  } else {
    /*  region = 1 */
    s = muDoubleScalarMin(muDoubleScalarMax((((c + e) - b) - d) / ((a - 2.0 * b)
      + c), 0.0), 1.0);
    t = 1.0 - s;
  }

  for (i = 0; i < 3; i++) {
    p_plane[i] = (p1[i] + p_plane[i] * s) + E1[i] * t;
  }
}
Пример #3
0
/* Function Definitions */
void imshift(uint8_T I[270000], const real_T disparity[2])
{
  real_T xStart2;
  real_T yStart2;
  real_T xEnd2;
  real_T yEnd2;
  int32_T i2;
  int32_T i3;
  int32_T i4;
  int32_T i5;
  int32_T tmp_size_idx_0;
  int32_T loop_ub;
  int32_T i6;
  int32_T tmp_data[225];
  int32_T b_tmp_size_idx_0;
  int32_T b_tmp_data[400];
  emxArray_uint8_T *b_I;
  int32_T iv3[3];
  int32_T i7;
  int32_T b_loop_ub;
  int32_T i8;
  int32_T c_I[3];
  int32_T c_tmp_data[400];
  int32_T d_tmp_data[225];
  emxArray_uint8_T *d_I;
  emlrtHeapReferenceStackEnterFcnR2012b(emlrtRootTLSGlobal);
  xStart2 = muDoubleScalarMax(1.0, 1.0 + disparity[0]);
  yStart2 = muDoubleScalarMax(1.0, 1.0 + disparity[1]);
  xEnd2 = muDoubleScalarMin(225.0, 225.0 + disparity[0]);
  yEnd2 = muDoubleScalarMin(400.0, 400.0 + disparity[1]);
  i2 = (int32_T)muDoubleScalarMax(1.0, 1.0 - disparity[0]) - 1;
  i3 = (int32_T)muDoubleScalarMin(225.0, 225.0 - disparity[0]);
  i4 = (int32_T)muDoubleScalarMax(1.0, 1.0 - disparity[1]) - 1;
  i5 = (int32_T)muDoubleScalarMin(400.0, 400.0 - disparity[1]);
  tmp_size_idx_0 = ((int32_T)xEnd2 - (int32_T)xStart2) + 1;
  loop_ub = (int32_T)xEnd2 - (int32_T)xStart2;
  for (i6 = 0; i6 <= loop_ub; i6++) {
    tmp_data[i6] = ((int32_T)xStart2 + i6) - 1;
  }

  b_tmp_size_idx_0 = ((int32_T)yEnd2 - (int32_T)yStart2) + 1;
  loop_ub = (int32_T)yEnd2 - (int32_T)yStart2;
  for (i6 = 0; i6 <= loop_ub; i6++) {
    b_tmp_data[i6] = ((int32_T)yStart2 + i6) - 1;
  }

  emxInit_uint8_T(&b_I, 3, &emlrtRTEI, TRUE);
  iv3[0] = tmp_size_idx_0;
  iv3[1] = b_tmp_size_idx_0;
  iv3[2] = 3;
  i6 = b_I->size[0] * b_I->size[1] * b_I->size[2];
  b_I->size[0] = i3 - i2;
  b_I->size[1] = i5 - i4;
  b_I->size[2] = 3;
  emxEnsureCapacity((emxArray__common *)b_I, i6, (int32_T)sizeof(uint8_T),
                    &emlrtRTEI);
  for (i6 = 0; i6 < 3; i6++) {
    loop_ub = i5 - i4;
    for (i7 = 0; i7 < loop_ub; i7++) {
      b_loop_ub = i3 - i2;
      for (i8 = 0; i8 < b_loop_ub; i8++) {
        b_I->data[(i8 + b_I->size[0] * i7) + b_I->size[0] * b_I->size[1] * i6] =
          I[((i2 + i8) + 225 * (i4 + i7)) + 90000 * i6];
      }
    }
  }

  for (i6 = 0; i6 < 3; i6++) {
    c_I[i6] = b_I->size[i6];
  }

  emxFree_uint8_T(&b_I);
  emlrtSubAssignSizeCheckR2012b(iv3, 3, c_I, 3, &emlrtECI, emlrtRootTLSGlobal);
  for (i6 = 0; i6 < b_tmp_size_idx_0; i6++) {
    c_tmp_data[i6] = b_tmp_data[i6];
  }

  for (i6 = 0; i6 < tmp_size_idx_0; i6++) {
    d_tmp_data[i6] = tmp_data[i6];
  }

  emxInit_uint8_T(&d_I, 3, &emlrtRTEI, TRUE);
  i6 = d_I->size[0] * d_I->size[1] * d_I->size[2];
  d_I->size[0] = i3 - i2;
  d_I->size[1] = i5 - i4;
  d_I->size[2] = 3;
  emxEnsureCapacity((emxArray__common *)d_I, i6, (int32_T)sizeof(uint8_T),
                    &emlrtRTEI);
  for (i6 = 0; i6 < 3; i6++) {
    loop_ub = i5 - i4;
    for (i7 = 0; i7 < loop_ub; i7++) {
      b_loop_ub = i3 - i2;
      for (i8 = 0; i8 < b_loop_ub; i8++) {
        d_I->data[(i8 + d_I->size[0] * i7) + d_I->size[0] * d_I->size[1] * i6] =
          I[((i2 + i8) + 225 * (i4 + i7)) + 90000 * i6];
      }
    }
  }

  for (i2 = 0; i2 < 3; i2++) {
    loop_ub = d_I->size[1];
    for (i3 = 0; i3 < loop_ub; i3++) {
      b_loop_ub = d_I->size[0];
      for (i4 = 0; i4 < b_loop_ub; i4++) {
        I[(d_tmp_data[i4] + 225 * c_tmp_data[i3]) + 90000 * i2] = d_I->data[(i4
          + d_I->size[0] * i3) + d_I->size[0] * d_I->size[1] * i2];
      }
    }
  }

  emxFree_uint8_T(&d_I);
  if (xStart2 == 1.0) {
    if (xEnd2 + 1.0 > 225.0) {
      i2 = 0;
      i3 = 0;
    } else {
      i2 = (int32_T)(xEnd2 + 1.0);
      i2 = emlrtDynamicBoundsCheckFastR2012b(i2, 1, 225, &b_emlrtBCI,
        emlrtRootTLSGlobal) - 1;
      i3 = 225;
    }

    tmp_size_idx_0 = i3 - i2;
    for (i3 = 0; i3 < 3; i3++) {
      for (i4 = 0; i4 < 400; i4++) {
        for (i5 = 0; i5 < tmp_size_idx_0; i5++) {
          I[((i2 + i5) + 225 * i4) + 90000 * i3] = 0;
        }
      }
    }
  } else {
    for (i2 = 0; i2 < 3; i2++) {
      for (i3 = 0; i3 < 400; i3++) {
        loop_ub = (int32_T)(xStart2 - 1.0);
        for (i4 = 0; i4 < loop_ub; i4++) {
          I[(i4 + 225 * i3) + 90000 * i2] = 0;
        }
      }
    }
  }

  if (yStart2 == 1.0) {
    if (yEnd2 + 1.0 > 400.0) {
      i2 = 0;
      i3 = 0;
    } else {
      i2 = (int32_T)(yEnd2 + 1.0);
      i2 = emlrtDynamicBoundsCheckFastR2012b(i2, 1, 400, &emlrtBCI,
        emlrtRootTLSGlobal) - 1;
      i3 = 400;
    }

    tmp_size_idx_0 = i3 - i2;
    for (i3 = 0; i3 < 3; i3++) {
      for (i4 = 0; i4 < tmp_size_idx_0; i4++) {
        for (i5 = 0; i5 < 225; i5++) {
          I[(i5 + 225 * (i2 + i4)) + 90000 * i3] = 0;
        }
      }
    }
  } else {
    for (i2 = 0; i2 < 3; i2++) {
      loop_ub = (int32_T)(yStart2 - 1.0);
      for (i3 = 0; i3 < loop_ub; i3++) {
        for (i4 = 0; i4 < 225; i4++) {
          I[(i4 + 225 * i3) + 90000 * i2] = 0;
        }
      }
    }
  }

  emlrtHeapReferenceStackLeaveFcnR2012b(emlrtRootTLSGlobal);
}
Пример #4
0
static void c_eml_qrsolve(const emlrtStack *sp, const emxArray_real_T *A,
  emxArray_real_T *B, emxArray_real_T *Y)
{
  emxArray_real_T *b_A;
  emxArray_real_T *work;
  int32_T mn;
  int32_T i51;
  int32_T ix;
  emxArray_real_T *tau;
  emxArray_int32_T *jpvt;
  int32_T m;
  int32_T n;
  int32_T b_mn;
  emxArray_real_T *vn1;
  emxArray_real_T *vn2;
  int32_T k;
  boolean_T overflow;
  boolean_T b12;
  int32_T i;
  int32_T i_i;
  int32_T nmi;
  int32_T mmi;
  int32_T pvt;
  int32_T iy;
  boolean_T b13;
  real_T xnorm;
  int32_T i52;
  real_T atmp;
  real_T d16;
  boolean_T b14;
  boolean_T b_i;
  ptrdiff_t n_t;
  ptrdiff_t incx_t;
  double * xix0_t;
  boolean_T exitg1;
  const mxArray *y;
  static const int32_T iv78[2] = { 1, 8 };

  const mxArray *m14;
  char_T cv76[8];
  static const char_T cv77[8] = { '%', '%', '%', 'd', '.', '%', 'd', 'e' };

  char_T cv78[14];
  uint32_T unnamed_idx_0;
  emlrtStack st;
  emlrtStack b_st;
  emlrtStack c_st;
  emlrtStack d_st;
  emlrtStack e_st;
  emlrtStack f_st;
  emlrtStack g_st;
  emlrtStack h_st;
  st.prev = sp;
  st.tls = sp->tls;
  b_st.prev = &st;
  b_st.tls = st.tls;
  c_st.prev = &b_st;
  c_st.tls = b_st.tls;
  d_st.prev = &c_st;
  d_st.tls = c_st.tls;
  e_st.prev = &d_st;
  e_st.tls = d_st.tls;
  f_st.prev = &e_st;
  f_st.tls = e_st.tls;
  g_st.prev = &f_st;
  g_st.tls = f_st.tls;
  h_st.prev = &g_st;
  h_st.tls = g_st.tls;
  emlrtHeapReferenceStackEnterFcnR2012b(sp);
  emxInit_real_T(sp, &b_A, 2, &m_emlrtRTEI, true);
  b_emxInit_real_T(sp, &work, 1, &rb_emlrtRTEI, true);
  mn = (int32_T)muDoubleScalarMin(A->size[0], A->size[1]);
  st.site = &mc_emlrtRSI;
  b_st.site = &nc_emlrtRSI;
  c_st.site = &oc_emlrtRSI;
  i51 = b_A->size[0] * b_A->size[1];
  b_A->size[0] = A->size[0];
  b_A->size[1] = A->size[1];
  emxEnsureCapacity(&c_st, (emxArray__common *)b_A, i51, (int32_T)sizeof(real_T),
                    &m_emlrtRTEI);
  ix = A->size[0] * A->size[1];
  for (i51 = 0; i51 < ix; i51++) {
    b_A->data[i51] = A->data[i51];
  }

  b_emxInit_real_T(&c_st, &tau, 1, &m_emlrtRTEI, true);
  b_emxInit_int32_T(&c_st, &jpvt, 2, &m_emlrtRTEI, true);
  m = b_A->size[0];
  n = b_A->size[1];
  b_mn = muIntScalarMin_sint32(b_A->size[0], b_A->size[1]);
  i51 = tau->size[0];
  tau->size[0] = b_mn;
  emxEnsureCapacity(&c_st, (emxArray__common *)tau, i51, (int32_T)sizeof(real_T),
                    &n_emlrtRTEI);
  d_st.site = &mf_emlrtRSI;
  e_st.site = &rb_emlrtRSI;
  f_st.site = &sb_emlrtRSI;
  g_st.site = &tb_emlrtRSI;
  eml_signed_integer_colon(&g_st, b_A->size[1], jpvt);
  if ((b_A->size[0] == 0) || (b_A->size[1] == 0)) {
  } else {
    ix = b_A->size[1];
    i51 = work->size[0];
    work->size[0] = ix;
    emxEnsureCapacity(&c_st, (emxArray__common *)work, i51, (int32_T)sizeof
                      (real_T), &m_emlrtRTEI);
    for (i51 = 0; i51 < ix; i51++) {
      work->data[i51] = 0.0;
    }

    b_emxInit_real_T(&c_st, &vn1, 1, &pb_emlrtRTEI, true);
    b_emxInit_real_T(&c_st, &vn2, 1, &qb_emlrtRTEI, true);
    d_st.site = &tc_emlrtRSI;
    ix = b_A->size[1];
    i51 = vn1->size[0];
    vn1->size[0] = ix;
    emxEnsureCapacity(&c_st, (emxArray__common *)vn1, i51, (int32_T)sizeof
                      (real_T), &pb_emlrtRTEI);
    i51 = vn2->size[0];
    vn2->size[0] = ix;
    emxEnsureCapacity(&c_st, (emxArray__common *)vn2, i51, (int32_T)sizeof
                      (real_T), &qb_emlrtRTEI);
    k = 1;
    d_st.site = &nf_emlrtRSI;
    overflow = (b_A->size[1] > 2147483646);
    if (overflow) {
      e_st.site = &db_emlrtRSI;
      check_forloop_overflow_error(&e_st);
    }

    for (ix = 0; ix + 1 <= b_A->size[1]; ix++) {
      d_st.site = &sc_emlrtRSI;
      vn1->data[ix] = b_eml_xnrm2(&d_st, b_A->size[0], b_A, k);
      vn2->data[ix] = vn1->data[ix];
      k += b_A->size[0];
    }

    d_st.site = &rc_emlrtRSI;
    if (1 > b_mn) {
      b12 = false;
    } else {
      b12 = (b_mn > 2147483646);
    }

    if (b12) {
      e_st.site = &db_emlrtRSI;
      check_forloop_overflow_error(&e_st);
    }

    for (i = 1; i <= b_mn; i++) {
      i_i = (i + (i - 1) * m) - 1;
      nmi = n - i;
      mmi = m - i;
      d_st.site = &of_emlrtRSI;
      ix = eml_ixamax(&d_st, 1 + nmi, vn1, i);
      pvt = (i + ix) - 2;
      if (pvt + 1 != i) {
        d_st.site = &pf_emlrtRSI;
        e_st.site = &bc_emlrtRSI;
        f_st.site = &cc_emlrtRSI;
        ix = 1 + m * pvt;
        iy = 1 + m * (i - 1);
        g_st.site = &dc_emlrtRSI;
        if (1 > m) {
          b13 = false;
        } else {
          b13 = (m > 2147483646);
        }

        if (b13) {
          h_st.site = &db_emlrtRSI;
          check_forloop_overflow_error(&h_st);
        }

        for (k = 1; k <= m; k++) {
          i51 = b_A->size[0] * b_A->size[1];
          xnorm = b_A->data[emlrtDynamicBoundsCheckFastR2012b(ix, 1, i51,
            &le_emlrtBCI, &f_st) - 1];
          i51 = b_A->size[0] * b_A->size[1];
          i52 = b_A->size[0] * b_A->size[1];
          b_A->data[emlrtDynamicBoundsCheckFastR2012b(ix, 1, i51, &le_emlrtBCI,
            &f_st) - 1] = b_A->data[emlrtDynamicBoundsCheckFastR2012b(iy, 1, i52,
            &le_emlrtBCI, &f_st) - 1];
          i51 = b_A->size[0] * b_A->size[1];
          b_A->data[emlrtDynamicBoundsCheckFastR2012b(iy, 1, i51, &le_emlrtBCI,
            &f_st) - 1] = xnorm;
          ix++;
          iy++;
        }

        ix = jpvt->data[pvt];
        jpvt->data[pvt] = jpvt->data[i - 1];
        jpvt->data[i - 1] = ix;
        vn1->data[pvt] = vn1->data[i - 1];
        vn2->data[pvt] = vn2->data[i - 1];
      }

      if (i < m) {
        d_st.site = &qc_emlrtRSI;
        atmp = b_A->data[i_i];
        d16 = 0.0;
        if (1 + mmi <= 0) {
        } else {
          e_st.site = &wc_emlrtRSI;
          xnorm = b_eml_xnrm2(&e_st, mmi, b_A, i_i + 2);
          if (xnorm != 0.0) {
            xnorm = muDoubleScalarHypot(b_A->data[i_i], xnorm);
            if (b_A->data[i_i] >= 0.0) {
              xnorm = -xnorm;
            }

            if (muDoubleScalarAbs(xnorm) < 1.0020841800044864E-292) {
              ix = 0;
              do {
                ix++;
                e_st.site = &xc_emlrtRSI;
                b_eml_xscal(&e_st, mmi, 9.9792015476736E+291, b_A, i_i + 2);
                xnorm *= 9.9792015476736E+291;
                atmp *= 9.9792015476736E+291;
              } while (!(muDoubleScalarAbs(xnorm) >= 1.0020841800044864E-292));

              e_st.site = &yc_emlrtRSI;
              xnorm = b_eml_xnrm2(&e_st, mmi, b_A, i_i + 2);
              xnorm = muDoubleScalarHypot(atmp, xnorm);
              if (atmp >= 0.0) {
                xnorm = -xnorm;
              }

              d16 = (xnorm - atmp) / xnorm;
              e_st.site = &ad_emlrtRSI;
              b_eml_xscal(&e_st, mmi, 1.0 / (atmp - xnorm), b_A, i_i + 2);
              e_st.site = &bd_emlrtRSI;
              if (1 > ix) {
                b14 = false;
              } else {
                b14 = (ix > 2147483646);
              }

              if (b14) {
                f_st.site = &db_emlrtRSI;
                check_forloop_overflow_error(&f_st);
              }

              for (k = 1; k <= ix; k++) {
                xnorm *= 1.0020841800044864E-292;
              }

              atmp = xnorm;
            } else {
              d16 = (xnorm - b_A->data[i_i]) / xnorm;
              atmp = 1.0 / (b_A->data[i_i] - xnorm);
              e_st.site = &cd_emlrtRSI;
              b_eml_xscal(&e_st, mmi, atmp, b_A, i_i + 2);
              atmp = xnorm;
            }
          }
        }

        tau->data[i - 1] = d16;
      } else {
        atmp = b_A->data[i_i];
        d_st.site = &pc_emlrtRSI;
        tau->data[i - 1] = eml_matlab_zlarfg();
      }

      b_A->data[i_i] = atmp;
      if (i < n) {
        atmp = b_A->data[i_i];
        b_A->data[i_i] = 1.0;
        d_st.site = &qf_emlrtRSI;
        eml_matlab_zlarf(&d_st, mmi + 1, nmi, i_i + 1, tau->data[i - 1], b_A, i
                         + i * m, m, work);
        b_A->data[i_i] = atmp;
      }

      d_st.site = &rf_emlrtRSI;
      if (i + 1 > n) {
        b_i = false;
      } else {
        b_i = (n > 2147483646);
      }

      if (b_i) {
        e_st.site = &db_emlrtRSI;
        check_forloop_overflow_error(&e_st);
      }

      for (ix = i; ix + 1 <= n; ix++) {
        if (vn1->data[ix] != 0.0) {
          xnorm = muDoubleScalarAbs(b_A->data[(i + b_A->size[0] * ix) - 1]) /
            vn1->data[ix];
          xnorm = 1.0 - xnorm * xnorm;
          if (xnorm < 0.0) {
            xnorm = 0.0;
          }

          atmp = vn1->data[ix] / vn2->data[ix];
          atmp = xnorm * (atmp * atmp);
          if (atmp <= 1.4901161193847656E-8) {
            if (i < m) {
              d_st.site = &sf_emlrtRSI;
              e_st.site = &uc_emlrtRSI;
              if (mmi < 1) {
                xnorm = 0.0;
              } else {
                f_st.site = &vc_emlrtRSI;
                g_st.site = &vc_emlrtRSI;
                n_t = (ptrdiff_t)(mmi);
                g_st.site = &vc_emlrtRSI;
                incx_t = (ptrdiff_t)(1);
                i51 = b_A->size[0] * b_A->size[1];
                i52 = (i + m * ix) + 1;
                xix0_t = (double *)(&b_A->data[emlrtDynamicBoundsCheckFastR2012b
                                    (i52, 1, i51, &vb_emlrtBCI, &f_st) - 1]);
                xnorm = dnrm2(&n_t, xix0_t, &incx_t);
              }

              vn1->data[ix] = xnorm;
              vn2->data[ix] = vn1->data[ix];
            } else {
              vn1->data[ix] = 0.0;
              vn2->data[ix] = 0.0;
            }
          } else {
            d_st.site = &tf_emlrtRSI;
            vn1->data[ix] *= muDoubleScalarSqrt(xnorm);
          }
        }
      }
    }

    emxFree_real_T(&vn2);
    emxFree_real_T(&vn1);
  }

  atmp = 0.0;
  if (mn > 0) {
    xnorm = muDoubleScalarMax(A->size[0], A->size[1]) * muDoubleScalarAbs
      (b_A->data[0]) * 2.2204460492503131E-16;
    k = 0;
    exitg1 = false;
    while ((!exitg1) && (k <= mn - 1)) {
      if (muDoubleScalarAbs(b_A->data[k + b_A->size[0] * k]) <= xnorm) {
        st.site = &lc_emlrtRSI;
        y = NULL;
        m14 = emlrtCreateCharArray(2, iv78);
        for (i = 0; i < 8; i++) {
          cv76[i] = cv77[i];
        }

        emlrtInitCharArrayR2013a(&st, 8, m14, cv76);
        emlrtAssign(&y, m14);
        b_st.site = &tg_emlrtRSI;
        emlrt_marshallIn(&b_st, c_sprintf(&b_st, b_sprintf(&b_st, y,
          emlrt_marshallOut(14.0), emlrt_marshallOut(6.0), &o_emlrtMCI),
          emlrt_marshallOut(xnorm), &p_emlrtMCI), "sprintf", cv78);
        st.site = &kc_emlrtRSI;
        b_eml_warning(&st, atmp, cv78);
        exitg1 = true;
      } else {
        atmp++;
        k++;
      }
    }
  }

  unnamed_idx_0 = (uint32_T)A->size[1];
  i51 = Y->size[0];
  Y->size[0] = (int32_T)unnamed_idx_0;
  emxEnsureCapacity(sp, (emxArray__common *)Y, i51, (int32_T)sizeof(real_T),
                    &m_emlrtRTEI);
  ix = (int32_T)unnamed_idx_0;
  for (i51 = 0; i51 < ix; i51++) {
    Y->data[i51] = 0.0;
  }

  for (ix = 0; ix < mn; ix++) {
    if (tau->data[ix] != 0.0) {
      xnorm = B->data[ix];
      i51 = A->size[0] + (int32_T)(1.0 - ((1.0 + (real_T)ix) + 1.0));
      emlrtForLoopVectorCheckR2012b((1.0 + (real_T)ix) + 1.0, 1.0, A->size[0],
        mxDOUBLE_CLASS, i51, &ac_emlrtRTEI, sp);
      for (i = 0; i < i51; i++) {
        unnamed_idx_0 = ((uint32_T)ix + i) + 2U;
        xnorm += b_A->data[((int32_T)unnamed_idx_0 + b_A->size[0] * ix) - 1] *
          B->data[(int32_T)unnamed_idx_0 - 1];
      }

      xnorm *= tau->data[ix];
      if (xnorm != 0.0) {
        B->data[ix] -= xnorm;
        i51 = A->size[0] + (int32_T)(1.0 - ((1.0 + (real_T)ix) + 1.0));
        emlrtForLoopVectorCheckR2012b((1.0 + (real_T)ix) + 1.0, 1.0, A->size[0],
          mxDOUBLE_CLASS, i51, &yb_emlrtRTEI, sp);
        for (i = 0; i < i51; i++) {
          unnamed_idx_0 = ((uint32_T)ix + i) + 2U;
          B->data[(int32_T)unnamed_idx_0 - 1] -= b_A->data[((int32_T)
            unnamed_idx_0 + b_A->size[0] * ix) - 1] * xnorm;
        }
      }
    }
  }

  emxFree_real_T(&tau);
  emlrtForLoopVectorCheckR2012b(1.0, 1.0, atmp, mxDOUBLE_CLASS, (int32_T)atmp,
    &xb_emlrtRTEI, sp);
  for (i = 0; i < (int32_T)atmp; i++) {
    Y->data[jpvt->data[i] - 1] = B->data[i];
  }

  emlrtForLoopVectorCheckR2012b(atmp, -1.0, 1.0, mxDOUBLE_CLASS, (int32_T)-(1.0
    + (-1.0 - atmp)), &wb_emlrtRTEI, sp);
  for (ix = 0; ix < (int32_T)-(1.0 + (-1.0 - atmp)); ix++) {
    xnorm = atmp + -(real_T)ix;
    Y->data[jpvt->data[(int32_T)xnorm - 1] - 1] = eml_div(Y->data[jpvt->data
      [(int32_T)xnorm - 1] - 1], b_A->data[((int32_T)xnorm + b_A->size[0] *
      ((int32_T)xnorm - 1)) - 1]);
    for (i = 0; i < (int32_T)(xnorm - 1.0); i++) {
      Y->data[jpvt->data[i] - 1] -= Y->data[jpvt->data[(int32_T)xnorm - 1] - 1] *
        b_A->data[i + b_A->size[0] * ((int32_T)xnorm - 1)];
    }
  }

  emxFree_int32_T(&jpvt);
  emxFree_real_T(&work);
  emxFree_real_T(&b_A);
  emlrtHeapReferenceStackLeaveFcnR2012b(sp);
}
Пример #5
0
/* Function Definitions */
void rsf2csf(const emxArray_real_T *Ur, const emxArray_real_T *Tr,
             emxArray_creal_T *U, emxArray_creal_T *T)
{
  int32_T y;
  int32_T loop_ub;
  int16_T varargin_1[2];
  int32_T mtmp;
  int32_T m;
  real_T c;
  real_T b;
  real_T temp;
  real_T p;
  real_T bcmax;
  real_T scale;
  real_T bb;
  real_T b_p;
  real_T cs;
  int32_T b_scale;
  real_T b_c;
  real_T mu1_re;
  emlrtPushRtStackR2012b(&bh_emlrtRSI, emlrtRootTLSGlobal);
  y = T->size[0] * T->size[1];
  T->size[0] = Tr->size[0];
  T->size[1] = Tr->size[1];
  emxEnsureCapacity((emxArray__common *)T, y, (int32_T)sizeof(creal_T),
                    &v_emlrtRTEI);
  loop_ub = Tr->size[0] * Tr->size[1];
  for (y = 0; y < loop_ub; y++) {
    T->data[y].re = Tr->data[y];
    T->data[y].im = 0.0;
  }

  y = U->size[0] * U->size[1];
  U->size[0] = Ur->size[0];
  U->size[1] = Ur->size[1];
  emxEnsureCapacity((emxArray__common *)U, y, (int32_T)sizeof(creal_T),
                    &v_emlrtRTEI);
  loop_ub = Ur->size[0] * Ur->size[1];
  for (y = 0; y < loop_ub; y++) {
    U->data[y].re = Ur->data[y];
    U->data[y].im = 0.0;
  }

  for (y = 0; y < 2; y++) {
    varargin_1[y] = (int16_T)Tr->size[y];
  }

  mtmp = varargin_1[0];
  if (varargin_1[1] < varargin_1[0]) {
    mtmp = varargin_1[1];
  }

  for (y = 0; y < 2; y++) {
    varargin_1[y] = (int16_T)Ur->size[y];
  }

  loop_ub = varargin_1[0];
  if (varargin_1[1] < varargin_1[0]) {
    loop_ub = varargin_1[1];
  }

  mtmp = (int32_T)muDoubleScalarMin(mtmp, loop_ub);
  if (mtmp == 0) {
  } else {
    for (m = mtmp - 1; m + 1 >= 2; m--) {
      if (Tr->data[m + Tr->size[0] * (m - 1)] != 0.0) {
        emlrtPushRtStackR2012b(&ch_emlrtRSI, emlrtRootTLSGlobal);
        c = Tr->data[m + Tr->size[0] * (m - 1)];
        b = Tr->data[(m + Tr->size[0] * m) - 1];
        temp = Tr->data[(m + Tr->size[0] * (m - 1)) - 1];
        if (Tr->data[m + Tr->size[0] * (m - 1)] == 0.0) {
        } else if (Tr->data[(m + Tr->size[0] * m) - 1] == 0.0) {
          temp = Tr->data[m + Tr->size[0] * m];
          b = -Tr->data[m + Tr->size[0] * (m - 1)];
          c = 0.0;
        } else if ((Tr->data[(m + Tr->size[0] * (m - 1)) - 1] - Tr->data[m +
                    Tr->size[0] * m] == 0.0) && ((Tr->data[(m + Tr->size[0] * m)
          - 1] < 0.0) != (Tr->data[m + Tr->size[0] * (m - 1)] < 0.0))) {
        } else {
          temp = Tr->data[(m + Tr->size[0] * (m - 1)) - 1] - Tr->data[m +
            Tr->size[0] * m];
          p = 0.5 * temp;
          bcmax = muDoubleScalarMax(muDoubleScalarAbs(Tr->data[(m + Tr->size[0] *
            m) - 1]), muDoubleScalarAbs(Tr->data[m + Tr->size[0] * (m - 1)]));
          if (!(Tr->data[(m + Tr->size[0] * m) - 1] < 0.0)) {
            loop_ub = 1;
          } else {
            loop_ub = -1;
          }

          if (!(Tr->data[m + Tr->size[0] * (m - 1)] < 0.0)) {
            y = 1;
          } else {
            y = -1;
          }

          scale = muDoubleScalarMax(muDoubleScalarAbs(p), bcmax);
          bcmax = p / scale * p + bcmax / scale * (muDoubleScalarMin
            (muDoubleScalarAbs(Tr->data[(m + Tr->size[0] * m) - 1]),
             muDoubleScalarAbs(Tr->data[m + Tr->size[0] * (m - 1)])) * (real_T)
            loop_ub * (real_T)y);
          if (bcmax >= 8.8817841970012523E-16) {
            emlrtPushRtStackR2012b(&gg_emlrtRSI, emlrtRootTLSGlobal);
            bb = muDoubleScalarSqrt(scale) * muDoubleScalarSqrt(bcmax);
            emlrtPopRtStackR2012b(&gg_emlrtRSI, emlrtRootTLSGlobal);
            if (!(p < 0.0)) {
              b_p = bb;
            } else {
              b_p = -bb;
            }

            temp = Tr->data[m + Tr->size[0] * m] + (p + b_p);
            b = Tr->data[(m + Tr->size[0] * m) - 1] - Tr->data[m + Tr->size[0] *
              (m - 1)];
            c = 0.0;
          } else {
            scale = Tr->data[(m + Tr->size[0] * m) - 1] + Tr->data[m + Tr->size
              [0] * (m - 1)];
            bcmax = muDoubleScalarHypot(scale, temp);
            emlrtPushRtStackR2012b(&hg_emlrtRSI, emlrtRootTLSGlobal);
            bb = 0.5 * (1.0 + muDoubleScalarAbs(scale) / bcmax);
            if (bb < 0.0) {
              emlrtPushRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
              eml_error();
              emlrtPopRtStackR2012b(&x_emlrtRSI, emlrtRootTLSGlobal);
            }

            cs = muDoubleScalarSqrt(bb);
            emlrtPopRtStackR2012b(&hg_emlrtRSI, emlrtRootTLSGlobal);
            if (!(scale < 0.0)) {
              b_scale = 1;
            } else {
              b_scale = -1;
            }

            bcmax = -(p / (bcmax * cs)) * (real_T)b_scale;
            scale = Tr->data[(m + Tr->size[0] * (m - 1)) - 1] * cs + Tr->data[(m
              + Tr->size[0] * m) - 1] * bcmax;
            bb = -Tr->data[(m + Tr->size[0] * (m - 1)) - 1] * bcmax + Tr->data
              [(m + Tr->size[0] * m) - 1] * cs;
            p = Tr->data[m + Tr->size[0] * (m - 1)] * cs + Tr->data[m + Tr->
              size[0] * m] * bcmax;
            temp = -Tr->data[m + Tr->size[0] * (m - 1)] * bcmax + Tr->data[m +
              Tr->size[0] * m] * cs;
            b = bb * cs + temp * bcmax;
            c = -scale * bcmax + p * cs;
            temp = 0.5 * ((scale * cs + p * bcmax) + (-bb * bcmax + temp * cs));
            if (c != 0.0) {
              if (b != 0.0) {
                if ((b < 0.0) == (c < 0.0)) {
                  emlrtPushRtStackR2012b(&ig_emlrtRSI, emlrtRootTLSGlobal);
                  emlrtPopRtStackR2012b(&ig_emlrtRSI, emlrtRootTLSGlobal);
                  emlrtPushRtStackR2012b(&jg_emlrtRSI, emlrtRootTLSGlobal);
                  emlrtPopRtStackR2012b(&jg_emlrtRSI, emlrtRootTLSGlobal);
                  bb = muDoubleScalarSqrt(muDoubleScalarAbs(b)) *
                    muDoubleScalarSqrt(muDoubleScalarAbs(c));
                  emlrtPushRtStackR2012b(&kg_emlrtRSI, emlrtRootTLSGlobal);
                  emlrtPopRtStackR2012b(&kg_emlrtRSI, emlrtRootTLSGlobal);
                  if (!(c < 0.0)) {
                    b_c = bb;
                  } else {
                    b_c = -bb;
                  }

                  temp += b_c;
                  b -= c;
                  c = 0.0;
                }
              } else {
                b = -c;
                c = 0.0;
              }
            }
          }
        }

        if (c == 0.0) {
          bcmax = 0.0;
        } else {
          emlrtPushRtStackR2012b(&lg_emlrtRSI, emlrtRootTLSGlobal);
          bcmax = muDoubleScalarSqrt(muDoubleScalarAbs(b)) * muDoubleScalarSqrt
            (muDoubleScalarAbs(c));
          emlrtPopRtStackR2012b(&lg_emlrtRSI, emlrtRootTLSGlobal);
        }

        emlrtPopRtStackR2012b(&ch_emlrtRSI, emlrtRootTLSGlobal);
        mu1_re = temp - Tr->data[m + Tr->size[0] * m];
        scale = muDoubleScalarHypot(muDoubleScalarHypot(mu1_re, bcmax), Tr->
          data[m + Tr->size[0] * (m - 1)]);
        if (bcmax == 0.0) {
          mu1_re /= scale;
          cs = 0.0;
        } else if (mu1_re == 0.0) {
          mu1_re = 0.0;
          cs = bcmax / scale;
        } else {
          mu1_re /= scale;
          cs = bcmax / scale;
        }

        c = Tr->data[m + Tr->size[0] * (m - 1)] / scale;
        emlrtPushRtStackR2012b(&dh_emlrtRSI, emlrtRootTLSGlobal);
        emlrtPopRtStackR2012b(&dh_emlrtRSI, emlrtRootTLSGlobal);
        for (loop_ub = m - 1; loop_ub + 1 <= mtmp; loop_ub++) {
          b = T->data[(m + T->size[0] * loop_ub) - 1].re;
          temp = T->data[(m + T->size[0] * loop_ub) - 1].im;
          bb = T->data[(m + T->size[0] * loop_ub) - 1].re;
          p = T->data[(m + T->size[0] * loop_ub) - 1].im;
          bcmax = T->data[(m + T->size[0] * loop_ub) - 1].im;
          scale = T->data[(m + T->size[0] * loop_ub) - 1].re;
          T->data[(m + T->size[0] * loop_ub) - 1].re = (mu1_re * bb + cs * p) +
            c * T->data[m + T->size[0] * loop_ub].re;
          T->data[(m + T->size[0] * loop_ub) - 1].im = (mu1_re * bcmax - cs *
            scale) + c * T->data[m + T->size[0] * loop_ub].im;
          bcmax = mu1_re * T->data[m + T->size[0] * loop_ub].re - cs * T->data[m
            + T->size[0] * loop_ub].im;
          scale = mu1_re * T->data[m + T->size[0] * loop_ub].im + cs * T->data[m
            + T->size[0] * loop_ub].re;
          T->data[m + T->size[0] * loop_ub].re = bcmax - c * b;
          T->data[m + T->size[0] * loop_ub].im = scale - c * temp;
        }

        emlrtPushRtStackR2012b(&eh_emlrtRSI, emlrtRootTLSGlobal);
        emlrtPopRtStackR2012b(&eh_emlrtRSI, emlrtRootTLSGlobal);
        for (loop_ub = 0; loop_ub + 1 <= m + 1; loop_ub++) {
          b = T->data[loop_ub + T->size[0] * (m - 1)].re;
          temp = T->data[loop_ub + T->size[0] * (m - 1)].im;
          bcmax = mu1_re * T->data[loop_ub + T->size[0] * (m - 1)].re - cs *
            T->data[loop_ub + T->size[0] * (m - 1)].im;
          scale = mu1_re * T->data[loop_ub + T->size[0] * (m - 1)].im + cs *
            T->data[loop_ub + T->size[0] * (m - 1)].re;
          bb = T->data[loop_ub + T->size[0] * m].re;
          p = T->data[loop_ub + T->size[0] * m].im;
          T->data[loop_ub + T->size[0] * (m - 1)].re = bcmax + c * bb;
          T->data[loop_ub + T->size[0] * (m - 1)].im = scale + c * p;
          bb = T->data[loop_ub + T->size[0] * m].re;
          p = T->data[loop_ub + T->size[0] * m].im;
          bcmax = T->data[loop_ub + T->size[0] * m].im;
          scale = T->data[loop_ub + T->size[0] * m].re;
          T->data[loop_ub + T->size[0] * m].re = (mu1_re * bb + cs * p) - c * b;
          T->data[loop_ub + T->size[0] * m].im = (mu1_re * bcmax - cs * scale) -
            c * temp;
        }

        emlrtPushRtStackR2012b(&fh_emlrtRSI, emlrtRootTLSGlobal);
        emlrtPopRtStackR2012b(&fh_emlrtRSI, emlrtRootTLSGlobal);
        for (loop_ub = 0; loop_ub + 1 <= mtmp; loop_ub++) {
          b = U->data[loop_ub + U->size[0] * (m - 1)].re;
          temp = U->data[loop_ub + U->size[0] * (m - 1)].im;
          bcmax = mu1_re * U->data[loop_ub + U->size[0] * (m - 1)].re - cs *
            U->data[loop_ub + U->size[0] * (m - 1)].im;
          scale = mu1_re * U->data[loop_ub + U->size[0] * (m - 1)].im + cs *
            U->data[loop_ub + U->size[0] * (m - 1)].re;
          bb = U->data[loop_ub + U->size[0] * m].re;
          p = U->data[loop_ub + U->size[0] * m].im;
          U->data[loop_ub + U->size[0] * (m - 1)].re = bcmax + c * bb;
          U->data[loop_ub + U->size[0] * (m - 1)].im = scale + c * p;
          bb = U->data[loop_ub + U->size[0] * m].re;
          p = U->data[loop_ub + U->size[0] * m].im;
          bcmax = U->data[loop_ub + U->size[0] * m].im;
          scale = U->data[loop_ub + U->size[0] * m].re;
          U->data[loop_ub + U->size[0] * m].re = (mu1_re * bb + cs * p) - c * b;
          U->data[loop_ub + U->size[0] * m].im = (mu1_re * bcmax - cs * scale) -
            c * temp;
        }

        T->data[m + T->size[0] * (m - 1)].re = 0.0;
        T->data[m + T->size[0] * (m - 1)].im = 0.0;
      }
    }
  }

  emlrtPopRtStackR2012b(&bh_emlrtRSI, emlrtRootTLSGlobal);
}
Пример #6
0
static void sf_c6_TTR_mdl(SFc6_TTR_mdlInstanceStruct *chartInstance)
{
  int32_T c6_i0;
  real_T c6_hoistedGlobal;
  real_T c6_u_ctrl;
  int32_T c6_i1;
  real_T c6_U_bounds[2];
  uint32_T c6_debug_family_var_map[6];
  real_T c6_i;
  real_T c6_nargin = 2.0;
  real_T c6_nargout = 1.0;
  real_T c6_u_ctrl_out;
  real_T c6_varargin_1;
  real_T c6_varargin_2;
  real_T c6_b_varargin_2;
  real_T c6_varargin_3;
  real_T c6_x;
  real_T c6_y;
  real_T c6_b_x;
  real_T c6_b_y;
  real_T c6_xk;
  real_T c6_yk;
  real_T c6_c_x;
  real_T c6_c_y;
  real_T c6_b_varargin_1;
  real_T c6_c_varargin_2;
  real_T c6_d_varargin_2;
  real_T c6_b_varargin_3;
  real_T c6_d_x;
  real_T c6_d_y;
  real_T c6_e_x;
  real_T c6_e_y;
  real_T c6_b_xk;
  real_T c6_b_yk;
  real_T c6_f_x;
  real_T c6_f_y;
  real_T *c6_b_u_ctrl;
  real_T *c6_b_u_ctrl_out;
  real_T (*c6_b_U_bounds)[2];
  c6_b_U_bounds = (real_T (*)[2])ssGetInputPortSignal(chartInstance->S, 1);
  c6_b_u_ctrl_out = (real_T *)ssGetOutputPortSignal(chartInstance->S, 1);
  c6_b_u_ctrl = (real_T *)ssGetInputPortSignal(chartInstance->S, 0);
  _sfTime_ = (real_T)ssGetT(chartInstance->S);
  _SFD_CC_CALL(CHART_ENTER_SFUNCTION_TAG, 5U, chartInstance->c6_sfEvent);
  _SFD_DATA_RANGE_CHECK(*c6_b_u_ctrl, 0U);
  _SFD_DATA_RANGE_CHECK(*c6_b_u_ctrl_out, 1U);
  for (c6_i0 = 0; c6_i0 < 2; c6_i0++) {
    _SFD_DATA_RANGE_CHECK((*c6_b_U_bounds)[c6_i0], 2U);
  }

  chartInstance->c6_sfEvent = CALL_EVENT;
  _SFD_CC_CALL(CHART_ENTER_DURING_FUNCTION_TAG, 5U, chartInstance->c6_sfEvent);
  c6_hoistedGlobal = *c6_b_u_ctrl;
  c6_u_ctrl = c6_hoistedGlobal;
  for (c6_i1 = 0; c6_i1 < 2; c6_i1++) {
    c6_U_bounds[c6_i1] = (*c6_b_U_bounds)[c6_i1];
  }

  _SFD_SYMBOL_SCOPE_PUSH_EML(0U, 6U, 6U, c6_debug_family_names,
    c6_debug_family_var_map);
  _SFD_SYMBOL_SCOPE_ADD_EML(&c6_i, 0U, c6_sf_marshallOut);
  _SFD_SYMBOL_SCOPE_ADD_EML_IMPORTABLE(&c6_nargin, 1U, c6_sf_marshallOut,
    c6_sf_marshallIn);
  _SFD_SYMBOL_SCOPE_ADD_EML_IMPORTABLE(&c6_nargout, 2U, c6_sf_marshallOut,
    c6_sf_marshallIn);
  _SFD_SYMBOL_SCOPE_ADD_EML(&c6_u_ctrl, 3U, c6_sf_marshallOut);
  _SFD_SYMBOL_SCOPE_ADD_EML(c6_U_bounds, 4U, c6_b_sf_marshallOut);
  _SFD_SYMBOL_SCOPE_ADD_EML_IMPORTABLE(&c6_u_ctrl_out, 5U, c6_sf_marshallOut,
    c6_sf_marshallIn);
  CV_EML_FCN(0, 0);
  _SFD_EML_CALL(0U, chartInstance->c6_sfEvent, 9);
  c6_u_ctrl_out = c6_u_ctrl;
  _SFD_EML_CALL(0U, chartInstance->c6_sfEvent, 11);
  CV_EML_IF(0, 1, 0, FALSE);
  _SFD_EML_CALL(0U, chartInstance->c6_sfEvent, 17);
  c6_i = 1.0;
  CV_EML_FOR(0, 1, 0, 1);
  _SFD_EML_CALL(0U, chartInstance->c6_sfEvent, 18);
  c6_varargin_1 = c6_u_ctrl;
  c6_varargin_2 = c6_U_bounds[0];
  c6_b_varargin_2 = c6_varargin_1;
  c6_varargin_3 = c6_varargin_2;
  c6_x = c6_b_varargin_2;
  c6_y = c6_varargin_3;
  c6_b_x = c6_x;
  c6_b_y = c6_y;
  c6_eml_scalar_eg(chartInstance);
  c6_xk = c6_b_x;
  c6_yk = c6_b_y;
  c6_c_x = c6_xk;
  c6_c_y = c6_yk;
  c6_eml_scalar_eg(chartInstance);
  c6_u_ctrl_out = muDoubleScalarMax(c6_c_x, c6_c_y);
  _SFD_EML_CALL(0U, chartInstance->c6_sfEvent, 19);
  c6_b_varargin_1 = c6_u_ctrl;
  c6_c_varargin_2 = c6_U_bounds[1];
  c6_d_varargin_2 = c6_b_varargin_1;
  c6_b_varargin_3 = c6_c_varargin_2;
  c6_d_x = c6_d_varargin_2;
  c6_d_y = c6_b_varargin_3;
  c6_e_x = c6_d_x;
  c6_e_y = c6_d_y;
  c6_eml_scalar_eg(chartInstance);
  c6_b_xk = c6_e_x;
  c6_b_yk = c6_e_y;
  c6_f_x = c6_b_xk;
  c6_f_y = c6_b_yk;
  c6_eml_scalar_eg(chartInstance);
  c6_u_ctrl_out = muDoubleScalarMin(c6_f_x, c6_f_y);
  CV_EML_FOR(0, 1, 0, 0);
  _SFD_EML_CALL(0U, chartInstance->c6_sfEvent, -19);
  _SFD_SYMBOL_SCOPE_POP();
  *c6_b_u_ctrl_out = c6_u_ctrl_out;
  _SFD_CC_CALL(EXIT_OUT_OF_FUNCTION_TAG, 5U, chartInstance->c6_sfEvent);
  _SFD_CHECK_FOR_STATE_INCONSISTENCY(_TTR_mdlMachineNumber_,
    chartInstance->chartNumber, chartInstance->instanceNumber);
}
Пример #7
0
/* Function Definitions */
void clcPMP_olyHyb_tmp(const emlrtStack *sp, real_T engKinPre, real_T engKinAct,
  real_T gea, real_T slp, real_T batEng, real_T psiBatEng, real_T psiTim, real_T
  batPwrAux, real_T batEngStp, real_T wayStp, const struct0_T *par, real_T
  *cosHamMin, real_T *batFrcOut, real_T *fulFrcOut)
{
  real_T mtmp;
  real_T vehVel;
  real_T b_engKinPre[2];
  real_T crsSpdVec[2];
  int32_T i18;
  int32_T k;
  boolean_T y;
  boolean_T exitg3;
  boolean_T exitg2;
  real_T crsSpd;
  real_T whlTrq;
  real_T crsTrq;
  real_T iceTrqMax;
  real_T iceTrqMin;
  real_T b_par[100];
  real_T emoTrqMaxPos;
  real_T emoTrqMinPos;
  real_T emoTrqMax;
  real_T emoTrqMin;
  real_T batPwrMax;
  real_T batPwrMin;
  real_T batOcv;
  real_T batEngDltMin;
  real_T batEngDltMax;
  real_T batEngDltMinInx;
  real_T batEngDltMaxInx;
  real_T batEngDlt;
  real_T fulFrc;
  real_T batFrc;
  real_T b_batFrc;
  real_T batPwr;
  real_T emoTrq;
  real_T iceTrq;
  real_T fulPwr;
  int32_T ixstart;
  int32_T itmp;
  int32_T ix;
  boolean_T exitg1;
  emlrtStack st;
  emlrtStack b_st;
  st.prev = sp;
  st.tls = sp->tls;
  b_st.prev = &st;
  b_st.tls = st.tls;

  /* CLCPMP Minimizing Hamiltonian: Co-States for soc and time */
  /*  Erstellungsdatum der ersten Version 19.08.2015 - Stephan Uebel */
  /*  */
  /*  Batterieleistungsgrenzen hinzugefügt am 13.12.2015 */
  /*  ^^added battery power limit */
  /*  */
  /*  Massenaufschlag durch Trägheitsmoment herausgenommen */
  /*  ^^Mass increment removed by inertia */
  /*  */
  /* % Inputdefinition */
  /*  */
  /*  engKinPre     - Double(1,1)  - kinetische Energie am Intervallanfang in J */
  /*                                 ^^ kinetic energy at start of interval (J) */
  /*  engKinAct     - Double(1,1)  - kinetische Energie am Intervallende in J */
  /*                                 ^^ kinetic energe at end of interval (J) */
  /*  gea           - Double(1,1)  - Gang */
  /*                                 ^^ gear */
  /*  slp           - Double(1,1)  - Steigung in rad */
  /*                                 ^^ slope in radians */
  /*  iceFlg        - Boolean(1,1) - Flag für Motorzustand */
  /*                                 ^^ flag for motor condition */
  /*  batEng        - Double(1,1)  - Batterieenergie in J */
  /*                                 ^^ battery energy (J) */
  /*  psibatEng     - Double(1,1)  - Costate für Batterieenergie ohne Einheit */
  /*                                 ^^ costate for battery energy w/o unity */
  /*  psiTim        - Double(1,1)  - Costate für die Zeit ohne Einheit */
  /*                                 ^^ costate for time without unity */
  /*  batPwrAux     - Double(1,1)  - elektr. Nebenverbraucherleistung in W */
  /*                                 ^^ electric auxiliary power consumed (W) */
  /*  batEngStp     - Double(1,1)  - Drehmomentschritt */
  /*                                 ^^ torque step <- no, it's a battery step */
  /*  wayStp        - Double(1,1)  - Intervallschrittweite in m */
  /*                                 ^^ interval step distance (m) */
  /*  par           - Struct(1,1)  - Modelldaten */
  /*                                 ^^ model data */
  /* % Initialisieren der Ausgabe der Funktion */
  /*    initializing function output */
  /*  Ausgabewert des Minimums der Hamiltonfunktion */
  /*    output for minimizing the hamiltonian */
  *cosHamMin = rtInf;

  /*  Batterieladungsänderung im Wegschritt beir minimaler Hamiltonfunktion */
  /*    battery change in path_idx step with the minial hamiltonian */
  *batFrcOut = rtInf;

  /*  Kraftstoffkraftänderung im Wegschritt bei minimaler Hamiltonfunktion */
  /*    fuel change in path_idx step with the minimal hamiltonian */
  *fulFrcOut = 0.0;

  /* % Initialisieren der persistent Größen */
  /*    initialize the persistance variables */
  /*  Diese werden die nur einmal für die Funktion berechnet */
  /*    only calculated once for the function */
  if (!crsSpdHybMax_not_empty) {
    /*  maximale Drehzahl Elektrommotor */
    /*    maximum electric motor rotational speed */
    /*  maximale Drehzahl der Kurbelwelle */
    /*    maximum crankshaft rotational speed */
    crsSpdHybMax = muDoubleScalarMin(par->iceSpdMgd[14850], par->emoSpdMgd[14850]);
    crsSpdHybMax_not_empty = true;

    /*  minimale Drehzahl der Kurbelwelle */
    /*    minimum crankshaft rotational speed */
    crsSpdHybMin = par->iceSpdMgd[0];
  }

  /* % Initialisieren der allgemein benötigten Kenngrößen */
  /*    initializing the commonly required parameters */
  /*  mittlere kinetische Energie im Wegschritt berechnen */
  /*    define the average kinetic energy at path_idx step - is just previous KE */
  /*  mittlere Geschwindigkeit im Wegschritt berechnen */
  /*    define the average speed at path_idx step */
  mtmp = 2.0 * engKinPre / par->vehMas;
  st.site = &g_emlrtRSI;
  if (mtmp < 0.0) {
    b_st.site = &h_emlrtRSI;
    eml_error(&b_st);
  }

  vehVel = muDoubleScalarSqrt(mtmp);

  /* % vorzeitiger Funktionsabbruch? */
  /*    premature function termination? */
  /*  Drehzahl der Kurbelwelle und Grenzen */
  /*    crankshaft speed and limits */
  /*  Aus den kinetischen Energien des Fahrzeugs wird über die Raddrehzahl */
  /*  und die übersetzung vom Getriebe die Kurbelwellendrehzahl berechnet. */
  /*  Zeilenrichtung entspricht den Gängen. (Zeilenvektor) */
  /*    from the vehicle's kinetic energy, the crankshaft speed is calculated */
  /*    by the speed and gearbox translation. Line direction corresponding to */
  /*    the aisles (row rector). EQUATION 1 */
  b_engKinPre[0] = engKinPre;
  b_engKinPre[1] = engKinAct;
  for (i18 = 0; i18 < 2; i18++) {
    crsSpdVec[i18] = 2.0 * b_engKinPre[i18] / par->vehMas;
  }

  st.site = &f_emlrtRSI;
  for (k = 0; k < 2; k++) {
    if (crsSpdVec[k] < 0.0) {
      b_st.site = &h_emlrtRSI;
      eml_error(&b_st);
    }
  }

  for (k = 0; k < 2; k++) {
    crsSpdVec[k] = muDoubleScalarSqrt(crsSpdVec[k]);
  }

  i18 = par->geaRat->size[1];
  k = (int32_T)gea;
  emlrtDynamicBoundsCheckR2012b(k, 1, i18, &mb_emlrtBCI, sp);
  mtmp = par->geaRat->data[(int32_T)gea - 1];
  for (i18 = 0; i18 < 2; i18++) {
    crsSpdVec[i18] = mtmp * crsSpdVec[i18] / par->whlDrr;
  }

  /*  Abbruch, wenn die Drehzahlen der Kurbelwelle zu hoch im hybridischen */
  /*  Modus */
  /*    stop if the crankshaft rotatoinal speed is too high in hybrid mode */
  y = false;
  k = 0;
  exitg3 = false;
  while ((!exitg3) && (k < 2)) {
    if (!!(crsSpdVec[k] > crsSpdHybMax)) {
      y = true;
      exitg3 = true;
    } else {
      k++;
    }
  }

  if (y) {
  } else {
    /*  Falls die Drehzahl des Verbrennungsmotors niedriger als die */
    /*  Leerlaufdrehzahl ist, */
    /*    stop if crankhaft rotional speed is lower than the idling speed */
    y = false;
    k = 0;
    exitg2 = false;
    while ((!exitg2) && (k < 2)) {
      if (!!(crsSpdVec[k] < crsSpdHybMin)) {
        y = true;
        exitg2 = true;
      } else {
        k++;
      }
    }

    if (y) {
    } else {
      /*  Prüfen, ob die Drehzahlgrenze des Elektromotors eingehalten wird */
      /*    check if electric motor speed limit is maintained */
      /*  mittlere Kurbelwellendrehzahlen berechnen */
      /*    calculate average crankshaft rotational speed */
      /*    - really just selecting the previous path_idx KE crankshaft speed */
      crsSpd = crsSpdVec[0];

      /* % Längsdynamik berechnen */
      /*    calculate longitundinal dynamics */
      /*  Es wird eine konstante Beschleunigung angenommen, die im Wegschritt */
      /*  wayStp das Fahrzeug von velPre auf velAct beschleunigt. */
      /*    constant acceleration assumed when transitioning from velPre to velAct */
      /*    for the selected wayStp path_idx step distance */
      /*  Berechnen der konstanten Beschleunigung */
      /*    calculate the constant acceleration */
      /*  Aus der mittleren kinetischen Energie im Intervall, der mittleren */
      /*  Steigung und dem Gang lässt sich über die Fahrwiderstandsgleichung */
      /*  die nötige Fahrwiderstandskraft berechnen, die aufgebracht werden */
      /*  muss, um diese zu realisieren. */
      /*    from the (avg) kinetic energy in the interval, the (avg) slope and */
      /*    transition can calculate the necessary traction force on the driving */
      /*    resistance equation (PART OF EQUATION 5) */
      /*  Steigungskraft aus der mittleren Steigung berechnen (Skalar) */
      /*    gradiant force from the calculated (average) gradient */
      /*  Rollreibungskraft berechnen (Skalar) */
      /*    calculated rolling friction force - not included in EQ 5??? */
      /*  Luftwiderstandskraft berechnen (2*c_a/m * E_kin) (Skalar)  */
      /*    calculated air resistance force  */
      /* % Berechnung der minimalen kosten der Hamiltonfunktion */
      /*    Calculating the minimum cost of the Hamiltonian */
      /* % Berechnen der Kraft am Rad für Antriebsstrangmodus */
      /*    calculate the force on the wheel for the drivetrain mode */
      /*  % dynamische Fahrzeugmasse bei Fahrzeugmotor an berechnen. Das */
      /*  % heißt es werden Trägheitsmoment von Verbrennungsmotor, */
      /*  % Elektromotor und Rädern mit einbezogen. */
      /*    calculate dynamic vehicle mass with the vehicle engine (with the moment */
      /*    of intertia of the ICE, electric motor, and wheels) */
      /*  vehMasDyn = (par.iceMoi_geaRat(gea) +... */
      /*      par.emoGeaMoi_geaRat(gea) + par.whlMoi)/par.whlDrr^2 ... */
      /*      + par.vehMas; */
      /*  Radkraft berechnen (Beschleunigungskraft + Steigungskraft + */
      /*  Rollwiderstandskraft + Luftwiderstandskraft) */
      /*    caluclating wheel forces (accerlation force + gradient force + rolling */
      /*    resistance + air resistance)    EQUATION 5 */
      /* % Getriebeübersetzung und -verlust */
      /*    gear ratio and loss */
      /*  Das Drehmoment des Rades ergibt sich über den Radhalbmesser aus */
      /*  der Fahrwiderstandskraft. */
      /*    the weel torque is obtained from the wheel radius of the rolling */
      /*    resistance force (torque = force * distance (in this case, radius) */
      whlTrq = ((((engKinAct - engKinPre) / (par->vehMas * wayStp) * par->vehMas
                  + par->vehMas * 9.81 * muDoubleScalarSin(slp)) +
                 par->whlRolResCof * par->vehMas * 9.81 * muDoubleScalarCos(slp))
                + 2.0 * par->drgCof / par->vehMas * engKinPre) * par->whlDrr;

      /*  Berechnung des Kurbelwellenmoments */
      /*  Hier muss unterschieden werden, ob das Radmoment positiv oder */
      /*  negativ ist, da nur ein einfacher Wirkungsgrad für das Getriebe */
      /*  genutzt wird */
      /*    it's important to determine sign of crankshaft torque (positive or */
      /*    negative), since only a simple efficiency is used for the transmission */
      /*    PART OF EQ4  <- perhaps reversed? not too sure */
      if (whlTrq < 0.0) {
        i18 = par->geaRat->size[1];
        k = (int32_T)gea;
        emlrtDynamicBoundsCheckR2012b(k, 1, i18, &nb_emlrtBCI, sp);
        crsTrq = whlTrq / par->geaRat->data[(int32_T)gea - 1] * par->geaEfy;
      } else {
        i18 = par->geaRat->size[1];
        k = (int32_T)gea;
        emlrtDynamicBoundsCheckR2012b(k, 1, i18, &ob_emlrtBCI, sp);
        crsTrq = whlTrq / par->geaRat->data[(int32_T)gea - 1] / par->geaEfy;
      }

      /* % Verbrennungsmotor */
      /*    internal combustion engine */
      /*  maximales Moment des Verbrennungsmotors berechnen */
      /*    calculate max torque of the engine (quadratic based on rotation speed) */
      iceTrqMax = (par->iceTrqMaxCof[0] * (crsSpdVec[0] * crsSpdVec[0]) +
                   par->iceTrqMaxCof[1] * crsSpdVec[0]) + par->iceTrqMaxCof[2];

      /*  minimales Moment des Verbrennungsmotors berechnen */
      /*    calculating mimimum ICE moment */
      iceTrqMin = (par->iceTrqMinCof[0] * (crsSpdVec[0] * crsSpdVec[0]) +
                   par->iceTrqMinCof[1] * crsSpdVec[0]) + par->iceTrqMinCof[2];

      /* % Elektromotor */
      /*    electric motor */
      /*  maximales Moment, dass die E-Maschine liefern kann */
      /*    max torque that the electric motor can provide - from interpolation */
      /*  emoTrqMaxPos = ... */
      /*      lininterp1(par.emoSpdMgd(1,:)',par.emoTrqMax_emoSpd,crsSpd); */
      for (i18 = 0; i18 < 100; i18++) {
        b_par[i18] = par->emoSpdMgd[150 * i18];
      }

      emoTrqMaxPos = interp1q(b_par, par->emoTrqMax_emoSpd, crsSpdVec[0]);

      /*  Die gültigen Kurbelwellenmomente müssen kleiner sein als das */
      /*  Gesamtmoment von E-Motor und Verbrennungsmotor */
      /*    The valid crankshaft moments must be less than the total moment of the */
      /*    electric motor and the ICE.Otherwise, leave the function */
      if (crsTrq > iceTrqMax + emoTrqMaxPos) {
      } else {
        /* % %% Optimaler Momentensplit - Minimierung der Hamiltonfunktion */
        /*        optimum torque split - minimizing the Hamiltonian */
        /*  Die Vorgehensweise ist ähnlich wie bei der ECMS. Es wird ein Vektor der */
        /*  möglichen Batterieenergieänderungen aufgestellt. Aus diesen lässt sich  */
        /*  eine Batterieklemmleistung berechnen. Aus der über das */
        /*  Kurbelwellenmoment, ein Elektromotormoment berechnet werden kann. */
        /*  Über das geforderte Kurbelwellenmoment, kann für jedes Moment des  */
        /*  Elektromotors ein Moment des Verbrennungsmotors gefunden werden. Für  */
        /*  jedes Momentenpaar kann die Hamiltonfunktion berechnet werden. */
        /*  Ausgegeben wird der minimale Wert der Hamiltonfunktion und die */
        /*  durch das dabei verwendete Elektromotormoment verursachte */
        /*  Batterieladungsänderung. */
        /*    The procedure is similar to ECMS. It's based on a vector of possible */
        /*    battery energy changes, from which a battery terminal power can be */
        /*    calculated. */
        /*    From the crankshaft torque, an electric motor torque can be */
        /*    calculated, and an engine torque can be found for every electric motor */
        /*    moment.  */
        /*    For every moment-pair the Hamiltonian can be calculated. It */
        /*    outputs the minimum Hamilotnian value and the battery charge change */
        /*    caused by the electric motor torque used. */
        /* % Elektromotor - Aufstellen des Batterienergievektors */
        /*    electric motor - positioning the battery energy vectors */
        if (batEngStp > 0.0) {
          /* Skalar - änderung der minimalen Batterieenergieänderung */
          /*  Skalar - änderung der maximalen Batterieenergieänderung */
          /*  FUNCTION CALL */
          /*       Skalar - Wegschrittweite */
          /*       Skalar - mittlere Geschwindigkeit im Intervall */
          /*    Skalar - Nebenverbraucherlast */
          /*       Skalar - Batterieenergie */
          /*          struct - Fahrzeugparameter */
          /*       Skalar - crankshaft rotational speed */
          /*       Skalar - crankshaft torque */
          /*    Skalar - min ICE torque allowed */
          /*    Skalar - max ICE torque allowed */
          /*  Skalar - max EM torque possible */
          st.site = &e_emlrtRSI;

          /* Skalar - änderung der minimalen Batterieenergieänderung */
          /*  Skalar - änderung der maximalen Batterieenergieänderung */
          /*       Skalar - Wegschrittweite */
          /*          Skalar - Geschwindigkeit im Intervall */
          /*    Skalar - Nebenverbraucherlast */
          /*    Skalar - Batterieenergie */
          /*          struct - Fahrzeugparameter */
          /*       Skalar - crankshaft rotational speed */
          /*       Skalar - crankshaft torque */
          /*    Skalar - min ICE torque allowed */
          /*    Skalar - max ICE torque */
          /*  Skalar - max EM torque possible */
          /* BatEngDltClc Calculates the change in battery energy */
          /*  */
          /*  Erstellungsdatum der ersten Version 17.11.2015 - Stephan Uebel */
          /*    Berechnung der Verluste des Elektromotors bei voller rein elektrischer */
          /*    Fahrt (voller Lastpunktabsenkung) und bei voller Lastpunktanhebung */
          /*        Calculations of loss of electric motor at purely full electric */
          /*        Driving (full load point lowering) and at full load point raising */
          /*  */
          /*  Version vom 17.02.2016: Keine Einbeziehung von Rotationsmassen */
          /*                          ^^ No inclusion of rotational masses */
          /*  */
          /*  Version vom 25.05.2016: Zero-Order-Hold (keine mittlere Geschwindigkeit) */
          /*                          ^^ Zero-Order-Hold (no average velocities) */
          /* % Initialisieren der Ausgabe der Funktion */
          /*    initializing the function output (delta battery_energy min and max) */
          /* % Elektromotor */
          /*  minimales Moment, dass die E-Maschine liefern kann */
          /*    minimum moment that the EM can provide (max is an input to function) */
          /*  emoTrqMinPos = ... */
          /*      lininterp1(par.emoSpdMgd(1,:)',par.emoTrqMin_emoSpd,crsSpd); */
          for (i18 = 0; i18 < 100; i18++) {
            b_par[i18] = par->emoSpdMgd[150 * i18];
          }

          emoTrqMinPos = interp1q(b_par, par->emoTrqMin_emoSpd, crsSpdVec[0]);

          /* % Verbrennungsmotor berechnen */
          /*  Durch EM zu lieferndes Kurbelwellenmoment */
          /*    crankshaft torque to be delivered by the electric motor (min and max) */
          emoTrqMax = crsTrq - iceTrqMin;
          emoTrqMin = crsTrq - iceTrqMax;

          /* % Elektromotor berechnen */
          /*    calculate the electric motor */
          if (emoTrqMaxPos < emoTrqMax) {
            /*  Moment des Elektromotors bei maximaler Entladung der Batterie */
            /*    EM torque at max battery discharge */
            emoTrqMax = emoTrqMaxPos;
          }

          if (emoTrqMaxPos < emoTrqMin) {
            /*  Moment des Elektromotors bei minimaler Entladung der Batterie */
            /*    EM torque at min battery discharge */
            emoTrqMin = emoTrqMaxPos;
          }

          emoTrqMax = muDoubleScalarMax(emoTrqMinPos, emoTrqMax);
          emoTrqMin = muDoubleScalarMax(emoTrqMinPos, emoTrqMin);

          /* % Berechnung der änderung der Batterieladung */
          /*    calculating the change in battery charge */
          /*  Interpolation der benötigten Batterieklemmleistung für das */
          /*  EM-Moment. Stellen die nicht definiert sind, werden mit inf */
          /*  ausgegeben. Positive Werte entsprechen entladen der Batterie. */
          /*    interpolating the required battery terminal power for the EM torque. */
          /*    Assign undefined values to inf. Positive values coresspond with battery */
          /*    discharge. */
          /*  batPwrMax = lininterp2(par.emoSpdMgd(1,:),par.emoTrqMgd(:,1),... */
          /*      par.emoPwr_emoSpd_emoTrq',crsSpd,emoTrqMax) + batPwrAux; */
          /*   */
          /*  batPwrMin = lininterp2(par.emoSpdMgd(1,:),par.emoTrqMgd(:,1),... */
          /*      par.emoPwr_emoSpd_emoTrq',crsSpd,emoTrqMin) + batPwrAux; */
          b_st.site = &i_emlrtRSI;
          batPwrMax = codegen_interp2(&b_st, par->emoSpdMgd, par->emoTrqMgd,
            par->emoPwr_emoSpd_emoTrq, crsSpdVec[0], emoTrqMax) + batPwrAux;
          b_st.site = &j_emlrtRSI;
          batPwrMin = codegen_interp2(&b_st, par->emoSpdMgd, par->emoTrqMgd,
            par->emoPwr_emoSpd_emoTrq, crsSpdVec[0], emoTrqMin) + batPwrAux;

          /*  überprüfen, ob Batterieleistung möglich */
          /*    make sure that current battery max power is not above bat max bounds */
          if (batPwrMax > par->batPwrMax) {
            batPwrMax = par->batPwrMax;
          }

          /*  überprüfen, ob Batterieleistung möglich */
          /*    make sure that current battery min power is not below bat min bounds */
          if (batPwrMin > par->batPwrMax) {
            batPwrMin = par->batPwrMax;
          }

          /*  Es kann vorkommen, dass mehr Leistung gespeist werden soll, als */
          /*  möglich ist. */
          /*    double check that the max and min still remain within the other bounds */
          if (batPwrMax < par->batPwrMin) {
            batPwrMax = par->batPwrMin;
          }

          if (batPwrMin < par->batPwrMin) {
            batPwrMin = par->batPwrMin;
          }

          /*  Batteriespannung aus Kennkurve berechnen */
          /*    calculating battery voltage of characteristic curve - eq?-------------- */
          batOcv = batEng * par->batOcvCof_batEng[0] + par->batOcvCof_batEng[1];

          /*  FUNCTION CALL - min delta bat.energy */
          /*            Skalar - Batterieklemmleistung */
          /*                  Skalar - mittlere Geschwindigkeit im Intervall */
          /*        Skalar - Entladewiderstand */
          /*        Skalar - Ladewiderstand */
          /*                Skalar - battery open-circuit voltage */
          batEngDltMin = batFrcClc_tmp(batPwrMax, vehVel, par->batRstDch,
            par->batRstChr, batOcv) * wayStp;

          /*  <-multiply by delta_S */
          /*  FUNCTION CALL - max delta bat.energy */
          /*            Skalar - Batterieklemmleistung */
          /*                  Skalar - mittlere Geschwindigkeit im Intervall */
          /*        Skalar - Entladewiderstand */
          /*        Skalar - Ladewiderstand */
          /*                Skalar - battery open-circuit voltage */
          batEngDltMax = batFrcClc_tmp(batPwrMin, vehVel, par->batRstDch,
            par->batRstChr, batOcv) * wayStp;

          /*  Es werden 2 Fälle unterschieden: */
          /*    there are 2 different cases */
          if ((batEngDltMin > 0.0) && (batEngDltMax > 0.0)) {
            /*         %% konventionelles Bremsen + Rekuperieren */
            /*    conventional brakes + recuperation */
            /*  */
            /*  set change in energy to discretized integer values w/ ceil and */
            /*  floor. This also helps for easy looping */
            /*  Konventionelles Bremsen wird ebenfalls untersucht. */
            /*  Hier liegt eventuell noch Beschleunigungspotential, da diese */
            /*  Zustandswechsel u.U. ausgeschlossen werden können. */
            /*  NOTE: u.U. = unter Ümständen = circumstances permitting */
            /*    convetional breaks also investigated. An accelerating potential */
            /*    is still possible, as these states can be excluded */
            /*    (circumstances permitting)  <- ??? not sure what above means */
            /*  */
            /*    so if both min and max changes in battery energy are */
            /*    increasing, then set the delta min to zero */
            batEngDltMinInx = 0.0;
            batEngDltMaxInx = muDoubleScalarFloor(batEngDltMax / batEngStp);
          } else {
            batEngDltMinInx = muDoubleScalarCeil(batEngDltMin / batEngStp);
            batEngDltMaxInx = muDoubleScalarFloor(batEngDltMax / batEngStp);
          }
        } else {
          batEngDltMinInx = 0.0;
          batEngDltMaxInx = 0.0;
        }

        /*  you got a larger min chnage and a max change, you're out of bounds. Leave */
        /*  the function */
        if (batEngDltMaxInx < batEngDltMinInx) {
        } else {
          /* % Schleife über alle Elektromotormomente */
          /*    now loop through all the electric-motor torques */
          i18 = (int32_T)(batEngDltMaxInx + (1.0 - batEngDltMinInx));
          emlrtForLoopVectorCheckR2012b(batEngDltMinInx, 1.0, batEngDltMaxInx,
            mxDOUBLE_CLASS, i18, &o_emlrtRTEI, sp);
          k = 0;
          while (k <= i18 - 1) {
            batEngDlt = (batEngDltMinInx + (real_T)k) * batEngStp;

            /*  open circuit voltage over each iteration */
            batOcv = (batEng + batEngDlt) * par->batOcvCof_batEng[0] +
              par->batOcvCof_batEng[1];

            /*           Skalar für die Batterieleistung in W */
            /*           Skalar Krafstoffkraft in N */
            /*             FUNCTION CALL */
            /*          Skalar für die Wegschrittweite in m, */
            /*           Skalar - vehicular velocity */
            /*        Nebenverbraucherlast */
            /*           Skalar - battery open circuit voltage */
            /*       Skalar - Batterieenergie�nderung, */
            /*           Skalar - crankshaft speed at given path_idx */
            /*           Skalar - crankshaft torque at given path_idx */
            /*        Skalar - min ICE torque allowed */
            /*        Skalar - max ICE torque */
            /*               struct der Fahrzeugparameter */
            st.site = &d_emlrtRSI;

            /*   Skalar für die Batterieleistung */
            /*       Skalar Kraftstoffkraft */
            /*      Skalar für die Wegschrittweite in m */
            /*          vehicular velocity */
            /*    Nebenverbraucherlast */
            /*       Skalar - battery open circuit voltage */
            /*   Skalar - Batterieenergieänderung */
            /*       Skalar - crankshaft speed at given path_idx */
            /*       Skalar - crankshaft torque at given path_idx */
            /*    Skalar - min ICE torque allowed */
            /*    Skalar - max ICE torque */
            /*           struct der Fahrzeugparameter */
            /*  */
            /* FULENGCLC Calculating fuel consumption */
            /*  Erstellungsdatum der ersten Version 04.09.2015 - Stephan Uebel */
            /*  */
            /*  Diese Funktion berechnet den Kraftstoffverbrauch für einen gegebenen */
            /*  Wegschritt der kinetischen Energie, der Batterieenergie und des */
            /*  Antriebsstrangzustands über dem Weg. */
            /*    this function calculates fuel consumption for a given route step of */
            /*    KE, the battery energy, and drivetrain state of the current path_idx */
            /*  */
            /*  Version vom 17.02.2016 : Rotationsmassen vernachlässigt */
            /*                            ^^ neglected rotatary masses */
            /*  */
            /*  Version vom 25.05.2016: Zero-Order-Hold (keine mittlere Geschwindigkeit) */
            /*  */
            /*  version from 1.06.2016 - removed crsTrq calulations - they are already */
            /*  done in parent function (clcPMP_olHyb_tmp) and do not change w/ each */
            /*  iteration, making the caluclation here unnecessary */
            /* % Initialisieren der Ausgabe der Funktion */
            /*    initializing function output */
            /*    Skalar - electric battery power (W) */
            fulFrc = rtInf;

            /*    Skalar - fuel force intialization (N) */
            /* % Batterie */
            /*  Batterieenergieänderung über dem Weg (Batteriekraft) */
            /*    Change in battery energy over the path_idx way (ie battery power) */
            batFrc = batEngDlt / wayStp;

            /*  Fallunterscheidung, ob Batterie geladen oder entladen wird */
            /*    Case analysis - check if battery is charging or discharging. Set */
            /*    resistance accordingly */
            /*  elektrische Leistung des Elektromotors */
            /*    electric power from electric motor - DERIVATION? dunno */
            /*  innere Batterieleistung / internal batt power */
            /* dissipat. Leist. / dissipated */
            if (batFrc < 0.0) {
              b_batFrc = par->batRstDch;
            } else {
              b_batFrc = par->batRstChr;
            }

            batPwr = (-batFrc * vehVel - batFrc * batFrc * (vehVel * vehVel) /
                      (batOcv * batOcv) * b_batFrc) - batPwrAux;

            /*           Nebenverbrauchlast / auxiliary power */
            /* % Elektromotor */
            /*  Ermitteln des Kurbelwellenmoments durch EM aus Batterieleistung */
            /*    determine crankshaft torque cauesd by EM's battery power */
            /*        switching out codegen_interp2 for lininterp2-just might work! */
            /*  */
            b_st.site = &k_emlrtRSI;
            emoTrq = codegen_interp2(&b_st, par->emoSpdMgd, par->emoPwrMgd,
              par->emoTrq_emoSpd_emoPwr, crsSpd, batPwr);

            /*  emoTrq = lininterp2(par.emoSpdMgd(1,:), par.emoPwrMgd(:,1),... */
            /*      par.emoTrq_emoSpd_emoPwr',crsSpd,emoPwrEle); */
            if (muDoubleScalarIsInf(emoTrq)) {
            } else {
              /*  Grenzen des Elektromotors müssen hier nicht überprüft werden, da die */
              /*  Ladungsdeltas schon so gewählt wurden, dass das maximale Motormoment */
              /*  nicht überstiegen wird. */
              /*    Electric motor limits need not be checked here, since the charge deltas */
              /*    have been chosen so that the max torque is not exceeded. */
              /* % Verbrennungsmotor */
              /*  Ermitteln des Kurbelwellenmoments durch den Verbrennungsmotor */
              /*    Determining the crankshaft moment from the ICE */
              iceTrq = crsTrq - emoTrq;

              /*  Wenn das Verbrennungsmotormoment kleiner als das minimale */
              /*  Moment ist, ist dieser in der Schubabschaltung. Das */
              /*  verbleibende Moment liefern die Bremsen */
              /*    If engine torque is less than the min torque, fuel is cut. The */
              /*    remaining torque is deliver the brakes. */
              if (iceTrq < iceTrqMin) {
                fulPwr = 0.0;
              } else if (iceTrq > iceTrqMax) {
                fulPwr = rtInf;
              } else {
                /*  replacing another coden_interp2 */
                b_st.site = &l_emlrtRSI;
                fulPwr = codegen_interp2(&b_st, par->iceSpdMgd, par->iceTrqMgd,
                  par->iceFulPwr_iceSpd_iceTrq, crsSpd, iceTrq);

                /*      fulPwr = lininterp2(par.iceSpdMgd(1,:), par.iceTrqMgd(:,1), ... */
                /*          par.iceFulPwr_iceSpd_iceTrq', crsSpd, iceTrq); */
              }

              /*  Berechnen der Kraftstoffkraft */
              /*    calculate fuel force */
              fulFrc = fulPwr / vehVel;

              /*  Berechnen der Kraftstoffvolumenänderung */
              /*  caluclate change in fuel volume - energy, no? */
              /* % Ende der Funktion */
            }

            /*       FUNCTION CALL */
            /*           Skalar - Batterieklemmleistung */
            /*           Skalar - mittlere Geschwindigkeit im Intervall */
            /*    Skalar - Entladewiderstand */
            /*    Skalar - Ladewiderstand */
            /*            Skalar - battery open circuit voltage */
            batFrc = batFrcClc_tmp(batPwr, vehVel, par->batRstDch,
              par->batRstChr, batOcv);

            /*     %% Hamiltonfunktion bestimmen */
            /*    determine the hamiltonian */
            /*  Eq (29b) */
            crsSpdVec[0] = (fulFrc + psiBatEng * batFrc) + psiTim / vehVel;
            ixstart = 1;
            mtmp = crsSpdVec[0];
            itmp = 1;
            if (muDoubleScalarIsNaN(crsSpdVec[0])) {
              ix = 2;
              exitg1 = false;
              while ((!exitg1) && (ix < 3)) {
                ixstart = 2;
                if (!muDoubleScalarIsNaN(*cosHamMin)) {
                  mtmp = *cosHamMin;
                  itmp = 2;
                  exitg1 = true;
                } else {
                  ix = 3;
                }
              }
            }

            if ((ixstart < 2) && (*cosHamMin < mtmp)) {
              mtmp = *cosHamMin;
              itmp = 2;
            }

            *cosHamMin = mtmp;

            /*  Wenn der aktuelle Punkt besser ist, als der in cosHamMin */
            /*  gespeicherte Wert, werden die Ausgabegrößen neu beschrieben. */
            /*    if the current point is better than the stored cosHamMin value, */
            /*    then the output values are rewritten (selected prev. value is = 2) */
            if (itmp == 1) {
              *batFrcOut = batFrc;
              *fulFrcOut = fulFrc;
            }

            k++;
            if (*emlrtBreakCheckR2012bFlagVar != 0) {
              emlrtBreakCheckR2012b(sp);
            }
          }
        }
      }
    }
  }

  /*  end of function */
}
/* Function Definitions */
void NewtonRaphsonFR2dof(const emlrtStack *sp, real_T tol, real_T m1, real_T m2,
  real_T c1, real_T c2, real_T k1, real_T k2, real_T beta, real_T fc11, real_T
  fc12, real_T fc21, real_T fc22, real_T fMin, real_T fMax, real_T
  numAmplSamples, real_T numPulsSamples, real_T aMax, real_T maxIter,
  emxArray_real_T *Amplitude1FR, emxArray_real_T *Amplitude2FR, emxArray_real_T *
  A1amplitudeFR, emxArray_real_T *A2amplitudeFR, emxArray_real_T *B1amplitudeFR,
  emxArray_real_T *B2amplitudeFR, emxArray_real_T *W)
{
  real_T kd;
  real_T d;
  real_T b;
  int32_T n;
  real_T anew;
  real_T apnd;
  boolean_T n_too_large;
  real_T ndbl;
  real_T cdiff;
  real_T absa;
  real_T absb;
  int32_T k;
  int32_T nm1d2;
  real_T b_kd;
  int32_T i;
  int32_T j;
  real_T b_n;
  real_T amplitude1[2];
  real_T amplitude2[2];
  real_T F[4];
  int32_T exitg1;
  real_T scale;
  real_T absxk;
  real_T t;
  real_T delta[4];
  real_T dv0[16];
  real_T dv1[16];
  int32_T b_j;
  int32_T c_n;
  int32_T c_j;
  int32_T d_n;
  int32_T d_j;
  int32_T e_n;
  int32_T e_j;
  int32_T f_n;
  int32_T f_j;
  int32_T g_n;
  int32_T g_j;
  int32_T h_n;
  emlrtStack st;
  emlrtStack b_st;
  emlrtStack c_st;
  st.prev = sp;
  st.tls = sp->tls;
  st.site = &emlrtRSI;
  b_st.prev = &st;
  b_st.tls = st.tls;
  c_st.prev = &b_st;
  c_st.tls = b_st.tls;
  kd = fMin * 2.0 * 3.1415926535897931;
  d = (fMax - fMin) / (numPulsSamples - 1.0) * 2.0 * 3.1415926535897931;
  b = fMax * 2.0 * 3.1415926535897931;
  b_st.site = &e_emlrtRSI;
  if (muDoubleScalarIsNaN(kd) || muDoubleScalarIsNaN(d) || muDoubleScalarIsNaN(b))
  {
    n = 0;
    anew = rtNaN;
    apnd = b;
    n_too_large = false;
  } else if ((d == 0.0) || ((kd < b) && (d < 0.0)) || ((b < kd) && (d > 0.0))) {
    n = -1;
    anew = kd;
    apnd = b;
    n_too_large = false;
  } else if (muDoubleScalarIsInf(kd) || muDoubleScalarIsInf(b)) {
    n = 0;
    anew = rtNaN;
    apnd = b;
    if (muDoubleScalarIsInf(d) || (kd == b)) {
      n_too_large = true;
    } else {
      n_too_large = false;
    }

    n_too_large = !n_too_large;
  } else if (muDoubleScalarIsInf(d)) {
    n = 0;
    anew = kd;
    apnd = b;
    n_too_large = false;
  } else {
    anew = kd;
    ndbl = muDoubleScalarFloor((b - kd) / d + 0.5);
    apnd = kd + ndbl * d;
    if (d > 0.0) {
      cdiff = apnd - b;
    } else {
      cdiff = b - apnd;
    }

    absa = muDoubleScalarAbs(kd);
    absb = muDoubleScalarAbs(b);
    if (muDoubleScalarAbs(cdiff) < 4.4408920985006262E-16 * muDoubleScalarMax
        (absa, absb)) {
      ndbl++;
      apnd = b;
    } else if (cdiff > 0.0) {
      apnd = kd + (ndbl - 1.0) * d;
    } else {
      ndbl++;
    }

    n_too_large = (2.147483647E+9 < ndbl);
    if (ndbl >= 0.0) {
      n = (int32_T)ndbl - 1;
    } else {
      n = -1;
    }
  }

  c_st.site = &f_emlrtRSI;
  if (!n_too_large) {
  } else {
    emlrtErrorWithMessageIdR2012b(&c_st, &c_emlrtRTEI, "Coder:MATLAB:pmaxsize",
      0);
  }

  k = W->size[0] * W->size[1];
  W->size[0] = 1;
  W->size[1] = n + 1;
  emxEnsureCapacity(&b_st, (emxArray__common *)W, k, (int32_T)sizeof(real_T),
                    &emlrtRTEI);
  if (n + 1 > 0) {
    W->data[0] = anew;
    if (n + 1 > 1) {
      W->data[n] = apnd;
      k = n + (n < 0);
      if (k >= 0) {
        nm1d2 = (int32_T)((uint32_T)k >> 1);
      } else {
Пример #9
0
/* Function Definitions */
void occflow(const emlrtStack *sp, const emxArray_real_T *cgridvec,
             emxArray_real_T *cgridvecprev, emxArray_real_T *context, const
             emxArray_real_T *nei_idx, const emxArray_real_T *nei_weight, real_T
             nei_filter_n, const emxArray_real_T *nei4u_idx, const
             emxArray_real_T *nei4u_weight, real_T nei4u_filter_n, real_T occval,
             real_T minthreshold, real_T maxthreshold, real_T reinitval, real_T
             intensifyrate, real_T nocc_attenuaterate, real_T
             unknown_attenuaterate, real_T sigm_coef, real_T
             do_attenuation_first, emxArray_real_T *predvec, emxArray_real_T
             *maxvec)
{
  emxArray_boolean_T *x;
  int32_T ix;
  int32_T idx;
  emxArray_boolean_T *r0;
  int32_T nx;
  emxArray_int32_T *ii;
  boolean_T overflow;
  int32_T iy;
  boolean_T exitg6;
  boolean_T guard3 = false;
  boolean_T guard4 = false;
  emxArray_real_T *newlyoccidx;
  boolean_T exitg5;
  boolean_T guard2 = false;
  boolean_T b_guard3 = false;
  emxArray_real_T *occidx;
  boolean_T exitg4;
  boolean_T guard1 = false;
  boolean_T b_guard2 = false;
  emxArray_real_T *noccidx;
  int32_T nrnocc;
  int32_T j;
  emxArray_real_T *curr_col;
  emxArray_real_T *updt_col;
  emxArray_real_T *z;
  int32_T coccidx;
  boolean_T b_guard1 = false;
  int32_T ixstart;
  int32_T n;
  real_T mtmp;
  boolean_T exitg3;
  int32_T varargin_1[2];
  int32_T k;
  int32_T iv3[2];
  int32_T iv4[2];
  real_T d0;
  emxArray_real_T *tempcontext;
  emxArray_real_T *b_nei4u_weight;
  real_T sumval;
  int32_T m;
  int32_T iv5[2];
  boolean_T b_ix;
  boolean_T exitg2;
  boolean_T b_ixstart;
  int32_T varargin_2[2];
  boolean_T p;
  boolean_T exitg1;
  emlrtStack st;
  emlrtStack b_st;
  emlrtStack c_st;
  emlrtStack d_st;
  emlrtStack e_st;
  emlrtStack f_st;
  (void)unknown_attenuaterate;
  st.prev = sp;
  st.tls = sp->tls;
  b_st.prev = &st;
  b_st.tls = st.tls;
  c_st.prev = &b_st;
  c_st.tls = b_st.tls;
  d_st.prev = &c_st;
  d_st.tls = c_st.tls;
  e_st.prev = &d_st;
  e_st.tls = d_st.tls;
  f_st.prev = &e_st;
  f_st.tls = e_st.tls;
  emlrtHeapReferenceStackEnterFcnR2012b(sp);
  emxInit_boolean_T(sp, &x, 1, &emlrtRTEI, true);

  /*  */
  /*  Occupancy flow with vector input  */
  /*  */
  /*  Compute indices first  */
  ix = x->size[0];
  x->size[0] = cgridvec->size[0];
  emxEnsureCapacity(sp, (emxArray__common *)x, ix, (int32_T)sizeof(boolean_T),
                    &emlrtRTEI);
  idx = cgridvec->size[0];
  for (ix = 0; ix < idx; ix++) {
    x->data[ix] = (cgridvec->data[ix] == occval);
  }

  emxInit_boolean_T(sp, &r0, 1, &emlrtRTEI, true);
  ix = r0->size[0];
  r0->size[0] = cgridvecprev->size[0];
  emxEnsureCapacity(sp, (emxArray__common *)r0, ix, (int32_T)sizeof(boolean_T),
                    &emlrtRTEI);
  idx = cgridvecprev->size[0];
  for (ix = 0; ix < idx; ix++) {
    r0->data[ix] = (cgridvecprev->data[ix] != occval);
  }

  ix = x->size[0];
  nx = r0->size[0];
  if (ix != nx) {
    emlrtSizeEqCheck1DR2012b(ix, nx, &emlrtECI, sp);
  }

  st.site = &emlrtRSI;
  ix = x->size[0];
  emxEnsureCapacity(&st, (emxArray__common *)x, ix, (int32_T)sizeof(boolean_T),
                    &emlrtRTEI);
  idx = x->size[0];
  for (ix = 0; ix < idx; ix++) {
    x->data[ix] = (x->data[ix] && r0->data[ix]);
  }

  emxFree_boolean_T(&r0);
  emxInit_int32_T(&st, &ii, 1, &l_emlrtRTEI, true);
  b_st.site = &i_emlrtRSI;
  nx = x->size[0];
  idx = 0;
  ix = ii->size[0];
  ii->size[0] = x->size[0];
  emxEnsureCapacity(&b_st, (emxArray__common *)ii, ix, (int32_T)sizeof(int32_T),
                    &emlrtRTEI);
  c_st.site = &j_emlrtRSI;
  if (1 > x->size[0]) {
    overflow = false;
  } else {
    overflow = (x->size[0] > 2147483646);
  }

  if (overflow) {
    d_st.site = &l_emlrtRSI;
    check_forloop_overflow_error(&d_st);
  }

  iy = 1;
  exitg6 = false;
  while ((!exitg6) && (iy <= nx)) {
    guard3 = false;
    if (x->data[iy - 1]) {
      idx++;
      ii->data[idx - 1] = iy;
      if (idx >= nx) {
        exitg6 = true;
      } else {
        guard3 = true;
      }
    } else {
      guard3 = true;
    }

    if (guard3) {
      iy++;
    }
  }

  if (idx <= x->size[0]) {
  } else {
    emlrtErrorWithMessageIdR2012b(&b_st, &s_emlrtRTEI,
      "Coder:builtins:AssertionFailed", 0);
  }

  if (x->size[0] == 1) {
    if (idx == 0) {
      ix = ii->size[0];
      ii->size[0] = 0;
      emxEnsureCapacity(&b_st, (emxArray__common *)ii, ix, (int32_T)sizeof
                        (int32_T), &emlrtRTEI);
    }
  } else {
    if (1 > idx) {
      ix = 0;
    } else {
      ix = idx;
    }

    c_st.site = &k_emlrtRSI;
    overflow = !(ii->size[0] != 1);
    guard4 = false;
    if (overflow) {
      overflow = false;
      if (ix != 1) {
        overflow = true;
      }

      if (overflow) {
        overflow = true;
      } else {
        guard4 = true;
      }
    } else {
      guard4 = true;
    }

    if (guard4) {
      overflow = false;
    }

    d_st.site = &m_emlrtRSI;
    if (!overflow) {
    } else {
      emlrtErrorWithMessageIdR2012b(&d_st, &t_emlrtRTEI,
        "Coder:FE:PotentialVectorVector", 0);
    }

    nx = ii->size[0];
    ii->size[0] = ix;
    emxEnsureCapacity(&b_st, (emxArray__common *)ii, nx, (int32_T)sizeof(int32_T),
                      &c_emlrtRTEI);
  }

  emxInit_real_T(&b_st, &newlyoccidx, 1, &f_emlrtRTEI, true);
  ix = newlyoccidx->size[0];
  newlyoccidx->size[0] = ii->size[0];
  emxEnsureCapacity(&st, (emxArray__common *)newlyoccidx, ix, (int32_T)sizeof
                    (real_T), &emlrtRTEI);
  idx = ii->size[0];
  for (ix = 0; ix < idx; ix++) {
    newlyoccidx->data[ix] = ii->data[ix];
  }

  st.site = &b_emlrtRSI;
  ix = x->size[0];
  x->size[0] = cgridvec->size[0];
  emxEnsureCapacity(&st, (emxArray__common *)x, ix, (int32_T)sizeof(boolean_T),
                    &emlrtRTEI);
  idx = cgridvec->size[0];
  for (ix = 0; ix < idx; ix++) {
    x->data[ix] = (cgridvec->data[ix] == occval);
  }

  b_st.site = &i_emlrtRSI;
  nx = x->size[0];
  idx = 0;
  ix = ii->size[0];
  ii->size[0] = x->size[0];
  emxEnsureCapacity(&b_st, (emxArray__common *)ii, ix, (int32_T)sizeof(int32_T),
                    &emlrtRTEI);
  c_st.site = &j_emlrtRSI;
  if (1 > x->size[0]) {
    overflow = false;
  } else {
    overflow = (x->size[0] > 2147483646);
  }

  if (overflow) {
    d_st.site = &l_emlrtRSI;
    check_forloop_overflow_error(&d_st);
  }

  iy = 1;
  exitg5 = false;
  while ((!exitg5) && (iy <= nx)) {
    guard2 = false;
    if (x->data[iy - 1]) {
      idx++;
      ii->data[idx - 1] = iy;
      if (idx >= nx) {
        exitg5 = true;
      } else {
        guard2 = true;
      }
    } else {
      guard2 = true;
    }

    if (guard2) {
      iy++;
    }
  }

  if (idx <= x->size[0]) {
  } else {
    emlrtErrorWithMessageIdR2012b(&b_st, &s_emlrtRTEI,
      "Coder:builtins:AssertionFailed", 0);
  }

  if (x->size[0] == 1) {
    if (idx == 0) {
      ix = ii->size[0];
      ii->size[0] = 0;
      emxEnsureCapacity(&b_st, (emxArray__common *)ii, ix, (int32_T)sizeof
                        (int32_T), &emlrtRTEI);
    }
  } else {
    if (1 > idx) {
      ix = 0;
    } else {
      ix = idx;
    }

    c_st.site = &k_emlrtRSI;
    overflow = !(ii->size[0] != 1);
    b_guard3 = false;
    if (overflow) {
      overflow = false;
      if (ix != 1) {
        overflow = true;
      }

      if (overflow) {
        overflow = true;
      } else {
        b_guard3 = true;
      }
    } else {
      b_guard3 = true;
    }

    if (b_guard3) {
      overflow = false;
    }

    d_st.site = &m_emlrtRSI;
    if (!overflow) {
    } else {
      emlrtErrorWithMessageIdR2012b(&d_st, &t_emlrtRTEI,
        "Coder:FE:PotentialVectorVector", 0);
    }

    nx = ii->size[0];
    ii->size[0] = ix;
    emxEnsureCapacity(&b_st, (emxArray__common *)ii, nx, (int32_T)sizeof(int32_T),
                      &c_emlrtRTEI);
  }

  emxInit_real_T(&b_st, &occidx, 1, &g_emlrtRTEI, true);
  ix = occidx->size[0];
  occidx->size[0] = ii->size[0];
  emxEnsureCapacity(&st, (emxArray__common *)occidx, ix, (int32_T)sizeof(real_T),
                    &emlrtRTEI);
  idx = ii->size[0];
  for (ix = 0; ix < idx; ix++) {
    occidx->data[ix] = ii->data[ix];
  }

  st.site = &c_emlrtRSI;
  ix = x->size[0];
  x->size[0] = cgridvec->size[0];
  emxEnsureCapacity(&st, (emxArray__common *)x, ix, (int32_T)sizeof(boolean_T),
                    &emlrtRTEI);
  idx = cgridvec->size[0];
  for (ix = 0; ix < idx; ix++) {
    x->data[ix] = (cgridvec->data[ix] != occval);
  }

  b_st.site = &i_emlrtRSI;
  nx = x->size[0];
  idx = 0;
  ix = ii->size[0];
  ii->size[0] = x->size[0];
  emxEnsureCapacity(&b_st, (emxArray__common *)ii, ix, (int32_T)sizeof(int32_T),
                    &emlrtRTEI);
  c_st.site = &j_emlrtRSI;
  if (1 > x->size[0]) {
    overflow = false;
  } else {
    overflow = (x->size[0] > 2147483646);
  }

  if (overflow) {
    d_st.site = &l_emlrtRSI;
    check_forloop_overflow_error(&d_st);
  }

  iy = 1;
  exitg4 = false;
  while ((!exitg4) && (iy <= nx)) {
    guard1 = false;
    if (x->data[iy - 1]) {
      idx++;
      ii->data[idx - 1] = iy;
      if (idx >= nx) {
        exitg4 = true;
      } else {
        guard1 = true;
      }
    } else {
      guard1 = true;
    }

    if (guard1) {
      iy++;
    }
  }

  if (idx <= x->size[0]) {
  } else {
    emlrtErrorWithMessageIdR2012b(&b_st, &s_emlrtRTEI,
      "Coder:builtins:AssertionFailed", 0);
  }

  if (x->size[0] == 1) {
    if (idx == 0) {
      ix = ii->size[0];
      ii->size[0] = 0;
      emxEnsureCapacity(&b_st, (emxArray__common *)ii, ix, (int32_T)sizeof
                        (int32_T), &emlrtRTEI);
    }
  } else {
    if (1 > idx) {
      ix = 0;
    } else {
      ix = idx;
    }

    c_st.site = &k_emlrtRSI;
    overflow = !(ii->size[0] != 1);
    b_guard2 = false;
    if (overflow) {
      overflow = false;
      if (ix != 1) {
        overflow = true;
      }

      if (overflow) {
        overflow = true;
      } else {
        b_guard2 = true;
      }
    } else {
      b_guard2 = true;
    }

    if (b_guard2) {
      overflow = false;
    }

    d_st.site = &m_emlrtRSI;
    if (!overflow) {
    } else {
      emlrtErrorWithMessageIdR2012b(&d_st, &t_emlrtRTEI,
        "Coder:FE:PotentialVectorVector", 0);
    }

    nx = ii->size[0];
    ii->size[0] = ix;
    emxEnsureCapacity(&b_st, (emxArray__common *)ii, nx, (int32_T)sizeof(int32_T),
                      &c_emlrtRTEI);
  }

  emxFree_boolean_T(&x);
  emxInit_real_T(&b_st, &noccidx, 1, &h_emlrtRTEI, true);
  ix = noccidx->size[0];
  noccidx->size[0] = ii->size[0];
  emxEnsureCapacity(&st, (emxArray__common *)noccidx, ix, (int32_T)sizeof(real_T),
                    &emlrtRTEI);
  idx = ii->size[0];
  for (ix = 0; ix < idx; ix++) {
    noccidx->data[ix] = ii->data[ix];
  }

  nrnocc = noccidx->size[0] - 1;

  /*  1 Intensify newly occupied cells  */
  j = 0;
  emxInit_real_T1(sp, &curr_col, 2, &i_emlrtRTEI, true);
  emxInit_real_T1(sp, &updt_col, 2, &j_emlrtRTEI, true);
  emxInit_real_T1(sp, &z, 2, &emlrtRTEI, true);
  while (j <= newlyoccidx->size[0] - 1) {
    /*  For newly occupied cells  */
    ix = newlyoccidx->size[0];
    if (!((j + 1 >= 1) && (j + 1 <= ix))) {
      emlrtDynamicBoundsCheckR2012b(j + 1, 1, ix, &eb_emlrtBCI, sp);
    }

    coccidx = (int32_T)newlyoccidx->data[j] - 1;
    ix = context->size[0];
    nx = (int32_T)newlyoccidx->data[j];
    if (!((nx >= 1) && (nx <= ix))) {
      emlrtDynamicBoundsCheckR2012b(nx, 1, ix, &emlrtBCI, sp);
    }

    st.site = &d_emlrtRSI;
    b_st.site = &n_emlrtRSI;
    c_st.site = &o_emlrtRSI;
    ix = context->size[1];
    b_guard1 = false;
    if (ix == 1) {
      b_guard1 = true;
    } else {
      ix = context->size[1];
      if (ix != 1) {
        b_guard1 = true;
      } else {
        overflow = false;
      }
    }

    if (b_guard1) {
      overflow = true;
    }

    if (overflow) {
    } else {
      emlrtErrorWithMessageIdR2012b(&c_st, &u_emlrtRTEI,
        "Coder:toolbox:autoDimIncompatibility", 0);
    }

    ix = context->size[1];
    if (ix > 0) {
    } else {
      emlrtErrorWithMessageIdR2012b(&c_st, &v_emlrtRTEI,
        "Coder:toolbox:eml_min_or_max_varDimZero", 0);
    }

    d_st.site = &p_emlrtRSI;
    ixstart = 1;
    n = context->size[1];
    nx = (int32_T)newlyoccidx->data[j];
    mtmp = context->data[nx - 1];
    ix = context->size[1];
    if (ix > 1) {
      if (muDoubleScalarIsNaN(mtmp)) {
        e_st.site = &r_emlrtRSI;
        ix = context->size[1];
        if (2 > ix) {
          overflow = false;
        } else {
          ix = context->size[1];
          overflow = (ix > 2147483646);
        }

        if (overflow) {
          f_st.site = &l_emlrtRSI;
          check_forloop_overflow_error(&f_st);
        }

        ix = 2;
        exitg3 = false;
        while ((!exitg3) && (ix <= n)) {
          ixstart = ix;
          if (!muDoubleScalarIsNaN(context->data[coccidx + context->size[0] *
               (ix - 1)])) {
            mtmp = context->data[coccidx + context->size[0] * (ix - 1)];
            exitg3 = true;
          } else {
            ix++;
          }
        }
      }

      ix = context->size[1];
      if (ixstart < ix) {
        e_st.site = &q_emlrtRSI;
        ix = context->size[1];
        if (ixstart + 1 > ix) {
          overflow = false;
        } else {
          ix = context->size[1];
          overflow = (ix > 2147483646);
        }

        if (overflow) {
          f_st.site = &l_emlrtRSI;
          check_forloop_overflow_error(&f_st);
        }

        for (ix = ixstart + 1; ix <= n; ix++) {
          if (context->data[coccidx + context->size[0] * (ix - 1)] > mtmp) {
            mtmp = context->data[coccidx + context->size[0] * (ix - 1)];
          }
        }
      }
    }

    if (mtmp < minthreshold) {
      idx = context->size[1];
      iy = context->size[0];
      nx = (int32_T)newlyoccidx->data[j];
      if (!((nx >= 1) && (nx <= iy))) {
        emlrtDynamicBoundsCheckR2012b(nx, 1, iy, &b_emlrtBCI, sp);
      }

      for (ix = 0; ix < idx; ix++) {
        context->data[(nx + context->size[0] * ix) - 1] = reinitval;
      }

      /*  Reinitialize */
    } else {
      idx = context->size[1];
      nx = (int32_T)newlyoccidx->data[j];
      ix = updt_col->size[0] * updt_col->size[1];
      updt_col->size[0] = 1;
      updt_col->size[1] = idx;
      emxEnsureCapacity(sp, (emxArray__common *)updt_col, ix, (int32_T)sizeof
                        (real_T), &emlrtRTEI);
      for (ix = 0; ix < idx; ix++) {
        updt_col->data[updt_col->size[0] * ix] = intensifyrate * context->data
          [(nx + context->size[0] * ix) - 1];
      }

      /*  Intensify */
      st.site = &e_emlrtRSI;
      b_st.site = &s_emlrtRSI;
      c_st.site = &o_emlrtRSI;
      d_st.site = &t_emlrtRSI;
      ix = curr_col->size[0] * curr_col->size[1];
      curr_col->size[0] = 1;
      curr_col->size[1] = updt_col->size[1];
      emxEnsureCapacity(&d_st, (emxArray__common *)curr_col, ix, (int32_T)sizeof
                        (real_T), &emlrtRTEI);
      idx = updt_col->size[0] * updt_col->size[1];
      for (ix = 0; ix < idx; ix++) {
        curr_col->data[ix] = updt_col->data[ix];
      }

      e_st.site = &u_emlrtRSI;
      for (ix = 0; ix < 2; ix++) {
        varargin_1[ix] = updt_col->size[ix];
      }

      ix = z->size[0] * z->size[1];
      z->size[0] = 1;
      z->size[1] = updt_col->size[1];
      emxEnsureCapacity(&e_st, (emxArray__common *)z, ix, (int32_T)sizeof(real_T),
                        &d_emlrtRTEI);
      iy = updt_col->size[1];
      ix = updt_col->size[0] * updt_col->size[1];
      updt_col->size[0] = 1;
      updt_col->size[1] = varargin_1[1];
      emxEnsureCapacity(&e_st, (emxArray__common *)updt_col, ix, (int32_T)sizeof
                        (real_T), &e_emlrtRTEI);
      if (dimagree(updt_col, curr_col)) {
      } else {
        emlrtErrorWithMessageIdR2012b(&e_st, &x_emlrtRTEI, "MATLAB:dimagree", 0);
      }

      e_st.site = &v_emlrtRSI;
      if (1 > z->size[1]) {
        overflow = false;
      } else {
        overflow = (z->size[1] > 2147483646);
      }

      if (overflow) {
        f_st.site = &l_emlrtRSI;
        check_forloop_overflow_error(&f_st);
      }

      for (k = 0; k + 1 <= iy; k++) {
        updt_col->data[k] = muDoubleScalarMin(curr_col->data[k], maxthreshold);
      }

      /*  Max-thesholding */
      ix = context->size[0];
      nx = (int32_T)newlyoccidx->data[j];
      if (!((nx >= 1) && (nx <= ix))) {
        emlrtDynamicBoundsCheckR2012b(nx, 1, ix, &c_emlrtBCI, sp);
      }

      idx = context->size[1];
      ix = ii->size[0];
      ii->size[0] = idx;
      emxEnsureCapacity(sp, (emxArray__common *)ii, ix, (int32_T)sizeof(int32_T),
                        &emlrtRTEI);
      for (ix = 0; ix < idx; ix++) {
        ii->data[ix] = ix;
      }

      iv3[0] = 1;
      iv3[1] = ii->size[0];
      emlrtSubAssignSizeCheckR2012b(iv3, 2, *(int32_T (*)[2])updt_col->size, 2,
        &b_emlrtECI, sp);
      nx = (int32_T)newlyoccidx->data[j];
      idx = updt_col->size[1];
      for (ix = 0; ix < idx; ix++) {
        context->data[(nx + context->size[0] * ii->data[ix]) - 1] =
          updt_col->data[updt_col->size[0] * ix];
      }
    }

    j++;
    if (*emlrtBreakCheckR2012bFlagVar != 0) {
      emlrtBreakCheckR2012b(sp);
    }
  }

  emxFree_real_T(&z);

  /*  2 Attenuate unoccupied cells */
  if (do_attenuation_first == 1.0) {
    j = 0;
    while (j <= nrnocc) {
      /*  For unoccupied cells */
      ix = noccidx->size[0];
      nx = j + 1;
      if (!((nx >= 1) && (nx <= ix))) {
        emlrtDynamicBoundsCheckR2012b(nx, 1, ix, &d_emlrtBCI, sp);
      }

      ix = context->size[0];
      nx = (int32_T)noccidx->data[j];
      if (!((nx >= 1) && (nx <= ix))) {
        emlrtDynamicBoundsCheckR2012b(nx, 1, ix, &e_emlrtBCI, sp);
      }

      idx = context->size[1];
      iy = (int32_T)noccidx->data[j];
      ix = updt_col->size[0] * updt_col->size[1];
      updt_col->size[0] = 1;
      updt_col->size[1] = idx;
      emxEnsureCapacity(sp, (emxArray__common *)updt_col, ix, (int32_T)sizeof
                        (real_T), &emlrtRTEI);
      for (ix = 0; ix < idx; ix++) {
        updt_col->data[updt_col->size[0] * ix] = context->data[(iy +
          context->size[0] * ix) - 1] * nocc_attenuaterate;
      }

      /*  Attenuate */
      ix = context->size[0];
      nx = (int32_T)noccidx->data[j];
      if (!((nx >= 1) && (nx <= ix))) {
        emlrtDynamicBoundsCheckR2012b(nx, 1, ix, &f_emlrtBCI, sp);
      }

      idx = context->size[1];
      ix = ii->size[0];
      ii->size[0] = idx;
      emxEnsureCapacity(sp, (emxArray__common *)ii, ix, (int32_T)sizeof(int32_T),
                        &emlrtRTEI);
      for (ix = 0; ix < idx; ix++) {
        ii->data[ix] = ix;
      }

      iv4[0] = 1;
      iv4[1] = ii->size[0];
      emlrtSubAssignSizeCheckR2012b(iv4, 2, *(int32_T (*)[2])updt_col->size, 2,
        &c_emlrtECI, sp);
      iy = (int32_T)noccidx->data[j];
      idx = updt_col->size[1];
      for (ix = 0; ix < idx; ix++) {
        context->data[(iy + context->size[0] * ii->data[ix]) - 1] =
          updt_col->data[updt_col->size[0] * ix];
      }

      j++;
      if (*emlrtBreakCheckR2012bFlagVar != 0) {
        emlrtBreakCheckR2012b(sp);
      }
    }
  }

  /*  4 Propagation  */
  j = 0;
  while (j <= occidx->size[0] - 1) {
    /*  For occupied cells  */
    ix = occidx->size[0];
    if (!((j + 1 >= 1) && (j + 1 <= ix))) {
      emlrtDynamicBoundsCheckR2012b(j + 1, 1, ix, &bb_emlrtBCI, sp);
    }

    idx = context->size[1];
    ix = context->size[0];
    iy = (int32_T)occidx->data[j];
    if (!((iy >= 1) && (iy <= ix))) {
      emlrtDynamicBoundsCheckR2012b(iy, 1, ix, &g_emlrtBCI, sp);
    }

    ix = curr_col->size[0] * curr_col->size[1];
    curr_col->size[0] = 1;
    curr_col->size[1] = idx;
    emxEnsureCapacity(sp, (emxArray__common *)curr_col, ix, (int32_T)sizeof
                      (real_T), &emlrtRTEI);
    for (ix = 0; ix < idx; ix++) {
      curr_col->data[curr_col->size[0] * ix] = context->data[(iy + context->
        size[0] * ix) - 1];
    }

    ix = nei_idx->size[0];
    nx = (int32_T)occidx->data[j];
    if (!((nx >= 1) && (nx <= ix))) {
      emlrtDynamicBoundsCheckR2012b(nx, 1, ix, &h_emlrtBCI, sp);
    }

    ix = nei_weight->size[0];
    nx = (int32_T)occidx->data[j];
    if (!((nx >= 1) && (nx <= ix))) {
      emlrtDynamicBoundsCheckR2012b(nx, 1, ix, &i_emlrtBCI, sp);
    }

    emlrtForLoopVectorCheckR2012b(1.0, 1.0, nei_filter_n, mxDOUBLE_CLASS,
      (int32_T)nei_filter_n, &p_emlrtRTEI, sp);
    k = 0;
    while (k <= (int32_T)nei_filter_n - 1) {
      /*  For all neighbor cells  */
      ix = curr_col->size[1];
      nx = k + 1;
      if (!((nx >= 1) && (nx <= ix))) {
        emlrtDynamicBoundsCheckR2012b(nx, 1, ix, &j_emlrtBCI, sp);
      }

      ix = nei_idx->size[1];
      nx = k + 1;
      if (!((nx >= 1) && (nx <= ix))) {
        emlrtDynamicBoundsCheckR2012b(nx, 1, ix, &k_emlrtBCI, sp);
      }

      ix = nei_weight->size[1];
      nx = k + 1;
      if (!((nx >= 1) && (nx <= ix))) {
        emlrtDynamicBoundsCheckR2012b(nx, 1, ix, &l_emlrtBCI, sp);
      }

      iy = (int32_T)occidx->data[j];
      if (nei_idx->data[(iy + nei_idx->size[0] * k) - 1] != 0.0) {
        /*  If properly connected, propagate */
        iy = (int32_T)occidx->data[j];
        d0 = nei_idx->data[(iy + nei_idx->size[0] * k) - 1];
        if (d0 != (int32_T)muDoubleScalarFloor(d0)) {
          emlrtIntegerCheckR2012b(d0, &emlrtDCI, sp);
        }

        ix = context->size[0];
        nx = (int32_T)d0;
        if (!((nx >= 1) && (nx <= ix))) {
          emlrtDynamicBoundsCheckR2012b(nx, 1, ix, &m_emlrtBCI, sp);
        }

        ix = context->size[1];
        nx = k + 1;
        if (!((nx >= 1) && (nx <= ix))) {
          emlrtDynamicBoundsCheckR2012b(nx, 1, ix, &n_emlrtBCI, sp);
        }

        /*  Maximum value thresholding  */
        iy = (int32_T)occidx->data[j];
        idx = (int32_T)occidx->data[j];
        nx = (int32_T)occidx->data[j];
        ix = context->size[0];
        nx = (int32_T)nei_idx->data[(nx + nei_idx->size[0] * k) - 1];
        if (!((nx >= 1) && (nx <= ix))) {
          emlrtDynamicBoundsCheckR2012b(nx, 1, ix, &cb_emlrtBCI, sp);
        }

        ix = context->size[1];
        if (!((k + 1 >= 1) && (k + 1 <= ix))) {
          emlrtDynamicBoundsCheckR2012b(k + 1, 1, ix, &db_emlrtBCI, sp);
        }

        context->data[(nx + context->size[0] * k) - 1] = muDoubleScalarMax
          (context->data[((int32_T)nei_idx->data[(iy + nei_idx->size[0] * k) - 1]
                          + context->size[0] * k) - 1], muDoubleScalarMin
           (nei_weight->data[(idx + nei_weight->size[0] * k) - 1] *
            curr_col->data[k], maxthreshold));

        /*  Make sure current context propagation does not weaken the flow information */
      }

      k++;
      if (*emlrtBreakCheckR2012bFlagVar != 0) {
        emlrtBreakCheckR2012b(sp);
      }
    }

    j++;
    if (*emlrtBreakCheckR2012bFlagVar != 0) {
      emlrtBreakCheckR2012b(sp);
    }
  }

  emxFree_real_T(&occidx);
  emxInit_real_T1(sp, &tempcontext, 2, &k_emlrtRTEI, true);

  /*  5 Uncertainty in acceleration */
  ix = tempcontext->size[0] * tempcontext->size[1];
  tempcontext->size[0] = context->size[0];
  tempcontext->size[1] = context->size[1];
  emxEnsureCapacity(sp, (emxArray__common *)tempcontext, ix, (int32_T)sizeof
                    (real_T), &emlrtRTEI);
  idx = context->size[0] * context->size[1];
  for (ix = 0; ix < idx; ix++) {
    tempcontext->data[ix] = context->data[ix];
  }

  emlrtForLoopVectorCheckR2012b(1.0, 1.0, nei_filter_n, mxDOUBLE_CLASS, (int32_T)
    nei_filter_n, &q_emlrtRTEI, sp);
  j = 0;
  emxInit_real_T1(sp, &b_nei4u_weight, 2, &emlrtRTEI, true);
  while (j <= (int32_T)nei_filter_n - 1) {
    /*  For all context level */
    k = 0;
    while (k <= nei_idx->size[0] - 1) {
      /*  For all cells */
      sumval = 0.0;
      emlrtForLoopVectorCheckR2012b(1.0, 1.0, nei4u_filter_n, mxDOUBLE_CLASS,
        (int32_T)nei4u_filter_n, &r_emlrtRTEI, sp);
      m = 0;
      while (m <= (int32_T)nei4u_filter_n - 1) {
        ix = nei4u_idx->size[0];
        nx = k + 1;
        if (!((nx >= 1) && (nx <= ix))) {
          emlrtDynamicBoundsCheckR2012b(nx, 1, ix, &o_emlrtBCI, sp);
        }

        ix = nei4u_idx->size[1];
        nx = m + 1;
        if (!((nx >= 1) && (nx <= ix))) {
          emlrtDynamicBoundsCheckR2012b(nx, 1, ix, &p_emlrtBCI, sp);
        }

        ix = nei4u_weight->size[0];
        nx = k + 1;
        if (!((nx >= 1) && (nx <= ix))) {
          emlrtDynamicBoundsCheckR2012b(nx, 1, ix, &q_emlrtBCI, sp);
        }

        ix = nei4u_weight->size[1];
        nx = m + 1;
        if (!((nx >= 1) && (nx <= ix))) {
          emlrtDynamicBoundsCheckR2012b(nx, 1, ix, &r_emlrtBCI, sp);
        }

        idx = nei4u_weight->size[1];
        ix = nei4u_weight->size[0];
        nx = 1 + k;
        if (!((nx >= 1) && (nx <= ix))) {
          emlrtDynamicBoundsCheckR2012b(nx, 1, ix, &s_emlrtBCI, sp);
        }

        ix = b_nei4u_weight->size[0] * b_nei4u_weight->size[1];
        b_nei4u_weight->size[0] = 1;
        b_nei4u_weight->size[1] = idx;
        emxEnsureCapacity(sp, (emxArray__common *)b_nei4u_weight, ix, (int32_T)
                          sizeof(real_T), &emlrtRTEI);
        for (ix = 0; ix < idx; ix++) {
          b_nei4u_weight->data[b_nei4u_weight->size[0] * ix] =
            nei4u_weight->data[(nx + nei4u_weight->size[0] * ix) - 1];
        }

        st.site = &f_emlrtRSI;
        mtmp = sum(&st, b_nei4u_weight);
        if (nei4u_idx->data[k + nei4u_idx->size[0] * m] != 0.0) {
          d0 = nei4u_idx->data[k + nei4u_idx->size[0] * m];
          if (d0 != (int32_T)muDoubleScalarFloor(d0)) {
            emlrtIntegerCheckR2012b(d0, &b_emlrtDCI, sp);
          }

          ix = context->size[0];
          nx = (int32_T)d0;
          if (!((nx >= 1) && (nx <= ix))) {
            emlrtDynamicBoundsCheckR2012b(nx, 1, ix, &t_emlrtBCI, sp);
          }

          ix = context->size[1];
          nx = j + 1;
          if (!((nx >= 1) && (nx <= ix))) {
            emlrtDynamicBoundsCheckR2012b(nx, 1, ix, &u_emlrtBCI, sp);
          }

          sumval += nei4u_weight->data[k + nei4u_weight->size[0] * m] / mtmp *
            context->data[((int32_T)nei4u_idx->data[k + nei4u_idx->size[0] * m]
                           + context->size[0] * j) - 1];
        }

        m++;
        if (*emlrtBreakCheckR2012bFlagVar != 0) {
          emlrtBreakCheckR2012b(sp);
        }
      }

      ix = tempcontext->size[0];
      nx = 1 + k;
      if (!((nx >= 1) && (nx <= ix))) {
        emlrtDynamicBoundsCheckR2012b(nx, 1, ix, &y_emlrtBCI, sp);
      }

      ix = tempcontext->size[1];
      if (!((j + 1 >= 1) && (j + 1 <= ix))) {
        emlrtDynamicBoundsCheckR2012b(j + 1, 1, ix, &ab_emlrtBCI, sp);
      }

      tempcontext->data[(nx + tempcontext->size[0] * j) - 1] = sumval;
      k++;
      if (*emlrtBreakCheckR2012bFlagVar != 0) {
        emlrtBreakCheckR2012b(sp);
      }
    }

    j++;
    if (*emlrtBreakCheckR2012bFlagVar != 0) {
      emlrtBreakCheckR2012b(sp);
    }
  }

  emxFree_real_T(&b_nei4u_weight);
  ix = context->size[0] * context->size[1];
  context->size[0] = tempcontext->size[0];
  context->size[1] = tempcontext->size[1];
  emxEnsureCapacity(sp, (emxArray__common *)context, ix, (int32_T)sizeof(real_T),
                    &emlrtRTEI);
  idx = tempcontext->size[1];
  for (ix = 0; ix < idx; ix++) {
    iy = tempcontext->size[0];
    for (nx = 0; nx < iy; nx++) {
      context->data[nx + context->size[0] * ix] = tempcontext->data[nx +
        tempcontext->size[0] * ix];
    }
  }

  if (do_attenuation_first == 0.0) {
    /*  2 Attenuate unoccupied cells */
    j = 0;
    while (j <= nrnocc) {
      /*  For unoccupied cells, attenuate */
      ix = noccidx->size[0];
      nx = j + 1;
      if (!((nx >= 1) && (nx <= ix))) {
        emlrtDynamicBoundsCheckR2012b(nx, 1, ix, &v_emlrtBCI, sp);
      }

      ix = context->size[0];
      nx = (int32_T)noccidx->data[j];
      if (!((nx >= 1) && (nx <= ix))) {
        emlrtDynamicBoundsCheckR2012b(nx, 1, ix, &w_emlrtBCI, sp);
      }

      idx = context->size[1];
      iy = (int32_T)noccidx->data[j];
      ix = updt_col->size[0] * updt_col->size[1];
      updt_col->size[0] = 1;
      updt_col->size[1] = idx;
      emxEnsureCapacity(sp, (emxArray__common *)updt_col, ix, (int32_T)sizeof
                        (real_T), &emlrtRTEI);
      for (ix = 0; ix < idx; ix++) {
        updt_col->data[updt_col->size[0] * ix] = context->data[(iy +
          context->size[0] * ix) - 1] * nocc_attenuaterate;
      }

      ix = context->size[0];
      nx = (int32_T)noccidx->data[j];
      if (!((nx >= 1) && (nx <= ix))) {
        emlrtDynamicBoundsCheckR2012b(nx, 1, ix, &x_emlrtBCI, sp);
      }

      idx = context->size[1];
      ix = ii->size[0];
      ii->size[0] = idx;
      emxEnsureCapacity(sp, (emxArray__common *)ii, ix, (int32_T)sizeof(int32_T),
                        &emlrtRTEI);
      for (ix = 0; ix < idx; ix++) {
        ii->data[ix] = ix;
      }

      iv5[0] = 1;
      iv5[1] = ii->size[0];
      emlrtSubAssignSizeCheckR2012b(iv5, 2, *(int32_T (*)[2])updt_col->size, 2,
        &d_emlrtECI, sp);
      iy = (int32_T)noccidx->data[j];
      idx = updt_col->size[1];
      for (ix = 0; ix < idx; ix++) {
        context->data[(iy + context->size[0] * ii->data[ix]) - 1] =
          updt_col->data[updt_col->size[0] * ix];
      }

      j++;
      if (*emlrtBreakCheckR2012bFlagVar != 0) {
        emlrtBreakCheckR2012b(sp);
      }
    }
  }

  emxFree_int32_T(&ii);
  emxFree_real_T(&updt_col);
  emxFree_real_T(&noccidx);

  /*  6 Prediction */
  st.site = &g_emlrtRSI;
  ix = tempcontext->size[0] * tempcontext->size[1];
  tempcontext->size[0] = context->size[1];
  tempcontext->size[1] = context->size[0];
  emxEnsureCapacity(&st, (emxArray__common *)tempcontext, ix, (int32_T)sizeof
                    (real_T), &emlrtRTEI);
  idx = context->size[0];
  for (ix = 0; ix < idx; ix++) {
    iy = context->size[1];
    for (nx = 0; nx < iy; nx++) {
      tempcontext->data[nx + tempcontext->size[0] * ix] = context->data[ix +
        context->size[0] * nx];
    }
  }

  b_st.site = &n_emlrtRSI;
  c_st.site = &o_emlrtRSI;
  if (((tempcontext->size[0] == 1) && (tempcontext->size[1] == 1)) ||
      (tempcontext->size[0] != 1)) {
    overflow = true;
  } else {
    overflow = false;
  }

  if (overflow) {
  } else {
    emlrtErrorWithMessageIdR2012b(&c_st, &u_emlrtRTEI,
      "Coder:toolbox:autoDimIncompatibility", 0);
  }

  if (tempcontext->size[0] > 0) {
  } else {
    emlrtErrorWithMessageIdR2012b(&c_st, &v_emlrtRTEI,
      "Coder:toolbox:eml_min_or_max_varDimZero", 0);
  }

  ix = curr_col->size[0] * curr_col->size[1];
  curr_col->size[0] = 1;
  curr_col->size[1] = tempcontext->size[1];
  emxEnsureCapacity(&c_st, (emxArray__common *)curr_col, ix, (int32_T)sizeof
                    (real_T), &emlrtRTEI);
  n = tempcontext->size[0];
  ix = 0;
  iy = -1;
  d_st.site = &ab_emlrtRSI;
  if (1 > tempcontext->size[1]) {
    overflow = false;
  } else {
    overflow = (tempcontext->size[1] > 2147483646);
  }

  if (overflow) {
    e_st.site = &l_emlrtRSI;
    check_forloop_overflow_error(&e_st);
  }

  for (nx = 1; nx <= tempcontext->size[1]; nx++) {
    d_st.site = &bb_emlrtRSI;
    ixstart = ix;
    idx = ix + n;
    mtmp = tempcontext->data[ix];
    if (n > 1) {
      if (muDoubleScalarIsNaN(tempcontext->data[ix])) {
        e_st.site = &r_emlrtRSI;
        if (ix + 2 > idx) {
          b_ix = false;
        } else {
          b_ix = (idx > 2147483646);
        }

        if (b_ix) {
          f_st.site = &l_emlrtRSI;
          check_forloop_overflow_error(&f_st);
        }

        k = ix + 1;
        exitg2 = false;
        while ((!exitg2) && (k + 1 <= idx)) {
          ixstart = k;
          if (!muDoubleScalarIsNaN(tempcontext->data[k])) {
            mtmp = tempcontext->data[k];
            exitg2 = true;
          } else {
            k++;
          }
        }
      }

      if (ixstart + 1 < idx) {
        e_st.site = &q_emlrtRSI;
        if (ixstart + 2 > idx) {
          b_ixstart = false;
        } else {
          b_ixstart = (idx > 2147483646);
        }

        if (b_ixstart) {
          f_st.site = &l_emlrtRSI;
          check_forloop_overflow_error(&f_st);
        }

        for (k = ixstart + 1; k + 1 <= idx; k++) {
          if (tempcontext->data[k] > mtmp) {
            mtmp = tempcontext->data[k];
          }
        }
      }
    }

    iy++;
    curr_col->data[iy] = mtmp;
    ix += n;
  }

  emxFree_real_T(&tempcontext);
  ix = maxvec->size[0];
  maxvec->size[0] = curr_col->size[1];
  emxEnsureCapacity(sp, (emxArray__common *)maxvec, ix, (int32_T)sizeof(real_T),
                    &emlrtRTEI);
  idx = curr_col->size[1];
  for (ix = 0; ix < idx; ix++) {
    maxvec->data[ix] = curr_col->data[curr_col->size[0] * ix];
  }

  emxFree_real_T(&curr_col);
  st.site = &h_emlrtRSI;

  /*  sigm_a  <= if we increase the value, than the sigm function gets peaky! */
  b_st.site = &cb_emlrtRSI;
  ix = predvec->size[0];
  predvec->size[0] = maxvec->size[0];
  emxEnsureCapacity(&b_st, (emxArray__common *)predvec, ix, (int32_T)sizeof
                    (real_T), &emlrtRTEI);
  idx = maxvec->size[0];
  for (ix = 0; ix < idx; ix++) {
    predvec->data[ix] = -sigm_coef * maxvec->data[ix];
  }

  c_st.site = &cb_emlrtRSI;
  b_exp(&c_st, predvec);
  ix = predvec->size[0];
  emxEnsureCapacity(&b_st, (emxArray__common *)predvec, ix, (int32_T)sizeof
                    (real_T), &emlrtRTEI);
  idx = predvec->size[0];
  for (ix = 0; ix < idx; ix++) {
    predvec->data[ix] = 1.0 - predvec->data[ix];
  }

  ix = newlyoccidx->size[0];
  newlyoccidx->size[0] = maxvec->size[0];
  emxEnsureCapacity(&b_st, (emxArray__common *)newlyoccidx, ix, (int32_T)sizeof
                    (real_T), &emlrtRTEI);
  idx = maxvec->size[0];
  for (ix = 0; ix < idx; ix++) {
    newlyoccidx->data[ix] = -sigm_coef * maxvec->data[ix];
  }

  c_st.site = &cb_emlrtRSI;
  b_exp(&c_st, newlyoccidx);
  ix = newlyoccidx->size[0];
  emxEnsureCapacity(&b_st, (emxArray__common *)newlyoccidx, ix, (int32_T)sizeof
                    (real_T), &emlrtRTEI);
  idx = newlyoccidx->size[0];
  for (ix = 0; ix < idx; ix++) {
    newlyoccidx->data[ix]++;
  }

  varargin_1[0] = predvec->size[0];
  varargin_1[1] = 1;
  varargin_2[0] = newlyoccidx->size[0];
  varargin_2[1] = 1;
  overflow = false;
  p = true;
  k = 0;
  exitg1 = false;
  while ((!exitg1) && (k < 2)) {
    if (!(varargin_1[k] == varargin_2[k])) {
      p = false;
      exitg1 = true;
    } else {
      k++;
    }
  }

  if (!p) {
  } else {
    overflow = true;
  }

  if (overflow) {
  } else {
    emlrtErrorWithMessageIdR2012b(&b_st, &w_emlrtRTEI, "MATLAB:dimagree", 0);
  }

  ix = predvec->size[0];
  emxEnsureCapacity(&b_st, (emxArray__common *)predvec, ix, (int32_T)sizeof
                    (real_T), &emlrtRTEI);
  idx = predvec->size[0];
  for (ix = 0; ix < idx; ix++) {
    predvec->data[ix] /= newlyoccidx->data[ix];
  }

  emxFree_real_T(&newlyoccidx);

  /*  7 Save previous grid */
  ix = cgridvecprev->size[0];
  cgridvecprev->size[0] = cgridvec->size[0];
  emxEnsureCapacity(sp, (emxArray__common *)cgridvecprev, ix, (int32_T)sizeof
                    (real_T), &emlrtRTEI);
  idx = cgridvec->size[0];
  for (ix = 0; ix < idx; ix++) {
    cgridvecprev->data[ix] = cgridvec->data[ix];
  }

  emlrtHeapReferenceStackLeaveFcnR2012b(sp);
}