⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 backvalve_.c

📁 amesim和matlab simulink联合仿真设置方法及多个精彩例程
💻 C
📖 第 1 页 / 共 5 页
字号:

extern int GetVarIndex(double *var)
{
   return GetIndex(v, NUMVARS, var);
}



/*******************************************************************
*                                                                  *
*  finite is a replacement for the finite function that used to    *
*  exist on HPUX and all other machines.  HP seems to have removed *
*  it in HPUX11 replacing it with a macro isfinite.                *
*  This function should make it possible to use the same libAME    *
*  on both HP10 and HP11.                                          *
*                                                                  *
*******************************************************************/

#ifdef HPUX

#ifdef isfinite

extern int finite
#ifdef _NO_PROTO
(value)
double value;
#else
(double value)
#endif
{
   return isfinite(value);
}

#endif
#endif


/*******************************************************************
*                                                                  *
*  IsFixed returns True if the variable specified by the varnum is *
*  fixed.                                                          *
*                                                                  *
*******************************************************************/

extern int IsFixed
#ifdef _NO_PROTO
   (var_num)

   int var_num;
#else
   (int var_num)
#endif
{
   static int num_fixed = 4;
   static int FIXED[4] = {13, 6, 18, 27};
   int i;

   for (i=0; i<num_fixed; ++i)
   {
      if (FIXED[i] == var_num)
      {
         return 1;
      }
   }
   return 0;
}


/*******************************************************************
*                                                                  *
*  firstc_ return True if its the first call to FunctionEval  else *
*  it returns False.                                               *
*                                                                  *
*******************************************************************/

extern int firstc_()
{
   return first_call;
}

/*******************************************************************
*                                                                  *
*  getfinaltime_ returns the final time of the simulation.         *
*                                                                  *
*******************************************************************/

extern double getfinaltime_()
{
   /* For Simulink interfaces we dont know when to stop
    * so say that we should stop "soon" */

   return FinalTime+0.2; 
}


/********************************************************************
 *                                                                  *
 *  PrintStateTitle reads the                                       *
                              backvalve_.state
 *                                                                  *
 *  file attempting to find a particular entry. If this is found, it*
 *  is printed.                                                     *
 *                                                                  *
 *  Input parameter:                                                *
 *                                                                  *
 *  int num      position of entry in the file                      *
 *                                                                  *
 ********************************************************************/
extern void PrintStateTitle
#ifdef _NO_PROTO
   (fptr, num)

   FILE *fptr;
   int num;
#else
(FILE *fptr, int num)
#endif

{
#ifndef NOEXTERNALFILES

   FILE *fp;
   char buf[FILELINE_MAX+1];
   int i;

   if(num < 1 || num > NUMSTATES)
   {
      ameprintf( "Call to PrintStateTitle ignored.\n");
      ameprintf(
              "State number %d should be in the range 1 to %d\n",
              num, NUMSTATES);
      return;
   }
   fp = fopen(GetStateFileName(), "r");

   if(fp == NULL)
   {
      ameprintf( "Can't open file %s\n", GetStateFileName());
      return;
   }
   i = 0;

   while(fgets(buf, FILELINE_MAX, fp) != NULL)
   {
      i++;
      
      if(i == num)
      {
         fprintf(fptr, " %s", buf);
         break;
      }
   }
   fclose(fp);
#endif
}

/********************************************************************
 *                                                                  *
 *  famexit is the function that Fortran submodels and utilities    *
 *  should call instead of exit                                     *
 *                                                                  *
 ********************************************************************/

void famexit_
#ifdef _NO_PROTO
(status)
int *status;
#else
(int *status)
#endif
{
   AmeExit(*status);
}

/*******************************************************************
*                                                                  *
* Subroutine GetOldValues attempts to read an old file             *
*                                                                  *
*                      backvalve_.results                                *
*                                                                  *
* and reset state variables. If runtype=1, it will also attempt to *
* reset points and position for a  continuation run.               *
*                                                                  *
* 22/11/02 STC numvars is always compared with NumToSave (even     *
*              when it is > 0). We also check that the variables   *
*              to save are the same as in the previous run.        *
*                                                                  *
*******************************************************************/
static void GetOldValues
#ifdef _NO_PROTO
   (t, y, successful)

   double *t, *y;
   int *successful;
#else
   (double *t, double *y, int *successful)
#endif
{
#ifndef NORESULTFILE
   /* Local variables. */

   int isize=sizeof(int), dsize = sizeof(double);
   long offset;
   int oldpts, numvar;

   int i, not_changed;
   int *saved_vars;
   char *resultsname;


   saved_vars = NULL;
   not_changed = 1;
   resultsname = GetResultsFileName();
   *successful = 0;

   if(access(resultsname, R_OK) != 0)
   {
      /* File does not exist or exists but cannot be read. */

      return;
   }
#ifdef WIN32
   fd_results = open(resultsname, O_RDWR|O_BINARY);
#else
   fd_results = open(resultsname, O_RDWR);
#endif

   if(fd_results == -1)
   {
      return;
   }

   if((read(fd_results, (char *)(&oldpts), isize) != isize) ||
      (read(fd_results, (char *)(&numvar), isize) != isize) ||
      oldpts < 1 || 
      (numvar > 0 && numvar != NumToSave) ||
      (numvar < 0 && numvar != -NumToSave))
   {
      /* If the no. of variable whose values are being saved in *
       * results has been changed, it is impossible to perform  *
       * continuation run without resizing the file header and  *
       * moving data positions. Therefore, for the time being,  *
       * abort execution.                                       */
      
      ameprintf( "\nNumber of variables to be saved has changed,");
      if (isconrun_())
      {
         ameprintf( "\ncannot perform continuation run.\n");
      }
      else
      {
         ameprintf( "\ncannot use old final values.\n");
      }
      /* Close file and exit. */
      
      close(fd_results);
      fd_results = -1;
      AmeExit(1);
   }
   else if (numvar < 0)
   {
      /* Case where:
         (i)  Not all of the variables are saved. 
         (ii) The number of variables to save has not changed.
         The problem here, is that these variables may not 
         be the same as in the previous run. */
      
      saved_vars = (int *)calloc(NumToSave, isize);
      
      if(read(fd_results, (char *)(saved_vars), NumToSave*isize) != NumToSave*isize)
      {
         /* Error while reading the results file. */
         
         close(fd_results);
         fd_results = -1;
         return;
      }
      
      for(i = 0; i < NumToSave; i++)
      {
         if (SaveFlags[saved_vars[i]] == 0)
         {
            not_changed = 0;
            break;
         }
      }

      free(saved_vars);

      if (not_changed == 0)
      {
         ameprintf( "\nVariables to be saved are not the same as in the");
         if (isconrun_())
         {
            ameprintf( "\nprevious run: cannot perform continuation run.\n");
         }
         else
         {
            ameprintf( "\nprevious run: cannot use old final values.\n");
         }       
         close(fd_results);
         fd_results = -1;
         AmeExit(1);
      }
   }

   if(numvar < 0)
   {
      /* Not all values have been saved in results file so read *
       * Read from the end of the last recorded data value.     */

      numvar = -numvar;

      offset = (long)(oldpts*(numvar+1)*dsize+(numvar+2)*isize);
   }
   else
   {
      offset = (long)((oldpts-1)*(NUMVARS+1)*dsize+2*isize);
   }

   lseek(fd_results, offset, 0);
   if (isconrun_())
   {
      /* Continuation run, we need the time  */

      if(read(fd_results, (char *)t, dsize) != dsize)
      {
         /* Close file and return with successful false. */

         close(fd_results);
         fd_results=-1;
         return;
      }
   }
   else
   {
      double temp;

      /* Not a continuation run,
         we do not need the time variable read it into a dummy variable */

      if(read(fd_results, (char *)&temp, dsize) != dsize)
      {
         /* Close file and return with successful false. */

         close(fd_results);
         fd_results=-1;
         return;
      }            
   }

   if(read(fd_results, (char *)v, NUMVARS*dsize) != NUMVARS*dsize)
   {
      /* Close file and return with successful false. */

      close(fd_results);
      fd_results=-1;
      return;
   }

   ChangeState(&y[0], v[2]);
   ChangeState(&y[2], v[24]);
   ChangeState(&y[5], v[35]);
   ChangeState(&y[1], v[20]);
   ChangeState(&y[3], v[29]);
   ChangeState(&y[6], v[38]);
   ChangeState(&y[4], v[32]);

   if(isconrun_())
   {
      /* Its a continuation run so must set points and position. */

      points = oldpts;
      position = points*(numvar+1);
      if(ASOPT.outoff)
      {
         close(fd_results);
         fd_results=-1;
      }
   }
   else
   {
      close(fd_results);
      fd_results=-1;
   }
   /* Return with successful true. */

   *successful = 1;
#else
   *successful = 0;
   fd_results=-1;   
#endif
}

/**********************************************************************
*                                                                     *
*     InitSaveFlags - this function initialises the value of the      *
*                     output flags for each of the variables.         *
*                                                                     *
**********************************************************************/

static int InitSaveFlags()
{
#ifndef NORESULTFILE
   FILE *fp;
   char buf[FILELINE_MAX+1];
   int i=0;
   int n=0;

#ifdef NOEXTERNALFILES
#include "backvalve_.ssf.h"
   if(savestatusflags_length == NUMVARS)
   {
      for(i = 0; i < NUMVARS; i++)
      {
         if( (sscanf(savestatusflags[i],"%d", &SaveFlags[i]) == 1) && (SaveFlags[i] != 0))
         {
            n++;
         }
      }
   }
#else
   if((fp = fopen(GetSaveFlagsFileName(), "r")) != NULL)
   {
      i = n = 0;

      while(fgets(buf, FILELINE_MAX, fp) != NULL)
      {
         if((sscanf(buf, "%d", &SaveFlags[i]) == 1) && (SaveFlags[i] != 0))
         {
            n++;
         }

         i++;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -