Ejemplo n.º 1
0
/*--------------------------------------------------------------------------*/
void sciblk4(scicos_block *Blocks, int flag)
{
    /*counter and address variable declaration*/
    int i = 0, j = 0, k = 0, topsave = 0;
    int ierr = 0;
    int kfun = 0;

    int *header = NULL, ne1 = 0;
    double *le111 = NULL;

    int *il_xd = NULL, *il_res = NULL, *il_out = NULL, *il_outptr = NULL;
    int *il_xprop = NULL;

    int *il_z = NULL, *il_oz = NULL, *il_ozptr = NULL, *il_x = NULL;
    int *il_mode = NULL, *il_evout = NULL, *il_g = NULL;
    double *l_mode = NULL;
    double *l_xprop = NULL;

    /* variable for output typed port */
    int nout = 0;

    int nv = 0, mv = 0;
    int *ptr = NULL, *funtyp = NULL;

    /* set number of left and right hand side parameters */
    int mlhs = 1, mrhs = 2;

    /* Save Top counter */
    topsave = Top;

    /* Retrieve block number */
    kfun = get_block_number();

    /* Retrieve funtyp by import structure */
    strcpy(C2F(cha1).buf, "funtyp");
    ierr = getscicosvarsfromimport(C2F(cha1).buf, (void**)&ptr, &nv, &mv);
    if (ierr == 0)
    {
        goto err;
    }
    funtyp = (int *) ptr;

    /****************************
     * create scilab tlist Blocks
     ****************************/
    if ((createblklist(&Blocks[0], &ierr, (i = -1), funtyp[kfun - 1])) == 0)
    {
        goto err;
    }

    /* * flag * */
    C2F(itosci)(&flag, (i = 1, &i), (j = 1, &j));
    if (C2F(scierr)() != 0)
    {
        goto err;
    }

    /**********************
     * Call scilab function
     **********************/
    C2F(scifunc)(&mlhs, &mrhs);
    if (C2F(scierr)() != 0)
    {
        goto err;
    }

    /***************************
     * Update C block structure
     **************************/
    /* get header of output variable Blocks of sciblk4 */
    header = (int *) stk(*Lstk(Top));

    /* switch to appropriate flag */
    switch (flag)
    {

            /**************************
             * update continuous state
             **************************/
        case 0 :
        {
            if (Blocks[0].nx != 0)
            {
                /* 14 - xd */
                il_xd = (int *) listentry(header, 14);
                ierr = sci2var(il_xd, Blocks[0].xd, SCSREAL_N); /* double */
                if (ierr != 0)
                {
                    goto err;
                }

                if ((funtyp[kfun - 1] == 10004) || (funtyp[kfun - 1] == 10005))
                {
                    /* 15 - res */
                    il_res = (int *) listentry(header, 15);
                    ierr = sci2var(il_res, Blocks[0].res, SCSREAL_N); /* double */
                    if (ierr != 0)
                    {
                        goto err;
                    }
                }
            }
        }
        break;

        /**********************
         * update output state
         **********************/
        case 1 :
        {
            /* 21 - outptr */
            if (Blocks[0].nout != 0)
            {
                il_out = (int*) listentry(header, 21);
                nout = il_out[1];

                for (j = 0; j < nout; j++)
                {
                    il_outptr = (int *) listentry(il_out, j + 1);
                    ierr = sci2var(il_outptr, Blocks[0].outptr[j], Blocks[0].outsz[2 * nout + j]);
                    if (ierr != 0)
                    {
                        goto err;
                    }
                }
            }
        }
        break;

        /***********************
         * update discrete state
         ***********************/
        case 2 :
        {
            /* 7 - z */
            if (Blocks[0].nz != 0)
            {
                il_z = (int *) listentry(header, 7);
                if (Blocks[0].scsptr > 0)
                {
                    le111 = (double *) listentry(header, 7);
                    ne1 = header[7 + 2] - header[7 + 1];
                    C2F(unsfdcopy)(&ne1, le111, (i = -1, &i), Blocks[0].z, (j = -1, &j));
                }
                else
                {
                    ierr = sci2var(il_z, Blocks[0].z, SCSREAL_N); /* double */
                    if (ierr != 0)
                    {
                        goto err;
                    }
                }
            }

            /* 11 - oz */
            if (Blocks[0].noz != 0)
            {
                il_oz = (int *) listentry(header, 11);
                /* C blocks : extract */
                if ((funtyp[kfun - 1] == 4) || (funtyp[kfun - 1] == 10004))
                {
                    for (j = 0; j < Blocks[0].noz; j++)
                    {
                        il_ozptr = (int *) listentry(il_oz, j + 1);
                        if (Blocks[0].oztyp[j] == SCSUNKNOW_N)
                        {
                            ne1 = Blocks[0].ozsz[j];
                            C2F(unsfdcopy)(&ne1, (double *)il_ozptr, \
                                           (i = 1, &i), (double *)Blocks[0].ozptr[j], (k = 1, &k));
                        }
                        else
                        {
                            ierr = sci2var(il_ozptr, Blocks[0].ozptr[j], Blocks[0].oztyp[j]);
                            if (ierr != 0)
                            {
                                goto err;
                            }
                        }
                    }
                }
                /* sci blocks : don't extract */
                else if ((funtyp[kfun - 1] == 5) || (funtyp[kfun - 1] == 10005))
                {
                    ne1 = Blocks[0].ozsz[0];
                    C2F(unsfdcopy)(&ne1, (double *)il_oz, \
                                   (i = 1, &i), (double *)Blocks[0].ozptr[0], (j = 1, &j));
                }
            }

            if (Blocks[0].nx != 0)
            {
                /* 13 - x */
                il_x = (int *) listentry(header, 13);
                ierr = sci2var(il_x, Blocks[0].x, SCSREAL_N); /* double */
                if (ierr != 0)
                {
                    goto err;
                }

                /* 14 - xd */
                il_xd = (int *) listentry(header, 14);
                ierr = sci2var(il_xd, Blocks[0].xd, SCSREAL_N); /* double */
                if (ierr != 0)
                {
                    goto err;
                }
            }
        }
        break;

        /***************************
         * update event output state
         ***************************/
        case 3 :
        {
            /* 23 - evout */
            il_evout = (int *) listentry(header, 23);
            ierr = sci2var(il_evout, Blocks[0].evout, SCSREAL_N); /* double */
            if (ierr != 0)
            {
                goto err;
            }
        }
        break;

        /**********************
         * state initialisation
         **********************/
        case 4 :
        {
            /* 7 - z */
            if (Blocks[0].nz != 0)
            {
                il_z = (int *) listentry(header, 7);
                if (Blocks[0].scsptr > 0)
                {
                    le111 = (double *) listentry(header, 7);
                    ne1 = header[7 + 2] - header[7 + 1];
                    C2F(unsfdcopy)(&ne1, le111, (i = -1, &i), Blocks[0].z, (j = -1, &j));
                }
                else
                {
                    ierr = sci2var(il_z, Blocks[0].z, SCSREAL_N); /* double */
                    if (ierr != 0)
                    {
                        goto err;
                    }
                }
            }

            /* 11 - oz */
            if (Blocks[0].noz != 0)
            {
                il_oz = (int *) listentry(header, 11);
                /* C blocks : extract */
                if ((funtyp[kfun - 1] == 4) || (funtyp[kfun - 1] == 10004))
                {
                    for (j = 0; j < Blocks[0].noz; j++)
                    {
                        il_ozptr = (int *) listentry(il_oz, j + 1);
                        if (Blocks[0].oztyp[j] == SCSUNKNOW_N)
                        {
                            ne1 = Blocks[0].ozsz[j];
                            C2F(unsfdcopy)(&ne1, (double *)il_ozptr, \
                                           (i = 1, &i), (double *)Blocks[0].ozptr[j], (k = 1, &k));
                        }
                        else
                        {
                            ierr = sci2var(il_ozptr, Blocks[0].ozptr[j], Blocks[0].oztyp[j]);
                            if (ierr != 0)
                            {
                                goto err;
                            }
                        }
                    }
                }
                /* sci blocks : don't extract */
                else if ((funtyp[kfun - 1] == 5) || (funtyp[kfun - 1] == 10005))
                {
                    ne1 = Blocks[0].ozsz[0];
                    C2F(unsfdcopy)(&ne1, (double *)il_oz, \
                                   (i = 1, &i), (double *)Blocks[0].ozptr[0], (j = 1, &j));
                }
            }

            if (Blocks[0].nx != 0)
            {
                /* 13 - x */
                il_x = (int *) listentry(header, 13);
                ierr = sci2var(il_x, Blocks[0].x, SCSREAL_N); /* double */
                if (ierr != 0)
                {
                    goto err;
                }

                /* 14 - xd */
                il_xd = (int *) listentry(header, 14);
                ierr = sci2var(il_xd, Blocks[0].xd, SCSREAL_N); /* double */
                if (ierr != 0)
                {
                    goto err;
                }
            }
        }
        break;

        /*********
         * finish
         *********/
        case 5 :
        {
            /* 7 - z */
            if (Blocks[0].nz != 0)
            {
                il_z = (int *) listentry(header, 7);
                if (Blocks[0].scsptr > 0)
                {
                    le111 = (double *) listentry(header, 7);
                    ne1 = header[7 + 2] - header[7 + 1];
                    C2F(unsfdcopy)(&ne1, le111, (i = -1, &i), Blocks[0].z, (j = -1, &j));
                }
                else
                {
                    ierr = sci2var(il_z, Blocks[0].z, SCSREAL_N); /* double */
                    if (ierr != 0)
                    {
                        goto err;
                    }
                }
            }

            /* 11 - oz */
            if (Blocks[0].noz != 0)
            {
                il_oz = (int *) listentry(header, 11);
                /* C blocks : extract */
                if ((funtyp[kfun - 1] == 4) || (funtyp[kfun - 1] == 10004))
                {
                    for (j = 0; j < Blocks[0].noz; j++)
                    {
                        il_ozptr = (int *) listentry(il_oz, j + 1);
                        if (Blocks[0].oztyp[j] == SCSUNKNOW_N)
                        {
                            ne1 = Blocks[0].ozsz[j];
                            C2F(unsfdcopy)(&ne1, (double *)il_ozptr, \
                                           (i = 1, &i), (double *)Blocks[0].ozptr[j], (k = 1, &k));
                        }
                        else
                        {
                            ierr = sci2var(il_ozptr, Blocks[0].ozptr[j], Blocks[0].oztyp[j]);
                            if (ierr != 0)
                            {
                                goto err;
                            }
                        }
                    }
                }
                /* sci blocks : don't extract */
                else if ((funtyp[kfun - 1] == 5) || (funtyp[kfun - 1] == 10005))
                {
                    ne1 = Blocks[0].ozsz[0];
                    C2F(unsfdcopy)(&ne1, (double *)il_oz, \
                                   (i = 1, &i), (double *)Blocks[0].ozptr[0], (j = 1, &j));
                }
            }
        }
        break;

        /*****************************
         * output state initialisation
         *****************************/
        case 6 :
        {
            /* 7 - z */
            if (Blocks[0].nz != 0)
            {
                il_z = (int *) listentry(header, 7);
                if (Blocks[0].scsptr > 0)
                {
                    le111 = (double *) listentry(header, 7);
                    ne1 = header[7 + 2] - header[7 + 1];
                    C2F(unsfdcopy)(&ne1, le111, (i = -1, &i), Blocks[0].z, (j = -1, &j));
                }
                else
                {
                    ierr = sci2var(il_z, Blocks[0].z, SCSREAL_N); /* double */
                    if (ierr != 0)
                    {
                        goto err;
                    }
                }
            }

            /* 11 - oz */
            if (Blocks[0].noz != 0)
            {
                il_oz = (int *) listentry(header, 11);
                /* C blocks : extract */
                if ((funtyp[kfun - 1] == 4) || (funtyp[kfun - 1] == 10004))
                {
                    for (j = 0; j < Blocks[0].noz; j++)
                    {
                        il_ozptr = (int *) listentry(il_oz, j + 1);
                        if (Blocks[0].oztyp[j] == SCSUNKNOW_N)
                        {
                            ne1 = Blocks[0].ozsz[j];
                            C2F(unsfdcopy)(&ne1, (double *)il_ozptr, \
                                           (i = 1, &i), (double *)Blocks[0].ozptr[j], (k = 1, &k));
                        }
                        else
                        {
                            ierr = sci2var(il_ozptr, Blocks[0].ozptr[j], Blocks[0].oztyp[j]);
                            if (ierr != 0)
                            {
                                goto err;
                            }
                        }
                    }
                }
                /* sci blocks : don't extract */
                else if ((funtyp[kfun - 1] == 5) || (funtyp[kfun - 1] == 10005))
                {
                    ne1 = Blocks[0].ozsz[0];
                    C2F(unsfdcopy)(&ne1, (double *)il_oz, \
                                   (i = 1, &i), (double *)Blocks[0].ozptr[0], (j = 1, &j));
                }
            }

            if (Blocks[0].nx != 0)
            {
                /* 13 - x */
                il_x = (int *) listentry(header, 13);
                ierr = sci2var(il_x, Blocks[0].x, SCSREAL_N); /* double */
                if (ierr != 0)
                {
                    goto err;
                }

                /* 14 - xd */
                il_xd = (int *) listentry(header, 14);
                ierr = sci2var(il_xd, Blocks[0].xd, SCSREAL_N); /* double */
                if (ierr != 0)
                {
                    goto err;
                }
            }

            /* 21 - outptr */
            if (Blocks[0].nout != 0)
            {
                il_out = (int *) listentry(header, 21);
                nout = il_out[1];
                for (j = 0; j < nout; j++)
                {
                    il_outptr = (int *) listentry(il_out, j + 1);
                    ierr = sci2var(il_outptr, Blocks[0].outptr[j], Blocks[0].outsz[2 * nout + j]);
                    if (ierr != 0)
                    {
                        goto err;
                    }
                }
            }
        }
        break;

        /*******************************************
         * define property of continuous time states
         * (algebraic or differential states)
         *******************************************/
        case 7 :
        {
            if (Blocks[0].nx != 0)
            {
                /* 40 - x */
                il_xprop = (int *) listentry(header, 40);
                l_xprop = (double *)(il_xprop + 4);
                for (nv = 0; nv < Blocks[0].nx; nv++)
                {
                    Blocks[0].xprop[nv] = (int) l_xprop[nv];
                }
            }
        }
        break;

        /****************************
         * zero crossing computation
         ****************************/
        case 9 :
        {
            /* 33 - g */
            il_g = (int *) listentry(header, 33);
            ierr = sci2var(il_g, Blocks[0].g, SCSREAL_N); /* double */
            if (ierr != 0)
            {
                goto err;
            }

            if (get_phase_simulation() == 1)
            {
                /* 39 - mode */
                il_mode = (int *) listentry(header, 39);
                // Alan, 16/10/07 : fix : mode is an int array
                l_mode = (double *)(il_mode + 4);
                for (nv = 0; nv < (il_mode[1]*il_mode[2]); nv++)
                {
                    Blocks[0].mode[nv] = (int) l_mode[nv];
                }
                //ierr=sci2var(il_mode,Blocks[0].mode,SCSINT_N); /* int */
                //if (ierr!=0) goto err;
            }
        }
        break;

        /**********************
         * Jacobian computation
         **********************/
        case 10 :
        {
            if ((funtyp[kfun - 1] == 10004) || (funtyp[kfun - 1] == 10005))
            {
                /* 15 - res */
                il_res = (int *) listentry(header, 15);
                ierr = sci2var(il_res, Blocks[0].res, SCSREAL_N); /* double */
                if (ierr != 0)
                {
                    goto err;
                }
            }
        }
        break;
    }

    /* Restore initial position Top */
    Top = topsave;
    return;

    /* if error then restore initial position Top
     * and set_block_error with flag -1 */
err:
    Top = topsave;
    if (ierr != 0) /*var2sci or sci2var error*/
    {
        /* Please update me !*/
        if (ierr < 1000) /*var2sci error*/
        {
            switch (ierr)
            {
                case 1  :
                    Scierror(888, _("%s: error %d. Stack is full.\n"), "var2sci", ierr);
                    break;

                case 2  :
                    Scierror(888, _("%s: error %d. No more space on the stack for new data.\n"), "var2sci", ierr);
                    break;

                default :
                    Scierror(888, _("%s: error %d. Undefined error.\n"), "var2sci", ierr);
                    break;
            }
        }
        else /*sci2var error*/
        {
            switch (ierr)
            {
                case 1001  :
                    Scierror(888, _("%s: error %d. Only int or double object are accepted.\n"), "sci2var", ierr);
                    break;

                case 1002  :
                    Scierror(888, _("%s: error %d. Bad double object sub_type.\n"), "sci2var", ierr);
                    break;

                case 1003  :
                    Scierror(888, _("%s: error %d. Bad int object sub_type.\n"), "sci2var", ierr);
                    break;

                case 1004  :
                    Scierror(888, _("%s: error %d. A type of a scilab object has changed.\n"), "sci2var", ierr);
                    break;

                default    :
                    Scierror(888, _("%s: error %d. Undefined error.\n"), "sci2var", ierr);
                    break;
            }
        }
    }
    set_block_error(-1);
}
Ejemplo n.º 2
0
/*--------------------------------------------------------------------------*/
int sci_callblk(char *fname, unsigned long fname_len)
{
    /* auxilary variables -dimension and address-
    * for scilab stack variables
    */
    int *il1 = NULL;
    int m1 = 0, n1 = 0;
    int *il2_1 = NULL;
    int m2_1 = 0, n2_1 = 0;
    int *il2 = NULL;
    int m2 = 0, n2 = 0;
    int *il3 = NULL;
    int m3 = 0, n3 = 0;

    /* local variable */
    int len_str = 0;
    char *str = NULL;
    int ierr = 0, ret = 0;
    int j = 0;
    int TopSave = 0;
    int l_tmp = 0;

    /* length of the scilab list scicos struct */
    int nblklst = 41;

    /* variable for callf */
    scicos_flag flag = 0;
    double t = 0.;

    scicos_block Block;
    memset(&Block, 0, sizeof(scicos_block));

    /* check number of rhs/lhs param */
    CheckRhs(3, 3);
    CheckLhs(1, 1);

    /* check rhs 1 (input scilab structure) */
    il1 = (int *) GetData(1);
    m1  = il1[1];
    n1  = il1[2];
    if (il1[0] != 16)
    {
        Scierror(888, _("%s : First argument must be a scicos_block typed list.\n"), fname);
        return 0;
    }

    il2_1 = (int *) (listentry(il1, 1));
    m2_1  = il2_1[1];
    n2_1  = il2_1[2];
    if ((il2_1[0] != 10) || ((m2_1 * n2_1) != nblklst))
    {
        Scierror(888, _("%s : First argument must be a valid scicos_block typed list.\n"), fname);
        return 0;
    }
    len_str = il2_1[5] - 1;
    if (len_str != 0)
    {
        if ((str = (char *) MALLOC((len_str + 1) * sizeof(char))) == NULL)
        {
            Scierror(888, _("%s: Memory allocation error.\n"), fname);
            return 0;
        }
        str[len_str] = '\0';
        C2F(cvstr)(&len_str, &il2_1[5 + nblklst], str, (j = 1, &j), len_str);
        ret = strcmp("scicos_block", str);
        FREE(str);
        if (ret != 0)
        {
            Scierror(888, _("%s : First argument must be a valid scicos_block typed list.\n"), fname);
            return 0;
        }
    }
    else
    {
        Scierror(888, _("%s : First argument must be a valid scicos_block typed list.\n"), fname);
        return 0;
    }

    /* convert scilab scicos struct to a C scicos struct */
    ret = extractblklist(il1, &Block, &ierr);

    /* error table */
    switch (ierr)
    {
        case -39 :
            Scierror(888, _("%s: Memory allocation error.\n"), fname);
            break;

        case 98  :
            Scierror(888, _("%s : First argument must be a valid scicos_block typed list.\n"), fname);
            break;

        default:
            break;
    }

    if (ierr != 0)
    {
        FREE(Block.z);
        FREE(Block.ozsz);
        FREE(Block.oztyp);
        for (j = 0; j < Block.noz; j++)
        {
            FREE(Block.ozptr[j]);
        }
        FREE(Block.ozptr);
        FREE(Block.x);
        FREE(Block.xd);
        FREE(Block.xprop);
        FREE(Block.res);
        FREE(Block.insz);
        for (j = 0; j < Block.nin; j++)
        {
            FREE(Block.inptr[j]);
        }
        FREE(Block.inptr);
        FREE(Block.outsz);
        for (j = 0; j < Block.nout; j++)
        {
            FREE(Block.outptr[j]);
        }
        FREE(Block.outptr);
        FREE(Block.evout);
        FREE(Block.rpar);
        FREE(Block.ipar);
        FREE(Block.oparsz);
        FREE(Block.opartyp);
        for (j = 0; j < Block.nopar; j++)
        {
            FREE(Block.oparptr[j]);
        }
        FREE(Block.oparptr);
        FREE(Block.g);
        FREE(Block.jroot);
        if (strlen(Block.label) != 0)
        {
            FREE(Block.label);
        }
        FREE(Block.mode);
        if (strlen(Block.uid) != 0)
        {
            FREE(Block.uid);
        }
        return 0;
    }

    /* check rhs 2 (flag) */
    il2 = (int *) GetData(2);
    m2  = il2[1];
    n2  = il2[2];
    if ((il2[0] != 1) || (m2 * n2 != 1))
    {
        Scierror(888, _("%s : Second argument must be scalar.\n"), fname);
        return 0;
    }
    flag = (scicos_flag) * ((double *)(&il2[4]));

    /* check rhs 3 (time) */
    il3 = (int *) GetData(3);
    m3  = il3[1];
    n3  = il3[2];

    if ((il3[0] != 1) || (m3 * n3 != 1))
    {
        Scierror(888, _("%s : Third argument must be scalar.\n"), fname);
        return 0;
    }
    t = *((double *)(&il3[4]));

    /* call block */
    callf(&t, &Block, &flag);

    /* build output  scilab structure */
    TopSave = Top;

    ierr = createblklist(&Block, &ierr, -1, Block.type);

    FREE(Block.z);
    FREE(Block.ozsz);
    FREE(Block.oztyp);
    for (j = 0; j < Block.noz; j++)
    {
        FREE(Block.ozptr[j]);
    }
    FREE(Block.ozptr);
    FREE(Block.x);
    FREE(Block.xd);
    FREE(Block.res);
    FREE(Block.insz);
    for (j = 0; j < Block.nin; j++)
    {
        FREE(Block.inptr[j]);
    }
    FREE(Block.inptr);
    FREE(Block.outsz);
    for (j = 0; j < Block.nout; j++)
    {
        FREE(Block.outptr[j]);
    }
    FREE(Block.outptr);
    FREE(Block.evout);
    FREE(Block.rpar);
    FREE(Block.ipar);
    FREE(Block.oparsz);
    FREE(Block.opartyp);
    for (j = 0; j < Block.nopar; j++)
    {
        FREE(Block.oparptr[j]);
    }
    FREE(Block.oparptr);
    FREE(Block.g);
    FREE(Block.jroot);
    if (strlen(Block.label) != 0)
    {
        FREE(Block.label);
    }
    FREE(Block.mode);
    if (Block.uid != NULL && strlen(Block.uid) != 0)
    {
        FREE(Block.uid);
    }

    Top = TopSave;

    CreateVar(4, TYPED_LIST_DATATYPE, &nblklst, (j = 1, &j), &l_tmp);
    LhsVar(1) = 4;
    PutLhsVar();

    return 0;
}