/* (c) 2005 Simulation Research */
/* Caspoc 2005 Release */
/* Automated generated ANSI C-code */

/* Usage: */
/* Read the output voltage from your register into a variable, for example, voltage */
/* Include the global variable declarations and the functions */
/* The global variables have the same name as your node-labels */
/* The local variables are for controlling the Caspoc model */

/* Include the function call: CaspocInit(double t, double h) */
/* Call caspocInit() once during initialization */

/* Include the function call: caspocFunction(double t, double h) */
/* Call: caspocFunction() from your main loop */
/* Set the calculated dutycycle d in your register */

/* The Function void caspocUpdateOutputs(double t, double h) updates the outputs */
/* that are dependent on the outputs  form the integrators */
/* Call this function at the end of the transient analysis if required. */

  static double _dbLib_16; // Remark =  Block = INTLIMIT1\PILIM23
  static double _dbLib_19; // Remark =  Block = INTLIMIT1\PILIM22
  static double _dbLib_23; // Remark =  Block = INTLIMIT1\PILIM21
  static double _dbLib_13; // Remark =  Block = SIN3\DQRST1
  static double _dbLib_10; // Remark =  Block = SIN2\DQRST1
  static double _dbLib_3; // Remark =  Block = SIN1\DQRST1
  static double _dbLib_14; // Remark =  Block = COS3\DQRST1
  static double _dbLib_9; // Remark =  Block = COS2\DQRST1
  static double _dbLib_4; // Remark =  Block = COS1\DQRST1
// static double _dbLib_41; // Remark =  Block = I1\PILIM23
// static double _dbLib_42; // Remark =  Block = I1\PILIM22
// static double _dbLib_43; // Remark =  Block = I1\PILIM21
  static double _dbLib_26; // Remark =  Block = COS1\RSTDQ1
  static double _dbLib_27; // Remark =  Block = COS2\RSTDQ1
  static double _dbLib_28; // Remark =  Block = COS3\RSTDQ1
  static double _dbLib_37; // Remark =  Block = SIN1\RSTDQ1
  static double _dbLib_36; // Remark =  Block = SIN2\RSTDQ1
  static double _dbLib_35; // Remark =  Block = SIN3\RSTDQ1
  static double _dbLib_25; // Remark =  Block = V0\RSTDQ1
// static double _dbLib_24; // Remark =  Block = P1\PILIM21
// static double _dbLib_34; // Remark =  Block = MUL1\RSTDQ1
// static double _dbLib_33; // Remark =  Block = MUL2\RSTDQ1
// static double _dbLib_32; // Remark =  Block = MUL3\RSTDQ1
// static double _dbLib_29; // Remark =  Block = MUL4\RSTDQ1
// static double _dbLib_30; // Remark =  Block = MUL5\RSTDQ1
// static double _dbLib_31; // Remark =  Block = MUL6\RSTDQ1
// static double _dbLib_22; // Remark =  Block = PI2\PILIM21
  static double _dbLib_40; // Remark =  Block = MAX1\PILIM21
// static double _dbLib_20; // Remark =  Block = P1\PILIM22
  static double _dbLib_21; // Remark =  Block = PI\PILIM21
// static double _dbLib_18; // Remark =  Block = PI2\PILIM22
// static double _dbLib_17; // Remark =  Block = P1\PILIM23
  static double _dbLib_39; // Remark =  Block = MAX1\PILIM22
// static double _dbLib_15; // Remark =  Block = PI2\PILIM23
  static double _dbLib_2; // Remark =  Block = PI\PILIM22
// static double _dbLib_8; // Remark =  Block = MUL4\DQRST1
// static double _dbLib_11; // Remark =  Block = MUL3\DQRST1
// static double _dbLib_5; // Remark =  Block = MUL1\DQRST1
  static double _dbLib_38; // Remark =  Block = MAX1\PILIM23
  static double _dbLib_1; // Remark =  Block = PI\PILIM23
// static double _dbLib_12; // Remark =  Block = MUL6\DQRST1
// static double _dbLib_7; // Remark =  Block = MUL5\DQRST1
// static double _dbLib_6; // Remark =  Block = MUL2\DQRST1
/* Declaration Local Private Variables */

#include "DIRECTVECTORCONTROL.h"
#include <stdlib.h>
#include <math.h>
#include <float.h>
void caspocInitDIRECTVECTORCONTROL(double t, double h)
  {
  _dbLib_16=0;
  _dbLib_19=0;
  _dbLib_23=0;
  THETA=0;
  }

void caspocFunctionDIRECTVECTORCONTROL(double t, double h)
  {
// static double _dbLib_16(INTLIMIT        )Remark =  Block = INTLIMIT1\PILIM23
  // Limited integrator:_dbLib_16
// static double _dbLib_19(INTLIMIT        )Remark =  Block = INTLIMIT1\PILIM22
  // Limited integrator:_dbLib_19
// static double _dbLib_23(INTLIMIT        )Remark =  Block = INTLIMIT1\PILIM21
  // Limited integrator:_dbLib_23
  // Integrator modulus i2:THETA
// static double _dbLib_13(SIN             )Remark =  Block = SIN3\DQRST1
  _dbLib_13=sin(1*THETA+ 2.09439510000000E+0000);
// static double _dbLib_10(SIN             )Remark =  Block = SIN2\DQRST1
  _dbLib_10=sin(1*THETA+(-2.09439510000000E+0000));
// static double _dbLib_3(SIN             )Remark =  Block = SIN1\DQRST1
  _dbLib_3=sin(1*THETA+0);
// static double _dbLib_14(SIN             )Remark =  Block = COS3\DQRST1
  _dbLib_14=cos(1*THETA+ 2.09439510000000E+0000);
// static double _dbLib_9(SIN             )Remark =  Block = COS2\DQRST1
  _dbLib_9=cos(1*THETA+(-2.09439510000000E+0000));
// static double _dbLib_4(SIN             )Remark =  Block = COS1\DQRST1
  _dbLib_4=cos(1*THETA+0);
  E=WSOLL-WIST;
// static double _dbLib_26(SIN             )Remark =  Block = COS1\RSTDQ1
  _dbLib_26=cos(1*THETA+0);
// static double _dbLib_27(SIN             )Remark =  Block = COS2\RSTDQ1
  _dbLib_27=cos(1*THETA+(-2.09439510000000E+0000));
// static double _dbLib_28(SIN             )Remark =  Block = COS3\RSTDQ1
  _dbLib_28=cos(1*THETA+ 2.09439510000000E+0000);
// static double _dbLib_37(SIN             )Remark =  Block = SIN1\RSTDQ1
  _dbLib_37=sin(1*THETA+0);
// static double _dbLib_36(SIN             )Remark =  Block = SIN2\RSTDQ1
  _dbLib_36=sin(1*THETA+(-2.09439510000000E+0000));
// static double _dbLib_35(SIN             )Remark =  Block = SIN3\RSTDQ1
  _dbLib_35=sin(1*THETA+ 2.09439510000000E+0000);
// static double _dbLib_25(SUM             )Remark =  Block = V0\RSTDQ1
  _dbLib_25= 3.33333333333333E-0001*WA_I1+ 3.33333333333333E-0001*WA_I2+ 3.33333333333333E-0001*WA_I3;
// extern double IDIST(SUM             )Remark =  Block = D\RSTDQ1
  IDIST= 6.66666666666667E-0001*(_dbLib_37*WA_I1)+ 6.66666666666667E-0001*(_dbLib_36*WA_I2)+ 6.66666666666667E-0001*(_dbLib_35*WA_I3);
// extern double IQIST(SUM             )Remark =  Block = Q\RSTDQ1
  IQIST= 6.66666666666667E-0001*(_dbLib_26*WA_I1)+ 6.66666666666667E-0001*(_dbLib_27*WA_I2)+ 6.66666666666667E-0001*(_dbLib_28*WA_I3);
// static double _dbLib_40(MAX             )Remark =  Block = MAX1\PILIM21
  if(0<=0)  {  if(((_dbLib_23*W_I)+(E*W_P))>ZPI_MIN)    {_dbLib_40=((_dbLib_23*W_I)+(E*W_P));}  else    {_dbLib_40=ZPI_MIN;}  }else  {  if( (0>=((_dbLib_23*W_I)+(E*W_P))) && (0>=ZPI_MIN) )    {_dbLib_40=0;}  else    {    if( (((_dbLib_23*W_I)+(E*W_P))>=0) && (((_dbLib_23*W_I)+(E*W_P))>=ZPI_MIN) )      {_dbLib_40=((_dbLib_23*W_I)+(E*W_P));}    else      {_dbLib_40=ZPI_MIN;}    }  }
  IDS=0-IDIST;
// static double _dbLib_21(MIN             )Remark =  Block = PI\PILIM21
  if(0<=0)  {  if(_dbLib_40<ZPI_MAX)    {_dbLib_21=_dbLib_40;}  else    {_dbLib_21=ZPI_MAX;}  }else  {  if( (0<=_dbLib_40) && (0<=ZPI_MAX) )    {_dbLib_21=0;}  else    {    if( (_dbLib_40<=0) && (_dbLib_40<=ZPI_MAX) )      {_dbLib_21=_dbLib_40;}    else      {_dbLib_21=ZPI_MAX;}    }  }
  IQS=_dbLib_21-IQIST;
// static double _dbLib_39(MAX             )Remark =  Block = MAX1\PILIM22
  if(0<=0)  {  if(((_dbLib_19*D_I)+(IDS*D_P))>ZPI_MIN)    {_dbLib_39=((_dbLib_19*D_I)+(IDS*D_P));}  else    {_dbLib_39=ZPI_MIN;}  }else  {  if( (0>=((_dbLib_19*D_I)+(IDS*D_P))) && (0>=ZPI_MIN) )    {_dbLib_39=0;}  else    {    if( (((_dbLib_19*D_I)+(IDS*D_P))>=0) && (((_dbLib_19*D_I)+(IDS*D_P))>=ZPI_MIN) )      {_dbLib_39=((_dbLib_19*D_I)+(IDS*D_P));}    else      {_dbLib_39=ZPI_MIN;}    }  }
// static double _dbLib_2(MIN             )Remark =  Block = PI\PILIM22
  if(0<=0)  {  if(_dbLib_39<ZPI_MAX)    {_dbLib_2=_dbLib_39;}  else    {_dbLib_2=ZPI_MAX;}  }else  {  if( (0<=_dbLib_39) && (0<=ZPI_MAX) )    {_dbLib_2=0;}  else    {    if( (_dbLib_39<=0) && (_dbLib_39<=ZPI_MAX) )      {_dbLib_2=_dbLib_39;}    else      {_dbLib_2=ZPI_MAX;}    }  }
// static double _dbLib_38(MAX             )Remark =  Block = MAX1\PILIM23
  if(0<=0)  {  if(((_dbLib_16*Q_I)+(IQS*Q_P))>ZPI_MIN)    {_dbLib_38=((_dbLib_16*Q_I)+(IQS*Q_P));}  else    {_dbLib_38=ZPI_MIN;}  }else  {  if( (0>=((_dbLib_16*Q_I)+(IQS*Q_P))) && (0>=ZPI_MIN) )    {_dbLib_38=0;}  else    {    if( (((_dbLib_16*Q_I)+(IQS*Q_P))>=0) && (((_dbLib_16*Q_I)+(IQS*Q_P))>=ZPI_MIN) )      {_dbLib_38=((_dbLib_16*Q_I)+(IQS*Q_P));}    else      {_dbLib_38=ZPI_MIN;}    }  }
// static double _dbLib_1(MIN             )Remark =  Block = PI\PILIM23
  if(0<=0)  {  if(_dbLib_38<ZPI_MAX)    {_dbLib_1=_dbLib_38;}  else    {_dbLib_1=ZPI_MAX;}  }else  {  if( (0<=_dbLib_38) && (0<=ZPI_MAX) )    {_dbLib_1=0;}  else    {    if( (_dbLib_38<=0) && (_dbLib_38<=ZPI_MAX) )      {_dbLib_1=_dbLib_38;}    else      {_dbLib_1=ZPI_MAX;}    }  }
// extern double VTSOLL(SUM             )Remark =  Block = T\DQRST1
  VTSOLL=1*(_dbLib_13*_dbLib_2)+1*(_dbLib_14*_dbLib_1)+1*0;
// extern double VSSOLL(SUM             )Remark =  Block = S\DQRST1
  VSSOLL=1*(_dbLib_10*_dbLib_2)+1*(_dbLib_9*_dbLib_1)+1*0;
// extern double VRSOLL(SUM             )Remark =  Block = R\DQRST1
  VRSOLL=1*(_dbLib_3*_dbLib_2)+1*(_dbLib_4*_dbLib_1)+1*0;

// integrators
  _dbLib_16=_dbLib_16+IQS*(double)h; //  yn+1=yn+h*input+O(h^2)
  if(_dbLib_16<ZPI_MIN)_dbLib_16=ZPI_MIN;
  if(_dbLib_16>ZPI_MAX)_dbLib_16=ZPI_MAX;
  _dbLib_19=_dbLib_19+IDS*(double)h; //  yn+1=yn+h*input+O(h^2)
  if(_dbLib_19<ZPI_MIN)_dbLib_19=ZPI_MIN;
  if(_dbLib_19>ZPI_MAX)_dbLib_19=ZPI_MAX;
  _dbLib_23=_dbLib_23+E*(double)h; //  yn+1=yn+h*input+O(h^2)
  if(_dbLib_23<ZPI_MIN)_dbLib_23=ZPI_MIN;
  if(_dbLib_23>ZPI_MAX)_dbLib_23=ZPI_MAX;
  THETA=THETA+WIST*(double)h; //  yn+1=yn+h*input+O(h^2)
  if(THETA>(2*pi))THETA=THETA-((2*pi))*floor(fabs(THETA/(2*pi)));
  if(THETA<0)THETA=THETA+((2*pi))*(1+floor(fabs(THETA/(2*pi))));
  }

//Final results
void caspocUpdateOutputsDIRECTVECTORCONTROL(double t, double h)
  {
  _dbLib_16=_dbLib_16;
  _dbLib_19=_dbLib_19;
  _dbLib_23=_dbLib_23;
  THETA=THETA;
// static double _dbLib_16(INTLIMIT        )Remark =  Block = INTLIMIT1\PILIM23
  // Limited integrator:_dbLib_16
// static double _dbLib_19(INTLIMIT        )Remark =  Block = INTLIMIT1\PILIM22
  // Limited integrator:_dbLib_19
// static double _dbLib_23(INTLIMIT        )Remark =  Block = INTLIMIT1\PILIM21
  // Limited integrator:_dbLib_23
  // Integrator modulus i2:THETA
// static double _dbLib_13(SIN             )Remark =  Block = SIN3\DQRST1
  _dbLib_13=sin(1*THETA+ 2.09439510000000E+0000);
// static double _dbLib_10(SIN             )Remark =  Block = SIN2\DQRST1
  _dbLib_10=sin(1*THETA+(-2.09439510000000E+0000));
// static double _dbLib_3(SIN             )Remark =  Block = SIN1\DQRST1
  _dbLib_3=sin(1*THETA+0);
// static double _dbLib_14(SIN             )Remark =  Block = COS3\DQRST1
  _dbLib_14=cos(1*THETA+ 2.09439510000000E+0000);
// static double _dbLib_9(SIN             )Remark =  Block = COS2\DQRST1
  _dbLib_9=cos(1*THETA+(-2.09439510000000E+0000));
// static double _dbLib_4(SIN             )Remark =  Block = COS1\DQRST1
  _dbLib_4=cos(1*THETA+0);
  E=WSOLL-WIST;
// static double _dbLib_26(SIN             )Remark =  Block = COS1\RSTDQ1
  _dbLib_26=cos(1*THETA+0);
// static double _dbLib_27(SIN             )Remark =  Block = COS2\RSTDQ1
  _dbLib_27=cos(1*THETA+(-2.09439510000000E+0000));
// static double _dbLib_28(SIN             )Remark =  Block = COS3\RSTDQ1
  _dbLib_28=cos(1*THETA+ 2.09439510000000E+0000);
// static double _dbLib_37(SIN             )Remark =  Block = SIN1\RSTDQ1
  _dbLib_37=sin(1*THETA+0);
// static double _dbLib_36(SIN             )Remark =  Block = SIN2\RSTDQ1
  _dbLib_36=sin(1*THETA+(-2.09439510000000E+0000));
// static double _dbLib_35(SIN             )Remark =  Block = SIN3\RSTDQ1
  _dbLib_35=sin(1*THETA+ 2.09439510000000E+0000);
// static double _dbLib_25(SUM             )Remark =  Block = V0\RSTDQ1
  _dbLib_25= 3.33333333333333E-0001*WA_I1+ 3.33333333333333E-0001*WA_I2+ 3.33333333333333E-0001*WA_I3;
// extern double IDIST(SUM             )Remark =  Block = D\RSTDQ1
  IDIST= 6.66666666666667E-0001*(_dbLib_37*WA_I1)+ 6.66666666666667E-0001*(_dbLib_36*WA_I2)+ 6.66666666666667E-0001*(_dbLib_35*WA_I3);
// extern double IQIST(SUM             )Remark =  Block = Q\RSTDQ1
  IQIST= 6.66666666666667E-0001*(_dbLib_26*WA_I1)+ 6.66666666666667E-0001*(_dbLib_27*WA_I2)+ 6.66666666666667E-0001*(_dbLib_28*WA_I3);
// static double _dbLib_40(MAX             )Remark =  Block = MAX1\PILIM21
  if(0<=0)  {  if(((_dbLib_23*W_I)+(E*W_P))>ZPI_MIN)    {_dbLib_40=((_dbLib_23*W_I)+(E*W_P));}  else    {_dbLib_40=ZPI_MIN;}  }else  {  if( (0>=((_dbLib_23*W_I)+(E*W_P))) && (0>=ZPI_MIN) )    {_dbLib_40=0;}  else    {    if( (((_dbLib_23*W_I)+(E*W_P))>=0) && (((_dbLib_23*W_I)+(E*W_P))>=ZPI_MIN) )      {_dbLib_40=((_dbLib_23*W_I)+(E*W_P));}    else      {_dbLib_40=ZPI_MIN;}    }  }
  IDS=0-IDIST;
// static double _dbLib_21(MIN             )Remark =  Block = PI\PILIM21
  if(0<=0)  {  if(_dbLib_40<ZPI_MAX)    {_dbLib_21=_dbLib_40;}  else    {_dbLib_21=ZPI_MAX;}  }else  {  if( (0<=_dbLib_40) && (0<=ZPI_MAX) )    {_dbLib_21=0;}  else    {    if( (_dbLib_40<=0) && (_dbLib_40<=ZPI_MAX) )      {_dbLib_21=_dbLib_40;}    else      {_dbLib_21=ZPI_MAX;}    }  }
  IQS=_dbLib_21-IQIST;
// static double _dbLib_39(MAX             )Remark =  Block = MAX1\PILIM22
  if(0<=0)  {  if(((_dbLib_19*D_I)+(IDS*D_P))>ZPI_MIN)    {_dbLib_39=((_dbLib_19*D_I)+(IDS*D_P));}  else    {_dbLib_39=ZPI_MIN;}  }else  {  if( (0>=((_dbLib_19*D_I)+(IDS*D_P))) && (0>=ZPI_MIN) )    {_dbLib_39=0;}  else    {    if( (((_dbLib_19*D_I)+(IDS*D_P))>=0) && (((_dbLib_19*D_I)+(IDS*D_P))>=ZPI_MIN) )      {_dbLib_39=((_dbLib_19*D_I)+(IDS*D_P));}    else      {_dbLib_39=ZPI_MIN;}    }  }
// static double _dbLib_2(MIN             )Remark =  Block = PI\PILIM22
  if(0<=0)  {  if(_dbLib_39<ZPI_MAX)    {_dbLib_2=_dbLib_39;}  else    {_dbLib_2=ZPI_MAX;}  }else  {  if( (0<=_dbLib_39) && (0<=ZPI_MAX) )    {_dbLib_2=0;}  else    {    if( (_dbLib_39<=0) && (_dbLib_39<=ZPI_MAX) )      {_dbLib_2=_dbLib_39;}    else      {_dbLib_2=ZPI_MAX;}    }  }
// static double _dbLib_38(MAX             )Remark =  Block = MAX1\PILIM23
  if(0<=0)  {  if(((_dbLib_16*Q_I)+(IQS*Q_P))>ZPI_MIN)    {_dbLib_38=((_dbLib_16*Q_I)+(IQS*Q_P));}  else    {_dbLib_38=ZPI_MIN;}  }else  {  if( (0>=((_dbLib_16*Q_I)+(IQS*Q_P))) && (0>=ZPI_MIN) )    {_dbLib_38=0;}  else    {    if( (((_dbLib_16*Q_I)+(IQS*Q_P))>=0) && (((_dbLib_16*Q_I)+(IQS*Q_P))>=ZPI_MIN) )      {_dbLib_38=((_dbLib_16*Q_I)+(IQS*Q_P));}    else      {_dbLib_38=ZPI_MIN;}    }  }
// static double _dbLib_1(MIN             )Remark =  Block = PI\PILIM23
  if(0<=0)  {  if(_dbLib_38<ZPI_MAX)    {_dbLib_1=_dbLib_38;}  else    {_dbLib_1=ZPI_MAX;}  }else  {  if( (0<=_dbLib_38) && (0<=ZPI_MAX) )    {_dbLib_1=0;}  else    {    if( (_dbLib_38<=0) && (_dbLib_38<=ZPI_MAX) )      {_dbLib_1=_dbLib_38;}    else      {_dbLib_1=ZPI_MAX;}    }  }
// extern double VTSOLL(SUM             )Remark =  Block = T\DQRST1
  VTSOLL=1*(_dbLib_13*_dbLib_2)+1*(_dbLib_14*_dbLib_1)+1*0;
// extern double VSSOLL(SUM             )Remark =  Block = S\DQRST1
  VSSOLL=1*(_dbLib_10*_dbLib_2)+1*(_dbLib_9*_dbLib_1)+1*0;
// extern double VRSOLL(SUM             )Remark =  Block = R\DQRST1
  VRSOLL=1*(_dbLib_3*_dbLib_2)+1*(_dbLib_4*_dbLib_1)+1*0;
  }
