📄 backvalve_.c
字号:
#define AMEDEBUG(a)
static char **ameGetCharVectorFromSParam(const mxArray *pVal, int *vectorsize, int *rowsize, int *colsize)
{
int NumElems = mxGetNumberOfElements(pVal);
int numRows=mxGetM(pVal);
int numCols=mxGetN(pVal);
char *buf=NULL;
char **cvect=NULL;
*vectorsize = 0;
*rowsize = 0;
*colsize = 0;
if (NumElems <= 0)
{
return NULL;
}
AMEDEBUG(ameprintf("ameGetCharVectorFromSParam> NumElems = %d\n",NumElems));
AMEDEBUG(ameprintf("ameGetCharVectorFromSParam> numRows = %d\n",numRows));
AMEDEBUG(ameprintf("ameGetCharVectorFromSParam> numCols = %d\n",numCols));
buf = calloc(1,NumElems+2);
if( (buf!=NULL) && mxGetString(pVal, buf, NumElems+1)==0 )
{
int row,col;
AMEDEBUG(ameprintf("ameGetCharVectorFromSParam>buf = '%s'\n",buf));
cvect = malloc(numRows*sizeof(char*));
for(row=0;row<numRows;row++)
{
cvect[row] = malloc(numCols+1);
for (col=0;col<numCols; col++)
{
cvect[row][col] = *(buf+row+col*numRows);
}
cvect[row][numCols] = '\0';
if(numCols > 0)
{
for (col=numCols-1; ( (col>=0) && (cvect[row][col] == ' ') ) ; col--)
{
cvect[row][col] = '\0';
}
}
AMEDEBUG(ameprintf("ameGetCharVectorFromSParam> cvect[%d] = '%s'\n",row,cvect[row]));
}
*vectorsize = NumElems;
*rowsize = numRows;
*colsize = numCols;
}
AMEDEBUG(ameprintf("ameGetCharVectorFromSParam> almost done\n"));
free(buf);
AMEDEBUG(ameprintf("ameGetCharVectorFromSParam> done\n"));
return cvect;
}
static double *ameGetRealVectorFromSParam(const mxArray *pVal, int *vectorsize)
{
int NumElems = mxGetNumberOfElements(pVal);
int numRows = mxGetM(pVal);
int numCols = mxGetN(pVal);
double *rvector=NULL;
*vectorsize = 0;
if (NumElems <= 0)
{
return NULL;
}
AMEDEBUG(ameprintf("ameGetRealVectorFromSParam> numRows = %d\n",numRows));
AMEDEBUG(ameprintf("ameGetRealVectorFromSParam> numCols = %d\n",numCols));
rvector = malloc(NumElems*sizeof(double));
if(rvector != NULL)
{
const double *realValues=mxGetPr(pVal);
int i;
for(i=0;i<NumElems;i++)
{
rvector[i] = realValues[i];
AMEDEBUG(ameprintf("ameGetRealVectorFromSParam> realValues[%d] = %g\n",i,realValues[i]));
}
*vectorsize = NumElems;
}
return rvector;
}
#define AMENOMOREARGS 99
static int ameSimOptions(SimStruct *S, struct ameSimOptionsStruct *AS)
{
int numSFuncParams=ssGetSFcnParamsCount(S);
static char mesg[250];
/* Check for old style (2 scalar args) */
if (numSFuncParams == 2)
{
const mxArray *pVal = ssGetSFcnParam(S,0);
int NumElems = mxGetNumberOfElements(pVal);
if(NumElems == 1)
{
pVal = ssGetSFcnParam(S,1);
NumElems = mxGetNumberOfElements(pVal);
if(NumElems == 1)
{
/* We are in the case of two scalar parameters - oldstyle */
if(mxGetPr(ssGetSFcnParam(S, 0))[0] == 0.0)
{
AS->outoff = 1;
}
else
{
AS->outoff = 0;
}
AS->PrintInt = mxGetPr(ssGetSFcnParam(S, 1))[0];
return AMENOMOREARGS;
}
}
}
if (numSFuncParams >= 1)
{
/* get the vector with sim options */
const mxArray *pVal = ssGetSFcnParam(S,0);
double *simoptions=NULL;
int numsimopts=0;
simoptions = ameGetRealVectorFromSParam(pVal, &numsimopts);
if(simoptions != NULL)
{
if (numsimopts == 2)
{
if(simoptions[0] == 0.0)
{
AS->outoff = 1;
}
else
{
AS->outoff = 0;
}
AS->PrintInt = simoptions[1];
}
free(simoptions);
}
}
AMEDEBUG(ameprintf("SIMOPT PrintInt> %g\n",AS->PrintInt));
AMEDEBUG(ameprintf("SIMOPT outoff> %d\n",AS->outoff));
return 0;
}
static int ameSetGlobalParams(SimStruct *S)
{
return 1;
}
static void ameDealWithInputArgs(SimStruct *S, struct ameSimOptionsStruct *AS)
{
int i;
/* in RTW all parameters are actually doubles - as far as I can see
the text can be extracted if one knows on what position they come */
/* for an AMESim S function I propose the following
1. vector with simulation options (real and integer values)
2. text vector with names of REAL global params
3. double vector with values of REAL global params
4. text vector with names of INTEGER global params
5. double vector with values of INTEGER global params
6. text vector with names of TEXT global params
7. text vector with values of TEXT global params */
int numSFuncParams=ssGetSFcnParamsCount(S);
/* First we deal with the simulation options
this we do in a separate function */
if (ameSimOptions(S, AS) == AMENOMOREARGS)
{
/* There are no args to be dealt with */
return;
}
AMEDEBUG(ameprintf("numSFuncParams> %d\n",numSFuncParams));
if (numSFuncParams >= 3)
{
/* get the vector with real global param names */
const mxArray *pnames = ssGetSFcnParam(S,1);
/* get the vector with real global param values */
const mxArray *pVal = ssGetSFcnParam(S,2);
int numtxtrows,numtxtcols,totaltxtlen;
double *realGlobalParVals=NULL;
char **realGlobalParNames=NULL;
int numRealGlobalParVals=0;
realGlobalParVals = ameGetRealVectorFromSParam(pVal, &numRealGlobalParVals);
realGlobalParNames = ameGetCharVectorFromSParam(pnames, &totaltxtlen, &numtxtrows, &numtxtcols);
if(realGlobalParVals && realGlobalParNames)
{
if(numRealGlobalParVals != numtxtrows)
{
amefprintf(stderr,"AMESIM> The real parameters passed as argument to the AMESim S-function\n");
amefprintf(stderr,"AMESIM> does not have the same number of titles as values (2nd and 3rd argument)\n");
amefprintf(stderr,"AMESIM> Number of rows for values %d, for titles %d\n", numtxtrows, numRealGlobalParVals);
return;
}
for (i=0; i<numRealGlobalParVals; i++)
{
AMEDEBUG(ameprintf("before call to ChangeOrAddRealGlobalParamValue\n"));
ChangeOrAddRealGlobalParamValue(realGlobalParNames[i], realGlobalParVals[i], 1);
AMEDEBUG(ameprintf("REAL %d> %s %g\n",i,realGlobalParNames[i], realGlobalParVals[i]));
free(realGlobalParNames[i]);
}
free(realGlobalParVals);
}
}
if (numSFuncParams >= 5)
{
/* get the vector with integer global param names */
const mxArray *pnames = ssGetSFcnParam(S,3);
/* get the vector with integer global param values */
const mxArray *pVal = ssGetSFcnParam(S,4);
int numtxtrows,numtxtcols,totaltxtlen;
double *intGlobalParVals=NULL;
char **intGlobalParNames=NULL;
int numIntGlobalParVals=0;
intGlobalParVals = ameGetRealVectorFromSParam(pVal, &numIntGlobalParVals);
intGlobalParNames = ameGetCharVectorFromSParam(pnames, &totaltxtlen, &numtxtrows, &numtxtcols);
if(intGlobalParVals != NULL && intGlobalParNames != NULL)
{
if(numIntGlobalParVals != numtxtrows)
{
amefprintf(stderr,"AMESIM> The integer parameters passed as argument to the AMESim S-function\n");
amefprintf(stderr,"AMESIM> does not have the same number of titles as values (4th and 5th argument)\n");
amefprintf(stderr,"AMESIM> Number of rows for values %d, for titles %d\n", numtxtrows, numIntGlobalParVals);
return;
}
for (i=0; i<numIntGlobalParVals; i++)
{
ChangeOrAddIntGlobalParamValue(intGlobalParNames[i], (int)intGlobalParVals[i], 1);
AMEDEBUG(ameprintf("INT %d> %s %d\n",i,intGlobalParNames[i], (int)intGlobalParVals[i]));
free(intGlobalParNames[i]);
}
free(intGlobalParNames);
free(intGlobalParVals);
}
}
if (numSFuncParams >= 7)
{
/* get the vector with text global param names */
const mxArray *pnames = ssGetSFcnParam(S,5);
/* get the vector with text global param values */
const mxArray *pVal = ssGetSFcnParam(S,6);
int numtxtrows,numtxtcols,totaltxtlen;
char **txtGlobalParVals=NULL;
char **txtGlobalParNames=NULL;
int numTxtGlobalParVals=0;
txtGlobalParVals = ameGetCharVectorFromSParam(pVal, &totaltxtlen, &numTxtGlobalParVals, &numtxtcols);
txtGlobalParNames = ameGetCharVectorFromSParam(pnames, &totaltxtlen, &numtxtrows, &numtxtcols);
if(txtGlobalParVals != NULL && txtGlobalParNames != NULL)
{
if(numTxtGlobalParVals != numtxtrows)
{
amefprintf(stderr,"AMESIM> The text parameters passed as argument to the AMESim S-function\n");
amefprintf(stderr,"AMESIM> does not have the same number of titles as values (6th and 7th argument)\n");
amefprintf(stderr,"AMESIM> Number of rows for values %d, for titles %d\n", numtxtrows, numTxtGlobalParVals);
return;
}
for (i=0; i<numTxtGlobalParVals; i++)
{
ChangeOrAddTextGlobalParamValue(txtGlobalParNames[i], txtGlobalParVals[i], 1);
AMEDEBUG(ameprintf("TEXT %d> %s %s\n",i,txtGlobalParNames[i], txtGlobalParVals[i]));
free(txtGlobalParNames[i]);
free(txtGlobalParVals[i]);
}
free(txtGlobalParNames);
free(txtGlobalParVals);
}
}
}
/******************************************************************
* Add a clear mex callback to the S-function block.
* This will hopefully clear up any problems we have with people
* forgetting to do this.
******************************************************************/
static void ameSetupClearCallback(SimStruct *S)
{
static int have_this_been_done=0;
if(have_this_been_done)
{
return;
}
have_this_been_done=1;
#ifdef MATLAB_MEX_FILE
{
static char cmdstr[1024];
const char *pathname;
const char *sfuncname;
pathname = ssGetPath(S);
sfuncname = ssGetModelName(S);
/* Should we really clear both at start and end ???? - it should be enough at end ?*/
sprintf(cmdstr,"set_param('%s', 'InitFcn', 'clear %s')",pathname,sfuncname);
if(mexEvalString(cmdstr))
{
amefprintf(stderr,"** Failed to install a InitFcn callback with %s\n",cmdstr);
amefprintf(stderr,"** Suggest that you do a 'clear %s' manually after each simulation\n", sfuncname);
}
sprintf(cmdstr,"set_param('%s', 'StopFcn', 'clear %s')",pathname,sfuncname);
if(mexEvalString(cmdstr))
{
amefprintf(stderr,"** Failed to install a StopFcn callback with %s\n",cmdstr);
amefprintf(stderr,"** Suggest that you do a 'clear %s' manually after each simulation\n", sfuncname);
}
}
#endif
}
/******************************************************************
* An interface neutral function that returns the number of
* inputs/outputs and the names of the variables on the ports.
******************************************************************/
static void getPortNames(int *numinputs, int *numoutputs, char ***arg_inputports, char ***arg_outputports)
{
int i;
*numinputs = NUMINPUTS;
*numoutputs = NUMOUTPUTS;
if ( (arg_inputports != NULL) && (arg_outputports != NULL) )
{
char **inputports;
char **outputports;
inputports = malloc(sizeof(char*)*NUMINPUTS);
outputports = malloc(sizeof(char*)*NUMOUTPUTS);
/* The generated code will create variables "outputVarTitles" and "inputVarTitles"
that contains the names of the variables on the ports */
/* START of generated code for 'INTERFACEPORTNAMES_START' */
{
int iii;
char *outputVarTitles[NUMOUTPUTS];
char *inputVarTitles[NUMINPUTS];
inputVarTitles[0] = malloc(4);
strcpy(inputVarTitles[0],"MOT");
inputVarTitles[1] = malloc(6);
strcpy(inputVarTitles[1],"VALVE");
outputVarTitles[0] = malloc(4);
strcpy(outputVarTitles[0],"PRE");
/* END of generated code for 'INTERFACEPORTNAMES_START' */
for (i=0; i<NUMOUTPUTS; i++)
{
outputports[i] = malloc(strlen(outputVarTitles[i])+1);
strcpy(outputports[i], outputVarTitles[i]);
}
for (i=0; i<NUMINPUTS; i++)
{
inputports[i] = malloc(strlen(inputVarTitles[i])+1);
strcpy(inputports[i], inputVarTitles[i]);
}
/* START of generated code for 'INTERFACEPORTNAMES_END' */
for (iii=0; iii< NUMOUTPUTS; iii++)
{
free(outputVarTitles[iii]);
}
for (iii=0; iii< NUMINPUTS; iii++)
{
free(inputVarTitles[iii]);
}
}
/* END of generated code for 'INTERFACEPORTNAMES_END' */
*arg_inputports = inputports;
*arg_outputports = outputports;
}
}
/******************************************************************
* Add names to the ports (if we
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -