示例#1
0
static void mdlStart(SimStruct *S)
{
    ParStruc    *Par    = malloc(sizeof *Par); /* Wavenet's parameters */
    #if defined(MATLAB_MEX_FILE)
    int_T       *Ir     = ssGetJacobianIr(S);
    int_T       *Jc     = ssGetJacobianJc(S);
    #endif
        
   if (Par == NULL){
       free(Par);
       ssSetErrorStatus(S, "Could not allocate data cache memory.");
       return;
   }
    
	/* Populate Parameters */
    Par->NumberOfUnits      =   (uint_T) mxGetScalar(NUMUNITS(S));
    /*Par->BreakPoints        =   mxGetData(PAR_BREAKPOINTS(S));*/
    Par->LinearCoef         =   mxGetData(PAR_LINEARCOEF(S));
    Par->OutputOffset       =   mxGetScalar(PAR_OUTPUTOFFSET(S));
    Par->OutputCoef         =   mxGetData(PAR_OUTPUTCOEF(S));
    Par->Translation        =   mxGetData(PAR_TRANSLATION(S));    
    
    /* dimensions */
    Par->DimXlin            =   (uint_T) mxGetM(PAR_LINEARCOEF(S));
    
    /* Set the cached data into the user data area for 'this' block. */
    ssSetUserData(S, Par);
  
    /* Finish the initialization */
    mdlProcessParameters(S);
    
    #if defined(MATLAB_MEX_FILE)
    /* Jacobian (dy/dR, R:=regressors) set up */
    /* If linearization is disabled, we'll have no storage */
    if ((Ir == NULL) || (Jc == NULL)) return;
    /* DimInp = 1 for PWLINEAR */
     Ir[0] = 0;
     Jc[0] = 0;
    
    #endif
}
示例#2
0
static void mdlStart(SimStruct *S)
{
    ParStruc    *Par    = malloc(sizeof *Par); /* Tree Partition's parameters */
    TreeStruc   *Tree   =  malloc(sizeof *Tree); /* struct for Parameters.Tree struct */
    #if defined(MATLAB_MEX_FILE)
    int_T       *Ir     = ssGetJacobianIr(S);
    int_T       *Jc     = ssGetJacobianJc(S);
    int_T       k;
    #endif
    
    /* for some reason, dim is one more than num reg (Anatoli would know why) */
    uint_T      DimInp  = (uint_T) mxGetScalar(NUMREG(S))+1;
    boolean_T   IsLinear= (boolean_T) mxIsEmpty(TREE_TREELEVELPNTR(S));
	uint_T  MaxLvl = 0;
    
   if ((Tree == NULL) || (Par == NULL)){
       free(Tree);
       free(Par);
       ssSetErrorStatus(S, "Could not allocate data cache memory.");
       return;
   }
    
	/* Populate Parameters */
    Par->NumberOfUnits  = (uint_T) mxGetScalar(NUMUNITS(S));
    Par->Threshold      = mxGetScalar(OPT_THRESHOLD(S));
    Par->RegressorMean  = mxGetData(PAR_REGRESSORMEAN(S));
    Par->OutputOffset   = mxGetScalar(PAR_OUTPUTOFFSET(S));
    Par->LinearCoef     = mxGetData(PAR_LINEARCOEF(S));
    Par->SampleLength   = (uint_T) mxGetScalar(PAR_SAMPLELENGTH(S));
    Par->NoiseVariance  = mxGetScalar(PAR_NOISEVARIANCE(S));
    Par->IsLinear           = IsLinear;

    /* Populate Tree */
    if (!IsLinear){
        Tree->TreeLevelPntr = mxGetData(TREE_TREELEVELPNTR(S));
        MaxLvl = (uint_T) Tree->TreeLevelPntr[Par->NumberOfUnits-1];
        Tree->AncestorDescendantPntr = mxGetData(TREE_ANCESTORDESCENDANTPNTR(S));
        Tree->LocalizingVectors = mxGetData(TREE_LOCALIZINGVECTORS(S));
        Tree->LocalCovMatrix = mxGetData(TREE_LOCALCOVMATRIX(S));
        Tree->LocalParVector = mxGetData(TREE_LOCALPARVECTOR(S));
        
        /* allocate memory for temporary arrays required by evaluate_treepartition */
        Par->ttv    = (real_T *) malloc(DimInp*sizeof(real_T));
        Par->ttm    = (real_T *) malloc(DimInp*DimInp*sizeof(real_T));
        Par->lfmax  = (real_T *) malloc(MaxLvl*sizeof(real_T));
        Par->lfmin  = (real_T *) malloc(MaxLvl*sizeof(real_T));
        
        /* Memory allocation error checking */
        if ( (Par->ttv == NULL) || (Par->ttm == NULL) ||
        (Par->lfmax == NULL) || (Par->lfmin == NULL) )
        {
            free(Tree);
            free(Par);
            ssSetErrorStatus(S, "Could not allocate data cache memory.");
            return;
        } /* Error checking complete */
        
    }	
		
	Par->Tree = Tree;   
    
    /* Set the cached data into the user data area for 'this' block. */
    ssSetUserData(S, Par);
  
    /* Finish the initialization */
    mdlProcessParameters(S);
    
    #if defined(MATLAB_MEX_FILE)
    /* Jacobian (dy/dR, R:=regressors) set up */
    /* If linearization is disabled, we'll have no storage */
    if ((Ir == NULL) || (Jc == NULL)) return;
    for (k = 0; k < DimInp-1; k++) {
        Ir[k] = 0;
        Jc[k] = k;
    }
    #endif
}
示例#3
0
/* Function: mdlJacobian ======================================================
 * Abstract: populate the model's Jacobian data.
 * See the on-line documentation for mxCreateSparse for
 * information regarding the format of Ir, Jc, and Pr data.
 *
 *        [ A | B ]    
 *  J =   [ --+-- ]  (= D here)
 *        [ C | D ]  
 *                 
 */
static void mdlJacobian(SimStruct *S)
{
    #if defined(MATLAB_MEX_FILE)
    real_T      *Pr     = ssGetJacobianPr(S);
	real_T		*out;
	real_T		*temp;
    uint_T      nr      = (uint_T) mxGetScalar(NUMREG(S));
    ParStruc    *Par    = ssGetUserData(S);
    const real_T      *Reg    = ssGetInputPortRealSignal(S,0); /* input signals are contiguous */
    int_T		k;
    mxArray *plhs;
    mxArray *prhs[3];
    mxArray *ParStruct, *TreeParStruct;
    mxArray *TypeStr;
    const char **fnamesPar;
    const char **fnamesTree;
    
    /* plhs            = mxCreateDoubleMatrix(1,nr,mxREAL); */
    TypeStr         = mxCreateString("treepartition");
    fnamesPar       = mxCalloc(8, sizeof(*fnamesPar));
    fnamesTree      = mxCalloc(5, sizeof(*fnamesTree));
       
     /* memory error check */
    if ( (fnamesPar==NULL) || (fnamesTree==NULL) || (TypeStr==NULL) || (prhs==NULL)){
        ssSetErrorStatus(S, "Could not allocate memory for Jacobian computation.");
        return;
    }
    
    /* Tree struct field names */
    fnamesTree[0] = "TreeLevelPntr";
    fnamesTree[1] = "AncestorDescendantPntr";
    fnamesTree[2] = "LocalizingVectors";
    fnamesTree[3] = "LocalCovMatrix";
    fnamesTree[4] = "LocalParVector";
    
    /* Parameter struct field names */
    fnamesPar[0] = "NumberOfUnits";
    fnamesPar[1] = "Threshold";
    fnamesPar[2] = "RegressorMean";
    fnamesPar[3] = "OutputOffset";
    fnamesPar[4] = "LinearCoef";
    fnamesPar[5] = "SampleLength";
    fnamesPar[6] = "NoiseVariance";
    fnamesPar[7] = "Tree";
    
    TreeParStruct   = mxCreateStructMatrix(1,1,5,fnamesTree);
    ParStruct       = mxCreateStructMatrix(1,1,8,fnamesPar);
    
    /* do memory error check */
    if ((TreeParStruct==NULL) || (ParStruct==NULL)){
        ssSetErrorStatus(S, "Could not allocate memory for Jacobian computation.");
        return;
    }
    
    /* set fields of Parameters.Tree struct */
    mxSetFieldByNumber(TreeParStruct, 0, 0, mxDuplicateArray(TREE_TREELEVELPNTR(S)));
    mxSetFieldByNumber(TreeParStruct, 0, 1, mxDuplicateArray(TREE_ANCESTORDESCENDANTPNTR(S)));
    mxSetFieldByNumber(TreeParStruct, 0, 2, mxDuplicateArray(TREE_LOCALIZINGVECTORS(S)));
    mxSetFieldByNumber(TreeParStruct, 0, 3, mxDuplicateArray(TREE_LOCALCOVMATRIX(S)));
    mxSetFieldByNumber(TreeParStruct, 0, 4, mxDuplicateArray(TREE_LOCALPARVECTOR(S)));
    
    /* set fields of Paramater struct */
    mxSetFieldByNumber(ParStruct, 0, 0, mxDuplicateArray(NUMUNITS(S)));
    mxSetFieldByNumber(ParStruct, 0, 1, mxDuplicateArray(OPT_THRESHOLD(S)));
    mxSetFieldByNumber(ParStruct, 0, 2, mxDuplicateArray(PAR_REGRESSORMEAN(S)));
    mxSetFieldByNumber(ParStruct, 0, 3, mxDuplicateArray(PAR_OUTPUTOFFSET(S)));
    mxSetFieldByNumber(ParStruct, 0, 4, mxDuplicateArray(PAR_LINEARCOEF(S)));
    mxSetFieldByNumber(ParStruct, 0, 5, mxDuplicateArray(PAR_SAMPLELENGTH(S)));
    mxSetFieldByNumber(ParStruct, 0, 6, mxDuplicateArray(PAR_NOISEVARIANCE(S)));
    mxSetFieldByNumber(ParStruct, 0, 7, TreeParStruct);
	
	prhs[0] = mxCreateDoubleMatrix(1,nr,mxREAL);
    temp = mxGetPr(prhs[0]);
	for (k=0; k<nr; k++){
		temp[k] = Reg[k];
	}

	/* mxSetPr(prhs[0],Reg); */
    prhs[1] = ParStruct;
    prhs[2] = TypeStr;
    
    /* 
     * Call utEvalStateJacobian to compute the regressors 
     * M file: dydx = utEvalStateJacobian(x,par,type) 
     */
    
    mexCallMATLAB(1,&plhs,3,prhs,"utEvalStateJacobian");

    out = mxGetPr(plhs);
	for(k=0; k<nr; k++){
		Pr[k] = out[k];
	}
    
    mxFree((void *)fnamesTree);
    mxFree((void *)fnamesPar);
    mxDestroyArray(plhs);
    
	mxDestroyArray(prhs[0]);
	mxDestroyArray(TypeStr);
	mxDestroyArray(ParStruct);
    #endif
    
}
示例#4
0
/* Function: mdlJacobian ======================================================
 * Abstract: populate the model's Jacobian data.
 * See the on-line documentation for mxCreateSparse for
 * information regarding the format of Ir, Jc, and Pr data.
 *
 *        [ A | B ]    
 *  J =   [ --+-- ]  (= D here)
 *        [ C | D ]  
 *                 
 */
static void mdlJacobian(SimStruct *S)
{
    #if defined(MATLAB_MEX_FILE)
    real_T      *Pr     = ssGetJacobianPr(S);
	real_T		*out;
	real_T		*temp;
    ParStruc    *Par    = ssGetUserData(S);
    const real_T *Reg   = ssGetInputPortRealSignal(S,0); /* input signals are contiguous */
    mxArray *plhs;
    mxArray *prhs[3];
    mxArray *ParStruct;
    mxArray *TypeStr;
    const char **fnamesPar;
    
    /* plhs            = mxCreateDoubleMatrix(1,nr,mxREAL); */
    TypeStr         = mxCreateString("pwlinear");
    fnamesPar       = mxCalloc(5, sizeof(*fnamesPar));
       
     /* memory error check */
    if ( (fnamesPar==NULL) || (TypeStr==NULL) || (prhs==NULL)){
        ssSetErrorStatus(S, "Could not allocate memory for Jacobian computation.");
        return;
    }
    
    /* Parameter struct field names */
    fnamesPar[0]  = "NumberOfUnits";
    fnamesPar[1]  = "LinearCoef";
    fnamesPar[2]  = "OutputCoef";
    fnamesPar[3]  = "OutputOffset";
    fnamesPar[4]  = "Translation";
    
    ParStruct     = mxCreateStructMatrix(1,1,5,fnamesPar);
    
    /* do memory error check */
    if (ParStruct==NULL) {
        ssSetErrorStatus(S, "Could not allocate memory for Jacobian computation.");
        return;
    }
    
    /* set fields of Paramater struct */
    mxSetFieldByNumber(ParStruct, 0, 0,  mxDuplicateArray(NUMUNITS(S)));
    mxSetFieldByNumber(ParStruct, 0, 1,  mxDuplicateArray(PAR_LINEARCOEF(S)));
    mxSetFieldByNumber(ParStruct, 0, 2,  mxDuplicateArray(PAR_OUTPUTCOEF(S)));
    mxSetFieldByNumber(ParStruct, 0, 3,  mxDuplicateArray(PAR_OUTPUTOFFSET(S)));
    mxSetFieldByNumber(ParStruct, 0, 4,  mxDuplicateArray(PAR_TRANSLATION(S)));
    
    /* nr = dimension of regressor = 1 */
	prhs[0] = mxCreateDoubleMatrix(1,1,mxREAL);
    temp = mxGetPr(prhs[0]);
	temp[0] = Reg[0];
    prhs[1] = ParStruct;
    prhs[2] = TypeStr;
    
    /* 
     * Call utEvalStateJacobian to compute the regressors 
     * M file: dydx = utEvalStateJacobian(x,par,type) 
     */
    mexCallMATLAB(1,&plhs,3,prhs,"utEvalStateJacobian");

    out = mxGetPr(plhs);
    Pr[0] = out[0];
    
    mxFree((void *)fnamesPar);
    mxDestroyArray(plhs);
    
	mxDestroyArray(prhs[0]);
	mxDestroyArray(TypeStr);
	mxDestroyArray(ParStruct);
    #endif
}