/*
 * VFControl.cpp
 *
 * Academic License - for use in teaching, academic research, and meeting
 * course requirements at degree granting institutions only.  Not for
 * government, commercial, or other organizational use.
 *
 * Code generation for model "VFControl".
 *
 * Model version              : 1.1
 * Simulink Coder version : 24.1 (R2024a) 19-Nov-2023
 * C++ source code generated on : Wed Oct 16 11:33:22 2024
 *
 * Target selection: speedgoat.tlc
 * Note: GRT includes extra infrastructure and instrumentation for prototyping
 * Embedded hardware selection: Intel->x86-64 (Linux 64)
 * Code generation objectives: Unspecified
 * Validation result: Not run
 */

#include "VFControl.h"
#include "VFControl_cal.h"
#include "rtwtypes.h"
#include <cmath>
#include <cstring>
#include "rt_hypotd_snf.h"
#include "rt_invd4x4_snf.h"
#include "rt_look.h"
#include "rt_look1d.h"
#include "rt_remd_snf.h"
#include "rt_modd_snf.h"
#include "VFControl_private.h"
#include "rt_TDelayInterpolate.h"
#include "look1_pbinlxpw.h"
#include "PWMGenerator.h"

extern "C"
{

#include "rt_nonfinite.h"

}

/* Block signals (default storage) */
B_VFControl_T VFControl_B;

/* Block states (default storage) */
DW_VFControl_T VFControl_DW;

/* Real-time model */
RT_MODEL_VFControl_T VFControl_M_ = RT_MODEL_VFControl_T();
RT_MODEL_VFControl_T *const VFControl_M = &VFControl_M_;

/* Model step function for TID0 */
void VFControl_step0(void)             /* Sample time: [0.0s, 0.0s] */
{
  real_T tmp[16];
  real_T tmp_0[16];
  real_T Bias;
  real_T RLinv_m;
  real_T tmp_1;
  real_T tmp_2;
  real_T xk1;
  int32_T i;
  int32_T i_0;
  int32_T tmp_3;

  /* Reset subsysRan breadcrumbs */
  srClearBC(VFControl_DW.Saturation_SubsysRanBC);

  /* Reset subsysRan breadcrumbs */
  srClearBC(VFControl_DW.Rotorreferenceframe_SubsysRan_i);

  /* Reset subsysRan breadcrumbs */
  srClearBC(VFControl_DW.Stationaryreferenceframe_Subs_n);

  /* Reset subsysRan breadcrumbs */
  srClearBC(VFControl_DW.Synchronousreferenceframe_Sub_k);

  /* Reset subsysRan breadcrumbs */
  srClearBC(VFControl_DW.Rotorreferenceframe_SubsysRanBC);

  /* Reset subsysRan breadcrumbs */
  srClearBC(VFControl_DW.Stationaryreferenceframe_Subsys);

  /* Reset subsysRan breadcrumbs */
  srClearBC(VFControl_DW.Synchronousreferenceframe_Subsy);

  /* Reset subsysRan breadcrumbs */
  srClearBC(VFControl_DW.sinbetacosbetasinthcosth_Subsys);

  /* Reset subsysRan breadcrumbs */
  srClearBC(VFControl_DW.sinthrcosthr_SubsysRanBC);

  /* Reset subsysRan breadcrumbs */
  srClearBC(VFControl_DW.sinthrcosthr1_SubsysRanBC);

  /* UnitDelay: '<S23>/fluxes' */
  xk1 = VFControl_DW.fluxes_DSTATE[0];
  VFControl_B.xk1[0] = xk1;

  /* Gain: '<S21>/Gain' */
  xk1 *= VFControl_cal->Gain_Gain;
  VFControl_B.Gain[0] = xk1;

  /* UnitDelay: '<S21>/fluxes' */
  Bias = VFControl_DW.fluxes_DSTATE_l[0];
  VFControl_B.fluxes[0] = Bias;

  /* Sum: '<S21>/Sum2' */
  VFControl_B.Sum2[0] = xk1 - Bias;

  /* UnitDelay: '<S23>/fluxes' */
  xk1 = VFControl_DW.fluxes_DSTATE[1];
  VFControl_B.xk1[1] = xk1;

  /* Gain: '<S21>/Gain' */
  xk1 *= VFControl_cal->Gain_Gain;
  VFControl_B.Gain[1] = xk1;

  /* UnitDelay: '<S21>/fluxes' */
  Bias = VFControl_DW.fluxes_DSTATE_l[1];
  VFControl_B.fluxes[1] = Bias;

  /* Sum: '<S21>/Sum2' */
  VFControl_B.Sum2[1] = xk1 - Bias;

  /* UnitDelay: '<S23>/fluxes' */
  xk1 = VFControl_DW.fluxes_DSTATE[2];
  VFControl_B.xk1[2] = xk1;

  /* Gain: '<S21>/Gain' */
  xk1 *= VFControl_cal->Gain_Gain;
  VFControl_B.Gain[2] = xk1;

  /* UnitDelay: '<S21>/fluxes' */
  Bias = VFControl_DW.fluxes_DSTATE_l[2];
  VFControl_B.fluxes[2] = Bias;

  /* Sum: '<S21>/Sum2' */
  VFControl_B.Sum2[2] = xk1 - Bias;

  /* UnitDelay: '<S23>/fluxes' */
  xk1 = VFControl_DW.fluxes_DSTATE[3];
  VFControl_B.xk1[3] = xk1;

  /* Gain: '<S21>/Gain' */
  xk1 *= VFControl_cal->Gain_Gain;
  VFControl_B.Gain[3] = xk1;

  /* UnitDelay: '<S21>/fluxes' */
  Bias = VFControl_DW.fluxes_DSTATE_l[3];
  VFControl_B.fluxes[3] = Bias;

  /* Sum: '<S21>/Sum2' */
  VFControl_B.Sum2[3] = xk1 - Bias;

  /* Outputs for Enabled SubSystem: '<S16>/Saturation' incorporates:
   *  EnablePort: '<S22>/Enable'
   */
  /* Constant: '<S16>/Constant' */
  if (VFControl_cal->Constant_Value_e > 0.0) {
    /* UnitDelay: '<S22>/Delay' */
    VFControl_B.Delay = VFControl_DW.Delay_DSTATE;

    /* SignalConversion generated from: '<S24>/Math Function' incorporates:
     *  Constant: '<S24>/u2'
     *
     * About SignalConversion generated from '<S24>/Math Function':
     *  Operator: reciprocal
     */
    VFControl_B.TmpSignalConversionAtMathFuncti[0] = VFControl_cal->u2_Value[0];
    VFControl_B.TmpSignalConversionAtMathFuncti[1] = VFControl_cal->u2_Value[1];
    VFControl_B.TmpSignalConversionAtMathFuncti[2] = VFControl_B.Delay;

    /* Math: '<S24>/Math Function'
     *
     * About '<S24>/Math Function':
     *  Operator: reciprocal
     */
    xk1 = VFControl_B.TmpSignalConversionAtMathFuncti[0];
    xk1 = 1.0 / xk1;

    /* Math: '<S24>/Math Function'
     *
     * About '<S24>/Math Function':
     *  Operator: reciprocal
     */
    VFControl_B.MathFunction_p[0] = xk1;

    /* Math: '<S24>/Math Function'
     *
     * About '<S24>/Math Function':
     *  Operator: reciprocal
     */
    xk1 = VFControl_B.TmpSignalConversionAtMathFuncti[1];
    xk1 = 1.0 / xk1;

    /* Math: '<S24>/Math Function'
     *
     * About '<S24>/Math Function':
     *  Operator: reciprocal
     */
    VFControl_B.MathFunction_p[1] = xk1;

    /* Math: '<S24>/Math Function'
     *
     * About '<S24>/Math Function':
     *  Operator: reciprocal
     */
    xk1 = VFControl_B.TmpSignalConversionAtMathFuncti[2];
    xk1 = 1.0 / xk1;

    /* Math: '<S24>/Math Function'
     *
     * About '<S24>/Math Function':
     *  Operator: reciprocal
     */
    VFControl_B.MathFunction_p[2] = xk1;

    /* Sum: '<S24>/Sum2' */
    xk1 = VFControl_B.MathFunction_p[0];
    xk1 += VFControl_B.MathFunction_p[1];
    xk1 += VFControl_B.MathFunction_p[2];

    /* Sum: '<S24>/Sum2' */
    VFControl_B.Sum2_i = xk1;

    /* Math: '<S24>/Math Function1'
     *
     * About '<S24>/Math Function1':
     *  Operator: reciprocal
     */
    xk1 = VFControl_B.Sum2_i;

    /* Math: '<S24>/Math Function1'
     *
     * About '<S24>/Math Function1':
     *  Operator: reciprocal
     */
    VFControl_B.MathFunction1 = 1.0 / xk1;

    /* Product: '<S26>/Product2' incorporates:
     *  Constant: '<S26>/u1'
     */
    VFControl_B.Product2_m[0] = VFControl_cal->u1_Value[0] *
      VFControl_B.MathFunction1;
    VFControl_B.Product2_m[1] = VFControl_cal->u1_Value[1] *
      VFControl_B.MathFunction1;

    /* Product: '<S26>/Product' */
    VFControl_B.Product_c[0] = VFControl_B.Sum2[0] * VFControl_B.Product2_m[0];
    VFControl_B.Product_c[1] = VFControl_B.Product2_m[1] * VFControl_B.Sum2[2];

    /* Sum: '<S26>/Sum2' */
    xk1 = VFControl_B.Product_c[0];
    xk1 += VFControl_B.Product_c[1];

    /* Sum: '<S26>/Sum2' */
    VFControl_B.phimq = xk1;

    /* Product: '<S26>/Product1' */
    VFControl_B.Product1_j[0] = VFControl_B.Product2_m[0] * VFControl_B.Sum2[1];
    VFControl_B.Product1_j[1] = VFControl_B.Product2_m[1] * VFControl_B.Sum2[3];

    /* Sum: '<S26>/Sum1' */
    xk1 = VFControl_B.Product1_j[0];
    xk1 += VFControl_B.Product1_j[1];

    /* Sum: '<S26>/Sum1' */
    VFControl_B.phimd = xk1;

    /* Math: '<S22>/Math Function' */
    VFControl_B.Phisat = rt_hypotd_snf(VFControl_B.phimq, VFControl_B.phimd);

    /* Lookup_n-D: '<S22>/1-D Lookup Table' incorporates:
     *  Math: '<S22>/Math Function'
     */
    VFControl_B.Isat = look1_pbinlxpw(VFControl_B.Phisat,
      VFControl_cal->uDLookupTable_bp01Data,
      VFControl_cal->uDLookupTable_tableData, &VFControl_DW.m_bpIndex_b, 1U);

    /* Switch: '<S22>/Switch' */
    if (VFControl_B.Isat != 0.0) {
      /* Product: '<S22>/Product' */
      VFControl_B.Lm = VFControl_B.Phisat / VFControl_B.Isat;

      /* Switch: '<S22>/Switch' */
      VFControl_B.Switch_g = VFControl_B.Lm;
    } else {
      /* Switch: '<S22>/Switch' incorporates:
       *  Constant: '<S22>/Constant1'
       */
      VFControl_B.Switch_g = VFControl_cal->Constant1_Value;
    }

    /* End of Switch: '<S22>/Switch' */

    /* Assignment: '<S25>/Lm in rows[1,3] & col[1,3]' incorporates:
     *  Constant: '<S25>/u1'
     */
    std::memcpy(&VFControl_B.Lminrows13col13[0], &VFControl_cal->u1_Value_d[0],
                sizeof(real_T) << 4U);

    /* Assignment: '<S25>/Lm in rows[1,3] & col[1,3]' */
    VFControl_B.Lminrows13col13[0] = VFControl_B.Switch_g;
    VFControl_B.Lminrows13col13[2] = VFControl_B.Switch_g;
    VFControl_B.Lminrows13col13[8] = VFControl_B.Switch_g;
    VFControl_B.Lminrows13col13[10] = VFControl_B.Switch_g;

    /* Assignment: '<S25>/Lm in rows[2,4] & col[2,4]' incorporates:
     *  Assignment: '<S25>/Lm in rows[1,3] & col[1,3]'
     */
    std::memcpy(&VFControl_B.Lminrows24col24[0], &VFControl_B.Lminrows13col13[0],
                sizeof(real_T) << 4U);

    /* Assignment: '<S25>/Lm in rows[2,4] & col[2,4]' */
    VFControl_B.Lminrows24col24[5] = VFControl_B.Switch_g;
    VFControl_B.Lminrows24col24[7] = VFControl_B.Switch_g;
    VFControl_B.Lminrows24col24[13] = VFControl_B.Switch_g;
    VFControl_B.Lminrows24col24[15] = VFControl_B.Switch_g;
    for (i = 0; i < 16; i++) {
      /* Sum: '<S25>/Sum2' incorporates:
       *  Assignment: '<S25>/Lm in rows[2,4] & col[2,4]'
       *  Constant: '<S25>/u5'
       */
      VFControl_B.Sum2_e[i] = VFControl_B.Lminrows24col24[i] +
        VFControl_cal->u5_Value[i];
    }

    /* Product: '<S22>/inversion' incorporates:
     *  Sum: '<S25>/Sum2'
     */
    rt_invd4x4_snf(VFControl_B.Sum2_e, VFControl_B.Linv_h);

    /* Product: '<S22>/Product1' incorporates:
     *  Constant: '<S22>/u1'
     *  Product: '<S22>/inversion'
     */
    std::memcpy(&tmp[0], &VFControl_cal->u1_Value_e[0], sizeof(real_T) << 4U);
    std::memcpy(&tmp_0[0], &VFControl_B.Linv_h[0], sizeof(real_T) << 4U);
    for (i = 0; i < 4; i++) {
      /* Product: '<S22>/Product1' */
      tmp_3 = i << 2;
      xk1 = tmp_0[tmp_3];
      Bias = tmp_0[tmp_3 + 1];
      tmp_1 = tmp_0[tmp_3 + 2];
      tmp_2 = tmp_0[tmp_3 + 3];
      for (i_0 = 0; i_0 < 4; i_0++) {
        /* Product: '<S22>/Product1' */
        RLinv_m = xk1 * tmp[i_0];
        RLinv_m += tmp[i_0 + 4] * Bias;
        RLinv_m += tmp[i_0 + 8] * tmp_1;
        RLinv_m += tmp[i_0 + 12] * tmp_2;

        /* Product: '<S22>/Product1' */
        VFControl_B.RLinv_m[i_0 + tmp_3] = RLinv_m;
      }
    }

    /* Update for UnitDelay: '<S22>/Delay' */
    VFControl_DW.Delay_DSTATE = VFControl_B.Switch_g;
    srUpdateBC(VFControl_DW.Saturation_SubsysRanBC);
  }

  /* End of Constant: '<S16>/Constant' */
  /* End of Outputs for SubSystem: '<S16>/Saturation' */
  for (i = 0; i < 16; i++) {
    /* Switch: '<S16>/Switch' incorporates:
     *  Constant: '<S16>/Constant1'
     */
    if (VFControl_cal->Constant1_Value_k >= VFControl_cal->Switch_Threshold) {
      /* Switch: '<S16>/Switch' incorporates:
       *  Product: '<S22>/inversion'
       */
      xk1 = VFControl_B.Linv_h[i];
    } else {
      /* Switch: '<S16>/Switch' incorporates:
       *  Constant: '<S16>/Constant2'
       */
      xk1 = VFControl_cal->Constant2_Value[i];
    }

    /* End of Switch: '<S16>/Switch' */

    /* Switch: '<S16>/Switch' */
    VFControl_B.Linv[i] = xk1;

    /* Product: '<S16>/Product3' incorporates:
     *  Switch: '<S16>/Switch'
     */
    tmp[i] = xk1;
  }

  /* Product: '<S16>/Product3' */
  Bias = VFControl_B.xk1[0];
  tmp_1 = VFControl_B.xk1[1];
  tmp_2 = VFControl_B.xk1[2];
  RLinv_m = VFControl_B.xk1[3];
  for (i = 0; i < 4; i++) {
    xk1 = tmp[i] * Bias;
    xk1 += tmp[i + 4] * tmp_1;
    xk1 += tmp[i + 8] * tmp_2;
    xk1 += tmp[i + 12] * RLinv_m;

    /* Product: '<S16>/Product3' */
    VFControl_B.Product3[i] = xk1;
  }

  /* DiscreteIntegrator: '<S15>/Rotor angle thetam' */
  VFControl_B.Rotoranglethetam = VFControl_DW.Rotoranglethetam_DSTATE;

  /* UnitDelay: '<S38>/wm_delay' */
  VFControl_B.wm_delay = VFControl_DW.wm_delay_DSTATE;

  /* Gain: '<S38>/F2' */
  VFControl_B.F2 = VFControl_cal->F2_Gain * VFControl_B.wm_delay;

  /* UnitDelay: '<S38>/wm_predict' */
  VFControl_B.wm_predict = VFControl_DW.wm_predict_DSTATE;

  /* Sum: '<S38>/Sum1' */
  VFControl_B.Sum1 = VFControl_B.F2 - VFControl_B.wm_predict;

  /* Outputs for Enabled SubSystem: '<S19>/sin(thr),cos(thr)' incorporates:
   *  EnablePort: '<S36>/Enable'
   */
  /* Constant: '<S19>/Constant' */
  if (VFControl_cal->Constant_Value_b) {
    VFControl_DW.sinthrcosthr_MODE = true;

    /* Constant: '<S36>/Constant' */
    VFControl_B.Constant_e[0] = VFControl_cal->Constant_Value[0];
    VFControl_B.Constant_e[1] = VFControl_cal->Constant_Value[1];

    /* Gain: '<S36>/Gain1' */
    VFControl_B.Gain1_e = VFControl_cal->Gain1_Gain * VFControl_B.Sum1;

    /* Trigonometry: '<S36>/Trigonometric Function' */
    xk1 = VFControl_B.Rotoranglethetam;
    Bias = std::sin(xk1);
    xk1 = std::cos(xk1);

    /* Trigonometry: '<S36>/Trigonometric Function' */
    VFControl_B.TrigonometricFunction_o1_d = Bias;

    /* Trigonometry: '<S36>/Trigonometric Function' */
    VFControl_B.TrigonometricFunction_o2_j = xk1;

    /* Assignment: '<S36>/W(1,2)=wr' incorporates:
     *  Constant: '<S36>/u1'
     */
    std::memcpy(&VFControl_B.W12wr[0], &VFControl_cal->u1_Value_j[0], sizeof
                (real_T) << 4U);

    /* Assignment: '<S36>/W(1,2)=wr' */
    VFControl_B.W12wr[4] = VFControl_B.Sum1;

    /* Assignment: '<S36>/W(2,1)=-wr' incorporates:
     *  Assignment: '<S36>/W(1,2)=wr'
     */
    std::memcpy(&VFControl_B.W21wr[0], &VFControl_B.W12wr[0], sizeof(real_T) <<
                4U);

    /* Assignment: '<S36>/W(2,1)=-wr' */
    VFControl_B.W21wr[1] = VFControl_B.Gain1_e;
    srUpdateBC(VFControl_DW.sinthrcosthr_SubsysRanBC);
  } else if (VFControl_DW.sinthrcosthr_MODE) {
    /* Disable for Trigonometry: '<S36>/Trigonometric Function' incorporates:
     *  Outport: '<S36>/sin(thr),cos(thr)'
     */
    VFControl_B.TrigonometricFunction_o1_d = VFControl_cal->sinthrcosthr_Y0;

    /* Disable for Trigonometry: '<S36>/Trigonometric Function' incorporates:
     *  Outport: '<S36>/sin(thr),cos(thr)'
     */
    VFControl_B.TrigonometricFunction_o2_j = VFControl_cal->sinthrcosthr_Y0;

    /* Disable for Outport: '<S36>/sin(thr),cos(thr)' incorporates:
     *  Constant: '<S36>/Constant'
     */
    VFControl_B.Constant_e[0] = VFControl_cal->sinthrcosthr_Y0;
    VFControl_B.Constant_e[1] = VFControl_cal->sinthrcosthr_Y0;
    for (i = 0; i < 16; i++) {
      /* Disable for Assignment: '<S36>/W(2,1)=-wr' incorporates:
       *  Outport: '<S36>/W'
       */
      VFControl_B.W21wr[i] = VFControl_cal->W_Y0_m;
    }

    VFControl_DW.sinthrcosthr_MODE = false;
  }

  /* End of Constant: '<S19>/Constant' */
  /* End of Outputs for SubSystem: '<S19>/sin(thr),cos(thr)' */

  /* Outputs for Enabled SubSystem: '<S19>/sin(thr),cos(thr)1' incorporates:
   *  EnablePort: '<S37>/Enable'
   */
  /* Constant: '<S19>/Constant1' */
  if (VFControl_cal->Constant1_Value_kj) {
    VFControl_DW.sinthrcosthr1_MODE = true;

    /* Constant: '<S37>/Constant' */
    VFControl_B.Constant[0] = VFControl_cal->Constant_Value_n[0];
    VFControl_B.Constant[1] = VFControl_cal->Constant_Value_n[1];

    /* Gain: '<S37>/Gain3' */
    VFControl_B.Gain3_e = VFControl_cal->Gain3_Gain * VFControl_B.Sum1;

    /* Trigonometry: '<S37>/Trigonometric Function' */
    xk1 = VFControl_B.Rotoranglethetam;
    Bias = std::sin(xk1);
    xk1 = std::cos(xk1);

    /* Trigonometry: '<S37>/Trigonometric Function' */
    VFControl_B.TrigonometricFunction_o1 = Bias;

    /* Trigonometry: '<S37>/Trigonometric Function' */
    VFControl_B.TrigonometricFunction_o2 = xk1;

    /* Assignment: '<S37>/W(3,4)=-wr' incorporates:
     *  Constant: '<S37>/u4'
     */
    std::memcpy(&VFControl_B.W34wr[0], &VFControl_cal->u4_Value[0], sizeof
                (real_T) << 4U);

    /* Assignment: '<S37>/W(3,4)=-wr' */
    VFControl_B.W34wr[14] = VFControl_B.Gain3_e;

    /* Assignment: '<S37>/W(4,3)=wr' incorporates:
     *  Assignment: '<S37>/W(3,4)=-wr'
     */
    std::memcpy(&VFControl_B.W43wr[0], &VFControl_B.W34wr[0], sizeof(real_T) <<
                4U);

    /* Assignment: '<S37>/W(4,3)=wr' */
    VFControl_B.W43wr[11] = VFControl_B.Sum1;
    srUpdateBC(VFControl_DW.sinthrcosthr1_SubsysRanBC);
  } else if (VFControl_DW.sinthrcosthr1_MODE) {
    /* Disable for Trigonometry: '<S37>/Trigonometric Function' incorporates:
     *  Outport: '<S37>/sin(thr),cos(thr)'
     */
    VFControl_B.TrigonometricFunction_o1 = VFControl_cal->sinthrcosthr_Y0_h;

    /* Disable for Trigonometry: '<S37>/Trigonometric Function' incorporates:
     *  Outport: '<S37>/sin(thr),cos(thr)'
     */
    VFControl_B.TrigonometricFunction_o2 = VFControl_cal->sinthrcosthr_Y0_h;

    /* Disable for Outport: '<S37>/sin(thr),cos(thr)' incorporates:
     *  Constant: '<S37>/Constant'
     */
    VFControl_B.Constant[0] = VFControl_cal->sinthrcosthr_Y0_h;
    VFControl_B.Constant[1] = VFControl_cal->sinthrcosthr_Y0_h;
    VFControl_DW.sinthrcosthr1_MODE = false;
  }

  /* End of Constant: '<S19>/Constant1' */
  /* End of Outputs for SubSystem: '<S19>/sin(thr),cos(thr)1' */

  /* Outputs for Enabled SubSystem: '<S19>/sin(beta),cos(beta),sin(th),cos(th)' incorporates:
   *  EnablePort: '<S35>/Enable'
   */
  /* Constant: '<S19>/Constant3' */
  if (VFControl_cal->Constant3_Value_h) {
    /* DigitalClock: '<S35>/Digital Clock' */
    VFControl_B.DigitalClock_kx = VFControl_M->Timing.t[1];

    /* Sum: '<S35>/Sum' incorporates:
     *  Constant: '<S35>/we'
     */
    VFControl_B.wewr = VFControl_cal->we_Value - VFControl_B.Sum1;

    /* Gain: '<S35>/Gain2' */
    VFControl_B.Gain2_k = VFControl_cal->Gain2_Gain * VFControl_B.wewr;

    /* Gain: '<S35>/web_psb' */
    VFControl_B.th = VFControl_cal->web_psb_Gain * VFControl_B.DigitalClock_kx;

    /* Sum: '<S35>/Sum1' */
    VFControl_B.beta = VFControl_B.th - VFControl_B.Rotoranglethetam;

    /* Trigonometry: '<S35>/Trigonometric Function' */
    xk1 = VFControl_B.th;
    Bias = std::sin(xk1);
    xk1 = std::cos(xk1);

    /* Trigonometry: '<S35>/Trigonometric Function' */
    VFControl_B.TrigonometricFunction_o1_dn = Bias;

    /* Trigonometry: '<S35>/Trigonometric Function' */
    VFControl_B.TrigonometricFunction_o2_k = xk1;

    /* Trigonometry: '<S35>/Trigonometric Function1' */
    xk1 = VFControl_B.beta;
    Bias = std::sin(xk1);
    xk1 = std::cos(xk1);

    /* Trigonometry: '<S35>/Trigonometric Function1' */
    VFControl_B.TrigonometricFunction1_o1 = Bias;

    /* Trigonometry: '<S35>/Trigonometric Function1' */
    VFControl_B.TrigonometricFunction1_o2 = xk1;

    /* Assignment: '<S35>/W(3,4)=1-wr' incorporates:
     *  Constant: '<S35>/u3'
     */
    std::memcpy(&VFControl_B.W341wr[0], &VFControl_cal->u3_Value[0], sizeof
                (real_T) << 4U);

    /* Assignment: '<S35>/W(3,4)=1-wr' */
    VFControl_B.W341wr[14] = VFControl_B.wewr;

    /* Assignment: '<S35>/W(4,3)=wr-1' incorporates:
     *  Assignment: '<S35>/W(3,4)=1-wr'
     */
    std::memcpy(&VFControl_B.W43wr1[0], &VFControl_B.W341wr[0], sizeof(real_T) <<
                4U);

    /* Assignment: '<S35>/W(4,3)=wr-1' */
    VFControl_B.W43wr1[11] = VFControl_B.Gain2_k;
    srUpdateBC(VFControl_DW.sinbetacosbetasinthcosth_Subsys);
  }

  /* End of Constant: '<S19>/Constant3' */
  /* End of Outputs for SubSystem: '<S19>/sin(beta),cos(beta),sin(th),cos(th)' */

  /* MultiPortSwitch: '<S19>/Multiport Switch' incorporates:
   *  Constant: '<S19>/Constant2'
   */
  switch (static_cast<int32_T>(VFControl_cal->Constant2_Value_n)) {
   case 1:
    /* MultiPortSwitch: '<S19>/Multiport Switch' */
    VFControl_B.MultiportSwitch[0] = VFControl_B.TrigonometricFunction_o1_d;
    VFControl_B.MultiportSwitch[1] = VFControl_B.TrigonometricFunction_o2_j;
    VFControl_B.MultiportSwitch[2] = VFControl_B.Constant_e[0];
    VFControl_B.MultiportSwitch[3] = VFControl_B.Constant_e[1];
    break;

   case 2:
    /* MultiPortSwitch: '<S19>/Multiport Switch' */
    VFControl_B.MultiportSwitch[0] = VFControl_B.TrigonometricFunction_o1;
    VFControl_B.MultiportSwitch[1] = VFControl_B.TrigonometricFunction_o2;
    VFControl_B.MultiportSwitch[2] = VFControl_B.Constant[0];
    VFControl_B.MultiportSwitch[3] = VFControl_B.Constant[1];
    break;

   default:
    /* MultiPortSwitch: '<S19>/Multiport Switch' */
    VFControl_B.MultiportSwitch[0] = VFControl_B.TrigonometricFunction1_o1;
    VFControl_B.MultiportSwitch[1] = VFControl_B.TrigonometricFunction1_o2;
    VFControl_B.MultiportSwitch[2] = VFControl_B.TrigonometricFunction_o1_dn;
    VFControl_B.MultiportSwitch[3] = VFControl_B.TrigonometricFunction_o2_k;
    break;
  }

  /* End of MultiPortSwitch: '<S19>/Multiport Switch' */

  /* Outputs for Enabled SubSystem: '<S18>/Rotor reference frame' incorporates:
   *  EnablePort: '<S32>/Enable'
   */
  /* Constant: '<S18>/Constant' */
  if (VFControl_cal->Constant_Value_px) {
    VFControl_DW.Rotorreferenceframe_MODE = true;

    /* Fcn: '<S32>/ira' */
    VFControl_B.ira_p = VFControl_B.Product3[2];

    /* Fcn: '<S32>/irb' */
    VFControl_B.irb_n = -(1.7320508075688772 * VFControl_B.Product3[3] +
                          VFControl_B.Product3[2]) / 2.0;

    /* Fcn: '<S32>/isa' */
    VFControl_B.isa_m = VFControl_B.Product3[0] * VFControl_B.MultiportSwitch[1]
      + VFControl_B.MultiportSwitch[0] * VFControl_B.Product3[1];

    /* Fcn: '<S32>/isb' */
    VFControl_B.isb_p = ((1.7320508075688772 * VFControl_B.MultiportSwitch[0] -
                          VFControl_B.MultiportSwitch[1]) *
                         VFControl_B.Product3[0] + (-1.7320508075688772 *
      VFControl_B.MultiportSwitch[1] - VFControl_B.MultiportSwitch[0]) *
                         VFControl_B.Product3[1]) / 2.0;
    srUpdateBC(VFControl_DW.Rotorreferenceframe_SubsysRanBC);
  } else if (VFControl_DW.Rotorreferenceframe_MODE) {
    /* Disable for Fcn: '<S32>/ira' incorporates:
     *  Outport: '<S32>/ira,irb'
     */
    VFControl_B.ira_p = VFControl_cal->irairb_Y0;

    /* Disable for Fcn: '<S32>/irb' incorporates:
     *  Outport: '<S32>/ira,irb'
     */
    VFControl_B.irb_n = VFControl_cal->irairb_Y0;

    /* Disable for Fcn: '<S32>/isa' incorporates:
     *  Outport: '<S32>/isa,isb'
     */
    VFControl_B.isa_m = VFControl_cal->isaisb_Y0;

    /* Disable for Fcn: '<S32>/isb' incorporates:
     *  Outport: '<S32>/isa,isb'
     */
    VFControl_B.isb_p = VFControl_cal->isaisb_Y0;
    VFControl_DW.Rotorreferenceframe_MODE = false;
  }

  /* End of Constant: '<S18>/Constant' */
  /* End of Outputs for SubSystem: '<S18>/Rotor reference frame' */

  /* Outputs for Enabled SubSystem: '<S18>/Stationary reference frame' incorporates:
   *  EnablePort: '<S33>/Enable'
   */
  /* Constant: '<S18>/Constant1' */
  if (VFControl_cal->Constant1_Value_b) {
    VFControl_DW.Stationaryreferenceframe_MODE = true;

    /* Fcn: '<S33>/ira' */
    VFControl_B.ira_g = VFControl_B.MultiportSwitch[1] * VFControl_B.Product3[2]
      - VFControl_B.MultiportSwitch[0] * VFControl_B.Product3[3];

    /* Fcn: '<S33>/irb' */
    VFControl_B.irb_g = ((-VFControl_B.MultiportSwitch[1] - 1.7320508075688772 *
                          VFControl_B.MultiportSwitch[0]) *
                         VFControl_B.Product3[2] + (VFControl_B.MultiportSwitch
      [0] - 1.7320508075688772 * VFControl_B.MultiportSwitch[1]) *
                         VFControl_B.Product3[3]) / 2.0;

    /* Fcn: '<S33>/isa' */
    VFControl_B.isa_a = VFControl_B.Product3[0];

    /* Fcn: '<S33>/isb' */
    VFControl_B.isb_f = -(1.7320508075688772 * VFControl_B.Product3[1] +
                          VFControl_B.Product3[0]) / 2.0;
    srUpdateBC(VFControl_DW.Stationaryreferenceframe_Subsys);
  } else if (VFControl_DW.Stationaryreferenceframe_MODE) {
    /* Disable for Fcn: '<S33>/ira' incorporates:
     *  Outport: '<S33>/ira,irb'
     */
    VFControl_B.ira_g = VFControl_cal->irairb_Y0_d;

    /* Disable for Fcn: '<S33>/irb' incorporates:
     *  Outport: '<S33>/ira,irb'
     */
    VFControl_B.irb_g = VFControl_cal->irairb_Y0_d;

    /* Disable for Fcn: '<S33>/isa' incorporates:
     *  Outport: '<S33>/isa,isb'
     */
    VFControl_B.isa_a = VFControl_cal->isaisb_Y0_k;

    /* Disable for Fcn: '<S33>/isb' incorporates:
     *  Outport: '<S33>/isa,isb'
     */
    VFControl_B.isb_f = VFControl_cal->isaisb_Y0_k;
    VFControl_DW.Stationaryreferenceframe_MODE = false;
  }

  /* End of Constant: '<S18>/Constant1' */
  /* End of Outputs for SubSystem: '<S18>/Stationary reference frame' */

  /* Outputs for Enabled SubSystem: '<S18>/Synchronous reference frame' incorporates:
   *  EnablePort: '<S34>/Enable'
   */
  /* Constant: '<S18>/Constant2' */
  if (VFControl_cal->Constant2_Value_d) {
    VFControl_DW.Synchronousreferenceframe_MODE = true;

    /* Fcn: '<S34>/ira' */
    VFControl_B.ira = VFControl_B.MultiportSwitch[1] * VFControl_B.Product3[2] +
      VFControl_B.MultiportSwitch[0] * VFControl_B.Product3[3];

    /* Fcn: '<S34>/irb' */
    VFControl_B.irb = ((1.7320508075688772 * VFControl_B.MultiportSwitch[0] -
                        VFControl_B.MultiportSwitch[1]) * VFControl_B.Product3[2]
                       + (-1.7320508075688772 * VFControl_B.MultiportSwitch[1] -
                          VFControl_B.MultiportSwitch[0]) *
                       VFControl_B.Product3[3]) / 2.0;

    /* Fcn: '<S34>/isa' */
    VFControl_B.isa = VFControl_B.Product3[0] * VFControl_B.MultiportSwitch[3] +
      VFControl_B.Product3[1] * VFControl_B.MultiportSwitch[2];

    /* Fcn: '<S34>/isb' */
    VFControl_B.isb = ((1.7320508075688772 * VFControl_B.MultiportSwitch[2] -
                        VFControl_B.MultiportSwitch[3]) * VFControl_B.Product3[0]
                       + (-1.7320508075688772 * VFControl_B.MultiportSwitch[3] -
                          VFControl_B.MultiportSwitch[2]) *
                       VFControl_B.Product3[1]) / 2.0;
    srUpdateBC(VFControl_DW.Synchronousreferenceframe_Subsy);
  } else if (VFControl_DW.Synchronousreferenceframe_MODE) {
    /* Disable for Fcn: '<S34>/ira' incorporates:
     *  Outport: '<S34>/ira,irb'
     */
    VFControl_B.ira = VFControl_cal->irairb_Y0_h;

    /* Disable for Fcn: '<S34>/irb' incorporates:
     *  Outport: '<S34>/ira,irb'
     */
    VFControl_B.irb = VFControl_cal->irairb_Y0_h;

    /* Disable for Fcn: '<S34>/isa' incorporates:
     *  Outport: '<S34>/isa,isb'
     */
    VFControl_B.isa = VFControl_cal->isaisb_Y0_m;

    /* Disable for Fcn: '<S34>/isb' incorporates:
     *  Outport: '<S34>/isa,isb'
     */
    VFControl_B.isb = VFControl_cal->isaisb_Y0_m;
    VFControl_DW.Synchronousreferenceframe_MODE = false;
  }

  /* End of Constant: '<S18>/Constant2' */
  /* End of Outputs for SubSystem: '<S18>/Synchronous reference frame' */

  /* MultiPortSwitch: '<S18>/Multiport Switch' incorporates:
   *  Constant: '<S18>/Constant3'
   */
  switch (static_cast<int32_T>(VFControl_cal->Constant3_Value_e)) {
   case 1:
    /* MultiPortSwitch: '<S18>/Multiport Switch' */
    VFControl_B.MultiportSwitch_p[0] = VFControl_B.ira_p;
    VFControl_B.MultiportSwitch_p[1] = VFControl_B.irb_n;
    break;

   case 2:
    /* MultiPortSwitch: '<S18>/Multiport Switch' */
    VFControl_B.MultiportSwitch_p[0] = VFControl_B.ira_g;
    VFControl_B.MultiportSwitch_p[1] = VFControl_B.irb_g;
    break;

   default:
    /* MultiPortSwitch: '<S18>/Multiport Switch' */
    VFControl_B.MultiportSwitch_p[0] = VFControl_B.ira;
    VFControl_B.MultiportSwitch_p[1] = VFControl_B.irb;
    break;
  }

  /* End of MultiPortSwitch: '<S18>/Multiport Switch' */

  /* MultiPortSwitch: '<S18>/Multiport Switch1' incorporates:
   *  Constant: '<S18>/Constant4'
   */
  switch (static_cast<int32_T>(VFControl_cal->Constant4_Value_c)) {
   case 1:
    /* MultiPortSwitch: '<S18>/Multiport Switch1' */
    VFControl_B.MultiportSwitch1[0] = VFControl_B.isa_m;
    VFControl_B.MultiportSwitch1[1] = VFControl_B.isb_p;
    break;

   case 2:
    /* MultiPortSwitch: '<S18>/Multiport Switch1' */
    VFControl_B.MultiportSwitch1[0] = VFControl_B.isa_a;
    VFControl_B.MultiportSwitch1[1] = VFControl_B.isb_f;
    break;

   default:
    /* MultiPortSwitch: '<S18>/Multiport Switch1' */
    VFControl_B.MultiportSwitch1[0] = VFControl_B.isa;
    VFControl_B.MultiportSwitch1[1] = VFControl_B.isb;
    break;
  }

  /* End of MultiPortSwitch: '<S18>/Multiport Switch1' */

  /* Gain: '<S18>/ib' */
  VFControl_B.ib[0] = VFControl_cal->ib_Gain * VFControl_B.MultiportSwitch_p[0];
  xk1 = VFControl_cal->ib_Gain * VFControl_B.MultiportSwitch1[0];
  VFControl_B.ib[2] = xk1;

  /* Gain: '<S13>/Gain Vr_Vs1' */
  VFControl_B.GainVr_Vs1[0] = VFControl_cal->GainVr_Vs1_Gain[0] * xk1;

  /* Gain: '<S18>/ib' */
  VFControl_B.ib[1] = VFControl_cal->ib_Gain * VFControl_B.MultiportSwitch_p[1];
  xk1 = VFControl_cal->ib_Gain * VFControl_B.MultiportSwitch1[1];
  VFControl_B.ib[3] = xk1;

  /* Gain: '<S13>/Gain Vr_Vs1' */
  VFControl_B.GainVr_Vs1[1] = VFControl_cal->GainVr_Vs1_Gain[1] * xk1;

  /* S-Function (sfun_spssw_discc): '<S96>/State-Space' incorporates:
   *  Constant: '<S39>/DC'
   *  Constant: '<S98>/SwitchCurrents'
   */

  /* S-Function block: <S96>/State-Space */
  {
    real_T accum;

    /* Circuit has switches */
    int_T *switch_status = (int_T*) VFControl_DW.StateSpace_PWORK.SWITCH_STATUS;
    int_T *switch_status_init = (int_T*)
      VFControl_DW.StateSpace_PWORK.SWITCH_STATUS_INIT;
    int_T *SwitchChange = (int_T*) VFControl_DW.StateSpace_PWORK.SW_CHG;
    int_T *gState = (int_T*) VFControl_DW.StateSpace_PWORK.G_STATE;
    real_T *yswitch = (real_T*)VFControl_DW.StateSpace_PWORK.Y_SWITCH;
    int_T *switchTypes = (int_T*) VFControl_DW.StateSpace_PWORK.SWITCH_TYPES;
    int_T *idxOutSw = (int_T*) VFControl_DW.StateSpace_PWORK.IDX_OUT_SW;
    real_T *DxCol = (real_T*)VFControl_DW.StateSpace_PWORK.DX_COL;
    real_T *tmp2 = (real_T*)VFControl_DW.StateSpace_PWORK.TMP2;
    real_T *uswlast = (real_T*)VFControl_DW.StateSpace_PWORK.USWLAST;
    int_T newState;
    int_T swChanged = 0;
    int loopsToDo = 20;
    real_T temp;

    /* keep an initial copy of switch_status*/
    memcpy(switch_status_init, switch_status, 6 * sizeof(int_T));
    memcpy(uswlast, &VFControl_B.StateSpace_o1[0], 6*sizeof(real_T));
    do {
      if (loopsToDo == 1) {            /* Need to reset some variables: */
        swChanged = 0;

        /* return to the original switch status*/
        {
          int_T i1;
          for (i1=0; i1 < 6; i1++) {
            swChanged = ((SwitchChange[i1] = switch_status_init[i1] -
                          switch_status[i1]) != 0) ? 1 : swChanged;
            switch_status[i1] = switch_status_init[i1];
          }
        }
      } else {
        /*
         * Compute outputs:
         * ---------------
         */
        real_T *Ds = (real_T*)VFControl_DW.StateSpace_PWORK.DS;

        {
          int_T i1;
          real_T *y0 = &VFControl_B.StateSpace_o1[0];
          for (i1=0; i1 < 14; i1++) {
            accum = 0.0;

            {
              int_T i2;
              const real_T *u0 = &VFControl_cal->SwitchCurrents_Value[0];
              for (i2=0; i2 < 6; i2++) {
                accum += *(Ds++) * u0[i2];
              }

              accum += *(Ds++) * VFControl_B.GainVr_Vs1[0];
              accum += *(Ds++) * VFControl_B.GainVr_Vs1[1];
              accum += *(Ds++) * VFControl_cal->DCVoltageSource_Amplitude;
            }

            y0[i1] = accum;
          }
        }

        swChanged = 0;

        {
          int_T i1;
          real_T *y0 = &VFControl_B.StateSpace_o1[0];
          for (i1=0; i1 < 6; i1++) {
            newState = ((y0[i1] > 0.0) && (gState[i1] > 0)) || (y0[i1] < 0.0) ?
              1 : (((y0[i1] > 0.0) && gState[i1] == 0) ? 0 : switch_status[i1]);
            swChanged = ((SwitchChange[i1] = newState - switch_status[i1]) != 0)
              ? 1 : swChanged;
            switch_status[i1] = newState;/* Keep new state */
          }
        }
      }

      /*
       * Compute new As, Bs, Cs and Ds matrixes:
       * --------------------------------------
       */
      if (swChanged) {
        real_T *Ds = (real_T*)VFControl_DW.StateSpace_PWORK.DS;
        real_T a1;

        {
          int_T i1;
          for (i1=0; i1 < 6; i1++) {
            if (SwitchChange[i1] != 0) {
              a1 = 1000.0*SwitchChange[i1];
              temp = 1/(1-Ds[i1*10]*a1);

              {
                int_T i2;
                for (i2=0; i2 < 14; i2++) {
                  DxCol[i2]= Ds[i2 * 9 + i1]*temp*a1;
                }
              }

              DxCol[i1] = temp;

              /* Copy row nSw of Ds into tmp2 and zero it out in Ds */
              memcpy(tmp2, &Ds[i1 * 9], 9 * sizeof(real_T));
              memset(&Ds[i1 * 9], '\0', 9 * sizeof(real_T));

              /* Cs = Cs + DxCol * tmp1, Ds = Ds + DxCol * tmp2 *******************/
              {
                int_T i2;
                for (i2=0; i2 < 14; i2++) {
                  a1 = DxCol[i2];

                  {
                    int_T i3;
                    for (i3=0; i3 < 9; i3++) {
                      Ds[i2 * 9 + i3] += a1 * tmp2[i3];
                    }
                  }
                }
              }
            }
          }
        }
      }                                /* if (swChanged) */
    } while (swChanged > 0 && --loopsToDo > 0);

    if (loopsToDo == 0) {
      real_T *Ds = (real_T*)VFControl_DW.StateSpace_PWORK.DS;

      {
        int_T i1;
        real_T *y0 = &VFControl_B.StateSpace_o1[0];
        for (i1=0; i1 < 14; i1++) {
          accum = 0.0;

          {
            int_T i2;
            const real_T *u0 = &VFControl_cal->SwitchCurrents_Value[0];
            for (i2=0; i2 < 6; i2++) {
              accum += *(Ds++) * u0[i2];
            }

            accum += *(Ds++) * VFControl_B.GainVr_Vs1[0];
            accum += *(Ds++) * VFControl_B.GainVr_Vs1[1];
            accum += *(Ds++) * VFControl_cal->DCVoltageSource_Amplitude;
          }

          y0[i1] = accum;
        }
      }
    }

    /* Output new switches states */
    {
      int_T i1;
      real_T *y1 = &VFControl_B.StateSpace_o2[0];
      for (i1=0; i1 < 6; i1++) {
        y1[i1] = (real_T)switch_status[i1];
      }
    }
  }

  /* Gain: '<S20>/1-1' */
  VFControl_B.iqsids[0] = VFControl_cal->u1_Gain[0] * VFControl_B.Product3[0];
  VFControl_B.iqsids[1] = VFControl_cal->u1_Gain[1] * VFControl_B.Product3[1];

  /* Product: '<S20>/Mult1' */
  VFControl_B.Mult1[0] = VFControl_B.iqsids[0] * VFControl_B.xk1[1];
  VFControl_B.Mult1[1] = VFControl_B.xk1[0] * VFControl_B.iqsids[1];

  /* Switch: '<S16>/Switch2' incorporates:
   *  Constant: '<S16>/Constant5'
   */
  if (VFControl_cal->Constant5_Value >= VFControl_cal->Switch2_Threshold) {
    /* Switch: '<S16>/Switch2' */
    VFControl_B.Switch2 = VFControl_B.Switch_g;
  } else {
    /* Switch: '<S16>/Switch2' incorporates:
     *  Constant: '<S16>/Lm_nosat'
     */
    VFControl_B.Switch2 = VFControl_cal->Lm_nosat_Value;
  }

  /* End of Switch: '<S16>/Switch2' */

  /* Sum: '<S20>/Sum2' */
  xk1 = VFControl_B.Mult1[0];

  /* Gain: '<S13>/Gain Vr_Vs' */
  Bias = VFControl_cal->GainVr_Vs_Gain[0] * VFControl_B.StateSpace_o1[6];
  VFControl_B.GainVr_Vs[0] = Bias;

  /* Gain: '<S17>/1_Vb' incorporates:
   *  Constant: '<S31>/Constant6'
   */
  VFControl_B.u_Vb[0] = VFControl_cal->u_Vb_Gain *
    VFControl_cal->Constant6_Value[0];
  VFControl_B.u_Vb[2] = VFControl_cal->u_Vb_Gain * Bias;

  /* Sum: '<S20>/Sum2' */
  xk1 += VFControl_B.Mult1[1];

  /* Gain: '<S13>/Gain Vr_Vs' */
  Bias = VFControl_cal->GainVr_Vs_Gain[1] * VFControl_B.StateSpace_o1[7];
  VFControl_B.GainVr_Vs[1] = Bias;

  /* Gain: '<S17>/1_Vb' incorporates:
   *  Constant: '<S31>/Constant6'
   */
  VFControl_B.u_Vb[1] = VFControl_cal->u_Vb_Gain *
    VFControl_cal->Constant6_Value[1];
  VFControl_B.u_Vb[3] = VFControl_cal->u_Vb_Gain * Bias;

  /* Sum: '<S20>/Sum2' */
  VFControl_B.Sum2_k = xk1;

  /* Outputs for Enabled SubSystem: '<S17>/Rotor reference frame' incorporates:
   *  EnablePort: '<S28>/Enable'
   */
  /* Constant: '<S17>/Constant' */
  if (VFControl_cal->Constant_Value_j) {
    VFControl_DW.Rotorreferenceframe_MODE_m = true;

    /* Fcn: '<S28>/vdr' */
    VFControl_B.vdr_l = -0.57735026918962573 * VFControl_B.u_Vb[1];

    /* Fcn: '<S28>/vds' */
    VFControl_B.vds_f = ((VFControl_B.MultiportSwitch[0] - 1.7320508075688772 *
                          VFControl_B.MultiportSwitch[1]) * VFControl_B.u_Vb[3]
                         + 2.0 * VFControl_B.MultiportSwitch[0] *
                         VFControl_B.u_Vb[2]) * 0.33333333333333331;

    /* Fcn: '<S28>/vqr' */
    VFControl_B.vqr_j = (2.0 * VFControl_B.u_Vb[0] + VFControl_B.u_Vb[1]) *
      0.33333333333333331;

    /* Fcn: '<S28>/vqs' */
    VFControl_B.vqs_l = ((1.7320508075688772 * VFControl_B.MultiportSwitch[0] +
                          VFControl_B.MultiportSwitch[1]) * VFControl_B.u_Vb[3]
                         + 2.0 * VFControl_B.MultiportSwitch[1] *
                         VFControl_B.u_Vb[2]) * 0.33333333333333331;
    srUpdateBC(VFControl_DW.Rotorreferenceframe_SubsysRan_i);
  } else if (VFControl_DW.Rotorreferenceframe_MODE_m) {
    /* Disable for Fcn: '<S28>/vqr' incorporates:
     *  Outport: '<S28>/vqr,vdr'
     */
    VFControl_B.vqr_j = VFControl_cal->vqrvdr_Y0;

    /* Disable for Fcn: '<S28>/vdr' incorporates:
     *  Outport: '<S28>/vqr,vdr'
     */
    VFControl_B.vdr_l = VFControl_cal->vqrvdr_Y0;

    /* Disable for Fcn: '<S28>/vqs' incorporates:
     *  Outport: '<S28>/vqs,vds'
     */
    VFControl_B.vqs_l = VFControl_cal->vqsvds_Y0;

    /* Disable for Fcn: '<S28>/vds' incorporates:
     *  Outport: '<S28>/vqs,vds'
     */
    VFControl_B.vds_f = VFControl_cal->vqsvds_Y0;
    VFControl_DW.Rotorreferenceframe_MODE_m = false;
  }

  /* End of Constant: '<S17>/Constant' */
  /* End of Outputs for SubSystem: '<S17>/Rotor reference frame' */

  /* Outputs for Enabled SubSystem: '<S17>/Stationary reference frame' incorporates:
   *  EnablePort: '<S29>/Enable'
   */
  /* Constant: '<S17>/Constant1' */
  if (VFControl_cal->Constant1_Value_f) {
    VFControl_DW.Stationaryreferenceframe_MODE_p = true;

    /* Fcn: '<S29>/vdr' */
    VFControl_B.vdr_p = ((-VFControl_B.MultiportSwitch[0] - 1.7320508075688772 *
                          VFControl_B.MultiportSwitch[1]) * VFControl_B.u_Vb[1]
                         + -2.0 * VFControl_B.MultiportSwitch[0] *
                         VFControl_B.u_Vb[0]) * 0.33333333333333331;

    /* Fcn: '<S29>/vds' */
    VFControl_B.vds_i = -0.57735026918962573 * VFControl_B.u_Vb[3];

    /* Fcn: '<S29>/vqr' */
    VFControl_B.vqr_l = ((VFControl_B.MultiportSwitch[1] - 1.7320508075688772 *
                          VFControl_B.MultiportSwitch[0]) * VFControl_B.u_Vb[1]
                         + 2.0 * VFControl_B.MultiportSwitch[1] *
                         VFControl_B.u_Vb[0]) * 0.33333333333333331;

    /* Fcn: '<S29>/vqs' */
    VFControl_B.vqs_i = (2.0 * VFControl_B.u_Vb[2] + VFControl_B.u_Vb[3]) *
      0.33333333333333331;
    srUpdateBC(VFControl_DW.Stationaryreferenceframe_Subs_n);
  } else if (VFControl_DW.Stationaryreferenceframe_MODE_p) {
    /* Disable for Fcn: '<S29>/vqr' incorporates:
     *  Outport: '<S29>/vqr,vdr'
     */
    VFControl_B.vqr_l = VFControl_cal->vqrvdr_Y0_a;

    /* Disable for Fcn: '<S29>/vdr' incorporates:
     *  Outport: '<S29>/vqr,vdr'
     */
    VFControl_B.vdr_p = VFControl_cal->vqrvdr_Y0_a;

    /* Disable for Fcn: '<S29>/vqs' incorporates:
     *  Outport: '<S29>/vqs,vds'
     */
    VFControl_B.vqs_i = VFControl_cal->vqsvds_Y0_m;

    /* Disable for Fcn: '<S29>/vds' incorporates:
     *  Outport: '<S29>/vqs,vds'
     */
    VFControl_B.vds_i = VFControl_cal->vqsvds_Y0_m;
    VFControl_DW.Stationaryreferenceframe_MODE_p = false;
  }

  /* End of Constant: '<S17>/Constant1' */
  /* End of Outputs for SubSystem: '<S17>/Stationary reference frame' */

  /* Outputs for Enabled SubSystem: '<S17>/Synchronous reference frame' incorporates:
   *  EnablePort: '<S30>/Enable'
   */
  /* Constant: '<S17>/Constant2' */
  if (VFControl_cal->Constant2_Value_kn) {
    VFControl_DW.Synchronousreferenceframe_MOD_l = true;

    /* Fcn: '<S30>/vdr' */
    VFControl_B.vdr = ((VFControl_B.MultiportSwitch[0] - 1.7320508075688772 *
                        VFControl_B.MultiportSwitch[1]) * VFControl_B.u_Vb[1] +
                       2.0 * VFControl_B.MultiportSwitch[0] * VFControl_B.u_Vb[0])
      / 3.0;

    /* Fcn: '<S30>/vds' */
    VFControl_B.vds = ((VFControl_B.MultiportSwitch[2] - 1.7320508075688772 *
                        VFControl_B.MultiportSwitch[3]) * VFControl_B.u_Vb[3] +
                       2.0 * VFControl_B.MultiportSwitch[2] * VFControl_B.u_Vb[2])
      / 3.0;

    /* Fcn: '<S30>/vqr' */
    VFControl_B.vqr = ((1.7320508075688772 * VFControl_B.MultiportSwitch[0] +
                        VFControl_B.MultiportSwitch[1]) * VFControl_B.u_Vb[1] +
                       2.0 * VFControl_B.MultiportSwitch[1] * VFControl_B.u_Vb[0])
      / 3.0;

    /* Fcn: '<S30>/vqs' */
    VFControl_B.vqs = ((1.7320508075688772 * VFControl_B.MultiportSwitch[2] +
                        VFControl_B.MultiportSwitch[3]) * VFControl_B.u_Vb[3] +
                       2.0 * VFControl_B.MultiportSwitch[3] * VFControl_B.u_Vb[2])
      / 3.0;
    srUpdateBC(VFControl_DW.Synchronousreferenceframe_Sub_k);
  } else if (VFControl_DW.Synchronousreferenceframe_MOD_l) {
    /* Disable for Fcn: '<S30>/vqr' incorporates:
     *  Outport: '<S30>/vqr,vdr'
     */
    VFControl_B.vqr = VFControl_cal->vqrvdr_Y0_am;

    /* Disable for Fcn: '<S30>/vdr' incorporates:
     *  Outport: '<S30>/vqr,vdr'
     */
    VFControl_B.vdr = VFControl_cal->vqrvdr_Y0_am;

    /* Disable for Fcn: '<S30>/vqs' incorporates:
     *  Outport: '<S30>/vqs,vds'
     */
    VFControl_B.vqs = VFControl_cal->vqsvds_Y0_p;

    /* Disable for Fcn: '<S30>/vds' incorporates:
     *  Outport: '<S30>/vqs,vds'
     */
    VFControl_B.vds = VFControl_cal->vqsvds_Y0_p;
    VFControl_DW.Synchronousreferenceframe_MOD_l = false;
  }

  /* End of Constant: '<S17>/Constant2' */
  /* End of Outputs for SubSystem: '<S17>/Synchronous reference frame' */

  /* MultiPortSwitch: '<S17>/Multiport Switch1' incorporates:
   *  Constant: '<S17>/Constant4'
   */
  switch (static_cast<int32_T>(VFControl_cal->Constant4_Value_i)) {
   case 1:
    /* MultiPortSwitch: '<S17>/Multiport Switch1' */
    VFControl_B.MultiportSwitch1_f[0] = VFControl_B.vqs_l;
    VFControl_B.MultiportSwitch1_f[1] = VFControl_B.vds_f;
    break;

   case 2:
    /* MultiPortSwitch: '<S17>/Multiport Switch1' */
    VFControl_B.MultiportSwitch1_f[0] = VFControl_B.vqs_i;
    VFControl_B.MultiportSwitch1_f[1] = VFControl_B.vds_i;
    break;

   default:
    /* MultiPortSwitch: '<S17>/Multiport Switch1' */
    VFControl_B.MultiportSwitch1_f[0] = VFControl_B.vqs;
    VFControl_B.MultiportSwitch1_f[1] = VFControl_B.vds;
    break;
  }

  /* End of MultiPortSwitch: '<S17>/Multiport Switch1' */

  /* MultiPortSwitch: '<S17>/Multiport Switch' incorporates:
   *  Constant: '<S17>/Constant3'
   */
  switch (static_cast<int32_T>(VFControl_cal->Constant3_Value_g)) {
   case 1:
    /* MultiPortSwitch: '<S17>/Multiport Switch' */
    VFControl_B.MultiportSwitch_p2[0] = VFControl_B.vqr_j;
    VFControl_B.MultiportSwitch_p2[1] = VFControl_B.vdr_l;
    break;

   case 2:
    /* MultiPortSwitch: '<S17>/Multiport Switch' */
    VFControl_B.MultiportSwitch_p2[0] = VFControl_B.vqr_l;
    VFControl_B.MultiportSwitch_p2[1] = VFControl_B.vdr_p;
    break;

   default:
    /* MultiPortSwitch: '<S17>/Multiport Switch' */
    VFControl_B.MultiportSwitch_p2[0] = VFControl_B.vqr;
    VFControl_B.MultiportSwitch_p2[1] = VFControl_B.vdr;
    break;
  }

  /* End of MultiPortSwitch: '<S17>/Multiport Switch' */

  /* UnitDelay: '<S23>/voltages' */
  VFControl_B.voltages[0] = VFControl_DW.voltages_DSTATE[0];
  VFControl_B.voltages[1] = VFControl_DW.voltages_DSTATE[1];
  VFControl_B.voltages[2] = VFControl_DW.voltages_DSTATE[2];
  VFControl_B.voltages[3] = VFControl_DW.voltages_DSTATE[3];

  /* DigitalClock: '<S23>/Digital Clock' */
  VFControl_B.DigitalClock = VFControl_M->Timing.t[1];

  /* Switch: '<S23>/IC' incorporates:
   *  Constant: '<S16>/Constant3'
   *  Switch: '<S16>/Switch1'
   */
  if (VFControl_B.DigitalClock >= VFControl_cal->IC_Threshold) {
    /* Sum: '<S23>/sum' */
    VFControl_B.ukuk1[0] = VFControl_B.MultiportSwitch1_f[0] +
      VFControl_B.voltages[0];
    VFControl_B.ukuk1[2] = VFControl_B.MultiportSwitch_p2[0] +
      VFControl_B.voltages[2];
    VFControl_B.ukuk1[1] = VFControl_B.MultiportSwitch1_f[1] +
      VFControl_B.voltages[1];
    VFControl_B.ukuk1[3] = VFControl_B.MultiportSwitch_p2[1] +
      VFControl_B.voltages[3];
    if (VFControl_cal->Constant3_Value >= VFControl_cal->Switch1_Threshold) {
      /* Switch: '<S16>/Switch1' incorporates:
       *  Product: '<S22>/Product1'
       */
      std::memcpy(&VFControl_B.RLinv[0], &VFControl_B.RLinv_m[0], sizeof(real_T)
                  << 4U);
    } else {
      /* Switch: '<S16>/Switch1' incorporates:
       *  Constant: '<S16>/Constant4'
       */
      std::memcpy(&VFControl_B.RLinv[0], &VFControl_cal->Constant4_Value[0],
                  sizeof(real_T) << 4U);
    }

    /* MultiPortSwitch: '<S19>/Multiport Switch1' incorporates:
     *  Constant: '<S16>/Constant3'
     *  Constant: '<S19>/Constant4'
     *  Switch: '<S16>/Switch1'
     */
    switch (static_cast<int32_T>(VFControl_cal->Constant4_Value_e)) {
     case 1:
      /* MultiPortSwitch: '<S19>/Multiport Switch1' incorporates:
       *  Assignment: '<S36>/W(2,1)=-wr'
       */
      std::memcpy(&VFControl_B.MultiportSwitch1_l[0], &VFControl_B.W21wr[0],
                  sizeof(real_T) << 4U);
      break;

     case 2:
      /* MultiPortSwitch: '<S19>/Multiport Switch1' incorporates:
       *  Assignment: '<S37>/W(4,3)=wr'
       */
      std::memcpy(&VFControl_B.MultiportSwitch1_l[0], &VFControl_B.W43wr[0],
                  sizeof(real_T) << 4U);
      break;

     default:
      /* MultiPortSwitch: '<S19>/Multiport Switch1' incorporates:
       *  Assignment: '<S35>/W(4,3)=wr-1'
       */
      std::memcpy(&VFControl_B.MultiportSwitch1_l[0], &VFControl_B.W43wr1[0],
                  sizeof(real_T) << 4U);
      break;
    }

    /* End of MultiPortSwitch: '<S19>/Multiport Switch1' */
    for (i = 0; i < 16; i++) {
      /* Sum: '<S16>/Sum1' incorporates:
       *  MultiPortSwitch: '<S19>/Multiport Switch1'
       *  Switch: '<S16>/Switch1'
       */
      xk1 = (0.0 - VFControl_B.MultiportSwitch1_l[i]) - VFControl_B.RLinv[i];
      VFControl_B.A[i] = xk1;

      /* Gain: '<S27>/wbase*Ts//2' incorporates:
       *  Sum: '<S16>/Sum1'
       */
      xk1 *= VFControl_cal->wbaseTs2_Gain;
      VFControl_B.wbaseTs2[i] = xk1;

      /* Sum: '<S27>/Sum1' incorporates:
       *  Constant: '<S27>/u5'
       *  Gain: '<S27>/wbase*Ts//2'
       */
      VFControl_B.Sum1_k[i] = VFControl_cal->u5_Value_l[i] - xk1;
    }

    /* Product: '<S27>/inversion' incorporates:
     *  Sum: '<S27>/Sum1'
     */
    rt_invd4x4_snf(VFControl_B.Sum1_k, VFControl_B.inversion);
    for (i = 0; i < 16; i++) {
      /* Gain: '<S27>/wbase*Ts//2 ' incorporates:
       *  Product: '<S27>/inversion'
       */
      xk1 = VFControl_cal->wbaseTs2_Gain_c * VFControl_B.inversion[i];
      VFControl_B.wbaseTs2_c[i] = xk1;

      /* Product: '<S23>/Product1' incorporates:
       *  Gain: '<S27>/wbase*Ts//2 '
       */
      tmp[i] = xk1;
    }

    /* Product: '<S23>/Product1' */
    Bias = VFControl_B.ukuk1[0];
    tmp_1 = VFControl_B.ukuk1[1];
    tmp_2 = VFControl_B.ukuk1[2];
    RLinv_m = VFControl_B.ukuk1[3];
    for (i = 0; i < 4; i++) {
      xk1 = tmp[i] * Bias;
      xk1 += tmp[i + 4] * tmp_1;
      xk1 += tmp[i + 8] * tmp_2;
      xk1 += tmp[i + 12] * RLinv_m;

      /* Product: '<S23>/Product1' */
      VFControl_B.Product1_d[i] = xk1;
    }

    for (i = 0; i < 16; i++) {
      /* Sum: '<S27>/Sum5' incorporates:
       *  Constant: '<S27>/u5'
       *  Gain: '<S27>/wbase*Ts//2'
       */
      xk1 = VFControl_cal->u5_Value_l[i] + VFControl_B.wbaseTs2[i];
      VFControl_B.Sum5[i] = xk1;

      /* Product: '<S27>/Product4' incorporates:
       *  Product: '<S27>/inversion'
       *  Sum: '<S27>/Sum5'
       */
      tmp[i] = VFControl_B.inversion[i];
      tmp_0[i] = xk1;
    }

    for (i = 0; i < 4; i++) {
      /* Product: '<S27>/Product4' */
      tmp_3 = i << 2;
      xk1 = tmp_0[tmp_3];
      Bias = tmp_0[tmp_3 + 1];
      tmp_1 = tmp_0[tmp_3 + 2];
      tmp_2 = tmp_0[tmp_3 + 3];
      for (i_0 = 0; i_0 < 4; i_0++) {
        /* Product: '<S27>/Product4' */
        RLinv_m = xk1 * tmp[i_0];
        RLinv_m += tmp[i_0 + 4] * Bias;
        RLinv_m += tmp[i_0 + 8] * tmp_1;
        RLinv_m += tmp[i_0 + 12] * tmp_2;

        /* Product: '<S27>/Product4' */
        VFControl_B.Product4_k[i_0 + tmp_3] = RLinv_m;
      }
    }

    /* Product: '<S23>/Product2' incorporates:
     *  Product: '<S27>/Product4'
     */
    std::memcpy(&tmp[0], &VFControl_B.Product4_k[0], sizeof(real_T) << 4U);
    Bias = VFControl_B.xk1[0];
    tmp_1 = VFControl_B.xk1[1];
    tmp_2 = VFControl_B.xk1[2];
    RLinv_m = VFControl_B.xk1[3];
    for (i = 0; i < 4; i++) {
      xk1 = tmp[i] * Bias;
      xk1 += tmp[i + 4] * tmp_1;
      xk1 += tmp[i + 8] * tmp_2;
      xk1 += tmp[i + 12] * RLinv_m;

      /* Product: '<S23>/Product2' */
      VFControl_B.Product2_o[i] = xk1;

      /* Sum: '<S23>/Ad*x(k-1) + Bd*( u(k-1) + u(k))' */
      xk1 += VFControl_B.Product1_d[i];
      VFControl_B.xk[i] = xk1;

      /* Switch: '<S23>/IC' */
      VFControl_B.IC[i] = xk1;
    }

    /* End of Product: '<S23>/Product2' */
  } else {
    /* Switch: '<S23>/IC' */
    VFControl_B.IC[0] = VFControl_B.xk1[0];
    VFControl_B.IC[1] = VFControl_B.xk1[1];
    VFControl_B.IC[2] = VFControl_B.xk1[2];
    VFControl_B.IC[3] = VFControl_B.xk1[3];
  }

  /* End of Switch: '<S23>/IC' */

  /* Sum: '<S18>/Sum2' */
  xk1 = 0.0 - VFControl_B.MultiportSwitch_p[0];
  xk1 -= VFControl_B.MultiportSwitch_p[1];

  /* Sum: '<S18>/Sum2' */
  VFControl_B.Sum2_l = xk1;

  /* Gain: '<S13>/unit conversion' */
  VFControl_B.unitconversion[2] = VFControl_cal->unitconversion_Gain[2] *
    VFControl_B.Sum2_l;

  /* Sum: '<S18>/Sum3' */
  xk1 = 0.0 - VFControl_B.MultiportSwitch1[0];

  /* Gain: '<S13>/unit conversion' */
  VFControl_B.unitconversion[0] = VFControl_cal->unitconversion_Gain[0] *
    VFControl_B.MultiportSwitch_p[0];
  VFControl_B.unitconversion[3] = VFControl_B.Product3[2] *
    VFControl_cal->unitconversion_Gain[3];
  VFControl_B.unitconversion[5] = VFControl_B.xk1[2] *
    VFControl_cal->unitconversion_Gain[5];
  VFControl_B.unitconversion[7] = VFControl_B.MultiportSwitch_p2[0] *
    VFControl_cal->unitconversion_Gain[7];
  VFControl_B.unitconversion[9] = VFControl_B.MultiportSwitch1[0] *
    VFControl_cal->unitconversion_Gain[9];
  VFControl_B.unitconversion[12] = VFControl_B.Product3[0] *
    VFControl_cal->unitconversion_Gain[12];
  VFControl_B.unitconversion[14] = VFControl_B.xk1[0] *
    VFControl_cal->unitconversion_Gain[14];
  VFControl_B.unitconversion[16] = VFControl_B.MultiportSwitch1_f[0] *
    VFControl_cal->unitconversion_Gain[16];

  /* Sum: '<S18>/Sum3' */
  xk1 -= VFControl_B.MultiportSwitch1[1];

  /* Gain: '<S13>/unit conversion' */
  VFControl_B.unitconversion[1] = VFControl_cal->unitconversion_Gain[1] *
    VFControl_B.MultiportSwitch_p[1];
  VFControl_B.unitconversion[4] = VFControl_B.Product3[3] *
    VFControl_cal->unitconversion_Gain[4];
  VFControl_B.unitconversion[6] = VFControl_B.xk1[3] *
    VFControl_cal->unitconversion_Gain[6];
  VFControl_B.unitconversion[8] = VFControl_B.MultiportSwitch_p2[1] *
    VFControl_cal->unitconversion_Gain[8];
  VFControl_B.unitconversion[10] = VFControl_B.MultiportSwitch1[1] *
    VFControl_cal->unitconversion_Gain[10];
  VFControl_B.unitconversion[13] = VFControl_B.Product3[1] *
    VFControl_cal->unitconversion_Gain[13];
  VFControl_B.unitconversion[15] = VFControl_B.xk1[1] *
    VFControl_cal->unitconversion_Gain[15];
  VFControl_B.unitconversion[17] = VFControl_B.MultiportSwitch1_f[1] *
    VFControl_cal->unitconversion_Gain[17];

  /* Sum: '<S18>/Sum3' */
  VFControl_B.Sum3 = xk1;

  /* Gain: '<S13>/unit conversion' */
  VFControl_B.unitconversion[11] = VFControl_cal->unitconversion_Gain[11] *
    VFControl_B.Sum3;
  VFControl_B.unitconversion[18] = VFControl_cal->unitconversion_Gain[18] *
    VFControl_B.Switch2;

  /* Gain: '<S15>/1\p' */
  VFControl_B.up = VFControl_cal->up_Gain * VFControl_B.Rotoranglethetam;

  /* Gain: '<S15>/1\p1' */
  VFControl_B.wTethr[0] = VFControl_cal->up1_Gain[0] * VFControl_B.Sum1;
  VFControl_B.wTethr[1] = VFControl_cal->up1_Gain[1] * VFControl_B.Sum2_k;
  VFControl_B.wTethr[2] = VFControl_cal->up1_Gain[2] * VFControl_B.up;

  /* Gain: '<S15>/Unit conversion' */
  VFControl_B.Unitconversion = VFControl_cal->Unitconversion_Gain * 0.0;

  /* Gain: '<S15>/F' */
  VFControl_B.F = VFControl_cal->F_Gain * VFControl_B.Sum1;

  /* Sum: '<S15>/Sum' */
  VFControl_B.Sum = (VFControl_B.Sum2_k - VFControl_B.Unitconversion) -
    VFControl_B.F;

  /* Gain: '<S15>/1_2H' */
  VFControl_B.u_2H = VFControl_cal->u_2H_Gain * VFControl_B.Sum;

  /* DiscreteIntegrator: '<S15>/Rotor speed(wm)' */
  if (VFControl_DW.Rotorspeedwm_SYSTEM_ENABLE != 0) {
    /* DiscreteIntegrator: '<S15>/Rotor speed(wm)' */
    VFControl_B.Rotorspeedwm = VFControl_DW.Rotorspeedwm_DSTATE;
  } else {
    /* DiscreteIntegrator: '<S15>/Rotor speed(wm)' */
    VFControl_B.Rotorspeedwm = VFControl_cal->Rotorspeedwm_gainval *
      VFControl_B.u_2H + VFControl_DW.Rotorspeedwm_DSTATE;
  }

  /* End of DiscreteIntegrator: '<S15>/Rotor speed(wm)' */

  /* Gain: '<S15>/web_psb' */
  VFControl_B.web_psb = VFControl_cal->web_psb_Gain_n * VFControl_B.Sum1;

  /* DigitalClock: '<S57>/Digital Clock' */
  VFControl_B.DigitalClock_k = VFControl_M->Timing.t[1];

  /* Lookup: '<S57>/Look-Up Table' */
  VFControl_B.LookUpTable = rt_Lookup(&VFControl_cal->LookUpTable_XData[0], 8,
    VFControl_B.DigitalClock_k, &VFControl_cal->LookUpTable_YData[0]);

  /* RelationalOperator: '<S49>/Relational Operator3' incorporates:
   *  Constant: '<S49>/Constant6'
   *  Constant: '<S49>/valp_nom1'
   */
  VFControl_B.RelationalOperator3 = (VFControl_cal->valp_nom1_Value ==
    VFControl_cal->Constant6_Value_k);

  /* RelationalOperator: '<S46>/Relational Operator' incorporates:
   *  Constant: '<S46>/Constant'
   *  Constant: '<S46>/valp_nom3'
   */
  VFControl_B.Amplitude = (VFControl_cal->valp_nom3_Value ==
    VFControl_cal->Constant_Value_nw);

  /* Logic: '<S49>/Logical Operator1' */
  VFControl_B.LogicalOperator1 = (VFControl_B.RelationalOperator3 &&
    VFControl_B.Amplitude);

  /* Step: '<S56>/Step1' */
  Bias = VFControl_M->Timing.t[1];
  if (Bias < VFControl_cal->Variationvaluesteprampmod_Toff_) {
    /* Step: '<S56>/Step1' */
    VFControl_B.Toff = VFControl_cal->Step1_Y0;
  } else {
    /* Step: '<S56>/Step1' */
    VFControl_B.Toff = VFControl_cal->Step1_YFinal;
  }

  /* End of Step: '<S56>/Step1' */

  /* DataTypeConversion: '<S56>/Data Type  Conversion2' */
  VFControl_B.DataTypeConversion2 = (VFControl_B.Toff != 0.0);

  /* Logic: '<S56>/Logical Operator1' */
  VFControl_B.LogicalOperator1_l = !VFControl_B.DataTypeConversion2;

  /* RelationalOperator: '<S56>/Relational Operator1' incorporates:
   *  Constant: '<S47>/valp_nom5'
   *  Constant: '<S56>/Constant3'
   */
  VFControl_B.RelationalOperator1 = (VFControl_cal->valp_nom5_Value ==
    VFControl_cal->Constant3_Value_ey);

  /* DataTypeConversion: '<S56>/Data Type  Conversion1' */
  VFControl_B.DataTypeConversion1_f = VFControl_B.RelationalOperator1;

  /* Logic: '<S56>/Logical Operator' */
  VFControl_B.LogicalOperator = (VFControl_B.LogicalOperator1_l &&
    VFControl_B.DataTypeConversion1_f);

  /* Step: '<S56>/Step' */
  Bias = VFControl_M->Timing.t[1];
  if (Bias < VFControl_cal->Variationvaluesteprampmod_Ton_V) {
    /* Step: '<S56>/Step' */
    VFControl_B.Ton = VFControl_cal->Step_Y0;
  } else {
    /* Step: '<S56>/Step' */
    VFControl_B.Ton = VFControl_cal->Step_YFinal;
  }

  /* End of Step: '<S56>/Step' */

  /* DiscreteIntegrator: '<S47>/Discrete-Time Integrator1' */
  VFControl_B.DiscreteTimeIntegrator1 =
    VFControl_DW.DiscreteTimeIntegrator1_DSTATE;

  /* UnitDelay: '<S47>/Unit Delay1' */
  VFControl_B.UnitDelay1 = VFControl_DW.UnitDelay1_DSTATE;

  /* Switch: '<S56>/Switch2' */
  if (VFControl_B.Toff >= VFControl_cal->Switch2_Threshold_a) {
    /* MultiPortSwitch: '<S56>/Multiport Switch1' incorporates:
     *  Constant: '<S47>/valp_nom5'
     */
    switch (static_cast<int32_T>(VFControl_cal->valp_nom5_Value)) {
     case 1:
      /* Product: '<S56>/Product2' incorporates:
       *  Constant: '<S56>/valp_nom6'
       */
      VFControl_B.Step = VFControl_cal->Variationvaluesteprampmod_Var_n *
        VFControl_B.Ton;

      /* MultiPortSwitch: '<S56>/Multiport Switch1' */
      VFControl_B.MultiportSwitch1_b = VFControl_B.Step;
      break;

     case 2:
      /* MultiPortSwitch: '<S56>/Multiport Switch1' */
      VFControl_B.MultiportSwitch1_b = VFControl_B.DiscreteTimeIntegrator1;
      break;

     case 3:
      /* Gain: '<S56>/Gain1' incorporates:
       *  Constant: '<S56>/valp_nom9'
       */
      VFControl_B.Gain1_f = VFControl_cal->Gain1_Gain_n *
        VFControl_cal->Variationvaluesteprampmod_Varia;

      /* Product: '<S56>/Product1' */
      VFControl_B.Product1_f = VFControl_B.DiscreteTimeIntegrator1 *
        VFControl_B.Gain1_f;

      /* Trigonometry: '<S56>/Trigonometric Function1' */
      VFControl_B.TrigonometricFunction1_l = std::sin(VFControl_B.Product1_f);

      /* Product: '<S56>/Product' incorporates:
       *  Constant: '<S56>/valp_nom8'
       */
      VFControl_B.Mod = VFControl_cal->Variationvaluesteprampmod_Var_l *
        VFControl_B.TrigonometricFunction1_l;

      /* MultiPortSwitch: '<S56>/Multiport Switch1' */
      VFControl_B.MultiportSwitch1_b = VFControl_B.Mod;
      break;

     default:
      /* MultiPortSwitch: '<S56>/Multiport Switch1' incorporates:
       *  Constant: '<S56>/Constant5'
       */
      VFControl_B.MultiportSwitch1_b = VFControl_cal->Constant5_Value_c;
      break;
    }

    /* End of MultiPortSwitch: '<S56>/Multiport Switch1' */

    /* Switch: '<S56>/Switch2' */
    VFControl_B.Switch2_d = VFControl_B.MultiportSwitch1_b;
  } else {
    /* Switch: '<S56>/Switch2' */
    VFControl_B.Switch2_d = VFControl_B.UnitDelay1;
  }

  /* End of Switch: '<S56>/Switch2' */

  /* Switch: '<S56>/Switch3' */
  if (VFControl_B.LogicalOperator) {
    /* Switch: '<S56>/Switch3' incorporates:
     *  Constant: '<S56>/Constant1'
     */
    VFControl_B.Switch3 = VFControl_cal->Constant1_Value_a;
  } else {
    /* Switch: '<S56>/Switch3' */
    VFControl_B.Switch3 = VFControl_B.Switch2_d;
  }

  /* End of Switch: '<S56>/Switch3' */

  /* Switch: '<S41>/Switch2' */
  if (VFControl_B.Amplitude) {
    /* Switch: '<S41>/Switch2' */
    VFControl_B.Switch2_a = VFControl_B.Switch3;
  } else {
    /* Switch: '<S41>/Switch2' incorporates:
     *  Constant: '<S41>/Constant1'
     */
    VFControl_B.Switch2_a = VFControl_cal->Constant1_Value_j;
  }

  /* End of Switch: '<S41>/Switch2' */

  /* Sum: '<S41>/Sum3' incorporates:
   *  Constant: '<S41>/valp_nom2'
   */
  VFControl_B.Sum3_e = VFControl_B.Switch2_a + VFControl_cal->valp_nom2_Value;

  /* Switch: '<S49>/Switch1' */
  if (VFControl_B.LogicalOperator1) {
    /* Switch: '<S49>/Switch1' */
    VFControl_B.Switch1 = VFControl_B.LookUpTable;
  } else {
    /* Switch: '<S49>/Switch1' */
    VFControl_B.Switch1 = VFControl_B.Sum3_e;
  }

  /* End of Switch: '<S49>/Switch1' */

  /* Switch: '<S50>/Switch5' */
  VFControl_B.Switch5[0] = VFControl_B.Switch1;

  /* Switch: '<S50>/Switch5' incorporates:
   *  Constant: '<S50>/SinglePhase'
   */
  if (VFControl_cal->SinglePhase_Value >= VFControl_cal->Switch5_Threshold) {
    /* Switch: '<S50>/Switch5' */
    VFControl_B.Switch5[1] = VFControl_B.Sum3_e;
    VFControl_B.Switch5[2] = VFControl_B.Sum3_e;
  } else {
    /* Switch: '<S50>/Switch5' */
    VFControl_B.Switch5[1] = VFControl_B.Switch1;
    VFControl_B.Switch5[2] = VFControl_B.Switch1;
  }

  /* DigitalClock: '<S42>/t' */
  VFControl_B.t = VFControl_M->Timing.t[1];

  /* Gain: '<S42>/Gain' incorporates:
   *  Constant: '<S42>/valp_nom1'
   */
  VFControl_B.Gain_p = VFControl_cal->Gain_Gain_k *
    VFControl_cal->valp_nom1_Value_g;

  /* Product: '<S42>/Product' */
  VFControl_B.Product = VFControl_B.t * VFControl_B.Gain_p;

  /* DiscreteIntegrator: '<S42>/Discrete-Time Integrator' */
  VFControl_B.DiscreteTimeIntegrator =
    VFControl_DW.DiscreteTimeIntegrator_DSTATE;

  /* Sum: '<S42>/Sum' */
  VFControl_B.Sum_n = VFControl_B.Product + VFControl_B.DiscreteTimeIntegrator;

  /* RelationalOperator: '<S54>/Relational Operator3' incorporates:
   *  Constant: '<S54>/Constant6'
   *  Constant: '<S54>/valp_nom1'
   */
  VFControl_B.RelationalOperator3_f = (VFControl_cal->valp_nom1_Value_c ==
    VFControl_cal->Constant6_Value_h);

  /* RelationalOperator: '<S46>/Relational Operator1' incorporates:
   *  Constant: '<S46>/Constant2'
   *  Constant: '<S46>/valp_nom3'
   */
  VFControl_B.Phase = (VFControl_cal->valp_nom3_Value ==
                       VFControl_cal->Constant2_Value_b);

  /* Logic: '<S54>/Logical Operator1' */
  VFControl_B.LogicalOperator1_b = (VFControl_B.RelationalOperator3_f &&
    VFControl_B.Phase);

  /* Gain: '<S45>/Gain3' incorporates:
   *  Constant: '<S45>/valp_nom'
   */
  VFControl_B.Gain3 = VFControl_cal->Gain3_Gain_e *
    VFControl_cal->valp_nom_Value;

  /* Switch: '<S45>/Switch3' */
  if (VFControl_B.Phase) {
    /* Gain: '<S45>/Gain4' */
    VFControl_B.Gain4 = VFControl_cal->Gain4_Gain * VFControl_B.Switch3;

    /* Switch: '<S45>/Switch3' */
    VFControl_B.Switch3_o = VFControl_B.Gain4;
  } else {
    /* Switch: '<S45>/Switch3' incorporates:
     *  Constant: '<S45>/Constant4'
     */
    VFControl_B.Switch3_o = VFControl_cal->Constant4_Value_l;
  }

  /* End of Switch: '<S45>/Switch3' */

  /* Sum: '<S45>/Sum2' incorporates:
   *  Constant: '<S45>/P1'
   */
  VFControl_B.Sum2_j[0] = (VFControl_B.Gain3 + VFControl_cal->P1_Value_e[0]) +
    VFControl_B.Switch3_o;
  VFControl_B.Sum2_j[1] = (VFControl_B.Gain3 + VFControl_cal->P1_Value_e[1]) +
    VFControl_B.Switch3_o;
  VFControl_B.Sum2_j[2] = (VFControl_B.Gain3 + VFControl_cal->P1_Value_e[2]) +
    VFControl_B.Switch3_o;

  /* Switch: '<S54>/Switch1' */
  if (VFControl_B.LogicalOperator1_b) {
    /* Gain: '<S54>/Gain3' */
    VFControl_B.Gain3_n = VFControl_cal->Gain3_Gain_c * VFControl_B.LookUpTable;

    /* Sum: '<S54>/Sum2' incorporates:
     *  Constant: '<S54>/P1'
     */
    xk1 = VFControl_B.Gain3_n + VFControl_cal->P1_Value[0];
    VFControl_B.Sum2_lw[0] = xk1;

    /* Switch: '<S54>/Switch1' */
    VFControl_B.Switch1_a[0] = xk1;

    /* Sum: '<S54>/Sum2' incorporates:
     *  Constant: '<S54>/P1'
     */
    xk1 = VFControl_B.Gain3_n + VFControl_cal->P1_Value[1];
    VFControl_B.Sum2_lw[1] = xk1;

    /* Switch: '<S54>/Switch1' */
    VFControl_B.Switch1_a[1] = xk1;

    /* Sum: '<S54>/Sum2' incorporates:
     *  Constant: '<S54>/P1'
     */
    xk1 = VFControl_B.Gain3_n + VFControl_cal->P1_Value[2];
    VFControl_B.Sum2_lw[2] = xk1;

    /* Switch: '<S54>/Switch1' */
    VFControl_B.Switch1_a[2] = xk1;
  } else {
    /* Switch: '<S54>/Switch1' */
    VFControl_B.Switch1_a[0] = VFControl_B.Sum2_j[0];
    VFControl_B.Switch1_a[1] = VFControl_B.Sum2_j[1];
    VFControl_B.Switch1_a[2] = VFControl_B.Sum2_j[2];
  }

  /* End of Switch: '<S54>/Switch1' */

  /* Switch: '<S55>/Switch5' incorporates:
   *  Constant: '<S55>/SinglePhase'
   */
  if (VFControl_cal->SinglePhase_Value_i >= VFControl_cal->Switch5_Threshold_m)
  {
    /* Switch: '<S55>/Switch5' */
    VFControl_B.Switch5_b[0] = VFControl_B.Switch1_a[0];
    VFControl_B.Switch5_b[1] = VFControl_B.Sum2_j[1];
    VFControl_B.Switch5_b[2] = VFControl_B.Sum2_j[2];
  } else {
    /* Switch: '<S55>/Switch5' */
    VFControl_B.Switch5_b[0] = VFControl_B.Switch1_a[0];
    VFControl_B.Switch5_b[1] = VFControl_B.Switch1_a[1];
    VFControl_B.Switch5_b[2] = VFControl_B.Switch1_a[2];
  }

  /* End of Switch: '<S55>/Switch5' */

  /* Sum: '<S40>/Sum7' */
  VFControl_B.Sum7[0] = VFControl_B.Sum_n + VFControl_B.Switch5_b[0];

  /* Trigonometry: '<S40>/Trigonometric Function1' */
  xk1 = std::sin(VFControl_B.Sum7[0]);
  VFControl_B.TrigonometricFunction1[0] = xk1;

  /* Product: '<S40>/Product2' */
  VFControl_B.Product2[0] = VFControl_B.Switch5[0] * xk1;

  /* Sum: '<S40>/Sum7' */
  VFControl_B.Sum7[1] = VFControl_B.Sum_n + VFControl_B.Switch5_b[1];

  /* Trigonometry: '<S40>/Trigonometric Function1' */
  xk1 = std::sin(VFControl_B.Sum7[1]);
  VFControl_B.TrigonometricFunction1[1] = xk1;

  /* Product: '<S40>/Product2' */
  VFControl_B.Product2[1] = VFControl_B.Switch5[1] * xk1;

  /* Sum: '<S40>/Sum7' */
  VFControl_B.Sum7[2] = VFControl_B.Sum_n + VFControl_B.Switch5_b[2];

  /* Trigonometry: '<S40>/Trigonometric Function1' */
  xk1 = std::sin(VFControl_B.Sum7[2]);
  VFControl_B.TrigonometricFunction1[2] = xk1;

  /* Product: '<S40>/Product2' */
  VFControl_B.Product2[2] = VFControl_B.Switch5[2] * xk1;

  /* Step: '<S51>/Step' */
  Bias = VFControl_M->Timing.t[1];
  if (Bias < VFControl_cal->Step_Time) {
    /* Step: '<S51>/Step' */
    VFControl_B.Ton_e = VFControl_cal->Step_Y0_k;
  } else {
    /* Step: '<S51>/Step' */
    VFControl_B.Ton_e = VFControl_cal->Step_YFinal_d;
  }

  /* End of Step: '<S51>/Step' */

  /* Step: '<S51>/Step1' */
  Bias = VFControl_M->Timing.t[1];
  if (Bias < VFControl_cal->Step1_Time) {
    /* Step: '<S51>/Step1' */
    VFControl_B.Toff_e = VFControl_cal->Step1_Y0_i;
  } else {
    /* Step: '<S51>/Step1' */
    VFControl_B.Toff_e = VFControl_cal->Step1_YFinal_k;
  }

  /* End of Step: '<S51>/Step1' */

  /* Sum: '<S51>/Sum4' */
  VFControl_B.Sum4 = VFControl_B.Ton_e + VFControl_B.Toff_e;

  /* DataTypeConversion: '<S51>/Data Type  Conversion1' */
  VFControl_B.DataTypeConversion1_j = (VFControl_B.Sum4 != 0.0);

  /* DataTypeConversion: '<S51>/Data Type  Conversion2' incorporates:
   *  Constant: '<S51>/valp_nom7'
   */
  VFControl_B.DataTypeConversion2_m = (VFControl_cal->valp_nom7_Value != 0.0);

  /* Logic: '<S51>/Logical Operator' */
  VFControl_B.LogicalOperator_l = (VFControl_B.DataTypeConversion1_j &&
    VFControl_B.DataTypeConversion2_m);

  /* DataTypeConversion: '<S51>/Data Type  Conversion' */
  VFControl_B.DataTypeConversion = VFControl_B.LogicalOperator_l;

  /* Gain: '<S52>/Gain1' */
  VFControl_B.Gain1 = VFControl_cal->HarmonicAgeneration_n_Harmo *
    VFControl_B.Sum_n;

  /* Gain: '<S52>/Gain3' incorporates:
   *  Constant: '<S52>/Phase_Harmo'
   */
  VFControl_B.Gain3_f = VFControl_cal->Gain3_Gain_l *
    VFControl_cal->HarmonicAgeneration_Phase_Harmo;

  /* Sum: '<S52>/Sum1' incorporates:
   *  Constant: '<S52>/Phase_Harmo2'
   *  Constant: '<S52>/valp_nom2'
   */
  VFControl_B.Sum1_m = VFControl_cal->HarmonicAgeneration_Seq_Harmo +
    VFControl_cal->valp_nom2_Value_i;

  /* MultiPortSwitch: '<S52>/Multiport Switch' */
  switch (static_cast<int32_T>(VFControl_B.Sum1_m)) {
   case 1:
    /* MultiPortSwitch: '<S52>/Multiport Switch' incorporates:
     *  Constant: '<S52>/Zero-sequence'
     */
    VFControl_B.MultiportSwitch_h[0] = VFControl_cal->Zerosequence_Value[0];
    VFControl_B.MultiportSwitch_h[1] = VFControl_cal->Zerosequence_Value[1];
    VFControl_B.MultiportSwitch_h[2] = VFControl_cal->Zerosequence_Value[2];
    break;

   case 2:
    /* MultiPortSwitch: '<S52>/Multiport Switch' incorporates:
     *  Constant: '<S52>/Positive-sequence'
     */
    VFControl_B.MultiportSwitch_h[0] = VFControl_cal->Positivesequence_Value[0];
    VFControl_B.MultiportSwitch_h[1] = VFControl_cal->Positivesequence_Value[1];
    VFControl_B.MultiportSwitch_h[2] = VFControl_cal->Positivesequence_Value[2];
    break;

   default:
    /* MultiPortSwitch: '<S52>/Multiport Switch' incorporates:
     *  Constant: '<S52>/Negative-sequence'
     */
    VFControl_B.MultiportSwitch_h[0] = VFControl_cal->Negativesequence_Value[0];
    VFControl_B.MultiportSwitch_h[1] = VFControl_cal->Negativesequence_Value[1];
    VFControl_B.MultiportSwitch_h[2] = VFControl_cal->Negativesequence_Value[2];
    break;
  }

  /* End of MultiPortSwitch: '<S52>/Multiport Switch' */

  /* Sum: '<S52>/Sum' */
  xk1 = VFControl_B.Gain1 + VFControl_B.Gain3_f;

  /* Sum: '<S52>/Sum' */
  VFControl_B.Sum_j[0] = xk1 + VFControl_B.MultiportSwitch_h[0];

  /* Trigonometry: '<S52>/Trigonometric Function1' */
  Bias = std::sin(VFControl_B.Sum_j[0]);
  VFControl_B.TrigonometricFunction1_d[0] = Bias;

  /* Product: '<S52>/Product1' incorporates:
   *  Constant: '<S52>/Phase_Harmo1'
   */
  tmp_1 = VFControl_B.DataTypeConversion *
    VFControl_cal->HarmonicAgeneration_Mag_Harmo;

  /* Product: '<S52>/Product1' */
  VFControl_B.Product1[0] = tmp_1 * Bias;

  /* Sum: '<S52>/Sum' */
  VFControl_B.Sum_j[1] = xk1 + VFControl_B.MultiportSwitch_h[1];

  /* Trigonometry: '<S52>/Trigonometric Function1' */
  Bias = std::sin(VFControl_B.Sum_j[1]);
  VFControl_B.TrigonometricFunction1_d[1] = Bias;

  /* Product: '<S52>/Product1' */
  VFControl_B.Product1[1] = tmp_1 * Bias;

  /* Sum: '<S52>/Sum' */
  VFControl_B.Sum_j[2] = xk1 + VFControl_B.MultiportSwitch_h[2];

  /* Trigonometry: '<S52>/Trigonometric Function1' */
  Bias = std::sin(VFControl_B.Sum_j[2]);
  VFControl_B.TrigonometricFunction1_d[2] = Bias;

  /* Product: '<S52>/Product1' */
  VFControl_B.Product1[2] = tmp_1 * Bias;

  /* Gain: '<S53>/Gain1' */
  VFControl_B.Gain1_m = VFControl_cal->HarmonicBgeneration_n_Harmo *
    VFControl_B.Sum_n;

  /* Gain: '<S53>/Gain3' incorporates:
   *  Constant: '<S53>/Phase_Harmo'
   */
  VFControl_B.Gain3_a = VFControl_cal->Gain3_Gain_j *
    VFControl_cal->HarmonicBgeneration_Phase_Harmo;

  /* Sum: '<S53>/Sum1' incorporates:
   *  Constant: '<S53>/Phase_Harmo2'
   *  Constant: '<S53>/valp_nom2'
   */
  VFControl_B.Sum1_m3 = VFControl_cal->HarmonicBgeneration_Seq_Harmo +
    VFControl_cal->valp_nom2_Value_c;

  /* MultiPortSwitch: '<S53>/Multiport Switch' */
  switch (static_cast<int32_T>(VFControl_B.Sum1_m3)) {
   case 1:
    /* MultiPortSwitch: '<S53>/Multiport Switch' incorporates:
     *  Constant: '<S53>/Zero-sequence'
     */
    VFControl_B.MultiportSwitch_n[0] = VFControl_cal->Zerosequence_Value_h[0];
    VFControl_B.MultiportSwitch_n[1] = VFControl_cal->Zerosequence_Value_h[1];
    VFControl_B.MultiportSwitch_n[2] = VFControl_cal->Zerosequence_Value_h[2];
    break;

   case 2:
    /* MultiPortSwitch: '<S53>/Multiport Switch' incorporates:
     *  Constant: '<S53>/Positive-sequence'
     */
    VFControl_B.MultiportSwitch_n[0] = VFControl_cal->Positivesequence_Value_o[0];
    VFControl_B.MultiportSwitch_n[1] = VFControl_cal->Positivesequence_Value_o[1];
    VFControl_B.MultiportSwitch_n[2] = VFControl_cal->Positivesequence_Value_o[2];
    break;

   default:
    /* MultiPortSwitch: '<S53>/Multiport Switch' incorporates:
     *  Constant: '<S53>/Negative-sequence'
     */
    VFControl_B.MultiportSwitch_n[0] = VFControl_cal->Negativesequence_Value_k[0];
    VFControl_B.MultiportSwitch_n[1] = VFControl_cal->Negativesequence_Value_k[1];
    VFControl_B.MultiportSwitch_n[2] = VFControl_cal->Negativesequence_Value_k[2];
    break;
  }

  /* End of MultiPortSwitch: '<S53>/Multiport Switch' */

  /* Sum: '<S53>/Sum' */
  xk1 = VFControl_B.Gain1_m + VFControl_B.Gain3_a;

  /* Sum: '<S53>/Sum' */
  VFControl_B.Sum_i[0] = xk1 + VFControl_B.MultiportSwitch_n[0];

  /* Trigonometry: '<S53>/Trigonometric Function1' */
  tmp_1 = std::sin(VFControl_B.Sum_i[0]);
  VFControl_B.TrigonometricFunction1_c[0] = tmp_1;

  /* Product: '<S53>/Product1' incorporates:
   *  Constant: '<S53>/Phase_Harmo1'
   */
  Bias = VFControl_B.DataTypeConversion *
    VFControl_cal->HarmonicBgeneration_Mag_Harmo;

  /* Product: '<S53>/Product1' */
  tmp_1 *= Bias;
  VFControl_B.Product1_i[0] = tmp_1;

  /* Sum: '<S43>/Sum4' */
  tmp_1 += VFControl_B.Product1[0];
  VFControl_B.Sum4_o[0] = tmp_1;

  /* Sum: '<S40>/Sum8' */
  VFControl_B.Sum8[0] = VFControl_B.Product2[0] + tmp_1;

  /* Sum: '<S53>/Sum' */
  VFControl_B.Sum_i[1] = xk1 + VFControl_B.MultiportSwitch_n[1];

  /* Trigonometry: '<S53>/Trigonometric Function1' */
  tmp_1 = std::sin(VFControl_B.Sum_i[1]);
  VFControl_B.TrigonometricFunction1_c[1] = tmp_1;

  /* Product: '<S53>/Product1' */
  tmp_1 *= Bias;
  VFControl_B.Product1_i[1] = tmp_1;

  /* Sum: '<S43>/Sum4' */
  tmp_1 += VFControl_B.Product1[1];
  VFControl_B.Sum4_o[1] = tmp_1;

  /* Sum: '<S40>/Sum8' */
  VFControl_B.Sum8[1] = VFControl_B.Product2[1] + tmp_1;

  /* Sum: '<S53>/Sum' */
  VFControl_B.Sum_i[2] = xk1 + VFControl_B.MultiportSwitch_n[2];

  /* Trigonometry: '<S53>/Trigonometric Function1' */
  tmp_1 = std::sin(VFControl_B.Sum_i[2]);
  VFControl_B.TrigonometricFunction1_c[2] = tmp_1;

  /* Product: '<S53>/Product1' */
  tmp_1 *= Bias;
  VFControl_B.Product1_i[2] = tmp_1;

  /* Sum: '<S43>/Sum4' */
  tmp_1 += VFControl_B.Product1[2];
  VFControl_B.Sum4_o[2] = tmp_1;

  /* Sum: '<S40>/Sum8' */
  VFControl_B.Sum8[2] = VFControl_B.Product2[2] + tmp_1;

  /* Clock: '<S73>/Clock' */
  VFControl_B.Clock = VFControl_M->Timing.t[0];

  /* Sum: '<S73>/Add1' incorporates:
   *  Constant: '<S73>/Constant3'
   */
  VFControl_B.Add1 = VFControl_B.Clock + VFControl_cal->Constant3_Value_m;

  /* Math: '<S73>/Math Function' incorporates:
   *  Constant: '<S73>/Constant4'
   */
  VFControl_B.MathFunction = rt_remd_snf(VFControl_B.Add1,
    VFControl_cal->Constant4_Value_p);

  /* Gain: '<S73>/1\ib1' */
  VFControl_B.uib1 = VFControl_cal->uib1_Gain * VFControl_B.MathFunction;

  /* Lookup_n-D: '<S73>/1-D Lookup Table' incorporates:
   *  Gain: '<S73>/1\ib1'
   */
  VFControl_B.uDLookupTable = look1_pbinlxpw(VFControl_B.uib1,
    VFControl_cal->uDLookupTable_bp01Data_k,
    VFControl_cal->uDLookupTable_tableData_d, &VFControl_DW.m_bpIndex, 2U);

  /* Sum: '<S73>/Add3' incorporates:
   *  Constant: '<S73>/Constant2'
   */
  VFControl_B.Add3 = VFControl_B.uDLookupTable -
    VFControl_cal->Constant2_Value_a;

  /* ModelReference: '<Root>/Model' incorporates:
   *  Constant: '<Root>/Constant'
   */
  PWMGenerator(&VFControl_B.Sum8[0], &VFControl_B.Add3,
               &VFControl_cal->Constant_Value_c, &VFControl_B.Model_o1,
               &VFControl_B.Model_o2, &VFControl_B.Model_o3,
               &(VFControl_DW.Model_InstanceData.rtb));

  /* DigitalClock: '<S74>/Digital Clock' */
  VFControl_B.DigitalClock_m = VFControl_M->Timing.t[1];

  /* Sum: '<S74>/Add1' incorporates:
   *  Constant: '<S74>/Constant3'
   */
  VFControl_B.Add1_e = VFControl_B.DigitalClock_m +
    VFControl_cal->Constant3_Value_ej;

  /* Math: '<S74>/Math Function' incorporates:
   *  Constant: '<S74>/Constant1'
   */
  VFControl_B.MathFunction_l = rt_remd_snf(VFControl_B.Add1_e,
    VFControl_cal->Constant1_Value_o);

  /* Gain: '<S74>/1\ib1' */
  VFControl_B.uib1_h = VFControl_cal->uib1_Gain_i * VFControl_B.MathFunction_l;

  /* Lookup_n-D: '<S74>/1-D Lookup Table' incorporates:
   *  Gain: '<S74>/1\ib1'
   */
  VFControl_B.uDLookupTable_b = look1_pbinlxpw(VFControl_B.uib1_h,
    VFControl_cal->uDLookupTable_bp01Data_i,
    VFControl_cal->uDLookupTable_tableData_f, &VFControl_DW.m_bpIndex_e, 2U);

  /* Sum: '<S74>/Add3' incorporates:
   *  Constant: '<S74>/Constant2'
   */
  VFControl_B.Add3_h = VFControl_B.uDLookupTable_b -
    VFControl_cal->Constant2_Value_at;

  /* ModelReference: '<Root>/Model1' incorporates:
   *  Constant: '<Root>/Constant1'
   */
  PWMGenerator(&VFControl_B.Sum8[1], &VFControl_B.Add3_h,
               &VFControl_cal->Constant1_Value_ah, &VFControl_B.Model1_o1,
               &VFControl_B.Model1_o2, &VFControl_B.Model1_o3,
               &(VFControl_DW.Model1_InstanceData.rtb));

  /* DigitalClock: '<S75>/Digital Clock' */
  VFControl_B.DigitalClock_ms = VFControl_M->Timing.t[1];

  /* Sum: '<S75>/Add1' incorporates:
   *  Constant: '<S75>/Constant3'
   */
  VFControl_B.Add1_l = VFControl_B.DigitalClock_ms +
    VFControl_cal->Constant3_Value_mm;

  /* Math: '<S75>/Math Function' incorporates:
   *  Constant: '<S75>/Constant1'
   */
  VFControl_B.MathFunction_o = rt_remd_snf(VFControl_B.Add1_l,
    VFControl_cal->Constant1_Value_p);

  /* Gain: '<S75>/1\ib1' */
  VFControl_B.uib1_e = VFControl_cal->uib1_Gain_c * VFControl_B.MathFunction_o;

  /* Lookup_n-D: '<S75>/1-D Lookup Table' incorporates:
   *  Gain: '<S75>/1\ib1'
   */
  VFControl_B.uDLookupTable_bk = look1_pbinlxpw(VFControl_B.uib1_e,
    VFControl_cal->uDLookupTable_bp01Data_m,
    VFControl_cal->uDLookupTable_tableData_h, &VFControl_DW.m_bpIndex_p, 2U);

  /* Sum: '<S75>/Add3' incorporates:
   *  Constant: '<S75>/Constant2'
   */
  VFControl_B.Add3_n = VFControl_B.uDLookupTable_bk -
    VFControl_cal->Constant2_Value_k;

  /* ModelReference: '<Root>/Model2' incorporates:
   *  Constant: '<Root>/Constant2'
   */
  PWMGenerator(&VFControl_B.Sum8[2], &VFControl_B.Add3_n,
               &VFControl_cal->Constant2_Value_a1, &VFControl_B.Model2_o1,
               &VFControl_B.Model2_o2, &VFControl_B.Model2_o3,
               &(VFControl_DW.Model2_InstanceData.rtb));

  /* TransportDelay: '<S3>/Transport Delay' */
  {
    real_T **uBuffer = (real_T**)
      &VFControl_DW.TransportDelay_PWORK.TUbufferPtrs[0];
    real_T simTime = VFControl_M->Timing.t[0];
    real_T tMinusDelay = simTime - VFControl_cal->TransportDelay_Delay;
    VFControl_B.TransportDelay = rt_TDelayInterpolate(
      tMinusDelay,
      0.0,
      *uBuffer,
      VFControl_DW.TransportDelay_IWORK.CircularBufSize,
      &VFControl_DW.TransportDelay_IWORK.Last,
      VFControl_DW.TransportDelay_IWORK.Tail,
      VFControl_DW.TransportDelay_IWORK.Head,
      (VFControl_cal->TransportDelay_InitOutput),
      1,
      0);
  }

  /* TransportDelay: '<S4>/Transport Delay' */
  {
    real_T **uBuffer = (real_T**)
      &VFControl_DW.TransportDelay_PWORK_m.TUbufferPtrs[0];
    real_T simTime = VFControl_M->Timing.t[0];
    real_T tMinusDelay = simTime - VFControl_cal->TransportDelay_Delay_l;
    VFControl_B.TransportDelay_i = rt_TDelayInterpolate(
      tMinusDelay,
      0.0,
      *uBuffer,
      VFControl_DW.TransportDelay_IWORK_g.CircularBufSize,
      &VFControl_DW.TransportDelay_IWORK_g.Last,
      VFControl_DW.TransportDelay_IWORK_g.Tail,
      VFControl_DW.TransportDelay_IWORK_g.Head,
      (VFControl_cal->TransportDelay_InitOutput_l),
      1,
      0);
  }

  /* TransportDelay: '<S5>/Transport Delay' */
  {
    real_T **uBuffer = (real_T**)
      &VFControl_DW.TransportDelay_PWORK_f.TUbufferPtrs[0];
    real_T simTime = VFControl_M->Timing.t[0];
    real_T tMinusDelay = simTime - VFControl_cal->TransportDelay_Delay_c;
    VFControl_B.TransportDelay_n = rt_TDelayInterpolate(
      tMinusDelay,
      0.0,
      *uBuffer,
      VFControl_DW.TransportDelay_IWORK_m.CircularBufSize,
      &VFControl_DW.TransportDelay_IWORK_m.Last,
      VFControl_DW.TransportDelay_IWORK_m.Tail,
      VFControl_DW.TransportDelay_IWORK_m.Head,
      (VFControl_cal->TransportDelay_InitOutput_le),
      1,
      0);
  }

  /* S-Function (sg_fpga_do_sf_a2): '<Root>/Digital output1' */

  /* Level2 S-Function Block: '<Root>/Digital output1' (sg_fpga_do_sf_a2) */
  {
    SimStruct *rts = VFControl_M->childSfunctions[0];
    sfcnOutputs(rts,0);
  }

  /* S-Function (sg_fpga_do_sf_a2): '<Root>/Digital output2' */

  /* Level2 S-Function Block: '<Root>/Digital output2' (sg_fpga_do_sf_a2) */
  {
    SimStruct *rts = VFControl_M->childSfunctions[1];
    sfcnOutputs(rts,0);
  }

  /* Switch: '<S84>/Switch' */
  if (VFControl_B.StateSpace_o2[0] >= VFControl_cal->Switch_Threshold_e) {
    /* Gain: '<S84>/1//Rsw' */
    VFControl_B.uRsw_k = VFControl_cal->uRsw_Gain * VFControl_B.StateSpace_o1[0];

    /* Switch: '<S84>/Switch' */
    VFControl_B.Switch = VFControl_B.uRsw_k;
  } else {
    /* Switch: '<S84>/Switch' incorporates:
     *  Constant: '<S84>/0 4'
     */
    VFControl_B.Switch = VFControl_cal->u4_Value_l;
  }

  /* End of Switch: '<S84>/Switch' */

  /* Saturate: '<S76>/Saturation' */
  if (VFControl_B.Model_o1 > VFControl_cal->Saturation_UpperSat) {
    xk1 = VFControl_cal->Saturation_UpperSat;
  } else if (VFControl_B.Model_o1 < VFControl_cal->Saturation_LowerSat) {
    xk1 = VFControl_cal->Saturation_LowerSat;
  } else {
    xk1 = VFControl_B.Model_o1;
  }

  /* Saturate: '<S76>/Saturation' */
  VFControl_B.Saturation[0] = xk1;

  /* Saturate: '<S76>/Saturation' */
  if (VFControl_B.TransportDelay > VFControl_cal->Saturation_UpperSat) {
    xk1 = VFControl_cal->Saturation_UpperSat;
  } else if (VFControl_B.TransportDelay < VFControl_cal->Saturation_LowerSat) {
    xk1 = VFControl_cal->Saturation_LowerSat;
  } else {
    xk1 = VFControl_B.TransportDelay;
  }

  /* Saturate: '<S76>/Saturation' */
  VFControl_B.Saturation[1] = xk1;

  /* Saturate: '<S76>/Saturation' */
  if (VFControl_B.Model1_o1 > VFControl_cal->Saturation_UpperSat) {
    xk1 = VFControl_cal->Saturation_UpperSat;
  } else if (VFControl_B.Model1_o1 < VFControl_cal->Saturation_LowerSat) {
    xk1 = VFControl_cal->Saturation_LowerSat;
  } else {
    xk1 = VFControl_B.Model1_o1;
  }

  /* Saturate: '<S76>/Saturation' */
  VFControl_B.Saturation[2] = xk1;

  /* Saturate: '<S76>/Saturation' */
  if (VFControl_B.TransportDelay_i > VFControl_cal->Saturation_UpperSat) {
    xk1 = VFControl_cal->Saturation_UpperSat;
  } else if (VFControl_B.TransportDelay_i < VFControl_cal->Saturation_LowerSat)
  {
    xk1 = VFControl_cal->Saturation_LowerSat;
  } else {
    xk1 = VFControl_B.TransportDelay_i;
  }

  /* Saturate: '<S76>/Saturation' */
  VFControl_B.Saturation[3] = xk1;

  /* Saturate: '<S76>/Saturation' */
  if (VFControl_B.Model2_o1 > VFControl_cal->Saturation_UpperSat) {
    xk1 = VFControl_cal->Saturation_UpperSat;
  } else if (VFControl_B.Model2_o1 < VFControl_cal->Saturation_LowerSat) {
    xk1 = VFControl_cal->Saturation_LowerSat;
  } else {
    xk1 = VFControl_B.Model2_o1;
  }

  /* Saturate: '<S76>/Saturation' */
  VFControl_B.Saturation[4] = xk1;

  /* Saturate: '<S76>/Saturation' */
  if (VFControl_B.TransportDelay_n > VFControl_cal->Saturation_UpperSat) {
    xk1 = VFControl_cal->Saturation_UpperSat;
  } else if (VFControl_B.TransportDelay_n < VFControl_cal->Saturation_LowerSat)
  {
    xk1 = VFControl_cal->Saturation_LowerSat;
  } else {
    xk1 = VFControl_B.TransportDelay_n;
  }

  /* Saturate: '<S76>/Saturation' */
  VFControl_B.Saturation[5] = xk1;

  /* RelationalOperator: '<S77>/Compare' incorporates:
   *  Constant: '<Root>/Constant6'
   *  Constant: '<S77>/Constant'
   */
  VFControl_B.Compare = (VFControl_cal->Constant6_Value_e <=
    VFControl_cal->CompareToConstant_const);

  /* DataTypeConversion: '<S76>/Data Type Conversion1' */
  VFControl_B.DataTypeConversion1 = VFControl_B.Compare;
  for (i = 0; i < 6; i++) {
    /* DataTypeConversion: '<S76>/Data Type Conversion' */
    xk1 = VFControl_B.Saturation[i];
    VFControl_B.DataTypeConversion_p[i] = xk1;

    /* Product: '<S76>/Product4' */
    VFControl_B.Product4[i] = xk1 * VFControl_B.DataTypeConversion1;
  }

  /* DataTypeConversion: '<S84>/Data Type Conversion' */
  VFControl_B.DataTypeConversion_f = VFControl_B.Product4[0];

  /* Switch: '<S86>/Switch' */
  if (VFControl_B.StateSpace_o2[1] >= VFControl_cal->Switch_Threshold_p) {
    /* Gain: '<S86>/1//Rsw' */
    VFControl_B.uRsw_c = VFControl_cal->uRsw_Gain_e * VFControl_B.StateSpace_o1
      [1];

    /* Switch: '<S86>/Switch' */
    VFControl_B.Switch_e = VFControl_B.uRsw_c;
  } else {
    /* Switch: '<S86>/Switch' incorporates:
     *  Constant: '<S86>/0 4'
     */
    VFControl_B.Switch_e = VFControl_cal->u4_Value_j;
  }

  /* End of Switch: '<S86>/Switch' */

  /* DataTypeConversion: '<S86>/Data Type Conversion' */
  VFControl_B.DataTypeConversion_l = VFControl_B.Product4[1];

  /* Switch: '<S88>/Switch' */
  if (VFControl_B.StateSpace_o2[2] >= VFControl_cal->Switch_Threshold_i) {
    /* Gain: '<S88>/1//Rsw' */
    VFControl_B.uRsw_g = VFControl_cal->uRsw_Gain_l * VFControl_B.StateSpace_o1
      [2];

    /* Switch: '<S88>/Switch' */
    VFControl_B.Switch_k = VFControl_B.uRsw_g;
  } else {
    /* Switch: '<S88>/Switch' incorporates:
     *  Constant: '<S88>/0 4'
     */
    VFControl_B.Switch_k = VFControl_cal->u4_Value_c;
  }

  /* End of Switch: '<S88>/Switch' */

  /* DataTypeConversion: '<S88>/Data Type Conversion' */
  VFControl_B.DataTypeConversion_h = VFControl_B.Product4[2];

  /* Switch: '<S90>/Switch' */
  if (VFControl_B.StateSpace_o2[3] >= VFControl_cal->Switch_Threshold_o) {
    /* Gain: '<S90>/1//Rsw' */
    VFControl_B.uRsw_i = VFControl_cal->uRsw_Gain_k * VFControl_B.StateSpace_o1
      [3];

    /* Switch: '<S90>/Switch' */
    VFControl_B.Switch_i = VFControl_B.uRsw_i;
  } else {
    /* Switch: '<S90>/Switch' incorporates:
     *  Constant: '<S90>/0 4'
     */
    VFControl_B.Switch_i = VFControl_cal->u4_Value_m;
  }

  /* End of Switch: '<S90>/Switch' */

  /* DataTypeConversion: '<S90>/Data Type Conversion' */
  VFControl_B.DataTypeConversion_k = VFControl_B.Product4[3];

  /* Switch: '<S92>/Switch' */
  if (VFControl_B.StateSpace_o2[4] >= VFControl_cal->Switch_Threshold_a) {
    /* Gain: '<S92>/1//Rsw' */
    VFControl_B.uRsw_n = VFControl_cal->uRsw_Gain_m * VFControl_B.StateSpace_o1
      [4];

    /* Switch: '<S92>/Switch' */
    VFControl_B.Switch_a = VFControl_B.uRsw_n;
  } else {
    /* Switch: '<S92>/Switch' incorporates:
     *  Constant: '<S92>/0 4'
     */
    VFControl_B.Switch_a = VFControl_cal->u4_Value_lk;
  }

  /* End of Switch: '<S92>/Switch' */

  /* DataTypeConversion: '<S92>/Data Type Conversion' */
  VFControl_B.DataTypeConversion_i = VFControl_B.Product4[4];

  /* Switch: '<S94>/Switch' */
  if (VFControl_B.StateSpace_o2[5] >= VFControl_cal->Switch_Threshold_n) {
    /* Gain: '<S94>/1//Rsw' */
    VFControl_B.uRsw = VFControl_cal->uRsw_Gain_k4 * VFControl_B.StateSpace_o1[5];

    /* Switch: '<S94>/Switch' */
    VFControl_B.Switch_a4 = VFControl_B.uRsw;
  } else {
    /* Switch: '<S94>/Switch' incorporates:
     *  Constant: '<S94>/0 4'
     */
    VFControl_B.Switch_a4 = VFControl_cal->u4_Value_o;
  }

  /* End of Switch: '<S94>/Switch' */

  /* DataTypeConversion: '<S94>/Data Type Conversion' */
  VFControl_B.DataTypeConversion_c = VFControl_B.Product4[5];

  /* Product: '<S3>/Product' incorporates:
   *  Constant: '<S3>/Constant'
   */
  VFControl_B.Product_n = VFControl_B.Model_o1 * VFControl_cal->Constant_Value_a;

  /* Product: '<S4>/Product' incorporates:
   *  Constant: '<S4>/Constant'
   */
  VFControl_B.Product_b = VFControl_B.Model1_o1 *
    VFControl_cal->Constant_Value_g;

  /* Product: '<S5>/Product' incorporates:
   *  Constant: '<S5>/Constant'
   */
  VFControl_B.Product_l = VFControl_B.Model2_o1 *
    VFControl_cal->Constant_Value_n5;

  /* RelationalOperator: '<S46>/Relational Operator2' incorporates:
   *  Constant: '<S46>/Constant3'
   *  Constant: '<S46>/valp_nom3'
   */
  VFControl_B.Frequency = (VFControl_cal->valp_nom3_Value ==
    VFControl_cal->Constant3_Value_n);

  /* Switch: '<S42>/Switch4' */
  if (VFControl_B.Frequency) {
    /* Switch: '<S42>/Switch4' */
    VFControl_B.Switch4 = VFControl_B.Switch3;
  } else {
    /* Switch: '<S42>/Switch4' incorporates:
     *  Constant: '<S42>/Constant5'
     */
    VFControl_B.Switch4 = VFControl_cal->Constant5_Value_ct;
  }

  /* End of Switch: '<S42>/Switch4' */

  /* Gain: '<S42>/Gain2' */
  VFControl_B.Gain2 = VFControl_cal->Gain2_Gain_h * VFControl_B.Switch4;

  /* Sum: '<S42>/Sum1' incorporates:
   *  Constant: '<S42>/valp_nom1'
   */
  VFControl_B.Sum1_o = VFControl_cal->valp_nom1_Value_g + VFControl_B.Switch4;

  /* Gain: '<S40>/Gain3' */
  VFControl_B.Gain3_m[0] = VFControl_cal->Gain3_Gain_i * VFControl_B.Switch5_b[0];
  VFControl_B.Gain3_m[1] = VFControl_cal->Gain3_Gain_i * VFControl_B.Switch5_b[1];
  VFControl_B.Gain3_m[2] = VFControl_cal->Gain3_Gain_i * VFControl_B.Switch5_b[2];

  /* Math: '<S44>/Math Function' incorporates:
   *  Constant: '<S44>/Constant7'
   */
  VFControl_B.wtrad = rt_modd_snf(VFControl_B.Sum7[0],
    VFControl_cal->Constant7_Value);

  /* Switch: '<S56>/Switch' */
  if (VFControl_B.Ton >= VFControl_cal->Switch_Threshold_d) {
    /* RelationalOperator: '<S56>/Relational Operator' incorporates:
     *  Constant: '<S47>/valp_nom5'
     *  Constant: '<S56>/Constant'
     */
    VFControl_B.RelationalOperator = (VFControl_cal->valp_nom5_Value ==
      VFControl_cal->Constant_Value_p);

    /* Switch: '<S56>/Switch1' */
    if (VFControl_B.RelationalOperator) {
      /* Switch: '<S56>/Switch1' incorporates:
       *  Constant: '<S56>/valp_nom7'
       */
      VFControl_B.Switch1_j = VFControl_cal->Variationvaluesteprampmod_Var_p;
    } else {
      /* Switch: '<S56>/Switch1' incorporates:
       *  Constant: '<S56>/Constant2'
       */
      VFControl_B.Switch1_j = VFControl_cal->Constant2_Value_i;
    }

    /* End of Switch: '<S56>/Switch1' */

    /* Switch: '<S56>/Switch' */
    VFControl_B.Switch_c = VFControl_B.Switch1_j;
  } else {
    /* Switch: '<S56>/Switch' incorporates:
     *  Constant: '<S56>/Constant4'
     */
    VFControl_B.Switch_c = VFControl_cal->Constant4_Value_f;
  }

  /* End of Switch: '<S56>/Switch' */

  /* Gain: '<S64>/do not delete this gain' */
  VFControl_B.donotdeletethisgain = VFControl_cal->donotdeletethisgain_Gain *
    VFControl_B.StateSpace_o1[8];

  /* Gain: '<S65>/do not delete this gain' */
  VFControl_B.donotdeletethisgain_k = VFControl_cal->donotdeletethisgain_Gain_d *
    VFControl_B.StateSpace_o1[9];

  /* Gain: '<S66>/do not delete this gain' */
  VFControl_B.donotdeletethisgain_o = VFControl_cal->donotdeletethisgain_Gain_h *
    VFControl_B.StateSpace_o1[10];

  /* Gain: '<S7>/Kv1' */
  VFControl_B.Kv1[0] = VFControl_cal->Kv1_Gain * VFControl_B.donotdeletethisgain;
  VFControl_B.Kv1[1] = VFControl_cal->Kv1_Gain *
    VFControl_B.donotdeletethisgain_k;
  VFControl_B.Kv1[2] = VFControl_cal->Kv1_Gain *
    VFControl_B.donotdeletethisgain_o;

  /* Gain: '<S61>/do not delete this gain' */
  VFControl_B.donotdeletethisgain_kv = VFControl_cal->donotdeletethisgain_Gain_j
    * VFControl_B.StateSpace_o1[11];

  /* Gain: '<S62>/do not delete this gain' */
  VFControl_B.donotdeletethisgain_b = VFControl_cal->donotdeletethisgain_Gain_n *
    VFControl_B.StateSpace_o1[12];

  /* Gain: '<S63>/do not delete this gain' */
  VFControl_B.donotdeletethisgain_h = VFControl_cal->donotdeletethisgain_Gain_p *
    VFControl_B.StateSpace_o1[13];

  /* Gain: '<S7>/Kv' */
  VFControl_B.Kv[0] = VFControl_cal->Kv_Gain *
    VFControl_B.donotdeletethisgain_kv;
  VFControl_B.Kv[1] = VFControl_cal->Kv_Gain * VFControl_B.donotdeletethisgain_b;
  VFControl_B.Kv[2] = VFControl_cal->Kv_Gain * VFControl_B.donotdeletethisgain_h;

  /* user code (Output function Trailer for TID0) */
  {
    if (0) {
      io3xx_sgdma_feedSequential(1);
    }
  }

  /* Update for UnitDelay: '<S23>/fluxes' */
  VFControl_DW.fluxes_DSTATE[0] = VFControl_B.IC[0];

  /* Update for UnitDelay: '<S21>/fluxes' */
  VFControl_DW.fluxes_DSTATE_l[0] = VFControl_B.xk1[0];

  /* Update for UnitDelay: '<S23>/fluxes' */
  VFControl_DW.fluxes_DSTATE[1] = VFControl_B.IC[1];

  /* Update for UnitDelay: '<S21>/fluxes' */
  VFControl_DW.fluxes_DSTATE_l[1] = VFControl_B.xk1[1];

  /* Update for UnitDelay: '<S23>/fluxes' */
  VFControl_DW.fluxes_DSTATE[2] = VFControl_B.IC[2];

  /* Update for UnitDelay: '<S21>/fluxes' */
  VFControl_DW.fluxes_DSTATE_l[2] = VFControl_B.xk1[2];

  /* Update for UnitDelay: '<S23>/fluxes' */
  VFControl_DW.fluxes_DSTATE[3] = VFControl_B.IC[3];

  /* Update for UnitDelay: '<S21>/fluxes' */
  VFControl_DW.fluxes_DSTATE_l[3] = VFControl_B.xk1[3];

  /* Update for DiscreteIntegrator: '<S15>/Rotor angle thetam' */
  VFControl_DW.Rotoranglethetam_DSTATE +=
    VFControl_cal->Rotoranglethetam_gainval * VFControl_B.web_psb;

  /* Update for UnitDelay: '<S38>/wm_delay' */
  VFControl_DW.wm_delay_DSTATE = VFControl_B.Rotorspeedwm;

  /* Update for UnitDelay: '<S38>/wm_predict' */
  VFControl_DW.wm_predict_DSTATE = VFControl_B.wm_delay;

  /* Update for S-Function (sfun_spssw_discc): '<S96>/State-Space' incorporates:
   *  Constant: '<S39>/DC'
   *  Constant: '<S98>/SwitchCurrents'
   */
  {
    int_T *gState = (int_T*)VFControl_DW.StateSpace_PWORK.G_STATE;

    /* Store switch gates values for next step */
    *(gState++) = (int_T) VFControl_B.DataTypeConversion_f;
    *(gState++) = (int_T) VFControl_B.DataTypeConversion_l;
    *(gState++) = (int_T) VFControl_B.DataTypeConversion_h;
    *(gState++) = (int_T) VFControl_B.DataTypeConversion_k;
    *(gState++) = (int_T) VFControl_B.DataTypeConversion_i;
    *(gState++) = (int_T) VFControl_B.DataTypeConversion_c;
  }

  /* Update for UnitDelay: '<S23>/voltages' */
  VFControl_DW.voltages_DSTATE[0] = VFControl_B.MultiportSwitch1_f[0];
  VFControl_DW.voltages_DSTATE[2] = VFControl_B.MultiportSwitch_p2[0];
  VFControl_DW.voltages_DSTATE[1] = VFControl_B.MultiportSwitch1_f[1];
  VFControl_DW.voltages_DSTATE[3] = VFControl_B.MultiportSwitch_p2[1];

  /* Update for DiscreteIntegrator: '<S15>/Rotor speed(wm)' */
  VFControl_DW.Rotorspeedwm_SYSTEM_ENABLE = 0U;
  VFControl_DW.Rotorspeedwm_DSTATE = VFControl_cal->Rotorspeedwm_gainval *
    VFControl_B.u_2H + VFControl_B.Rotorspeedwm;

  /* Update for DiscreteIntegrator: '<S47>/Discrete-Time Integrator1' */
  VFControl_DW.DiscreteTimeIntegrator1_DSTATE +=
    VFControl_cal->DiscreteTimeIntegrator1_gainval * VFControl_B.Switch_c;

  /* Update for UnitDelay: '<S47>/Unit Delay1' */
  VFControl_DW.UnitDelay1_DSTATE = VFControl_B.Switch2_d;

  /* Update for DiscreteIntegrator: '<S42>/Discrete-Time Integrator' */
  VFControl_DW.DiscreteTimeIntegrator_DSTATE +=
    VFControl_cal->DiscreteTimeIntegrator_gainval * VFControl_B.Gain2;

  /* Update for TransportDelay: '<S3>/Transport Delay' */
  {
    real_T **uBuffer = (real_T**)
      &VFControl_DW.TransportDelay_PWORK.TUbufferPtrs[0];
    real_T simTime = VFControl_M->Timing.t[0];
    VFControl_DW.TransportDelay_IWORK.Head =
      ((VFControl_DW.TransportDelay_IWORK.Head <
        (VFControl_DW.TransportDelay_IWORK.CircularBufSize-1)) ?
       (VFControl_DW.TransportDelay_IWORK.Head+1) : 0);
    if (VFControl_DW.TransportDelay_IWORK.Head ==
        VFControl_DW.TransportDelay_IWORK.Tail) {
      VFControl_DW.TransportDelay_IWORK.Tail =
        ((VFControl_DW.TransportDelay_IWORK.Tail <
          (VFControl_DW.TransportDelay_IWORK.CircularBufSize-1)) ?
         (VFControl_DW.TransportDelay_IWORK.Tail+1) : 0);
    }

    (*uBuffer + VFControl_DW.TransportDelay_IWORK.CircularBufSize)
      [VFControl_DW.TransportDelay_IWORK.Head] = simTime;
    (*uBuffer)[VFControl_DW.TransportDelay_IWORK.Head] = VFControl_B.Product_n;
  }

  /* Update for TransportDelay: '<S4>/Transport Delay' */
  {
    real_T **uBuffer = (real_T**)
      &VFControl_DW.TransportDelay_PWORK_m.TUbufferPtrs[0];
    real_T simTime = VFControl_M->Timing.t[0];
    VFControl_DW.TransportDelay_IWORK_g.Head =
      ((VFControl_DW.TransportDelay_IWORK_g.Head <
        (VFControl_DW.TransportDelay_IWORK_g.CircularBufSize-1)) ?
       (VFControl_DW.TransportDelay_IWORK_g.Head+1) : 0);
    if (VFControl_DW.TransportDelay_IWORK_g.Head ==
        VFControl_DW.TransportDelay_IWORK_g.Tail) {
      VFControl_DW.TransportDelay_IWORK_g.Tail =
        ((VFControl_DW.TransportDelay_IWORK_g.Tail <
          (VFControl_DW.TransportDelay_IWORK_g.CircularBufSize-1)) ?
         (VFControl_DW.TransportDelay_IWORK_g.Tail+1) : 0);
    }

    (*uBuffer + VFControl_DW.TransportDelay_IWORK_g.CircularBufSize)
      [VFControl_DW.TransportDelay_IWORK_g.Head] = simTime;
    (*uBuffer)[VFControl_DW.TransportDelay_IWORK_g.Head] = VFControl_B.Product_b;
  }

  /* Update for TransportDelay: '<S5>/Transport Delay' */
  {
    real_T **uBuffer = (real_T**)
      &VFControl_DW.TransportDelay_PWORK_f.TUbufferPtrs[0];
    real_T simTime = VFControl_M->Timing.t[0];
    VFControl_DW.TransportDelay_IWORK_m.Head =
      ((VFControl_DW.TransportDelay_IWORK_m.Head <
        (VFControl_DW.TransportDelay_IWORK_m.CircularBufSize-1)) ?
       (VFControl_DW.TransportDelay_IWORK_m.Head+1) : 0);
    if (VFControl_DW.TransportDelay_IWORK_m.Head ==
        VFControl_DW.TransportDelay_IWORK_m.Tail) {
      VFControl_DW.TransportDelay_IWORK_m.Tail =
        ((VFControl_DW.TransportDelay_IWORK_m.Tail <
          (VFControl_DW.TransportDelay_IWORK_m.CircularBufSize-1)) ?
         (VFControl_DW.TransportDelay_IWORK_m.Tail+1) : 0);
    }

    (*uBuffer + VFControl_DW.TransportDelay_IWORK_m.CircularBufSize)
      [VFControl_DW.TransportDelay_IWORK_m.Head] = simTime;
    (*uBuffer)[VFControl_DW.TransportDelay_IWORK_m.Head] = VFControl_B.Product_l;
  }

  /* Update absolute time */
  /* The "clockTick0" counts the number of times the code of this task has
   * been executed. The absolute time is the multiplication of "clockTick0"
   * and "Timing.stepSize0". Size of "clockTick0" ensures timer will not
   * overflow during the application lifespan selected.
   * Timer of this task consists of two 32 bit unsigned integers.
   * The two integers represent the low bits Timing.clockTick0 and the high bits
   * Timing.clockTickH0. When the low bit overflows to 0, the high bits increment.
   */
  if (!(++VFControl_M->Timing.clockTick0)) {
    ++VFControl_M->Timing.clockTickH0;
  }

  VFControl_M->Timing.t[0] = VFControl_M->Timing.clockTick0 *
    VFControl_M->Timing.stepSize0 + VFControl_M->Timing.clockTickH0 *
    VFControl_M->Timing.stepSize0 * 4294967296.0;

  /* Update absolute time */
  /* The "clockTick1" counts the number of times the code of this task has
   * been executed. The absolute time is the multiplication of "clockTick1"
   * and "Timing.stepSize1". Size of "clockTick1" ensures timer will not
   * overflow during the application lifespan selected.
   * Timer of this task consists of two 32 bit unsigned integers.
   * The two integers represent the low bits Timing.clockTick1 and the high bits
   * Timing.clockTickH1. When the low bit overflows to 0, the high bits increment.
   */
  if (!(++VFControl_M->Timing.clockTick1)) {
    ++VFControl_M->Timing.clockTickH1;
  }

  VFControl_M->Timing.t[1] = VFControl_M->Timing.clockTick1 *
    VFControl_M->Timing.stepSize1 + VFControl_M->Timing.clockTickH1 *
    VFControl_M->Timing.stepSize1 * 4294967296.0;
}

/* Model step function for TID2 */
void VFControl_step2(void)             /* Sample time: [0.0005s, 0.0s] */
{
  /* UnitDelay: '<S73>/Unit Delay' */
  VFControl_B.UnitDelay = VFControl_DW.UnitDelay_DSTATE;

  /* Update for UnitDelay: '<S73>/Unit Delay' */
  VFControl_DW.UnitDelay_DSTATE = VFControl_B.UnitDelay;

  /* Update absolute time */
  /* The "clockTick2" counts the number of times the code of this task has
   * been executed. The resolution of this integer timer is 0.0005, which is the step size
   * of the task. Size of "clockTick2" ensures timer will not overflow during the
   * application lifespan selected.
   * Timer of this task consists of two 32 bit unsigned integers.
   * The two integers represent the low bits Timing.clockTick2 and the high bits
   * Timing.clockTickH2. When the low bit overflows to 0, the high bits increment.
   */
  VFControl_M->Timing.clockTick2++;
  if (!VFControl_M->Timing.clockTick2) {
    VFControl_M->Timing.clockTickH2++;
  }
}

/* Model initialize function */
void VFControl_initialize(void)
{
  /* Registration code */

  /* initialize non-finites */
  rt_InitInfAndNaN(sizeof(real_T));

  /* Set task counter limit used by the static main program */
  (VFControl_M)->Timing.TaskCounters.cLimit[0] = 1;
  (VFControl_M)->Timing.TaskCounters.cLimit[1] = 1;
  (VFControl_M)->Timing.TaskCounters.cLimit[2] = 5;

  {
    /* Setup solver object */
    rtsiSetSimTimeStepPtr(&VFControl_M->solverInfo,
                          &VFControl_M->Timing.simTimeStep);
    rtsiSetTPtr(&VFControl_M->solverInfo, &rtmGetTPtr(VFControl_M));
    rtsiSetStepSizePtr(&VFControl_M->solverInfo, &VFControl_M->Timing.stepSize0);
    rtsiSetErrorStatusPtr(&VFControl_M->solverInfo, (&rtmGetErrorStatus
      (VFControl_M)));
    rtsiSetRTModelPtr(&VFControl_M->solverInfo, VFControl_M);
  }

  rtsiSetSimTimeStep(&VFControl_M->solverInfo, MAJOR_TIME_STEP);
  rtsiSetIsMinorTimeStepWithModeChange(&VFControl_M->solverInfo, false);
  rtsiSetIsContModeFrozen(&VFControl_M->solverInfo, false);
  rtsiSetSolverName(&VFControl_M->solverInfo,"FixedStepDiscrete");
  VFControl_M->solverInfoPtr = (&VFControl_M->solverInfo);

  /* Initialize timing info */
  {
    int_T *mdlTsMap = VFControl_M->Timing.sampleTimeTaskIDArray;
    mdlTsMap[0] = 0;
    mdlTsMap[1] = 1;
    mdlTsMap[2] = 2;

    /* polyspace +2 MISRA2012:D4.1 [Justified:Low] "VFControl_M points to
       static memory which is guaranteed to be non-NULL" */
    VFControl_M->Timing.sampleTimeTaskIDPtr = (&mdlTsMap[0]);
    VFControl_M->Timing.sampleTimes = (&VFControl_M->Timing.sampleTimesArray[0]);
    VFControl_M->Timing.offsetTimes = (&VFControl_M->Timing.offsetTimesArray[0]);

    /* task periods */
    VFControl_M->Timing.sampleTimes[0] = (0.0);
    VFControl_M->Timing.sampleTimes[1] = (0.0001);
    VFControl_M->Timing.sampleTimes[2] = (0.0005);

    /* task offsets */
    VFControl_M->Timing.offsetTimes[0] = (0.0);
    VFControl_M->Timing.offsetTimes[1] = (0.0);
    VFControl_M->Timing.offsetTimes[2] = (0.0);
  }

  rtmSetTPtr(VFControl_M, &VFControl_M->Timing.tArray[0]);

  {
    int_T *mdlSampleHits = VFControl_M->Timing.sampleHitArray;
    int_T *mdlPerTaskSampleHits = VFControl_M->Timing.perTaskSampleHitsArray;
    VFControl_M->Timing.perTaskSampleHits = (&mdlPerTaskSampleHits[0]);
    mdlSampleHits[0] = 1;
    VFControl_M->Timing.sampleHits = (&mdlSampleHits[0]);
  }

  rtmSetTFinal(VFControl_M, -1);
  VFControl_M->Timing.stepSize0 = 0.0001;
  VFControl_M->Timing.stepSize1 = 0.0001;
  VFControl_M->solverInfoPtr = (&VFControl_M->solverInfo);
  VFControl_M->Timing.stepSize = (0.0001);
  rtsiSetFixedStepSize(&VFControl_M->solverInfo, 0.0001);
  rtsiSetSolverMode(&VFControl_M->solverInfo, SOLVER_MODE_MULTITASKING);

  /* block I/O */
  (void) std::memset((static_cast<void *>(&VFControl_B)), 0,
                     sizeof(B_VFControl_T));

  /* states (dwork) */
  (void) std::memset(static_cast<void *>(&VFControl_DW), 0,
                     sizeof(DW_VFControl_T));

  /* child S-Function registration */
  {
    RTWSfcnInfo *sfcnInfo = &VFControl_M->NonInlinedSFcns.sfcnInfo;
    VFControl_M->sfcnInfo = (sfcnInfo);
    rtssSetErrorStatusPtr(sfcnInfo, (&rtmGetErrorStatus(VFControl_M)));
    VFControl_M->Sizes.numSampTimes = (3);
    rtssSetNumRootSampTimesPtr(sfcnInfo, &VFControl_M->Sizes.numSampTimes);
    VFControl_M->NonInlinedSFcns.taskTimePtrs[0] = (&rtmGetTPtr(VFControl_M)[0]);
    VFControl_M->NonInlinedSFcns.taskTimePtrs[1] = (&rtmGetTPtr(VFControl_M)[1]);
    VFControl_M->NonInlinedSFcns.taskTimePtrs[2] = (&rtmGetTPtr(VFControl_M)[2]);
    rtssSetTPtrPtr(sfcnInfo,VFControl_M->NonInlinedSFcns.taskTimePtrs);
    rtssSetTStartPtr(sfcnInfo, &rtmGetTStart(VFControl_M));
    rtssSetTFinalPtr(sfcnInfo, &rtmGetTFinal(VFControl_M));
    rtssSetTimeOfLastOutputPtr(sfcnInfo, &rtmGetTimeOfLastOutput(VFControl_M));
    rtssSetStepSizePtr(sfcnInfo, &VFControl_M->Timing.stepSize);
    rtssSetStopRequestedPtr(sfcnInfo, &rtmGetStopRequested(VFControl_M));
    rtssSetDerivCacheNeedsResetPtr(sfcnInfo, &VFControl_M->derivCacheNeedsReset);
    rtssSetZCCacheNeedsResetPtr(sfcnInfo, &VFControl_M->zCCacheNeedsReset);
    rtssSetContTimeOutputInconsistentWithStateAtMajorStepPtr(sfcnInfo,
      &VFControl_M->CTOutputIncnstWithState);
    rtssSetSampleHitsPtr(sfcnInfo, &VFControl_M->Timing.sampleHits);
    rtssSetPerTaskSampleHitsPtr(sfcnInfo, &VFControl_M->Timing.perTaskSampleHits);
    rtssSetSimModePtr(sfcnInfo, &VFControl_M->simMode);
    rtssSetSolverInfoPtr(sfcnInfo, &VFControl_M->solverInfoPtr);
  }

  VFControl_M->Sizes.numSFcns = (2);

  /* register each child */
  {
    (void) std::memset(static_cast<void *>
                       (&VFControl_M->NonInlinedSFcns.childSFunctions[0]), 0,
                       2*sizeof(SimStruct));
    VFControl_M->childSfunctions =
      (&VFControl_M->NonInlinedSFcns.childSFunctionPtrs[0]);
    VFControl_M->childSfunctions[0] =
      (&VFControl_M->NonInlinedSFcns.childSFunctions[0]);
    VFControl_M->childSfunctions[1] =
      (&VFControl_M->NonInlinedSFcns.childSFunctions[1]);

    /* Level2 S-Function Block: VFControl/<Root>/Digital output1 (sg_fpga_do_sf_a2) */
    {
      SimStruct *rts = VFControl_M->childSfunctions[0];

      /* timing info */
      time_T *sfcnPeriod = VFControl_M->NonInlinedSFcns.Sfcn0.sfcnPeriod;
      time_T *sfcnOffset = VFControl_M->NonInlinedSFcns.Sfcn0.sfcnOffset;
      int_T *sfcnTsMap = VFControl_M->NonInlinedSFcns.Sfcn0.sfcnTsMap;
      (void) std::memset(static_cast<void*>(sfcnPeriod), 0,
                         sizeof(time_T)*1);
      (void) std::memset(static_cast<void*>(sfcnOffset), 0,
                         sizeof(time_T)*1);
      ssSetSampleTimePtr(rts, &sfcnPeriod[0]);
      ssSetOffsetTimePtr(rts, &sfcnOffset[0]);
      ssSetSampleTimeTaskIDPtr(rts, sfcnTsMap);

      {
        ssSetBlkInfo2Ptr(rts, &VFControl_M->NonInlinedSFcns.blkInfo2[0]);
      }

      _ssSetBlkInfo2PortInfo2Ptr(rts,
        &VFControl_M->NonInlinedSFcns.inputOutputPortInfo2[0]);

      /* Set up the mdlInfo pointer */
      ssSetRTWSfcnInfo(rts, VFControl_M->sfcnInfo);

      /* Allocate memory of model methods 2 */
      {
        ssSetModelMethods2(rts, &VFControl_M->NonInlinedSFcns.methods2[0]);
      }

      /* Allocate memory of model methods 3 */
      {
        ssSetModelMethods3(rts, &VFControl_M->NonInlinedSFcns.methods3[0]);
      }

      /* Allocate memory of model methods 4 */
      {
        ssSetModelMethods4(rts, &VFControl_M->NonInlinedSFcns.methods4[0]);
      }

      /* Allocate memory for states auxilliary information */
      {
        ssSetStatesInfo2(rts, &VFControl_M->NonInlinedSFcns.statesInfo2[0]);
        ssSetPeriodicStatesInfo(rts,
          &VFControl_M->NonInlinedSFcns.periodicStatesInfo[0]);
      }

      /* inputs */
      {
        _ssSetNumInputPorts(rts, 3);
        ssSetPortInfoForInputs(rts,
          &VFControl_M->NonInlinedSFcns.Sfcn0.inputPortInfo[0]);
        ssSetPortInfoForInputs(rts,
          &VFControl_M->NonInlinedSFcns.Sfcn0.inputPortInfo[0]);
        _ssSetPortInfo2ForInputUnits(rts,
          &VFControl_M->NonInlinedSFcns.Sfcn0.inputPortUnits[0]);
        ssSetInputPortUnit(rts, 0, 0);
        ssSetInputPortUnit(rts, 1, 0);
        ssSetInputPortUnit(rts, 2, 0);
        _ssSetPortInfo2ForInputCoSimAttribute(rts,
          &VFControl_M->NonInlinedSFcns.Sfcn0.inputPortCoSimAttribute[0]);
        ssSetInputPortIsContinuousQuantity(rts, 0, 0);
        ssSetInputPortIsContinuousQuantity(rts, 1, 0);
        ssSetInputPortIsContinuousQuantity(rts, 2, 0);

        /* port 0 */
        {
          ssSetInputPortRequiredContiguous(rts, 0, 1);
          ssSetInputPortSignal(rts, 0, &VFControl_B.TransportDelay);
          _ssSetInputPortNumDimensions(rts, 0, 1);
          ssSetInputPortWidthAsInt(rts, 0, 1);
        }

        /* port 1 */
        {
          ssSetInputPortRequiredContiguous(rts, 1, 1);
          ssSetInputPortSignal(rts, 1, &VFControl_B.TransportDelay_i);
          _ssSetInputPortNumDimensions(rts, 1, 1);
          ssSetInputPortWidthAsInt(rts, 1, 1);
        }

        /* port 2 */
        {
          ssSetInputPortRequiredContiguous(rts, 2, 1);
          ssSetInputPortSignal(rts, 2, &VFControl_B.TransportDelay_n);
          _ssSetInputPortNumDimensions(rts, 2, 1);
          ssSetInputPortWidthAsInt(rts, 2, 1);
        }
      }

      /* path info */
      ssSetModelName(rts, "Digital output1");
      ssSetPath(rts, "VFControl/Digital output1");
      ssSetRTModel(rts,VFControl_M);
      ssSetParentSS(rts, (NULL));
      ssSetRootSS(rts, rts);
      ssSetVersion(rts, SIMSTRUCT_VERSION_LEVEL2);

      /* parameters */
      {
        mxArray **sfcnParams = (mxArray **)
          &VFControl_M->NonInlinedSFcns.Sfcn0.params;
        ssSetSFcnParamsCount(rts, 6);
        ssSetSFcnParamsPtr(rts, &sfcnParams[0]);
        ssSetSFcnParam(rts, 0, (mxArray*)VFControl_cal->Digitaloutput1_P1_Size);
        ssSetSFcnParam(rts, 1, (mxArray*)VFControl_cal->Digitaloutput1_P2_Size);
        ssSetSFcnParam(rts, 2, (mxArray*)VFControl_cal->Digitaloutput1_P3_Size);
        ssSetSFcnParam(rts, 3, (mxArray*)VFControl_cal->Digitaloutput1_P4_Size);
        ssSetSFcnParam(rts, 4, (mxArray*)VFControl_cal->Digitaloutput1_P5_Size);
        ssSetSFcnParam(rts, 5, (mxArray*)VFControl_cal->Digitaloutput1_P6_Size);
      }

      /* work vectors */
      ssSetPWork(rts, (void **) &VFControl_DW.Digitaloutput1_PWORK[0]);

      {
        struct _ssDWorkRecord *dWorkRecord = (struct _ssDWorkRecord *)
          &VFControl_M->NonInlinedSFcns.Sfcn0.dWork;
        struct _ssDWorkAuxRecord *dWorkAuxRecord = (struct _ssDWorkAuxRecord *)
          &VFControl_M->NonInlinedSFcns.Sfcn0.dWorkAux;
        ssSetSFcnDWork(rts, dWorkRecord);
        ssSetSFcnDWorkAux(rts, dWorkAuxRecord);
        ssSetNumDWorkAsInt(rts, 1);

        /* PWORK */
        ssSetDWorkWidthAsInt(rts, 0, 2);
        ssSetDWorkDataType(rts, 0,SS_POINTER);
        ssSetDWorkComplexSignal(rts, 0, 0);
        ssSetDWork(rts, 0, &VFControl_DW.Digitaloutput1_PWORK[0]);
      }

      /* registration */
      sg_fpga_do_sf_a2(rts);
      sfcnInitializeSizes(rts);
      sfcnInitializeSampleTimes(rts);

      /* adjust sample time */
      ssSetSampleTime(rts, 0, 0.0001);
      ssSetOffsetTime(rts, 0, 0.0);
      sfcnTsMap[0] = 1;

      /* set compiled values of dynamic vector attributes */
      ssSetNumNonsampledZCsAsInt(rts, 0);

      /* Update connectivity flags for each port */
      _ssSetInputPortConnected(rts, 0, 1);
      _ssSetInputPortConnected(rts, 1, 1);
      _ssSetInputPortConnected(rts, 2, 1);

      /* Update the BufferDstPort flags for each input port */
      ssSetInputPortBufferDstPort(rts, 0, -1);
      ssSetInputPortBufferDstPort(rts, 1, -1);
      ssSetInputPortBufferDstPort(rts, 2, -1);
    }

    /* Level2 S-Function Block: VFControl/<Root>/Digital output2 (sg_fpga_do_sf_a2) */
    {
      SimStruct *rts = VFControl_M->childSfunctions[1];

      /* timing info */
      time_T *sfcnPeriod = VFControl_M->NonInlinedSFcns.Sfcn1.sfcnPeriod;
      time_T *sfcnOffset = VFControl_M->NonInlinedSFcns.Sfcn1.sfcnOffset;
      int_T *sfcnTsMap = VFControl_M->NonInlinedSFcns.Sfcn1.sfcnTsMap;
      (void) std::memset(static_cast<void*>(sfcnPeriod), 0,
                         sizeof(time_T)*1);
      (void) std::memset(static_cast<void*>(sfcnOffset), 0,
                         sizeof(time_T)*1);
      ssSetSampleTimePtr(rts, &sfcnPeriod[0]);
      ssSetOffsetTimePtr(rts, &sfcnOffset[0]);
      ssSetSampleTimeTaskIDPtr(rts, sfcnTsMap);

      {
        ssSetBlkInfo2Ptr(rts, &VFControl_M->NonInlinedSFcns.blkInfo2[1]);
      }

      _ssSetBlkInfo2PortInfo2Ptr(rts,
        &VFControl_M->NonInlinedSFcns.inputOutputPortInfo2[1]);

      /* Set up the mdlInfo pointer */
      ssSetRTWSfcnInfo(rts, VFControl_M->sfcnInfo);

      /* Allocate memory of model methods 2 */
      {
        ssSetModelMethods2(rts, &VFControl_M->NonInlinedSFcns.methods2[1]);
      }

      /* Allocate memory of model methods 3 */
      {
        ssSetModelMethods3(rts, &VFControl_M->NonInlinedSFcns.methods3[1]);
      }

      /* Allocate memory of model methods 4 */
      {
        ssSetModelMethods4(rts, &VFControl_M->NonInlinedSFcns.methods4[1]);
      }

      /* Allocate memory for states auxilliary information */
      {
        ssSetStatesInfo2(rts, &VFControl_M->NonInlinedSFcns.statesInfo2[1]);
        ssSetPeriodicStatesInfo(rts,
          &VFControl_M->NonInlinedSFcns.periodicStatesInfo[1]);
      }

      /* inputs */
      {
        _ssSetNumInputPorts(rts, 3);
        ssSetPortInfoForInputs(rts,
          &VFControl_M->NonInlinedSFcns.Sfcn1.inputPortInfo[0]);
        ssSetPortInfoForInputs(rts,
          &VFControl_M->NonInlinedSFcns.Sfcn1.inputPortInfo[0]);
        _ssSetPortInfo2ForInputUnits(rts,
          &VFControl_M->NonInlinedSFcns.Sfcn1.inputPortUnits[0]);
        ssSetInputPortUnit(rts, 0, 0);
        ssSetInputPortUnit(rts, 1, 0);
        ssSetInputPortUnit(rts, 2, 0);
        _ssSetPortInfo2ForInputCoSimAttribute(rts,
          &VFControl_M->NonInlinedSFcns.Sfcn1.inputPortCoSimAttribute[0]);
        ssSetInputPortIsContinuousQuantity(rts, 0, 0);
        ssSetInputPortIsContinuousQuantity(rts, 1, 0);
        ssSetInputPortIsContinuousQuantity(rts, 2, 0);

        /* port 0 */
        {
          ssSetInputPortRequiredContiguous(rts, 0, 1);
          ssSetInputPortSignal(rts, 0, &VFControl_B.Model_o1);
          _ssSetInputPortNumDimensions(rts, 0, 1);
          ssSetInputPortWidthAsInt(rts, 0, 1);
        }

        /* port 1 */
        {
          ssSetInputPortRequiredContiguous(rts, 1, 1);
          ssSetInputPortSignal(rts, 1, &VFControl_B.Model1_o1);
          _ssSetInputPortNumDimensions(rts, 1, 1);
          ssSetInputPortWidthAsInt(rts, 1, 1);
        }

        /* port 2 */
        {
          ssSetInputPortRequiredContiguous(rts, 2, 1);
          ssSetInputPortSignal(rts, 2, &VFControl_B.Model2_o1);
          _ssSetInputPortNumDimensions(rts, 2, 1);
          ssSetInputPortWidthAsInt(rts, 2, 1);
        }
      }

      /* path info */
      ssSetModelName(rts, "Digital output2");
      ssSetPath(rts, "VFControl/Digital output2");
      ssSetRTModel(rts,VFControl_M);
      ssSetParentSS(rts, (NULL));
      ssSetRootSS(rts, rts);
      ssSetVersion(rts, SIMSTRUCT_VERSION_LEVEL2);

      /* parameters */
      {
        mxArray **sfcnParams = (mxArray **)
          &VFControl_M->NonInlinedSFcns.Sfcn1.params;
        ssSetSFcnParamsCount(rts, 6);
        ssSetSFcnParamsPtr(rts, &sfcnParams[0]);
        ssSetSFcnParam(rts, 0, (mxArray*)VFControl_cal->Digitaloutput2_P1_Size);
        ssSetSFcnParam(rts, 1, (mxArray*)VFControl_cal->Digitaloutput2_P2_Size);
        ssSetSFcnParam(rts, 2, (mxArray*)VFControl_cal->Digitaloutput2_P3_Size);
        ssSetSFcnParam(rts, 3, (mxArray*)VFControl_cal->Digitaloutput2_P4_Size);
        ssSetSFcnParam(rts, 4, (mxArray*)VFControl_cal->Digitaloutput2_P5_Size);
        ssSetSFcnParam(rts, 5, (mxArray*)VFControl_cal->Digitaloutput2_P6_Size);
      }

      /* work vectors */
      ssSetPWork(rts, (void **) &VFControl_DW.Digitaloutput2_PWORK[0]);

      {
        struct _ssDWorkRecord *dWorkRecord = (struct _ssDWorkRecord *)
          &VFControl_M->NonInlinedSFcns.Sfcn1.dWork;
        struct _ssDWorkAuxRecord *dWorkAuxRecord = (struct _ssDWorkAuxRecord *)
          &VFControl_M->NonInlinedSFcns.Sfcn1.dWorkAux;
        ssSetSFcnDWork(rts, dWorkRecord);
        ssSetSFcnDWorkAux(rts, dWorkAuxRecord);
        ssSetNumDWorkAsInt(rts, 1);

        /* PWORK */
        ssSetDWorkWidthAsInt(rts, 0, 2);
        ssSetDWorkDataType(rts, 0,SS_POINTER);
        ssSetDWorkComplexSignal(rts, 0, 0);
        ssSetDWork(rts, 0, &VFControl_DW.Digitaloutput2_PWORK[0]);
      }

      /* registration */
      sg_fpga_do_sf_a2(rts);
      sfcnInitializeSizes(rts);
      sfcnInitializeSampleTimes(rts);

      /* adjust sample time */
      ssSetSampleTime(rts, 0, 0.0001);
      ssSetOffsetTime(rts, 0, 0.0);
      sfcnTsMap[0] = 1;

      /* set compiled values of dynamic vector attributes */
      ssSetNumNonsampledZCsAsInt(rts, 0);

      /* Update connectivity flags for each port */
      _ssSetInputPortConnected(rts, 0, 1);
      _ssSetInputPortConnected(rts, 1, 1);
      _ssSetInputPortConnected(rts, 2, 1);

      /* Update the BufferDstPort flags for each input port */
      ssSetInputPortBufferDstPort(rts, 0, -1);
      ssSetInputPortBufferDstPort(rts, 1, -1);
      ssSetInputPortBufferDstPort(rts, 2, -1);
    }
  }

  /* Model Initialize function for ModelReference Block: '<Root>/Model' */
  PWMGenerator_initialize(rtmGetErrorStatusPointer(VFControl_M),
    &(VFControl_DW.Model_InstanceData.rtm),
    &(VFControl_DW.Model_InstanceData.rtb));

  /* Model Initialize function for ModelReference Block: '<Root>/Model1' */
  PWMGenerator_initialize(rtmGetErrorStatusPointer(VFControl_M),
    &(VFControl_DW.Model1_InstanceData.rtm),
    &(VFControl_DW.Model1_InstanceData.rtb));

  /* Model Initialize function for ModelReference Block: '<Root>/Model2' */
  PWMGenerator_initialize(rtmGetErrorStatusPointer(VFControl_M),
    &(VFControl_DW.Model2_InstanceData.rtm),
    &(VFControl_DW.Model2_InstanceData.rtb));

  /* Start for S-Function (sfun_spssw_discc): '<S96>/State-Space' incorporates:
   *  Constant: '<S39>/DC'
   *  Constant: '<S98>/SwitchCurrents'
   */

  /* S-Function block: <S96>/State-Space */
  {
    VFControl_DW.StateSpace_PWORK.DS = (real_T*)calloc(14 * 9, sizeof(real_T));
    VFControl_DW.StateSpace_PWORK.DX_COL = (real_T*)calloc(14, sizeof(real_T));
    VFControl_DW.StateSpace_PWORK.TMP2 = (real_T*)calloc(9, sizeof(real_T));
    VFControl_DW.StateSpace_PWORK.SWITCH_STATUS = (int_T*)calloc(6, sizeof(int_T));
    VFControl_DW.StateSpace_PWORK.SW_CHG = (int_T*)calloc(6, sizeof(int_T));
    VFControl_DW.StateSpace_PWORK.G_STATE = (int_T*)calloc(6, sizeof(int_T));
    VFControl_DW.StateSpace_PWORK.Y_SWITCH = (real_T*)calloc(6, sizeof(real_T));
    VFControl_DW.StateSpace_PWORK.SWITCH_TYPES = (int_T*)calloc(6, sizeof(int_T));
    VFControl_DW.StateSpace_PWORK.IDX_OUT_SW = (int_T*)calloc(6, sizeof(int_T));
    VFControl_DW.StateSpace_PWORK.SWITCH_STATUS_INIT = (int_T*)calloc(6, sizeof
      (int_T));
    VFControl_DW.StateSpace_PWORK.USWLAST = (real_T*)calloc(6, sizeof(real_T));
  }

  /* Start for TransportDelay: '<S3>/Transport Delay' */
  {
    real_T *pBuffer = &VFControl_DW.TransportDelay_RWORK.TUbufferArea[0];
    VFControl_DW.TransportDelay_IWORK.Tail = 0;
    VFControl_DW.TransportDelay_IWORK.Head = 0;
    VFControl_DW.TransportDelay_IWORK.Last = 0;
    VFControl_DW.TransportDelay_IWORK.CircularBufSize = 1024;
    pBuffer[0] = (VFControl_cal->TransportDelay_InitOutput);
    pBuffer[1024] = VFControl_M->Timing.t[0];
    VFControl_DW.TransportDelay_PWORK.TUbufferPtrs[0] = (void *) &pBuffer[0];
  }

  /* Start for TransportDelay: '<S4>/Transport Delay' */
  {
    real_T *pBuffer = &VFControl_DW.TransportDelay_RWORK_d.TUbufferArea[0];
    VFControl_DW.TransportDelay_IWORK_g.Tail = 0;
    VFControl_DW.TransportDelay_IWORK_g.Head = 0;
    VFControl_DW.TransportDelay_IWORK_g.Last = 0;
    VFControl_DW.TransportDelay_IWORK_g.CircularBufSize = 1024;
    pBuffer[0] = (VFControl_cal->TransportDelay_InitOutput_l);
    pBuffer[1024] = VFControl_M->Timing.t[0];
    VFControl_DW.TransportDelay_PWORK_m.TUbufferPtrs[0] = (void *) &pBuffer[0];
  }

  /* Start for TransportDelay: '<S5>/Transport Delay' */
  {
    real_T *pBuffer = &VFControl_DW.TransportDelay_RWORK_m.TUbufferArea[0];
    VFControl_DW.TransportDelay_IWORK_m.Tail = 0;
    VFControl_DW.TransportDelay_IWORK_m.Head = 0;
    VFControl_DW.TransportDelay_IWORK_m.Last = 0;
    VFControl_DW.TransportDelay_IWORK_m.CircularBufSize = 1024;
    pBuffer[0] = (VFControl_cal->TransportDelay_InitOutput_le);
    pBuffer[1024] = VFControl_M->Timing.t[0];
    VFControl_DW.TransportDelay_PWORK_f.TUbufferPtrs[0] = (void *) &pBuffer[0];
  }

  /* Start for S-Function (sg_fpga_do_sf_a2): '<Root>/Digital output1' */
  /* Level2 S-Function Block: '<Root>/Digital output1' (sg_fpga_do_sf_a2) */
  {
    SimStruct *rts = VFControl_M->childSfunctions[0];
    sfcnStart(rts);
    if (ssGetErrorStatus(rts) != (NULL))
      return;
  }

  /* Start for S-Function (sg_fpga_do_sf_a2): '<Root>/Digital output2' */
  /* Level2 S-Function Block: '<Root>/Digital output2' (sg_fpga_do_sf_a2) */
  {
    SimStruct *rts = VFControl_M->childSfunctions[1];
    sfcnStart(rts);
    if (ssGetErrorStatus(rts) != (NULL))
      return;
  }

  {
    int32_T i;

    /* InitializeConditions for UnitDelay: '<S23>/fluxes' */
    VFControl_DW.fluxes_DSTATE[0] = VFControl_cal->fluxes_InitialCondition[0];

    /* InitializeConditions for UnitDelay: '<S21>/fluxes' */
    VFControl_DW.fluxes_DSTATE_l[0] = VFControl_cal->fluxes_InitialCondition_a[0];

    /* InitializeConditions for UnitDelay: '<S23>/fluxes' */
    VFControl_DW.fluxes_DSTATE[1] = VFControl_cal->fluxes_InitialCondition[1];

    /* InitializeConditions for UnitDelay: '<S21>/fluxes' */
    VFControl_DW.fluxes_DSTATE_l[1] = VFControl_cal->fluxes_InitialCondition_a[1];

    /* InitializeConditions for UnitDelay: '<S23>/fluxes' */
    VFControl_DW.fluxes_DSTATE[2] = VFControl_cal->fluxes_InitialCondition[2];

    /* InitializeConditions for UnitDelay: '<S21>/fluxes' */
    VFControl_DW.fluxes_DSTATE_l[2] = VFControl_cal->fluxes_InitialCondition_a[2];

    /* InitializeConditions for UnitDelay: '<S23>/fluxes' */
    VFControl_DW.fluxes_DSTATE[3] = VFControl_cal->fluxes_InitialCondition[3];

    /* InitializeConditions for UnitDelay: '<S21>/fluxes' */
    VFControl_DW.fluxes_DSTATE_l[3] = VFControl_cal->fluxes_InitialCondition_a[3];

    /* InitializeConditions for DiscreteIntegrator: '<S15>/Rotor angle thetam' */
    VFControl_DW.Rotoranglethetam_DSTATE = VFControl_cal->Rotoranglethetam_IC;

    /* InitializeConditions for UnitDelay: '<S38>/wm_delay' */
    VFControl_DW.wm_delay_DSTATE = VFControl_cal->wm_delay_InitialCondition;

    /* InitializeConditions for UnitDelay: '<S38>/wm_predict' */
    VFControl_DW.wm_predict_DSTATE = VFControl_cal->wm_predict_InitialCondition;

    /* InitializeConditions for S-Function (sfun_spssw_discc): '<S96>/State-Space' incorporates:
     *  Constant: '<S39>/DC'
     *  Constant: '<S98>/SwitchCurrents'
     */
    {
      int32_T i, j;
      real_T *Ds = (real_T*)VFControl_DW.StateSpace_PWORK.DS;

      /* Copy and transpose D */
      for (i=0; i<14; i++) {
        for (j=0; j<9; j++)
          Ds[i*9 + j] = (VFControl_cal->StateSpace_DS_param[i + j*14]);
      }

      {
        /* Switches work vectors */
        int_T *switch_status = (int_T*)
          VFControl_DW.StateSpace_PWORK.SWITCH_STATUS;
        int_T *gState = (int_T*)VFControl_DW.StateSpace_PWORK.G_STATE;
        real_T *yswitch = (real_T*)VFControl_DW.StateSpace_PWORK.Y_SWITCH;
        int_T *switchTypes = (int_T*)VFControl_DW.StateSpace_PWORK.SWITCH_TYPES;
        int_T *idxOutSw = (int_T*)VFControl_DW.StateSpace_PWORK.IDX_OUT_SW;
        int_T *switch_status_init = (int_T*)
          VFControl_DW.StateSpace_PWORK.SWITCH_STATUS_INIT;

        /* Initialize work vectors */
        switch_status[0] = 0;
        switch_status_init[0] = 0;
        gState[0] = (int_T) 0.0;
        yswitch[0] = 1/0.001;
        switchTypes[0] = (int_T)7.0;
        idxOutSw[0] = ((int_T)0.0) - 1;
        switch_status[1] = 0;
        switch_status_init[1] = 0;
        gState[1] = (int_T) 0.0;
        yswitch[1] = 1/0.001;
        switchTypes[1] = (int_T)7.0;
        idxOutSw[1] = ((int_T)0.0) - 1;
        switch_status[2] = 0;
        switch_status_init[2] = 0;
        gState[2] = (int_T) 0.0;
        yswitch[2] = 1/0.001;
        switchTypes[2] = (int_T)7.0;
        idxOutSw[2] = ((int_T)0.0) - 1;
        switch_status[3] = 0;
        switch_status_init[3] = 0;
        gState[3] = (int_T) 0.0;
        yswitch[3] = 1/0.001;
        switchTypes[3] = (int_T)7.0;
        idxOutSw[3] = ((int_T)0.0) - 1;
        switch_status[4] = 0;
        switch_status_init[4] = 0;
        gState[4] = (int_T) 0.0;
        yswitch[4] = 1/0.001;
        switchTypes[4] = (int_T)7.0;
        idxOutSw[4] = ((int_T)0.0) - 1;
        switch_status[5] = 0;
        switch_status_init[5] = 0;
        gState[5] = (int_T) 0.0;
        yswitch[5] = 1/0.001;
        switchTypes[5] = (int_T)7.0;
        idxOutSw[5] = ((int_T)0.0) - 1;
      }
    }

    /* InitializeConditions for UnitDelay: '<S23>/voltages' */
    VFControl_DW.voltages_DSTATE[0] = VFControl_cal->voltages_InitialCondition;
    VFControl_DW.voltages_DSTATE[1] = VFControl_cal->voltages_InitialCondition;
    VFControl_DW.voltages_DSTATE[2] = VFControl_cal->voltages_InitialCondition;
    VFControl_DW.voltages_DSTATE[3] = VFControl_cal->voltages_InitialCondition;

    /* InitializeConditions for DiscreteIntegrator: '<S15>/Rotor speed(wm)' */
    VFControl_DW.Rotorspeedwm_DSTATE = VFControl_cal->Rotorspeedwm_IC;

    /* InitializeConditions for DiscreteIntegrator: '<S47>/Discrete-Time Integrator1' */
    VFControl_DW.DiscreteTimeIntegrator1_DSTATE =
      VFControl_cal->DiscreteTimeIntegrator1_IC;

    /* InitializeConditions for UnitDelay: '<S47>/Unit Delay1' */
    VFControl_DW.UnitDelay1_DSTATE = VFControl_cal->UnitDelay1_InitialCondition;

    /* InitializeConditions for DiscreteIntegrator: '<S42>/Discrete-Time Integrator' */
    VFControl_DW.DiscreteTimeIntegrator_DSTATE =
      VFControl_cal->DiscreteTimeIntegrator_IC;

    /* InitializeConditions for UnitDelay: '<S73>/Unit Delay' */
    VFControl_DW.UnitDelay_DSTATE = VFControl_cal->UnitDelay_InitialCondition;

    /* SystemInitialize for Enabled SubSystem: '<S16>/Saturation' */
    /* InitializeConditions for UnitDelay: '<S22>/Delay' */
    VFControl_DW.Delay_DSTATE = VFControl_cal->Delay_InitialCondition;

    /* SystemInitialize for Switch: '<S22>/Switch' incorporates:
     *  Outport: '<S22>/Lm'
     */
    VFControl_B.Switch_g = VFControl_cal->Lm_Y0;

    /* End of SystemInitialize for SubSystem: '<S16>/Saturation' */

    /* SystemInitialize for Enabled SubSystem: '<S17>/Rotor reference frame' */
    /* SystemInitialize for Fcn: '<S28>/vqr' incorporates:
     *  Outport: '<S28>/vqr,vdr'
     */
    VFControl_B.vqr_j = VFControl_cal->vqrvdr_Y0;

    /* SystemInitialize for Fcn: '<S28>/vdr' incorporates:
     *  Outport: '<S28>/vqr,vdr'
     */
    VFControl_B.vdr_l = VFControl_cal->vqrvdr_Y0;

    /* SystemInitialize for Fcn: '<S28>/vqs' incorporates:
     *  Outport: '<S28>/vqs,vds'
     */
    VFControl_B.vqs_l = VFControl_cal->vqsvds_Y0;

    /* SystemInitialize for Fcn: '<S28>/vds' incorporates:
     *  Outport: '<S28>/vqs,vds'
     */
    VFControl_B.vds_f = VFControl_cal->vqsvds_Y0;

    /* End of SystemInitialize for SubSystem: '<S17>/Rotor reference frame' */

    /* SystemInitialize for Enabled SubSystem: '<S17>/Stationary reference frame' */
    /* SystemInitialize for Fcn: '<S29>/vqr' incorporates:
     *  Outport: '<S29>/vqr,vdr'
     */
    VFControl_B.vqr_l = VFControl_cal->vqrvdr_Y0_a;

    /* SystemInitialize for Fcn: '<S29>/vdr' incorporates:
     *  Outport: '<S29>/vqr,vdr'
     */
    VFControl_B.vdr_p = VFControl_cal->vqrvdr_Y0_a;

    /* SystemInitialize for Fcn: '<S29>/vqs' incorporates:
     *  Outport: '<S29>/vqs,vds'
     */
    VFControl_B.vqs_i = VFControl_cal->vqsvds_Y0_m;

    /* SystemInitialize for Fcn: '<S29>/vds' incorporates:
     *  Outport: '<S29>/vqs,vds'
     */
    VFControl_B.vds_i = VFControl_cal->vqsvds_Y0_m;

    /* End of SystemInitialize for SubSystem: '<S17>/Stationary reference frame' */

    /* SystemInitialize for Enabled SubSystem: '<S17>/Synchronous reference frame' */
    /* SystemInitialize for Fcn: '<S30>/vqr' incorporates:
     *  Outport: '<S30>/vqr,vdr'
     */
    VFControl_B.vqr = VFControl_cal->vqrvdr_Y0_am;

    /* SystemInitialize for Fcn: '<S30>/vdr' incorporates:
     *  Outport: '<S30>/vqr,vdr'
     */
    VFControl_B.vdr = VFControl_cal->vqrvdr_Y0_am;

    /* SystemInitialize for Fcn: '<S30>/vqs' incorporates:
     *  Outport: '<S30>/vqs,vds'
     */
    VFControl_B.vqs = VFControl_cal->vqsvds_Y0_p;

    /* SystemInitialize for Fcn: '<S30>/vds' incorporates:
     *  Outport: '<S30>/vqs,vds'
     */
    VFControl_B.vds = VFControl_cal->vqsvds_Y0_p;

    /* End of SystemInitialize for SubSystem: '<S17>/Synchronous reference frame' */

    /* SystemInitialize for Enabled SubSystem: '<S18>/Rotor reference frame' */
    /* SystemInitialize for Fcn: '<S32>/ira' incorporates:
     *  Outport: '<S32>/ira,irb'
     */
    VFControl_B.ira_p = VFControl_cal->irairb_Y0;

    /* SystemInitialize for Fcn: '<S32>/irb' incorporates:
     *  Outport: '<S32>/ira,irb'
     */
    VFControl_B.irb_n = VFControl_cal->irairb_Y0;

    /* SystemInitialize for Fcn: '<S32>/isa' incorporates:
     *  Outport: '<S32>/isa,isb'
     */
    VFControl_B.isa_m = VFControl_cal->isaisb_Y0;

    /* SystemInitialize for Fcn: '<S32>/isb' incorporates:
     *  Outport: '<S32>/isa,isb'
     */
    VFControl_B.isb_p = VFControl_cal->isaisb_Y0;

    /* End of SystemInitialize for SubSystem: '<S18>/Rotor reference frame' */

    /* SystemInitialize for Enabled SubSystem: '<S18>/Stationary reference frame' */
    /* SystemInitialize for Fcn: '<S33>/ira' incorporates:
     *  Outport: '<S33>/ira,irb'
     */
    VFControl_B.ira_g = VFControl_cal->irairb_Y0_d;

    /* SystemInitialize for Fcn: '<S33>/irb' incorporates:
     *  Outport: '<S33>/ira,irb'
     */
    VFControl_B.irb_g = VFControl_cal->irairb_Y0_d;

    /* SystemInitialize for Fcn: '<S33>/isa' incorporates:
     *  Outport: '<S33>/isa,isb'
     */
    VFControl_B.isa_a = VFControl_cal->isaisb_Y0_k;

    /* SystemInitialize for Fcn: '<S33>/isb' incorporates:
     *  Outport: '<S33>/isa,isb'
     */
    VFControl_B.isb_f = VFControl_cal->isaisb_Y0_k;

    /* End of SystemInitialize for SubSystem: '<S18>/Stationary reference frame' */

    /* SystemInitialize for Enabled SubSystem: '<S18>/Synchronous reference frame' */
    /* SystemInitialize for Fcn: '<S34>/ira' incorporates:
     *  Outport: '<S34>/ira,irb'
     */
    VFControl_B.ira = VFControl_cal->irairb_Y0_h;

    /* SystemInitialize for Fcn: '<S34>/irb' incorporates:
     *  Outport: '<S34>/ira,irb'
     */
    VFControl_B.irb = VFControl_cal->irairb_Y0_h;

    /* SystemInitialize for Fcn: '<S34>/isa' incorporates:
     *  Outport: '<S34>/isa,isb'
     */
    VFControl_B.isa = VFControl_cal->isaisb_Y0_m;

    /* SystemInitialize for Fcn: '<S34>/isb' incorporates:
     *  Outport: '<S34>/isa,isb'
     */
    VFControl_B.isb = VFControl_cal->isaisb_Y0_m;

    /* End of SystemInitialize for SubSystem: '<S18>/Synchronous reference frame' */

    /* SystemInitialize for Enabled SubSystem: '<S19>/sin(beta),cos(beta),sin(th),cos(th)' */
    /* SystemInitialize for Trigonometry: '<S35>/Trigonometric Function1' incorporates:
     *  Outport: '<S35>/sin(beta),cos(beta), sin(th),cos(th)'
     */
    VFControl_B.TrigonometricFunction1_o1 =
      VFControl_cal->sinbetacosbetasinthcosth_Y0;

    /* SystemInitialize for Trigonometry: '<S35>/Trigonometric Function1' incorporates:
     *  Outport: '<S35>/sin(beta),cos(beta), sin(th),cos(th)'
     */
    VFControl_B.TrigonometricFunction1_o2 =
      VFControl_cal->sinbetacosbetasinthcosth_Y0;

    /* SystemInitialize for Trigonometry: '<S35>/Trigonometric Function' incorporates:
     *  Outport: '<S35>/sin(beta),cos(beta), sin(th),cos(th)'
     */
    VFControl_B.TrigonometricFunction_o1_dn =
      VFControl_cal->sinbetacosbetasinthcosth_Y0;

    /* SystemInitialize for Trigonometry: '<S35>/Trigonometric Function' incorporates:
     *  Outport: '<S35>/sin(beta),cos(beta), sin(th),cos(th)'
     */
    VFControl_B.TrigonometricFunction_o2_k =
      VFControl_cal->sinbetacosbetasinthcosth_Y0;

    /* End of SystemInitialize for SubSystem: '<S19>/sin(beta),cos(beta),sin(th),cos(th)' */

    /* SystemInitialize for Enabled SubSystem: '<S19>/sin(thr),cos(thr)' */
    /* SystemInitialize for Trigonometry: '<S36>/Trigonometric Function' incorporates:
     *  Outport: '<S36>/sin(thr),cos(thr)'
     */
    VFControl_B.TrigonometricFunction_o1_d = VFControl_cal->sinthrcosthr_Y0;

    /* SystemInitialize for Trigonometry: '<S36>/Trigonometric Function' incorporates:
     *  Outport: '<S36>/sin(thr),cos(thr)'
     */
    VFControl_B.TrigonometricFunction_o2_j = VFControl_cal->sinthrcosthr_Y0;

    /* SystemInitialize for Outport: '<S36>/sin(thr),cos(thr)' incorporates:
     *  Constant: '<S36>/Constant'
     */
    VFControl_B.Constant_e[0] = VFControl_cal->sinthrcosthr_Y0;
    VFControl_B.Constant_e[1] = VFControl_cal->sinthrcosthr_Y0;

    /* End of SystemInitialize for SubSystem: '<S19>/sin(thr),cos(thr)' */

    /* SystemInitialize for Enabled SubSystem: '<S19>/sin(thr),cos(thr)1' */
    /* SystemInitialize for Trigonometry: '<S37>/Trigonometric Function' incorporates:
     *  Outport: '<S37>/sin(thr),cos(thr)'
     */
    VFControl_B.TrigonometricFunction_o1 = VFControl_cal->sinthrcosthr_Y0_h;

    /* SystemInitialize for Trigonometry: '<S37>/Trigonometric Function' incorporates:
     *  Outport: '<S37>/sin(thr),cos(thr)'
     */
    VFControl_B.TrigonometricFunction_o2 = VFControl_cal->sinthrcosthr_Y0_h;

    /* SystemInitialize for Outport: '<S37>/sin(thr),cos(thr)' incorporates:
     *  Constant: '<S37>/Constant'
     */
    VFControl_B.Constant[0] = VFControl_cal->sinthrcosthr_Y0_h;
    VFControl_B.Constant[1] = VFControl_cal->sinthrcosthr_Y0_h;

    /* SystemInitialize for Enabled SubSystem: '<S19>/sin(thr),cos(thr)' */
    /* SystemInitialize for Enabled SubSystem: '<S19>/sin(beta),cos(beta),sin(th),cos(th)' */
    /* SystemInitialize for Enabled SubSystem: '<S16>/Saturation' */
    for (i = 0; i < 16; i++) {
      /* SystemInitialize for Product: '<S22>/inversion' incorporates:
       *  Outport: '<S22>/Linv'
       */
      VFControl_B.Linv_h[i] = VFControl_cal->Linv_Y0;

      /* SystemInitialize for Product: '<S22>/Product1' incorporates:
       *  Outport: '<S22>/R*Linv'
       */
      VFControl_B.RLinv_m[i] = VFControl_cal->RLinv_Y0;

      /* SystemInitialize for Assignment: '<S35>/W(4,3)=wr-1' incorporates:
       *  Outport: '<S35>/W'
       */
      VFControl_B.W43wr1[i] = VFControl_cal->W_Y0;

      /* SystemInitialize for Assignment: '<S36>/W(2,1)=-wr' incorporates:
       *  Outport: '<S36>/W'
       */
      VFControl_B.W21wr[i] = VFControl_cal->W_Y0_m;

      /* SystemInitialize for Assignment: '<S37>/W(4,3)=wr' incorporates:
       *  Outport: '<S37>/W'
       */
      VFControl_B.W43wr[i] = VFControl_cal->W_Y0_o;
    }

    /* End of SystemInitialize for SubSystem: '<S16>/Saturation' */
    /* End of SystemInitialize for SubSystem: '<S19>/sin(beta),cos(beta),sin(th),cos(th)' */
    /* End of SystemInitialize for SubSystem: '<S19>/sin(thr),cos(thr)' */
    /* End of SystemInitialize for SubSystem: '<S19>/sin(thr),cos(thr)1' */
  }

  /* Enable for DiscreteIntegrator: '<S15>/Rotor speed(wm)' */
  VFControl_DW.Rotorspeedwm_SYSTEM_ENABLE = 1U;
}

/* Model terminate function */
void VFControl_terminate(void)
{
  /* Terminate for S-Function (sfun_spssw_discc): '<S96>/State-Space' incorporates:
   *  Constant: '<S39>/DC'
   *  Constant: '<S98>/SwitchCurrents'
   */

  /* S-Function block: <S96>/State-Space */
  {
    /* Free memory */
    free(VFControl_DW.StateSpace_PWORK.DS);
    free(VFControl_DW.StateSpace_PWORK.DX_COL);
    free(VFControl_DW.StateSpace_PWORK.TMP2);

    /*
     * Circuit has switches*/
    free(VFControl_DW.StateSpace_PWORK.G_STATE);
    free(VFControl_DW.StateSpace_PWORK.SWITCH_STATUS);
    free(VFControl_DW.StateSpace_PWORK.SW_CHG);
    free(VFControl_DW.StateSpace_PWORK.SWITCH_STATUS_INIT);
  }

  /* Terminate for S-Function (sg_fpga_do_sf_a2): '<Root>/Digital output1' */
  /* Level2 S-Function Block: '<Root>/Digital output1' (sg_fpga_do_sf_a2) */
  {
    SimStruct *rts = VFControl_M->childSfunctions[0];
    sfcnTerminate(rts);
  }

  /* Terminate for S-Function (sg_fpga_do_sf_a2): '<Root>/Digital output2' */
  /* Level2 S-Function Block: '<Root>/Digital output2' (sg_fpga_do_sf_a2) */
  {
    SimStruct *rts = VFControl_M->childSfunctions[1];
    sfcnTerminate(rts);
  }

  /* user code (Terminate function Trailer) */
  {
    uintptr_t bar2Addr;
    volatile io3xx_pull *ptrIO31x_pull;
    volatile io3xx_2x *ptrio3xx_2x;
    uint16_t moduleArchitecture;
    sg_fpga_io3xxModuleIdT moduleId;
    static char msg[500];

    // Get module IDs (PIC info)
    sg_fpga_IO3xxGetModuleId(39750, &moduleId);
    moduleArchitecture = moduleId.moduleArchitecture;
    SG_PRINTF(DEBUG, "moduleArchitecture %d\n",moduleArchitecture);
    if (moduleArchitecture == TEWS_TXMC) {
      // Get pointer to io31x_pull
      bar2Addr = (uintptr_t)io3xxGetAddressSgLib((int32_t)1, SG_FPGA_IO3XX_BAR2);
      if (bar2Addr == 0) {
        sprintf(msg, "%s", pSgErrorStr);
        rtmSetErrorStatus(VFControl_M, msg);
        SG_PRINTF(ERROR,msg);
        return;
      }

      ptrIO31x_pull = (io3xx_pull *)((uintptr_t)bar2Addr + IO3xx_PULL_BASE);

      // Disable pull resistors
      ptrIO31x_pull->enable = 0x0;     // disable
    }

    // Pull down and disable DIOs
    if ((1 == 2) || (1 == 3)) {
      bar2Addr = (uintptr_t)io3xxGetAddressSgLib((int32_t)1, SG_FPGA_IO3XX_BAR2);
      if (bar2Addr == 0) {
        sprintf(msg, "%s", pSgErrorStr);
        rtmSetErrorStatus(VFControl_M, msg);
        SG_PRINTF(ERROR,msg);
        return;
      }

      ptrio3xx_2x = (io3xx_2x *)((uintptr_t)bar2Addr +IO3xx_2x_BASE);
      ptrio3xx_2x->pull = 0xffffffff;  // pull down
      ptrio3xx_2x->dir = 0x0;          // input
      ptrio3xx_2x->update = 0x1;
      sg_wait_s(SG_FPGA_WAIT_TIME_100us);
      ptrio3xx_2x->update = 0x0;
      sg_wait_s(SG_FPGA_WAIT_TIME_1ms);

#if DEBUGGING

      // For debugging output port register of IO-Expander
      sg_wait_s(SG_FPGA_WAIT_TIME_100ms);
      SG_PRINTF(INFO, "last configuration from mdl start\n");
      SG_PRINTF(INFO, "rxData of Expander1: 0x%X\n",
                ptrio3xx_2x->rxDataExpander1);
      SG_PRINTF(INFO, "rxData of Expander2: 0x%X\n",
                ptrio3xx_2x->rxDataExpander2);
      SG_PRINTF(INFO, "rxData of Expander3: 0x%X\n",
                ptrio3xx_2x->rxDataExpander3);
      SG_PRINTF(INFO, "rxData of Expander4: 0x%X\n",
                ptrio3xx_2x->rxDataExpander4);

#endif

    } else if (1 == 4) {
      IO3xx_24_terminate(1);
    }

    freeFPGAModuleSgLib((uint32_t)1);
  }
}
