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

📄 backvalve_.c

📁 amesim和matlab simulink联合仿真设置方法及多个精彩例程
💻 C
📖 第 1 页 / 共 5 页
字号:
/* This is (almost) a -*-c-*- file  */


#ifndef lint
static char vcid[] = "$Id: simulink.etemp,v 4.19 2004-08-06 13:49:56+02 arne Exp $";
#endif /* lint */

/******************************************************************* 
*                                                                  *
*                                                                  *
*       AMESim C code for mex file written by code generator.      *
*     Major rewrite for 4.2 - Level2 S-function
*                             tested with xpc
*                             multiple ports
*                             etc.
*
                          backvalve_

*                                                                  *
* Template version: 4.2                                            *
* Version history:                                                 *
*
* $Log: simulink.etemp,v $
* Revision 4.19  2004-08-06 13:49:56+02  arne
* Corrected a crash in ameSetupPortNames when we have many labels.
* Added a macro AMEssPrintf that is defined depending on the RT platform (or
* just Simulink) we are using.
*
* Revision 4.18  2004-06-11 14:56:20+02  arne
* Added some init of first_call
*
* Revision 4.17  2004-05-07 17:09:27+02  arne
* Add the portnames when the simulation has started. It is not as
* beautiful, but it works.
*
* Revision 4.16  2004-05-04 09:02:54+02  arne
* Add checks to ensure that the parameter values/titles sent as argument to the
* S-function have the same size.
*
* Revision 4.15  2004-04-30 18:01:26+02  arne
* Install a "clear mex" callback at beginning and end of simulation.
* This should get rid of most complaints about that people are unable to
* run several simulations in a row.
*
* Revision 4.14  2004-04-30 17:39:55+02  arne
* Converted to Level2 S-function and many real-time related changes.
* Also made the S-function be a multiple port with labels on each port
* that is set with the corresponding AMESim variable name.
*
* Revision 4.13  2004-03-11 17:41:19+01  olivier
* Removed the functions dealing with filenames.
*
* Revision 4.12  2004/02/27 12:57:48  olivier
* Corrected the bug 107973 concerning exit operations before
* closing nicely the results file.
*
* Revision 4.11  2003/11/13 14:15:13  sclement
* Modifications for 404258: Function ameSimulinkfprintf is added and
* ameInstallFprintf is called.
*
* Revision 4.10  2003/03/10 17:02:59  arne
* Misc changes in templates. activityindex and GetFlagAddresses.
*
* Revision 4.9  2003-02-26 12:59:57+01  gauthier
* declaration of PrintStateTitle was not OK with _NO_PROTO defined.
*
* Revision 4.8  2003/01/15 12:52:53  olivier
* Introduced the IsFixed function for linking with libAME. It is
* called only during the linearization process for building B and D
* matrices.
*
* Revision 4.7  2003/01/07 14:10:00  sclement
* Error message when disk full (BUG REF 100714).
*
* Revision 4.6  2002/12/05 08:45:33  arne
* Major revision for AMESim 4.1.
* -The ZeroCrossing code using mdlZeroCrossings is now used - don't use
*  the "Hit Crossing" block.
* -Moved the discontinuity updates to mdlOutputs.
* -Try to use the Simulink solvers more efficiently.
* -Now compatible with RealTime Workshop.
* -Other changes for 4.1 - runtype no longer used etc.
*
* Revision 4.5  2002-11-07 09:43:46+01  sclement
* The .oil file is not used anymore.
*
* Revision 4.4  2002/01/07 13:27:41  arne
* Some cleanup of zero crossing and discontinuity code.
* ssSetNumDiscStates seems to be needed.
*
* Revision 4.3  2001-12-13 09:24:49+01  arne
* Modififications to allow use of fixed step solvers in Simulink.
* THERE REMAINS SOME QUESTIONS HERE ABOUT ssSetNumDiscStates in
* mdlInitializeSizes.  Anoother remaining question is the
* Zero crossing stuff.
*
* Revision 4.2  2001-10-12 13:37:05+02  olivier
* Added the function GetCustomGlobalParamFileName.
*
* Revision 4.1  2001/10/02 13:01:36  arne
* Functions WriteErrorCount, SummarizeErrors added (from v3.5)
* SetIsPrinting and ClearIsPrinting calls added (also from v3.5).
* Various changes to make this template more the same as the etemplate.
* For instance, static functions, the char* variables for filenames.
* Added function GetGlobalParamFileName -needed.
*
* Revision 4.0  2000-11-23 14:48:57+01  ameman
* Start of v4
*
* Revision 1.8  2001-05-02 15:16:57+02  arne
* Added two dummy functions WriteErrorCount and SummarizeErrors that
* enables you to add the RSTAT submodel to your sketch without having
* unresolved references. The result from this submodel will be rubbish.
*
* Revision 1.7  2000-11-30 16:36:20+01  sclement
* IsPrinting functions added.
*
* Revision 1.6  2000/10/18 08:25:24  arne
* Added a function finite that will be used only on HPUX 11.
*
* Revision 1.5  2000-10-11 15:43:09+02  arne
* Added AmeCallAtEnd in every function we exit the simulation. This way
* we will do some essential cleanup before leaving.
*
* Revision 1.4  2000-08-09 17:59:43+02  arne
* In mdlUpdate the declaration of y had as size NUMINPUTS when it should
* have NUMOUTPUTS.  Ref. Sim3-023.
*
* Revision 1.3  2000-07-31 10:06:55+02  terrade
* Add declaration of rdummy in the Input function. Bug reference
* sim30.012.
*
* Revision 1.2  2000-02-28 16:24:50+01  arne
* First version of this template for AMESim v3.0
*
* Revision 1.1  1999-12-01 15:07:26+01  arne
* Initial revision
*
*
*******************************************************************/

#include <stdio.h>
#ifndef _MICROTEC
#include <sys/types.h>
#include <sys/stat.h>
#endif
#include <fcntl.h>
#include <stdlib.h>
#include <stdarg.h>
#include <math.h>
#include <string.h>
#include <time.h>

/* Simplifying the options needed to compile with RTW */
/* We always ? wants to have a model without files  
   when using LABCAR and RTLAB*/

#if defined (LABCAR) || defined (RTLAB)
#ifndef AMERT
#define AMERT
#endif
#endif

#ifdef AMEWIN32
#ifndef WIN32
#define WIN32
#endif
#endif

/*
  Do we display one port with many variables or many ports with one variable each, 
  by default we say that we use multiple ports. But if you want to keep backward
  compability in your Simulink sketch you can remove the define of USEMULTIPLEPORTS.
*/

#define USEMULTIPLEPORTS 1

/* Add labels on the S-function block or not. */
#define AMEPORTLABELS 1


/* NOEXTERNALFILES will disable the use of files for parameters
 * NORESULTFILE will disable completely the use of the AMESim result file */

#if defined(_MICROTEC) || defined(XPCMSVISUALC) || defined(AMERT)
#define NOEXTERNALFILES
#define NORESULTFILE
#endif

#ifdef XPCMSVISUALC
#define WIN32
#endif

#include "ameutils.h"

#if defined (WIN32) || defined(XPCMSVISUALC)
#include <io.h>
/* Symbolic constants for the "access" routine: */
#define	R_OK	4	/* Test for Read permission */
#define	W_OK	2	/* Test for Write permission */
#define	X_OK	1	/* Test for eXecute permission */
#define	F_OK	0	/* Test for existence of File */
#else
#ifndef _MICROTEC
#include <unistd.h>
#endif
#endif

#if defined(AME_DS1005) || defined(AME_DS1006)
#include "dsmsg.h"
#define AMEssPrintf(a)  (msg_info_printf(MSG_SM_USER,2,"%s",(a)))
#endif


#ifdef RTLAB
#include "OpalPrint.h"
#define AMEssPrintf OpalPrint
#endif

#ifndef AMEssPrintf
#define AMEssPrintf ssPrintf 
#endif

/* ***************************************************************** 

   code inserted by AMESim
   
   *****************************************************************/

#define SUB_LENGTH    23
#define NUMSTATES    7
#define NUMINPUTS    2
#define NUMOUTPUTS   1
#define NUMVARS      56

static double r0[3], c0[5], r1[2], r4[13], r6[4], r9[2], c9[1]
              , r10[2], r13[7], c13[15], r18[7], c18[15], r19[7]
              , c19[15];

static int    I0[2], J0[2], J1[2], I4[5], J6[3], I9[1], I13[2]
              , J13[4], I18[2], J18[4], I19[2], J19[4];

static char   *t4[2], *t6[1];

/* **********  Various static declrs for use in this file ***/

/* We expect that we get 2 simulation options */

#define NUMSIMOPTS 2

static int first_call=1;
static char version[] = "LIB1.5.1";

static double NextPrint;
static double FinalTime;

static int activityindex=0;

static double v[NUMVARS];
static double saved_dot[NUMSTATES];

#ifndef NORESULTFILE
static int HeaderSize;
static int NumToSave;
static int SaveFlags[NUMVARS];
static int points=0;
static int position;
#endif

static int fd_results=-1;
static int iwrite=2;

static int consflag=0; /* Used for automatic assembly - probably not useful for Simulink???*/

struct ameSimOptionsStruct
{
   double PrintInt;
   int outoff;
};
static struct ameSimOptionsStruct ASOPT = {0.0, 0};

#ifdef _NO_PROTO

extern void ha001in_(), ha001_();
extern void rv00in_(), rv00_();
extern void tk000in_();
extern void fp04in_();
extern void pmv00in_(), pmv00_();
extern void vor01in_(), vor01_();
extern void pu001in_(), pu001_();
extern void pt001in_(), pt001_();
extern void h4node2in_();
extern void h3node3in_();
extern void hl04in_(), hl04_();

#else

extern void ha001in_(int *n, double *r, int *i, double *c, int *j
                   , double *y0);
extern void ha001_ (int *n, double *ve0, double *vedot0, double *ve1
                   , double *vi2, double *vi3, double *vi4, double *r
                 , int *i, double *c, int *j, int *flag);
extern void rv00in_(int *n, double *r, int *j);
extern void rv00_ (int *n, double *ve0, double *ve1, double *ve2
                   , double *vi3, double *r, int *j, int *flag
                   );
extern void tk000in_(int *n, double *y0);
extern void fp04in_(int *n, double *r, int *i, char **s);
extern void pmv00in_(int *n);
extern void pmv00_ (int *n, double *ve0, double *ve1);
extern void vor01in_(int *n, double *r, char **s, int *j);
extern void vor01_ (int *n, double *ve0, double *ve1, double *ve2
                   , double *ve3, double *vi4, double *r, char **s
                 , int *j);
extern void pu001in_(int *n, double *r, int *i, double *c);
extern void pu001_ (int *n, double *ve0, double *ve1, double *ve2
                   , double *ve3, double *r, int *i, double *c
                 );
extern void pt001in_(int *n, double *r, double *y0);
extern void pt001_ (int *n, double *ve0, double *ve1, double *r
                 );
extern void h4node2in_(int *n, double *y0, double *y1, double *y2
                   );
extern void h3node3in_(int *n, double *y0, double *y1);
extern void hl04in_(int *n, double *r, int *i, double *c, int *j
                   , double *y0, double *y1);
extern void hl04_(int *n, double *ve0, double *vedot0, double *ve1
                   , double *ve2, double *vedot2, double *ve3
                   , double *vi4, double *vi5, double *vi6, double *vi7
                   , double *vi8, double *r, int *i, double *c
                 , int *j, int *flag);

#endif

void ameInstallFprintf( int (*newameInternalfprintf)(FILE *fp, const char *fmt, va_list ap) );
int ameprintf(const char *fmt, ...);
int amefprintf(FILE *fp, const char *fmt, ...);
int amewarning(const char *fmt, ...);
int amemessage(const char *fmt, ...);
extern int ChangeOrAddRealGlobalParamValue(char *parname, double value, int allowAdd);
extern int ChangeOrAddIntGlobalParamValue(char *parname, int value, int allowAdd);
extern int ChangeOrAddTextGlobalParamValue(char *parname, char *value, int allowAdd);
extern int ameReadGlobalParamsFromCharArrays(char **globalParamCharArray, 
                                      int numlineGlobalParams,
                                      char **customParamCharArray, 
                                      int numlinesCustomParam);
extern void ameGetAndInterpretValuesFromCharArray(char **paramarray, int *lineptr, 
                                           double *rpar, int numrpar, 
                                           int *ipar, int numipar, 
                                           char *text);
extern void ameGetAndInterpretTextValuesFromCharArray(char **paramarray, 
                                                      int *lineptr, 
                                                      char **textpar, 
                                                      int numtextpar, 
                                                      char *textdummy);


#ifdef AMERT
#define resdis(a,b,c,d,e,f)
#define ChangeState(a,b) ( (*(a)) = (b) )
#define getnfg_() 0
#endif
/* ******************    Here comes the functions ************ */

/*******************************************************************
*                                                                  *
*  This function returns the status of the assembly process.       *
*                                                                  *
*******************************************************************/

extern int ConstructionLevel_()
{
   return consflag;
}


/*******************************************************************
*                                                                  *
*  This function returns the index in the given array of a given   *
*  variable pointer                                                *
*                                                                  *
*  Inputs: array      - the array where the variable is stored.    *
*          array_size - size of the given array.                   *
*          var        - the variable we are looking for.           *
*                                                                  *
*  Return value: the corresponding index in the array.             *
*                                                                  *
*******************************************************************/

static int GetIndex(double *array, int array_size, double *var)
{
   int i;

   for (i=0; i<array_size; ++i)
   {
      if (var == &(array[i]))
      {
         return i;
      }
   }
   /* Safe return value (?). */

   return 0;
}


/*******************************************************************
*                                                                  *
*  This function returns the index in the y array of the given     *
*  state variable.                                                 *
*  This can be useful for getting the locked status of a state.    *
*                                                                  *
*  Inputs: var        - the variable we are looking for.           *
*                                                                  *
*  Return value: the corresponding index in the y array.           *
*                                                                  *
*******************************************************************/

extern int GetStateVarIndex(double *var)
{
   return 0; /* For Simulink I dont think we can do this !!!! */
}

/*******************************************************************
*                                                                  *
*  This function returns the index in the v array of the given     *
*  variable. This index corresponds to the rank of the variable in *
*  the results file.                                               *
*                                                                  *
*  Inputs: var        - the variable we are looking for.           *
*                                                                  *
*  Return value: the corresponding index in the v array.           *
*                                                                  *
*******************************************************************/

⌨️ 快捷键说明

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